var ajaxLocSearcher;
function getCountryListToSearcher()
{
    var url="/pls/web_prod-plq-dad/ou_maps.getCountries";
	if (window.XMLHttpRequest)	
			ajaxLocSearcher = new XMLHttpRequest();	
	else 
	{
	  if (window.ActiveXObject) 
	  {
		try 
		{
			ajaxLocSearcher = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) 
		{
			alert("Could not create Microsoft.XMLHTTP Object");
		}
	  }
	}		
	if (ajaxLocSearcher) 
	{
			ajaxLocSearcher.onreadystatechange = drawCountrySelect;
			ajaxLocSearcher.open("GET", url, true);
			ajaxLocSearcher.send(null);			   	
	}
	else 
		alert("Sorry, but I could not create and XMLHttpRequest");	
} 
//-------------------------------------------------------------------------------------------
function drawCountrySelect()
{       
	if (ajaxLocSearcher.readyState==4) 
    { 	  
		if (ajaxLocSearcher.status == 200) 
		{
				if (ajaxLocSearcher.responseText) 
				{
				   var countrySelect=document.getElementById("countrySelect");  
				   getEmptyList(countrySelect);
                                   cleanSelects(countrySelect);
				   var xmlRes= parseXml(ajaxLocSearcher.responseText);				   
				   for(i=0 ; i<xmlRes.getElementsByTagName("COUNTRY").length;i++)
				   {
				     var opt= document.createElement('option');
				     opt.text=xmlRes.getElementsByTagName("NAME")[i].firstChild.nodeValue;
				     opt.value=xmlRes.getElementsByTagName("ORG_ID")[i].firstChild.nodeValue;				     				     	
				     opt.selected=opt.value==orgid?true:false;				     				     
				     countrySelect.options[countrySelect.length] = 	opt;
				    // if(opt.selected)
				     	//setLocationPath(countrySelect);
				     					        	 
				   }				   
				   countrySelect.style["display"]="";
				   document.getElementById("loadingCountry").style["display"]="none";				   
				   getStatesFromCountry();				   				 			   
				}				
	    }
	 }
}
//--------------------------------------------------------------------------------------------------
function getStateListToSearcher(orgIdList)
{
    var url="/pls/web_prod-plq-dad/ou_maps.getStates";
	if (window.XMLHttpRequest)	
			ajaxLocSearcher = new XMLHttpRequest();	
	else 
	{
	  if (window.ActiveXObject) 
	  {
		try 
		{
			ajaxLocSearcher = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) 
		{
			alert("Could not create wa.XMLHTTP Object");
		}
	  }
	}		
	if (ajaxLocSearcher) 
	{
	        url+="?p_countryList="+orgIdList;
			ajaxLocSearcher.onreadystatechange = drawStateSelect;
			ajaxLocSearcher.open("GET", url, true);
			ajaxLocSearcher.send(null);			   	
	}
	else 
		alert("Sorry, but I could not create and XMLHttpRequest");	
}
//-------------------------------------------------------------------------------------------
function drawStateSelect()
{
    if (ajaxLocSearcher.readyState==4) 
    { 	  
		if (ajaxLocSearcher.status == 200) 
		{
				if (ajaxLocSearcher.responseText) 
				{
				   var stateSelect=document.getElementById("statesSelect");  
				   getEmptyList(stateSelect);
                                   cleanSelects(stateSelect);
				   var xmlRes= parseXml(ajaxLocSearcher.responseText);		
				   		   
				   for(i=0 ; i<xmlRes.getElementsByTagName("STATE").length;i++)
				   {
				     var opt= document.createElement("option");				     
				     opt.value=xmlRes.getElementsByTagName("ID")[i].firstChild.nodeValue;
                                     opt.text = xmlRes.getElementsByTagName("NAME")[i].firstChild.nodeValue;
                                     stateSelect.options[stateSelect.length] = opt;			
				   }
				  if(stateSelect.length==1) 
				  {
				        stateSelect.options[0].selected=true;
				  	getCitiesFromStates();
				  	setLocationPath(stateSelect);
				  }	
				  document.getElementById("loadingState").style["display"]="none";
				  document.getElementById("statesSelect").style["display"]="";			  				   				 			   
				}				
	    }
	 }
}
//------------------------------------------------------------------------------------------
function getCityListToSearcher(orgIdList,statesList)
{
	  var url="/pls/web_prod-plq-dad/ou_maps.getCities";
	if (window.XMLHttpRequest)	
			ajaxLocSearcher = new XMLHttpRequest();	
	else 
	{
	  if (window.ActiveXObject) 
	  {
		try 
		{
			ajaxLocSearcher = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) 
		{
			alert("Could not create Microsoft.XMLHTTP Object");
		}
	  }
	}		
	if (ajaxLocSearcher) 
	{
	        url+="?p_countryList="+orgIdList;
	        url+="&p_stateList="+statesList;
			ajaxLocSearcher.onreadystatechange = drawCitySelect;
			ajaxLocSearcher.open("GET", url, true);
			ajaxLocSearcher.send(null);			   	
	}
	else 
		alert("Sorry, but I could not create and XMLHttpRequest");	
}
//------------------------------------------------------------------------------------------
function drawCitySelect()
{
	if (ajaxLocSearcher.readyState==4) 
    { 	  
		if (ajaxLocSearcher.status == 200) 
		{
				if (ajaxLocSearcher.responseText) 
				{
				   var citySelect=document.getElementById("citySelect");  
				   getEmptyList(citySelect);				   
                                   cleanSelects(citySelect);
				   var xmlRes= parseXml(ajaxLocSearcher.responseText);		
				   		   
				   for(i=0 ; i<xmlRes.getElementsByTagName("CITY").length;i++)
				   {
				     var opt= document.createElement('option');
				     opt.text=xmlRes.getElementsByTagName("DISPLAY_NAME")[i].firstChild.nodeValue;
				     opt.value=xmlRes.getElementsByTagName("NAME")[i].firstChild.nodeValue;	
				     opt.value=opt.value.toUpperCase();		     				     				     
				     citySelect.options[citySelect.length] = opt;				     			     
				   }
				   if(citySelect.length==1) 
				   {
				       citySelect.options[0].selected=true;
				  	   getTCentersFromCities();
				  	   setLocationPath(citySelect);
				   }				   
				   document.getElementById("loadingCity").style["display"]="none";
				   document.getElementById("citySelect").style["display"]="";					     	   	 				   				 			   
				}				
	    }
	 }
}

//------------------------------------------------------------------------------------------
function getTCenterListToSearcher(orgIdList,statesList,citiesList)
{
	  var url="/pls/web_prod-plq-dad/ou_maps.getTCenters";
	if (window.XMLHttpRequest)	
			ajaxLocSearcher = new XMLHttpRequest();	
	else 
	{
	  if (window.ActiveXObject) 
	  {
		try 
		{
			ajaxLocSearcher = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) 
		{
			alert("Could not create Microsoft.XMLHTTP Object");
		}
	  }
	}		
	if (ajaxLocSearcher) 
	{
	
	        url+="?p_org_id="+orgid;
	        url+="&p_lang="+lang;
	        url+="&p_countryList="+orgIdList;
	        url+="&p_stateList="+statesList;
	        url+="&p_cityList=" + encodeURIComponent(citiesList);
			ajaxLocSearcher.onreadystatechange = drawTCenterList;
			ajaxLocSearcher.open("GET", url, true);
			ajaxLocSearcher.send(null);			   	
	}
	else 
		alert("Sorry, but I could not create and XMLHttpRequest");	
}
//------------------------------------------------------------------------------------------
function drawTCenterList()
{
	if (ajaxLocSearcher.readyState==4) 
    { 
		if (ajaxLocSearcher.status == 200) 
		{
				if (ajaxLocSearcher.responseText) 
				{
				   var tcenterList=document.getElementById("tcenterList");  
				   flushList(tcenterList);
                                   //cleanSelects(tcenterList);
				   var xmlRes= parseXml(ajaxLocSearcher.responseText);		
				   		   
				   for(i=0 ; i<xmlRes.getElementsByTagName("TCENTER").length;i++)
				   {
				     var liObj = document.createElement('li');				     
				     var aobj= document.createElement('a');	
				     aobj.href=xmlRes.getElementsByTagName("MAP_URL")[i].firstChild.nodeValue;
				     aobj.innerHTML=xmlRes.getElementsByTagName("NAME")[i].firstChild.nodeValue;			     
				     liObj.appendChild(aobj);
				     tcenterList.appendChild(liObj);				     		   	 
				   }				   
				   document.getElementById("loadingtcenter").style["display"]="none";	
				   document.getElementById("tcenterList").style["display"]="";			   				 			   
				}				
	    }
	    else 
	    {
	    	alert("An Error Ocurred on the Ajax Call");
	    }
	 }
}

//------------------------------------------------------------------------------------------
function getEmptyList(comboList)
{
    for (i = comboList.length - 1; i>=0; i--) 
    {    
      comboList.remove(i);
    }
}
//------------------------------------------------------------------------------------------
function flushList(list)
{
	list.innerHTML="";
}
//------------------------------------------------------------------------------------------
// ---- PAGE PART ----- /
//------------------------------------------------------------------------------------------
function getStatesFromCountry()
{
    document.getElementById("statesSelect").style["display"]="none";
    document.getElementById("loadingState").style["display"]="";
	var countryList=document.getElementById("countrySelect");
	var countriesSelectedArr = new Array();	
	for (i = countryList.length - 1; i>=0; i--)
	{
		if(countryList.options[i].selected)
			countriesSelectedArr[countriesSelectedArr.length]=countryList.options[i].value;
	}
	getStateListToSearcher(countriesSelectedArr);	
}

//------------------------------------------------------------------------------------------
function getCitiesFromStates()
{
    document.getElementById("citySelect").style["display"]="none";
    document.getElementById("loadingCity").style["display"]="";
	var countryList=document.getElementById("countrySelect");
	var countriesSelectedArr = new Array();	
	var statesList=document.getElementById("statesSelect");
	var statesSelectedArr = new Array();
			
	for (i = countryList.length - 1; i>=0; i--)
	{
		if(countryList.options[i].selected)
			countriesSelectedArr[countriesSelectedArr.length]=countryList.options[i].value;
	}

    for (i = statesList.length - 1; i>=0; i--)
	{
		if(statesList.options[i].selected)
			statesSelectedArr[statesSelectedArr.length]=statesList.options[i].value;
	}		
			
	getCityListToSearcher(countriesSelectedArr,statesSelectedArr);
}

//------------------------------------------------------------------------------------------
function getTCentersFromCities()
{
    document.getElementById("tcenterList").style["display"]="none";
    document.getElementById("loadingtcenter").style["display"]="";
	var countryList=document.getElementById("countrySelect");
	var countriesSelectedArr = new Array();	
	var statesList=document.getElementById("statesSelect");
	var statesSelectedArr = new Array();
	var cityList=document.getElementById("citySelect");
	var citySelectedArr = new Array();	
			
	for (i = countryList.length - 1; i>=0; i--)
	{
		if(countryList.options[i].selected)
			countriesSelectedArr[countriesSelectedArr.length]=countryList.options[i].value;
	}
    for (i = statesList.length - 1; i>=0; i--)
	{
		if(statesList.options[i].selected)
			statesSelectedArr[statesSelectedArr.length]=statesList.options[i].value;
	}		
	for (i = cityList.length - 1; i>=0; i--)
	{
		if(cityList.options[i].selected)
			citySelectedArr[citySelectedArr.length]=cityList.options[i].value;
	}			
	getTCenterListToSearcher(countriesSelectedArr,statesSelectedArr,citySelectedArr);
}

//------------------------------------------------------------------------------------------
function setLocationPath(fromSelect)
{
	var spanPath = document.getElementById("currentLocationPath");
	var stringPath ="";
	var countryList=document.getElementById("countrySelect");		
	var statesList=document.getElementById("statesSelect");	
	var cityList=document.getElementById("citySelect");	
	var statesCount=0;
	var citiesCount=0;
	var ignoreStates = (fromSelect.id == "countrySelect")?true:false;
	var ignoreCities = (fromSelect.id == "countrySelect" || fromSelect.id == "statesSelect")?true:false;
	
    for (i = statesList.length - 1; i>=0; i--)
	{
		if(statesList.options[i].selected)
			 statesCount++;
	}		
	for (i = cityList.length - 1; i>=0; i--)
	{
		if(cityList.options[i].selected)
			citiesCount++;
	}
	
	if(countryList.selectedIndex>=0)	
	{
		stringPath=countryList.options[countryList.selectedIndex].text + " &gt; ";
		
		if(!ignoreStates)
		{		
			if(statesCount>1)		
				stringPath+= " * &gt; ";		
			else if(statesCount==1)	
				stringPath+= statesList.options[statesList.selectedIndex].text + " &gt; ";
		}
	    if(!ignoreCities)
	    {
			if(citiesCount>1)		
				stringPath+= " * &gt; ";		
			else if(citiesCount==1)	
				stringPath+= cityList.options[cityList.selectedIndex].text + " &gt; ";
		}			
	}
	spanPath.innerHTML = stringPath;
}
//------------------------------------------------------------------------------------------
function cleanSelects(fromSelect)
{
  if(fromSelect.id == "countrySelect")
  	getEmptyList(document.getElementById("statesSelect"))
  if(fromSelect.id == "countrySelect" || fromSelect.id == "statesSelect")
    getEmptyList(document.getElementById("citySelect"))
  if(fromSelect.id == "countrySelect" || fromSelect.id == "statesSelect" || fromSelect.id == "citySelect")
  	flushList(document.getElementById("tcenterList"))
}
//------------------------------------------------------------------------------------------
