// 
// eshop.js - Javascript for the eShop
//
//

// Function for expanding and contracting the shopping basket contents
function basketSlider(elementId, intNoOfItems)
{
	var element = document.getElementById(elementId);
	if(element.up == false || element.down)
	{
		$(element).animate({height: "4px"}, 500);
		$(element).animate({ height:"0px",left:"535px", top:"0px", width: "0px"}, 1);
		element.up = true;
		element.down = false;
	}
	else
	{
		$(element).animate({ height:"4px",left:getLeft(getWidth(intNoOfItems)), top:"0px", width: getWidth(intNoOfItems)}, 1);
		$(element).animate({height: getHeight(intNoOfItems)}, 500);
		element.down = true;
		element.up = false;
	}
}
//Function which will calculate the correct height of the slide out basket depending on the number of items in it
function getHeight(intNoOfItems){
	if(document.getElementById && document.getElementById("basketSummary")){
		var intOffsetHeight = document.getElementById("basketSummary").offsetHeight;
		var intOffsetValue = (intOffsetHeight - 22) * 2;
		var intNewLineHeight = 25 + (intOffsetValue + 1);
		return intNoOfItems * intNewLineHeight;
	}
}
//Function which will calculate the correct width of the slide out basket depending on the number of items in it
function getWidth(){
	if(document.getElementById && document.getElementById("basketSummary")){
		var intOffsetWidth = document.getElementById("basketSummary").offsetWidth;
		var intOffsetValue = (intOffsetWidth - 4) * 2;
		return intOffsetValue;
	}
}
function getLeft(intWidth){
	var intNewLeft = 535 - intWidth;
	return intNewLeft;
}

//Function to display the correct postage price on the basket page if the select box is changed
function UpdatePostage()
{
	var element1 = document.getElementById("postageOption").value;
	window.location = "/eShop/shop_basket.asp?action=updatepostage&postageoption=" + element1;
}

// Shows a JS popup containing a message
function showAddressForm(strTitle, strMessage) {
	
	var myNewString = strMessage.replace(/#replacedquote#/g, '"');
	$("body").after('<div id="itemPreviewOverlay" class="flora" title="'+strTitle+'">'+myNewString+'</div>');
	$("#itemPreviewOverlay").dialog({
		width: "450px",
		height: "auto"
	});
	
	var errorConfirmBtn = document.getElementById("addressbutton");
	$("#addressbutton").click( function () {
		var frmElement = document.getElementById('frmAddress');
		frmElement.submit();
		$("#itemPreviewOverlay").dialog('close');
		});
}

//Function to submit the checkout form to the correct provider during the onload
function SubmitCheckout()
{
	if (document.getElementById('frmCheckout') != null) {
		var frmElement = document.getElementById('frmCheckout');
		frmElement.submit();
	} 
}

addLoadEvent(SubmitCheckout);