//******************************************************************
//Functions to trim a value 
//******************************************************************

	// Removes leading whitespaces
	function LTrim( value ) {
		
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
		
	}
	
	// Removes ending whitespaces
	function RTrim( value ) {      
		
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
		
	}   
	 
	// Removes leading and ending whitespaces
	function trim( value ) {
		
		return LTrim(RTrim(value));
		      
	}
//******************************************************************
//Functions to remove HTML Tags from a value 
//******************************************************************
	function removeHTMLTags(value){
		if(value != null){
			return(value.replace(/<.*?>/g,''));
		}else{
			return null;
		} 
	}

//******************************************************************
//Functions related to Cookies
//******************************************************************

	function returnCookieValue(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}


//******************************************************************
//Functions for Recently viewed products
//******************************************************************
	var recentProductsArr=new Array();
	var brokenArr=new Array();
	function restrict(value,cookieSize){
		var tempArr1=value.split("|");
		if(tempArr1.length>cookieSize){
			tempArr1 = tempArr1.slice(tempArr1.length-cookieSize,tempArr1.length);      
		}
		return tempArr1.join("|");
	}
	function addRecentlyViewedProducts(cookieString,cookieValue,key,mkId,Recently_Viewed_Cookie_Size){
		if(cookieString == "null" || cookieString == null){
			cookieString=cookieValue;
		}else{
			var tempArr=cookieString.split("|");
			var addValue=true;
			for(arrCount=0;arrCount<tempArr.length;arrCount++){
				if(tempArr[arrCount].indexOf(key) >-1){
					addValue=false;
					break;
				}
			}
			if(addValue == true || addValue == 'true'){
				cookieString=cookieString+"|"+cookieValue;
			}
		}
		if(mkId == null || mkId == "null"){  
		 var restrictString  = restrict(cookieString,Recently_Viewed_Cookie_Size);
			if(!isCookieFull(2,restrictString) && checkResonanceCookie()) {
				createCookie("RECENTLY_VIEWED_PRODUCTS",restrictString,10);
			}
		}
	}
	function populateArrays(cookieString){
		if(cookieString != null){
			recentProductsArr=cookieString.split("|");
			recentProductsArr.reverse();
		}
	}
	function getBreakup(value){
		if(value != null)
			brokenArr=value.split("+");	
	}

	function populateEmptyDiv(marginLeft,style){
		document.write('<div class="'+style+'" style="margin-left:'+marginLeft+';" ><br/><br/><br/>');
		document.write('<img align="absmiddle" src="/wcsstore/carparts/general/en_US/ui8_images/certona_jcw_logo.gif" style="margin-top: 2px; margin-left:20px;" vspace="3">');
		document.write('</div>');		
	}
	function populateDataforItemDiv(arrObj){
		var recentProduct = '';
		getBreakup(arrObj);
		rate=getRatingforBV(brokenArr[6]);
		if(brokenArr[9] == null || brokenArr[9] == "null" || brokenArr[9] == "undefined"){
			brokenArr[9] = '10101'
		}
		if(brokenArr[3] != null && brokenArr[3] != "null"){
			if(brokenArr[3] == brokenArr[2])
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&appId='+brokenArr[3]+'&Pr=p_CATENTRY_ID:'+brokenArr[2];
			else
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&appId='+brokenArr[3]+'&Pr=p_CATENTRY_ID:'+brokenArr[2]+brokenArr[3];
		}else{
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&Pr=p_CATENTRY_ID:'+brokenArr[2];
		}
		
		recentProduct = '<a href="'+imageLink+'" border="0"><img src="/wcsstore/carparts/images/imagecache/'+(brokenArr[4])+'" border="0" width="120px" height="60px"/></a>';
		recentProduct = recentProduct + '<div class="home_prod_name">'+trimKeyValue(unescape(brokenArr[5]),65)+'</div>'
		
		recentProduct = recentProduct + '<br class="cleaner"/>';
        if(brokenArr[7] != null && brokenArr[7] != "null"){
        	recentProduct = recentProduct + '<div class="home_prod_hour">'+unescape(brokenArr[7])+'</div>';
         } 
          
		if(rate != null && rate != "null" ){
			recentProduct = recentProduct + '<img src="/wcsstore/carparts/general/en_US/ui8_images/star'+rate+'.gif" style="width:75; height:14;" vspace="3" >';
		}
		
		return recentProduct;
	}

	function populateDataforProductDiv(arrObj){
		var recentProduct = '';
		getBreakup(arrObj);
		rate=getRatingforBV(brokenArr[5]);
		catalogID=parseInt(brokenArr[1])+100;
		if(brokenArr[8] == null || brokenArr[8] == "null" || brokenArr[8] == "undefined"){
			brokenArr[8] = '10101'
		}		
		imageLink='/autoparts/ProductBrowse?storeId='+brokenArr[8]+'&Pr=p_Product.CATENTRY_ID:'+brokenArr[2]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1];
		
		recentProduct = '<a href="'+imageLink+'" border="0"><img src="/wcsstore/carparts/images/imagecache/'+brokenArr[3]+'" border="0" width="120px" height="60px"/></a>';
		recentProduct = recentProduct + '<div class="home_prod_name">'+trimKeyValue(unescape(brokenArr[4]),65)+'</div>'
		
		recentProduct = recentProduct + '<br class="cleaner"/>';
		if(brokenArr[6] != null && brokenArr[6] != "null"){
        	recentProduct = recentProduct + '<div class="home_prod_hour">'+unescape(brokenArr[6])+'</div>';
         } 
          
		if(rate != null && rate != "null" ){
			recentProduct = recentProduct + '<img src="/wcsstore/carparts/general/en_US/ui8_images/star'+rate+'.gif" style="width:75; height:14;" vspace="3" >';
		}
		return recentProduct;
	}
	
	  
	  
//******************************************************************
// Added for Affinova Release 8.3
//******************************************************************
	function populateDataforAffiItemDiv(arrObj){
		var recentProduct = '';
		getBreakup(arrObj);
		rate=getRatingforBV(brokenArr[6]);
		if(brokenArr[9] == null || brokenArr[9] == "null" || brokenArr[9] == "undefined"){
			brokenArr[9] = '10101'
		}
		if(brokenArr[3] != null && brokenArr[3] != "null"){
			if(brokenArr[3] == brokenArr[2])
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&appId='+brokenArr[3]+'&Pr=p_CATENTRY_ID:'+brokenArr[2];
			else
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&appId='+brokenArr[3]+'&Pr=p_CATENTRY_ID:'+brokenArr[2]+brokenArr[3];
		}else{
			imageLink='/autoparts/ItemBrowse?storeId='+brokenArr[9]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1]+'&langId=-1&Pr=p_CATENTRY_ID:'+brokenArr[2];
		}
		
		recentProduct = '<a href="'+imageLink+'" border="0">'+trimKeyValue(unescape(brokenArr[5]),20)+'...</a>';
		return recentProduct;
	}

	function populateDataforAffiProductDiv(arrObj){
		var recentProduct = '';
		getBreakup(arrObj);
		rate=getRatingforBV(brokenArr[5]);
		catalogID=parseInt(brokenArr[1])+100;
		if(brokenArr[8] == null || brokenArr[8] == "null" || brokenArr[8] == "undefined"){
			brokenArr[8] = '10101'
		}		
		
		imageLink='/autoparts/ProductBrowse?storeId='+brokenArr[8]+'&Pr=p_Product.CATENTRY_ID:'+brokenArr[2]+'&productId='+brokenArr[2]+'&catalogId='+brokenArr[1];
		recentProduct = '<a href="'+imageLink+'" border="0">'+trimKeyValue(unescape(brokenArr[4]),20)+'</a>';
		return recentProduct;
	}	

//******************************************************************
//Functions to return BV star image name based on the rating
//******************************************************************
    function getRatingforBV(value){
		if(value == null || value == "null" || value == "")
			return null;
		if(value.length<2){
			value=value+"0";
		}
		else{
			if(value>0){
				value=value * 10;
				value=(value.toString()).substring(0,2);
			}
		}
		if(value>-1 && value<10)
			value="0"+value;
		return value;
	}

//******************************************************************
//Functions for Recent History
//******************************************************************

	function restrictCookieString(cookieData, cookieSize){
		arr=cookieData.split("|");
		arrSearch=new Array();
		arrSearch[0]="default";
		arrCategory=new Array();
		arrCategory[0]="default";
		for(arrCount=0;arrCount<arr.length;arrCount++){
			if(arr[arrCount].indexOf("S-") > -1)
				arrSearch.push(arr[arrCount]);
			if(arr[arrCount].indexOf("C-") > -1)
				arrCategory.push(arr[arrCount]);
		}
		cookieValue="";
		if(arrSearch.length > cookieSize){
			arrSearch.splice(0,(arrSearch.length-cookieSize));
			cookieValue=arrSearch.join("|");
		}else{
			cookieValue=arrSearch.join("|");
		}
		if(arrCategory.length > cookieSize){
			arrCategory.splice(0,(arrCategory.length-cookieSize));
			cookieValue=cookieValue+"|"+arrCategory.join("|");
		}else{
			cookieValue=cookieValue+"|"+arrCategory.join("|");
		}
		if(cookieValue == "")
			cookieValue=cookieData;
		return cookieValue;
	} 

	function saveSearch(searchForm , catalogId , cookieSize){
		var success = true;
			cookieSize = 5;
		if (Trim(searchForm.sku.value) == "" || Trim(searchForm.sku.value) == "Keyword(s) or Part#") {
		    success = false;
		}

		if(success){
			recentHistoryCookie=readCookie("RECENT_HISTORY");
			recentHistoryCookie = replaceAll(recentHistoryCookie,"^",",");
			var skuValue = removeHTMLTags(searchForm.sku.value);
			skuValue = Trim(skuValue);
			searchForm.sku.value = skuValue;
			if(recentHistoryCookie == null){
				cookieValue="S-"+catalogId+"+"+skuValue;
				if(!isCookieFull(1,cookieValue)  && checkResonanceCookie()) {
					cookieValue = replaceAll(cookieValue,",","^");
					createCookie("RECENT_HISTORY",cookieValue,10);
				}
			}else{
				var arr=new Array();
				arr=recentHistoryCookie.split("|");
				addValue=true;
				for(arrCount=0;arrCount<arr.length;arrCount++){
					if(arr[arrCount].indexOf("S-") > -1){
						if(arr[arrCount].indexOf(skuValue) != -1){
							addValue=false;
							break;					
						}
					}
				}
				if(addValue){
					recentHistoryCookie=recentHistoryCookie+"|"+"S-"+catalogId+"+"+skuValue;
				}
				
				var restrictString = restrictCookieString(recentHistoryCookie,cookieSize);
				if(!isCookieFull(1,restrictString) && checkResonanceCookie()) {
				    restrictString = replaceAll(restrictString,",","^");
					createCookie("RECENT_HISTORY",restrictString,10);
				}
			}
		}
	}


	// Categories
	function getCatKeyValue(value){
		keyValue = value.substring((value.lastIndexOf("+")+1));
		return keyValue;
	}

	function deleteIfFromHierarchy(cookieString,breadCrumValues){
		var orgCat=new Array();
		orgCat=cookieString.split("|");
		if(breadCrumValues.indexOf(">") > -1){
			breadCrumValues=breadCrumValues.substring(0,breadCrumValues.lastIndexOf(">"));
		}else{
			breadCrumValues = null;
		}
		if(breadCrumValues != null && breadCrumValues != "null"){
			for(arrCount=0;arrCount<orgCat.length;arrCount++){
				if(orgCat[arrCount].indexOf("C-") > -1){
					if(breadCrumValues.indexOf(getCatKeyValue(orgCat[arrCount])) > -1){
						orgCat.splice(arrCount,1);
					}
				}
			}
			var concatString = orgCat.join("|");
			if(!isCookieFull(1,concatString) && checkResonanceCookie()) {
				concatString = replaceAll(concatString,",","^");
				createCookie("RECENT_HISTORY",concatString,10);	
			}
		}
	}

	function addCategories(categoryString,hyperlink,catId,descName,breadCrumValues){
		var nValue = null;
		if(hyperlink.indexOf("N-") > 0) {
			newlink = hyperlink.substring(hyperlink.indexOf("N-"));
			newlink = newlink.substring(0,newlink.indexOf("/"));
			nValue = newlink.substring(newlink.lastIndexOf("+6")+1);
		} else if(hyperlink.indexOf(".car") > 0) {
			nValue = hyperlink.match(/\/(\d+)\.car/)[1];
		}
		if(categoryString == null || categoryString == 'null'){
				cookieValue="C-"+catId+"+"+nValue+"+"+descName;
				if(!isCookieFull(1,cookieValue) && checkResonanceCookie()) {
					cookieValue = replaceAll(cookieValue,",","^");
					createCookie("RECENT_HISTORY",cookieValue,10);
				}
		}else{ 
			var catArr=new Array();
			catArr=categoryString.split("|");
			addValue=true;
			for(arrCount=0;arrCount<catArr.length;arrCount++){
				if(catArr[arrCount].indexOf("C-") > -1){
					if(catArr[arrCount].indexOf(nValue) != -1){
						addValue=false;
						break;					
					}
				}
			}
			if(addValue){
				newValue="C-"+catId+"+"+nValue+"+"+descName;
				categoryString=categoryString+"|"+newValue;
			}
			categoryString = restrictCookieString(categoryString,5);
			if(!isCookieFull(1,categoryString) && checkResonanceCookie()) {
				categoryString = replaceAll(categoryString,",","^");
				createCookie("RECENT_HISTORY",categoryString,10);
				breadCrumValues = replaceAll(breadCrumValues,",","^");
				deleteIfFromHierarchy(categoryString,breadCrumValues);
			}
		}

	}

//******************************************************************
//Functions to get coordinats of an element
//******************************************************************
		function pageOffsetLeft(elem) {
		    if (elem.offsetParent) {
		        for(var offX = 0; elem.offsetParent; elem = elem.offsetParent){
		            offX += elem.offsetLeft;
		        }
		
		        return offX;
		    } else {
		        return elem.x;
		    }
		}
		function pageOffsetTop(elem) {
		    if (elem.offsetParent) {
		        for(var offY = 0; elem.offsetParent; elem = elem.offsetParent){
		            offY += elem.offsetTop;
		        }
		        return offY;
		    } else {
		        return elem.y;
		    }
		}
		
//******************************************************************
//Functions related to RecentHistory
//******************************************************************		
		function getCatalogID(value){
			return value.substring(2,(value.indexOf("+")));
		} 
		function getCatEntryID(value){
			return value.substring((value.indexOf("+")+1),value.lastIndexOf("+"));
		}
		function getKeyValue(value){
			keyValue = value.substring((value.lastIndexOf("+")+1));
			return keyValue;
		}
		function trimKeyValue(keyValue,keyLength){
			if(keyValue.length > keyLength)
				keyValue=keyValue.substring(0,keyLength)+"...";
			return keyValue;
		}
		
//******************************************************************
//Function To check cookie size
//******************************************************************		
		function isCookieFull(cond,cookieStringValue) {
			var cookieString = null;
				if(cond == 1) {
					cookieString = readCookie("RECENTLY_VIEWED_PRODUCTS");
				}
				if(cond == 2) {
					cookieString = readCookie("RECENT_HISTORY");
				}
				cookieString = cookieString + readCookie("JCWMakeModel") + cookieStringValue;
				if(cookieString != null && cookieString != "null") {
					if(cookieString.length > 1800)
						return true;
				}
			return false;
		}
	
//Function to unescape the HTML Tags		
	function unescapeHTMLTags(value){
	       return value.replace(/&gt;/g,'>').                                           
					replace(/&lt;/g,'<').                                           
					replace(/&quot;/g,'"').
					replace(/&amp;/g,"&");
	 } 		
		