// JavaScript Document
var makedropdowncount = 0;
var modeldropdowncount = 0;
var http_request = false;

function fnShowElement(passedElement) {
	if (document.getElementById(passedElement).style.display =='none') {
		if ((browser.isIE55 || browser.isIE6up) && browser.isWin32) {
			document.getElementById(passedElement).style.display='block';
		}
		else {
			document.getElementById(passedElement).style.display='table-cell';
		}
	}
}

function fnHideElement(passedElement) {
	if (document.getElementById(passedElement).style.display !='none') {
			document.getElementById(passedElement).style.display='none';
	}
}

function makeRequest(url, callback_function, return_xml) {
	http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
  	http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) 
    	{http_request.overrideMimeType('text/xml');}
  } 
  else if (window.ActiveXObject) { // IE
	    try {
	        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	        try {
	            http_request = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e) {}
	    }
	}
 	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
	http_request.onreadystatechange = function() {
  	if (http_request.readyState == 4) {
    	if (http_request.status == 200) {
      	if (return_xml) {
					eval(callback_function + '(http_request.responseXML)');
        } 
				else {
					eval(callback_function + '(http_request.responseText)');
        }
      } 
			else {
      	alert('There was a problem with the request.(Code: ' + http_request.status + ')');
      }
    }
  } 			
  http_request.open('GET', url, true);
  http_request.send(null);
}

function alertContents(text) {
	alert(text)
}

function GetSubClasses(vehicleclass) {
	makeRequest('/Search/AdvSearch_V.cfm?classID='+vehicleclass, 'fnPopulateSubClasses');
	return false;
}

function fnPopulateSubClasses(PassedText) {
	var aryPassedText = PassedText.split("|");
	document.searchform.subclasses.length = 1;
	document.searchform.subclasses.length = aryPassedText.length;
	for (i=1; i < aryPassedText.length; i++) {
		var subclasslist =  aryPassedText[i].split(",");
		document.searchform.subclasses[i].value = subclasslist[0];
		document.searchform.subclasses[i].text = subclasslist[1];
	}
}

function GetMakes(Class) {
	if (Class != 0) {
		makeRequest('/Search/AdvSearch_V.cfm?Class='+Class+'&IsNew='+document.searchform.NewUsed.value, 'fnPopulateMakes',true);
		var aryPassedClass = Class.split(",");
		return false;
	}
	else {
		document.searchform.make.length = 1;
		document.searchform.make[0].text = 'Select a class';
		document.searchform.make[0].value = '';
		document.searchform.model.length = 1;
		document.searchform.model[0].text = 'Select a class';
		document.searchform.model[0].value = '';
		return false;
	}
}

function fnPopulateMakes(PassedXML) {
	var collection = PassedXML.getElementsByTagName('make');
	var classID = PassedXML.getElementsByTagName('classID');
	var subclassid = PassedXML.getElementsByTagName('subclassid');

	document.searchform.make.length = 1;
	document.searchform.make.length = collection.length+1;
	document.searchform.make[0].text = 'All Makes';
	document.searchform.make[0].value = '';
	for (i=1; i <= collection.length; i++) {
		//alert(collection.item(i-1).firstChild.nodeValue+','+classID.item(0).firstChild.nodeValue);
		//document.searchform.make[i].value = collection.item(i-1).firstChild.nodeValue+','+classID.item(0).firstChild.nodeValue;
		//document.searchform.make[i].text = collection.item(i-1).firstChild.nodeValue;
		document.searchform.make[i].value = collection.item(i-1).firstChild.nodeValue+'^^'+classID.item(0).firstChild.nodeValue+'^^'+subclassid.item(0).firstChild.nodeValue;
		document.searchform.make[i].text = collection.item(i-1).firstChild.nodeValue;
}
	document.searchform.make[0].seletected = true;
	document.searchform.model[0].text = 'All Models';
	document.searchform.model[0].value = '';
	GetModels(document.searchform.make[0].value);
}

function GetModels(Make) {
	if (Make != '') {
		//alert('/Search/AdvSearch_V.cfm?Make='+Make+'&IsNew='+document.searchform.NewUsed.value);
		makeRequest('/Search/AdvSearch_V.cfm?Make='+Make+'&IsNew='+document.searchform.NewUsed.value, 'fnPopulateModels',true);
		return false;
	}
	else {
		document.searchform.model.length = 1;
		document.searchform.model[0].text = 'All Models';
		document.searchform.model[0].value = '';
	}
}

function fnPopulateModels(PassedXML) {
	var collection = PassedXML.getElementsByTagName('model')
	document.searchform.model.length = 1;
	document.searchform.model.length = collection.length+1;
	document.searchform.model[0].text = 'All Models';
	document.searchform.model[0].value = '';
	for (i=1; i < (collection.length+1); i++) {
		//document.searchform.model[i].text = collection.item(i-1).firstChild.nodeValue;
		document.searchform.model[i].text = collection.item(i-1).firstChild.nodeValue+' ('+collection.item(i-1).getAttribute("count")+')';
		document.searchform.model[i].value = collection.item(i-1).firstChild.nodeValue;
	}
	document.searchform.model[0].selected = true;
}
