var xmlhttp;
function getCategories(key,catname)
{
	switch(key)
	{
		case 1:
		try{document.getElementById('searchBanner').style.display = 'block';}catch(e){}
		document.getElementById('cat_2').innerHTML = '';
		document.getElementById('cat_3').innerHTML = '';
		break;
		
		case 2:
		try{document.getElementById('searchBanner').style.display = 'none';}catch(e){}
		document.getElementById('cat_3').innerHTML = '';
		break;
		
		case 3:
		try{document.getElementById('searchBanner').style.display = 'none';}catch(e){}
		document.getElementById('cat_3').innerHTML = '';
		break;
		
	}
	if(catname==''){return;}

	var url = 'products.php?c='+key+'&n='+URLencode(catname);
	
	
	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4)
			{
				// if "OK"
				//alert(xmlhttp.responseText);
				if (xmlhttp.status==200)
				{
					if(xmlhttp.responseText)
					{
						var html = '<label for="category_'+key+'">Category '+key+'</label>'+xmlhttp.responseText;
						document.getElementById('cat_'+key).innerHTML = html;
					}
					else
					{
						document.getElementById('cat_'+key).innerHTML = '';
						document.searchForm.submit();
					}
				}
				else
				{
					alert("Problem retrieving XML data")
				}
			}
		}
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
}
var popup;
function openPopup(url,width,height)
{
	popup = window.open(url,null,'height='+height+',width='+width);
	if(popup){popup.focus();}
}
function URLencode(n){
	
	var encoded = "";

    var HEX = "0123456789ABCDEF";

    var UNSAFECHARS = '<>"#%{}|^~[];/?:@=&';

    for (var i = 0; i < n.length; i++ ) {

    	var ch = n.charAt(i);

    	if (ch == " ") {
		encoded += "+";	// x-www-urlencoded, rather than %20
    	}
    	else if (UNSAFECHARS.indexOf(ch) != -1) {

			var charCode = ch.charCodeAt(0);
			encoded += "%";
            encoded += HEX.charAt((charCode >> 4) & 0xF);
            encoded += HEX.charAt(charCode & 0xF);

			}

		else{
			//it’s safe
			encoded += ch;
    	}

    } // for

    return encoded;
}





/**
 * Google Map functions
 */
var MAP;
var START_ZOOM = 6;
var START_LAT = -3.42;
var START_LNG = 54.16;
var DISTRIBUTORS = [];
function mapInit() 
{
	if (GBrowserIsCompatible()) 
	{
		MAP = new GMap2(document.getElementById("map_canvas"));
		MAP.addControl(new GLargeMapControl());
		MAP.addControl(new GMapTypeControl());
		MAP.setCenter(new GLatLng(START_LNG,START_LAT),START_ZOOM);
		MAP.setMapType(G_HYBRID_MAP);
		for(var i in DISTRIBUTORS)
		{
			MAP.addOverlay(createMarker(DISTRIBUTORS[i]));
			//addToDistributorList(DISTRIBUTORS[i]);
		}
	}
}
function createMarker(distributor)
{	
	var point = new GLatLng(distributor.LAT,distributor.LNG);
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function()
	{		
		marker.openInfoWindowHtml('<strong>'+distributor.NAME+'</strong><br/>'+distributor.ADDRESS);
	});
	return marker;
}
function addToDistributorList(distributor)
{
	document.getElementById('col_2').innerHTML+='<p><strong>'+distributor.NAME+'</strong></p>';
}