//using moo class and vector
/****************************************** CLASSES *********************************************/
var certifiedCombos = new Class({
    initialize: function(){
        this.makes = new Array();
    }
});
var Make = new Class({
    initialize: function(id, display){
        this.models =  new Array();
		this.display = display;
		this.id = id;
    }
});
var Model = new Class({
    initialize: function(id, display){
        this.years = new Array();
		this.display = display;
		this.id = id;
    }
});

function show(){
	var i, j, k;
	var jmakes, kmodels, iyears, out=" ";
	jmakes = this.makes.length;
	
	for(j=0; j < jmakes; j++){
		out = out + "\n\n" + this.makes[j].display + "\n\n";
		kmodels = this.makes[j].models.length;
		for(k=0; k < kmodels; k++){
			out = out + "\n" + this.makes[j].models[k].display + "/";
			iyears = this.makes[j].models[k].years.length;
			for (i=0; i < iyears; i++){
				out = out + this.makes[j].models[k].years[i] + "-";
			}
		}
	}
	
	alert(out);
}

//return the position of the id inside the makes vector, if doesn't exists return -1
function indexOfMake(makeId){
	for(i=0; i<this.makes.length; i++){
		if(this.makes[i].id == makeId){
			return i;
		}
	}
	return -1;
}
function indexOfModel(modelId){
	for(i=0; i<this.models.length; i++){
		if(this.models[i].id == modelId){
			return i;
		}
	}
	return -1;
}
function indexOfYear(yearId){
	for(i=0; i<this.years.length; i++){
		if(this.years[i] == yearId){
			return i;
		}
	}
	return -1;
}

certifiedCombos.prototype.indexOfMake = indexOfMake;
certifiedCombos.prototype.show = show;
Make.prototype.indexOfModel = indexOfModel;
Model.prototype.indexOfYear = indexOfYear;

/****************************************** GLOBAL *********************************************/

var xmlCombosFile = "/es/xml/makedata.xml";
var combosData = new certifiedCombos();
var xmlMakeData = createXMLDoc(xmlCombosFile);

function loadHPFlash(eventArgs) {
	if (typeof sIFR == "function" && useFlash) {
		if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6"){
		sIFR.replaceElement(named({nWidth: "900", nHeight: "800", nPaddingTop: "140", oNodeRef: document.getElementById("gWrapper"), sFlashSrc: "/es/flash/home.swf"}));
		}
		else {
		sIFR.replaceElement(named({nWidth: "900", nHeight: "800", oNodeRef: document.getElementById("gWrapper"), sFlashSrc: "/es/flash/home.swf"}));
			}
	}
}

function loadHPFlashSP(eventArgs) {
	if (typeof sIFR == "function" && useFlash) {
		sIFR.replaceElement(named({nWidth: "900", nHeight: "800", oNodeRef: document.getElementById("gWrapper"), sFlashSrc: "/es/flash/home.swf"}));
	}
}


function loadCombos(){
	xmlMakeData = getElementChildNodes(xmlMakeData.childNodes[0]);//clear non element type nodes
	fillCube();
	fillMakes();
	loadYears();
	//combosData.show();
}
/****************************************** FUNCTIONALITY *********************************************/
function fillCube(fYear,tYear){
	var i, j, k, yearXML, actYearObj, iyears, makeXML, actMakeObj, jmakes, modelXML, actModelObj, kmodels, actMakeIndex, actModelIndex;
	if(xmlMakeData!=null){
		iyears = xmlMakeData.getElementsByTagName("year").length;
		for (i=0; i < iyears; i++){	//go trought all  years
			yearXML = xmlMakeData.getElementsByTagName("year")[i];
			jmakes = yearXML.getElementsByTagName("make").length;
			for(j=0; j < jmakes; j++){ //go trought all makes for every year
				makeXML = yearXML.getElementsByTagName("make")[j];//clear the make, to have only model nodes (not blanks nodes in firefox)
				actMakeIndex = combosData.indexOfMake(makeXML.getAttribute("id"));
				if(actMakeIndex==-1){//if the make is not in the makes array, push it
					combosData.makes.push( new Make(makeXML.getAttribute("id"),makeXML.getAttribute("display")) )
					actMakeIndex=combosData.makes.length - 1
				}
				actMakeObj = combosData.makes[actMakeIndex];//stores a reference to the actual make object
				kmodels = makeXML.getElementsByTagName("model").length;
				for(k=0; k < kmodels; k++){ //go trought all makes for every year
					modelXML = makeXML.getElementsByTagName("model")[k];
					actModelIndex = actMakeObj.indexOfModel(modelXML.getAttribute("id"));
					if(actModelIndex==-1){//if the make is not in the makes array, push it
						actMakeObj.models.push( new Model(modelXML.getAttribute("id"),modelXML.getAttribute("display")) );
						actModelIndex = actMakeObj.models.length - 1;
					}
					actModelObj = actMakeObj.models[actModelIndex];
					if(actModelObj.indexOfYear(yearXML.getAttribute("from"))==-1){
						actModelObj.years.push(yearXML.getAttribute("from"));
					}
				}
			}
		}
	}
}

function clearSelect(select){
	select.length = 0;
}

function setSelectToDefault(select, defaultValue, defaultText){
	clearSelect(select);
	select.options[0] = new Option(defaultText,defaultValue);
}

function fillMakes(){
	var i, imakes, select;
	select = document.forms['QSForm'].makes;
	clearSelect(select);
	setSelectToDefault(document.forms['QSForm'].models, "","Modelo");
	setSelectToDefault(document.forms['QSForm'].years, "",strAno);
	imakes = combosData.makes.length;
	select.options[0] = new Option("Marca","");
	for(i=0; i < imakes; i++){
		select.options[i+1] = new Option(combosData.makes[i].display,combosData.makes[i].id);
	}
	// Fix for non-flash version of Homepage on Safire.
	if (BrowserDetect.browser == "Safari"){
		select = document.forms['QSForm'].makes;
		select.options[0] = new Option("Marca","");
		select.options[1] = new Option("Buick","004");
		select.options[2] = new Option("Chevrolet","001");
		select.options[3] = new Option("GMC","012");
		select.options[4] = new Option("Pontiac","002");
		}
	//End Fix
}

function fillModels(selectedIndex){
	var k, kmodels, select, actMakeObj;
	
	// Fix for non-flash version of Homepage.
	if (BrowserDetect.browser == "Safari"){
		if(selectedIndex ==0){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("Modelo","");
		}
		//Change form to Buick Models
		else if(selectedIndex==1){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("Modelo","");
		select.options[1] = new Option("Century","9");
		select.options[2] = new Option("Lacrosse","1195");
		select.options[3] = new Option("Lesabre","1");
		select.options[4] = new Option("Lucerne","1674");
		select.options[5] = new Option("Park Avenue","39");
		select.options[6] = new Option("Rainier","1008");
		select.options[7] = new Option("Regal","13");
		select.options[8] = new Option("Rendezvous","197");
		select.options[9] = new Option("Terraza","1211");
		select.options[10] = new Option("Enclave","2330");					
		}
		//Change form to Chevrolet Models
		else if(selectedIndex==2){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("Modelo","");
		select.options[1] = new Option("Astro Van","1794");
		select.options[2] = new Option("Avalanche","48");
		select.options[3] = new Option("Aveo","1034");
		select.options[4] = new Option("Blazer","31");
		//select.options[5] = new Option("Camaro","19");
		select.options[5] = new Option("Cavalier","2");
		select.options[6] = new Option("Cobalt","1206");
		select.options[7] = new Option("Colorado","1012");
		select.options[8] = new Option("Corvette","22");
		select.options[9] = new Option("Equinox","1061");
		select.options[10] = new Option("Express Van 1500","38");
		select.options[11] = new Option("Express Van 2500","195");
		select.options[12] = new Option("Express Van 3500","32");
		select.options[13] = new Option("HHR","1499");
		select.options[14] = new Option("Impala","29");
		select.options[15] = new Option("Malibu","18");
		select.options[16] = new Option("Malibu Maxx","1145");
		select.options[17] = new Option("Monte Carlo","10");
		//select.options[19] = new Option("Prizm","24");
		select.options[18] = new Option("S10 Pickup","4");
		select.options[19] = new Option("Silverado","15");
		select.options[20] = new Option("Silverado 1500","1832");
		select.options[21] = new Option("Silverado 1500 Classic","2055");
		select.options[22] = new Option("Silverado 1500HD","2313");
		select.options[23] = new Option("Silverado 1500HD Classic","2089");		
		select.options[24] = new Option("Silverado 2500","1848");		
		select.options[25] = new Option("Silverado 2500HD","1836");		
		select.options[26] = new Option("Silverado 2500HD Classic","2048");		
		select.options[27] = new Option("Silverado 3500","1861");		
		select.options[28] = new Option("Silverado 3500HD","2278");		
		select.options[29] = new Option("Silverado 3500 Classic","2111");										
		select.options[30] = new Option("SSR","1011");
		select.options[31] = new Option("Suburban","25");
		select.options[32] = new Option("Tahoe","14");
		select.options[33] = new Option("Tracker","47");
		select.options[34] = new Option("TrailBlazer","972");
		select.options[35] = new Option("Traverse","2348");
		select.options[36] = new Option("Uplander","1193");
		select.options[37] = new Option("Venture","7");
		}
		//Change form to GMC Models
		else if(selectedIndex==3){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("Modelo","");
		select.options[1] = new Option("Acadia","111");
		select.options[2] = new Option("Canyon","1016");
		select.options[3] = new Option("Envoy","134");
		select.options[4] = new Option("Envoy Denali","2326");
		select.options[5] = new Option("Envoy XUV","981");
		select.options[6] = new Option("Jimmy","17");
		select.options[7] = new Option("Safari","45");
		select.options[8] = new Option("Savana G1500","42");
		select.options[9] = new Option("Savana G2500","211");
		select.options[10] = new Option("Savana G3500","139");
		select.options[11] = new Option("Sierra","30");
		select.options[12] = new Option("Sierra Denali","2325");
		select.options[13] = new Option("Sierra 1500","1859");
		select.options[14] = new Option("Sierra 1500HD","2320");
		select.options[15] = new Option("Sierra 1500 Classic","2071");
		select.options[16] = new Option("Sierra 2500","2319");
		select.options[17] = new Option("Sierra 2500HD","1845");
		select.options[18] = new Option("Sierra 2500HD Classic","2100");
		select.options[19] = new Option("Sierra 3500","2317");
		select.options[20] = new Option("Sierra 3500 Classic","2131");
		select.options[21] = new Option("Sonoma","35");
		select.options[22] = new Option("Yukon","40");
		select.options[23] = new Option("Yukon Denali","2332");		
		select.options[24] = new Option("Yukon XL","51");
		select.options[25] = new Option("Yukon XL Denali","2331");
		}
		//Change form to Pontiac Models
		else if(selectedIndex==4){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("Modelo","");
		select.options[1] = new Option("Aztek","196");
		select.options[2] = new Option("Bonneville","28");
		//select.options[3] = new Option("Firebird","5");
		select.options[3] = new Option("G5","108");
		select.options[4] = new Option("G6","1166");
		select.options[5] = new Option("Grand Am","3");
		select.options[6] = new Option("Grand Prix","6");
		select.options[7] = new Option("GTO","1015");
		select.options[8] = new Option("Montana","41");
		select.options[9] = new Option("Montana SV6","1243");
		select.options[10] = new Option("Solstice","1607");
		select.options[11] = new Option("Sunfire","11");
		select.options[12] = new Option("Torrent","1597");
		select.options[13] = new Option("Vibe","193");
		}
		
		else{
		select = document.forms['QSForm'].models;
		clearSelect(select);
		select.options[0] = new Option("MAKE","");
		}
		}

	else if(selectedIndex!=0){
		select = document.forms['QSForm'].models;
		clearSelect(select);
		setSelectToDefault(document.forms['QSForm'].years, "",strAno);
		actMakeObj = combosData.makes[selectedIndex-1];
		kmodels = actMakeObj.models.length;
		select.options[0] = new Option("Modelo","");
		for(k=0; k < kmodels; k++){
			select.options[k+1] = new Option(actMakeObj.models[k].display,actMakeObj.models[k].id);
		}
	}
	
	else {
		fillMakes(); 
	}
	loadYears();
	}

function fillYears(selectedIndex){
	var i, iyears, select, actModelObj;
	var yearText = "";
	
	if (selectedIndex<=0){
		if (BrowserDetect.browser == "Safari"){
		loadYears();
		}
	}
	
	if (BrowserDetect.browser != "Safari"){
		if (selectedIndex>=0){
			
			select = document.forms['QSForm'].years;
			clearSelect(select);
			actModelObj = combosData.makes[document.forms['QSForm'].makes.selectedIndex-1].models[selectedIndex-1];
			iyears = actModelObj.years.length;
			select.options[0] = new Option("Todos los "+ strAno +"s","2003");
			for (i=0; i < iyears; i++){
				if(i==0){yearText = ""}else{yearText = " o "+ strMas +" nuevo";}
				select.options[i+1] = new Option(actModelObj.years[i]+yearText,actModelObj.years[i]);
			}
		}
	}
}

function loadYears(){
	select = document.forms['QSForm'].years;
	select.options[0] = new Option(Ano,"2003");
	select.options[1] = new Option("Todos los "+ strAno +"s","2005");
	select.options[2] = new Option("2010","2010");
	select.options[3] = new Option("2009 o "+strMas+ " nuevo","2009");
	select.options[4] = new Option("2008 o "+strMas+ " nuevo","2008");
	select.options[5] = new Option("2007 o "+strMas+ " nuevo","2007");
	select.options[6] = new Option("2006 o "+strMas+ " nuevo","2006");
	select.options[7] = new Option("2005 o "+strMas+ " nuevo","2005");

}

function testXMLdoc(xmlNode){
	var xmlNodeClean;//node fill with only element childs
	alert("Node name: " + xmlNode.tagName + " Childs:  " + xmlNode.childNodes.length);
	xmlNodeClean = getElementChildNodes(xmlNode);//if xmlNode has element node childs, getElementChildNodes retrieve a new xml object filled with only those element node childs
	if(xmlNodeClean != null){xmlNode = xmlNodeClean;}
	if(xmlNode.childNodes.length==0){alert(xmlNode.tagName + xmlNode.getAttribute("display"));}
	else{for (i=0; i < xmlNode.childNodes.length; i++){testXMLdoc(xmlNode.childNodes[i]);}}
}

AttachEvent(window, "load", loadCombos);

function quickSearch(params) {
	var href = "/cuv/gmcertified/UISResult.jhtml?";
	if (typeof params.Make != "undefined") if (params.Make != "") href += "&Make=" + params.Make;
	if (typeof params.FromYear != "undefined") if (params.FromYear != "") href += "&FromYear=" + params.FromYear;
	if (typeof params.ZipCode != "undefined") if (params.ZipCode != "") href += "&ZipCode=" + params.ZipCode;
	if (typeof params.Model != "undefined") if (params.Model != "") href += "&Model=" + params.Model;
	href = href.replace("?&", "?");
	window.location.href = href;
}

