/*******************
 *
 * Custom AfterOffice Javascript
 *
 * @author: Wido van Heemstra
 * @copyright: AfterOffice 2007
 *
 */


function process (resp) {if (resp) {eval(resp.responseText);}}

function showAddress (id, address) {
	// id: placeholder
	var map = new GMap2(document.getElementById("ao_map"));
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				map.addControl(new GSmallMapControl());
				//marker.openInfoWindowHtml(address);
			}
		}
	);
}

function displayMap (id, lat, lng) {
	// id: placeholder
	var map = new GMap2(document.getElementById(id));
	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point);
	map.setCenter (point, 13);
	map.addOverlay(marker);
	map.addControl(new GSmallMapControl());
}

function setfilter (formid, elementname, value) {
	document.getElementsByName(elementname)[0].value = value;
	document.getElementById(formid).submit();
}

function setzipcode (form_id, zip_name, rad_name) {
	// Get zip code
	var zipcode = document.getElementsByName(zip_name)[0].value;
	// Check zip code on format
	if (zipcode == '') {
		alert ('Postcode is niet ingevuld.');
		return false;
	}
	
	if (zipcode.match(/^[\d]{4}[\s]?[a-zA-Z]{2}$/)) {
		// Get radius to be searched in
		var dropdownIndex = document.getElementsByName(rad_name)[0].selectedIndex;
		var radius = document.getElementsByName(rad_name)[0][dropdownIndex].value;
		if (radius == -1) {
			alert ('Straal is niet ingevuld.');
			return false;
		}
		/*var address = zipcode;
		var geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode('nl');
		geocoder.getLatLng (
			address,
			function(point) {
				if (!point) { //Address has not been found
					alert ('Helaas, de postcode kon niet gevonden worden.');
					return false;
				} else {
					document.getElementsByName(coord_name)[0].value = point.lat() + ',' + point.lng();
					document.getElementById(form_id).submit();
					return true;
				}
			}
		);*/
		return true;
	} else {
		alert ('Vul een geldige postcode in.');
		return false;
	}
}

function submit (form_id, zip_name, rad_name) {
	if (setzipcode(form_id, zip_name, rad_name))
		document.getElementById(form_id).submit();
}

function setClick (comp_id, source) {
	new Ajax.Request('/setclick/'+comp_id+'/'+source, { asynchronous: true });
}

function phoneClick(comp_id, container1_id, container2_id){
	// compid-id: id of Company to be registrated
	// container1-id: id of DIV that is clicked
	// container2-id: id of DIV to be shown
	$(container2_id).className='info';
	$(container1_id).className='caption';
	$(container1_id).onclick='';
	setClick(comp_id, 'phone');
}

function addressClick(comp_id, container1_id, container2_id){
	// compid-id: id of Company to be registrated
	// container1-id: id of DIV that is clicked
	// container2-id: id of DIV to be shown
	$(container2_id).className='info';
	$(container1_id).className='caption';
	$(container1_id).onclick='';
	setClick(comp_id, 'address');
}

function urlClick(comp_id){
	// compid-id: id of Company to be registrated
	setClick(comp_id, 'url');
}

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

function setLoader (DIVID) {
	div = document.getElementById(DIVID);
	div.innerHTML = "<br /><img src='images/loader_blue.gif' alt='loading' />";
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
