
function MM_effectAppearFade(targetElement, duration, from, to, toggle, finish)
{
	Spry.Effect.DoFade(targetElement, {duration: duration, from: from, to: to, toggle: toggle, finish: finish});
}

function checkEnter(e, myForm, action) {
	if (e && e.which) {
		e = e;
		characterCode = e.which;
	} else {
		//e = event;
		characterCode = e.keyCode;
	}
        
	if (characterCode == 13) {
		if (action != 'submit') {
			eval(action);
		} else {
			eval('document.' + myForm + '.submit()');
		}
		return false;
	} else {
		return true;
	}
}

function getSelectedIndexValue (id) {
	return (document.getElementById(id)[document.getElementById(id).selectedIndex].value);
}

function formatCurrency (num) {
  var symbol = document.getElementById('set_currency').value;
  if (symbol == 'NZD') symbol = '$';
  
	num = num.toString().replace(/\$|\, /g, "");

	if (isNaN(num)) {
		num = "0";
	}

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();

	if (cents < 10) {
		cents = "0" + cents;
	}

	for (var i=0; i<Math.floor((num.length-(1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	}

	return (((sign)?"":"-") + symbol + num + "." + cents);
}

function updateShoppingCartDiv () {
	// fix for ssl ajax calls
	var url = (location.protocol.indexOf("https")>-1?"https:":"http:") + "//" + location.hostname + (location.port != 80 && location.port != 443?":" + location.port:"");

	simpleAjax(url + "/shop/getShoppingCartSummary", "if (response['shoppingCart'].totalItems == 0) { document.getElementById('cartContentsHeader').style.display = 'none'; }; document.getElementById('shoppingCartMessageDiv').innerHTML = (response['shoppingCart'].totalItems>0?response['shoppingCart'].totalItems:'no') + ' item(s) in your cart'; document.getElementById('shoppingCartValueDiv').innerHTML = formatCurrency(response['shoppingCart'].totalPrice);", "");
}

function addToShoppingCart (productId, productQuantity, productColour, productSize, promoCheckbox) {
	// fix for ssl ajax calls
	var url = (location.protocol.indexOf("https")>-1?"https:":"http:") + "//" + location.hostname + (location.port != 80 && location.port != 443?":" + location.port:"");

	simpleAjax(url + '/shop/addItemToCart/' + productId + '/' + productQuantity + '/' + productColour.replace(/\//, '||') + '/' + productSize.replace(/\//, '||') + '/' + promoCheckbox, 'updateShoppingCartDiv(); MM_effectAppearFade("successMessage", 500, 0, 100, false); MM_effectAppearFade("cartContentsHeader", 2000, 0, 100, false)', '');
}

function emailtofriend(productID, productCatId) {
	
	//var url = (location.protocol.indexOf("https")>-1?"https:":"http:") + "//" + location.hostname +'/ajaxJson.php?action=tellafriend&productID='
	var url = (location.protocol.indexOf("https")>-1?"https:":"http:") + "//" + location.hostname + (location.port != 80 && location.port != 443?":" + location.port:"")+'/ajaxJson.php?action=tellafriend&productID='
		url = url + productID + '&productCatId=' + productCatId + '&emailReceiver=' + document.getElementById('emailReceiver').value;
	
	simpleAjax(url, 'domailto()', '');
		
	
}
var response;
function domailto() {
	alert(response);
	alert('Email sent');
}
function simpleAjax (url, success, failure) {
	new Ajax.Request(url,
	{
		method: 'get',
		type: 'text/javascript',
		onSuccess: function(transport){
			var response = transport.responseText.evalJSON() || "no response text";
			eval(success);
		},
      
		onFailure: function(){
			eval(failure);
		}
	});
}

function adjustPaddingDivSize() {
	var myHeight = 0;

	if (typeof(window.innerWidth) == "number") {
		//Non-IE
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in "standards compliant mode"
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}

	if (myHeight < screen.availHeight) {
		document.getElementById("pageData").style.paddingBottom = ((screen.availHeight + 100) - myHeight) + "px";
	}
}



