

// Ajax Functions
function getProductData(trim) {
      $.post("/cars-trucks/compareService/", { trim: 1 },
          function(data){
	$(data).find('trim').each(function(){
	alert($(this).text());
         });
    });
}

//This function open the get a quote URL using the parameter      
function openGetAQuoteWindow(year, make, model, zipcode, formId){
	document.getElementById(formId).action= "/cars-trucks/getAQuote/?year=" + year + "&model=" + model + "&make=" + make + "&zipcode=" + zipcode;
	return true;
}

//This function clears the text of the specified element
function clearText(id){
	$("#" + id).html("");
}

//This function open the price with options page using the parameter      
function openPriceWithOptions(year, make, model, trimname, trimid){
	
	var spchars = ["\\","/",":","?","\"","<",">","|","*","&","'"];
	model1 = model;
	for(i=0;i<spchars.length;i++){
	
		indexmatch = model.indexOf(spchars[i]);
		while(indexmatch != -1){
			model = model.replace(spchars[i],"");
			indexmatch = model.indexOf(spchars[i]);
		}
	
	}
	var intIndexOfMatch = model.indexOf( " " );
	while (intIndexOfMatch != -1){
		model = model.replace( " ", "-" )
		// Get the index of any next matching substring.
		intIndexOfMatch = model.indexOf( " " );
	}
	intIndexOfMatch = trimname.indexOf( " " );
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		trimname = trimname.replace( " ", "-" )
		// Get the index of any next matching substring.
		intIndexOfMatch = trimname.indexOf( " " );
	}
	var urlPWO="/cars-trucks/" + year + "-" + make+ "-" + model1 + "/" + model + "-" +trimname+ "-" +trimid +"/price-with-options/";
	window.location.href=urlPWO;
}


var sectionVar = "Section";
var scoreVar = "score";
var pricewithoptionsVar = "pricewithoptions";

var sections = new Array();
sections[0] = "pricing";
sections[1] = "overall";
sections[2] = "performance";
sections[3] = "exterior";
sections[4] = "interior";
sections[5] = "safety";
sections[6] = "reliability";

// Clears the trim data for a column
function removeTrimData(pos){
	$("div[id^='" + pos + "_']").parents("td[class^=attrValue").removeClass("optionStandard");
	$("div[id^='" + pos + "_']").parents("td[class^=attrValue").removeClass("optionNotAvailable");
	$("div[id^='" + pos + "_']").parents("td[class^=attrValue").removeClass("optionOptional");
}

function removeCarColumn(column){


	$("div[id^='" + column + "_']").html("<br />");
	$("div[id^='" + column + "_']").parents("div.selectedCarContent").hide();
	$("div[id^='" + column + "_']").parents("div.selectedCar").addClass("noneSelected");
	$("div[id^='" + column + "_']").parents("div.selectedCar").attr("rowTrim","0");
	$("div[id^='" + column + "_']").parents("div.selectedCar").attr("repTrim","0");
	$("div[id^='" + column + "_']").parents("div.score").addClass("noscore");
	$("div[id^='" + column + "_']").parents("td[class^=attrValue").removeClass("optionStandard");
	$("div[id^='" + column + "_']").parents("td[class^=attrValue").removeClass("optionNotAvailable");
	$("div[id^='" + column + "_']").parents("td[class^=attrValue").removeClass("optionOptional");

}

function removeCarData(column){
	var p_top = $('html').offset().top;	
	$("div#progress").css({top: p_top + 150});
	$("div#progress").show();
    window.setTimeout(function() { 
		if(column == 1 && trimArr[1] != "0"){
			changeProduct(trimArr[1],1);	
			removeCarData(2);
		}else if(column == 2 && trimArr[2] != "0"){
			changeProduct(trimArr[2],2);
			removeCarColumn(3);
		} else {
			removeCarColumn(column);
		}
		updateCarousels();
		$("div#progress").hide();
	}
	, 10);    
}




function populateRankingData(rid){
	$.ajax({
            type: "GET",
            url: "/cars-trucks/compareService/?requestType=rankingWidgetData&rid=" + rid,
            dataType: "xml",
            success: function(xml) {
                $(xml).find('widgetData').each(function(){
                	$("#carViewer").html($(this).text());
                }); //close each(
            }
        }); //close $.ajax(
}


// Populates the trim data from the query
function populateTrimData(trimid,trimname,dropdownDivId,trimDivId,pos,cfTrimName){
	$.ajax({
            type: "GET",
            url: "/cars-trucks/compareService/?t=" + trimid + "&trimPos=" + (pos-1) + "&column=" + pos,
            dataType: "xml",
            success: function(xml) {
                $(xml).find('data').each(function(){
                	var type = $(this).attr("type");
                	var attrid = $(this).attr("id");
					if(type == "chrome"){
						$("#" + $(this).attr("id")).html($(this).text());
						$("#" + $(this).attr("id")).parents("td.attrValue").addClass($(this).attr("class"));
		
					}
					if(type == "usnv" && attrid.indexOf("_pricewithoptions") > 0){
						$("#" + $(this).attr("id")).html($(this).text());
					}
                }); //close each(
		$("#" + trimDivId).html(cfTrimName);
		$("div[rowid^='" + pos + "']").attr("rowTrim",trimid);
		updateCarousels();
		popupHide();
            }
        }); //close $.ajax(
}


function populateProductData(trimid, pos){
	$.ajax({
            type: "GET",
            url: "/cars-trucks/compareService/?t=" + trimid + "&trimPos=" + (pos-1) + "&column=" + pos + "&type=1",
            dataType: "xml",
            complete: function(xhr, status) {
            	$('#progress').hide();
            },
            success: function(xml) {
            	tmpCarMake="";
            	tmpCarModel="";
            	$(xml).find('data').each(function(){
		var type = $(this).attr("type");
		if(type == "header"){
				if($(this).attr("id") == (pos + "_carMake")){
					tmpCarMake=$(this).text();
				} else if($(this).attr("id") == (pos + "_carModel")){
					tmpCarModel=$(this).text();
				} else if($(this).attr("id") == (pos + "_carPic")){
					$("#" + $(this).attr("id")).attr("src",$(this).text());
					$("div.selectedCar").each(function(){
						if($(this).attr("rowid") == pos){
							$(this).attr("rowtrim",trimid);
							$(this).attr("reptrim",trimid);
							$(this).removeClass("noneSelected");
							$(this).children("div.selectedCarContent").each(function(){
								$(this).show();
							});
						}
					});
				}

				if($(this).attr("id") == (pos + "_carPic")){

				} else{
					$("#" + $(this).attr("id")).html($(this).text());
				}
			}
		});
		            
            
                $(xml).find('data').each(function(){
                	var type = $(this).attr("type");
					if(type == "chrome"){
						$("#" + $(this).attr("id")).html($(this).text());
						$("#" + $(this).attr("id")).parents("td.attrValue").addClass($(this).attr("class"));
					}else if(type == "usnv"){
						$("#" + $(this).attr("id")).html($(this).text());
					}else if(type == "score"){
						$("#" + $(this).attr("id")).html($(this).text());
						$("#" + $(this).attr("id")).parents("div.score noscore").removeClass("noscore");
					}
                }); //close each(
                
                $("div[rowid^='" + pos).attr("rowTrim",trimid);
                updateCarousels();

		productForTracking=tmpCarMake+" "+tmpCarModel;
                
                popupHide();
		$("#doubleClickIDRight160x600").show();

            }
        }); //close $.ajax(
}


	
// Removes the column data for a particular trim.


//Changes the trim value to the value selected in the dropdown
function changeTrim(trimid,trimname,dropdownDivId,trimDivId,pos,cfTrimName) {
	$("#" + pos + "_carTrim").text("Loading ..");
	chromeShow();
	removeTrimData(pos);
	populateTrimData(trimid,trimname,dropdownDivId,trimDivId,pos,cfTrimName);
	return true;
}


function changeProduct(trimid, pos) {
	chromeShow();
	removeCarColumn(pos);
	populateProductData(trimid, pos);
	return true;
}
