// JavaScript Document
function calcTot(c,form,i)
{
	var tot,pst,gst;
	pst = eval(form.pst.value);

	gst = eval(form.gst.value) ;

	form.subTot.value = eval(form.subTot.value) - eval(form.elements[i].value);

	form.subTot.value = eval(form.subTot.value) + eval(c);

	tot = form.subTot.value;

	form.grandTot.value = pst + gst + eval(tot);
}



function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }





function calCost(q,p,t) 
{
 
// check if the passed value of quantity is a number 
   var PassedQTY = eval("document.form.qty"+q.name.substring(3)).value ;
	if(isNaN(PassedQTY)){
		alert ("Please enter numbers only.");
		eval("document.form.qty"+q.name.substring(3)).value = 0;
	}
// Check the quantity is positive number.	
	if( PassedQTY < 0){
		alert ("Please enter positive number.");
		eval("document.form.qty"+q.name.substring(3)).value = 0;
	}	
// Check the quantity is not decimal num.
	 if (q.value.indexOf(".") == 1){
	 alert ("Please note that we do not accept order in decimal number.");	
	 eval("document.form.qty"+q.name.substring(3)).value = 0;
	 }
	





	
// Get values from form	
	var oldCost = eval("document.form.cost"+q.name.substring(3)+".value");
	//alert("oldCost: " + oldCost);
	var newCost = eval(q.value * p);
	//alert("newCost: " + newCost);
	var oldSubTot = eval(document.form.subTot.value);
	//alert("oldSubTot: " + oldSubTot);
	var newPST, newGST, newSubTot,tempPST,tempGST,tempSubTot;
	var oldPST = eval(document.form.pst.value);
	//alert("oldPST: " + oldPST);
	var oldGST = eval(document.form.gst.value);
	//alert("oldGST: " + oldGST);
// This statements will be executed only if the price is taxable

	if (t == "tax")
	{
		tempPST = oldPST - eval(format(oldCost * 0.08));
		//alert("tempPST: " + tempPST);
		tempGST = oldGST - eval(format(oldCost * 0.05));
		//alert("tempGST: " + tempGST);
		eval("document.form.pst"+q.name.substring(3)).value = format(eval(newCost * 0.08));
		eval("document.form.gst"+q.name.substring(3)).value = format(eval(newCost * 0.05));
		newPST = tempPST + eval(format(eval(newCost * 0.08)));
		//alert("newPST: " + newPST);
		newGST = tempGST + eval(format(eval(newCost * 0.05)));
		//alert("newGST: " + newGST);
		document.form.pst.value = format(newPST);
		document.form.gst.value = format(newGST);
		tax = newPST + newGST; 
	}
	else
	{	
		tax = eval(oldPST + oldGST);
		//alert ("tax :" + tax);
	}
	// Subtract oldCost from subtotal
	tempSubTot = oldSubTot - eval(oldCost);
	//alert("tempSubTot: " + tempSubTot);

	newSubTot = eval(tempSubTot) + eval(newCost) ;
	//alert("newSubTot: " + newSubTot);

	// This is where the new values are added!!
	eval("document.form.cost"+q.name.substring(3)).value = format(newCost);
	document.form.subTot.value = format(newSubTot);
	document.form.grandTot.value = format(newSubTot + tax);
}

function addTax(q,p,i)

{

	document.form.pst.value = eval(document.form.pst.value) - (eval(document.form.elements[i].value)* 0.08);

	var pst = (q*p)* 0.08;

	document.form.pst.value = format(eval(document.form.pst.value) + eval(pst)); 

	document.form.gst.value = eval(document.form.gst.value) - format((document.form.elements[i].value * 0.05));

	var gst = (q*p)* 0.05;

	document.form.gst.value = format(eval(document.form.gst.value) + eval(gst)); 

}

function format(num)

   {

   finished=Math.floor(num)+".";

   var base=100*(num-Math.floor(num))+0.5;

   finished += Math.floor(base/10);

   finished += Math.floor(base%10);

   return finished;

   }
function checkCookieFromTexBox(qt,box)
{
	var quantity = window.document.form.qty1.value;
	if (qt > quantity)
	{ 
		window.document.form.qty1.focus();
		alert("If you are going to type in this box, make sure you have selected " + qt+ " cookie(s) in the quantity box above" );
		
	}
} 
