function showStatus(message){
  var blurb = "status='"+message+"'";
  setTimeout(blurb,0);
  return true;
}

function clearStatus(){
  setTimeout("status=''",0);
  return true;
}

function goLocation(thisFormSelect) {
	var thisSelection = thisFormSelect.options[thisFormSelect.selectedIndex].value;
  	if (thisSelection != "") {
    	location = thisSelection;
	}
}

function isEmpty(string) {
	var whitespace = " \t\n\r";

	if ((string == null) || (string.length == 0)) {
		return true;
	}
    for (i = 0; i < string.length; i++) {
		var chr = string.charAt(i);
		if (whitespace.indexOf(chr) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function tooLong(string, max) {
	if (string.length > max) {
		return true;
	}
}

function stripChars (string, accept) {
// string to clean, acceptable characters
	var i;
	var returnString = "";

	for (i = 0; i < string.length; i++) {
		var chr = string.charAt(i);
		if (accept.indexOf(chr) != -1) returnString += chr;
	}

	return returnString;
}

function showError(ourform, where, warning) {
	ourform.elements[where].focus();
	if (document.getElementById) {
		document.getElementById(where).style.backgroundColor = '#fa0';
	}
	alert(warning);
}

function hideCheckoutbutton() {
	if (document.all||document.getElementById) {
		document.forms['checkout'].next.disabled=true;
	}
}

function checkoutForm() {
	ourform = document.forms['checkout'];
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;

	if (document.getElementById) {
		document.getElementById('forename').style.backgroundColor = '#fff';
		document.getElementById('surname').style.backgroundColor = '#fff';
		document.getElementById('address').style.backgroundColor = '#fff';
		document.getElementById('city').style.backgroundColor = '#fff';
		document.getElementById('county').style.backgroundColor = '#fff';
		document.getElementById('postcode').style.backgroundColor = '#fff';
		document.getElementById('email').style.backgroundColor = '#fff';
		document.getElementById('phone').style.backgroundColor = '#fff';
		document.getElementById('ship_forename').style.backgroundColor = '#fff';
		document.getElementById('ship_surname').style.backgroundColor = '#fff';
		document.getElementById('ship_address').style.backgroundColor = '#fff';
		document.getElementById('ship_city').style.backgroundColor = '#fff';
		document.getElementById('ship_county').style.backgroundColor = '#fff';
		document.getElementById('ship_postcode').style.backgroundColor = '#fff';
	}

	if (isEmpty(ourform.forename.value)) {
 		showError(ourform, "forename", "Please make sure you enter your first name");
		return false;
	}

	if (isEmpty(ourform.surname.value)) {
 		showError(ourform, "surname", "Please make sure you enter your family name");
		return false;
	}

	if (tooLong(ourform.forename.value, 40)) {
 		showError(ourform, "forename", "Sorry, your first name exceeds the maximum 40 letters");
		return false;
	}

	if (tooLong(ourform.surname.value, 40)) {
 		showError(ourform, "surname", "Sorry, your family name exceeds the maximum 40 letters");
		return false;
	}

	if (isEmpty(ourform.address.value)) {
 		showError(ourform, "address", "Please make sure you enter the cardholder's address");
		return false;
	}

	if (tooLong(ourform.address.value, 40)) {
 		showError(ourform, "address", "Sorry, your address exceeds the maximum 40 letters");
		return false;
	}

	if (isEmpty(ourform.city.value)) {
 		showError(ourform, "city", "Please make sure you enter the cardholder's address");
		return false;
	}

	if (tooLong(ourform.city.value, 25)) {
 		showError(ourform, "city", "Sorry, your town or city exceeds the maximum 25 letters");
		return false;
	}

	if (tooLong(ourform.county.value, 25)) {
 		showError(ourform, "county", "Sorry, your county or region exceeds the maximum 25 letters");
		return false;
	}

	if (isEmpty(ourform.postcode.value)) {
 		showError(ourform, "postcode", "Please make sure you enter your postal or zip code");
		return false;
	}

	if (tooLong(ourform.postcode.value, 8)) {
 		showError(ourform, "postcode", "Sorry, your postal or zip code exceeds the maximum 8 characters");
		return false;
	}

	if (isEmpty(ourform.email.value)) {
 		showError(ourform, "email", "Please make sure you enter your email address\n- we respect your privacy and will only\nuse it to communicate about your order");
		return false;
	}

	if (tooLong(ourform.email.value, 60)) {
 		showError(ourform, "email", "Sorry, your email address exceeds the maximum 60 characters");
		return false;
	}

	if (!(filter.test(ourform.email.value))) {
		showError(ourform, "email", "Please check your email address - it does not appear to be valid");
		return false;
	}

	if (isEmpty(ourform.phone.value)) {
 		showError(ourform, "phone", "Please make sure you enter a contact number\n- we respect your privacy and will only use\nit in case of a problem with your order");
		return false;
	}

	if (tooLong(ourform.ship_forename.value, 40)) {
 		showError(ourform, "ship_forename", "Sorry, the delivery first name exceeds the maximum 40 letters");
		return false;
	}

	if (tooLong(ourform.ship_surname.value, 40)) {
 		showError(ourform, "ship_surname", "Sorry, the delivery surname exceeds the maximum 40 letters");
		return false;
	}

	if (tooLong(ourform.ship_address.value, 40)) {
 		showError(ourform, "ship_address", "Sorry, your delivery address exceeds the maximum 40 letters");
		return false;
	}

	if (tooLong(ourform.ship_city.value, 25)) {
 		showError(ourform, "ship_city", "Sorry, your delivery town or city\nexceeds the maximum 25 letters");
		return false;
	}

	if (tooLong(ourform.ship_county.value, 25)) {
 		showError(ourform, "ship_county", "Sorry, your delivery county or region exceeds the maximum 25 letters");
		return false;
	}

	if (tooLong(ourform.ship_postcode.value, 9)) {
 		showError(ourform, "ship_postcode", "Sorry, your delivery postal or zip code exceeds the maximum 9 characters");
		return false;
	}

	else {
		hideCheckoutbutton();
		return true;
	}
}

function hidePurchasebutton() {
	if (document.all||document.getElementById) {
		document.forms['cardform'].purchase.disabled=true;
	}
}

function cardForm() {
	ourform = document.forms['cardform'];

	if (document.getElementById) {
		document.getElementById('CardHolder').style.backgroundColor = '#fff';
		document.getElementById('CardNumber').style.backgroundColor = '#fff';
		document.getElementById('StartDateYear').style.backgroundColor = '#fff';
		document.getElementById('ExpiryDateYear').style.backgroundColor = '#fff';
		document.getElementById('IssueNumber').style.backgroundColor = '#fff';
	}

	// strip out anything thats not alphanumeric, hyphen or space
	oldcardholder = ourform.CardHolder.value;
	var accept = '0123456789- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	ourform.CardHolder.value = stripChars(oldcardholder, accept);

	if (isEmpty(ourform.CardHolder.value)) {
 		showError(ourform, "CardHolder", "Please make sure you enter your name as it appears on your card");
		return false;
	}

	if (tooLong(ourform.CardHolder.value, 40)) {
 		showError(ourform, "CardHolder", "Sorry, your name exceeds the maximum 40 letters");
		return false;
	}

	// strip out non-numbers
	oldcardnumber = ourform.CardNumber.value;
	var accept = '0123456789';
	ourform.CardNumber.value = stripChars(oldcardnumber, accept);

	if (isEmpty(ourform.CardNumber.value)) {
 		showError(ourform, "CardNumber", "Please make sure you enter your card number");
		return false;
	}

	if (tooLong(ourform.CardNumber.value, 20)) {
 		showError(ourform, "CardNumber", "Sorry, your card number exceeds the maximum numbers allowed");
		return false;
	}

	if (ourform.CardNumber.value.length < 12) {
 		showError(ourform, "CardNumber", "Sorry, your card number does not seem to be correct");
		return false;
	}

	if (ourform.CardType.options[ourform.CardType.selectedIndex].value == "SWITCH") {
		if (isEmpty(ourform.IssueNumber.value)) {
			showError(ourform, "IssueNumber", "Please make sure you enter the Issue Number of your Switch card");
			return false;
		}
	}

	if (ourform.CardType.options[ourform.CardType.selectedIndex].value == "DELTA") {
		if (isEmpty(ourform.IssueNumber.value)) {
			showError(ourform, "IssueNumber", "Please make sure you enter the Issue Number of your Delta card");
			return false;
		}
	}

	var date = new Date();
	var now = date.getFullYear();
	startyear = '20'+ ourform.StartDateYear.options[ourform.StartDateYear.selectedIndex].value;
	expireyear = '20'+ ourform.ExpiryDateYear.options[ourform.ExpiryDateYear.selectedIndex].value;
	if (startyear > now) {
		showError(ourform, "StartDateYear", "Please check your card \"Valid From\" date");
		return false;
	}

	if (expireyear < now) {
		showError(ourform, "ExpiryDateYear", "Please check your card \"Expires\" date");
		return false;
	}

	if (isEmpty(ourform.CV2.value)) {
 		showError(ourform, "CV2", "Please make sure you enter your\nCard Verification Number\n- this is the 3 or 4 digit number\nfound on the signature strip\non the back of your card.");
		return false;
	}

	if ((ourform.showterms.value == "1") && (!ourform.agreeterms.checked)) {
		alert('You must tick the box to indicate that\nyou agree with our terms and conditions\nbefore you can proceed');
		return false;
	}

	else {
		hidePurchasebutton();
		return true;
	}
}

function chequeForm() {
	ourform = document.forms['chequeform'];
	if ((ourform.showterms.value == "1") && (!ourform.agreeterms.checked)) {
		alert('You must tick the box to indicate that\nyou agree with our terms and conditions\nbefore you can proceed');
		return false;
	}
}
