/* 
 * cpfurnitures.com
 * functions.js
 */

/* utility functions
----------------------------------------------- */
function switchAll(state) {
	var all = document.getElementsByTagName('input');
	for (var i = 0; i < all.length; i++) { 
		var type = all[i].getAttribute('type'); 
		if (type == 'checkbox') {
			all[i].checked = state;
		} else if ((type == 'hidden') && (all[i].getAttribute('name') == 'rights[all][all]')) {
			all[i].value = state;
		}
	}
}

function checkState(element) {
	if (element.checked == false) {
		var all = document.getElementsByTagName('input');
		for (var i = 0; i < all.length; i++) { 
			var type = all[i].getAttribute('type');
			var name = all[i].getAttribute('name');
			 if ((type == 'hidden') && (name == 'rights[all][all]')) {
				all[i].value = 'false';
			}
		}			
	}
}

function externalLinks() {
   if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
         anchor.target = "_blank";
   }
}

function goToURL(selectbox)
{
	window.location.href='/admin/pages/edit/?id='+selectbox.options[selectbox.selectedIndex].value;
}

function URLEncode(str)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < str.length; i++ ) 
	{
		var ch = str.charAt(i);
		if (ch == " ") {
			encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '" 
						+ ch 
						+ "' cannot be encoded using standard URL encoding.\n" +
						  "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

/* google maps
----------------------------------------------- */
function loadZ() 
{
	var html = '<p><strong>C&amp;P Furnitures<\/strong><br\/>Langendijkstraat 3, B-3690 Zutendaal<\/p><p><input type="text" id="start" style="width:250px;" \/><br\/><span>bv. Groenplein 1, 3500 Hasselt, Belgium<\/span><\/p><p><input type="button" onclick="getRouteZ()" value="Plan route" /><\/p>';
			   
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(50.918060, 5.540234), 13);
		// Place a marker in the center of the map and open the info window automatically
		var marker = new GMarker(map.getCenter());
		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(html);
		});
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
	}
}

function loadI() 
{
	var html = '<p><strong>C&amp;P Furnitures<\/strong><br\/>Meenseweg 167, B-8900 Ieper<\/p><p><input type="text" id="start" style="width:250px;" \/><br\/><span>bv. Groenplein 1, 3500 Hasselt, Belgium<\/span><\/p><p><input type="button" onclick="getRouteI()" value="Plan route" /><\/p>';
			   
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(50.849904, 2.905222), 13);
		// Place a marker in the center of the map and open the info window automatically
		var marker = new GMarker(map.getCenter());
		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(html);
		});
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
	}
}

function loadL() 
{
	var html = '<p><strong>C&amp;P Furnitures<\/strong><br\/>Route de Luxembourg 55b, L-4972 Dippach<\/p><p><input type="text" id="start" style="width:250px;" \/><br\/><span>bv. Groenplein 1, 3500 Hasselt, Belgium<\/span><\/p><p><input type="button" onclick="getRouteI()" value="Plan route" /><\/p>';
			   
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(49.58818,5.978622), 13);
		// Place a marker in the center of the map and open the info window automatically
		var marker = new GMarker(map.getCenter());
		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml(html);
		});
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
	}
}

function hideMap()
{
	document.getElementById('contactform').className = 'show';
	document.getElementById('map').className = 'hide';
	document.getElementById('iep').className = 'hide';
	document.getElementById('lux').className = 'hide';
	document.getElementById('zut').className = 'hide';
	GUnload();
}

function showMapZ()
{
	document.getElementById('contactform').className = 'hide';
	document.getElementById('iep').className = 'hide';
	document.getElementById('lux').className = 'hide';
	document.getElementById('zut').className = 'show';
	document.getElementById('map').className = 'show';
  	loadZ();
}

function showMapI()
{
	document.getElementById('contactform').className = 'hide';
	document.getElementById('iep').className = 'show';
	document.getElementById('lux').className = 'hide';
	document.getElementById('zut').className = 'hide';
	document.getElementById('map').className = 'show';
  	loadI();
}

function showMapL()
{
	document.getElementById('contactform').className = 'hide';
	document.getElementById('iep').className = 'hide';
	document.getElementById('lux').className = 'show';
	document.getElementById('zut').className = 'hide';
	document.getElementById('map').className = 'show';
  	loadL();
}

function getRouteZ()
{
	var daddr = 'Langendijkstraat+3,+3690+Zutendaal,+Belgium';
	var saddr = URLEncode(document.getElementById("start").value);
	window.open('http://maps.google.com/maps?saddr='+saddr+'&daddr='+daddr, '_blank');
}

function getRouteI()
{
	var daddr = 'Meenseweg+167,+8900+Ieper,+Belgium';
	var saddr = URLEncode(document.getElementById("start").value);
	window.open('http://maps.google.com/maps?saddr='+saddr+'&daddr='+daddr, '_blank');
}

function getRouteL()
{
	var daddr = 'Route+de+Luxembourg+55b,+L-4972+Dippach,+Luxemburg';
	var saddr = URLEncode(document.getElementById("start").value);
	window.open('http://maps.google.com/maps?saddr='+saddr+'&daddr='+daddr, '_blank');
}

window.onload = externalLinks;