
var http = createRequestObject();

function createRequestObject() {  
	// find the correct xmlHTTP, works with IE, FF and Opera
	var xmlhttp;
	try {
  	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e) {
    try {
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e) {
    	xmlhttp=null;
    }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
  	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function sendRequest(call_to_function) {
	try{
		switch(call_to_function){
		   case "handleResponse1" :
									var countryId = document.getElementById("country").value;
									http.open("GET", "getstates.php?cid="+countryId, true);
									http.setRequestHeader('Content-Type',  "text/xml");
						    	    http.onreadystatechange = handleResponse1;
								    break;
		   case "handleResponse2" :
									var countryId = document.getElementById("country_shipping").value;
									http.open("GET", "getstates.php?cid="+countryId, true);
									http.setRequestHeader('Content-Type',  "text/xml");
						    	    http.onreadystatechange = handleResponse2;
								    break;

        }//end of switch
		http.send(null);
	}
	catch(e){
		// caught an error
		alert('Request send failed.');
	}
}

function handleResponse1() {

	try{
    if((http.readyState == 4)&&(http.status == 200)){
    	var response = http.responseXML.documentElement;
		var _data = response.getElementsByTagName('stateDetails');
		document.getElementById('state').length="";
		if(_data.length == 0){
            //Hide State Combo List
			document.getElementById('state').style.display = "none";
			//Show Text Box For State Combo List
			document.getElementById('state_province').style.display = "block";
			
			document.getElementById('state').options[0] = new Option("No State Avilable available",'1');	
		}//end of if
		else{
            //Show State Combo List
			document.getElementById('state').style.display = "block";
			//Hide Text Box For State Combo List
			document.getElementById('state_province').style.display = "none";
			
		}//end of else   
		var i
		for ( i = 0 ; i < _data.length ; i ++ ){
		
			document.getElementById('state').options[i] = new Option(response.getElementsByTagName('stateName')[i].firstChild.data,response.getElementsByTagName('id')[i].firstChild.data);	
		}//end of for
		
	}//end of if
  }
	catch(e){
		// caught an error
		alert('Response failed.');
	}
	finally{}
}



function handleResponse2() {

	try{
    if((http.readyState == 4)&&(http.status == 200)){
    	var response = http.responseXML.documentElement;
		var _data = response.getElementsByTagName('stateDetails');
		document.getElementById('state_shipping').length="";
		if(_data.length == 0){
            //Hide State Combo List
			document.getElementById('state_shipping').style.display = "none";
			//Show Text Box For State Combo List
			document.getElementById('state_province_shipping').style.display = "block";
			
			document.getElementById('state_shipping').options[0] = new Option("No State Avilable available",'1');	
		}//end of if
		else{
            //Show State Combo List
			document.getElementById('state_shipping').style.display = "block";
			//Hide Text Box For State Combo List
			document.getElementById('state_province_shipping').style.display = "none";
			
		}//end of else   
		var i
		for ( i = 0 ; i < _data.length ; i ++ ){
		
			document.getElementById('state_shipping').options[i] = new Option(response.getElementsByTagName('stateName')[i].firstChild.data,response.getElementsByTagName('id')[i].firstChild.data);	
		}//end of for
		
	}//end of if
  }
	catch(e){
		// caught an error
		alert('Response failed.');
	}
	finally{}
}

