// get the XmlHttp Object as pe the client browser

var xmlCityHttp
var selectedCity

function GetCityXmlHttpObject()
{
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlCityHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlCityHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlCityHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlCityHttp;
}

function getCity(){
if(document.forms[0].state.value==-1){
document.forms[0].city.options[1].selected = true;
}
getCity(0);
}

function getCity(cityId){

selectedCity = cityId
stateid = (document.forms[0].state.value);
xmlCityHttp=GetCityXmlHttpObject();
if (xmlCityHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
		xmlCityHttp.open("POST","citylist.asp",true);
		xmlCityHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlCityHttp.send('stateid='+stateid);
		xmlCityHttp.onreadystatechange=stateChanged;
}


function stateChanged() 
{ 
	if (xmlCityHttp.readyState==4)
	{
		var responseText = xmlCityHttp.responseText;
		//alert(responseText)
		if (responseText != null || responseText !=""){
		populateCity(responseText);
		}
	}
}


function populateCity(allCities){
if (allCities != null){
city=allCities.split(",");
//document.courseform.city.options.length=null;
removeCityOptions();
selecbox = document.forms[0].city;
addOption(selectbox,"Choose your City","0");
for (var i = 0; i < city.length-1 ; i++ )
{
	temp = city[i];
	city_details = temp.split(":");
	cityId = city_details[0];
	cityName = city_details[1];
	addOption(selectbox,cityName,cityId);
}
}
}

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
if(selectedCity == value){
optn.selected = true;
}
selectbox.options.add(optn);
}

function removeCityOptions()
{

selectbox=document.forms[0].city;
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
	selectbox.remove(i);
}
}

function reload(){
document.forms[0].stateName.value = document.forms[0].state.options[document.forms[0].state.selectedIndex].text;
document.forms[0].stateId.value = document.forms[0].state.value;
document.forms[0].cityName.value = document.forms[0].city.options[document.forms[0].city.selectedIndex].text;
document.forms[0].submit();
}

function checkAndReload(){
document.forms[0].stateName.value = document.forms[0].state.options[document.forms[0].state.selectedIndex].text;
document.forms[0].stateId.value = document.forms[0].state.value;
document.forms[0].cityName.value = document.forms[0].city.options[document.forms[0].city.selectedIndex].text;
if(document.forms[0].state.value=="0" ){
	alert("Please select State to proceed");
}else if(document.forms[0].city.value=="0"){
	alert("Please select City to proceed");
}else{
document.forms[0].submit();
}
}
