// JavaScript Document
function Trim(str)
{
	//trim leding spaces
	while(true)
	{
		if(str.charAt(0) == ' ')
			str = str.substr(1);
		else
			break;
	}
	
	//trim trailing spaces
	while(true)
	{
		if(str.charAt(str.length-1) == ' ')
			str = str.substr(0,str.length-1);
		else
			break;
	}
	return(str);	
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}
/*clear quantity*/

function restoreqty()
{document.getElementById('qty').value=0;}
/*create cookie*/
function createCookiefromallproduct(pname,pid) 
{
	var qty="qty"+pid;
	if(document.getElementById(qty).value==0)
	{alert("Please select the Quantity");}
	else if(document.getElementById(qty).value!=0)
	{
		var prodname = pname;
		var price=getprodprice(pid);
		var productadddiv="productadddiv"+pid;
		//var chkupd=updatecookie(document.getElementById('prodname').value,document.getElementById('qty').value);
		//	if (chkupd>0)
		//	{document.cookie = "HUSA"+document.getElementById('prodname').value+"="+chkupd+"="+prodname+";path=/";}
		//	else
		//	{
		document.cookie = "HUSA"+pid+"="+document.getElementById(qty).value+"="+prodname+"="+price+";path=/";
		//}
		shoppinginfo();
		document.getElementById(productadddiv).style.display="block";	
		hide_divauto2(pid);
	}
}
/*create cookie*/
function createCookiefromproduct() 
{
	if(document.getElementById('qty').value==0)
	{alert("Please select the Quantity");}
	else if(document.getElementById('qty').value!=0)
	{
		var prodname = document.getElementById('prodname').value;
		var price=getprodprice(document.getElementById('prodid').value);
		//var chkupd=updatecookie(document.getElementById('prodname').value,document.getElementById('qty').value);
		//	if (chkupd>0)
		//	{document.cookie = "HUSA"+document.getElementById('prodname').value+"="+chkupd+"="+prodname+";path=/";}
		//	else
		//	{
		document.cookie = "HUSA"+document.getElementById('prodid').value+"="+document.getElementById('qty').value+"="+prodname+"="+price+";path=/";
		//}
		shoppinginfo();
		document.getElementById('productadddiv').style.display="block";	
		hide_divauto();
	}
}
// Exit without save
function hide_div()
{
	document.getElementById('productadddiv').style.display="none";
}
function hide_div2(pval)
{
	var productadddiv="productadddiv"+pval;
	document.getElementById(productadddiv).style.display="none";
}
// automatically 
function hide_divauto()
{
	setTimeout ( "hide_div()", 2000 );
}
function hide_divauto2(pval)
{
	setTimeout ( "hide_div2("+pval+")", 2000 );
}
function shoppinginfo()
{	
	var k;
	k=0;
	var c;
	var gt;
	gt=0;
	var cntqty;
	cntqty=0;
	var product;
	var viewtab;
	var ca = document.cookie.split(';');
	if(ca.length>0)
	{
		for(var i=0;i<ca.length;i++) 
		{
			var prod2,qty2,prodid,pr2;
			var pname;
			prod2=0;
			qty2=0;
			k=k+1;
			c = ca[i];
			product=c.split('=');
			prod2=product[0];
			if (prod2.indexOf("HUSA")>=0)
			{
				qty2=parseInt(product[1]);
				pname=product[2];
				pr2=product[3];
				prodid=Trim(prod2).substring(4,prod2.length);
				gt=gt+(parseFloat(pr2)*parseFloat(qty2));
				cntqty=cntqty+parseFloat(qty2);
			}
		}
		document.getElementById('item').innerHTML="<br><br><b>&nbsp;"+cntqty+"&nbsp;items&nbsp;@&nbsp;$&nbsp;"+parseFloat(gt).toFixed(2)+"</b>";
	}
	else
	{
		document.getElementById('item').innerHTML="";
	}
}

/*create cookie*/
function createCookiefromqof() 
{
	if(document.getElementById('prodname').value==0 && document.getElementById('qty').value!=0)
	{alert("Please select the product");}
	else if(document.getElementById('prodname').value!=0 && document.getElementById('qty').value==0)
	{alert("Please select the Quantity");}
	else if(document.getElementById('prodname').value!=0 && document.getElementById('qty').value!=0)
	{
		var x=document.getElementById('prodname');
		var prodname = (x.options[x.selectedIndex].text);
		var price=getprodprice(document.getElementById('prodname').value);
		//var chkupd=updatecookie(document.getElementById('prodname').value,document.getElementById('qty').value);
		//	if (chkupd>0)
		//	{document.cookie = "HUSA"+document.getElementById('prodname').value+"="+chkupd+"="+prodname+";path=/";}
		//	else
		//	{
		document.cookie = "HUSA"+document.getElementById('prodname').value+"="+document.getElementById('qty').value+"="+prodname+"="+price+";path=/";
		//}
		readcook();
	}
}
function readcook()
{	
	var k;
	k=0;
	var c;
	var gt;
	gt=0;
	var product;
	var viewtab;
	var cookval;
	var ca;
	ca="";
	document.getElementById('prodview').innerHTML="";
	viewtab="<table class='headerTable' width='100%' cellpadding='0' cellspacing='0'>";
	viewtab=viewtab+"<tr class='thStyle'>";
	viewtab=viewtab+"<td><div align='center'>Remove Items</div></td>";
	viewtab=viewtab+"<td><div align='center'>Product Name</div></td>";
	viewtab=viewtab+"<td><div align='center'>Price</div></td>";
	viewtab=viewtab+"<td><div align='center'>Quantity</div></td>";
	viewtab=viewtab+"<td><div align='center'>Total</div></td>";
	viewtab=viewtab+"</tr>";
	cookval=Trim(document.cookie);
	if (cookval!="")
	{
			ca = cookval.split(';');
			if(ca.length>=1)
			{
				for(var i=0;i<ca.length;i++) 
				{
					var prod2,qty2,prodid,pr2;
					var pname;
					prod2=0;
					qty2=0;
					k=k+1;
					c = ca[i];
					product=c.split('=');
					prod2=product[0];
					if (prod2.indexOf("HUSA")>=0)
					{
						qty2=parseInt(product[1]);
						pname=product[2];
						pr2=product[3];
						prodid=Trim(prod2).substring(4,prod2.length);
						viewtab=viewtab+"<tr>"
						viewtab=viewtab+"<td class='tdcol'><div align='center'><a href='javascript:delcookie("+prodid+")'>Remove</a></div></td>"
						//viewtab=viewtab+"<td class='tdcol'><div align='center'><img src='thumbimg/"+prodid+".jpg'></div></td>"
						viewtab=viewtab+"<td class='tdcol'><div align='left'><div id='pname"+prodid+"' style='padding-top:10px;'>&nbsp;"+pname+"</div>&nbsp;</div></td>"
						viewtab=viewtab+"<td class='tdcol'><div align='right'><div id='price"+prodid+"' style='padding-top:10px;'>$"+pr2+"</div>&nbsp;</div></td>"
						viewtab=viewtab+"<td class='tdcol'><div align='center'><select name='qty"+prodid+"' id='qty"+prodid+"' onChange='modifycookie("+prodid+",this.value)' class='inputbox'>";
							for (j=1;j<=50;j++)
							{
								if (qty2==j)
								{viewtab=viewtab+"<option value="+j+" selected>"+j+"</option>";}
								else
								{viewtab=viewtab+"<option value="+j+">"+j+"</option>";}
							}
						viewtab=viewtab+"&nbsp;</select></div></td>";
						viewtab=viewtab+"<td class='tdcol'><div align='right'>&nbsp;$"+(parseFloat(pr2)*parseFloat(qty2)).toFixed(2)+"&nbsp;</div></td>"
						viewtab=viewtab+"</tr>"
						gt=gt+(parseFloat(pr2)*parseFloat(qty2));
					}
				}
				viewtab=viewtab+"<tr>"
				viewtab=viewtab+"<td class='tdcol' colspan='4'><div align='center'><strong>Grand Total</strong></div></td>"
				viewtab=viewtab+"<td class='tdcol'><div align='Right'><strong>&nbsp;$"+gt.toFixed(2)+"&nbsp;</strong></div></td>"
				viewtab=viewtab+"</tr>"
				}
	}
	else
	{
		viewtab=viewtab+"<tr>"
		viewtab=viewtab+"<td class='tdcol' colspan='5'><div align='center'>Shopping Cart is Empty</div></td>"
		viewtab=viewtab+"</tr>"
	}
	viewtab=viewtab+"</table>";
	document.getElementById('prodview').innerHTML=viewtab;
	shoppinginfo();
}

function modifycookie(pid,qval)
{
		var pname="pname"+pid;
		var prodname = document.getElementById(pname).innerHTML;
		prodname=prodname.replace("&nbsp;"," ");
		prodname=prodname.replace("&amp;"," & ");
		var pr="price"+pid;
		var price=document.getElementById(pr).innerHTML;
		price=price.replace("&nbsp;"," ");
		price=price.replace("$","");
		document.cookie = "HUSA"+pid+"="+qval+"="+prodname+"="+price+";path=/";
		readcook();	
}
/* make quantity update for same product */
function updatecookie(pval,qval)
{
	var prod3,qty3,qq,product2,prodid3;
	prod3=0;
	qty3=0;
	qq=0;
	var c2;
	var ca = document.cookie.split(';');
	if(ca.length>0)
	{
		for(var i=0;i<ca.length;i++) 
		{
			c2 = ca[i];
			var product2=c2.split('=');
			prod3=product2[0];
			if (prod3.indexOf("HUSA")>=0)
			{
				prodid3=Trim(prod3).substring(4,prod3.length);
				qty3=parseInt(product2[1]);
				if (pval==prodid3)
				{qq=parseInt(qty3)+parseInt(qval);}
			}
		}
	}
	return qq;
}
/*Delete the Product*/
function delcookie(pid)
{
	var prod4,qty4,product4,prodid2,pr4;
	var prodname2;
	prod4=0;
	var c3;
	var ca = document.cookie.split(';');
	if(ca.length>0)
	{
		for(var i=0;i<ca.length;i++) 
		{
			c3 = ca[i];
			var product4=c3.split('=');
			prod4=product4[0];
			if (prod4.indexOf("HUSA")>=0)
			{
				qty4=parseInt(product4[1]);
				prodname2=product4[2];
				pr4=product4[3];
				prodid2=Trim(prod4).substring(4,prod4.length);
				if(pid==prodid2)
				{
					var date = new Date();
					date.setTime(date.getTime()+(-1*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
					document.cookie = "HUSA"+pid+"="+qty4+"="+prodname2+"="+pr4+";expires="+expires+";path=/";
				}
			}
		}
	}
	readcook();
}

/*Finally submit to  netsuite site*/
function readCookiefinal() 
{
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';
	var ca = document.cookie.split(';');
	subfrm3(0);
}
function subfrm3(i)
{
			var product;
			var prod,qty,id;
			var c;
			prod=0;
			qty=0;
			id=0;
			var ca = document.cookie.split(';');
			c = ca[i];
			product=c.split('=');
			prod=product[0];
			if (prod.indexOf("HUSA")>=0)
			{
				qty=parseInt(product[1]);
				id=Trim(prod).substring(4,prod.length);
				document.getElementById('shoppost2').contentWindow.document.getElementById('buyid').value=parseInt(id);
				document.getElementById('shoppost2').contentWindow.document.getElementById('add').value=parseInt(qty);
				document.getElementById('shoppost2').contentWindow.document.form1.action="http://shopping.netsuite.com/app/site/query/additemtocart.nl?c=298357&n=1&ext=T";
				document.getElementById('shoppost2').contentWindow.document.form1.submit();	
				document.getElementById('hidordcount').value=parseInt(document.getElementById('hidordcount').value)+1;
			}
			if (document.getElementById('hidordcount').value<ca.length) 
			{
				setTimeout ( "subfrm3('"+document.getElementById('hidordcount').value+"')", 4000);
			}
			else if (document.getElementById('hidordcount').value==ca.length) 
			{
				setTimeout ("chgpath()",500);
			}
}

function subfrm2(p,q)
{
	document.getElementById('shoppost2').contentWindow.document.getElementById('buyid').value=parseInt(p);
	document.getElementById('shoppost2').contentWindow.document.getElementById('add').value=parseInt(q);
	document.getElementById('shoppost2').contentWindow.document.form1.action="http://shopping.netsuite.com/app/site/query/additemtocart.nl?c=298357&n=1&ext=T";
	document.getElementById('shoppost2').contentWindow.document.form1.submit();
	document.getElementById('shoppost2').src="cookiecart3.htm";
}
function chgpath()
{
	window.location="http://shopping.netsuite.com/himalayausa";
	//clearcookie();
}
function clearcookie()
{
	var prod5,qty5,product5,pr5;
	var prodname3;
	prod5=0;
	qty5=0;
	var c4;
	var date = new Date();
	date.setTime(date.getTime()+(-1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	var ca = document.cookie.split(';');
	if(ca.length>0)
	{
		for(var i=0;i<ca.length;i++) 
		{
			c4 = ca[i];
			product5=c4.split('=');
			prod5=product5[0];
			if (prod5.indexOf("HUSA")>=0)
			{
				qty5=parseInt(product5[1]);
				prodname3=product5[2];
				pr5=product5[3];
				document.cookie = prod5+"="+qty5+"="+prodname3+"="+pr5+";expires="+expires+";path=/";
			}
		}
	}
}


/* Product price list */
function getprodprice(prodval)
{
	switch (prodval)
	{
			case "1486": //Volumizing Shampoo 11.83 fl oz
			return "8.99";
		break;
			case "1487"://Hydrating Shampoo 11.83 fl oz
			return "8.99";
		break;
			case "1488"://Rejuvenating Shampoo 11.83 fl oz
			return "8.99";
		break;
			case "1489"://Amla & Holy Basil Conditioner 11.83 fl oz
			return "8.99";	
		break;
			case "1474"://Neem & Turmeric Face Wash 5.07 fl oz
			return "7.99";	
		break;
			case "1477"://Invigorating Face Wash 5.07 fl oz
			return "7.99";	
		break;
			case "1476"://Hydrating Face Wash 5.07 fl oz
			return "7.99";	
		break;
			case "1478"://Exfoliating Walnut & Wood Apple Face Scrub 5.07 fl oz
			return "7.99";	
		break;
			case "1483"://Nourishing Night Cream 3.38 fl oz
			return "12.99";	
		break;
			case "1482"://Nourishing Face Moisturizing Lotion 6.76 fl oz
			return "9.99";	
		break;
			case "1490"://Soothing Lotus Flower LipCare 0.176 oz
			return "2.49";
		break;
			case "1484"://Revitalizing Hand & Body Lotion 6.76 fl oz
			return "8.99";	
		break;
			case "1485"://Rejuvenating Massage Oil 6.8 fl oz
			return "12.99";	
		break;
			case "1480"://Stress Massage Oil 6.8 fl oz
			return "12.99";	
		break;
			case "1472"://Purifying Neem & Turmeric Handcrafted Cleansing Bar 4.41 oz
			return "4.99";	
		break;
			case "1473"://Refreshing Lavender and Rosemary Handcrafted Cleansing Bar 4.41 oz
			return "4.99";	
		break;
			case "1492"://i.e. balm (inhale ~ exhale) 1.76 oz
			return "7.99";	
		break;
			case "1491"://U-Knead-It Balm 1.76 oz
			return "7.99";	
		break;
			case "1479"://Neem & Pomegranate Toothpaste 5.29 oz
			return "4.99";	
		break;
			case "1481"://Intensive Moisturizing FootCare Cream 3.53 oz
			return "9.99";	
		break;
			default : return "0";
	}
}

