/*
	Created					:	S.Kumaravelan
	Date						:	Sep 07 2007
	Last Modfied			:	Jan 21 08
	File Description		:	statecity.js
									Loading states, cities, localities usign the function "do_xml()"
*/
		//Global Variable
	var GlobalCity = "";											//some times city name may differ
   	var text;
   	var combo;
	var globalCountryObj;
	var globalStateObj;
	var globalCityObj;
	var globalLocalityObj;
	var globalObj;
	var globalFrm;
	function generateState(frm,Obj,StateObj,CityObj) {
		if(!StateObj) StateObj= frm.state;
		if(!CityObj) CityObj= frm.city;
		StateObj.disabled = true;
		CityObj.disabled = true;
		//Check if Selected is India
		if(Obj.options[Obj.selectedIndex].value==1){
			StateObj.options[0].text = "Loading...";
			StateObj.disabled = false;
			CityObj.disabled = false;
			try{StateObj.selectedIndex=0;}catch(e){}
			StateObj.options[0].text = " - Select a State - ";
		}else {
			//State & City Values has been changed to NULL And Hided
			StateObj.options[0].selected=true;
			CityObj.options[0].selected=true;
		}
	}
/*
	Function Name			:		generateCity
	Params						:		frm	=>	Form name(Required),
											state =>	StateID(Required),
											city	=>	CityID(Optional)			=> this is to select the given city ID
	Return						:		REturn a Select Object with all city names/values for the particular state ID
	example					:		generateCity(frmFormName, 31 , 3158)
*/
	function generateCity(frm,state,city,StateObj,CityObj){
		if(!StateObj) StateObj= frm.state;
		if(!CityObj) CityObj= frm.city;
		GlobalCity = CityObj;		//This global variable used to assign the city name dynamically [ whatever the city name it wont have any problem ]
		if(!state) return false;
		StateObj.disabled = true;
		CityObj.disabled = true;
		CityObj.options[0].text = "Loading...";
		var urltoget = ip.host() + "/index.php?option=search&page=generatecity&notemplate=yes&state="+state+"&city="+city+"&CityObj="+CityObj.name;
		try{
			var xmlHTTP4City = new getHTTPObject();
			xmlHTTP4City.open("GET", urltoget, true);
			xmlHTTP4City.onreadystatechange = function(){
				if (xmlHTTP4City.readyState == 4) {
					document.getElementById("DivCity").innerHTML = xmlHTTP4City.responseText;
					GlobalCity = document.getElementById(GlobalCity.name);
					StateObj.disabled = false;
					try{
						toolTip.createAttribute(GlobalCity);
					}catch(e){
						 try{window.setTimeout("toolTip.createAttribute("+GlobalCity+")",500);}catch(e){}
					}
					try{CityObj.focus();}catch(e){ip.traceErr(e,"generatecity","statecity");}
				}
			};
			xmlHTTP4City.send(null);
		}catch(e){ip.traceErr(e,"genCity","statecity");}
	}
	function cityGenerate(frm,state,city,StateObj,CityObj){
		if(!StateObj) StateObj= frm.state;
		if(!CityObj) CityObj= frm.city;
		GlobalCity = CityObj;		//This global variable used to assign the city name dynamically [ whatever the city name it wont have any problem ]
		if(!state) return false;
		StateObj.disabled = true;
		CityObj.disabled = true;
		CityObj.options[0].text = "Loading...";
		var urltoget = ip.host() + "/index.php?option=search&page=citygenerate&notemplate=yes&state="+state+"&city="+city+"&CityObj="+CityObj.name;
		try{
			var xmlHTTP4City = new getHTTPObject();
			xmlHTTP4City.open("GET", urltoget, true);
			xmlHTTP4City.onreadystatechange = function(){
				if (xmlHTTP4City.readyState == 4) {
					document.getElementById("DivCity").innerHTML = xmlHTTP4City.responseText;
					GlobalCity = document.getElementById(GlobalCity.name);
					StateObj.disabled = false;
					try{
						toolTip.createAttribute(GlobalCity);
					}catch(e){
						 try{window.setTimeout("toolTip.createAttribute("+GlobalCity+")",500);}catch(e){}
					}
				}
			};
			xmlHTTP4City.send(null);
		}catch(e){ip.traceErr(e,"genCity","statecity");}
	}
	// Function to send HTTP Request
	function makeRequest(url) {
    	var AReq = null;
		AReq = new getHTTPObject();
		if (!AReq) {
			alert('Cannot create XMLHTTP instance');
			return false;
		}
		AReq.open('GET', url, true);
		AReq.setRequestHeader('content-type', 'text/xml');
		AReq.onreadystatechange = function(){
			if (AReq.readyState == 4) {
			 if (AReq.status == 200) {
					var Obj = (combo==1) ? globalStateObj :  (combo==2) ? globalCityObj : globalLocalityObj;
					convertXML(AReq,Obj);
			   }
		   }
		};
		AReq.send(null);
   }
	// Main Function tacking form event
	function do_xml(option,frm,Obj) {
		globalFrm = frm;
   		combo = option;
		try{globalCountryObj = frm.country;}catch(e){globalCountryObj=new Object;}
		globalStateObj = frm.state;
		globalCityObj = frm.city;
		try{globalLocalityObj = frm.locality;}catch(e){globalLocalityObj = new Object; }
		var selectedID = Obj.options[Obj.selectedIndex].value;
		//Loading States
		if(option == 1){
			globalCountryObj = Obj;	//this is the Object that we have to process
			if(selectedID!=0){
				globalStateObj.disabled = true;
				globalStateObj.options[0].text = "Loading...";
				globalStateObj.options.length = 1;
				globalCityObj.selectedIndex = 0;
				globalCityObj.options.length = 1;
				globalLocalityObj.selectedIndex = 0;
				globalLocalityObj.options.length = 1;
				addLocality(globalLocalityObj);
				var url =ip.host() + 'ajax/generatestatecityxml.php?type=state&sid=' + selectedID;
				makeRequest(url);
				try{document.getElementById("Div_state").style.display="none";}catch(e){}
			}else {
				globalStateObj.disabled = true;
				globalStateObj.selectedIndex = 0;
				globalStateObj.options.length = 1;

				globalCityObj.selectedIndex = 0;
				globalCityObj.options.length = 1;

				globalLocalityObj.selectedIndex = 0;
				globalLocalityObj.options.length = 1;
				addLocality(globalLocalityObj);
			}
		}//Loading Cities
		else if(option == 2){
			globalStateObj = Obj;	//this is the Object that we have to process
			if(globalStateObj.options[globalStateObj.selectedIndex].value!=0){
				globalCityObj.disabled = true;
				globalCityObj.options[0].text = "Loading...";
				globalCityObj.options.length = 1;
				globalLocalityObj.selectedIndex = 0;
				globalLocalityObj.options.length = 1;
				addLocality(globalLocalityObj);
				var url =ip.host() + '/ajax/generatestatecityxml.php?type=city&sid=' + selectedID;
				makeRequest(url);
				try{document.getElementById("Div_city").style.display="none";}catch(e){}
			}else {
				globalCityObj.options.length = 1;
				globalLocalityObj.selectedIndex = 0;
				globalLocalityObj.options.length = 1;
				addLocality(globalLocalityObj);
			}
		}/* Loading Localities */
		else if(option==3){
			globalCityObj = Obj;	//this is the Object that we have to process
			//here no neeed to check the city value => it may return 0 values 'becoz it will return a "Add Locality"
			try{
				document.getElementById("locality_others").style.display="none";
				document.getElementById("Div_locality_others").style.display="none";
			}catch(e){}
			globalLocalityObj.disabled = true;
			globalLocalityObj.options[0].text = "Loading...";
			globalLocalityObj.options.length = 1;
			var url =ip.host() + '/ajax/generatestatecityxml.php?type=locality&cid=' + selectedID;
			makeRequest(url);
		}
	}


	function convertXML(xmlObj,SelectObj){
		//alert(SelectObj.name);
		var xmlDoc;
		//xmlDoc => text or xmlDocument
		if (window.ActiveXObject){
			// code for IE
			var doc=new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(xmlObj.responseText);
			var x=doc.documentElement;
			var i=0;
			var k=0;
			//alert(x.childNodes.length);
			while(i<x.childNodes.length){
				var val = x.childNodes[i].childNodes[0].nodeValue;
				i=i+1;
				var key = x.childNodes[i].childNodes[0].nodeValue;
				SelectObj.options[k++] = new Option(key,val);
				i=i+1;
			}
		}
		// code for Mozilla, Firefox, Opera, etc.
		else{
			xmlDoc = xmlObj.responseXML;
			var message_nodes = xmlDoc.getElementsByTagName('name');
			var code_nodes = xmlDoc.getElementsByTagName('code');
			var n_messages = message_nodes.length;
			//alert(n_messages);
			for (i = 0; i <n_messages; i++) {
				var key = message_nodes[i].firstChild.nodeValue;
				var val = code_nodes[i].firstChild.nodeValue;
				SelectObj.options[i] = new Option(key,val);
			}
		}
		SelectObj.disabled = false;
		//This is for Dubai City
		try{
			if(combo==1 && globalCountryObj.options[globalCountryObj.selectedIndex].value=="220"){
				globalStateObj.selectedIndex = 1;
				globalStateObj.disabled=true;
				do_xml(2,globalFrm,globalStateObj);
			}
		}catch(e){}
		try{/*SelectObj.focus();*/}catch(e){ip.traceErr(e,"convertXML","statecity");}
   }
   function addLocality(Obj){
	   var e = document.getElementById("locality_others");
	   if(Obj.options[Obj.selectedIndex].value=="Add" || Obj.options[Obj.selectedIndex].value=="add"){
		   e.style.display="";
		   try{e.focus();}catch(e){}
	   }else{
		   e.style.display="none";
		   try{document.getElementById("Div_locality_others").style.display="none";}catch(e){}
		}
   }
	function addPageLocality(Obj){
		var e = document.getElementById("page2_locality_others");
		if(Obj.options[Obj.selectedIndex].value=="Add" || Obj.options[Obj.selectedIndex].value=="add"){
			e.value='';
			e.style.display="";
			try{e.focus();}catch(e){}
		}else{
			e.style.display="none";
			try{document.getElementById("Div_locality_others").style.display="none";}catch(e){}
		}
	}
	/*
	var ip_scl = {
		callXML : function(Opt,frm,e){

		}
	}*/
