function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
function passwordfocus(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
thisfield.type = "password";
}
}
function passwordblur(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
thisfield.type = "text";
}
}

function input_blue_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-280px"; }
function input_blue_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_navy_blue_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-35px"; }
function input_navy_blue_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_grape_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-70px"; }
function input_grape_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_purple_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-70px"; }
function input_purple_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_pink_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-105px"; }
function input_pink_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_red_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-140px"; }
function input_red_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_orange_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-175px"; }
function input_orange_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_yellow_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-210px"; }
function input_yellow_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_green_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-245px"; }
function input_green_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_brown_focusbackground_id(){document.getElementById(background_id).style.backgroundPositionY = "-350px"; }
function input_brown_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_black_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-315px"; }
function input_black_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function input_white_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-315px"; }
function input_white_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "-385px"; }

function textarea_focus(background_id){document.getElementById(background_id).style.backgroundPositionY = "-120px"; }
function textarea_blur(background_id){document.getElementById(background_id).style.backgroundPositionY = "0"; }

function search_focus(){document.getElementById('search-field-bg').style.backgroundPositionY = "-24px";}
function search_blur(){document.getElementById('search-field-bg').style.backgroundPositionY = "0";}


function registerpopup(){
document.getElementById('register-popup').style.display = "block";
document.getElementById('login-popup').style.display = "none";
document.getElementById('lost-password-popup').style.display = "none";
}
function registercancel(){
document.getElementById('register-popup').style.display = "none";
}
function loginpopup(){
document.getElementById('login-popup').style.display = "block";
document.getElementById('register-popup').style.display = "none";
document.getElementById('lost-password-popup').style.display = "none";
}
function logincancel(){
document.getElementById('login-popup').style.display = "none";
}
function lostpasswordpopup(){
document.getElementById('register-popup').style.display = "none";
document.getElementById('login-popup').style.display = "none";
document.getElementById('lost-password-popup').style.display = "block";
}
function lostpasswordcancel(){
document.getElementById('lost-password-popup').style.display = "none";
}

// Form Validator

function formValidator(){
	// Make quick references to our fields
	var firstname = document.getElementById('formfirstname');
	var family = document.getElementById('formfamily');
	var lastname = document.getElementById('formlastname');
	var email = document.getElementById('formemail');
	var street = document.getElementById('formstreet');
	var phone = document.getElementById('formphone');
	var referral = document.getElementById('formreferral_other');
	var page = document.getElementById('formpage');
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(firstname, "Please enter only letters for your first name")){
		if(isAlphabet(lastname, "Please enter only letters for your last name")){
			if(emailValidator(email, "Please enter a valid email address")){
				if(honeypotEmpty(family, "Please ensure this form is filled out correctly")){
					if(isAlphanumeric(street, "Please ensure this form is filled out correctly")){
						if(isNumeric(phone, "Please ensure this form is filled out correctly")){
							if(isAlphanumeric(referral, "Please ensure this form is filled out correctly")){
								if(isAlphanumeric(message, "Please ensure this form is filled out correctly")){
									if(isNumeric(page, "Please ensure this form is filled out correctly")){
										if(lengthRestriction(phone, 8,20)){
											if(lengthRestriction(firstname, 2,40)){
												if(lengthRestriction(lastname, 2,40)){
													if(lengthRestriction(email, 6,100)){
														if(lengthRestriction(message, 0,500)){
															if(lengthRestriction(street, 0,200)){
																if(lengthRestriction(referral, 0,300)){
																	if(lengthRestriction(page, 1,6)){
																		return true;
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function honeypotEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		return true;
	}else{
		alert(helperMsg);
		return false;
	}
}

function messageempty(elem, helperMsg){
	if(elem.value != 'Type your enquiry here. (required)'){
		return true;
	}else{
		alert(helperMsg);
		return false;
	}
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9 _()]*[0-9()][0-9 _()]*$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[A-Za-z0-9 _.-?;:&$%#@!()]*[A-Za-z0-9.-?;:&%#@!()][A-Za-z0-9 _.-?;:&$%#@!()]*$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
