// JavaScript Document

function getCities(state,citySelect)
{

	
	//empty the city select
	var groupOptions = document.getElementById(citySelect);
	
	// Empty the second drop down box of any choices
	for(var q=groupOptions.options.length; q>=0 ; q--)
	{
		groupOptions.options[q]=null;
	}
	
	// add the default option to the city select
	var myOption = document.createElement("option") ;
  	myOption.value = "" ;
  	myOption.text = "-- select city --";
  	if(navigator.appName=='Netscape')
  		groupOptions.appendChild(myOption);
	else
		groupOptions.add(myOption);
	myOption = null;
	
	var url = "getCities.php?state=" + state;
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() 
	{ 
		if (http_request.readyState == 4) 
		{
			if (http_request.status == 200) 
			{
				var options = http_request.responseText;
				var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(options);
				
				var optionNodes = xmlDoc.selectNodes("//option");
				
				for (var i = 0; i<optionNodes.length; i++)
				{
					var tempOption = document.createElement("option");
					tempOption.text = optionNodes[i].text;
					tempOption.value = optionNodes[i].getAttribute("value");
					
					if(navigator.appName=='Netscape')
				  		groupOptions.appendChild(tempOption);
					else
						groupOptions.add(tempOption);
						
					tempOption = null;	 
				}
				
				if (state != "" && state != null)
				{
					var tempOption = document.createElement("option");
					tempOption.value = "add city" ;
			  		tempOption.text = "-- add city --";
			  		if(navigator.appName=='Netscape')
				  		groupOptions.appendChild(tempOption);
					else
						groupOptions.add(tempOption);
			  		tempOption = null; 
				}
			} 
			else 
			{
				alert('There was a problem with the change Options request.');
			}
		} 
	};
	http_request.open('GET', url, true);
	http_request.send(null);	
}

function checkNewCity(selectValue)
{
	if (selectValue == "add city")
	{
		var newcitydiv = document.getElementById("newcitydiv");
		newcitydiv.style.display = 'block';
	}
	else
	{
		var newcitydiv = document.getElementById("newcitydiv");
		newcitydiv.style.display = 'none';
		
		var newcity = document.getElementById("newcity");
		newcity.value = "";
	}
}

function hideCity()
{

	var newcitydiv = document.getElementById("newcitydiv");
	newcitydiv.style.display = 'none';
	
	var newcity = document.getElementById("newcity");
	newcity.value = "";
}
