// JavaScript Document
// @author Ashutosh K Tripathi

var http ; // global variable for ajax xmlhttp request

//function createRequestObject
// @iparam void
// @oparam xmlhttpRequest Object
//function to actually create global xmlhttpRequest Object 
function createRequestObject()
        {
	      var xmlhttp=false;
	      try 
	       {
	 	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	       }catch (e) 
	        {try 
		     {
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  		 }catch (E) 
		      {
		       xmlhttp = false;
	  	      }
	       }
	     if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	      {
		   try 
		   {
			xmlhttp = new XMLHttpRequest();
	       }catch (e) 
		    {
			 xmlhttp=false;
		    }
	     }
	    if (!xmlhttp && window.createRequest) 
	    {
		try 
		 {
			xmlhttp = window.createRequest();
		 }catch (e) 
		  {
			xmlhttp=false;
		  }
	  }	
	return xmlhttp;
   }  //Create Request Object function ends.

//function getStateByCountry
// @iparam void
// @oparam xmlhttpRequest Object
//request function to get States for a given country using ajax
function getStateByCountry()
{
  var mf = document.forms[0];
  var val = mf.Country.options[mf.Country.selectedIndex].value ;
  if(val!=""&&val!='0')
  {
  http=createRequestObject();
  http.onreadystatechange = function(){getStateByCountryResponse();};
  http.open("GET", "../asphp/LMProcess.php?purpose=LoadState&id="+val, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  http.send(null);
  mf.State.options.length = 0;
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.State.options[0] = new Option("Loading...","");
  mf.City.options[0] = new Option("---","");
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltCountryTD').innerHTML = "";
  document.getElementById('AltStateTD').innerHTML = "";
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
  else if(val=='0'){
  mf.State.options.length = 0;
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.State.options[0] = new Option("---","");
  mf.City.options[0] = new Option("---","");
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltCountryTD').innerHTML = "<input type='text' name='AltCountry'>";
  document.getElementById('AltStateTD').innerHTML = "<input type='text' name='AltState'>";
  document.getElementById('AltCityTD').innerHTML = "<input type='text' name='AltCity'>";
  document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";
  }
  else
  {
  mf.State.options.length = 0;
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.State.options[0] = new Option("---","");
  mf.City.options[0] = new Option("---","");
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltCountryTD').innerHTML = "";
  document.getElementById('AltStateTD').innerHTML = "";
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
}
//function getStateByCountryResponse
// @iparam void
// @oparam xmlhttpRequest Object
//response function to get States for a given country using ajax
function getStateByCountryResponse()
{
	 if(http.readyState==4)
	 {	  
	  if(http.status==200)
	   {		
	    var mf = document.forms[0];
	    var response = http.responseText ;
		StateArr = response.split("|");
		mf.State.options.length = 0;
		mf.State.options[0] = new Option("Select","");
		for(var i=0; i<StateArr.length-1; i++)
	     {
		  var CodeValArr = StateArr[i].split("+");
		  mf.State.options[i+1] = new Option(CodeValArr[1],CodeValArr[0]);
		 }
		//mf.State.options[mf.State.options.length] = new Option("Other","0");  
	   }
	 }
}
//function getCityByState
// @iparam void
// @oparam xmlhttpRequest Object
//request function to get City for a given state using ajax
function getCityByState()
{
  var mf = document.forms[0];
  var val = mf.State.options[mf.State.selectedIndex].value ;
  if(val!=""&&val!="0")
  {
  http=createRequestObject();
  http.onreadystatechange = function(){getCityByStateResponse();};
  http.open("GET", "../asphp/LMProcess.php?purpose=LoadCity&id="+val, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  http.send(null);
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.City.options[0] = new Option("Loading...","");
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltStateTD').innerHTML = "<input type='text' name='AltState'>";
  document.getElementById('AltCityTD').innerHTML = "<input type='text' name='AltCity'>";
  document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";
  document.getElementById('AltCountryTD').innerHTML = "";
  document.getElementById('AltStateTD').innerHTML = "";
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
  else if(val=='0'){
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.City.options[0] = new Option("---","");
  mf.Location.options[0] = new Option("---","");	  
  document.getElementById('AltStateTD').innerHTML = "<input type='text' name='AltState'>";
  document.getElementById('AltCityTD').innerHTML = "<input type='text' name='AltCity'>";
  document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";
  }
  else
  {
  mf.City.options.length = 0;
  mf.Location.options.length = 0;
  mf.City.options[0] = new Option("---","");
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltStateTD').innerHTML = "";
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
}
//function getCityByStateResponse
// @iparam void
// @oparam xmlhttpRequest Object
//response function to get City for a given State using ajax
function getCityByStateResponse()
{
	 if(http.readyState==4)
	 {	  
	  if(http.status==200)
	   {		
	    var mf = document.forms[0];
	    var response = http.responseText ;
		CityArr = response.split("|");
		mf.City.options.length = 0;
		mf.City.options[0] = new Option("Select","");
		for(var i=0; i<CityArr.length-1; i++)
	     {
		  var CodeValArr = CityArr[i].split("+");
		  mf.City.options[i+1] = new Option(CodeValArr[1],CodeValArr[0]);
		 }
		 //mf.City.options[mf.City.options.length] = new Option("Other","0");
	   }
	 }
}
//function getLocationByCity
// @iparam void
// @oparam xmlhttpRequest Object
//request function to get Location for a given City using ajax
function getLocationByCity()
{
  var mf = document.forms[0];
  var val = mf.City.options[mf.City.selectedIndex].value ;
  if(val!=""&&val!="0")
  {
  http=createRequestObject();
  http.onreadystatechange = function(){getLocationByCityResponse();};
  http.open("GET", "../asphp/LMProcess.php?purpose=LoadLocation&id="+val, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  http.send(null);
  mf.Location.options.length = 0;
  mf.Location.options[0] = new Option("Loading...","");
  document.getElementById('AltCityTD').innerHTML = "<input type='text' name='AltCity'>";
  document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";
  document.getElementById('AltCountryTD').innerHTML = "";
  document.getElementById('AltStateTD').innerHTML = "";
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
  else if(val=='0'){
  mf.Location.options.length = 0;
  mf.Location.options[0] = new Option("---","");	  
  document.getElementById('AltCityTD').innerHTML = "<input type='text' name='AltCity'>";
  document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";  
  }
  else
  {
  mf.Location.options.length = 0;
  mf.Location.options[0] = new Option("---","");
  document.getElementById('AltCityTD').innerHTML = "";
  document.getElementById('AltLocationTD').innerHTML = "";
  }
}
//function getLcoationByCityResponse
// @iparam void
// @oparam xmlhttpRequest Object
//response function to get Location for a given City using ajax
function getLocationByCityResponse()
{
	 if(http.readyState==4)
	 {	  
	  if(http.status==200)
	   {		
	    var mf = document.forms[0];
	    var response = http.responseText ;
		LocationArr = response.split("|");
		mf.Location.options.length = 0;
		mf.Location.options[0] = new Option("Select","");
		for(var i=0; i<LocationArr.length-1; i++)
	     {
		  var CodeValArr = LocationArr[i].split("+");
		  mf.Location.options[i+1] = new Option(CodeValArr[1],CodeValArr[0]);
		 }
		 mf.Location.options[mf.Location.options.length] = new Option("Other","0");
	   }
	 }
}

function getAltLocation()
{
  var mf = document.forms[0];
  var val = mf.Location.options[mf.Location.selectedIndex].value ;
  if(val=='0')
   document.getElementById('AltLocationTD').innerHTML = "<input type='text' name='AltLocation'>";
  else{   
   document.getElementById('AltLocationTD').innerHTML = "";
  }
}