/* This is used to show confirmation dialog after adding item to shopping list */
function showAddConfirmation(giftItemId,giftlistId,returnToShopping) {
	if ((giftlistId != null) && (giftlistId != null)) {
		var dialogURL = "../user/add_shopping_list_confirmation.jsp?giftItemId="+giftItemId+"&giftlistId="+giftlistId+"&returnToShopping="+returnToShopping;
		if (window.showModalDialog) {
			var returnVal = window.showModalDialog(dialogURL,'Add to Shoppinglist','dialogWidth:677px;dialogHeight:509px;resizable:no;help:no;status:no;scroll:no');
			if (returnVal != null) {
				window.location.href = returnVal;
			}
		} else {
			window.open(dialogURL, "Add to Shoppinglist", "width=677,height=509,scrollbars=no,titlebar=no,alwaysRaised=yes,dialog,modal=yes");
		}
	}
}
	
function submitSelect() {
	var url = document.form.urlInput.value;
	document.form.action = url;
	var filterform = document.getElementById("filterForm");
	document.form.submit();
	if (filterform !== null) {
		document.filterForm.submit();
	}
}

function bottomSubmitSelect() {
	var url = document.form.urlInput.value;
	document.paginatorBottomForm.action = url;
	var filterform = document.getElementById("filterForm");
	document.paginatorBottomForm.submit();
	if (filterform !== null) {
		document.filterForm.submit();
	}
}

function items() {
	var url = document.form.urlInput.value;
	document.form.pageJump.value = "1";
	document.form.action = url;
	var filterform = document.getElementById("filterForm");
	document.form.submit();
	if (filterform !== null) {
		document.filterForm.submit();
	}
}

function bottomItems() {
	var url = document.form.urlInput.value;
	document.form.pageJump.value = "1";
	document.paginatorBottomForm.action = url;
	var filterform = document.getElementById("filterForm");
	document.paginatorBottomForm.submit();
	if (filterform !== null) {
		document.filterForm.submit();
	}
}
	
function saveToList(itemIndex)
 {
    var elementName = "submit"+itemIndex;
    document.getElementById(elementName).click();
 }
 
function checkKey()
	{
		if ((event.keyCode < 48) || (event.keyCode > 57))
		{
			event.returnValue = false;
		}
	}
	
/* This is used to check max quantity of a product */
function checkQty(count,maxqnty){
    var userQnty=document.getElementById("userQty"+count).value;
    document.getElementById("qty"+count).value=userQnty;
    //null check added by kc for maxqty
	if(maxqnty != null)
    {
		if(maxqnty > 0 && userQnty>maxqnty)
		{        
			document.getElementById("limit"+count).style.display="block";        
		}else
			document.getElementById("limit"+count).style.display="none";
	}
}

/* This function is used to handle show or hide div for product images */
function hideDiv(divid,index,size){
	try
	{
			var maxCount=document.form.maxCount.value;
			var totalcount=Number(index)+Number(size);
			if(totalcount>maxCount)
			{    
				totalcount=maxCount;    
				totalcount++;
			}
			if(divid=='show')
			{ 
				document.getElementById(divid).style.display ="block";
				document.getElementById('hide').style.display ="none";
				document.getElementById('imageview').value="show";
				document.form.inpImage.value='show';
				for(count=index;count<totalcount;count++)
				{
					var elem=document.getElementById("prod"+count);        
					elem.style.display='block';
				}
			}else if(divid=='hide')
			{ 
				document.getElementById(divid).style.display ="block";
				document.getElementById('show').style.display ="none";
				document.getElementById('imageview').value="hide";
				document.form.inpImage.value='hide';
				for(count=index;count<totalcount;count++)
				{
					var elem=document.getElementById("prod"+count);
					elem.style.display ='none';
				}
			} 
	}
	catch(e){}

} 

/* This function is used to print product details */
function printWindow(){
       bV = parseInt(navigator.appVersion)
       if (bV >= 4) window.print()
    }
	
function tabCheck(){
	if(document.getElementById("tab1value")!=null)
	{
    if(document.getElementById("tab1value").value == "true"){
        if(document.getElementById("tab2value").value == "true"){
            if(document.getElementById("tab3value").value == "true"){
                tabSwitch('warning');
            } else {
                tabSwitch('direction');
            }
        } else {
            tabSwitch('prodIngd');
        }
    } else {
            tabSwitch('prodDesc');
    }
    }
}

function checkMaxQty(maxqnty){    
    var userQnty=document.getElementById("userQty").value;    
    document.getElementById("qnty").value=userQnty;
    if(userQnty>maxqnty)
    {        
        document.getElementById("limit").style.display="block";        
    }else
        document.getElementById("limit").style.display="none";    
}

/* This function is used to show different tabs in th product details page */
function tabSwitch(tabName){ 
    if(tabName=='prodDesc'){ 
        document.getElementById(tabName).style.display = 'block';
        document.getElementById('prodDescCont').style.display = 'block';
        document.getElementById('prodIngd').style.display = 'none';
        document.getElementById('direction').style.display = 'none';
        document.getElementById('warning').style.display = 'none';
        document.getElementById('prodIngdCont').style.display = 'none';
        document.getElementById('directionCont').style.display = 'none';
        document.getElementById('warningCont').style.display = 'none';
    }else if(tabName=='prodIngd'){ 
        document.getElementById(tabName).style.display = 'block';
        document.getElementById('prodIngdCont').style.display = 'block';
        document.getElementById('prodDesc').style.display = 'none';
        document.getElementById('direction').style.display = 'none';
        document.getElementById('warning').style.display = 'none';
        document.getElementById('prodDescCont').style.display = 'none';
        document.getElementById('directionCont').style.display = 'none';
        document.getElementById('warningCont').style.display = 'none';
    } else if(tabName=='direction'){ 
        document.getElementById(tabName).style.display = 'block';
        document.getElementById('directionCont').style.display = 'block';
        document.getElementById('prodDesc').style.display = 'none';
        document.getElementById('prodIngd').style.display = 'none';
        document.getElementById('warning').style.display = 'none';
        document.getElementById('prodDescCont').style.display = 'none';
        document.getElementById('prodIngdCont').style.display = 'none';
        document.getElementById('warningCont').style.display = 'none';
    } else if(tabName=='warning'){ 
        document.getElementById(tabName).style.display = 'block';
        document.getElementById('warningCont').style.display = 'block';
        document.getElementById('prodDesc').style.display = 'none';
        document.getElementById('prodIngd').style.display = 'none';
        document.getElementById('direction').style.display = 'none';
        document.getElementById('prodDescCont').style.display = 'none';
        document.getElementById('prodIngdCont').style.display = 'none';
        document.getElementById('directionCont').style.display = 'none';
    } 
} 

function textCounter(field, countfield, maxlimit) 
	{
       if (field.value.length > maxlimit) // if too long...trim it!
       field.value = field.value.substring(0, maxlimit);
       // otherwise, update 'characters left' counter
       else 
       countfield.value = maxlimit - field.value.length;
}

function sortBy()
    {  
       var url=document.form.urlInput.value;
       window.form.action=url;
        window.form.submit();
    }
	
function disableEnterKey()
{
     if(event.keyCode == 13)
          return false;
     else
          return true;
}


function setCriteria(criteria)
    {
		
		var resetBean=document.filterForm.resetBean.value;
		var onProdList=document.filterForm.onProdList.value;
		var brandName=document.filterForm.brandName.value;
		var filterBy=document.filterForm.filterBy.value;
		var itemId=document.filterForm.itemId.value;
		var navAction=document.filterForm.navAction.value;
		var navCount=document.filterForm.navCount.value;
		var brandBy=document.filterForm.brandBy.value;
		var brandId=document.filterForm.brandId.value;
		document.form.pageJump.value="1";
		if(document.form.range.options[document.form.range.options.selectedIndex].text=="ALL")
    	{
    	document.form.range.value="20";
    	}
		if(criteria=="fsa"){		
			document.getElementById("checkFSA").checked=true;
			document.filterForm.action = "shop_sub_category.jsp?resetBean="+resetBean+"&onProdList="+onProdList+"&brandName="+brandName+"&filterBy="+filterBy+"&itemId="+itemId+"&navAction="+navAction+"&navCount="+navCount+"&brandBy="+brandBy+"&brandId="+brandId;
		}
		else if(criteria=="online"){
			document.getElementById("checkOnline").checked=true;
			document.filterForm.action = "shop_sub_category.jsp?resetBean="+resetBean+"&onProdList="+onProdList+"&brandName="+brandName+"&filterBy="+filterBy+"&itemId="+itemId+"&navAction="+navAction+"&navCount="+navCount+"&brandBy="+brandBy+"&brandId="+brandId;
		}
		else if(criteria=="sales"){
			document.getElementById("checkSales").checked=true;
			document.filterForm.action = "shop_sub_category.jsp?resetBean="+resetBean+"&onProdList="+onProdList+"&brandName="+brandName+"&filterBy="+filterBy+"&itemId="+itemId+"&navAction="+navAction+"&navCount="+navCount+"&brandBy="+brandBy+"&brandId="+brandId;
		}
		else if(criteria=="new"){
			document.getElementById("checkNew").checked=true;
			document.filterForm.action = "shop_sub_category.jsp?resetBean="+resetBean+"&onProdList="+onProdList+"&brandName="+brandName+"&filterBy="+filterBy+"&itemId="+itemId+"&navAction="+navAction+"&navCount="+navCount+"&brandBy="+brandBy+"&brandId="+brandId;
		}
		else if(criteria=="natural"){
			document.getElementById("checkNatural").checked=true;
			document.filterForm.action = "shop_sub_category.jsp?resetBean="+resetBean+"&onProdList="+onProdList+"&brandName="+brandName+"&filterBy="+filterBy+"&itemId="+itemId+"&navAction="+navAction+"&navCount="+navCount+"&brandBy="+brandBy+"&brandId="+brandId;
		}
		document.form.submit();
		document.filterForm.submit();
	}
	function removeFilter(criteria,url,selectedCriteria)
    {
    	if(document.form.range.options[document.form.range.options.selectedIndex].text=="ALL")
    	{
    	document.form.range.value="20";
    	}
		if(criteria=="FSA")
			document.getElementById("checkFSA").checked=false;
		else if(criteria=="online")
			document.getElementById("checkOnline").checked=false;
		else if(criteria=="sales")
			document.getElementById("checkSales").checked=false;
		else if(criteria=="new")
			document.getElementById("checkNew").checked=false;
		else if(criteria=="natural")
			document.getElementById("checkNatural").checked=false;
		if(selectedCriteria=="FSA" && criteria=="FSA")
			document.filterForm.action =url+"&filterBy=";
		else if(selectedCriteria=="online" && criteria=="online")
			document.filterForm.action =url+"&filterBy=";
		else if(selectedCriteria=="sales" && criteria=="sales")
			document.filterForm.action =url+"&filterBy=";
		else
			document.filterForm.action =url+"&filterBy="+selectedCriteria;
			
		document.getElementById("form").submit();
		document.getElementById("filterForm").submit();
	}
	function brandRemoveFilter(removeUrl)
	{
		if(document.form.range.options[document.form.range.options.selectedIndex].text=="ALL")
    	{
    	document.form.range.value="20";
    	}
		document.filterForm.action =removeUrl;
		document.getElementById("form").submit();
		document.getElementById("filterForm").submit();
	}
	function removeAllFilters(url)
    {
    	if(document.form.range.options[document.form.range.options.selectedIndex].text=="ALL")
    	{
    	document.form.range.value="20";
    	}
		document.getElementById("checkFSA").checked=false;
		document.getElementById("checkOnline").checked=false;
		document.getElementById("checkSales").checked=false;
		document.getElementById("checkNew").checked=false;
		document.getElementById("checkNatural").checked=false;
		document.getElementById("filterForm").action=url;
		document.getElementById("form").submit();
		document.getElementById("filterForm").submit();
	}
	function clearStart(url,index)
{
	if(document.form.range.options[document.form.range.options.selectedIndex].text=="ALL")
    	{
    	document.form.range.value="20";
    	}
	document.form.pageJump.value="1";
	var brandname= document.getElementById("brandText"+index).value;
	var brandId= document.getElementById("brandId"+index).value;
	document.filterForm.action =url+"&brandName="+brandname+"&brandId="+brandId;
	document.form.action =url+"&brandName="+brandname+"&brandId="+brandId;
	document.getElementById("filterForm").submit();
	document.getElementById("form").submit();
}