// JavaScript Document

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() 
	{
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++)
		{
			urlVars = varArray[i].split("=");
			if (true == encode)
			{
				this.encVar(urlVars[0], urlVars[1]);
			} 
			else
			{
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) 
	{
		if (this.encodeURIString && this.URLString.length) 
		{
			this.processURLString(this.URLString, true);
		}

		if (urlstring) 
		{
			if (this.URLString.length) 
			{
				this.URLString += this.argumentSeparator + urlstring;
			}
			else 
			{
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() 
	{
		eval(this.response);
	}

	this.runAJAX = function(urlstring)
	{
		if (this.failed) 
		{
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element)
			{
				this.elementObj = document.getElementById(this.element);
			
			}
			
			if (this.xmlhttp) 
			{
				var self = this;
				
				if (this.method == "GET")
				{
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function()   //Call back function
				{
					
					//alert(self.response);
						
					switch (self.xmlhttp.readyState)
					{
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
						
						//alert('Yes');
							
							self.response = self.xmlhttp.responseText;
							
						//	alert(self.response);
							
							if(self.response==1)
							{
							
							document.getElementById('o').style.display='block';
							document.getElementById('s').style.display='none';
							
							document.getElementById('city_code').value='';
							//for City Drop Down
							document.getElementById('c').style.display='none';
							
							//for City textBox
							document.getElementById('t').style.display='block';	
							//enable the countrycode box
							
							
							}
							else
							{
						
						//alert('else');
							
							document.getElementById('s').style.display='block';
							document.getElementById('o').style.display='none';
							//--------------------------
							
							//for city text box
							document.getElementById('t').style.display='none';
							document.getElementById('c').style.display='none';
							}
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							
							if (self.execute) 
							{
								self.runResponse();
							}

							if (self.elementObj) 
							{
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") 
								{
									self.elementObj.value = self.response;
								} 
								else 
								{
									self.elementObj.innerHTML = self.response;
								}
							} //end of if statement
							
							if (self.responseStatus[0] == "200") 
							{
								self.onCompletion();
							} 
							else 
							{
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}


//-----------------------------------------


function country_code_enabled()
{
  
 // alert(document.getElementById('country').value);
 
  if(document.getElementById('country').value=="00")
  {
   
   //alert('ye');
   
   document.getElementById('country_code').value='';
    document.getElementById('DivOCountry').style.display='block';
   }
   else
   {
   document.getElementById('country_code').value='';
   
   document.getElementById('DivOCountry').style.display='none';
   }

} //end of function


	var ajax = new sack();

function getCityList(sel)
{
	//alert("Hello");
	
	//alert(sel);
	
	var countryCode = sel.options[sel.selectedIndex].value;
	
//	alert("Country Code : " + countryCode);
	
	document.getElementById('state').options.length = 0;	// Empty city select box
	
	//  countryCode.options[countryCode.options.length] = new Option("Select State", "");

	
	if(countryCode.length > 0)
	{
	   //alert(countryCode);
		
		ajax.requestFile ='country.php?countryCode='+countryCode;	// Specifying which file to get
		ajax.onCompletion=createCities;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
   else
   {
   document.getElementById('o').style.display='none'; 
   document.getElementById('city_code').value='';
   }
}

function createCities()
{
	var obj = document.getElementById('state');
	
//	alert(ajax.response);
	
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

//End For State
		
//Start of City

     	    var ur = "state.php";
            var wha = "SetCities(req.responseText)";
      function DoCallback1(data)
{
	// branch for native XMLHttpRequest object
	//alert(data);
	
	if (window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange1; //Call Back Function
		req.open('POST', ur, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(data);
	
	} 
	else if (window.ActiveXObject) 
	{
		req = new ActiveXObject('Microsoft.XMLHTTP')
		
		if (req) 
		{
			req.onreadystatechange = processReqChange1;
			req.open('POST', ur, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.send(data);
		}
	}
} //end of the function

function processReqChange1()
{
	// only if req shows 'loaded'
	if (req.readyState == 4) 
	{
		// only if 'OK'
		if (req.status == 200) 
		{
			
			//alert(wha);
			eval(wha);
		 
		} 
		else
		 {
			alert('There was a problem retrieving the XML data:\n' +req.responseText);
		}
	}
} //end of function


            function GetCities1(State)
            {
				//alert(State);
				DoCallback1("state="+State);
            }  //end of function

     
	 function SetCities(Cities)
         {
              var cityBox = document.getElementById("city");
               
                cityBox.options.length = 0;

           if(Cities != "")
          {
            		
			//alert('Ubhay');
		    //alert(Cities);
			
			  if(Cities != 1)
				{ 
					
					document.formx.city.style.visibility='visible';
					document.getElementById('c').style.display='block';
					document.getElementById('u').style.display='none';
			         //document.getElementById('t').style.display='none';
					 			
					var arrCities = Cities.split(",");
   			// new Option("Text", "Value");
		  cityBox.options[cityBox.options.length] = new Option("Select City", "");
		  
		           for(i = 0; i < arrCities.length - 1; i++)
                    {
    
					    if(arrCities[i] != "")
                        {
         // cityBox.options[cityBox.options.length] = new Option('Choose City', '0']);
		        cityBox.options[cityBox.options.length] = new Option(arrCities[i], arrCities[i]);
				
                        }
                  
				    } //end of for loop
                  cityBox.options[cityBox.options.length] = new Option("Other", "other");
				}
			else if(Cities == 1)
			  		{
			
					 //document.getElementById('city_code').value='';
					 
					 document.getElementById('t').style.display='block';
		             document.getElementById('c').style.display='none';
					
					}
			  
			
		  }
			 else 
			 {
	
	      alert('Else: For City ');
		// document.getElementById('city_code').disabled=false; 
		//	 document.getElementById('city_code').value='';
		//	 document.getElementById('t').style.display='block';
		//	 document.getElementById('c').style.display='none';
		  }  
			 
			 
		  // } //end of If
             
		 } //end of function

function createRequestObject()
{
  var request_;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  {
    request_ = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    request_ = new XMLHttpRequest();
  }
  return request_;
}

var http = createRequestObject();

//alert(http);

function getInfo()
{
  
  //alert("Hello");
  
  http.open('get', 'country.php?id='+ document.getElementById('country').value);
  
  http.onreadystatechange = handleInfo; //Call back function
  
  http.send(null);
}

//Call back function 
function handleInfo()
{
  
  if(http.readyState == 1)
  {
  // document.getElementById('dyn_div').innerHTML = '<font size="+1" color="red">Loading...</font>';
  }

  if(http.readyState == 4)
  {
    var response = http.responseText;
    
	//alert('State :' +response);
	
	document.getElementById('country_code').value = response;
  }

} //end of function

<!-- For Fetching the City Code from City Table -->

function createRequestObject()
{
  var request_;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  {
    request_ = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    request_ = new XMLHttpRequest();
  }
  return request_;
}

var http = createRequestObject();

function getInfo2()
{
 // alert(document.formx.country.selectedIndex);
  
  http.open('get', 'state.php?id='+ document.getElementById('city').value);
  
  http.onreadystatechange = handleInfo2; //Call back function
  
  http.send(null);
}

//Call back function 
function handleInfo2()
{
   if(http.readyState == 1)
  {
// document.getElementById('dyn_div').innerHTML = '<font size="+1" color="red">Loading...</font>';
    //document.getElementById('dyn_div').innerHTML = 'Loading...';
  }

  if(http.readyState == 4)
  {
    var response = http.responseText;
//   alert("City Code: "+response);

	document.getElementById('city_code').value = response;
     //document.getElementById('city_code').disabled =true; 
  }

} //end of function

