﻿// JScript File

var requestS,responseS;
var cboMaster,cboChild;
var appTo = "";

function xmlError(e) {//there was an error, show the user
    alert(e);
}

function populateListingDetail(Master,Child)
{
    cboMaster = Master;
    cboChild = Child;
	clearSelectS(cboChild);				
	if(cboMaster.options[cboMaster.selectedIndex].value != "")
	{	// Source
		return SendRequestSource(cboMaster.options[cboMaster.selectedIndex].value);
	}
	else
	{
		clearSelectS(cboChild);
	}
}
	
function populateListingDetailMas(Master,Child)
{
    cboMaster = Master;
    cboChild = Child;
    clearSelectS(cboChild);
	waitDisplay('spanModelWait','spanModel');
	appTo="mas";
	if(cboMaster.options[cboMaster.selectedIndex].value != "")
	{	// Source
		return SendRequestSource(cboMaster.options[cboMaster.selectedIndex].value);
	}
	else
	{
		clearSelectS(cboChild);
	}
}
	
function populateListingDetailPage(Master,Child)
{
    cboMaster = Master;
    cboChild = Child;
    clearSelectS(cboChild);
    waitDisplay('spanModelWait_AdvancedSearch', 'spanModel_AdvancedSearch');
    appTo="page";
    if(cboMaster.options[cboMaster.selectedIndex].value != "")
    {	// Source
        return SendRequestSource(cboMaster.options[cboMaster.selectedIndex].value);
    }
    else
    {
        clearSelectS(cboChild);
    }
}
        
function populateListingcarAd(Master,Child)
{
    cboMaster = Master;
    cboChild = Child;
    clearSelectS(cboChild);
    waitDisplay('spanModelWait_CarAd', 'spanModel_CarAd');
    appTo="Adcar";
    if(cboMaster.options[cboMaster.selectedIndex].value != "")
    {	// Source
        return SendRequestSource(cboMaster.options[cboMaster.selectedIndex].value);
    }
    else
    {
        clearSelectS(cboChild);
    }

}
function SendRequestSource(MasterID)
{	var dt =new Date();
	requestS=CommonInitializeRequestObject();
	var url = sPath+"/js/CommonAJAX.aspx?PageName=FillModelByMake&MakeID="+MasterID+"&dt="+dt.getSeconds();

	
	requestS.onreadystatechange = ProcessRequestSource;//Delegate ProcessRequestCampus to onreadystatechange property so it gets called for every change in readyState value
	requestS.open("GET", url, true);//Open a GET request to the URL
	//alert(url);
	requestS.send(null);//Send the request with a null body.
}
	
	function ProcessRequestSource()
		{   //alert(requestS.readyState);
		    
			if(requestS.readyState == 4)//If the readyState is in the "Ready" State
			{   //alert(requestS.status);
				if(requestS.status == 200)//If the returned status code was 200. Everything was OK.
				{
					if(requestS.responseText != "")//If responseText is not blank
					{	
					    
						if(requestS.responseText.indexOf("No record found.") < 0)
						{
						var response=requestS.responseText;
						var str=response.split("<!DOCTYPE");
	                        response=str[0];
	                           
						    populateListS(response);//Call the populateList fucntion
						    cboChild.selectedIndex=0;
						    
						}
						else
					    {
					      clearSelectS(cboChild);
					      cboChild.selectedIndex=0;  	
					     //appendToSelect(cboChild,-1,document.createTextNode("Select"));
					    }
					  
						
					}
					else
					{
						clearSelectS(cboChild);	
						cboChild.selectedIndex=0;  	
					}
					
					  if(appTo=="mas")
						{
						 waitDisplay('spanModel','spanModelWait');
						}
						else if	(appTo=="page")					
						{
						    waitDisplay('spanModel_AdvancedSearch', 'spanModelWait_AdvancedSearch');
						}
						else if(appTo=="Adcar")
						{
						    waitDisplay('spanModel_CarAd', 'spanModelWait_CarAd');
						}
						appTo="";
				}
			}
			return true;//return
		}
		
function populateListS(response)
{  
	var xmlDoc;
	if (navigator.appName.indexOf("Microsoft")>=0)      //Microsoft Case
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");   //Create the XMLDOM object
	else											    // Mozilla Case
		xmlDoc = new XMLHttpRequest();
	xmlDoc=new XMLDoc(response, xmlError);
	//alert(response);
	if(xmlDoc.docNode!=null)
	{	
		obRoot = xmlDoc.docNode;
		if(obRoot.getElements().length > 0)//If there are one or more TERRITORIES nodes
		{
			clearSelectS(cboChild);	
			for (var i = 0; i < obRoot.getElements().length; i++)//Loop through the XML TERRITORIES nodes
			{
				var textNode = document.createTextNode(obRoot.getElements()[i].getText());//Create a TextNode
				appendToSelect(cboChild, obRoot.getElements()[i].getAttribute("ModelID"), textNode);//Call appendToSelect to append the text elements to the PType dropdown
			}
		}
	}
}

function appendToSelect(select, value, content)
{
	var opt;
	opt = document.createElement("option");//Create an Element of type option
	opt.value = value;//Set the option's value
	opt.appendChild(content);//Attach the text content to the option
	select.appendChild(opt);//Append the option to the referenced [PType] select box
}

function clearSelectS(select)
{
	select.options.length = 1;//Set the select box's length to 1 so only "--Select--" is availale in the selection on calling this function.
	//You may want to write your own clearSelect logic
	select.options[0].text = "Any Model";					
}