function getXMLHTTPRequest() 
{
	try 
    {
     	req = new XMLHttpRequest();
    } 
	catch(err1) 
	{
      	try 
		{
           req = new ActiveXObject("Msxml2.XMLHTTP");
        } 
		catch(err2) 
		{
           try 
		   {
              req = new ActiveXObject("Microsoft.XMLHTTP");
           }
		   catch (err3)
		   {
              req = false;
           }
        }
    }
	return req;
}
var http = getXMLHTTPRequest();
var id_pr;
var id_val;
var id_cou;					  


function useHttpResponse()
{
	if (http.readyState == 4) 
	{
		document.getElementById('box').innerHTML = '';
	 	if(http.status == 200) 
		{
		    var response = http.responseText;
			var cities = response.split('|');
			removeAllOptions(document.getElementById('province_change'));
			removeAllOptions(document.getElementById('city_change'));
			addOption(document.getElementById('province_change'),'all', 'Select')
			//addOption(document.getElementById('province_change'),'all', 'Select All')
			for (i=1; i<cities.length; i++) {
				var sep = cities[i].split('~');
				addOption(document.getElementById('province_change'),sep[0], sep[1])
		    }
		    
      	}
	}
	else
	{
 		//document.getElementById('box').innerHTML = '<img src="javascript/loading.gif" />';
	}
	
}

function fillCity()
{
	if (http.readyState == 4) 
	{
		document.getElementById('box1').innerHTML = '';
	 	if(http.status == 200) 
		{
		    var response = http.responseText;
			var cities = response.split('|');
			removeAllOptions(document.getElementById('city_change'));
			//addOption(document.getElementById('city_change'),'all', 'Select City')
			addOption(document.getElementById('city_change'),'all', 'Search All')
			for (i=1; i<cities.length; i++) {
				var sep = cities[i].split('~');
				addOption(document.getElementById('city_change'),sep[0], sep[1])
		    }
		    document.getElementById('city_change').options[0].selected = true;
      	}
	}
	else
	{
 		//document.getElementById('box1').innerHTML = '<img src="javascript/loading.gif" />';
	}
	
}

function useHttpResponseLoad()
{
	if (http.readyState == 4) 
	{
		document.getElementById('box').innerHTML = '';
	 	if(http.status == 200) 
		{
		    var response = http.responseText;
			var cities = response.split('|');
			removeAllOptions(document.getElementById('province_change'));
			removeAllOptions(document.getElementById('city_change'));
			addOption(document.getElementById('province_change'),'all', 'Select')
			//addOption(document.getElementById('province_change'),'all', 'Select All')
			for (i=1; i<cities.length; i++) {
				var sep = cities[i].split('~');
				addOption(document.getElementById('province_change'),sep[0], sep[1])
		    }
		    var selectBox= document.getElementById('province_change');
		    var test_var = 1;
   			for (var no = 0; no < selectBox.options.length; no++)
   			{
       			if (selectBox.options[no].value == id_pr)
	   			{
            		selectBox.selectedIndex = no;
            		if(id_pr || id_pr != null)
            		{
            			changeCity('city_change',id_pr,'canada');
            		}
            		test_var = 2;
            		break;
            		
       			}
       			      			
       			
   			}
   			if(test_var == 1)
   			{
				addOption(document.getElementById('city_change'),'all', 'Select');
			}
   			
   		}
	}
	else
	{
 		//document.getElementById('box').innerHTML = '<img src="javascript/loading.gif" />';
	}
	
}

function useHttpResponseLoad1()
{
	if (http.readyState == 4) 
	{
		document.getElementById('box').innerHTML = '';
	 	if(http.status == 200) 
		{
		    var response = http.responseText;
			var cities = response.split('|');
			removeAllOptions(document.getElementById('province_change'));
			removeAllOptions(document.getElementById('city_change'));
			addOption(document.getElementById('province_change'),'all', 'Select');
			//addOption(document.getElementById('province_change'),'all', 'Select All');
			for (i=1; i<cities.length; i++) {
				var sep = cities[i].split('~');
				addOption(document.getElementById('province_change'),sep[0], sep[1])
		    }
		    var selectBox= document.getElementById('province_change');
		   	for (var no = 0; no < selectBox.options.length; no++)
   			{
       			if (selectBox.options[no].value == id_pr)
	   			{
	   				
            		selectBox.selectedIndex = no;
            		if(id_pr == 'all' || !id_pr || id_pr == null)
					{
						removeAllOptions(document.getElementById('city_change'));
						addOption(document.getElementById('city_change'),'all', 'Select');	
					}
					else
					{
            			changeCity('city_change',id_pr,'canada');
            		}	
            		break;
       			}
   			}
   			if(id_pr != 'all'){
            setTimeout ("document.getElementById('city_change').options[0].selected = false;var selectBox= document.getElementById('city_change');var sep = id_val.split('~'); for(var i=0;i < sep.length ;i++){ for (var no = 0; no < selectBox.options.length; no++){if (selectBox.options[no].value == sep[i]){selectBox.options[no].selected = true;break;}}}"
			, 2400 ); 
			}
   		}
	}
	else
	{
 		//document.getElementById('box').innerHTML = '<img src="javascript/loading.gif" />';
	}
	
}		  


function changeProvince(id,value)
{
	var myurl = 'index2.php';
	var modurl = myurl+"?com_route=country&value="+value+"&id="+id;
	http.open("GET", modurl, true);
	http.onreadystatechange = useHttpResponse;
	http.send(null); 
}

function changeCity(id,value,country)
{
	
	if(value == 'all' || !value || value == null)
	{
		removeAllOptions(document.getElementById('city_change'));
		addOption(document.getElementById('city_change'),'all', 'Select');
	}
	else
	{
		var myurl = 'index2.php';
		var modurl = myurl+"?com_route=country&value="+value+"&city=1&id="+id+"&country="+country;
		http.open("GET", modurl, true);
		http.onreadystatechange = fillCity;
		http.send(null);
	}	 
}

function changeProvinceLoad1(id,value,pr,val)
{
	
		var myurl = 'index2.php';
		var modurl = myurl+"?com_route=country&value="+value+"&id="+id;
		http.open("GET", modurl, true);
		id_pr = pr;
		id_val = val;
		http.onreadystatechange = useHttpResponseLoad1;
		http.send(null);
	 
}

function changeProvinceLoad(id,value,pr)
{

	var myurl = 'index2.php';
	var modurl = myurl+"?com_route=country&value="+value+"&id="+id;
	http.open("GET", modurl, true);
	id_pr = pr;
	http.onreadystatechange = useHttpResponseLoad;
	http.send(null); 
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}

