﻿/* General Functions */

// Get parent path name; This is so we can stay in the right directory at the top level when we change pages
var sParentPath = new String(parent.document.location);
if (sParentPath.indexOf('?') > 0) {
	sParentPath = sParentPath.substring(0 , sParentPath.indexOf('?'));
}
var aParts = sParentPath.split('/');
aParts[aParts.length-1] = '';
sParentPath = aParts.join('/');



// QueryString state functions
var oQS = Array();

// Function: SetQueryState
// This function will set the oQS hash based on the querystring variables passed in
function SetQueryState(sState) {

    var aTmpArray=sState.split('&');
    var iLength=aTmpArray.length;
	var aTmp;
	for (var iCount = 0; iCount < iLength; iCount++) {
		aTmp = '';
		aTmp = aTmpArray[iCount].split("=");
		if (aTmp[1] != '') {
			oQS[aTmp[0]] = aTmp[1];
		}
	}

}


// Function: MakeQueryStringFromoQS
// This function will create a string from the oQS hash
function MakeQueryStringFromoQS() {

	var sReturn = '';
	for( var i in oQS) {
		if ( (i != 'Language') ) {
			sReturn += i + '=' + oQS[i] + '&';
		}
	}

	// Get rid of last &
	sReturn = sReturn.substr(0 , sReturn.length-1);

	return sReturn;

}


// Function: GoToStep
function GoToStep(sStep) {

	// There are certain pages that we don't want to overwrite the laststep
	// models_change: Since we will need to go to trims_change
	// select_competitor_similar_back: Since we will need to go back to results
	// select_competitor_honda_back: Since we will need to go back to results
	// select_competitor_custom_back: Since we will need to go back to results
	if ( (sCurrentStep != 'models_change') && (sCurrentStep != 'select_competitor_similar_back')&& (sCurrentStep != 'select_competitor_honda_back')&& (sCurrentStep != 'select_competitor_custom_back') ) {
		// Get current state
		oQS['LastState'] = '';
		oQS['LastState'] = UFURLEncode(parent.document.location.pathname + '?' + MakeQueryStringFromoQS(oQS));
	}

	// if leaving a step, change filter to nothing
	if ( (sCurrentStep != sStep) && (sStep.indexOf('_print') < 0) ) {
		oQS['Filter'] = '';
		oQS['Mode'] = ''
	}
	
	// if leaving the photo page, reset the 'Photo' key
	if ( (sStep != 'results_photos') && (sStep != 'results_photos_print') ) {
		oQS['Photo'] = '';
	}
	

	switch (sStep) {
		case 'change_model':
			parent.document.location = sParentPath + 'models-change.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'model':
			parent.document.location = sParentPath + 'models.aspx';
			break;
		case 'change_trim':
			parent.document.location = sParentPath + 'trims-change.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'trims':
			parent.document.location = sParentPath + 'trims.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'trimsp':
			parent.document.location = sParentPath + 'trims.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'trimleveloverview':
			ShowAlertBlackout();
            document.getElementById('lyrBigPopup').style.display = 'block';
			document.getElementById('bigpopupframe').src = sParentPath + 'trim-levels.aspx?ModelName=' + oQS['ModelName'];
			break;
		case 'select_competitor_similar':
			parent.document.location = sParentPath + 'select-competitor-similar.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_similar_change':
			parent.document.location = sParentPath + 'select-competitor-similar-change.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_similar_back':
			parent.document.location = sParentPath + 'select-competitor-similar-back.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_honda':
			parent.document.location = sParentPath + 'select-competitor-honda.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_honda_change':
			parent.document.location = sParentPath + 'select-competitor-honda-change.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_honda_back':
			parent.document.location = sParentPath + 'select-competitor-honda-back.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_custom':
			parent.document.location = sParentPath + 'select-competitor-custom.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_custom_change':
			parent.document.location = sParentPath + 'select-competitor-custom-change.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'select_competitor_custom_back':
			parent.document.location = sParentPath + 'select-competitor-custom-back.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_overview':
			oQS['Change'] = '';
			parent.document.location = sParentPath + 'results-overview.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_overview_print':
				parent.document.location = sParentPath + 'results-overview-print.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_photos':
			parent.document.location = sParentPath + 'results-photos.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_photos1':
			oQS['Filter'] = 1;
			parent.document.location = sParentPath + 'results-photos.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_photos2':
			oQS['Filter'] = 2;
			parent.document.location = sParentPath + 'results-photos.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_photos3':
			oQS['Filter'] = 3;
			parent.document.location = sParentPath + 'results-photos.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_photos_print':
				parent.document.location = sParentPath + 'results-photos-print.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_pricing':
			parent.document.location = sParentPath + 'results-pricing.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_pricing_print':
				parent.document.location = sParentPath + 'results-pricing-print.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_advantages':
			parent.document.location = sParentPath + 'results-advantages.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
		case 'results_advantages_print':
				parent.document.location = sParentPath + 'results-advantages-print.aspx?' + MakeQueryStringFromoQS(oQS);
			break;
	}

}


// Function: Add competitor
// This function will add a new competitor
function AddCompetitor(sAICNum) {

	if (sAICNum == '') {
		return;
	}

	// Check to see if this is a Change page or not
	if (typeof(iChange) == 'undefined') {

		// Figure out which competitor this is
		if (typeof(oQS['AICNum2']) == 'undefined' ) {
			oQS['AICNum2'] = sAICNum;
		} else if (typeof(oQS['AICNum3']) == 'undefined') {
			oQS['AICNum3'] = sAICNum;
		} else if (typeof(oQS['AICNum4']) == 'undefined') {
			oQS['AICNum4'] = sAICNum;
		} else {
			// If there are 3 competitors, so show the warning alert
			scrollTo(0,0);
			ShowAlertBlackout();
			document.getElementById('lyrAlertPopup').style.left = 169;
			document.getElementById('lyrAlertPopup').style.top = 140;
			document.getElementById('lyrAlertPopup').style.visibility = 'visible';
			document.getElementById('alertpopupframe').src = sParentPath + 'alert-maximum-competitors.aspx';
			return;
		}

	} else {

		// This is a change page, so change the AIC number of the change vehicle
		oQS['AICNum' + (iChange + 1)] = sAICNum;

	}

	GoToStep(sCurrentStep);

}


// Function: RemoveCompetitor
// This function will remove a competitor
function RemoveCompetitor(iNum) {

	oQS['AICNum' + (iNum + 1)] = '';
	
	RealignCompetitors();

	// If we're not on a competitor selection page, and there are no competitors left, redirect user to competitor selection page
	if ( ( (typeof(oQS['AICNum2']) == 'undefined') || (oQS['AICNum2'] == '') ) && (sCurrentStep.indexOf("select_competitor") < 0) ) {
		GoToStep('select_competitor_custom');
	} else {
		GoToStep(sCurrentStep);
	}

}


// Function: Realign Competitors
// This function will realign the competitors so there are no gaps in numbers
function RealignCompetitors() {

	var aCompetitors = Array();
	
	if ( (typeof(oQS['AICNum2']) != 'undefined') && (oQS['AICNum2'] != '') ) {
		aCompetitors[aCompetitors.length] = oQS['AICNum2'];
	}
	if ( (typeof(oQS['AICNum3']) != 'undefined') && (oQS['AICNum3'] != '') ) {
		aCompetitors[aCompetitors.length] = oQS['AICNum3'];
	}
	if ( (typeof(oQS['AICNum4']) != 'undefined') && (oQS['AICNum4'] != '') ) {
		aCompetitors[aCompetitors.length] = oQS['AICNum4'];
	}
	
	if (aCompetitors.length >= 1) {
		oQS['AICNum2'] = aCompetitors[0];
	} else {
		oQS['AICNum2'] = '';
	}
	
	if (aCompetitors.length >= 2) {
		oQS['AICNum3'] = aCompetitors[1];
	} else {
		oQS['AICNum3'] = '';
	}

	if (aCompetitors.length >= 3) {
		oQS['AICNum4'] = aCompetitors[2];
	} else {
		oQS['AICNum4'] = '';
	}

}

// Function: ShowAlertBlackout
function ShowAlertBlackout() {

	// Dynamically resize blockout layer based on size of screen

    if (self.innerWidth)
        {
	        frameWidth = self.innerWidth;
	        frameHeight = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientWidth)
        {
	        frameWidth = document.documentElement.clientWidth;
	        frameHeight = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
	        frameWidth = document.body.clientWidth;
	        frameHeight = document.body.clientHeight;
        }


	var iBiggerHeight = document.body.scrollHeight > screen.height ? document.body.scrollHeight : screen.height;

	document.getElementById('lyrAlertBlockout').style.height = iBiggerHeight
	document.getElementById('lyrAlertBlockout').style.width = frameWidth
	document.getElementById('lyrAlertBlockout').style.display = 'block';

	if (parent.comparenav) {
		parent.comparenav.document.getElementById('lyrAlertBlockout').style.height = iBiggerHeight;
		parent.comparenav.document.getElementById('lyrAlertBlockout').style.display = 'block';
	}

}


// Function: CloseLargePopup
function CloseLargePopup() {

	HideAlertBlackout();
	document.getElementById('lyrBigPopup').style.display = 'none';
	document.getElementById('bigpopupframe').src = '/blank.html';
}


// Function: CloseAlertPopup
function CloseAlertPopup() {

	HideAlertBlackout();
	document.getElementById('lyrAlertPopup').style.left = -1000;
	document.getElementById('lyrAlertPopup').style.top = -1000;
	document.getElementById('lyrAlertPopup').style.visibility = 'hidden';
	document.getElementById('alertpopupframe').src = '/blank.html';

}


// Function: HideAlertBlackout
function HideAlertBlackout() {

	document.getElementById('lyrAlertBlockout').style.display = 'none';

	if (parent.comparenav) {
		parent.comparenav.document.getElementById('lyrAlertBlockout').style.display = 'none';
	}

}


// Function: ChangeModel
function ChangeModel(sModelName , iModelYear , iAICGroupNum) {

	oQS['ModelName'] = sModelName;
	oQS['ModelYear'] = iModelYear;
	oQS['AICGroupNum'] = iAICGroupNum
	oQS['AICNum1'] = '';
	GoToStep('change_trim');

}


// Function: GoToLastState
// Goes back to the last state for when we're in little subprocesses
function GoToLastState() {

	parent.document.location = unescape(oQS['LastState']);

}


// Function: UFURLEncode
// URLEncodes a passed in string
function UFURLEncode(sValue)
{

	sValue += '';	// Convert sValue to a string

	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

		var encoded = "";
	for (var i = 0; i < sValue.length; i++ ) {

		var ch = sValue.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;

}




function UFGetQueryStringValue(sName) {

	var sQueryString = parent.parent.location.toString();
	
	if (sQueryString.indexOf('?') < 0) {
		return '';
	}

	sQueryString = sQueryString.split("?")[1];

	var aPairs = sQueryString.split("&");
	var aTmp;
	for (var iCount = 0; iCount < aPairs.length; iCount++) {
		aTmp = aPairs[iCount].split("=");
		if (aTmp[0].toLowerCase() == sName.toLowerCase()) {
			return aTmp[1];
		}
	}
	return '';
}








/* Trims */

var sSelectedModelId = '';		// Selected model_id
var iSelectedTrimRowNum = '';	// Select trim row number


// This function will highlight a trim selector model cell
function HighlightModelCell(sCell) {

	var aCellInfo = sCell.split("_");
	var sModelId = aCellInfo[0];
	var iTrimRow = aCellInfo[1];

	if (sModelId != '') {
		document.getElementById('ModelCell_' + sModelId).style.backgroundColor='#CBD9E8';
		document.getElementById('TrimCell_' + iTrimRow).style.backgroundColor='#CBD9E8';
	}
}


// bOverride is used to distinguish between rolling off of the selected model (where we don't want to unhighlight) and
//  and when we unhighlight the selected model by selecting another one
function UnHighlightModelCell(sCell , bOverride) {

	var aCellInfo = sCell.split("_");
	var sModelId = aCellInfo[0];
	var iTrimRow = aCellInfo[1];


	// We don't want to unhighlight the selected model if we pass over it
	// bOverride overrides this logic if another model was selected
	if ( (sModelId != sSelectedModelId) || (bOverride) ) {
		document.getElementById('ModelCell_' + sModelId).style.backgroundColor='#F6FAFF';
	}
	
	// We don't want to unhighlight the selected trim if we pass over it
	// bOverride overrides this logic if another model was selected
	if ( (iTrimRow != iSelectedTrimRowNum) || (bOverride) ) {
		document.getElementById('TrimCell_' +  iTrimRow).style.backgroundColor='#F6FAFF';
	}

}


// Function: SelectModel
// This function will select a model
function SelectModel(sCell , sAICPhotoPath , sTrimName , sModelName , sAICNum , iModelYear , sModelGroupName) {

	var aCellInfo = sCell.split("_");
	var sModelId = aCellInfo[0];
	var iTrimRow = aCellInfo[1];

	
	// If there is already a selected model, unselect it
	if (sSelectedModelId != '') {
		document.getElementById('Model_' + sSelectedModelId).src = '/images/tools/compare/radio-off.gif';
		UnHighlightModelCell(sSelectedModelId + '_' + iSelectedTrimRowNum , 1);
	}
	document.getElementById('Model_' + sModelId).src = '/images/tools/compare/radio-on.gif';
	HighlightModelCell(sCell);
	iSelectedTrimRowNum = iTrimRow;
	sSelectedModelId = sModelId;

	// Change model stage
	ShowModelStage(sAICPhotoPath , sTrimName , sModelName , iModelYear , sModelGroupName);

	// Set querystring param
	oQS['AICNum1'] = sAICNum;

}


// Function: ShowModelStage
function ShowModelStage(sPhotoURL , sTrimName , sModelName , iYear , sGroupName) {

	var sYourHondaText;
	if (sLanguage == 'en') {
		sYourHondaText = 'Your Honda';
	} else {
		sYourHondaText = 'Tu Honda';
	}
	
	// In some cases, the trim name has the model group name in it. In this instances, when we concatenate
	//	them together, you see the model group name repeated (e.g. 2007 Fit Fit Sport Manual Transmission)
	// Let's check for this case and blank out the model group name in these instances so it shows properly
	//	(e.g. 2007 Fit Sport Manual Transmission)
	if (sTrimName.indexOf(sGroupName) >= 0) {
		sGroupName = '';
	}


	sContent = '';
	sContent += '<table width="359" cellspacing="0" cellpadding="0" border="0">\n';
	sContent += '<tr>\n';
	sContent += '	<td width="359" colspan="5" bgcolor="#cccccc"><img src="/images/trans.gif" width="1" height="1" alt="" border="0" /></td>\n';
	sContent += '</tr>\n';
	sContent += '<tr>\n';
	sContent += '	<td width="1" bgcolor="#cccccc"><img src="/images/trans.gif" width="1" height="1" alt="" border="0" /></td>\n';
	sContent += '	<td width="131" bgcolor="#666666" align="right" valign="middle">\n';
	sContent += '		<img src="/images/trans.gif" width="1" height="4" alt="" border="0" /><br />\n';
	sContent += '		<img src="' + sPhotoURL + '" alt="" border="0" /><br />\n';
	sContent += '		<img src="/images/trans.gif" width="1" height="4" alt="" border="0" /><br />\n';
	sContent += '	</td>\n';
	sContent += '	<td width="7" bgcolor="#666666"><img src="/images/trans.gif" width="7" height="1" alt="" border="0" /></td>\n';
	sContent += '	<td width="219" bgcolor="#666666" class="white11" valign="middle">\n';
	sContent += '		<strong class="grey11">' + sYourHondaText + '</strong><br />\n';
	sContent += '		<strong>' + iYear + ' ' + sGroupName + '</strong><br />\n';
	sContent += '		' + sTrimName + ' ' + sModelName + '<br />\n';
	sContent += '</td>\n';
	sContent += '	<td width="1" bgcolor="#cccccc"><img src="/images/trans.gif" width="1" height="1" alt="" border="0" /></td>\n';
	sContent += '</tr>\n';
	sContent += '<tr>\n';
	sContent += '	<td width="359" colspan="5" bgcolor="#cccccc"><img src="/images/trans.gif" width="1" height="1" alt="" border="0" /></td>\n';
	sContent += '</tr>\n';
	sContent += '</table>\n';

	document.getElementById('lyrModelStage').innerHTML = sContent;

}


/* Results */

var iActivePhotoThumb = '';	// Active photo thumbnail



// Function: GoToBuildAndPrice
// This function will open up the Build and Price tool, given a specific model_id
function GoToBuildAndPrice(sModelID) {
	parent.document.location = '/tools/build-price/?ModelID=' + sModelID;
}


// Function: ChangeCompetitor
// This function will start the process to change a specific competitor
function ChangeCompetitor(iNum) {

	oQS['Change'] = iNum;
		GoToStep('select_competitor_similar_change');
}


// Function: ChangeFilter
// This function will change the 'Filter' parameter and reload the current page
function ChangeFilter(iCount) {

	oQS['Filter'] = iCount;
	GoToStep(sCurrentStep);

}


// Function: ChangeMode
// This function will change the 'Mode' parameter and reload the current page
function ChangeMode(sMode) {

	oQS['Mode'] = sMode;
	GoToStep(sCurrentStep);

}



// Function: ChangePhoto
// This function will Change the current photo being displayed
function ChangePhoto(sImage1URL , sImage2URL , iPhotoNum) {

	// Change large photos
	document.getElementById('BaseCarLargePhoto').src=sImage1URL;
	document.getElementById('CompetitorPhoto').src=sImage2URL;

	// Turn off the previous active thumbnail highlight
	if (iActivePhotoThumb != '') {
		document.getElementById('PhotoThumb' + iActivePhotoThumb).className = 'image';
	}

	// Highlight new active thumbnail
	document.getElementById('PhotoThumb' + iPhotoNum).className = 'imageon';

	// Set active thumbnail number
	iActivePhotoThumb = iPhotoNum;
	
	// Change 'Photo' parameter so we can pass in the proper photo to the print page
	oQS['Photo'] = iActivePhotoThumb;

}


// Function: StartProcessOver
// This function will allow the user to start the entire Comparison process over
function StartProcessOver() {

	scrollTo(0,0);
	ShowAlertBlackout();
	document.getElementById('lyrAlertPopup').style.left = 169;
	document.getElementById('lyrAlertPopup').style.top = 140;
	document.getElementById('lyrAlertPopup').style.visibility = 'visible';
	document.getElementById('alertpopupframe').src = sParentPath + 'alert-start-over.aspx';
	return false;

}




/* Select Competitor */


// Function: CancelSelectionBack
// This function will alert to the user that they are about to cancel changes they've made
//  to the select_competitors_back pages
function CancelSelectionBack() {

	scrollTo(0,0);
	ShowAlertBlackout();
	document.getElementById('lyrAlertPopup').style.left = 169;
	document.getElementById('lyrAlertPopup').style.top = 140;
	document.getElementById('lyrAlertPopup').style.visibility = 'visible';
	document.getElementById('alertpopupframe').src = sParentPath + 'alert-return-to-results.aspx';
	return false;

}

//Function TrackCompetitorClick
//This Function Tracks which competitor vehicle and comparison type a user choose
//and pass this info to omniture
function TrackCompetitorClick(compareTypeId,mainSAICNum,competitorSAICNum){
    var compareType;
    if(compareTypeId==1)compareType="Similar Vehicles";
    else if(compareTypeId==2)compareType="Honda Vehicles";
    else if(compareTypeId==3)compareType="Custom Make & Model";
    var s=s_gi(s_account);
    s.linkTrackVars='prop17,prop18,prop19,prop21,prop23,prop24,prop25,eVar21,eVar23,eVar24,eVar25';
    s.prop17=compareType;
    s.prop18=mainSAICNum;
    s.prop19=competitorSAICNum;
    s.prop21=Currentprop21;
    s.prop23=Currentprop23;
    s.prop24=Currentprop24;
    s.prop25=Currentprop25;
    s.eVar21=s.prop21;
    s.eVar23=s.prop23;
    s.eVar24=s.prop24;
    s.eVar25=s.prop25;
    s.tl(this,'o','COMPARE VEHICLE COMPETITOR CLICK');
}
