var loader = new Image();
loader.src = "images/loader.gif";
// Cart Functions
function CartLoading()
{
	var obj = document.getElementById('cart_loading');
	obj.innerHTML = "<img src='images/loader.gif'>"
}

function CartComplete(statusText, responseText, responseXML)
{

	var obj = document.getElementById('cart_loading');
	obj.innerHTML = "&nbsp;";
	
	var obj = document.getElementById('cart');
	obj.innerHTML = responseText;

}

// Recieving Methods Functions
function RemLoading()
{
	
	var obj = document.getElementById('rem_loading');
	obj.innerHTML = "<img src='images/loader.gif'>"

}

function RemComplete(statusText, responseText, responseXML)
{
	
	var obj = document.getElementById('Rem');
	obj.innerHTML = responseText;

}

function DeliveryLoading()
{
	
	var obj = document.getElementById('delivery_loading');
	obj.innerHTML = "<img src='images/loader.gif'>"

}

function DeliveryComplete(statusText, responseText)
{
	
	var obj = document.getElementById('delivery_body');
	obj.innerHTML = responseText;

}

var CartAsync = new Asynchronous();
CartAsync.Loading = CartLoading;
CartAsync.Complete = CartComplete;

var RemAsync = new Asynchronous();
RemAsync.Loading = RemLoading;
RemAsync.Complete = RemComplete;

var DeliveryAsync = new Asynchronous();
DeliveryAsync.Loading = DeliveryLoading;
DeliveryAsync.Complete = DeliveryComplete;


var CouponAsync = new Asynchronous();
CouponAsync.Loading = CartLoading;
CouponAsync.Complete = CouponComplete;


function CouponComplete(statusText, responseText)
{
	window.location.reload();
}


function GetFormValues(FormObject)
{
	var str = "";
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < FormObject.elements.length; i++)
	{
		str += FormObject.elements[i].name + "=" + escape(FormObject.elements[i].value) + "&";
	}
	//Then return the string values.
	return str;
}

function DeleteItem(id)
{
	CartAsync.Call("async.php?action=cart&del="+id+"&time="+GetTimestamp());	
}

function Coupon(no)
{
	if(document.getElementById('coupons').value.replace(/^\s*|\s*$/g,'') == '') {
		alert('Please enter a coupon code.');
	} else if(document.getElementById('coupons').value.replace(/^\s*|\s*$/g,'') != '') {
		
		$.post('async.php?action=coupon_check', 
		{ 
		  time:GetTimestamp(), 
		  coupon_code:document.getElementById('coupons').value.replace(/^\s*|\s*$/g,''),
		  action: 'coupon_check' 
		},
		function(data) {
			if($.trim(data) == 'coupon_expired')
			{
				alert('Coupon code is expired.');
			} 
			else if($.trim(data) == 'not_found')
			{
				alert('Coupon code is invalid!');
			}
			else { 
				CouponAsync.Call("async.php?action=cart&coupon="+no+"&time="+GetTimestamp());
			}
		});
	}
}

function UpdateCart(Form)
{
	var str = GetFormValues(Form);

	CartAsync.PostCall("async.php?action=cart&time="+GetTimestamp(), str);
	
	return false;	
}

function RefreshCart()
{
	CartAsync.Call("async.php?action=cart&time="+GetTimestamp());	
}

function RecievingMethod(code)
{
	RemAsync.Call("async.php?action=rem&rm="+code+"&time="+GetTimestamp());
}

function DeliveryType(code)
{
	DeliveryAsync.Call("async.php?action=delivery&dtype="+code+"&time="+GetTimestamp());
}


function ZipCode(code)
{
	CartAsync.Call("async.php?action=cart&zip="+code+"&time="+GetTimestamp());
}

// Fills the hidden value 
function PickupDate(dd,mm,yy)
{
	var obj = document.getElementById('pdate');
	obj.innerHTML = dd+" "+mm+" "+yy; 
	
	var i=1;
	
	document.PDForm.date.value = dd+","+mm+","+yy;
}

// Validate the pickup form
function ValidatePickupForm(Form)
{
	var count = 0;
	
	if(Form.date.value == 'null')
	{
		alert('Please select a Date!');
		count++;
	}
	
	else if(Form.DateTime.value == 0)
	{
		alert('Please specify the Time!');
		count++;
	}
	
	
	if(count > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// Validate the pickup form
function ValidateDeliveryForm(Form)
{
	var count = 0;
	
	if(Form.date.value == 'null')
	{
		alert('Please select a Date!');
		count++;
	}
	
	else if(Form.DateTime.value == 0)
	{
		alert('Please specify the Time!');
		count++;
	}
	
	else if(Form.ZipCodes.value == 0)
	{
		alert('Please specify Zip code!');
		count++;
	}

	if(count > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function GetTimestamp()
{
	var date = new Date();
	var timestamp = date.getTime();
	return timestamp;
}
