// JavaScript Document
//-------------------------------------------------ATTEMPT AT DOM PARSER FOR SAFARI-------------------------------------
if (typeof DOMParser == "undefined") {
   DOMParser = function () {}

   DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
      }
   }
}
//--------------------------------------------------------------------------------------------------------------------------

// global request and XML document objects
var xVar;
function loadScript(){

//LOAD SELECT DROP DOWNS
		loadSel("selMake","xmlMake.php","make");	14/1/05
		//alert('finished loadsel');
		if(document.getElementById('hdnMake').value!='' && document.getElementById('hdnMake').value!=0)
		{
			selMakeChange();
			
		}else
		{
		
			//initSel("selModel","SELECT MODEL");
			initSel("selModel","any model");
			//initSel("selTrans","SELECT TRANSMISSION");
			initSel("selTrans","any transmission");
			//initSel("selYrFrom","YEAR FROM",'');
			//initSel("selYrTo","YEAR TO",'');
			initSel("selYrFrom","any year",'');
			initSel("selYrTo","any year",'');
		}
}

function selMakeChange(){
	//document.getElementById('hdnMake').innerText=value=document.getElementById('selMake').value;
	loadSel("selModel","xmlModel.php?make=" + document.getElementById('selMake').value,"model");
	loadSel("selTrans","xmlTrans.php?make=" + document.getElementById('selMake').value ,"transmission");
	loadSel("selYrFrom","xmlYearAsc.php?make=" + document.getElementById('selMake').value,"year");
	loadSel("selYrTo","xmlYearDesc.php?make=" + document.getElementById('selMake').value,"year");
}

function selModelChange(){
	loadSel("selTrans","xmlTrans.php?make=" + document.getElementById('selMake').value + "&model=" + document.getElementById('selModel').value ,"transmission");
	loadSel("selYrFrom","xmlYearAsc.php?make=" + document.getElementById('selMake').value + "&model=" + document.getElementById('selModel').value ,"year");
	loadSel("selYrTo","xmlYearDesc.php?make=" + document.getElementById('selMake').value + "&model=" + document.getElementById('selModel').value ,"year");
}

function selTransChange(){
	loadSel("selYrFrom","xmlYearAsc.php?make=" + document.getElementById('selMake').value + "&model=" + document.getElementById('selModel').value + "&trans=" + document.getElementById('selTrans').value,"year");
	loadSel("selYrTo","xmlYearAsc.php?make=" + document.getElementById('selMake').value + "&model=" + document.getElementById('selModel').value + "&trans=" + document.getElementById('selTrans').value,"year");
}

function clearValues(){
	document.getElementById('hdnMake').value='';
	document.getElementById('hdnModel').value='';
	document.getElementById('hdnMake').value='';
	document.getElementById('hdnMake').value='';
	document.getElementById('hdnMake').value='';
	loadScript();
	document.getElementById('selPriceFrom').selectedIndex=0;
	document.getElementById('selPriceTo').selectedIndex=0;
}

function loadSel(sSel,sUrl,sTag)
{

    var myDocument;
    var xmlString;
    var newEl = document.getElementById(sSel);
    var oOption;
    var sVal;
    var sChosen;
    var stemp;


    if (window.XMLHttpRequest)
    	// branch for native XMLHttpRequest object (should handle safari & mozilla)
	{
		xVar = new XMLHttpRequest();
	}else
		// branch for IE/Windows ActiveX version
	{
		xVar = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(xVar){
		xVar.open("GET",sUrl,false);
		xVar.send("");
	}
	

	// branch for native XMLHttpRequest object
  /*  if (window.DOMParser) {
      // Mozilla, create a new DOMParser
      var parser = new DOMParser();
      myDocument = parser.parseFromString(xVar.responseText, "text/xml");
      } else {
      myDocument = new ActiveXObject("Microsoft.XMLDOM")
      myDocument.async="false";
      myDocument.loadXML(xVar.responseText);

	}
	*/
	var parser = new DOMParser();
	myDocument = parser.parseFromString(xVar.responseText,"text/xml");
	//traverse XML object nodes
	var x = myDocument.getElementsByTagName(sTag);
	//attempt to guard against issue of drop down not loading - results just crashes however becuase filter needs to be built first!
	//if(sTag=='make' && x.length==0){
	//	location.href='results.asp'
	//}
	//clear out drop down
	initSel(sSel,"any " + LCase(sTag));
	//set correct counter for ref nodes depending on browser
	if (document.implementation && document.implementation.createDocument)
	{
		var ctr = 1;
	}else
	{
		var ctr = 0;
	}

	//loop thru data in xml file
	for (i=0;i<x.length;i++)
	{
		if(x[i].childNodes[ctr].hasChildNodes())
		{
			oOption = document.createElement('option');
			sVal = UCase(x[i].childNodes[ctr].firstChild.nodeValue);
			if (document.implementation && document.implementation.createDocument)
			{
				oOption.text = sVal;
			}else
			{
				oOption.innerText = sVal;
			}
			oOption.value = sVal;

			oOption.selected = optSelect(sSel,sVal);

			newEl.appendChild(oOption);
		}
	}
}

function initSel(sSel,sTag){
	//alert('initsel ' + sTag);
	var newEl = document.getElementById(sSel);
	var oOption;

	// erase options first so clear to append
	for (i=newEl.options.length-1; i>=0; i--) {
		newEl.options[i] = null;
	}

	//create blank "select" option with value of 0
	oOption = document.createElement('option')
	if (document.implementation && document.implementation.createDocument)
	{
		oOption.text = sTag;
	}else
	{
		oOption.innerText = sTag;
	}
	oOption.value = 0;
	newEl.appendChild(oOption);
}

function optSelect(sSel,sVal){
	switch(LCase(sSel)){
		case 'selmake':
			return LCase(document.getElementById('hdnMake').value)==LCase(sVal);
			break;
		case 'selmodel':
			return LCase(document.getElementById('hdnModel').value)==LCase(sVal);
			break;
		case 'seltrans':
			return LCase(document.getElementById('hdnTrans').value)==LCase(sVal);
			break;
		case 'selyrfrom':
			return LCase(document.getElementById('hdniYrFrom').value)==LCase(sVal);
			break;
		case 'selyrto':
			return LCase(document.getElementById('hdniYrTo').value)==LCase(sVal);
			break;
	}
}
