
//-------------------------------------------------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.xml","make");//	14/1/05
		initSel("selModel","ANY MODEL");
}

function selMakeChange(){
	//document.getElementById('hdnMake').innerText=value=document.getElementById('selMake').value;
	loadSel("selModel","xmlModelwl.asp?make=" + document.getElementById('selMake').value,"model");
	//loadSel("selTrans","xmlTrans.asp?make=" + document.getElementById('selMake').value ,"transmission");
	//loadSel("selYrFrom","../xmlYearAsc.asp?make=" + document.getElementById('selMake').value,"year");
	//loadSel("selYrTo","../xmlYearAsc.asp?make=" + document.getElementById('selMake').value,"year");
}

function refMakeChange(){
	loadSel("refModel","xmlModel.asp?make=" + document.getElementById('refMake').value,"model");
}

function refModelChange(){
	loadSel("refBadge","xmlBadge.asp?make=" + document.getElementById('refMake').value + "&model=" + document.getElementById('refModel').value,"badge");
}

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){
		 window.alert("no makes found");
	}


	//clear out drop down
	switch(sSel){
		case 'selMake':
			initSel(sSel,"ANY MAKE");
			break;
		case 'selModel':
			initSel(sSel,"ANY MODEL");
			break;
		case 'refModel':
			initSel(sSel,"ANY MODEL");
			break;
	}

	//initSel(sSel,"All " + sTag + "s");

	//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 = 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 = "";
	newEl.appendChild(oOption);
}

function val(t) {
	if(document.getElementById('selMake').value==''&&document.getElementById('selModel').value==''&&document.getElementById('selState').value=='') {
		document.getElementById('err').style.visibility = '';
	} else {
		document.getElementById('t').value=t;
		document.forms.frmSearch.submit();
	}
}

