/*
	Compiled all cart functions in one place by karthik
*/

//declare this variable in your page before including this file
//var clearCartPage        = "ShoppingBag.asp";
//var continueCheckoutPage = "Registration.asp";
//var continueShoppingPage = "Product.asp";

function AddToCart(newProductName,newProductId,newPrice,newQty,newProductType,newProductFlavour,newSetupfee,newMinQty)
{

	if ((newQty=="") || (newQty < 1))
	{
		alert("Qty cannot be null, non-numeric or Less than One.");
	}
	else 
	{
	index = document.cookie.indexOf("MyCart");
	
	if (index == -1)	//addition of first item in the cart
	{
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		
		if (countend == -1) 
		{
			countend = document.cookie.length;
		}
		
		document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+"!"+newPrice+"^"+newQty+"#"+newProductType+"*"+newProductName+"~"+newProductFlavour+"|"+newSetupFee+"}"+newMinQty+"$]";
    }
    else
    {
		blnFound = false;
		newitemlist = null;
		itemstart = 0;
		itemend = 0;
		index = document.cookie.indexOf("MyCart");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1) 
		{
			countend = document.cookie.length;
		}
		fulllist = document.cookie.substring(countbegin, countend);
		for (var i = 0; i <= fulllist.length; i++) 
		{
			itemMainStart = 0;
			if (fulllist.substring(i,i+1) == '[') 
			{
				itemstart = i+1;
			} 
			else if (fulllist.substring(i,i+1) == ']') 
			{
				itemend = i;
				tempProductId = fulllist.substring(itemstart,itemComma);
				tempType      = fulllist.substring(itemHash+1,itemStar);
				tempFlavour   = fulllist.substring(itemTild+1,itemend);
				tempNewQty = parseInt(tempQty)+parseInt(newQty);
				if (tempNewQty>=100)
				{
					tempNewQty = 99;
				}
				afterString = fulllist.substring(itemHash,fulllist.length);
				if (tempProductId==newProductId && tempType == newProductType && tempFlavour == newProductFlavour)
				{
					newitemlist = prevString + tempNewQty + afterString;
					
					document.cookie="MyCart="+newitemlist;
					
					blnFound = true;
					break;
				}
			}
			else if (fulllist.substring(i,i+1) == '^') 
			{	
				prevString = fulllist.substring(itemMainStart,i+1);
				itemCarret = i;
			}
			else if (fulllist.substring(i,i+1) == '*') 
			{	
				itemStar = i;
			}
			else if (fulllist.substring(i,i+1) == '!') 
			{	
				itemComma = i;
			}
			else if (fulllist.substring(i,i+1) == '#') 
			{	
				tempQty = fulllist.substring(itemCarret+1,i);
				itemHash = i;
			}
			else if (fulllist.substring(i,i+1) == '~') 
			{	
				itemTild = i;
			}
			else if (fulllist.substring(i,i+1) == '|') 
			{	
				itemPipe = i;
			}
			else if (fulllist.substring(i,i+1) == '}') 
			{	
				itemRBracket = i;
			}
			else if (fulllist.substring(i,i+1) == '$') 
			{	
				itemDollarSign = i;
			}

		}
		if (blnFound==false)
		{
			//list = "MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+"!"+newPrice+"^"+newQty+"#"+newProductType+"*"+newProductName+"]";
			
			document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+"!"+newPrice+"^"+newQty+"#"+newProductType+"*"+newProductName+"~"+newProductFlavour+"|"+newSetupFee+"}"+newMinQty+"$]";
		}
		
		//var sTemp = "SearchProducts.asp?txtSearch=" + sSearch;
		//var sTemp = "CartDisplay.asp?fileurl="+getUrl()+"&txtSearch="+getStr()+";
		//window.location.href  = sTemp;
    }
    //window.location.href  = "CartDisplay.asp";
    //callCart();
    return;
    }
}

function clearCart() 
{
	if (confirm('Are you sure you wish to clear the cart?')) 
	{
		index = document.cookie.indexOf("MyCart");
		document.cookie="MyCart=Empty";
		window.location.href  = clearCartPage;
	}
}

// Function to Display Currency
function convertValue(val)
{
  val = val.toString();
  var place = 2;
  var chkPoint;
  chkPoint = val.indexOf('.');
  if (chkPoint == -1)
  {
  	val = val + ".00";
  }
  else
  {
    var decLen = val.substr(chkPoint + 1 , val.length - (chkPoint + 1)).length
    
    if(decLen == 1)
    {
      val = val + '0';
    }
    if(decLen > 2)
    {
      x = val.substr(0,chkPoint); 
      y = val.substr(chkPoint + 1,place);
      z = val.substr( chkPoint + 1 + place , (val.length-(chkPoint + 1 + place )) );

      val1 = parseFloat(x + y + '.' + z);
    
      val1 = Math.round(val1); 
      val1 = val1.toString();
      val = val1.substr(0 , val1.length - place) + '.' + val1.substr(val1.length - place,val1.length);
    }
  }	
  return val;
}

//returns total amount of the cart.
function CartAmount()
{
    var itemstart;
	var itemend;

	index = document.cookie.indexOf("MyCart");
	countofB = (document.cookie.indexOf("["));
	if (countofB == -1)
	{	
		document.cookie="MyCart=Empty";
	}
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) 
	{
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);

	totprice = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
		if (fulllist.substring(i,i+1) == '[') 
		{
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == ']') 
		{
			itemend = i;
			theitem = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
		} 
		else if (fulllist.substring(i,i+1) == '!') 
		{
			theid = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '^') 
		{
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '#') 
		{
			thequantity = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '*') 
		{
			thecode = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '~') 
		{
			theoption = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '|') 
		{
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '}') 
		{
			thesetupfee = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '$') 
		{
			theminqty = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}

	}
	return convertValue(totprice.toString());
}

//returns no. of Items in the cart.
function CartTotalItems()
{
    var itemstart;
	var itemend;

	index = document.cookie.indexOf("MyCart");
	countofB = (document.cookie.indexOf("["));
	if (countofB == -1)
	{	
		document.cookie="MyCart=Empty";
	}
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) 
	{
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);

	totprice = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
		if (fulllist.substring(i,i+1) == '[') 
		{
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == ']') 
		{
			itemend = i;
			theitem = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
		} 
		else if (fulllist.substring(i,i+1) == '!') 
		{
			theid = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '^') 
		{
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '#') 
		{
			thequantity = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '*') 
		{
			thecode = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '~') 
		{
			theoption = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '|') 
		{
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '}') 
		{
			thesetupfee = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}

	}
	return itemlist;
}

function checkOut() 
{
	var itemstart;
	var itemend;

	index = document.cookie.indexOf("MyCart");
	countofB = (document.cookie.indexOf("["));
	if (countofB == -1)
	{	
		document.cookie="MyCart=Empty";
	}
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);

	if (countend == -1) 
	{
		countend = document.cookie.length;
	}

	fulllist = document.cookie.substring(countbegin, countend);
	
	if (fulllist=="Empty" || fulllist=="null")
	{
		alert("Can't Checkout, Cart is Empty.");
	}
	else
	{
	  //window.location = continueCheckoutPage;
	  //window.location = "CheckOut.asp";
	  document.frmBottom.txtFullList.value = fulllist;
	  document.frmBottom.action = continueCheckoutPage;
	  document.frmBottom.submit();
	}
}

function updateCart()
{

	itemstart = 0;
	itemend = 0;
	var itemCount;
	itemCount = 0;
	index = document.cookie.indexOf("MyCart");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) 
	{
		countend = document.cookie.length;
	}
	var fulllist = document.cookie.substring(countbegin, countend);

	removeItemNo = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
			
		itemMainStart = 0;
		if (fulllist.substring(i,i+1) == '[') 
		{
			itemstart = i+1;
				
		} 
		else if (fulllist.substring(i,i+1) == ']') 
		{
			newitemlist = null;
			itemend = i;
			removeItemNo = removeItemNo + 1;
			tempProductId = fulllist.substring(itemstart,itemComma);
			newProductId = document.frmDisplayCart.elements[itemCount].name;
			tempNewQty = document.frmDisplayCart.elements[itemCount].value;
			tempMinQty = fulllist.substring(itemRBracket+1,itemDollarSign);


			afterString = fulllist.substring(itemHash,fulllist.length);
				
			if (tempProductId==newProductId )
			{
				if ((tempNewQty=="") || (tempNewQty==0))
				{
					alert("Qty cannot be null, non-numeric or zero");
					return ;
					//removeItem(removeItemNo);					
				}
				else
				{
					newitemlist = prevString + tempNewQty + afterString;
					document.cookie="MyCart="+newitemlist;
					//return true;
				}
			}
			itemCount = itemCount+1;
			if ((tempNewQty>oldQty) && (tempNewQty>=10))
			{
				i = i+1;
			}
			if ((tempNewQty<oldQty) && (tempNewQty<10) && (oldQty>=10))
			{
				i = i-1;
			}
		}
		else if (fulllist.substring(i,i+1) == '^') 
		{	
			prevString = fulllist.substring(itemMainStart,i+1);
			itemC = i+1;
		}
		else if (fulllist.substring(i,i+1) == '*') 
		{	
			itemStar = i;
			oldQty = fulllist.substring(itemC,i);	
		}
		else if (fulllist.substring(i,i+1) == '!') 
		{	
			itemComma = i;
		}
		else if (fulllist.substring(i,i+1) == '#') 
		{	
			itemHash = i;
		}
		else if (fulllist.substring(i,i+1) == '~') 
		{	
			itemTild = i;
		}
		else if (fulllist.substring(i,i+1) == '|') 
		{	
			itemPipe = i;
		}
		else if (fulllist.substring(i,i+1) == '}') 
		{	
			itemRBracket = i;
		}
		else if (fulllist.substring(i,i+1) == '$') 
		{	
			itemDollarSign = i;
		}
		
		index = document.cookie.indexOf("MyCart");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1) 
		{
			countend = document.cookie.length;
		}
		fulllist = document.cookie.substring(countbegin, countend);
	}	// end of for loop
	index = document.cookie.indexOf("MyCart");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) 
	{
		countend = document.cookie.length;
	}
		
	fulllist = document.cookie.substring(countbegin, countend);
	window.location.href  = clearCartPage;
}

function removeItem(itemno) 
{
	if (confirm("Are you sure you want to remove this item from cart?")){
		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) 
		{
			if (fulllist.substring(i,i+1) == '[') 
			{
				itemstart = i+1;
			} 
			else if (fulllist.substring(i,i+1) == ']') 
			{
				itemend = i;
				theitem = fulllist.substring(itemstart, itemend);
				itemlist=itemlist+1;
				if (itemlist != itemno) 
				{
					newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
				}
			}
		}

		index = document.cookie.indexOf("MyCart");
		document.cookie="MyCart="+newItemList;
		fulllist = document.cookie.substring(countbegin, countend);
		
		window.location.href  = clearCartPage;
	}
}

var fulllist;
function cartDisplay()
{ 
    var itemstart;
    var showtype;
    showtype = "";
    var setupfeetotal=0.00;
	var itemend;

	index = document.cookie.indexOf("MyCart");
	countofB = (document.cookie.indexOf("["));

	if (countofB == -1)
	{	
		document.cookie="MyCart=Empty";
		index = document.cookie.indexOf("MyCart");
		countofB = (document.cookie.indexOf("["));
	}
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	
	if (countend == -1) 
	{
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);

	//alert(fulllist);
	if (fulllist=="Empty" || fulllist=="null")
	{
			document.write ('<TABLE cellSpacing=0 cellPadding=0 width=100% border="0">');
			document.write ('<TBODY>');
			document.write ('<TR>');
			document.write ('<TD vAlign=center align=middle>');
			document.write ('<a href="javascript:continueShopping()">');
			document.write ('<IMG   alt="continue shopping" src="images/animcontinueshopping.jpg" border="0"></A>');
			document.write ('</TD>');
			document.write ('</TR>');
			document.write ('</TBODY>');
			document.write ('</TABLE>');
	}
	else
	{
	totprice = 0;
	
	document.write('<form name="frmDisplayCart" id="frmDisplayCart" method="post">');
	document.write('<TABLE cellSpacing=0 cellPadding=0 width=100% border="0" valign=top>');
	document.write('<TR><TD colSpan=7>');
		document.write('<table width="580" border="0" cellspacing="1" cellpadding="5" bgcolor="#DAB662">');
// start Table


	document.write('<TR class=Subhead>');
  	//document.write('<TD width=1 bgcolor="E7DFAB" class="carttitle">');
	//document.write('<IMG height=1 alt="" src="images/spacer.gif" width=0 border="0">');
   	//document.write('</TD>');
    
	document.write('<TD vAlign=center align=middle width=10% height=20 bgcolor="E7DFAB" class="carttitle">');
		document.write('<B>Delete</B>');
	document.write('</TD>');
	document.write('<TD vAlign=center align=center width=20% bgcolor="E7DFAB" class="carttitle">');
	document.write('<B>Quantity</B>');
	document.write('</TD>');
      document.write('<TD vAlign=center align=left width=40% bgcolor="E7DFAB" class="carttitle">');
	document.write('<B>Product Name</B>')
	document.write('</TD>');
	document.write('<TD vAlign=center align=right width=15% bgcolor="E7DFAB" class="carttitle">');
	document.write('<B>Selling Price</B>');
	document.write('</TD>');
	document.write('<TD vAlign=center align=right width=15% bgcolor="E7DFAB" class="carttitle">');
	document.write('<B>Amount</B>');
	document.write('</TD>');
	//document.write('<TD width=1% bgcolor="E7DFAB" class="carttitle"><IMG height=1 alt="" src="images/spacer.gif" width=0 border="0">');
	//document.write('</TD>');
	document.write('</TR>');
		
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) 
	{
		if (fulllist.substring(i,i+1) == '[') 
		{
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == ']') 
		{
			itemend = i;
			theflavour = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			setupfeetotal = setupfeetotal + thesetupfee;
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;

			document.write('<TR>');
				document.write('<TD vAlign=center width=10% align=middle height=20 bgcolor="E8E1B5">');
					document.write('<A href="javascript:removeItem('+itemlist+')"><IMG height=20 alt=remove src="images/remove.jpg" width=20 border="0"></A>');
				document.write('</TD>');
				document.write('<TD vAlign=center width=20% align=middle bgcolor="E8E1B5">');
					document.write('<input type="text" class="input_text" name='+theid+' id='+theid+' size="3" value='+thequantity+' onkeypress="return validate()" maxlength="2" onblur="return validateIntOnblur(this,this.value)">');
				document.write('</TD>');
				document.write('<TD vAlign=center width=40% align=left class="carttitle" bgcolor="E8E1B5">');
					var prdItem='';
					for(var frmpos=0;frmpos <= theitem.length; frmpos++)
					{
						if (theitem.charAt(frmpos) == '`')
						{
							prdItem = prdItem+"'";
						}
						else
						{
							prdItem = prdItem+theitem.charAt(frmpos);
						}
					}
					if (thetype == "M")
						showtype = " (Milk)";
					else if (thetype == "D")
						showtype = " (Dark)";
					else if (thetype == "W")
						showtype = " (White)";
					else
						showtype == "";
				document.write(theitem+showtype);
				document.write('</TD>');
				document.write('<TD vAlign=center width=15% align=right bgcolor="E8E1B5" class="carttitle">');
					document.write('$');
					document.write(convertValue(theprice));
					document.write('&nbsp;&nbsp;');
				document.write('</TD>');
				document.write('<TD vAlign=center width=15% align=right bgcolor="E8E1B5" class="carttitle">');
					document.write('$');
					document.write(convertValue(itemtotal.toString()));
					document.write('&nbsp;&nbsp;');
				document.write('</TD>');
			document.write('</TR>');
			
		} 
		else if (fulllist.substring(i,i+1) == '!') 
		{
			theid = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '^') 
		{
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if (fulllist.substring(i,i+1) == '#') 
		{
			thequantity = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '*') 
		{
			thetype = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '~') 
		{
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '|') 
		{
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '}') 
		{
			thesetupfee = parseFloat(fulllist.substring(itemstart,i));
			itemstart = i+1;
		}
		else if (fulllist.substring(i,i+1) == '$') 
		{
			theminqty = parseFloat(fulllist.substring(itemstart,i));
			itemstart = i+1;
		}


	}
	document.write('<TR>');
		document.write('<TD vAlign=center align=left colspan=5 height=30 bgcolor="#F2EBBD">');
			document.write('<IMG height=1 alt="" src="images/spacer.gif" width=54 border="0">');
			document.write('<a href=javascript:updateCart()>');
            document.write('<IMG alt=update src="images/animUpdate.jpg" border="0" id=image1 name=image1>');
            document.write('</a>');
        document.write('</TD>');
	document.write('</TR>');
	
	document.write('<TR>');
        document.write('<TD vAlign=center align=right class="carttitle" colSpan=3 bgcolor="#E8E1B5">');
			document.write('<B>Sub-Total</B>&nbsp;&nbsp;');
		document.write('</TD>');
        document.write('<TD vAlign=center align=right class="carttitle" colSpan=2 bgcolor="#E8E1B5">');
			document.write('<B>');
			document.write('$');
			document.write(convertValue(totprice.toString()));
			document.write('</B>&nbsp;&nbsp;');
		document.write('</TD>');
		document.write('</TR>');
		document.write('</TABLE></TD></TR>');		

// end the table		
        document.write('</TBODY>');
		document.write('</TABLE>');
		document.write ('</form>');
		
		document.write ('<TABLE cellSpacing=0 cellPadding=0 width=100% border="0">');
        document.write ('<TBODY>');
        document.write ('<tr>');
			document.write ('<TD vAlign=center align=middle>');
		    document.write ('<a href=javascript:clearCart()>');
			document.write ('<IMG alt="empty cart" src="images/animEmptyCart.jpg"  border="0"></a>');
			document.write ('</td>');	
        document.write ('</tr>');

		document.write ('<tr>');
			document.write ('<TD vAlign=center align=middle height=15>');
		    document.write ('</td>');	
        document.write ('</tr>');
                
        document.write ('<TR>');
			document.write ('<TD vAlign=center align=middle>');
			//*****************
				document.write ('<a href="javascript:continueShopping()">');
				document.write ('<IMG  alt="continue shopping" src="images/animcontinueshopping.jpg" border="0"></A>');
				document.write ('&nbsp;&nbsp;&nbsp;&nbsp;');
				//document.write ('<Button name="btnContinue" class="input_text" style="font-size=10;width=100" onclick="javascript:callContinue();">Continue Shopping</Button>');
				document.write ('<a href="javascript:checkOut()">');
				document.write ('<img  alt="proceed to checkout"  src="images/animcheckout.jpg" border="0"></a>');
			document.write ('</TD>');
		document.write ('</TR>');
		document.write ('</TBODY>');
		document.write ('</TABLE>');
	}	
}

function validate()
{
	if ((event.keyCode>=48 && event.keyCode <=57))
	{
		return true;
	}
	else
	{
		if (event.keyCode == 13)
		{
			
			event.returnValue = false;
			var C = updateCart();
		}   
		else
		{
			return false;	
		}
	}	
}

function validateIntOnblur(objName,val)
{   
    var iVal = parseInt(val);
    
    if (isNaN(iVal))
	{
	  objName.value ="";
	  objName.focus();
	  return false;
	  
	}
	else
	{
	    objName.value = iVal;
		return true;
	}	
}

function continueShopping()
{
	window.location.href  = continueShoppingPage;
}
