/********************************************************
CreateCookie:
--------------------------------------------------------
To add or modify a cookie
*********************************************************/

function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
	{
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
}

/********************************************************
ReadCookie:
--------------------------------------------------------
To read a cookie value
*********************************************************/

function readCookie(name) 
{
	var nameEQ = name + "=";
	
	//Separate all the cookie entries
	var ca = document.cookie.split(';');
	
	//Read all the cookies
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		//Remove preceding white spaces
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
		
		//Check if the cookie name is the specified name
		//If yes, return the value of cookie
		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length,c.length);
	}
	
	//If no details found, return NULL
	return null;
}

/********************************************************
EraseCookie:
--------------------------------------------------------
To delete a cookie value
*********************************************************/

function eraseCookie(name) 
{
	//Set the cookie expiry to earlier time
	createCookie(name,"",-1);
}

/********************************************************
CheckCookieValue:
--------------------------------------------------------
To check whether any cookie have specified value
*********************************************************/

function checkCookieValue(value)
{
	var cookieRegistered = false;

	//Separate all the cookie entries		
	var ca = document.cookie.split(';');

	//Read all the cookies	
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		//Separate the cookie name and value
		var cookieDetails = c.split('=');

		var cookieValue = "";
		
		if(cookieDetails.length==2)
		{
			cookieValue = cookieDetails[1];
		}
		
		//If cookie value matches with the specified value, return true
		//else check next cookie
		if(cookieValue == value)
		{
			cookieRegistered = true;
			break;
		}
		else
		{
			cookieRegistered = false;
		}
	}
	
	return cookieRegistered;
}

/********************************************************
GetCookieName:
--------------------------------------------------------
To get cookie name for specified value
*********************************************************/

function getCookieName(value)
{
	var cookieName = "";
	//Separate the cookies
	var ca = document.cookie.split(';');
	
	//Read all the cookies
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		//Separate the cookie details
		var cookieDetails = c.split('=');

		var cookieValue = "";
		
		if(cookieDetails.length==2)
		{
			cookieValue = cookieDetails[1];
		}
		
		//If the value of cookie matches with specified value
		//Get the cookie name
		if(cookieValue == value)
		{
			cookieName = cookieDetails[0];
			break;
		}
	}
	
	return cookieName;
}

/********************************************************
AddProduct:
--------------------------------------------------------
To add a product in Shopping Cart
*********************************************************/

function addProduct()
{
	var products = readCookie("ProductsCount");
	var country = readCookie("COUNTRY");
	
	var productsCount = 0;
	
	//Check if Products are the in Shopping Cart or not
	//If no product is there create and start product count with 1
	//else read the current product count
	
	if(products == null)
		createCookie("ProductsCount", productsCount,1);
	else
		productsCount = parseInt(readCookie("ProductsCount"));
	
	//Check if there is any prodcut entry for same product
	//If product is already in shopping cart, increment its quantity and amount accordingly
	//Else add a new product entry in shopping cart

	if(checkCookieValue(document.forms.productInfo.productName.value))
	{
		//As product is in Shoppin Cart, read the cookie name by which the product is saved
		var productCookieName = getCookieName(document.forms.productInfo.productName.value);

		//Get the index of product from cookie name		
		var productCookieParts = productCookieName.split('_');
		
		var productIndex = productsCount;
			
		if(productCookieParts.length==3)
			productIndex = productCookieParts[2];

		var productName = "item_name_" + (productIndex);
		var productType = "type_" + (productIndex);
		var productMinAge = "minAge_" + (productIndex);
		var productMaxAge = "maxAge_" + (productIndex);
		var productQuantity = "quantity_" + (productIndex);
		var productPrice = "item_price_" + (productIndex)
		var productAmount = "amount_" + (productIndex);
		var productUSDPrice = "usd_price_"+ (productIndex);
		
		//Adjust the Product Quantity
		var previousProductQuantity = parseInt(readCookie(productQuantity));
		var newProductQuantity = parseInt(document.forms.productInfo.productQuantity.value) + previousProductQuantity

		//Overwrite the cookie data with new data
		createCookie(productName, document.forms.productInfo.productName.value,1);
		createCookie(productType, document.forms.productInfo.productType.value,1);
		createCookie(productMinAge, document.forms.productInfo.productMinAge.value,1);
		createCookie(productMaxAge, document.forms.productInfo.productMaxAge.value,1);
		createCookie(productQuantity, newProductQuantity,1);

		if(country=="IN")
		{
			createCookie(productPrice, document.forms.productInfo.productPriceINR.value,1);
			createCookie(productUSDPrice, document.forms.productInfo.productPriceINRUSD.value,1);
			createCookie(productAmount, document.forms.productInfo.productPriceINR.value * newProductQuantity,1);
		}
		else
		{
			createCookie(productPrice, document.forms.productInfo.productPriceUSD.value,1);
			createCookie(productUSDPrice, document.forms.productInfo.productPriceUSD.value,1);
			createCookie(productAmount, document.forms.productInfo.productPriceUSD.value * newProductQuantity,1);
		}		
	}
	else
	{
		//As product is not in Shopping cart, create its cookies
		var productName = "item_name_" + (productsCount + 1);
		var productType = "type_" + (productsCount + 1);
		var productMinAge = "minAge_" + (productsCount + 1);
		var productMaxAge = "maxAge_" + (productsCount + 1);
		var productQuantity = "quantity_" + (productsCount + 1);
		var productPrice = "item_price_" + (productsCount + 1)
		var productAmount = "amount_" + (productsCount + 1);
		var productUSDPrice = "usd_price_"+ (productsCount + 1);
		
		createCookie(productName, document.forms.productInfo.productName.value,1);
		createCookie(productType, document.forms.productInfo.productType.value,1);
		createCookie(productMinAge, document.forms.productInfo.productMinAge.value,1);
		createCookie(productMaxAge, document.forms.productInfo.productMaxAge.value,1);		
		createCookie(productQuantity, document.forms.productInfo.productQuantity.value,1);
		
		if(country=="IN")
		{
			createCookie(productPrice, document.forms.productInfo.productPriceINR.value,1);
			createCookie(productUSDPrice, parseFloat(document.forms.productInfo.productPriceINRUSD.value).toFixed(2),1);
			createCookie(productAmount, document.forms.productInfo.productPriceINR.value * document.forms.productInfo.productQuantity.value,1);
		}
		else
		{
			createCookie(productPrice, document.forms.productInfo.productPriceUSD.value,1);
			createCookie(productUSDPrice, document.forms.productInfo.productPriceUSD.value,1);
			createCookie(productAmount, document.forms.productInfo.productPriceUSD.value * document.forms.productInfo.productQuantity.value,1);
		}	

		createCookie("ProductsCount", productsCount + 1, 1);
	}
		
	refreshShoppingCartView("billingnshipping.php", "Proceed To Checkout");
}

/********************************************************
DeleteProduct:
--------------------------------------------------------
To delete a product from Shopping Cart
*********************************************************/

function deleteProduct(index, url, button, functionName)
{
	//Get the count of products
	var productsCount = parseInt(readCookie("ProductsCount"));
	
	//For all subsequent prodcuts in the list, move their index up by 1
	//Delete the last item cookie
	
	//Current Product cookie names
	var previousProductName = "";
	var previousProductType = "";
	var previousProductMinAge = "";
	var previousProductMaxAge = "";
	var previousProductQuantity = "";
	var previousProductPrice = "";
	var previousProductAmount = "";
	var previousProductUSDPrice = "";

	//Next Product cookie names
	var nextProductName = "item_name_" + index;
	var nextProductType = "type_" + index;
	var nextProductMinAge = "minAge_" + index;
	var nextProductMaxAge = "maxAge_" + index;
	var nextProductQuantity = "quantity_" + index;
	var nextProductPrice = "item_price_" + index;
	var nextProductAmount = "amount_" + index;
	var nextProductUSDPrice = "usd_price_" + index;

	for(var i = index; i<productsCount; i++)
	{
		//Current Product cookie names
		previousProductName = "item_name_" + i;
		previousProductType = "type_" + i;
		previousProductMinAge = "minAge_" + i;
		previousProductMaxAge = "maxAge_" + i;
		previousProductQuantity = "quantity_" + i;
		previousProductPrice = "item_price_" + i;
		previousProductAmount = "amount_" + i;
		previousProductUSDPrice = "usd_price_"+ i;

		//Next Product cookie names
		nextProductName = "item_name_" + (i + 1);
		nextProductType = "type_" + (i + 1);
		nextProductMinAge = "minAge_" + (i + 1);
		nextProductMaxAge = "maxAge_" + (i + 1);
		nextProductQuantity = "quantity_" + (i + 1);
		nextProductPrice = "item_price_" + (i + 1);
		nextProductAmount = "amount_" + (i + 1);
		nextProductUSDPrice = "usd_price_"+ (i + 1);

		createCookie(previousProductName, readCookie(nextProductName),1);
		createCookie(previousProductType, readCookie(nextProductType),1);
		createCookie(previousProductMinAge, readCookie(nextProductMinAge),1);
		createCookie(previousProductMaxAge, readCookie(nextProductMaxAge),1);
		createCookie(previousProductQuantity, readCookie(nextProductQuantity),1);
		createCookie(previousProductPrice, readCookie(nextProductPrice),1);
		createCookie(previousProductAmount, readCookie(nextProductAmount),1);
		createCookie(previousProductUSDPrice, readCookie(nextProductUSDPrice),1);
	}
	
	eraseCookie(nextProductName);
	eraseCookie(nextProductType);
	eraseCookie(nextProductMinAge);
	eraseCookie(nextProductMaxAge);
	eraseCookie(nextProductQuantity);
	eraseCookie(nextProductPrice);
	eraseCookie(nextProductAmount);
	eraseCookie(nextProductUSDPrice);
	
	createCookie("ProductsCount", productsCount - 1, 1);
	
	refreshShoppingCartView(url, button, functionName);
}

/********************************************************
AddGiftProduct:
--------------------------------------------------------
To add a gift product in Shopping Cart
*********************************************************/

function addGiftProduct(formId)
{
	document.forms.productInfo.productName.value = document.getElementById(formId).productName.value;
	document.forms.productInfo.productType.value = document.getElementById(formId).productType.value;
	document.forms.productInfo.productMinAge.value = document.getElementById(formId).productMinAge.value;
	document.forms.productInfo.productMaxAge.value = document.getElementById(formId).productMaxAge.value;
	document.forms.productInfo.productQuantity.value = document.getElementById(formId).productQuantity.value;
	document.forms.productInfo.productPriceINR.value = document.getElementById(formId).productPriceINR.value;
	document.forms.productInfo.productPriceINRUSD.value = parseFloat(document.getElementById(formId).productPriceINRUSD.value).toFixed(2);
	document.forms.productInfo.productPriceUSD.value = document.getElementById(formId).productPriceUSD.value;
		
	addProduct();
	
	productsCount = parseInt(readCookie("ProductsCount"));
	
	if(readCookie("BillingName") == "" || readCookie("BillingName") == null)
	{
		refreshShoppingCartView("billingnshipping.php", "Continue");
	}
	else
	{
		refreshShoppingCartView("orderconfirm.php", "Continue");		
	}
}

/********************************************************
ChangeQuantity:
--------------------------------------------------------
To change the quantity of a product in Shopping Cart
*********************************************************/

function changeQuantity(index, id, url, button, functionName)
{
	//Next Product cookie names
	var productName = "item_name_" + index;
	var productType = "type_" + index;
	var productMinAge = "minAge_" + index;
	var productMaxAge = "maxAge_" + index;
	var productQuantity = "quantity_" + index;
	var productPrice = "item_price_" + index;
	var productAmount = "amount_" + index;
	var productUSDPrice = "usd_price_" + index;

	var quantity = parseInt(document.getElementById(id).value);
	
	if(quantity == 0 || isNaN(quantity))
	{
		eraseCookie(productName);
		eraseCookie(productType);
		eraseCookie(productMinAge);
		eraseCookie(productMaxAge);
		eraseCookie(productQuantity);
		eraseCookie(productPrice);
		eraseCookie(productAmount);
		eraseCookie(productUSDPrice);	
		
		//Get the count of products
		var productsCount = parseInt(readCookie("ProductsCount"));
			
		createCookie("ProductsCount", productsCount - 1, 1);
	}
	else
	{
		var country = readCookie("COUNTRY");
		var type = readCookie(productType);
	
		var inrPrice = parseFloat(readCookie(productPrice));
		var usdPrice = parseFloat(readCookie(productUSDPrice));
		var previousQuantity = parseInt(readCookie(productQuantity));
		
		if(country == "IN" && type=="Comic")
		{
			var additionalComicPrice = 500;
			var currencyRatio = parseFloat(document.getElementById("currencyINRTOUSD").value).toFixed(2);
			
			inrPrice = inrPrice + ((quantity - 1) * additionalComicPrice);
			usdPrice = ((usdPrice * previousQuantity) + ((quantity - previousQuantity)*(additionalComicPrice/currencyRatio))) / quantity;
		}
		else
		{
			inrPrice = inrPrice * quantity;
			usdPrice = usdPrice;
		}
		
		createCookie(productQuantity, quantity,1);
		createCookie(productAmount, parseFloat(inrPrice),1);
		createCookie(productUSDPrice, parseFloat(usdPrice),1);
	}		
	
	refreshShoppingCartView(url, button, functionName);
}

/********************************************************
RefreshShoppingCartView:
--------------------------------------------------------
To update the view of shopping cart shown to the user
*********************************************************/

function refreshShoppingCartView()
{	
	var url = "";
	var button = "";
	var functionName = "";
	//Check for the arguments and set default values
	if(arguments.length == 1)
	{
		url = arguments[0];
		button = "Continue";
		functionName = "";
	}
	else if(arguments.length == 2)
	{
		url = arguments[0];
		button = arguments [1];
		functionName = "";
	}
	else if(arguments.length == 3)
	{
		url = arguments[0];
		button = arguments [1];
		functionName = arguments[2];
	}
	else
	{
		url = "https://www.paypal.com/cgi-bin/webscr";
		//url = "https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/1234567890"
		//url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
		button = "Confirm";
		functionName = "";
	}
	
	//Get the current number of products in cart
	var productsCount = parseInt(readCookie("ProductsCount"));

	var shoppingCartContent = "";
	
	//If there are products in Shopping Cart then show the prodcuts in table structure
	//Else show the cart empty message
	if(productsCount > 0)
	{
		var totalAmount = 0;
		
		var submitFunction = "";
		if(functionName != "")
			submitFunction = 'onSubmit=" return ' + functionName + '()"';
			
		shoppingCartContent += '<form action="' + url + '" method="post" ' + submitFunction + '">';
		
		shoppingCartContent += '<input type="hidden" name="cmd" value="_cart">';
		shoppingCartContent += '<input type="hidden" name="upload" value="1">';
		shoppingCartContent += '<input type="hidden" name="business" value="orders@arkincomics.com">';

		shoppingCartContent += '<input type="hidden" name="cancel_return" value="http://' + document.domain + '/paymentfailed.php">';
		shoppingCartContent += '<input type="hidden" name="return" value="http://' + document.domain + '/imagesUpload.php">';
		shoppingCartContent += '<input type="hidden" name="image_url" value="http://www.arkincomics.com/images/arkin_customcomics_logo.gif">';
		shoppingCartContent += '<input type="hidden" name="cpp_header_image" value="http://www.arkincomics.com/images/arkin_customcomics_logo.gif">';
		shoppingCartContent += '<input type="hidden" name="cs" value="1">';
		shoppingCartContent += '<input type="hidden" name="rm" value="2">';
		//shoppingCartContent += '<input type="hidden" name="no_shipping" value="0">';

		shoppingCartContent += '<table border=0 cellpadding=10 cellspacing=0 width=100%>';
		shoppingCartContent += '<tr>';
		shoppingCartContent += '<td style="border-bottom: Solid 1px #808080;"><b>Product</b></td>';
		shoppingCartContent += '<td style="border-bottom: Solid 1px #808080;"><b>Qty</b></td>';
		shoppingCartContent += '<td style="border-bottom: Solid 1px #808080;"><b>Price</b></td>';
		shoppingCartContent += '<td style="border-bottom: Solid 1px #808080;"><b>Amount</b></td>';
		shoppingCartContent += '<td style="border-bottom: Solid 1px #808080;"><b>&nbsp;</b></td>';
		shoppingCartContent += '</tr>';

		//Show each product in specified row
		for(var i=1; i<=productsCount; i++)
		{
			var itemName = 'item_name_' + i;
			var itemQuantity = 'quantity_' + i;
			var itemPrice = 'item_price_' + i;
			var itemAmount = 'amount_' + i;
			var itemUSDPrice = 'usd_price_' + i;
			
			var name = readCookie(itemName);
			var quantity = readCookie(itemQuantity);
			var price = readCookie(itemPrice);
			var amount = parseFloat(readCookie(itemAmount));
			var usdPrice = parseFloat(readCookie(itemUSDPrice)).toFixed(2);

			var country = readCookie("COUNTRY");
			if(country == "IN")
				currencyUnit = "Rs";
			else
				currencyUnit = "$";
				
			shoppingCartContent += '<input type="hidden" name="' + itemName + '" value="' + name + '">';
			shoppingCartContent += '<input type="hidden" name="' + itemQuantity + '" value="' + quantity + '">';
			shoppingCartContent += '<input type="hidden" name="' + itemAmount + '" value="' + usdPrice + '">';

			shoppingCartContent += '<tr>';
			shoppingCartContent += '<td>' + name + '</td>';
			shoppingCartContent += '<td><input type="text" size="1" name="' +itemQuantity+ 'Shown" id="' +itemQuantity+ 'Shown" value="' + quantity + '" onchange="changeQuantity(' +i+ ', \'' +itemQuantity+ 'Shown\', \'' +url+ '\', \'' +button+ '\', \'' +functionName+ '\')"/></td>';
			shoppingCartContent += '<td>' + currencyUnit + ' ' + price + '</td>';
			shoppingCartContent += '<td>' + currencyUnit + ' ' + amount + '</td>';
			//shoppingCartContent += '<td><input type="Button" value="X" onclick="deleteProduct(' +i+ ', \'' +url+ '\', \'' +button+ '\', \'' +functionName+ '\')" /></td>';
			shoppingCartContent += '<td><input type="Image" src="images/Delete.gif" value="X" onclick="deleteProduct(' +i+ ', \'' +url+ '\', \'' +button+ '\', \'' +functionName+ '\')" /></td>';
			shoppingCartContent += '</tr>';
									
			totalAmount += amount;
		}

		shoppingCartContent += '<tr>';
		shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
		shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
		shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>Total</b></td>';
		shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>' + currencyUnit + ' '+ parseFloat(totalAmount).toFixed(2) + '</b></td>';
		shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
		shoppingCartContent += '</tr>';
		
		if(button == "Confirm")
		{
			var tax = parseFloat(document.getElementById("tax").value)/100;
			var taxAmount = 0;
			var usdTaxAmount = 0;
			
			if(country == "IN")
			{
				taxAmount = totalAmount * tax;
				usdTaxAmount = taxAmount / parseFloat(document.getElementById("shippingINR").value);;
			}
			else
			{
				taxAmount = totalAmount * tax;
				usdTaxAmount = taxAmount;
			}
			
			shoppingCartContent += '<tr>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>Tax ' +document.getElementById("tax").value+ '%</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>' + currencyUnit + ' '+ parseFloat(taxAmount).toFixed(2) + '</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '</tr>';
			shoppingCartContent += '<input type="hidden" name="tax_cart" value="' + parseFloat(usdTaxAmount).toFixed(2) + '">';
			createCookie("Tax",parseFloat(usdTaxAmount).toFixed(2),1);
			
			var shippingAmount = 0;
			var usdShippingAmount = 0;
			
			if(country == "IN")
			{
				shippingAmount = parseFloat(document.getElementById("shippingINR").value);
				usdShippingAmount = shippingAmount / parseFloat(document.getElementById("currencyINRTOUSD").value);
			}
			else
			{
				shippingAmount = parseFloat(document.getElementById("shippingUSD").value);
				usdShippingAmount = shippingAmount;
			}
			createCookie("Shipping",parseFloat(usdShippingAmount).toFixed(2),1);
			
			shoppingCartContent += '<tr>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>Shipping Amount</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>' + currencyUnit + ' '+ parseFloat(shippingAmount).toFixed(2) + '</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '</tr>';
			shoppingCartContent += '<input type="hidden" name="handling_cart" value="' + parseFloat(usdShippingAmount).toFixed(2) + '">';

			var usdTotalAmount = 0;
			var usdGrandTotal = 0;
			var grandTotal = totalAmount + taxAmount + shippingAmount;
			
			if(country == "IN")
			{
				usdTotalAmount = totalAmount / parseFloat(document.getElementById("currencyINRTOUSD").value);
				usdGrandTotal = grandTotal / parseFloat(document.getElementById("currencyINRTOUSD").value);
			}
			else
			{
				usdTotalAmount = totalAmount;
				usdGrandTotal = grandTotal;
			}		

			shoppingCartContent += '<tr>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>Grand Total</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;"><b>' + currencyUnit + ' '+ parseFloat(grandTotal).toFixed(2) + '</b></td>';
			shoppingCartContent += '<td style="border-top: Solid 1px #808080;">&nbsp;</td>';
			shoppingCartContent += '</tr>';
			
			createCookie("TotalAmount",parseFloat(usdTotalAmount).toFixed(2),1);
			createCookie("GrandTotal",parseFloat(usdGrandTotal).toFixed(2),1);

			if(checkAge())
			{
				shoppingCartContent += '</table>';		
				shoppingCartContent += '<input type="image" src="images/' +button+ '.gif" value="' + button + '">';
				shoppingCartContent += '</form>';
				shoppingCartContent += '<br/>';
			}
		}
		else
		{
			shoppingCartContent += '</table>';		
			shoppingCartContent += '<input type="image" src="images/' +button+ '.gif" value="' + button + '">';
			shoppingCartContent += '</form>';
			shoppingCartContent += '<br/>';
		}
		
		if(country == "IN")
			shoppingCartContent += 'Buy another copy of comic for just Rs. 500';

	}
	else
	{		
		shoppingCartContent += '<table border=0 cellpadding=10 cellspacing=0 width=100%>';
		shoppingCartContent += '<tr>';
		shoppingCartContent += '<td>Your shopping cart is empty</td>';
		shoppingCartContent += '</tr>';
		shoppingCartContent += '</table>';
	}		
	
	document.getElementById("shoppingCartView").innerHTML = shoppingCartContent;
}

/********************************************************
sameBillingShippingAddress:
--------------------------------------------------------
To make billing and shipping addresses same
*********************************************************/

function sameBillingShippingAddress()
{
	if(document.getElementById("sameBillingShippingAddress").checked)
	{
		document.getElementById("Shipping_Name").value = document.getElementById("Billing_Name").value;
		document.getElementById("Shipping_Email").value = document.getElementById("Billing_Email").value;
		document.getElementById("Shipping_Address").value = document.getElementById("Billing_Address").value;
		document.getElementById("Shipping_Phone_Country").value = document.getElementById("Billing_Phone_Country").value;
		document.getElementById("Shipping_Phone_LocalCode").value = document.getElementById("Billing_Phone_LocalCode").value;
		document.getElementById("Shipping_Phone").value = document.getElementById("Billing_Phone").value;
		
		document.getElementById("Shipping_Name").disabled = true;
		document.getElementById("Shipping_Email").disabled = true;
		document.getElementById("Shipping_Address").disabled = true;
		document.getElementById("Shipping_Phone_Country").disabled = true;
		document.getElementById("Shipping_Phone_LocalCode").disabled = true;
		document.getElementById("Shipping_Phone").disabled = true;
	}
	else
	{
		document.getElementById("Shipping_Name").value = "";
		document.getElementById("Shipping_Email").value = "";
		document.getElementById("Shipping_Address").value = "";
		document.getElementById("Shipping_Phone_Country").value = "";
		document.getElementById("Shipping_Phone_LocalCode").value = "";
		document.getElementById("Shipping_Phone").value = "";
		
		document.getElementById("Shipping_Name").disabled = false;
		document.getElementById("Shipping_Email").disabled = false;
		document.getElementById("Shipping_Address").disabled = false;
		document.getElementById("Shipping_Phone_Country").disabled = false;
		document.getElementById("Shipping_Phone_LocalCode").disabled = false;
		document.getElementById("Shipping_Phone").disabled = false;		
	}
}

/********************************************************
validateBillingNShippingInformation:
--------------------------------------------------------
To validate billing and shipping information before proceeding
*********************************************************/

function validateBillingNShippingInformation()
{
	var errors=0;
	var msg ="";

	if(document.getElementById("Billing_Name").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing name";
		errors++;
	}

	if(document.getElementById("Billing_Email").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing email";
		errors++;
	}
	else
	{
		var emailFilter=/^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
		if (!(emailFilter.test(document.getElementById("Billing_Email").value)))
		{
			if(errors>0)
			{
				msg+= ", ";
			}

			msg += "valid billing email id ";
			errors++;
		}
	}

	if(document.getElementById("Billing_Address").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing address";
		errors++;
	}

	if(document.getElementById("Billing_Phone_Country").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing phone country code";
		errors++;
	}

	if(document.getElementById("Billing_Phone_LocalCode").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing phone local code";
		errors++;
	}

	if(document.getElementById("Billing_Phone").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "billing phone number";
		errors++;
	}

	if(document.getElementById("Shipping_Name").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping name";
		errors++;
	}

	if(document.getElementById("Shipping_Email").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping email";
		errors++;
	}
	else
	{
		var emailFilter=/^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
		if (!(emailFilter.test(document.getElementById("Shipping_Email").value)))
		{
			if(errors>0)
			{
				msg+= ", ";
			}

			msg += "valid shipping email id ";
			errors++;
		}
	}

	if(document.getElementById("Shipping_Address").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping address";
		errors++;
	}

	if(document.getElementById("Shipping_Phone_Country").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping phone country code";
		errors++;
	}

	if(document.getElementById("Shipping_Phone_LocalCode").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping phone local code";
		errors++;
	}

	if(document.getElementById("Shipping_Phone").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "shipping phone number";
		errors++;
	}

	if(document.getElementById("Superhero_Name").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "superhero name";
		errors++;
	}

	if(document.getElementById("Superhero_Age").value=="")
	{
		if(errors>0)
		{
			msg+= ", "
		}
		msg += "superhero age";
		errors++;
	}

	if(errors>0)
	{
		alert("You must enter information for: " +msg);
		return false;
	}
	else
	{
		var billAddress = document.getElementById("Billing_Address").value;
		billAddress = billAddress.replace(/\n/g,",");

		var shipAddress = document.getElementById("Shipping_Address").value;
		shipAddress = shipAddress.replace(/\n/g,",");
				
		createCookie("BillingName", document.getElementById("Billing_Name").value,1);
		createCookie("BillingEmail", document.getElementById("Billing_Email").value,1);
		createCookie("BillingAddress", billAddress,1);
		createCookie("BillingPhoneCountry", document.getElementById("Billing_Phone_Country").value,1);
		createCookie("BillingPhoneLocalCode", document.getElementById("Billing_Phone_LocalCode").value,1);
		createCookie("BillingPhone", document.getElementById("Billing_Phone").value,1);

		createCookie("ShippingName", document.getElementById("Shipping_Name").value,1);
		createCookie("ShippingEmail", document.getElementById("Shipping_Email").value,1);
		createCookie("ShippingAddress", shipAddress,1);
		createCookie("ShippingPhoneCountry", document.getElementById("Shipping_Phone_Country").value,1);
		createCookie("ShippingPhoneLocalCode", document.getElementById("Shipping_Phone_LocalCode").value,1);
		createCookie("ShippingPhone", document.getElementById("Shipping_Phone").value,1);

		createCookie("SuperheroName", document.getElementById("Superhero_Name").value,1);
		createCookie("SuperheroAge", document.getElementById("Superhero_Age").value,1);

		//Check if any Custom Tshirt Already Purchased
		var isTshirtPurchased = 0;
		var productsCount = parseInt(readCookie("ProductsCount"));

		for(var i = 1; i<=productsCount; i++)
		{
			//Current Product cookie type
			var productType = "type_" + i;
			
			if(readCookie(productType) == "Tshirt")
			{
				isTshirtPurchased = 1;
				break;
			}
		}
	
		//document.location.href = "giftoptions.php";
		if(isTshirtPurchased==1)
		{
			document.location.href = "orderconfirm.php";
		}
		else
		{
			document.location.href = "tshirt.php";
		}
		return true;
	}
}

/********************************************************
clearCookies:
--------------------------------------------------------
To clear shopping cart details
*********************************************************/

function clearCookies()
{
	//Separate all the cookie entries		
	var ca = document.cookie.split(';');
		
	//Read all the cookies
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		//Separate the cookie details
		var cookieDetails = c.split('=');

		var cookieValue = "";
		
		if(cookieDetails.length==2)
		{
			cookieName = cookieDetails[0];
			eraseCookie(cookieName);
		}
	}
}

/********************************************************
changePaymentOption:
--------------------------------------------------------
To change payment options
*********************************************************/

function changePaymentOption(paymentOption)
{
	createCookie('PaymentOption',paymentOption,1);
	
	switch(paymentOption)
	{
		case 'Paypal':
			refreshShoppingCartView();
			break;
			
		case 'DemandDraft':
			refreshShoppingCartView("imagesUpload.php","Confirm");
			break;

		case 'Cheque':
			refreshShoppingCartView("imagesUpload.php","Confirm");
			break;
			
		default:
			refreshShoppingCartView();
			break;
	}
}

/********************************************************
CheckAge
--------------------------------------------------------
To check the age for each product with customer age
*********************************************************/

function checkAge()
{
	var productsCount = parseInt(readCookie("ProductsCount"));
	var age = parseInt(readCookie("SuperheroAge"));
	
	var message = "";
	var error = 0;
	
	for(var i = 1; i<=productsCount; i++)
	{
		var productName = "item_name_" + i;
		var productMinAge = "minAge_" + i;
		var productMaxAge = "maxAge_" + i;
		
		var minAge = parseInt(readCookie(productMinAge));
		var maxAge = parseInt(readCookie(productMaxAge));
		
		if(age <= minAge || age >= maxAge)
		{
			error = error + 1;
			message += "Oops, you are pretty little for appearing in " + readCookie(productName) + ", Kindly choose another comic book  \n";
		}
	}
	
	if(error > 0)
	{
		alert(message);
		return false;
	}
	else
	{
		return true;
	}
}