/*
	shared.js
	v1.0 - 19/dec/2007 - Bob Kersten - Initial version.
	(C) Copyright 2007 Fellownet.
	All rights reserved.
*/

var old_onload = window.onload;

window.onload = function() {
	/*
	 * This function is executed upon loading of the page. If there's an old onload function
	 * present, we need to execute it prior to our own onload function.
	 */

	if (old_onload) old_onload();

	try {
		if (ie6_crap) {
			var __dialogIE = true;
			var images = $A(document.getElementsByTagName('img'));
			images.each( function(img_) {
				name = img_.getAttribute('src');
				if (name.toLowerCase().substring(name.length - 3) == 'png') {
					img_.setAttribute('src', 'images/blank.gif');
					img_.style.cssText = "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + name + "')";
				}
			} );
		}
	} catch(e_) { /* do nothing */ }

	try {
		if (
			$('map') &&
			GBrowserIsCompatible()
		) {
			var map = new GMap2($('map'));
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng('Am Panneschopp 7, D-41334, Nettetal, Germany', function(point_) {
				if (! point_) { } else {
			        map.setCenter(point_, 9);
			        var marker = new GMarker(point_);
			        map.addOverlay(marker);		
				}
				geocoder.getLatLng('Keulse Barriere 3, 5915 PG, Venlo, the Netherlands', function(point_) {
					if (! point_) { } else {
				        var marker = new GMarker(point_);
				        map.addOverlay(marker);		
					}
				} );
			} );
		}
	} catch(e_) { /* do nothing */ }

//	try {
//		var swfobject = new SWFObject("flash/road.swf", "logo", "490", "182", "6", "#FFF");
//		with(swfobject) {
//			addParam("menu", "false");
//			addParam("wmode", "transparent");
//			write("logo");
//		}
//	} catch(e_) { /* do nothing */ }
}

function switchLanguage(language_) {
	new Ajax.Request(
		'xf.language.php', {
			method:'post',
			parameters:'lang=' + language_,
			onComplete:function(req_) {
				top.location.href = top.location.href;
			}
		}
	);
	return false;
}

function sendForm(form_, target_, button_, onSuccess_) {
	/**
	 * This is a generic function to submit forms and parse the results.
	 */	
	var fields = $A(document.getElementsByClassName('field_error'));
	fields.each( function(el_) {
		Element.removeClassName(el_, 'field_error');
	} );
	if ($(button_)) $(button_).style.visibility = 'hidden';

	// This is the first step in the registration process. Let's validate all the
	// supplied data before we continue.
	new Ajax.Request(
		target_, {
			method:'post',
			parameters:Form.serialize(form_),
			onComplete:function(req_) {
				var results = req_.responseText.toQueryParams();
				if (results['result'] && results['result'] != 'OK') {
					var errors = $A(results['fields'].split(';'));
					errors.each( function(el_) {
						if ($(el_)) Element.addClassName(el_, 'field_error');
					} );
					dialog.open( {
						content:results['content'],
						title:results['title'],
						buttons:'Sluiten',
						width:320,
						duration:0,
						callback:function(action_, dialog_) {
							try {
								if ($(button_)) $(button_).style.visibility = 'visible';
								dialog_.close();
								if (false != (field = $(errors[0]))) field.focus();
							} catch(e_) { /* do nothing */ }
						}
					} );
				} else if (results['result'] && results['result'] == 'OK') {
					if (onSuccess_) onSuccess_(results);
				} else {
					if ($(button_)) $(button_).style.visibility = 'visible';
					alert(req_.responseText);
				}
			}
		}
	);
}

function onContactFormSubmit(button_) {
	sendForm('form_contact', 'xf.contact.php', button_, function(results_) {
		dialog.open( {
			content:results_['content'],
			title:results_['title'],
			buttons:'Sluiten',
			width:320,
			duration:0
		} );
	} );
}