function createRequestObject()
{
	request_o = false;
	if (window.XMLHttpRequest){
		var request_o = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try {
		request_o = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   request_o = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
	}
	
	return request_o;	
}

var http = createRequestObject();

function getCities()
{
	map.clearOverlays();	
	document.getElementById("data").innerHTML = "";
	document.getElementById("data").style.border = '0px solid #000000';
	document.getElementById("selectCity").options[0] = new Option('[ Loading cities... ]','');
	document.getElementById("selectCity").options[0].selected = true;
	document.getElementById("progress").style.display = 'block';
	setTimeout("getCities2()", 1000);
}

function getCities2()
{	
	selected = document.getElementById("selectCountry").selectedIndex;
	countryCode = document.getElementById("selectCountry").options[selected].value;	
	http.open('post', 'inc/process.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.send('country=' + countryCode + '&app=1');
	http.onreadystatechange = handleReturnData;
}

function getStores()
{
	map.clearOverlays();
	document.getElementById("data").innerHTML = "";
	document.getElementById("data").style.border = '0px solid #000000';
	document.getElementById("progress").style.display = 'block';
	setTimeout("getStores2()", 1000);
}

function getStores2()
{
	selected = document.getElementById("selectCity").selectedIndex;
	cityName = document.getElementById("selectCity").options[selected].value;
	selected = document.getElementById("selectCountry").selectedIndex;
	countryCode = document.getElementById("selectCountry").options[selected].value;	
	http.open('POST', 'inc/process.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.send('city=' + cityName + '&country=' + countryCode + '&app=2');
	http.onreadystatechange = handleReturnData2;	
}
	

function handleReturnData()
{		
	if(http.readyState == 4) //Finished loading the response
	{		
		if (http.responseText.indexOf('invalid') == -1)
		{
			var XMLResponse = http.responseXML;
			
			myNodes = XMLResponse.getElementsByTagName("name");
					
			myNodesValue = XMLResponse.getElementsByTagName("value");
			
			coordsCountry = XMLResponse.getElementsByTagName("coords");
			
			if (coordsCountry.item(0).firstChild.nodeValue != "-")
			{			
				coordsTotal = coordsCountry.item(0).firstChild.nodeValue;
				
				var coords = coordsTotal.split(",");								
				if (coords[0] != "-")
				{
					var longi_country = coords[0];
					var lati_country = coords[1];
					var point_country = new GLatLng(lati_country, longi_country);
					map.setZoom(5);			
					map.panTo(point_country);
				}
			}
			else
			{
				map.setCenter(new GLatLng(52, 4), 4);
			}
			
			var content = "Select City:<br /><select name=\"selectCity\" id=\"selectCity\" style=\"width:200px;\" onchange=\"javascript:getStores(this.value);\"><option value=\"\">- Select a city -</option>\n";
											
			for(var counter=0;counter<myNodes.length;counter++)
			{
				content += "<option value=\"" + myNodesValue.item(counter).firstChild.nodeValue + "\">" + myNodes.item(counter).firstChild.nodeValue + "</option>\n";
			}
			
			content += "</select>";
			
			document.getElementById("select_city").innerHTML = content;
			
			document.getElementById("progress").style.display = 'none';
							
		}			
	}
}

function handleReturnData2()
{		
	if(http.readyState == 4) //Finished loading the response
	{
		//alert(navigator.appName);
		if (http.responseText.indexOf('invalid') == -1)
		{
			selected = document.getElementById("selectCountry").selectedIndex;
			var countryCode = document.getElementById("selectCountry").options[selected].value;
			
			var XMLResponse = http.responseXML;
			
			var array_stores = new Array();
			
			var y = XMLResponse.getElementsByTagName("data");
			
			for (i=0;i<y.length;i++)
			{
				if (navigator.appName == "Netscape")
				{					
					var city = y[i].childNodes[1].firstChild.nodeValue;
					var cords = city.split(",");
									
					if (cords[0] != "-")
					{
						var longi_city = cords[0];
						var lati_city = cords[1];
						var point_city = new GLatLng(lati_city, longi_city);
					}
					else
					{
						var point_city = "";
					}					
				}
				else
				{
					var city = y[i].childNodes[0].firstChild.nodeValue;
					var cords = city.split(",");
					
					if (cords[0] != "-")
					{
						var longi_city = cords[0];
						var lati_city = cords[1];
						var point_city = new GLatLng(lati_city, longi_city);
					}
					else
					{
						var point_city = "";
					}										
				}
			}		
			
			var x = XMLResponse.getElementsByTagName("customer");				
					
			var temp_content = '';
			
			var tellertje = 0;
			var array_coords = new Array();
			var array_names = new Array();
						
			for (i=0;i<x.length;i++)
			{
				if (navigator.appName == "Netscape")
				{					
					var coords = '';
					var content = '';
					var display = '';
					
					if (x[i].childNodes[13].firstChild.nodeValue != "-")
					{
						coords = x[i].childNodes[13].firstChild.nodeValue.split(",");
						array_coords[tellertje] = x[i].childNodes[13].firstChild.nodeValue;						
					}				
					content += "<td bgcolor=\"#FFFFFF\" width=\"200\" style=\"border:1px solid #000000;\">";
					display += "<td width=\"200\">";
					content += "<span style=\"font-weight: bold;\">";
					display += "<span style=\"font-weight: bold;\">";
					content += x[i].childNodes[1].firstChild.nodeValue; // Naam
					display += x[i].childNodes[1].firstChild.nodeValue; // Naam
					content += "</span>";
					display += "</span>";					
					content += "<br />";
					display += "<br />";
					content += "Phone: ";
					display += "Phone: ";
					content += x[i].childNodes[3].firstChild.nodeValue; // Telefoonnummer
					display += x[i].childNodes[3].firstChild.nodeValue; // Telefoonnummer					
					content += "<br />";
					display += "<br />";
					content += x[i].childNodes[5].firstChild.nodeValue; // Straat
					display += x[i].childNodes[5].firstChild.nodeValue; // Straat
					content += "<br />";
					display += "<br />";
					content += x[i].childNodes[7].firstChild.nodeValue; // City
					display += x[i].childNodes[7].firstChild.nodeValue; // City
					content += ", ";
					display += ", ";
					if (x[i].childNodes[9].firstChild.nodeValue != "-")
					{
						content += x[i].childNodes[9].firstChild.nodeValue; // ZIP
						display += x[i].childNodes[9].firstChild.nodeValue; // ZIP
						content += ", ";
						display += ", ";
					}						
					content += x[i].childNodes[11].firstChild.nodeValue; // Country
					display += x[i].childNodes[11].firstChild.nodeValue; // Country
					if (coords != '')
					{
						var longi = coords[0];
						var lati = coords[1];
						content += "<br /><br /><a href=\"javascript:go("+lati+","+longi+","+tellertje+");\"><img src=\"http://jibbitz.crocs.eu/images/universal/map.jpg\" alt=\"View on map!\" border=\"0\"/> View on map!</a>";
					}
					else
					{
						content += "<br /><br /><img src=\"http://jibbitz.crocs.eu/images/universal/map_no.jpg\" alt=\"Location not found!\" border=\"0\"/> Location not found!";
					}
					content += "</td>";
					display += "</td>";
					
					var display_final = ''
					
					if (x[i].childNodes[13].firstChild.nodeValue != "-")
					{
						display_final += "<table><tr>" + display + "</tr></table>";
						
						array_names[tellertje] = display_final;
						tellertje++;
					}					
					
					if ((i+1) % 4 == 0)
					{
						content += "</tr>";							
					}
					
					temp_content += content;					
					
				}
				else
				{					
					var coords = '';
					var content = '';
					var display = '';
					
					if (x[i].childNodes[6].firstChild.nodeValue != "-")
					{
						coords = x[i].childNodes[6].firstChild.nodeValue.split(",");
						array_coords[tellertje] = x[i].childNodes[6].firstChild.nodeValue;						
					}				
					content += "<td bgcolor=\"#FFFFFF\" width=\"200\" style=\"border:1px solid #000000;\">";
					display += "<td width=\"200\">";
					content += "<span style=\"font-weight: bold;\">";
					display += "<span style=\"font-weight: bold;\">";
					content += x[i].childNodes[0].firstChild.nodeValue; // Naam
					display += x[i].childNodes[0].firstChild.nodeValue; // Naam
					content += "</span>";
					display += "</span>";					
					content += "<br />";
					display += "<br />";
					content += "Phone: ";
					display += "Phone: ";
					content += x[i].childNodes[1].firstChild.nodeValue; // Telefoonnummer
					display += x[i].childNodes[1].firstChild.nodeValue; // Telefoonnummer					
					content += "<br />";
					display += "<br />";
					content += x[i].childNodes[2].firstChild.nodeValue; // Straat
					display += x[i].childNodes[2].firstChild.nodeValue; // Straat
					content += "<br />";
					display += "<br />";
					content += x[i].childNodes[3].firstChild.nodeValue; // City
					display += x[i].childNodes[3].firstChild.nodeValue; // City
					content += ", ";
					display += ", ";
					if (x[i].childNodes[4].firstChild.nodeValue != "-")
					{
						content += x[i].childNodes[4].firstChild.nodeValue; // ZIP
						display += x[i].childNodes[4].firstChild.nodeValue; // ZIP
						content += ", ";
						display += ", ";
					}						
					content += x[i].childNodes[5].firstChild.nodeValue; // Country
					display += x[i].childNodes[5].firstChild.nodeValue; // Country
					if (coords != '')
					{
						var longi = coords[0];
						var lati = coords[1];
						content += "<br /><br /><a href=\"javascript:go("+lati+","+longi+","+tellertje+");\"><img src=\"http://jibbitz.crocs.eu/images/universal/map.jpg\" alt=\"View on map!\" border=\"0\"/> View on map!</a>";
					}
					else
					{
						content += "<br /><br /><img src=\"http://jibbitz.crocs.eu/images/universal/map_no.jpg\" alt=\"Location not found!\" border=\"0\"/> Location not found!";
					}
					content += "</td>";
					display += "</td>";
					
					var display_final = ''
					
					if (x[i].childNodes[6].firstChild.nodeValue != "-")
					{
						display_final += "<table><tr>" + display + "</tr></table>";
						
						array_names[tellertje] = display_final;
						tellertje++;
					}					
					
					if ((i+1) % 4 == 0)
					{
						content += "</tr>";										
					}
					
					temp_content += content;
				}
			}		
			
			var final_content = "<table bgcolor=\"#CCCCCC\" border=\"0\" width=\"800\" cellspacing=\"2\" cellpadding=\"5\" style=\"border: 1px solid #000000;\"><tr><td colspan=\"3\" style=\"font-size:18px; font-weight:bold;\">Found specialists</td></tr><tr>" + temp_content + "</table>";
						
			document.getElementById("data").innerHTML = final_content;
			//document.getElementById("data").style.border = '1px solid #000000';
			
			document.getElementById("progress").style.display = 'none';
					
			add_marker(point_city, array_coords, array_names);							
		}			
	}
}