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

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

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 = '#fc0';
	}
	alert(warning);
}


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

	if (document.getElementById) {
		document.getElementById('name').style.backgroundColor = '#eee';
		document.getElementById('email').style.backgroundColor = '#eee';
		document.getElementById('phone').style.backgroundColor = '#eee';
		document.getElementById('message').style.backgroundColor = '#eee';
	}

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

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

	if (isEmpty(ourform.message.value)) {
 		showError(ourform, "message", "Please make sure you enter a message");
		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 (tooLong(ourform.name.value, 40)) {
 		showError(ourform, "name", "Sorry, your name exceeds the maximum 40 characters");
		return false;
	}

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

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

	else {
		if (document.all||document.getElementById) {
			document.forms[0].submit.disabled=true;
		}
		return true;
	}
}

function checkTellfriend() {
	ourform = document.forms[0];
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	var accept = '0123456789- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

	if (document.getElementById) {
		document.getElementById('to').style.backgroundColor = '#fff';
		document.getElementById('toemail').style.backgroundColor = '#fff';
		document.getElementById('from').style.backgroundColor = '#fff';
		document.getElementById('fromemail').style.backgroundColor = '#fff';
	}

	if (isEmpty(ourform.to.value)) {
 		showError(ourform, "to", "Please make sure you enter your friend's name");
		return false;
	}

	if (isEmpty(ourform.toemail.value)) {
 		showError(ourform, "toemail", "Please make sure you enter your friend's email address");
		return false;
	}

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

	if (isEmpty(ourform.fromemail.value)) {
 		showError(ourform, "fromemail", "Please make sure you enter your email address");
		return false;
	}

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

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

	if (tooLong(ourform.to.value, 35)) {
 		showError(ourform, "to", "Sorry, your friend's name exceeds the maximum 35 characters");
		return false;
	}

	if (tooLong(ourform.from.value, 35)) {
 		showError(ourform, "from", "Sorry, your name exceeds the maximum 35 characters");
		return false;
	}

	if (tooLong(ourform.toemail.value, 100)) {
 		showError(ourform, "toemail", "Sorry, your friend's email address exceeds the maximum 100 characters");
		return false;
	}

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

	else {
		ourform.to.value = stripChars(ourform.to.value, accept);
		ourform.from.value = stripChars(ourform.from.value, accept);
		if (document.all||document.getElementById) {
			document.forms[0].send.disabled=true;
		}
		return true;
	}
}

function checkAddproduct() {
	ourform = document.forms['addproduct'];
	if(ourform.lineid) {
		if ((isEmpty(ourform.lineid.value)) || (ourform.lineid.value=='0')) {
			ourform.lineid.focus();
	 		alert("Please make sure you choose one of the available options for this product");
			return false;
		}
	}
	var user_input_reqd = document.getElementById('user_input_reqd');
	var user_input = document.getElementById('user_input');
	if(user_input_reqd.value=='true') {
		if ((isEmpty(user_input.value)) || (user_input.value=='')) {
			user_input.focus();
	 		alert("Please make sure you complete the notes field for this product");
			return false;
		}
	}
	return true;
}

function checkAddMultiproduct() {
	var ourform = document.forms['addproduct'];
	var curProduct = 0;
	var curQty;
	for (var i=0; i<ourform.elements.length; i++) {
		curQty = 'qty' + curProduct;
		var productID = 'new' + curProduct;
		if(ourform.elements[curQty]) {
			if (ourform.elements[curQty].value > 0) {
				return true;
			}
			curProduct++;
		}
	}
	ourform.qty0.focus();
	alert("Please select at least one product to add to your basket");
	return false;
}

function goAffiliate(form) {
	windowName = 'formTarget' + (new Date().getTime());
	form.target = windowName;
  	window.open('', windowName, 'width=720,height=650,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,status=yes');
}