function AddToCart(newProductName,newProductId,newPrice,newQty,newProductCode)

{

	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+"*"+newProductName+"]";

		document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

    }

    else

    {

		if(newProductCode=="PHO"){

			phamt=parseFloat(newPrice)* parseFloat(newQty);

			if(checkPhcard(phamt)){

				alert("Maximum limit for purchase of phone cards is $50.00")

				return;

			}

		}





		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);

				tempCode=fulllist.substring(itemHash+1,itemStar);

				tempNewQty = parseInt(tempQty)+parseInt(newQty);

				if (tempNewQty>=100)

				{

					tempNewQty = 99;

				}

				afterString = fulllist.substring(itemHash,fulllist.length);

				if (tempProductId==newProductId && tempCode == newProductCode)

				{

					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) == '*') 

			{	

				tempQty = fulllist.substring(itemCarret+1,i);

				itemStar = i;

			}

			else if (fulllist.substring(i,i+1) == ',') 

			{	

				itemComma = i;

			}

			else if (fulllist.substring(i,i+1) == '#') 

			{	

				itemHash = i;

			}

		}

		if (blnFound==false)

		{

			//list = "MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"*"+newProductName+"]";

			list = "MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

			

			//document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"*"+newProductName+"]";

			document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

		}

		

		//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;

    }

}



//For Movies and Music

function AddToCart_MM(newProductName,newProductId,newPrice,newQty,newProductCode,blnreDirect)

{

	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+"*"+newProductName+"]";

		document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

    }

    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);

				tempCode=fulllist.substring(itemHash+1,itemStar);

				tempNewQty = parseInt(tempQty)+parseInt(newQty);

				if (tempNewQty>=100)

				{

					tempNewQty = 99;

				}

				afterString = fulllist.substring(itemHash,fulllist.length);

				if (tempProductId==newProductId && tempCode == newProductCode)

				{

					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) == '*') 

			{	

				tempQty = fulllist.substring(itemCarret+1,i);

				itemStar = i;

			}

			else if (fulllist.substring(i,i+1) == ',') 

			{	

				itemComma = i;

			}

			else if (fulllist.substring(i,i+1) == '#') 

			{	

				itemHash = i;

			}

		}

		if (blnFound==false)

		{

			//list = "MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"*"+newProductName+"]";

			list = "MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

			

			//document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"*"+newProductName+"]";

			document.cookie="MyCart="+document.cookie.substring(countbegin, countend)+"["+newProductId+","+newPrice+"^"+newQty+"#"+newProductCode+"*"+newProductName+"]";

		}

		

    }

    if (blnreDirect=='Y')

    {

		window.location.href  = "CartDisplay.asp";

	}	

    return;

    }

}





// 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));

			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;

		}

	}

	return convertValue(totprice.toString());

}



// returns additional shipping charges

function Additional_Ship_Amount()

{

    var itemstart;

	var itemend;

	var shipCDAmt = 0;

	var shipDVDAmt = 0;

	

	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;



			var prdtype = '';

			for(var frompos=0;frompos <= thecode.length; frompos++)

			{

				if (thecode.charAt(frompos) == '_')

				{

					prdtype = thecode.substr(frompos+1,thecode.length);

					break;

				}

			}

			if (prdtype == 'CD')

			{

				shipCDAmt = Number(shipCDAmt) + Number(thequantity);

			}

			if (prdtype == 'DVD')

			{

				shipDVDAmt = Number(shipDVDAmt) + Number(thequantity);

			}

		} 

		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;

		}

	}

	if (shipDVDAmt > 0)

	{

		shipDVDAmt -= 1;

	}

	if (shipCDAmt > 0)

	{

		shipCDAmt -= 1;

	}



	return shipCDAmt + shipDVDAmt;

}







function checkPhcard(phamt){

		

		blnFound = false;

		newitemlist = null;

		itemstart = 0;

		itemend = 0;

		price=0

		preamt=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);

		

		fulllist=fulllist.split("[")

		for(i=0;i<=fulllist.length-1;i++){

			if(fulllist[i].indexOf("PHO") > 0){

				

				pricestart=fulllist[i].indexOf(",")+1

				priceend=fulllist[i].indexOf("^")

				price=fulllist[i].substring(pricestart,priceend)				

				qtystart=fulllist[i].indexOf("^")+1

				qtyend=fulllist[i].indexOf("#")				

				qty=fulllist[i].substring(qtystart,qtyend)								

				preamt= parseFloat(preamt) + (parseFloat(price) * parseFloat(qty))

				

			}

		}



				if(parseFloat(preamt) + parseFloat(phamt) > parseFloat(50)){

					return true;		

				}

				else{

					return false;		

				}

							

}