/************************************************************/
/*	These functions are build for Lexus.com cpo Landing page*/
/*	For more details of the functionalities specs,					*/
/*	See Wireframe Document 7.0 from Genex.									*/
/*	Author: Christian Dumais																*/
/************************************************************/

/* Execute global function at load time */
/*Event.observe(window, 'load', cpo_default, false);*/

/* Global Variables */
var segmentName = null; /* Variable that will become an Array of Segment Names for models */
var modelNames = null; /* Variable that will become an Array of Model Names for models */
var modelString = "model=all"; /* Variable that contains a string of selected models */
var cpo_btnWidth = 117; /* Button Width in pixels used in cpo_modulesSwap */
var zipOrState = 1;
var cpo_queryString = "http://www.lcpo.com/cpo_poresults.psp?certified=1&";
if (typeof currentStateID=="undefined" && getCookie('state') != null){var currentStateID = getCookie('state');} /* Check in session cookie if a Model exist */
var states = new Array("Alaska","Alabama","Arkansas","Arizona","California","Colorado","Connecticut","District of Columbia","Delaware","Florida","Georgia","Iowa","Idaho","Illinois","Indiana","Kansas","Kentucky","Louisiana","Massachusetts","Maryland","Maine","Michigan","Minnesota","Missouri","Mississippi","Montana","North Carolina","North Dakota","Nebraska","New Hampshire","New Jersey","New Mexico","Nevada","New York","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Virginia","Vermont","Washington","Wisconsin","West Virginia","Wyoming");
var statesAbv = new Array("AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY");

/* Function that Default dynamic components on the page */
function cpo_default(){ /* Function executed after the page has loaded */
	createModelArrays();/* Creates the neccessary arrays for the model selector */
	$('chk_cpoModels99').checked = true; /* Checked All Models at load time */
	cpo_modulesSwap(0);/* Hilite First Button */
	$('txt_cpoZip').value = (getCookie('zip'))?getCookie('zip'):"";/* Insert saved Zip Code in text field */
	if (getCookie("state")){
		$('cpo_drDnHdTxtZipState').innerHTML = states[getCookie('state')-1];/* Set the state header with the saved cookie value */
		$('vc_cpoStateBG_'+getCookie("state")).src = $('vc_cpoStateBG_'+getCookie("state")).src.replace(new RegExp('(.*)(\.jpg)', 'i'), '$1Over$2');/* Hilite Cookie saved state */
	}
}

/* Function that swaps every dynamic component of the page */
function cpo_modulesSwap(index){
	/* Elements IDs */
	var zipBox 		= $('cpo_drDnHdZip');
	var zipModule = $('cpo_srcZip');
	var input 		= $('cpo_srcZip').getElementsByTagName('input')[0];
	var model 		= $('cpo_slcModel');
	var states 		= $('cpo_srcState');
	var stateHead	=	$('cpo_ZipStateHead');
	
	/* Show/Hide appropriate divs upon user selection */
	switch (index){
		case 0: {/* Show Default Zip Code Module */
			zipOrState = 1;
			cpo_tabSwap(index);
			cpo_showHide(zipBox,0);
			cpo_showHide(model,0);
			cpo_showHide(states,0);
			cpo_showHide(input,1);
			cpo_showHide(zipModule,1);
			cpo_innerHTML(stateHead,"Zip Code:");
			break;
		}
		case 1: {/* Show Default State Module */
			zipOrState = 2;
			cpo_tabSwap(index);
			cpo_showHide(zipBox,1);
			cpo_showHide(zipModule,1);
			cpo_showHide(input,0);
			cpo_showHide(model,0);
			cpo_showHide(states,0);
			cpo_innerHTML(stateHead,"State:");
			break;
		}
		case 2: {/* Show Model Menu */
			cpo_showHide(zipModule,0);
			cpo_showHide(input,0);
			cpo_showHide(model,1);
			cpo_showHide(states,0);
			break;
		}
		case 3: {/* Show State Selector Menu */
			cpo_showHide(zipModule,0);
			cpo_showHide(input,0);
			cpo_showHide(model,0);
			cpo_showHide(states,1);
			break;
		}
		default: {/* Default State (Same as 0) */
			zipOrState = 1;
			cpo_tabSwap(index);
			cpo_showHide(zipBox,0);
			cpo_showHide(model,0);
			cpo_showHide(states,0);
			cpo_showHide(input,1);
			cpo_showHide(zipModule,1);
			cpo_innerHTML(stateHead,"Zip Code:");
			break;
		}
	}
	adjustFooter();
}
/* Function that display/hide an element */
function cpo_showHide(element,display){
	element.style.display = (display==0)?"none":"block";
}
/* Function that Sets an element innerHTML */
function cpo_innerHTML(id,html){
	id.innerHTML = html;
}
/* Sets the position and value of the hilited tab */
function cpo_tabSwap(index){
	var btn = $("ts_btnTabHilite");
	cpo_showHide(btn,1);
	btn.style.left=(cpo_btnWidth*index) + "px";
	cpo_innerHTML(btn,"<a href='javascript: void(0);' onClick='cpo_modulesSwap("+index+");'>" + $('cpo_tab'+index).innerHTML + "</a>");
}

/* String Builder when the user pushes "Enter Selection" button for models */
function cpo_modelStringBuilder(){
	modelString 	= "model=all"; /* Declared globally */
	var head 			= "All Models";
	var segment 	= "";
	var model 		= "";
	
	/* "All Model" is NOT selected */
	if (!$('chk_cpoModels99').checked){
		/* Outer loop for the segments */
		for (var i=0;i<segmentName.length;i++){
			/* The segment is selected */
			if ($('chk_cpoModels'+i).checked){
				segment = (segment=="")?segmentName[i]:-1;/* Insert current segment to the segment header if no other segments were selected in previous loops */
				for (var j=0;j<modelNames[i].length;j++){/* Inner loop to add all the models of the current segment */
					modelString = (modelString=="model=all")?"model=":modelString+",";/* Building query --> */
					modelString += (modelNames[i][j]).toLowerCase();
				}
			}
			else{
				for (var j=0;j<modelNames[i].length;j++){/* Inner loop for the models of the current segment */
					if ($('chk_cpoModels'+i+j).checked){/* The model of that segment is selected */
						model = (model=="")?modelNames[i][j]:-1;/* Insert current model to the model header if no other models were selected in previous loops */
						modelString = (modelString=="model=all")?"model=":modelString+",";/* Building query --> */
						modelString += (modelNames[i][j]).toLowerCase();
					}
				}
			}
		}/* End Outer Loop */
		/* Model Selector header builder */
		if (segment==-1 || model==-1){
			head = "Open to see selections";/* More than 1 selection was made from either models or segments */
		}
		else{
			if (segment==""){/* No segment selected */
				if (model==""){/* No Model selected */
					head = "All Models";
					$('chk_cpoModels99').checked = true;/* Every checkboxes were unchecked, check back All models */
				}
				else{/* Only one model selected */
					head = model;
				}
			}
			else{
				head = (model=="")?segment:"Open to see selections";/* Only one segment was selected ELSE At least 1 model AND 1 segment were selected separately */
			}
		}
	}
	$('cpo_drDnHdTxtModel').innerHTML = head;
	if (zipOrState==1){cpo_modulesSwap(0)}else{cpo_modulesSwap(1)};/* Bring the page back to default */
} 

/* Model Selector Tool */
function cpo_modelSelector(row,column){
	if (!(row==99 && column==-1)){/* All Models is NOT selected */
		if (column==-1){/* A Segment has been selected */
			$('chk_cpoModels99').checked = false;/* Making Sure All Models is unchecked */
			for (var i=0;i<modelNames[row].length;i++){
				$("chk_cpoModels"+row+i).checked=($("chk_cpoModels"+row).checked)?true:false;
			}
		}
		else{/* A Model has been selected */
			$('chk_cpoModels99').checked = false;/* Making Sure All Models is unchecked */
			$("chk_cpoModels"+row).checked = false;
		}
	}
	return false;
}

/* Creates the arrays needed to select models [TEAMSITE GENARATED] */
/* The following function has to be on the page directly in order */
/* to be dynamically populated by teamsite. */
/* The function below is there has an example */
/*
function createModelArrays(){
	segmentName 	= new Array("Luxury Sedans","Luxury Coupes","Luxury Utility Vehicles","Luxury Hybrids");
	modelNames		= new Array(new Array("LS","GS","ES","IS"),
														new Array("SC"),
														new Array("LX","GX","RX"),
														new Array("GSh","RXh"));
}
/* End example function */

/* Function that show the full name of a state when a state button is mouse overed */
function cpo_stateSwap(pos){
	$('cpo_stateSearchHeader').innerHTML = states[pos-1];
	if (pos != currentStateID){
		swapOver('vc_cpoStateBG_' + pos); /* Swap state button to hilite */
		}
}

function cpo_stateSwapOut(pos) {
	if (pos != currentStateID){
		swapOut('vc_cpoStateBG_' + pos); /* Swap state button to de-hilite */
		}
	setStateHeader();
	}

/* Sets the State selection tool header  */
function setStateHeader(){
	if (getCookie('state')){
		$('cpo_stateSearchHeader').innerHTML = states[getCookie('state')-1];
	}
	else{
		$('cpo_stateSearchHeader').innerHTML = "";
	}
}
function setStateBtn(pos){
	swapOn('vc_cpoStateBG_' + pos); /* Swap clicked state button to hilite */
	if (typeof currentStateID!="undefined" && currentStateID!=""){
		swapOver('vc_cpoStateBG_' + currentStateID); /* Swap previous state button back to default */
	}
	$('cpo_drDnHdTxtZipState').innerHTML = states[pos-1];
	setCookie("state",pos); /* Set the cookie to the new state value */
	currentStateID = getCookie('state'); /* Set the current  */
	cpo_modulesSwap(1);
}
function cpo_buildQuery(){
	setCookie('zip',document.forms['frm_cpoZip'].txt_cpoZip.value);
	var queryStringZip = (getCookie('zip'))?"&zip="+getCookie('zip'):"";
	var queryStringState = (getCookie('state'))?"&state="+(statesAbv[getCookie('state')-1]).toLowerCase():"";
	if ($('txt_cpoZip').style.display=="block"){/* Search Query by Zip */
		cpo_queryString += modelString+queryStringZip;
	}
	else{/* Search Query by State */
		cpo_queryString += modelString+queryStringState;
	}
	window.location = cpo_queryString;
}
/* Ajusting the bottom spacing between content and footer (40px when content is below sideNav)*/
function adjustFooter(){
	if (Dom.getBottomY($('cpo_rightContent')) > Dom.getBottomY($('cpo_leftContent'))){
		var containerBottom = Dom.getBottomY($('cpo_rightContent'));
	}
	else{
		var containerBottom = Dom.getBottomY($('cpo_leftContent'));
	}
	$('ftArea').style.top = (containerBottom+20) + "px";
	$('ftArea').style.display = "block";
}
/* Validates Zip Code for digits only */
//function by Danny Goodman
function mustBeZip(evt){
	evt = (evt) ? evt : window.event
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		status = "This field accepts numbers only."
		return false
	}
	status = ""
	return true
}
