var brand = '';
var allBrandsNode = null;
var makeNode = null;
var currentYearNode = null;
var currentModelNode = null;			
var currentTypeNode = null;
var multiBrandEnabled = true;

var currentYear = "";
var currentModel = "";
var currentModelCode = "";
var currentType = "";
var currentTypeMMC = "";			
var currentStyle = "";
var currentStylePVC = "";

var typeDropDownHidden = false;

//These objects hold the information for the currently selected model, type and style
var selectedModelObject = new Object();
var selectedTypeObject = new Object();
var selectedStyleObject = new Object();

var duplicates = false;

function OnXMLLoaded(){
	
	if (window.ActiveXObject){
	    allBrandsNode = xmlDoc.documentElement;
		
	}else{
	    allBrandsNode = xmlDoc.responseXML.documentElement;
	}
	
	var i = 0;
	if (multiBrandEnabled){					
		fillBrandDropDown();	
		loadStartPageCookies();
	}
	else{
		document.getElementById("brandSelect").style.display = "none";
		while (i < allBrandsNode.childNodes.length){
			if (allBrandsNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF
				if (brand.toUpperCase() == allBrandsNode.childNodes[i].attributes[0].value.toUpperCase())
				{					
					makeNode = allBrandsNode.childNodes[i];
					fillYearDropDown();
					loadStartPageCookies();
				}
			}
			i++;
		}	
	}
	
/* comment these 5 line if your brand does not need the message */
	document.getElementById("topLegendText").innerHTML = "Begin by selecting a " + brand + " vehicle to compare with others.";

}	

function loadXMLFile(){							
	if (window.ActiveXObject){
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4)OnXMLLoaded();
		}
			xmlDoc.load("/xml/brandInfo.xml");
	}
	else if (document.implementation && document.implementation.createDocument){
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4)OnXMLLoaded();
		};
		if (xmlDoc != null) {
			xmlDoc.open("GET","/xml/brandInfo.xml", true);
			xmlDoc.send(null);
		}
	}
}		


function fillBrandDropDown(){
	var i = 0;
	var j = 1;
	while (i < allBrandsNode.childNodes.length){	
		if (allBrandsNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF	
			if(allBrandsNode.childNodes[i].attributes[0].value.indexOf("Saab")==-1 && allBrandsNode.childNodes[i].attributes[0].value.indexOf("Saturn")==-1 && allBrandsNode.childNodes[i].attributes[0].value.indexOf("Hummer")==-1 && allBrandsNode.childNodes[i].attributes[0].value.indexOf("Pontiac")==-1){
				document.getElementById("brandSelect").options[j] = new Option(allBrandsNode.childNodes[i].attributes[0].value, allBrandsNode.childNodes[i].attributes[0].value);
				j++;
			}
		}
		i++;
	}
}


function brandChanged(){
	brand = document.forms["brandsForm"].brandSelect[document.forms["brandsForm"].brandSelect.selectedIndex].value;

	clearYearDropDown();
	clearModelDropDown();
	clearTypeDropDown();
	clearStyleDropDown();
	
	var i = 0;
	while (i < allBrandsNode.childNodes.length){				
		if (allBrandsNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF							
			if (brand.toUpperCase() == allBrandsNode.childNodes[i].attributes[0].value.toUpperCase())
			{
				makeNode = allBrandsNode.childNodes[i];
				fillYearDropDown();			
			}
		}					
		i++;
	}
	
	/* comment this line if your brand does not need the message */
	document.getElementById("yearDivisionModelLegend").innerHTML = brand;
	resetErrorMessages();
}


function fillYearDropDown(){
	var i = 0;
	var j = 1;
	document.getElementById("yearSelect").options.length  = 1;
	while (i < makeNode.childNodes.length){	
		if (makeNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF					
			document.getElementById("yearSelect").options[j] = new Option(makeNode.childNodes[i].attributes[0].value, makeNode.childNodes[i].attributes[0].value);				
			j++;
		}
		i++;
	}
}

function yearChanged(){
	currentYear = document.forms["brandsForm"].yearSelect[document.forms["brandsForm"].yearSelect.selectedIndex].value;
	
	clearModelDropDown();
	clearTypeDropDown();
	clearStyleDropDown();
	
	var i = 0;
	while (i < makeNode.childNodes.length){				
		if (makeNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF							
			if (currentYear.toUpperCase() == makeNode.childNodes[i].attributes[0].value.toUpperCase())
			{
				currentYearNode = makeNode.childNodes[i];
				
				fillModelDropDown();				
			}
		}					
		i++;
	}
	
	/* comment this line if your brand does not need the message */
	document.getElementById("yearDivisionModelLegend").innerHTML = brand + " " + currentYear;
	 
	document.forms["brandsForm"].modelYear.value = currentYear;
	resetErrorMessages();
}

function clearYearDropDown(){
	document.getElementById("yearSelect").options.length  = 1;
}


function fillModelDropDown(){
	var i = 0;
	var j = 1;
	document.getElementById("modelSelect").options.length  = 1;
	while (i < currentYearNode.childNodes.length){
		if ((currentYearNode.childNodes[i].nodeType == 1) && (currentYearNode.childNodes[i].attributes[5].value == "t"))
{ // This get rides of #text belonging to "whitespaces" for FF					
			document.getElementById("modelSelect").options[j] = new Option(currentYearNode.childNodes[i].attributes[0].value + " " + currentYearNode.childNodes[i].attributes[3].value, currentYearNode.childNodes[i].attributes[1].value + "," + currentYearNode.childNodes[i].attributes[4].value);
			j++;
		}
		
		i++;
	}
}

function modelChanged(){
	var myArray = document.forms["brandsForm"].modelSelect[document.forms["brandsForm"].modelSelect.selectedIndex].value.split(',');

	currentModelCode = myArray[0];
	currentSubmodelCode = myArray[1];
	
	clearTypeDropDown();
	clearStyleDropDown();
	
	var i = 0;
	while (i < currentYearNode.childNodes.length){				
		if (currentYearNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF					
			if (currentModelCode.toUpperCase() == currentYearNode.childNodes[i].attributes[1].value.toUpperCase() && currentSubmodelCode.toUpperCase() == currentYearNode.childNodes[i].attributes[4].value.toUpperCase()){
				selectedModelObject.Model = currentYearNode.childNodes[i].attributes[0].value;
				selectedModelObject.Model_Code = currentYearNode.childNodes[i].attributes[1].value;
				selectedModelObject.Image_Location = currentYearNode.childNodes[i].attributes[2].value;
				selectedModelObject.Submodel = currentYearNode.childNodes[i].attributes[3].value;
				selectedModelObject.Submodel_Code = currentYearNode.childNodes[i].attributes[4].value;
			
				currentModelNode = currentYearNode.childNodes[i];
				fillTypeDropDown();				
			}
		}					
		i++;
	}
	
	document.getElementById("imageContainer").innerHTML = "<img src='" + selectedModelObject.Image_Location + "' />"
	
	document.getElementById("yearDivisionModelLegend").innerHTML = brand + " " + currentYear + " " + selectedModelObject.Model;
	resetErrorMessages();
}

function clearModelDropDown(){
	document.getElementById("modelSelect").options.length  = 1;
	document.getElementById("imageContainer").innerHTML = "<img src='/images/compare/no_vehicle.gif' alt='' width='260' height='150'/>";
}

function fillTypeDropDown(){
	var i = 0;
	var j = 1;
	var nodeCount = 0;
	var nodeCountIsStandard = 0;
	
	var allIsStandard = false;
	duplicates = false;

	document.getElementById("typeSelect").options.length  = 1;
	while (i < currentModelNode.childNodes.length){	
		if ((currentModelNode.childNodes[i].nodeType == 1) && (currentModelNode.childNodes[i].attributes[5].value == "t" )){ // This get rides of #text belonging to "whitespaces" for FF												
			var Types = getTypes(currentModelNode.childNodes[i]);
			if(!exists(Types,"typeSelect")){
				document.getElementById("typeSelect").options[j] = new Option(Types, currentModelNode.childNodes[i].attributes[3].value);				
			}else{
				duplicates = true;
				j--;
			}					
			nodeCount++;
			if (Types.toLowerCase().replace(/^\s+|\s+$/g,"") == "standard"){
				nodeCountIsStandard++;
			}
			j++;
		}
		i++;
	}
	
	if (nodeCount == nodeCountIsStandard){
		document.getElementById("typeSelectDIV").style.display = "none";
		typeDropDownHidden = true;
		duplicates = false;
		fillStylesForStandard();	
	}else{
		typeDropDownHidden = false;
	}
}


function exists(element,containerId){
	container = document.getElementById(containerId);
	for(var i = 0; i < container.length; i++){	
		if(container.options[i].text == element)
			return true;
	}
	return false;
}

function typeChanged(){
	currentTypeMMC = document.forms["brandsForm"].typeSelect[document.forms["brandsForm"].typeSelect.selectedIndex].value;				
	
	clearStyleDropDown();
	
	var i = 0;
	while (i < currentModelNode.childNodes.length){				
		if (currentModelNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF										
			if (currentTypeMMC.toUpperCase() == currentModelNode.childNodes[i].attributes[3].value.toUpperCase())
			{
				//selectedTypeObject.Type = currentModelNode.childNodes[i].attributes[0].value;
				selectedTypeObject.MMC = currentModelNode.childNodes[i].attributes[3].value;
				selectedTypeObject.Image_Location = currentModelNode.childNodes[i].attributes[2].value;
									
				currentTypeNode = currentModelNode.childNodes[i];
				if(duplicates != true){
					fillStyleDropDown();				
				}else{
					fillStylesForDuplicates();
				}
			}
		}
		i++;
	}
	resetErrorMessages();
	
}

function fillStylesForDuplicates(){
	var k = 1;
	selected = document.getElementById("typeSelect").options[document.getElementById("typeSelect").selectedIndex].text;
	for (var i = 0; i < currentModelNode.childNodes.length; i++){	
		if (currentModelNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF
			if(currentModelNode.childNodes[i].attributes[0].value == selected || currentModelNode.childNodes[i].attributes[2].value == selected){
				for (var j = 0; j < currentModelNode.childNodes[i].childNodes.length; j++){
					if ((currentModelNode.childNodes[i].childNodes[j].nodeType == 1) && (currentModelNode.childNodes[i].childNodes[j].attributes[3].value == "t")){
						document.getElementById("styleSelect").options[k++] = new Option(currentModelNode.childNodes[i].childNodes[j].attributes[0].value, currentModelNode.childNodes[i].childNodes[j].attributes[1].value);
					}
								
				}
			}
		}
	}
}


function clearTypeDropDown(){
	document.getElementById("typeSelect").options.length  = 1;
	document.getElementById("typeSelectDIV").style.display = "block";
}

function fillStyleDropDown(){
	var i = 0;
	var j = 1;
	document.getElementById("styleSelect").options.length  = 1;
	while (i < currentTypeNode.childNodes.length){	
		if ((currentTypeNode.childNodes[i].nodeType == 1) && (currentTypeNode.childNodes[i].attributes[3].value == "t")){ // This get rides of #text belonging to "whitespaces" for FF										
			document.getElementById("styleSelect").options[j] = new Option(currentTypeNode.childNodes[i].attributes[0].value, currentTypeNode.childNodes[i].attributes[1].value);
			j++;
		}
		i++; 
	}
}
						
function styleChanged(){
	currentStylePVC = document.forms["brandsForm"].styleSelect[document.forms["brandsForm"].styleSelect.selectedIndex].value;
	
	if(duplicates){
		selected = document.getElementById("typeSelect").options[document.getElementById("typeSelect").selectedIndex].text;
		for (var i = 0; i < currentModelNode.childNodes.length; i++){	
			if (currentModelNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF
				if(currentModelNode.childNodes[i].attributes[0].value == selected || currentModelNode.childNodes[i].attributes[2].value == selected){
					for (var j = 0; j < currentModelNode.childNodes[i].childNodes.length; j++){
						if ((currentModelNode.childNodes[i].childNodes[j].nodeType == 1) && (currentStylePVC.toUpperCase() == currentModelNode.childNodes[i].childNodes[j].attributes[1].value.toUpperCase())){							
							selectedStyleObject.Style = currentModelNode.childNodes[i].childNodes[j].attributes[0].value;
							selectedStyleObject.PVC = currentModelNode.childNodes[i].childNodes[j].attributes[1].value;
							selectedStyleObject.MSRP = currentModelNode.childNodes[i].childNodes[j].attributes[2].value;
						}
									
					}
				}
			}
		}
		/*for ( var i = 0; i < currentTypeNode.childNodes.length; i++){				
			if (currentTypeNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF										
				if (currentStylePVC.toUpperCase() == currentTypeNode.childNodes[i].attributes[1].value.toUpperCase())
				{
					selectedStyleObject.Style = currentTypeNode.childNodes[i].attributes[0].value;
					selectedStyleObject.PVC = currentTypeNode.childNodes[i].attributes[1].value;
					selectedStyleObject.MSRP = currentTypeNode.childNodes[i].attributes[2].value;
				}
			}					
		}*/
	}else{

		if (!typeDropDownHidden){	
			for ( var i = 0; i < currentTypeNode.childNodes.length; i++){				
				if (currentTypeNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF										
					if (currentStylePVC.toUpperCase() == currentTypeNode.childNodes[i].attributes[1].value.toUpperCase())
					{
						selectedStyleObject.Style = currentTypeNode.childNodes[i].attributes[0].value;
						selectedStyleObject.PVC = currentTypeNode.childNodes[i].attributes[1].value;
						selectedStyleObject.MSRP = currentTypeNode.childNodes[i].attributes[2].value;
					}
				}					
			}
		}
		else{
			for ( var i = 0; i < currentModelNode.childNodes.length; i++){
				if (currentModelNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF
					for ( var j = 0; j < currentModelNode.childNodes[i].childNodes.length; j++){
						if (currentModelNode.childNodes[i].childNodes[j].nodeType == 1){						
							if (currentStylePVC.toUpperCase() == currentModelNode.childNodes[i].childNodes[j].attributes[1].value.toUpperCase()){
								selectedStyleObject.Style = currentModelNode.childNodes[i].childNodes[j].attributes[0].value;
								selectedStyleObject.PVC = currentModelNode.childNodes[i].childNodes[j].attributes[1].value;
								selectedStyleObject.MSRP = currentModelNode.childNodes[i].childNodes[j].attributes[2].value;
							}
						}
					}
				}	
			}		
		}
	}

/* comment these 2 lines if your brand does not need the message */
	document.getElementById("trimLegend").innerHTML = selectedStyleObject.Style;	
	document.getElementById("MSRPLegend").innerHTML = formatPrice(selectedStyleObject.MSRP);
	
	document.forms["brandsForm"].baseVehicleId.value = currentStylePVC;
	resetErrorMessages();
}

function formatPrice(price){
	decimal = price.indexOf('.');
	var ret;
	//if the price has decimal
	if (decimal >= 0)
		if (price == "999999.00")
			ret = "";
		else	
			ret  = "$" + price.substring(0,price.length-6) + ","+   price.substring((price.length-6),price.length)
		
	return ret;	
}


function clearStyleDropDown(){
	
	document.getElementById("styleSelect").options[0]  = new Option("Select Your Style","-1");
	document.getElementById("styleSelect").options.length  = 1;
	
	document.getElementById("trimLegend").innerHTML = "";
	document.getElementById("MSRPLegend").innerHTML = "";
}


//Gets the string composed only from cab and box from the type string
function getTypes(node){
	
	//get the vues requested cabType, boxType and driveType
	var myReturnString  = node.attributes[0].value + " " + node.attributes[1].value + " " + node.attributes[2].value;
	
	//remove blank spaces, this is to fix a bug in IE
	return myReturnString.replace(/^\s+|\s+$/g,"");
	
}


function resetErrorMessages(){
	document.getElementById("brandreq").style.display = "none";
	document.getElementById("modelreq").style.display = "none";
    document.getElementById("yearreq").style.display = "none";
    document.getElementById("stylereq").style.display = "none";
    document.getElementById("typereq").style.display = "none";
    return true;
}

function checkAndSubmitForm(frm) {
	// hide our error messages
    resetErrorMessages();
    
    var errorsNum  = 0;
	
	if (frm.brandSelect.selectedIndex == 0 && multiBrandEnabled) {
		document.getElementById("brandreq").style.display = "block";
		errorsNum++;
    }

    if (frm.yearSelect.selectedIndex == 0) {
		document.getElementById("yearreq").style.display = "block";
		errorsNum++;
    }

    if (frm.modelSelect.selectedIndex == 0) {
		document.getElementById("modelreq").style.display = "block";
		errorsNum++;
    }
	
    if (frm.typeSelect.selectedIndex == 0 && !typeDropDownHidden) {
		document.getElementById("typereq").style.display = "block";
		errorsNum++;
    }
	
    if (frm.styleSelect.selectedIndex == 0) {
		document.getElementById("stylereq").style.display = "block";
		errorsNum++;
    }
	
	if (document.getElementById("brandSelect").value == "Saab"){
		document.brandsForm.divisionCode.value = "sb";
	}else{
		document.brandsForm.divisionCode.value = "fl";
		}
   
    if (errorsNum == 0){
		frm.submit();
		createStartPageCookies();
	}
	
	
}	

// Fills the style dropdown for all given styles for a model (not for type as usual)
function fillStylesForStandard(){
	var i = 0;
	var j = 0;
	var k = 1;

	while (i < currentModelNode.childNodes.length){	
		if (currentModelNode.childNodes[i].nodeType == 1){ // This get rides of #text belonging to "whitespaces" for FF
			j = 0;
			while(j < currentModelNode.childNodes[i].childNodes.length){
				if ((currentModelNode.childNodes[i].childNodes[j].nodeType == 1) && (currentModelNode.childNodes[i].childNodes[j].attributes[3].value == "t")){
					document.getElementById("styleSelect").options[k++] = new Option(currentModelNode.childNodes[i].childNodes[j].attributes[0].value, currentModelNode.childNodes[i].childNodes[j].attributes[1].value);
				}
			
				j++;				
			}
		}
		i++;
	}
	sortlist();
}

function createStartPageCookies(){
	eraseCookie("mySelectedBrand");
	eraseCookie("mySelectedYear");
	eraseCookie("mySelectedModel");
	eraseCookie("mySelectedType");
	eraseCookie("mySelectedStyle");
	eraseCookie("typeDropDownHidden");	
	
	var mySelectedBrand = document.brandsForm.brandSelect.options[document.brandsForm.brandSelect.selectedIndex].value;
	var mySelectedYear = document.brandsForm.yearSelect.options[document.brandsForm.yearSelect.selectedIndex].value;
	var mySelectedModel = document.brandsForm.modelSelect.options[document.brandsForm.modelSelect.selectedIndex].value;
	var mySelectedType = document.brandsForm.typeSelect.options[document.brandsForm.typeSelect.selectedIndex].value;
	var mySelectedStyle = document.brandsForm.styleSelect.options[document.brandsForm.styleSelect.selectedIndex].value;	
	
	createCookie("mySelectedBrand",mySelectedBrand,30);
	createCookie("mySelectedYear",mySelectedYear,30);
	createCookie("mySelectedModel",mySelectedModel,30);
	createCookie("mySelectedType",mySelectedType,30);
	createCookie("mySelectedStyle",mySelectedStyle,30);
	createCookie("typeDropDownHidden",typeDropDownHidden,30);
}

function loadStartPageCookies(){
	var mySelectedBrand = readCookie("mySelectedBrand");
	var mySelectedYear = readCookie("mySelectedYear");
	var mySelectedModel = readCookie("mySelectedModel");
	var mySelectedType = readCookie("mySelectedType");
	var mySelectedStyle = readCookie("mySelectedStyle");
	var isTypeDropDownHidden = readCookie("typeDropDownHidden");
	
	if (mySelectedBrand != null && multiBrandEnabled){	
		document.brandsForm.brandSelect.value = mySelectedBrand;
		brandChanged();
	}
	
	if (mySelectedYear != null){
		document.brandsForm.yearSelect.value = mySelectedYear;
		yearChanged()
	}
	
	if (mySelectedModel != null){
		document.brandsForm.modelSelect.value = mySelectedModel;
		modelChanged();
	}
	
	if (mySelectedType != null){
		document.brandsForm.typeSelect.value = mySelectedType;
		if (isTypeDropDownHidden  == "false"){
			typeChanged();
		}
	}
	
	if (mySelectedStyle != null){
		document.brandsForm.styleSelect.value = mySelectedStyle;
		styleChanged();
	}
}

function sortlist() {
		
          //  var lb=;
			var lb = document.getElementById('styleSelect');
            var oldValue;

            try
            {
            oldValue = lb[lb.selectedIndex].value;

            arrTexts = new Array();

            for(i=0; i<lb.length; i++) {
               arrTexts[i] =  lb.options[i].value +':'+ lb.options[i].text;
            }
            arrTexts.sort();
            for(i=0; i<lb.length; i++) {
                el = arrTexts[i].split(':');
				lb.options[i].value = el[0];
                lb.options[i].text = el[1];
               
                if(oldValue == lb.options[i].value)
                    lb.options[i].selected = true;
                else
                    lb.options[i].selected = false;
            }

            }catch(e)
            {
            //In case of any error, dont do anything.
            alert('The following error occurred: ' + e.name + ' - ' + e.message);
            lb=idGiven;
            }
}
