//Updated on 21/08/2009 as in Mozilla Code dropdown population was not working

// JavaScript Document
var http_request = false;
var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
var scriptname="getXMLData.asp";
var urloutput="";

function setScriptName(vscript)
{
	scriptname=vscript
}

function xhrRequest(type) {
	if (!type) {
		type = 'html';
	}
	// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
	// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
	// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
	var xhrsend = xi.length; 
	
	// GO THROUGH AVAILABLE xi VALUES
	for (var i=0; i<xi.length; i++) {
		// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
		if (xi[i] == 1) {
			xi[i] = 0;
			xhrsend = i;
			break;
		}
	}
	// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
	xi[xhrsend] = 0;
	// SET UP THE REQUEST
	if (window.ActiveXObject) {
		try {
			xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhr[xhrsend] = new XMLHttpRequest();
		if (xhr[xhrsend].overrideMimeType) {
			xhr[xhrsend].overrideMimeType('text/' + type);
		}
	}
	return (xhrsend);
}


function populateListBox(reqType,reqId,tablename,codefield,namefield,parentname,parentvalue,formname,defaultvalue,functionname)
{		//alert("hi");
	
	if(parentvalue=='2931')
	{
		
		
		if (reqId=='selBalaViharState')
		{   document.form1.txtBalaViharCountry.disabled=false
			document.form1.txtBalaViharState.disabled=false	
			document.form1.txtBalaViharCity.disabled=false	
		//	document.form1.txtBalaViharArea.disabled=false	
			
			
			
			document.form1.selBalaViharCity.value='2931'
			//document.form1.selBalaViharArea.value='2931'
			document.form1.selBalaViharState.value='2931'
			return true;
			
		}
		else if (reqId=='selBalaViharCity')
		{document.form1.txtBalaViharState.disabled=false
			if(document.form1.txtBalaViharCountry.value=='')
			{
				document.form1.txtBalaViharCountry.disabled=true
				
			
			}
			else
			{
				document.form1.txtBalaViharCountry.disabled=false
				
				
				//document.form1.selBalaViharArea.disabled=false
				
				
			}
			document.form1.selBalaViharCity.value='2931'
			//document.form1.selBalaViharArea.value='2931'
			document.form1.txtBalaViharCity.disabled=false	
			//document.form1.txtBalaViharArea.disabled=false
			return true;
		}
		
		  	
	 
	}
	
	else
		{
			if(document.form1.txtBalaViharCountry.value=='' || document.form1.selBalaViharCountry.value!='2931' )
			{
				document.form1.txtBalaViharCountry.disabled=true
				document.form1.selBalaViharState.disabled=false	
			document.form1.selBalaViharCity.disabled=false	
		//	document.form1.selBalaViharArea.disabled=false	
			
			}
			if(document.form1.txtBalaViharState.value=='' || document.form1.selBalaViharState.value!='2931' )
			{
				document.form1.txtBalaViharState.disabled=true
			}
		if(document.form1.txtBalaViharCity.value=='' || document.form1.selBalaViharCity.value!='2931' )
			{
				document.form1.txtBalaViharCity.disabled=true
			}
		
			//document.form1.txtBalaViharCity.disabled=true	
			//document.form1.txtBalaViharArea.disabled=true	
		}
	
	
	
	var url = scriptname+"?reqType="+reqType+"&reqId="+reqId+"&table="+tablename+"&codefield="+codefield+"&namefield="+namefield+"&parentname="+parentname+"&parentvalue="+parentvalue+"&formname="+formname;
	var xhri = xhrRequest('html');			
	xhr[xhri].open('GET', url, true);
	xhr[xhri].onreadystatechange = function() {
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
			//alert(xhr[xhri].responseText);
			processXMLResponse(xhr[xhri].responseText,defaultvalue,functionname);
			xi[xhri] = 1;
			xhr[xhri] = null;
		}
	};
	xhr[xhri].send(null);

}


function processXMLResponse(strResponse,defaultvalue,functionname)
{
	var xmlDoc;
	//load xml file
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(strResponse);
	}
	// code for Mozilla, etc.
	else if (document.implementation &&	document.implementation.createDocument)
	{
		
//		xmlDoc= document.implementation.createDocument("","",null);
		parser=new DOMParser();
		 xmlDoc=parser.parseFromString(strResponse,"text/xml");
		
	}
	else
	{
		alert('Your browser cannot handle this script');
	}

	
	var dsObj = xmlDoc.documentElement;
	var listObjName=dsObj.getAttribute("nm");
	var formName=dsObj.getAttribute("fnm");
	var codefield=dsObj.getAttribute("cf");
	var namefield=dsObj.getAttribute("nf");
	var rowObjs = xmlDoc.getElementsByTagName("ROW");
	//alert(codefield);
	var listObj = eval ("document." + formName + "." + listObjName)
	var idx=listObj.length;
	if(idx>1)
	{
		idx=1;
	}
	listObj.length=rowObjs.length+idx;
	for(r=0;r<rowObjs.length;r++)
	{
		codevalue="";
		namevalue="";

		if(rowObjs.item(r).childNodes.item(0).getAttribute("nm")==codefield)
		{
			// For IE
			if (window.ActiveXObject)
			{
				codevalue=rowObjs.item(r).childNodes.item(0).text
			}

			// For Mozilla
			else
			{
				codevalue=rowObjs.item(r).childNodes.item(0).textContent
			}
			

		}
		if(rowObjs.item(r).childNodes.item(1).getAttribute("nm")==namefield)
		{

			// For IE
			if (window.ActiveXObject)
			{
				namevalue=rowObjs.item(r).childNodes.item(1).text
			}
			// For Mozilla
			else
			{
				namevalue=rowObjs.item(r).childNodes.item(1).textContent
			}
		}
		listObj.options[idx].value=codevalue;		
		listObj.options[idx].text=namevalue;

		var arrListValues = defaultvalue.split(",");
		for(j=0;j<arrListValues.length;j++)
		{
			var listValue=arrListValues[j].replace(" ","");
			if(listValue == codevalue)
			{
				listObj.options[idx].selected=true
			}
		}
		idx++;
	}
	
	eval(functionname);
}

function SubmitURL(surl)
{
	var xhri = xhrRequest('html');			
	xhr[xhri].open('GET', surl, true);
	xhr[xhri].onreadystatechange = function() {
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
			urloutput=xhr[xhri].responseText
			xi[xhri] = 1;
			xhr[xhri] = null;
		}
	};
	xhr[xhri].send(null);
}

function getURLOutput()
{
	var newurloutput=urloutput;
	urloutput="";
	return newurloutput;
}

function loadState(default_value)
{
	if(default_value.length>0)
		populateListBox('table','state','state','','','country_code',document.searchform1.country.options[document.searchform1.country.selectedIndex].value,'searchform1',default_value,"loadCity('<%=city%>')")
		
}


function populateListBox1(parentvalue)
{
	
	if(parentvalue=='2931')
	{
		document.form1.txtBalaViharCity.disabled=false
		
		
	
	}
	else
	{
			document.form1.txtBalaViharCity.disabled=true
		
		
		
	}
}
