// Insert IE stylesheet.
document.write('<!--[if lt IE 9]><link href="/en/css/tools/cpo/pCPOResult_ie.css" type="text/css" rel="stylesheet" /><![endif]-->');
document.close();

/*This function replaces an image if it is not found*/
function changeImageDefault(id){
	id.src = BASE_CONTEXT + "/assets/en/images/tools/cpo/cadillac_logo.jpg";
}

/* warranty comparison overlay: when switching brand in dropdown */
function changeWarrantyBrand() {
	var options = document.getElementById("brandsdropdown").options;
	var trs = document.getElementById("wcOverlay").getElementsByTagName("table")[0].getElementsByTagName("tr");
	var selectedBrandIndex = 0;
	for (var j = 1; j < options.length; j++) {
		if(options[j].selected == true){
			selectedBrandIndex = j;
		}
	}
	for (var i = 1; i < trs.length; i++) {
		var spans = trs[i].getElementsByTagName("td")[2].getElementsByTagName("span");
		for (var j = 0; j < options.length; j++) {
			if(j==selectedBrandIndex){
				spans[j].style.display = 'inline';
			} else {
				spans[j].style.display = 'none';
			}
		}
	}
}
function popupWarrantyComparison(){
	var wcContainerStyle = document.getElementById('wcContainer').style;
	if(wcContainerStyle.display!='block'){
		var position = getCenterPosition('wcContainer');
		wcContainerStyle.left = position.x - 255 + "px";
		wcContainerStyle.top = position.y - 250 + "px";
		wcContainerStyle.display='block';
	}

	clickTrack({pageName: BRAND.toUpperCase() +' | DIVISIONAL | CPO | CPO HOME',
				prop1: 'DIVISIONAL',
				prop2: 'CPO',
				prop3: 'DIVISIONAL | CPO',
				prop4: 'CPO HOME',
				prop5: 'CPO | CPO HOME',
				prop6: 'DIVISIONAL | CPO | CPO HOME',
				prop7: 'WARRANTY COMPARISON',
				prop8: 'CPO HOME | WARRANTY COMPARISON',
				prop9: 'CPO | CPO HOME | WARRANTY COMPARISON',
				prop10: 'DIVISIONAL | CPO | CPO HOME | WARRANTY COMPARISON',
				prop24: 'EN',
				prop25: BRAND.toUpperCase(),
				prop26: (new Date()).getHours(),
				prop27: weekday[(new Date()).getDay()]
				});

	return false;
}
function popupCPOExt(query, windowName, width, height){
	if(width == "" || width=='undefined'){width=760;}
	if(height == "" || height=='undefined'){height=700;}
	window.open(BASE_CONTEXT+ '/tools/cpo/popup.do?'+query, windowName, 'width='+width+',height='+height+',left=500,top=20,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
}
function popupCPOExtInterstitial(query){
	if(query.indexOf("?")>=0){query=query.substring(query.indexOf("?")+1);}
	window.open(BASE_CONTEXT+ '/tools/cpo/interstitial.do?'+query, 'interstitial', 'width=500,height=305,left=400,top=400,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
}
function showEPCalculator(ele, price, model, style){
	var peC$ = mrm.$('div#peContainer');
	peC$.css('display', 'block');
	peC$.after('<div id="peBg">');
	peB$ = mrm.$('div#peBg');
	peB$.css('visibility', 'hidden').fadeTo(0, 0.5);
	peB$.css('background', '#000');
	var t = setTimeout("peB$.css('visibility', 'visible')", 50);
	var myOmniVehInfo = getOmniVehInfo(model, style);
	
 	initCalc(price);

	clickTrack({pageName: 'ca:no:us:en:index:vehicles:certified-used-vehicles:search-results:payment-estimator',
				hier1: 'index,vehicles,certified-used-vehicles,search-results,payment-estimator',
				prop1: myOmniVehInfo['model'],
				prop2: myOmniVehInfo['bodyStyle'],
				prop3: myOmniVehInfo['vehicleType'],
				eVar5: myOmniVehInfo['model'],
				eVar6: myOmniVehInfo['bodyStyle'],
				eVar7: myOmniVehInfo['vehicleType'],
				prop10: 'vehicles',
				prop11: 'certified-used-vehicles',
				prop12: 'search-results',
				prop13: 'payment-estimator',
				prop16: 'certified-used-vehicles',
				eVar27: myOmniVehInfo['family'],
				prop33: 'divisional | cpo',
				prop34: 'cpo | payment-estimator',
				prop35: 'divisional | cpo | payment-estimator'
				});
}
function hidePE() {
	mrm.$('div#peBg').remove();
	mrm.$('div#peContainer').css('display', 'none');
	return false;
}
function trackingCPOModelComparison(){
	var divisionObj=BRAND.toUpperCase();
	clickTrack({pageName: divisionObj+' | DIVISIONAL | CPO | VEHICLE SHOWROOM', prop1: 'DIVISIONAL', prop2:'CPO', prop3: 'DIVISIONAL | CPO', prop4:'VEHICLE SHOWROOM', prop5:'CPO | VEHICLE SHOWROOM', prop6: 'DIVISIONAL | CPO | VEHICLE SHOWROOM', prop7:'MODEL COMPARISON', prop8: 'VEHICLE SHOWROOM | MODEL COMPARISON', prop9:'CPO | VEHICLE SHOWROOM | MODEL COMPARISON', prop10:'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON',prop25:divisionObj, prop26:(new Date()).getHours(), prop27: weekday[(new Date()).getDay()]});
}
function trackingCPOModelComparisonPhoto(model,year){
	var divisionObj=BRAND.toUpperCase();
	var modelValue=model.toUpperCase();
	clickTrack({pageName: divisionObj+' | DIVISIONAL | CPO | VEHICLE SHOWROOM', prop1: 'DIVISIONAL', prop2:'CPO', prop3: 'DIVISIONAL | CPO', prop4:'VEHICLE SHOWROOM', prop5:'CPO | VEHICLE SHOWROOM', prop6: 'DIVISIONAL | CPO | VEHICLE SHOWROOM', prop7:'MODEL COMPARISON', prop8: 'VEHICLE SHOWROOM | MODEL COMPARISON', prop9:'CPO | VEHICLE SHOWROOM | MODEL COMPARISON', prop10:'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON',prop11:'PHOTOS | '+modelValue+' | '+year,prop12:'MODEL COMPARISON | PHOTOS | '+modelValue+' | '+year,prop13:'VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+modelValue+' | '+year,prop14:'CPO | VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+modelValue+' | '+year,prop15:'DIVISIONAL | CPO | VEHICLE SHOWROOM | MODEL COMPARISON | PHOTOS | '+modelValue+' | '+year,prop25:divisionObj, prop26:(new Date()).getHours(), prop27: weekday[(new Date()).getDay()]});
}
function showSubmissionAlert(){
	var position = getScreenCenter();
	//alert(position.x+", "+position.y);
	document.getElementById('submissionAlert').style.left = position.x-400+"px";
	document.getElementById('submissionAlert').style.top = position.y-30+"px";
	document.getElementById('submissionAlert').style.display='block';
}

function getOmniModel(myModel) {
	var oModels = ['cts', 'dts', 'escalade', 'srx', 'sts', 'xlr'], oModel = '', i;
	for (i = 0; i < oModels.length; i++) {
		if (myModel.toLowerCase().indexOf(oModels[i]) != -1) {
			oModel = oModels[i];
			break;
		}
	}
	return oModel;
}

function getCtsVehInfo(myModel, myStyle) {
	myModel = myModel.toLowerCase();
	myStyle = myStyle.toLowerCase();
	var ctsVehInfo = new Array();
	if (myModel.indexOf('coupe') != -1 || myStyle.indexOf('coupe') != -1) {
		ctsVehInfo['bodyStyle'] = (myModel.indexOf('v') != -1 || myStyle.indexOf('v') != -1)? 'ctsv coupe': 'cts coupe';
		ctsVehInfo['vehicleType'] = 'coupe';
	} else if (myModel.indexOf('sedan') != -1 || myStyle.indexOf('sedan') != -1) {
		ctsVehInfo['bodyStyle'] = (myModel.indexOf('v') != -1 || myStyle.indexOf('v') != -1)? 'ctsv sedan': 'cts sedan';
		ctsVehInfo['vehicleType'] = 'sedan';
	} else if (myModel.indexOf('wagon') != -1 || myStyle.indexOf('wagon') != -1) {
		ctsVehInfo['bodyStyle'] = (myModel.indexOf('v') != -1 || myStyle.indexOf('v') != -1)? 'ctsv wagon': 'cts wagon';
		ctsVehInfo['vehicleType'] = 'sedan';
	}
	return ctsVehInfo;
}

function getOmniVehInfo(myModel, myStyle) {
	var oVehInfo = new Array(), oModel = getOmniModel(myModel);
	oVehInfo['model'] = oModel;
	switch(oModel) {
		case 'cts':			var myCtsVehInfo = getCtsVehInfo(myModel, myStyle);
							oVehInfo['bodyStyle'] = myCtsVehInfo['bodyStyle'];
							oVehInfo['vehicleType'] = myCtsVehInfo['vehicleType'];
							oVehInfo['family'] = 'cts';
							break;
		case 'dts':			oVehInfo['bodyStyle'] = 'dts';
							oVehInfo['vehicleType'] = 'sedan';
							oVehInfo['family'] = 'dts';
							break;
		case 'escalade':	oVehInfo['bodyStyle'] = myModel.toLowerCase();
							oVehInfo['vehicleType'] = (myModel.indexOf('EXT') == -1)? 'suv': 'truck';
							oVehInfo['family'] = 'escalade';
							break;
		case 'sts':			oVehInfo['bodyStyle'] = (myModel.indexOf('V') == -1)? 'sts sedan': 'sts-v';
							oVehInfo['vehicleType'] = 'sedan';
							oVehInfo['family'] = 'sts';
							break;
		case 'srx':			oVehInfo['bodyStyle'] = 'srx';
							oVehInfo['vehicleType'] = 'suv';
							oVehInfo['family'] = 'srx';
							break;
		case 'xlr':			oVehInfo['bodyStyle'] = (myModel.indexOf('V') == -1)? 'xlr roadster': 'xlr-v';
							oVehInfo['vehicleType'] = '[n/a]';
							oVehInfo['family'] = 'xlr';
							break;
		default:			oVehInfo['bodyStyle'] = '[n/a]';
							oVehInfo['vehicleType'] = '[n/a]';
							oVehInfo['family'] = '[n/a]';
							break;
	}
	return oVehInfo;
}

