// JavaScript Document

//Start Shopping Cart Functions
	function addtocart(id,p_name,price,qty,d_qty,d_price)
	{	
	
		if(parseInt(document.getElementById(qty).value) > 0)
		{
			
			var conmsg = "Add "+parseInt(document.getElementById(qty).value)+" to Basket?\n\r"+p_name+"\n\r";
		
			if (parseInt(d_qty) > 0) {
				if (parseInt(document.getElementById(qty).value) >= parseInt(d_qty)) {
					var calc=parseInt(document.getElementById(qty).value)*parseFloat(d_price);
					conmsg = conmsg+" Unit Price: "+(parseFloat(d_price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2))+"\n\r(Discounted Price)";
					if(confirm(conmsg))
						addtocookie(id,parseInt(document.getElementById(qty).value));
				} else {
					var calc=parseInt(document.getElementById(qty).value)*parseFloat(price);
					conmsg = conmsg+" Unit Price: "+(parseFloat(price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2));
					if(confirm(conmsg))
						addtocookie(id,parseInt(document.getElementById(qty).value));
				}
			} else {
				var calc=parseInt(document.getElementById(qty).value)*parseFloat(price);
				conmsg = conmsg+" Unit Price: "+(parseFloat(price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2));
				if(confirm(conmsg))
					addtocookie(id,parseInt(document.getElementById(qty).value));
			}
		} else {
			alert("You have not entered a valid quantity");
		}
	}
	
	function addpromotocart(id,p_name,price,qty,d_qty,d_price,d_type)
	{	
	
		if(parseInt(document.getElementById(qty).value) > 0)
		{
			
			var conmsg = "Add "+parseInt(document.getElementById(qty).value)+" to Basket?\n\r"+p_name+"\n\r";
		
			if (parseInt(d_qty) > 0) {
				if (parseInt(document.getElementById(qty).value) >= parseInt(d_qty)) {
					if(d_type == 'D')
					{
						var calc=parseInt(document.getElementById(qty).value)*parseFloat(d_price);
						conmsg = conmsg+" Unit Price: "+(parseFloat(d_price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2))+"\n\r(Discounted Price)";
						if(confirm(conmsg))
							addtocookie(id,parseInt(document.getElementById(qty).value));

					} else if(d_type == 'F') {
						var calc=parseInt(document.getElementById(qty).value)*parseFloat(price);
						var freebtl = parseInt(document.getElementById(qty).value)/parseInt(d_qty);
						conmsg = conmsg+" Unit Price: "+(parseFloat(price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2))+"\n\r(" +  freebtl + " Free Bottle(s))";
						if(confirm(conmsg))
							addtocookie(id,parseInt(document.getElementById(qty).value));
					}
				} else {
					var calc=parseInt(document.getElementById(qty).value)*parseFloat(price);
					conmsg = conmsg+" Unit Price: "+(parseFloat(price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2));
					if(confirm(conmsg))
						addtocookie(id,parseInt(document.getElementById(qty).value));
				}
			} else {
				var calc=parseInt(document.getElementById(qty).value)*parseFloat(price);
				conmsg = conmsg+" Unit Price: "+(parseFloat(price).toFixed(2))+"\n\r Sub-Total: "+(parseFloat(calc).toFixed(2));
				if(confirm(conmsg))
					addtocookie(id,parseInt(document.getElementById(qty).value));
			}
		} else {
			alert("You have not entered a valid quantity");
		}
	}
	
	function addtocookie(id,qty)
	{
		if(GetCookie("WineBasket"))
		{
			var en = new Array();
			var BarItem = new Array();
			var strCookie = GetCookie("WineBasket");
			en = strCookie.split("::");
			var c = en.length;
			var q = 0;
			
			while (q < c) {
				BarItem[en[q]] = en[q+1];
				q += 2;
			}
			
			if (id) {
				if (parseInt(qty)) {
					if (BarItem[id]) {
						newqty = parseInt(BarItem[id]) + parseInt(qty);
					} else {
						newqty = parseInt(qty);
					}
					BarItem[id] = newqty;
					BarBasket = "0::0";
					for(var key in BarItem) {
//						if (key > 0) {
						if (key) {
							BarBasket = BarBasket+"::"+key+"::"+BarItem[key];
						}
					}
					SetCookie("WineBasket", BarBasket,30);
					window.location.reload(true);
				}
			}
		} else {
			SetCookie("WineBasket","0::0::"+id+"::"+qty,30);
			window.location.reload(true);
		}
	}
	
	function GetCookie(cookieName) {
	 var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
	 if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length;
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	}
	
	function SetCookie(cookieName,cookieValue,nDays) {
	 var today = new Date();
	 var expire = new Date();
	 if (nDays==null || nDays==0) nDays=1;
	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = cookieName+"="+escape(cookieValue)
					 + ";expires="+expire.toGMTString();
	}
//End Shopping Cart Functions

//Start Validation Function
function IsNumeric(passedVal){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	var newpassedVal;
	newpassedVal = document.getElementById(passedVal);
	newpassedVal = newpassedVal.value;
	if(newpassedVal != "")
	{
		if(parseInt(newpassedVal) != "NaN")
		{
			for (i = 0; i < newpassedVal.length && IsNumber == true; i++)
			{
				Char = newpassedVal.charAt(i);
				if (ValidChars.indexOf(Char).toString() == -1)
				{	
					IsNumber = false;
				}
			}
		} else { IsNumber = false; }
	} else { IsNumber = false; }
					
	if(IsNumber == true)
	{
		modify(document.mod);
	} else {
		alert("Please Enter a Valid Quantity.");
	}
}
//End Validation Function

//Checkout Redirect
function reDirect() 
{ 
	location.replace("./?p=deliverydetail"); 
}
function viewcart() {
		location.href = "./?p=cart";
}
