//Functions used on the product list display page
//=====================================================================

  	//Function that detect if session cookie "pcompitems" contains items
	function setItemsAdded(){
		if(getCookie("pcompitems") && getCookie("pcompitems").length > 0){
			itemsAdded = true;
			arrPcompitems = getCookie("pcompitems").split(",");
		}else{
			itemsAdded = false;
		}
		setCompareNowRel();

	}
//=====================================================================	
      	//Function that sets the content rel property for compare now anchors
      	//depending of the value of sessions cookie "pcompitems"
      	function setCompareNowRel(){
			itemsAddedCount = arrPcompitems.length;
			itemsToAddCount = 5 - itemsAddedCount;
			
			sAddedTxt = (itemsAddedCount > 1 || itemsAddedCount == 0)? "s" : "";
			sToAddTxt = (itemsToAddCount > 1)? "s" : "";
			
			if(itemsAddedCount < 5){
				relText = "You have selected " + itemsAddedCount + " product" + sAddedTxt + " for comparison, you may select " + itemsToAddCount + " more product" + sToAddTxt + " to compare."; 		
			} 
			else {
				relText = "You have selected " + itemsAddedCount + " product" + sAddedTxt + " for comparison, and reached the limit."; 		
			}   

   			for (pC=0;pC<prodCounter;pC++){
   				var compareAnchor = document.getElementById("compareAnchor"+pC);
   				if (compareAnchor){
   					compareAnchor.rel = relText;
   				}
   			}

      	}
//=====================================================================	
      	//Function that sets the Session Cookie value for Product Compare tool
      	function setCompareItems(catentryId,checkState,checkId){
      		if(checkState){
      			if(arrPcompitems.length>=5){
      				//document.getElementById("estTax-help"+checkId).style.display='block';
      				//dojo.removeClass(dojo.byId('estTax-help'+checkId), 'hidden');
      				//dojo.byId('noComp'+checkId).click();
      				dijit.byId('dialog_prod_comp').show();
      				document.getElementById("compareCheck"+checkId).checked = false;
      				
      			}else{
	      			arrPcompitems.push(catentryId);
	      			dijit.byId('dialog_prod_comp').hide();
	      		}
	      	}else{
	      		for(i=0;i<arrPcompitems.length;i++){
	      			if(arrPcompitems[i] == catentryId){
	      				arrPcompitems.splice(i,1);
	      			}
	      		}
	      	}
	      	setCookie("pcompitems",arrPcompitems.toString());
	      	//arrPcompitems = getCookie("pcompitems").split(",");
			setItemsAdded();
			compareLinkActivation();
      	}
//=====================================================================	      	
      	//Function that compare if a Product is on "pcompitems" list 
      	//and sets the status of checkbox
      	function setCompareCheck(catentryId){
      		for (pC=0;pC<prodCounter;pC++){
      			var compareCheck = document.getElementById("compareCheck"+pC);
   				var vCatentryId = 0;
   				var isAdded = false;
   				if(compareCheck){
   					for(i=0;i<arrPcompitems.length;i++){
   						vCatentryId = compareCheck.value;
   						if(arrPcompitems[i] == vCatentryId){
	      					isAdded = true;
	      				}
	      			}
	      			compareCheck.checked = isAdded;
				}
   			}
   			compareLinkActivation();
      	}
//=====================================================================	   
      	//Function that activates - deactivates the Compare Now links.
      	function compareLinkActivation(){
			var firstLink = -1;
			var count = 0;
      		for (var i = 0; i < prodCounter; i++){
      			var checkbox = document.getElementById("compareCheck"+i);
      			var link = document.getElementById("compareAnchor"+i);
      			if (checkbox){ 
	      			if (checkbox.checked){
	      				if (firstLink == -1){
	      					if (arrPcompitems.length > 1){
	      						count++;
	      					}
	      					firstLink = i;
	      				}else{
	      					dojo.addClass(link,"active");
	      					count++;
	      				}	
	      			}else{
	      				dojo.removeClass(link,"active");      			
	      			}
	      		}	
      		}
      		if (firstLink > -1){
      			if (count > 0){
      				dojo.addClass(document.getElementById("compareAnchor"+firstLink),"active");
      			}else{
      				dojo.removeClass(document.getElementById("compareAnchor"+firstLink),"active");
      			}	
      		}
      	}
//=====================================================================	      	
      	//Function that submit Compare Items Form
      	function submitCompareItems(catentryId){
      		if(itemsAdded && arrPcompitems.length > 1){
      			var submit = false;
      			if (catentryId == 1) {
      				submit = true;
      			} else {
      				for(i=0;i<arrPcompitems.length;i++){
	      				if(arrPcompitems[i] == catentryId){
	      					submit = true;
		      				break;
		      			}
	    	  		}
	    	  	}
      		
      			if (submit) {
	      			document.LowesProductComparisionForm.pcompitems.value = arrPcompitems.toString();
    	  			document.LowesProductComparisionForm.returnShoppingUrl.value=document.location.href
      				document.LowesProductComparisionForm.submit();
      			}
      		}
      	}
//=====================================================================	 	
		function defaultQV_zip(x){
			if(document.getElementById(x).value == ""){
				document.getElementById(x).value = "Enter ZIP Code";
				document.getElementById(x).maxLength = 20;
			}
		}
//=====================================================================	 		
		function clearQV_zip(x){
			if (document.getElementById(x).value == "Enter ZIP Code"){
				document.getElementById(x).value = "";
				document.getElementById(x).maxLength = 5;
			}
		}
//=====================================================================	 		
		function submitZipCodeForm(e,prodURL){
			if (e.keyCode == 13){
				document.storeSearchForm.zipCode.value = document.storeSearchFormQV.enterZip.value;
				document.storeSearchForm.isQvSearch.value="searchQV";
				document.storeSearchForm.qvRedirect.value=prodURL;
				document.storeSearchForm.submit();
				return false;
			}
		}
//=====================================================================	 
		function giftCardAddToCartContinue( a , url) {
			var eps = dojo.getObject("lowes.catalog.endecaParams", true);
			if ( eps ) {
				var o = new Object();
				if (eps.N)   { o.N = eps.N   };
				if (eps.Ne)  { o.N = eps.Ne  };
				if (eps.Ntt) { o.N = eps.Ntt };
				if (eps.Ns)  { o.N = eps.Ns  };
				var eo = dojo.objectToQuery( o );
				if ( eo ) {
					window.location = url + '&' + eo;
				}	
			} else {
				window.location = url;
			}
		}	
//=====================================================================	 	
      	//Function that returns the URL for the pagination form
      	function callChangePageNumber(formNumber){
      		//Changed from form submit to query string because of the
      		//Dynamic Cache
      		var form = eval('document.paginatorForm' + formNumber);
      		var totalPagesValue = form.totalPages.value;
      		var currentPageValue = form.currentPage.value;
      		var pageNumberValue = form.pageNumber.value;
      		var sb = getDefaultURL();
      		if (sb.indexOf('?') > -1) {    		
				sb += '&pageNumber=' + pageNumberValue;
			} else {
				sb += '?pageNumber=' + pageNumberValue;
			}	
			if(totalPagesValue!=''){
				sb += '&totalPages=' + totalPagesValue;
			}	
      		if(currentPageValue!=''){
				sb += '&currentPage=' + currentPageValue;
			}
      		var endecaParams = dojo.getObject("lowes.catalog.endecaParams", true);
			if ( endecaParams.Ntt && endecaParams.Ntt != null ){
				sb += '&Ntt=' + endecaParams.Ntt;
			} 
			location.href = sb;
      	}
//=====================================================================		
      	//Function that set the view all parameter
      	function callViewAll(totalRecords){
      		var sb = getDefaultURL();
      		
      		if (sb.indexOf('?') > -1)      		
				sb += '&Va=' + totalRecords;
			else 
				sb += '?Va=' + totalRecords;
      		
      		var endecaParams = dojo.getObject("lowes.catalog.endecaParams", true);
			if ( endecaParams.Ntt && endecaParams.Ntt != null ){
				sb += '&Ntt=' + endecaParams.Ntt;
			} 
      		
			location.href = sb;
      	}
//=====================================================================	      	
      	//Function that set the view type (Grid or List)
      	function callSwapView(viewTypeName){
      	
      		var form = eval('document.paginatorForm1');
			var pageNumber = '';
      		if (form) {
      			pageNumber = form.currentPage.value;
      		}
      		var sb = getDefaultURL();
      		
      		if (sb.indexOf('?') > -1)      		
				sb += '&viewTypeName=' + viewTypeName;
			else 
				sb += '?viewTypeName=' + viewTypeName;
      		
      		var endecaParams = dojo.getObject("lowes.catalog.endecaParams", true);
			if ( endecaParams.Ntt && endecaParams.Ntt != null ){
				sb += '&Ntt=' + endecaParams.Ntt;
			} 
      		if(pageNumber!=''){
				sb += '&pageNumber=' + pageNumber;
			}
			location.href = sb;
      	}      	
//=====================================================================	
		//Function that set the Results per page parameter
		function callResultPerPageDromDown(sizePageParam){
      		var sb = getDefaultURL();
      		if (sb.indexOf('?') > -1)
				sb += '&rpp=' + sizePageParam;
			else 
				sb += '?rpp=' + sizePageParam;
				
			var endecaParams = dojo.getObject("lowes.catalog.endecaParams", true);
			if ( endecaParams.Ntt && endecaParams.Ntt != null ){
				sb += '&Ntt=' + endecaParams.Ntt;
			} 
			location.href = sb;
      	}
//=====================================================================	
/*
	Submits the OrderItemAddForm for the specific catentryId.  
	This also sets the cmCategoryId value on the form with the value that was generated
	by the coremetrics pageview tag.  The OrderItemAddCmd will save the cmCategoryId value 
	in a cookie so that it can be persisted and sent in the coremetrics shop5 tag.
	
	We have to set the cmCategoryId using javascript like this because we won't know the value
	until after the coremetrics pageview tag executes.
*/
function productListAddToCart( catentryId ) {

	var f = dojo.byId('OrderItemAddForm');
	if ( f ) {
		f.catEntryId.value = catentryId;
		f.productId.value = catentryId;
		try {
			var o = dojo.formToObject('OrderItemAddForm_' + catentryId);
			if ( o ) {
				f.parentProductCacheKey.value = o.parentProductCacheKey;
				f.paOfferPrice.value = o.paOfferPrice;
				f.quantity.value = o.quantity;
			}
			var pg = dojo.getObject("cmPageviewData");
			if ( pg && pg.categoryId ) {
				f.cmCategoryId.value = pg.categoryId;
			}
		} catch (e){}
		f.submit();
	} else {
		//This is only here temporarily for backwards compatability.  Once the .jsps have been pushed everywhere this should be removed
		var form = eval( 'document.OrderItemAddForm_' + catentryId );
		if ( form ) {
			if ( form.cmCategoryId ) {
				if ( typeof cmPageviewData != undefined ) {
	                if ( cmPageviewData.categoryId ) {
	                    form.cmCategoryId.value = cmPageviewData.categoryId;
	                }
	            }
			}
			
			form.submit();
		}
	}
}
