function load(div,url)
{
	ajax_loadContent(div,url);
}

function esc(val)
{
	val = val.replace("'","\'");
	val = val.replace('"',"&quot;");
	return val;
}

function hide(div)
{
	document.getElementById(div).style.display='none';
}

function show(div)
{
	document.getElementById(div).style.display='';
}

function reset(div)
{
	document.getElementById(div).innerHTML='';
}

function getvalue(div)
{
	return document.getElementById(div).value;
}

function setvalue(div, value)
{
	document.getElementById(div).value=value;
}

function gethtml(div)
{
	return document.getElementById(div).innerHTML;
}

function sethtml(div, value)
{
	document.getElementById(div).innerHTML=value;
}

function setsrc(div, value)
{
	document.getElementById(div).src=value;
}

//SHOPPING PART
function check_quantity(product_code, allow_zero)
{
	var qty_element = document.getElementById('quantity_'+product_code);
	var issue_element = document.getElementById('issue_'+product_code);
	var check_stock_element = document.getElementById('check_stock_'+product_code);
	var stock_element = document.getElementById('stock_'+product_code);
	
	var qty = qty_element.value;
	if (qty == '' || isNaN(qty))
	{
		alert ('Quantity can only be in Number Format');
		qty_element.value=0;
		qty_element.focus();
		return false;
	}
	
	var issue = issue_element.value;
	
	if (allow_zero)
	{
	} else
	{
		if (qty==0)
		{
			alert('The quantity has to be more than zero');
			qty_element.focus();
			return false;
		}
	}
	
	var mod = qty%issue;
	if (mod==0)
	{
	} else
	{
		alert('The quantity has to be a multiple of the issue quantity: '+issue);
		qty_element.focus();
		return false;
	}
	
	var check_stock = check_stock_element.value;

	var cur_stock = stock_element.value;
	
	if (parseInt(qty) > parseInt(cur_stock))
	{
		if (check_stock==1)
		{
			alert('We don\'t have enough stock for your desired quantity. Currently there '+(cur_stock==1?'is':'are')+' '+cur_stock+' available.');
			qty_element.focus();
			return false;
		} else
		{
			return confirm('We don\'t have enough stock for your desired quantity. Currently there '+(cur_stock==1?'is':'are')+' '+cur_stock+' available. This item '+product_code+' will be in backorder. Click OK to continue or Cancel to cancel.');
		}
	}
	return true;
}

function add_to_basket(product_code)
{
	if (check_quantity(product_code))
	{
		var qty = getvalue('quantity_'+product_code);
		var price = getvalue('price_'+product_code);
		var issue = getvalue('issue_'+product_code);
	    addToBasket(product_code, qty, price);
		//setvalue('quantity_'+product_code,issue);
	} 
}

function addToBasket(product_code, qty, price)
{  
	var productX 		= $("#image_" + product_code).offset().left;
	var productY 		= $("#image_" + product_code).offset().top;
	
	var basketX 		= $("#shop_cart").offset().left;
	var basketY 		= $("#shop_cart").offset().top;
	
	var gotoX 			= basketX - productX;
	var gotoY 			= basketY - productY;
	
	var newImageWidth 	= $("#image_" + product_code).width() / 3;
	var newImageHeight	= $("#image_" + product_code).height() / 3;

	var the_image = $("#image_" + product_code + " img");
	var the_src = the_image.attr("src");
	
	//the_image.hide();
	the_image.clone()
	.prependTo("#image_" + product_code)
	.css({'position' : 'absolute'})
	.animate({opacity: 0.4}, 0 )
	.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() 
	{
		$(this).remove();
		$.ajax({  
			type: "GET",  
			url: "./ajax.php",
			data: { file:"cart", c: product_code, q: qty, p: price},
			success: function(theResponse) 
			{
				$("#breadcrumb_right").html(theResponse);
				//$("#image_" + product_code + " img").show();
			}  
		});
	});
}

function empty_cart(md5)
{
	load('breadcrumb_right', '/ajax.php?file=cart&rem=1&md5='+md5);
	sethtml('errormessage','There are no items in your shopping cart at the moment.');
	show('errormessage');
	hide('all_cart');
}

function check_cart(products, delivery)
{
	var all_prod = products.split(",");
	for (var i=0; i<all_prod.length; i++)
	{
		var check_result = check_quantity(all_prod[i], 1);
		if (check_result==false)
			return false;
	}
	
	if (delivery==true)
	{
		return confirm('Your order is below our Minimum Order Value.\r\nA delivery charge of $25 will be applied to this order.\r\nClick OK to continue or Cancel to return to your Cart.');
	}
	return true;
}

function update_cart(products)
{
	if (check_cart(products))
	{
		document.forms["cart_form"]["single_product"].value = '';
		warn_onunload=false;
		document.forms["cart_form"].submit();
	}
	return false;
}

function update_single_cart(products)
{
	if (check_cart(products))
	{
		document.forms["cart_form"]["single_product"].value = products;
		warn_onunload=false;
		document.forms["cart_form"].submit();
	}
	return false;
}

function update_ext(product_code)
{
	var price = getvalue('price_'+product_code);
	var qty_raw = getvalue('quantity_'+product_code);
	if (qty_raw=="") {qty_raw=0;}
	if (qty_raw=="08")
		qty_raw=8;
	else if (qty_raw=="09")
		qty_raw=9;
	var qty = parseInt(qty_raw);

	setvalue('quantity_'+product_code,qty);
	
	var total = round_number(price*qty,2);
	if (String(total).indexOf(".")>0)
	{
		total = String(total).split(".");
		var total1 = total[0];
		var total2 = total[1];
		if (total2.length==1)
			total2 += "0";
	} else
	{
		var total1 = total;
		var total2 = "00";
	}
	
	sethtml('extended1_'+product_code,total1);
	sethtml('extended2_'+product_code,total2);
}

function round_number(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
