var purchased=new Array();
var totalprice=0;
var left = (screen.width/2)-(400);
var top = (screen.height/2)-(400);
/*
$(document).ready(function(){
	
	
	$(".product img").draggable({
	
	containment: 'document',
	opacity: 0.6,
	revert: 'invalid',
	helper: 'clone',
	zIndex: 2000
	
	});

	$("div.content.drop-here").droppable({
	
		drop:
			function(e, ui)
			{
				var param = $(ui.draggable).attr('id');			
				
				var patt1=/balloon/gi;
				var result = param.match(patt1);
				
				if(result == 'balloon')
				{
					var len = param.length;
					len = len - 8;
					param = param.substring(0,len);
					
					addballoontocart(param, null);
				}
				else	
					addtocart(param);
			}
	
	});

});*/


function addtocart(param)
{
	
	var amt = document.getElementById(param+"-qty").value;

	
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcart.php",
		data: 'req=add&id='+encodeURIComponent(param)+'&qty='+encodeURIComponent(amt),
		dataType: 'json',
		beforeSend: function(x)
		{
			$('#ajax-loader').css('visibility','visible');
		},
		success: function(msg)
		{		
			$('#ajax-loader').css('visibility','hidden');
			if(parseInt(msg.status)!=1)
			{
				return false;
			}
			else
			{		
				update_total();
				$('#div_'+msg.id).remove();
				$('#item-list').append(msg.txt);
				
			}
			
		}
		});
}

function remove(id)
{	
	
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcart.php",
		data: 'req=remove&id='+encodeURIComponent(id),
		dataType: 'json',
		success: function(msg)
		{
			if(parseInt(msg.status)!=1)
			{
				return false;
			}
			else
			{
				$('#div_'+id).remove();
				update_total();	
			}
		}
	});	
	
	update_total();	
}



function update_total()
{
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcart.php",
		data: 'req=total',
		dataType: 'json',
		beforeSend: function(x)
		{
			$('#ajax-loader').css('visibility','visible');
			
		},
		success: function(msg)
		{	
			$('#ajax-loader').css('visibility','hidden');
			if(parseInt(msg.status)!=1)
			{
				return false;
			}
			else
			{
				var totalprice=msg.total;
				var totalitems=msg.txt;
						
				if(totalprice)
				{
					totalprice=totalprice.toFixed(2);
					$('#total').html('sub-total: $'+totalprice);
					$('#totalitems').html(totalitems+' item/s in cart');
					$('a.button').css('display','block');
					$('#emptycart').hide();
				
				}
				else
				{
					$('#total').html('');
					$('#totalitems').html('0 items in cart');
					$('a.button').hide();
					$('#emptycart').css('display','inline');
				}
			}
		}
	});
}

function clearcart()
{	

	$('#item-list').empty();

	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcart.php",
		data: 'req=empty',
		dataType: 'json',
		success: function(msg)
		{
			if(parseInt(msg.status)!=1)
			{
				return false;
			}
			else
			{
				update_total();
			}
		}
	});
	
	update_total();
}


function cartcheck()
{	
	
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcart.php",
		data: 'req=load',
		dataType: 'json',
		success: function(msg)
		{
			if(parseInt(msg.status)!=1)
			{
				update_total();
				setpcode();
			}
			else
			{
				$('#item-list').empty();	
				$('#item-list').append(msg.txt);
				update_total();
				setpcode();
			}
		}
	});
	
}


function setpcode()
{	
	var postcode = document.getElementById("postcode").value;
	
	if ((document.getElementById("postcode").value.length==0) || (document.getElementById("postcode").value==null)) 
	{
		$('#welcome').css('display','inline');
	}
	else
	{
		$.ajax({
			type: "POST",
			url: "/ajax/ajaxcart.php",
			data: 'req=postcode&pcode='+encodeURIComponent(postcode),
			dataType: 'json',
			success: function(msg)
			{
				if(parseInt(msg.status)!=1)
				{
					$('#post').empty();
					$('#post').append('Postcode: '+postcode+' <a href="javascript:emptypc();">(change)</a>');
					$('#welcome').hide();
				}
				else
				{
					$('#post').empty();
					$('#post').append('Postcode: '+postcode+' <a href="javascript:emptypc();">(change)</a>');
					$('#welcome').hide();
				}
			}
		});
		
		update_total();	
		
	}
}

function setpcoder()
{	
	var postcode = document.getElementById("postcode").value;
	
	if ((document.getElementById("postcode").value.length==0) || (document.getElementById("postcode").value==null)) 
	{
		$('#welcome').css('display','inline');
	}
	else
	{
		$.ajax({
			type: "POST",
			url: "/ajax/ajaxcart.php",
			data: 'req=postcode&pcode='+encodeURIComponent(postcode),
			dataType: 'json',
			success: function(msg)
			{
				$('#post').empty();
				$('#post').append('Postcode: '+postcode+' <a href="javascript:emptypc();">change</a>');
				$('#welcome').hide();
				$('#redirect').append('Thankyou. Please proceed to the <a href="/checkout.php">checkout</a>');
			}
		});
		
		update_total();	
		
	}
}

function vouchercheck()
{	
	var voucher = document.getElementById("voucher").value;
	
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcheckout.php",
		data: 'req=load&code='+encodeURIComponent(voucher),
		dataType: 'json',
		success: function(msg)
		{
			if(parseInt(msg.status)!=1)
			{
				$('#checkout-items').empty();
				$('#checkout-items').append(msg.txt);
				$('#checkout-items').append('<p>Sorry there has been an error calculating your cart. Please go back to the home page and retry your order.</p>');
			}
			else
			{
				setHeight('checkout-items',msg.lines);
				$('#checkout-items').empty();
				$('#checkout-items').append(msg.txt);
			}
		}
	});
				
}

function emptypc()
{
	document.getElementById('postcode').value=''; 
	setpcode();
}


function loadCheckout()
{
		$.ajax({
		type: "POST",
		url: "/ajax/ajaxcheckout.php",
		data: 'req=load',
		dataType: 'json',
		success: function(msg)
		{
			if(parseInt(msg.status)!=1)
			{
				$('#checkout-items').empty();
				$('#checkout-items').append(msg.txt);
				$('#checkout-items').append('<p>Sorry there has been an error calculating your cart. Please go back to the home page and retry your order.</p>');
			}
			else
			{
				setHeight('checkout-items',msg.lines);
				$('#checkout-items').empty();
				$('#checkout-items').append(msg.txt); 
			}
		}
	});
	
	
}

function setHeight(id, newHeight) 
{
	document.getElementById(id).style.height = newHeight;
	return true;
}

function updateCheckout(param)
{	
	var amt = document.getElementById(param+"-ckqty").value;
	
	$.ajax({
		type: "POST",
		url: "/ajax/ajaxcheckout.php",
		data: 'req=update&id='+encodeURIComponent(param)+'&qty='+encodeURIComponent(amt),
		dataType: 'json',
		success: function(msg)
		{	
			$('#ajax-loader').css('visibility','hidden');
			if(parseInt(msg.status)!=1)
			{
				$('#checkout-items').append('<p>Sorry there has been an error updating your cart. Please go back to the home page and retry your order.</p>');
			}
			else
			{
				loadCheckout();
			}
		}
	});
}

function selectsize(param)  
{  

	var myString = param;
    myArray = myString.split("-");
   
    title =  myArray[0];
    param =  myArray[1]; 

	$.ajax({  
		type: "GET",  
		url: "/ajax/sizes.php",  
		data: 'id='+encodeURIComponent(param),  
		dataType: 'text/html',  
		success: function(html)
		{     
			$('#'+title).css('display','inline');
			$('#'+title).html(html); 		
			$('#availableqty'+title).css('display','none');
		}  
     	
	}); 
}  
