
function displayDeliveryDetails(f) {
	var c = f.delivery_address;
	var agt=navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

	for (var i = 0; i < c.length; i++) {
		if (c[i].checked) {
			if (c[i].value == 1) {
				if (is_ie) {
					document.getElementById('deliveryDetailsTable').style.display='';
				} else {
					document.getElementById('deliveryDetailsTable').style.display='';
				}
			} else {
				document.getElementById('deliveryDetailsTable').style.display='none';
			}
		}
	}
}

function changeQuantity(f, i) {
	var f = document.getElementById(f);
	var v = parseInt(f.value);
	v = v + i;

	if (f.disabled) return false;

	if (v < 1) {
		if (!confirm("Are you sure? This will remove the item from your order.")) return false;
	}

	f.value = v;
	f.onchange();

}

function validateCheckout() {

	var df = document.getElementById('checkoutForm');
	var cf = document.getElementById('checkoutPaymentForm');
	var m  = document.getElementById('paymentStatusMessage');
	
	if ($('#bill_country').val() == 'USA') {
		$('#bill_addr_row_usa').show();
		$('#bill_addr_row').hide();
		$('#bill_addr_4').attr('disabled', true);
		$('#bill_addr_4_us').removeAttr('disabled');
	} else {
		$('#bill_addr_row_usa').hide();
		$('#bill_addr_row').show();
		$('#bill_addr_4_us').attr('disabled', true);
		$('#bill_addr_4').removeAttr('disabled');
	}
	
	if ($('#ship_country').val() == 'USA') {
		$('#ship_addr_row_usa').show();
		$('#ship_addr_row').hide();
		$('#ship_addr_4').attr('disabled', true);
		$('#ship_addr_4_us').removeAttr('disabled');
	} else {
		$('#ship_addr_row_usa').hide();
		$('#ship_addr_row').show();
		$('#ship_addr_4_us').attr('disabled', true);
		$('#ship_addr_4').removeAttr('disabled');
	}
	
	var incompleteForm = 0;

	$('.required-field').each(function (index) {
		if ($(this).val() == '') {
			if ($(this).attr('disabled') == false) {
				incompleteForm = 1;
			}
		}
	});
	
	var paymentDisabled = false;
	if (incompleteForm == 1) {
		m.innerHTML = 'Please complete "Your Details" and click the "Next &gt;" button before proceeding.';
		paymentDisabled = true;
		document.getElementById('checkoutBuyButton').src = '/buttons/buy_now_button_grey.jpg';
	} else {
		m.innerHTML = 'Now click on or select a payment method, enter your details and click "Buy Now".';
		document.getElementById('checkoutBuyButton').src = '/buttons/buy_now_button_up.jpg';
	}

	for (var i = 0; i < cf.elements.length; i++) {
		cf.elements[i].disabled = paymentDisabled;
	}

}

function updatePriceQty(keyup) {

	var basePrice = document.getElementById('basePrice').value;
	var cSymbol = document.getElementById('currencySymbol').value;
	var sQty = document.getElementById('selectQuantity').value;

	if (keyup!=1 && (!sQty || isNaN(sQty))) {
		sQty = 1;
		document.getElementById('selectQuantity').value = 1;
	} else if (keyup!=1 && (!isNaN(sQty) && sQty > 20)) {
		alert("To order more than 20 items, please contact us.");
		sQty = 19;
		document.getElementById('selectQuantity').value = 19;
	}

	var totalPrice = sQty * basePrice;
	totalPrice = totalPrice.toFixed(2);
	totalPrice = totalPrice.toString(10);
	
	document.getElementById('priceDisplay').innerHTML = cSymbol + totalPrice;

}

function addToCart() {
	var s = document.getElementById('productVariantID');
	var q = document.getElementById('selectQuantity').value;

	if (!q || isNaN(q)) {
		alert("Please enter a quantity before adding to your cart.");
		return false;
	}

	var id = s.options[s.selectedIndex].value;

	addToCartAJAX(id, q);

	alert("Your cart has been updated. Please see the 'Shopping Basket' box for details.");

	return true;

}

function alertAddedToCart() {
	alert("Your shopping basket has been updated.\nIf you would like to change quantities, please click 'Details...' in the shopping basket to the right.");
}

function showHideSelects(selStyle) {
	var sel = document.getElementsByTagName("SELECT");

	for (var i = 0; i < sel.length; i++) {
		sel[i].style.visibility = selStyle;
	}
}

function numeralsOnly(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	}
	return true;
}


function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}

function validateMailingListForm(f) {
	if (f.firstName.value == '' || f.lastName.value == '' || f.email.value == '') {
		alert("Please complete all three fields to continue.");
		return false;
	} else {
		f.submit();
		return true;
	}
}

function showOrderComments(s) {
	if (!document.getElementById('orderCommentsRow2') || !document.getElementById('orderCommentsRow1')) {
		alert("Sorry, unable to show/hide the comments field.");
		return false;
	}

	if (s.options[s.selectedIndex].value == 1) {
		document.getElementById('orderCommentsRow1').style.display = '';
		document.getElementById('orderCommentsRow2').style.display = '';
	} else {
		s.form.order_comments.innerHTML = '';
		s.form.order_comments.onchange();
		document.getElementById('orderCommentsRow1').style.display = 'none';
		document.getElementById('orderCommentsRow2').style.display = 'none';
	}
}

function validateYesPlease(f) {
	var requiredFields = new Array("firstName", "lastName", "emailAddress",
		"topPublications", "radioStations", "topWebsites", "preferredShop",
		"address1", "stateCounty", "zipCode", "country");
	
	var errorState = 0;
	
	for (var i = 0; i < requiredFields.length; i++) {
		with (f.elements[requiredFields[i]]) {
			switch (tagName) {
				case 'INPUT':
					if (value == '') {
						errorState = 1;
					}
					break;
				case 'SELECT':
					if (selectedIndex == 0) {
						errorState = 1;
					}
					break;
			}
		}
	}
	
	var gotActive = 0;
	
	for (var i = 0; i < f.age.length; i++) {
		if (f.age[i].checked) gotActive = 1;
	}
	
	if (gotActive == 0) {
		errorState = 1;
	}
	
	gotActive = 0;
	
	for (var i = 0; i < f.gender.length; i++) {
		if (f.gender[i].checked) gotActive = 1;
	}
	
	if (gotActive == 0) {
		errorState = 1;
	}	
	

	if (errorState == 1) {
		alert("Please complete all asterisked fields.");
		return false;
	}

	f.Submit1.disabled = true;
	f.submit();
	return true;	
}

function validateBCNRequest(f) {
	var requiredFields = new Array("firstName", "lastName", "emailAddress",
		"address1", "stateCounty", "zipCode");
	
	var errorState = 0;
	
	for (var i = 0; i < requiredFields.length; i++) {
		with (f.elements[requiredFields[i]]) {
			switch (tagName) {
				case 'INPUT':
					if (value == '') {
						errorState = 1;
					}
					break;
				case 'SELECT':
					if (selectedIndex == 0) {
						errorState = 1;
					}
					break;
			}
		}
	}

	if (errorState == 1) {
		alert("Please complete all asterisked fields.");
		return false;
	}

	f.Submit1.disabled = true;
	f.submit();
	return true;	
}

function validateSampleRequest(f) {
	var requiredFields = new Array("firstName", "lastName", "emailAddress",
		"topPublications", "radioStations", "topWebsites", "preferredShop",
		"howHeard", "address1", "stateCounty", "zipCode", "whyTrial");
	
	var errorState = 0;
	
	for (var i = 0; i < requiredFields.length; i++) {
		with (f.elements[requiredFields[i]]) {
			switch (tagName) {
				case 'INPUT':
					if (value == '') {
						errorState = 1;
					}
					break;
				case 'SELECT':
					if (selectedIndex == 0) {
						errorState = 1;
					}
					break;
				case 'TEXTAREA':
					if (value == '') {
						errorState = 1;
					}
			}
		}
	}
	
	var gotActive = 0;
	
	for (var i = 0; i < f.age.length; i++) {
		if (f.age[i].checked) gotActive = 1;
	}
	
	if (gotActive == 0) {
		errorState = 1;
	}
	
	gotActive = 0;
	
	for (var i = 0; i < f.gender.length; i++) {
		if (f.gender[i].checked) gotActive = 1;
	}
	
	if (gotActive == 0) {
		errorState = 1;
	}
	
	gotActive = 0;
	
	for (var i = 0; i < f.everDisappointed.length; i++) {
		if (f.everDisappointed[i].checked) gotActive = 1;
	}
	
	if (gotActive == 0) {
		errorState = 1;
	}
	
	if (errorState == 1) {
		alert("Please complete all asterisked fields.");
		return false;
	}

	f.Submit1.disabled = true;
	f.submit();
	return true;
}

function showCurrencyHelp() {
	var chelpWin = window.open('/which_currency_popup.htm', chelpWin, 'resizable=yes,status=no,scrollbars=no,toolbar=no,height=385,width=500');
	chelpWin.focus();
}

function validateCardType() {
	
	var type = $('#cardType').val();

	if (type == 'PAYPAL') {
		$('tr.cc-field').each(function (i) {
			$(this).hide();
		});
		$('tr.paypal-notice').show();
	} else {
		$('tr.cc-field').each(function (i) {
			$(this).show();
		});
		$('tr.paypal-notice').hide();
	} 
	
	if (type == 'MAESTRO' || type =='SWITCH' || type == 'SOLO' || type == 'AMEX') {
		$('#cc-start-date').show();
	} else {
		$('#cc-start-date').hide();
	}
	
	if (type == 'MAESTRO' || type =='SWITCH' || type == 'SOLO') {
		$('#cc-issue').show();
	} else {
		$('#cc-issue').hide();
	}
	
	if (type == 'AMEX') {
		$('#cvnPromptLabel').text('(4 digits on front of card)');
	} else {
		$('#cvnPromptLabel').text('(3 digits on back of card)');
	}
	
	if ($('#bill_country').val() == 'GBR') return true;
	
	if (type == 'UKE' || type == 'MAESTRO' || type == 'SWITCH') {
		alert("Unfortunately we are only able to accept UK-issued Visa Electron and Maestro.\n\nPlease select another card type to proceed.");
		$('#cardType').selectedIndex = 0;
		return false;
	}
}

function chooseCardType(type) {
	$('#cardType').val(type);
	validateCardType();
}

function displayUpsellDiv() {
	var div = document.getElementById('upsellAlertDiv');
	var anchor = document.getElementById('checkoutOrderTotal');
	if (!div || !anchor) return false;
	div.style.display = 'block';
	var pos = findPos(anchor);
	div.style.left = pos[0] + anchor.offsetWidth - 5 + 'px';
	div.style.top = pos[1] - div.offsetHeight + (anchor.offsetHeight) + 'px';
}

function hideUpsellDiv() {
	var div = document.getElementById('upsellAlertDiv');
	if (!div) return false;
	div.style.display = 'none';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function searchReferrerLog() {
	if (!document.createElement) return;
	ref = document.referrer;
	
	if (ref.indexOf('?') == -1) return;
	qs = ref.substr(ref.indexOf('?')+1);
	qsa = qs.split('&');
	var url = '';
	for (i=0;i<qsa.length;i++) {
		qsip = qsa[i].split('=');
	        if (qsip.length == 1) continue;
        	if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
				url = 'srchTerm=' + qsip[1];				
	        }
	}
	if (url != '') {
		var tempRO;
		tempRO = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		var tempDO = new Date();
		var requestURL = '/ajax/rpc.php?timestamp=' + tempDO.getTime() + '&action=registerReferrer&' + url;
		tempRO.open('get', requestURL);
		tempRO.send(null);
	}
	
}

searchReferrerLog();
