function showPopup(url) {
	window.open(url,'Popup','width=658,height=500,location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=no');
}


//cart functions 
function cartAdd(id,price,quantity) {
	//first check if price is in range
	new Ajax.Request('/Page.php', {
		parameters: {ajax:"",action:"cartAdd",productId: id,price: price,quantity: quantity},
		onSuccess: function(transport,json) {
				if (transport.responseText == "ERROR_OUTSIDERANGE") {
					alert("The value entered is not in the allowed range");
					$('cardvalue').value = $('prevcardvalue').value;
					$('cardvalue').select;
				}
				else {
					if ($('addToCartFeedback')) $('addToCartFeedback').innerHTML = "Product added.";
					cartRefresh();
				}
			}
		})
}

function cartUpdate(idx,price) {
	//first check if price is in range
	new Ajax.Request('/Page.php', {
		parameters: {ajax:"",action:"cartUpdate",index: idx,price: price},
		onSuccess: function(transport,json) {
				if (transport.responseText == "ERROR_OUTSIDERANGE") {
					alert("The value entered is not in the allowed range");
					$('cardvalue['+idx+']').value = $('prevcardvalue['+idx+']').value;
				}
				else {
					$('prevcardvalue['+idx+']').value = price;
					$('prevcardvalue['+idx+']').select;
					cartRefresh();
				}
			}
		})
}

function cartRemove(idx) {
	//first check if price is in range
	new Ajax.Request('/Page.php', {
		parameters: {ajax:"",action:"cartRemove",index: idx},
		onSuccess: function(transport) {
				itemsLeft = transport.responseText;
				if (itemsLeft == 0) document.location = "/cart/";
				else {
					deleteRow('cartitems_'+idx+'');
					cartRefresh();
					refreshSubtotals();
				}
			}
		})
}

function cartRefresh() {
	if (!$('CartTab')) return;
	new Ajax.Updater('CartTab','/Page.php', {parameters: {ajax:"",action:"cartRefresh"}});
}

function refreshSubtotals(setCountry,setPromoCode) {
	if (!$('CartSubtotals')) return;
	new Ajax.Updater('CartSubtotals','/Page.php', {parameters: {ajax:"",action:"cartSubtotals",country:setCountry,promoCode:setPromoCode}});	
}

function deleteRow(theId) {
	if ($(theId)) $(theId).remove();
}

//login functions
function submitForm(form,callback) {
	var params = Form.serialize(form)+"&ajax=";
	new Ajax.Request('/Page.php', { parameters: params, 
									onSuccess: callback
								}
					)
	return false;
}

function LoginFormCallback(response) {
	if (response.readyState != 4) {
	    return;
	}
	else {
		if (response.responseText == null || response.responseText == "") window.location.reload();
		else $('loginFailedMessage').show();
	}
}

function LoginFormCallback2(response) {
	if (response.readyState != 4) {
	    return;
	}
	else {
		if (response.responseText == null || response.responseText == "") window.location.reload();
		else $('loginFailedMessage2').show();
	}
}

function doAjaxAction(action,callback) {
	var params="ajax=&action="+action;
	new Ajax.Request('/Page.php', { parameters: params, onSuccess: callback} );
}

function doAjaxUpdate(action,element) {
	if ($(action)) var params = Form.serialize(action)+"&ajax=";
	else var params="ajax=&action="+action;
	new Ajax.Updater(element,'/Page.php', { parameters: params } );	
}