//
function isNumber(value) {
	var pattern = /\D/i; // test for alpha only
	return (pattern.test(value) == false);
}

// home
// contact form function
function OnPostContactForm(form) {
	
	// serialise the form
	// into an object, which can be posted
	// to the contact service
	$.post('ws/contact.php',
		   $(form).serialize(),
		   function(data) {
			   alert("Thank you for your messsage.  We will email to you shortly");
			   $(form)[0].reset();
		   }
	);

}

//home
//called by: home "add to cart"
// no longer used
function onBuyClicked(_priceId) {
	
	// double-click in progress, quit
	if($.buyNowPressed) return;
	
	// prevent double-click problems
	$.buyNowPressed = true;
	
	// reset the success message
	$('#basketmsg').removeClass('addBuySuccess');
	$('#basketmsg').removeClass('addBuyFailure');
	$('#basketmsg').addClass('addBuyWait');
	$('#basketmsg').html('Please wait whilst we add your product to cart');
	
	// get the qty
	// the form input element is named qty_{priceId}
	var _qty = $("#qty_" + _priceId).val();
	// make sure the entry is a number
	if(isNumber(_qty)) {
		// post the details to the server	
		$.ajax({
			type: 'POST',
			url: 'ws/updatebasket.php',
			data: { priceid: _priceId, qty: _qty },
			success: function(data) {

				// reset the buynow double-click flag
				$.buyNowPressed = false;
				
				$('#basketmsg').removeClass('addBuyWait');
				
				$('#basketmsg').html(data.message);

				if(data.errorCode >= 0) // success
				{
					$('#basketitems').html('items: ' + data.items);
					$('#baskettotal').html('total: $' + data.total);
					$('#basketmsg').addClass('addBuySuccess');
				}
				else
				{
					$('#basketmsg').addClass('addBuyFailure');
				}
			},
			dataType: 'json'
		});
	}
	else
	{
		alert("Please end a valid quantity");
	}
}

/**** basket **************************/

function onCheckoutClicked(base) {
	// post the page over to the start of the checkout process
    var url = base + "/delivery";
	document.location.href = url;
}
 /*
function onCheckoutClicked() {
	// post the page over to the start of the checkout process
	document.location.href = "/delivery";
}
*/
// updates the shopping cart quantity
function updateBasket(orderitemid) {
	// fetch the quantity, the form field will be labelled
	// qty_{orderitemid}
	var quantity = $('#qty_' + orderitemid).val();
	$('#orderitemid').val(orderitemid);
	$('#quantity').val(quantity);
	$('#basketAction').val("modify");
	$('#basket').submit();
}

function removeFromBasket(orderitemid) {
	$('#orderitemid').val(orderitemid);
	$('#quantity').val(0);
	$('#basketAction').val("modify");
	$('#basket').submit();
}

//
function addCoupon() {
	$('#basketAction').val("coupon");
	$('#basket').submit();
}

/********** delivery *********************/

function popup(name) {
	
	window.open(name + '?popup=true', "popup", "height=600, width=500, toolbar=no, location=no, scrollbars=yes");
	
}

/****************** DETAIL POPUP ************************/


function ui_dialog_getParam(param, def){
	if(param != undefined)
		return param;
	return def;
}

function modalBackground() {
	var obj = $("<div id=\"uiBackground\"></div>").addClass("ui-dialog-modal-bkgnd").appendTo(document.body);
	var dim = {"width": $(document).width(), "height": $(document).height()};
	obj.css(dim);
}

jQuery.prototype.dialog = function(params){

	if(params == null || params == undefined)
	{
		// default to hidden
		$(this).addClass("ui-dialog-hide");
		$("#uiBackground").remove();
	}
	else
	{
		$(this).removeClass("ui-dialog-hide");
		var width = ui_dialog_getParam(params.width, 300);
		var height = ui_dialog_getParam(params.height, 300);
		var modal = ui_dialog_getParam(params.modal, false);
		
		// top & left positioning for dialog
		// required to centre
		var left = ($(window).width()-width)/2;
		var top = ($(window).height()-height)/2;

		// new style
		var style =  {'position':'absolute',
				'top': top + 'px',
				'left': left + 'px',
				'width': width + 'px',
				'height': height + 'px',
				'z-index': 10000
		};

		//
		if(modal)
		{
			modalBackground();
		}

		$(this).css(style);
		
	}
};

/*************************/
// detail popup
function detail(_productId){

	// request the data
    $.ajax({
		type: 'POST',
		url: 'ws/detail.php',
		data: { productid: _productId },
		success: function(data) {
/*
	        // set some properties
	        $("#productDetail .image img").attr("src", data.image);
	        $("#productDetail .detail .title").html(data.title);
	        $("#productDetail .detail .desc").html(data.desc);
	        //
	        // remove all children of prices
	        $("#productDetail .detail .prices .price").remove();
*/
	        // set some properties
	        $("#productDetail .image img").attr("src", data.image);
	        $("#productDetail .title").html(data.title);
	        $("#productDetail .desc").html(data.desc);
	        //
	        // remove all children of prices
	        $("#productDetail .prices .price").remove();
	        
	        // loop through the price data
	        for(var i = 0; i < data.prices.length; i++) {
				// add a price div
				var obj = data.prices[i];

	        	var sz = "<div class=\"price\"><table width=\"100%\"><tr>" +
	        	"<td><input type=\"text\" value=\"1\" id=\"qty_" + obj.priceid + "\" size=\"2\" class=\"txt\" /></td>";
	        	if(obj.disc > 0) {
	        		sz += "<td><span class=\"priceStrike\">" + data.symbol + obj.price + "/" + obj.desc + "</span><br/>" + 
					  	  "<span class=\"priceRed small\">" + data.symbol + obj.disc + "/" + obj.desc + "</span></td>";
	        	}
	        	else {
	        		sz += "<td><span>" + data.symbol + obj.price + "/" + obj.desc + "</span></td>";
	        	}
	        	
	        	sz += "<td align=\"right\">" +
		        	"		<input type=\"button\" value=\"" + obj.buytxt + "\" class=\"btn\" price=\"" + obj.priceid + "\" />" +
		        	"  </td></tr></table>" +
		        	"</div>";

				 //$(sz).appendTo($("#productDetail .detail .prices"));
				 $(sz).appendTo($("#productDetail .prices"));
	        }
	        
	        //
			$("#productDetail").dialog({width: '700', height: '350', modal: true});
		},
		dataType: 'json'
	});
		
}


