// recommendations.js

var theHandle = "";
var theRoot   = "";
			
function ShowProducts(seeMoreType) {
	HideProducts();
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body ;
	if(seeMoreType == 'insurance_popup_view'){
		document.getElementById("insurance_popup_view").style.display='block';
		document.getElementById('popup_holder').setAttribute("class", "hidden");
		theHandle = document.getElementById("handle_insurance_popup");
		theRoot   = document.getElementById("insurance_popup_view");
	}
	Drag.init(theHandle, theRoot);
	if (document.all||document.getElementById){
		var dsoctop = 100;
		try{
			dsoctop = document.all? iebody.scrollTop : pageYOffset;
		}catch(err){}
	
		if(seeMoreType == 'insurance_popup_view') {
			theRoot.style.top = (dsoctop+100)+"px";
			theRoot.style.left = (200)+"px";
		}
	}
}

function HideProducts() {
	if(document.getElementById("insurance_popup_view") != null)
		document.getElementById("insurance_popup_view").style.display='none';	

}



//This method returns the AppID required by recommendation engine to set the default vehicle
	function getDefaultVehicleforUser(){
		var appid=null;
		var cookieString = readCookie("JCWMakeModel");
		if(cookieString != null && cookieString != "null"){
			appid=cookieString.substring(cookieString.lastIndexOf("+")+1);
		}
		return appid;
	}
//This function returns itemids that should be excluded by certona
	function getRecentlyViewedItemIds(){
		var cookieString = readCookie("RECENTLY_VIEWED_PRODUCTS");
		if(cookieString != null){
			var itemList=new Array();
			itemList[0]="";
			var cookieValues=cookieString.split("|");
			for(valueCount=0;valueCount<cookieValues.length;valueCount++){
				tempCookieValue=cookieValues[valueCount];
				for(counterValue=0;counterValue<2;counterValue++){
					tempCookieValue=tempCookieValue.substring(tempCookieValue.indexOf("+")+1);
				}
				tempCookieValue=tempCookieValue.substring(0,tempCookieValue.indexOf("+"));
				itemList.push(tempCookieValue);
			}
			returnParam = itemList.join(";");
			return returnParam.substring(1);
		}
	}
//this function returns the Default MMY
function getDefaultMMY()
{
	var cookieString=readCookie("JCWMakeModel");
	if(cookieString == null  || cookieString == "null"){
		return null;
	}
	else{
		var MMYArr=new Array();
		cookieString=cookieString.substring((cookieString.lastIndexOf("?")+1));
		MMYArr=cookieString.split("|");
		//10101|2003|1+Acura|2+EL|111+2003+200728258?
		year=MMYArr[1];
		make=MMYArr[2].substring(MMYArr[2].lastIndexOf("+")+1)
		model=MMYArr[3].substring(MMYArr[3].lastIndexOf("+")+1)
		var retString = null;
		if(year == null || make == null || model == null){
			retString = null;
		}else{
			retString = year+"+"+make+"+"+model;
			if(retString.indexOf("#") > -1)
				retString = retString.substring(0,(retString.length-1)) 
		}
		return escape(retString);
	}
}
//This function returns the Default Year
	function getDefaultYear()
	{
		var cookieString=readCookie("JCWMakeModel");
		if(cookieString == null || cookieString == "null"){
			return null;
		}
		else{
			var MMYArr=new Array();
			cookieString=cookieString.substring((cookieString.lastIndexOf("?")+1));
			MMYArr=cookieString.split("|");
			var year=MMYArr[1];
			if(year == null || year == "null")
				return null;
			else
				return year;
		}
	}
	
//This function returns the Document inside iframe.
	function extractIFrameDoc(iFrameEl) {
	  	var doc = null;
		if (iFrameEl.contentDocument) { 
			doc = iFrameEl.contentDocument; 
	  	} 
	  	else if (iFrameEl.contentWindow) { 
			doc = iFrameEl.contentWindow.document;
		} 
	  	else if (iFrameEl.document) { 
			doc = iFrameEl.document;
		} 
	  	else {
	  		return null;
	  	}
	  	
	  	return doc;
	}
	
	function $() 
	{
		var elements = new Array();
		
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
		  	
			if (typeof element == 'string') {
				if (document.getElementById) {
			  		element = document.getElementById(element);
				} 
				else if (document.all) {
			  		element = document.all[element];
				}
		  	}
		  	elements.push(element);
		}
		
		if (arguments.length == 1 && elements.length > 0) {
		  	return elements[0];
		} 
		else {
		  	return elements;
		}
	}	

//This function is used for A/B split functionality.
//If returns true then Recommendations will be shown on home page.
  function checkResonanceCookie() {
  
  	var cookieString=readCookie("ResonanceSegment");
		if(cookieString == null){
			return false;
		}
		else{
			if(cookieString == "1")
				return true;
			else
				return false;
			
		}
  	}	
  
//If returns User Id value set by Certona
  function getResUserId() {
  
  	var cookieString=readCookie("RES_TRACKINGID");
		if(cookieString == null){
			return "";
		}
		else{
			return cookieString;	
		}
  	}
  		

//this function returns the Default MMY ID in Certona URL Format.
 function getDefaultMMYinURL()
{
	var cookieString=readCookie("JCWMakeModel");
	if(cookieString == null  || cookieString == "null"){
		return "&makeid=&modelid=&year=";
	}
	else{
		var MMYArr=new Array();
		cookieString=cookieString.substring((cookieString.lastIndexOf("?")+1));
		MMYArr=cookieString.split("|");
		//10101|2005|29+Ford|6691+F-Series Pickup|111+2005+200729571
		//alert("cookieString=" + cookieString);
		year=MMYArr[1];
		makeid=MMYArr[2].substring(0,MMYArr[2].lastIndexOf("+"));
		modelid=MMYArr[3].substring(0,MMYArr[3].lastIndexOf("+"));
		appid=MMYArr[4].substring(MMYArr[4].lastIndexOf("+")+1);
		return "&makeid="+makeid+"&modelid="+modelid+"&year="+year + "&vehicleappid=" + appid;
	}
}
//******************************************************************
//Function retaled to Add to Cart
//******************************************************************				
		var busy = false;	
		function AddItemToCart(form){
			if(!busy) {
				busy = true;
				form.submit();
			}
		}
		var bSubmitted = false;

		function setSubmitted(value){
			bSubmitted = value;
			return;
		}
		
		function getSubmitted(){
		    var bOldValue = bSubmitted;
		    setSubmitted(true);
		    
		    return bOldValue;
		}
		
//******************************************************************
//Function Added for callback .. Release 8.3
//******************************************************************				
		
function myCallback(json)
{
	try
	{
		var elem = document.getElementById("cp_proof_items;cp_recent_items;cp_star_values");
			
		if(elem != null)
		{		
			if(json.Resonance.Response[0].display == "yes")
			{
				elem.innerHTML = json.Resonance.Response[0].output;
			}
		}

		populateData();
	}
	catch(e)
	{

	}
}		