function showmenu(menu) {
	menu.getElementsByTagName('ul')[0].style.display = "block";
} 
function hidemenu(menu) {
	menu.getElementsByTagName('ul')[0].style.display = "none";
}

var default_bg = "white";
var invalid_bg = "#ffff55";

function validateEnterForm(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.mens_challenge_registration_first_name,"First Name");
	reason += validateEmpty(theForm.mens_challenge_registration_last_name,"Surname");
	reason += validateEmpty(theForm.mens_challenge_registration_address,"Address");
	reason += validateEmpty(theForm.mens_challenge_registration_suburb,"Suburb");
	reason += validateSelection(theForm.mens_challenge_registration_state,"State");
	reason += validatePostcode(theForm.mens_challenge_registration_postcode,"Postcode");
	reason += validatePhoneNo(theForm.mens_challenge_registration_phone,"Phone number");
	reason += validateEmpty(theForm.mens_challenge_registration_reciept,"Reciept No.");
	reason += validateEmpty(theForm.mens_challenge_registration_barcode1,"Item1 Barcode");
	reason += validateEmpty(theForm.mens_challenge_registration_comp_entry,"Answer");
	
	reason += validateTerms(theForm.mens_challenge_registration_terms,document.getElementById("terms_label"));

	if(reason != "") {
		alert(	"Some fields need correction:\n\n"+reason);
		return false;
	}
	return true;
}

function validateEnterFormColes(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.mens_challenge_registration_first_name,"First Name");
	reason += validateEmpty(theForm.mens_challenge_registration_last_name,"Surname");
	reason += validateEmpty(theForm.mens_challenge_registration_address,"Address");
	reason += validateEmpty(theForm.mens_challenge_registration_suburb,"Suburb");
	reason += validateSelection(theForm.mens_challenge_registration_state,"State");
	reason += validatePostcode(theForm.mens_challenge_registration_postcode,"Postcode");
	reason += validatePhoneNo(theForm.mens_challenge_registration_phone,"Phone number");
	reason += validateEmpty(theForm.mens_challenge_registration_reciept,"Reciept No.");
	reason += validateEmpty(theForm.mens_challenge_registration_barcode1,"Item1 Barcode");	
	reason += validateEmpty(theForm.mens_challenge_registration_barcode2,"Item2 Barcode");
	reason += validateEmpty(theForm.mens_challenge_registration_comp_entry,"Answer");
	
	reason += validateTerms(theForm.mens_challenge_registration_terms,document.getElementById("terms_label"));

	if(reason != "") {
		alert(	"Some fields need correction:\n\n"+reason);
		return false;
	}
	return true;
}

function validateCommentForm(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.mens_challenge_comment_name,"Name");	
	reason += validateEmpty(theForm.mens_challenge_comment_location,"Location");	
	reason += validateEmail(theForm.mens_challenge_comment_email,"Email");	
	reason += validateEmpty(theForm.mens_challenge_comment_comment,"Comment");
	if(reason != "") {
		alert(	"Some fields need correction:\n\n"+reason);
		return false;
	}
	return true;
}

function validateSendToAFriendForm(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.mens_challenge_send_sender_name,"Your Name");	
	reason += validateEmpty(theForm.mens_challenge_send_recipient_name,"Friend's Name");	
	reason += validateEmail(theForm.mens_challenge_send_recipient_email,"Friend's Email");		
	if(reason != "") {
		alert(	"Some fields need correction:\n\n"+reason);
		return false;
	}
	return true;
}

function validateEmpty(field,label) {
	var error = "";
	if(field.value == "") {
		field.style.backgroundColor = invalid_bg;
		error = label+" has not been filled in.\n";
	}
	else {
		field.style.backgroundColor = default_bg;
	}
	return error;
}

function validatePostcode(field,label) {
	var error = "";
	var min=4;
	if((field.value.length < min) || (field.value.length > min)) {
		field.style.backgroundColor = invalid_bg;
		error = label+" should be 4 digits.\n";
	}
	else {
		field.style.backgroundColor = default_bg;
	}
	return error;
}

function validatePhoneNo(field,label) {
	var error = "";
	var min=8;
	if(field.value.length < min) {
		field.style.backgroundColor = invalid_bg;
		error = label+" should be at least 8 digits.\n";
	}
	else {
		field.style.backgroundColor = default_bg;
	}
	return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(field) {
    var error="";
    var tfield = trim(field.value);
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (field.value == "") {
    	field.style.backgroundColor = invalid_bg;
        error = "Email address has not been filled in.\n";
    } else if (!emailFilter.test(tfield)) {
    	field.style.backgroundColor = invalid_bg;
        error = "Please enter a valid email address.\n";
    } else if (field.value.match(illegalChars)) {
    	field.style.backgroundColor = invalid_bg;
        error = "The email address contains illegal characters.\n";
    } else {
    	field.style.backgroundColor = default_bg;
    }
    return error;
}

function validateSelection(field,label) {
	var error = "";
	if(field.selectedIndex == 0) {
		field.style.backgroundColor = invalid_bg;
		error = "Please select an option for "+label+".\n";
	}
	else {
		field.style.backgroundColor = default_bg;
	}
	return error;
}

function validateTerms(field,label) {
	var error = "";
	if(field.checked == false) {
		label.style.color = invalid_bg;
		error = "You must accept the Terms & Conditions to enter.\n";
	}
	else {
		label.style.color = default_bg;
	}
	return error;
}