// JavaScript Document to retrieve data from an XML Document needed to populate a dependent drop-down
function SymError()
{
  return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
  return (new Object());
}

window.open = SymWinOpen;

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

	function update() {
		clear();
		stateSelected = document.fransitecriteria.Property_state_or_country.options[document.fransitecriteria.Property_state_or_country.selectedIndex].value;
		if (stateSelected != "")
		{
			document.fransitecriteria.Property_city.disabled=false;
		}
		
		xmla=loadXMLDoc("Documents/DMA_City_St.xml");
		patha="/DMAinfo/dmaLocation/DMA_City_St[State = '"+stateSelected+"']/City_x0020_or_x0020_Town";
		patha1="/DMAinfo/dmaLocation/DMA_City_St[State = '"+stateSelected+"']/DMA";
		// code for IE
		if (window.ActiveXObject)
		{
		xmla.setProperty("SelectionLanguage","XPath");
		var nodesa=xmla.selectNodes(patha);
		var nodesa1=xmla.selectNodes(patha1);
		addOption(document.fransitecriteria.Property_city,"--Select Closest City--", "");
		document.fransitecriteria.Property_city.selectedIndex = 0;
		for (i=0;i<nodesa.length;i++) {	
			addOption(document.fransitecriteria.Property_city,nodesa[i].childNodes[0].nodeValue, nodesa1[i].childNodes[0].nodeValue);
			document.fransitecriteria.Property_city.selectedIndex = 0;
			}
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument)
		{
		var nodesa=xmla.evaluate(patha, xmla, null, XPathResult.ANY_TYPE,null);
		var resulta=nodesa.iterateNext();
		var nodesa1=xmla.evaluate(patha1, xmla, null, XPathResult.ANY_TYPE,null);
		var resulta1=nodesa1.iterateNext();
		addOption(document.fransitecriteria.Property_city,"--Select Closest City--","");
		document.fransitecriteria.Property_city.selectedIndex = 0;
		while (resulta && resulta1)
		  {		
		  	resultTexta = resulta.childNodes[0].nodeValue;
			resultValuea1 = resulta1.childNodes[0].nodeValue;			
			addOption(document.fransitecriteria.Property_city,resultTexta,resultValuea1);
			document.fransitecriteria.Property_city.selectedIndex = 0;
			
			resulta=nodesa.iterateNext();
			resulta1=nodesa1.iterateNext();
		  }
		}
	}

	function clear() {
		while(document.fransitecriteria.Property_city.length != 0) {
			document.fransitecriteria.Property_city.options[document.fransitecriteria.Property_city.length-1] = null;
		}

		//history.go(0);
	}
	
	
	function loadXMLDoc(path)
		{	var Browser = "";
		try	{ // MSIE		
		
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");	}	
		catch(e)	{		
		try		{ // Firefox, Opera, etc.			
		var xmlDoc=document.implementation.createDocument("","",null);		}		
		catch(e)		{			
		alert(e.message)
				}	
		}	
		try	{		xmlDoc.async = false; // halt code execution until xmlDoc is loaded		
		xmlDoc.load( path );	// load file	
		}	catch(e)	{ // unable to load		
		try		{	// for Safari			
		xmlDoc = new XMLHttpRequest();			
		xmlDoc.open("GET", path, false);			
		xmlDoc.send();			
		xmlDoc=xmlDoc.responseXML;					
		}		
		catch(e)		
		{			
		alert(e.message)			

		return; // return with nothing		
		}	
		}	
		return xmlDoc;}
		
		function getContactEmails()
		{
		DMASelected = document.fransitecriteria.Property_city.options[document.fransitecriteria.Property_city.selectedIndex].value;
		CitySelected = document.fransitecriteria.Property_city.options[document.fransitecriteria.Property_city.selectedIndex].text;
		xmlb=loadXMLDoc("Documents/DMA_Emails.xml");
		pathb="/DMA_Email_Root/DMA_Emails[DMA = '"+DMASelected+"']/Email_x0020_Address";
		var emailsByDMA;
		emailsByDMA = "";
		// code for IE
		if (window.ActiveXObject)
		{
		xmlb.setProperty("SelectionLanguage","XPath");
		var nodesb=xmlb.selectNodes(pathb);
		
		emailsByDMA = "";
		for (i=0;i<nodesb.length;i++) {
			emailsByDMA = emailsByDMA + nodesb[i].childNodes[0].nodeValue + "_";
			}			
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument)
		{
		var nodesb=xmlb.evaluate(pathb, xmlb, null, XPathResult.ANY_TYPE,null);
		var resultb=nodesb.iterateNext();
		
		while (resultb)
		  {
		  emailsByDMA = emailsByDMA + resultb.childNodes[0].nodeValue + "_";
		  resultb=nodesb.iterateNext();
		  }
		}
		document.fransitecriteria.DMAemails.value = emailsByDMA;
		
		document.fransitecriteria.CitySelected.value = CitySelected;
	}