// Preloading navigation buttonsvar image1 = new Image();
var image1 = new Image();
var image2 = new Image();
var image3 = new Image();
var image4 = new Image();
var image5 = new Image();
var image6 = new Image();
image1.src = 'images/beerOver.png';
image2.src = 'images/breweryOver.png';
image3.src = 'images/keyOver.png';
image4.src = 'images/eventOver.png';
image5.src = 'images/stockistOver.png';
image6.src = 'images/contactOver.png';

// onMouseOver and onMouseOut function
function mouseOver(imageObject){	
	imageObject.src = imageObject.src.replace('.','Over.');
}

function mouseOut (imageObject) {
	imageObject.src = imageObject.src.replace('Over.','.');
}

// validating login
function validateLogin() {
	var errors = new Array();
	
	if (document.loginform.username.value == '') {
		errors.push('You need to enter a username.');
		document.loginform.username.className = 'error';
	}
	if (document.loginform.password.value.length < 6) {
		errors.push('You need to enter your password of 6 or more characters.');
		document.loginform.password.className = 'error';
	}
	if (errors.length > 0) {
		alert('Errors detected \n' + errors.join('\n'));
		return false;
	} else {
		alert('Thank you for login in. \n' + ((document.loginform.remember.checked) ? 'Username and password REMEMBERED.' : 'Username and password NOT REMEMBERED.'));
		return true;
	}
}

// validate registration
function validateRegForm(regForm) {
	var errors = new Array();
	
	var name = regForm.name.value;
	var dob = regForm.dob.value;
	var email = regForm.email.value;
	var username = regForm.username.value;
	var password = regForm.password.value;
	var repassword = regForm.repassword.value;
	
// test if text fields are blank
	if (name=="" || dob=="" || email=="" || username=="" || password=="" || repassword=="") {
		errors.push('One or more of the required text field is empty');
	}
	
// test date of birth for numeric content and length	
	if (isNaN(dob)) {
		errors.push('Date of Birth not in numbers \n');
	} else {
// it is a number
		if (dob.length!=6) {
			errors.push('Date of Birth must be 6 digits');
		}
	}
	
// check email address
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		errors.push('Invalid E-mail address')
	}	
	
// test a radio button has been selected
	var gender = false;
	
	for (i=0; i<regForm.gender.length; i++) {
		if (regForm.gender[i].checked) {
			gender = true;
		}
	}
	
	if (!gender) {
		errors.push('You have not selected a gender');
	}

// check country selected
	var select = regForm.country.selectedIndex;
	
	var country = regForm.country.options[select].value;
	
	if (country=="") {
		errors.push('You have not chosen your country');
	}

// check password length is atleast 6 character and passwords are identical
	if (password.length<6) {
		errors.push('Password has to be more than 6 character');
	}
	
	if (password!=repassword) {
		errors.push('Password has to be the same');
	}
	
	if (errors.length>0) {
		alert('Please correct the form \n' + errors.join('\n'));
			return false; // stop the form from being submitted
		} else {
			alert('Thank you for registering');
			return true; // allow the form to be submitted
		}
}

// popup window for registration terms and conditions
function popUpTnC (popURL,popName,popFeats) {
	// function to open a new window
	TnCWin = window.open(popURL,popName,popFeats);
}

function closeTnC () {
	// function to close terms and condition window
	this.close();
}

function validateRecForm(RecForm) {
	var errors = new Array();
	
	var email = RecForm.email.value;
	var email2 = RecForm.email2.value;
	var reset = RecForm.reset.value;

// check if text fields are blank
	if (email=="" || email2=="" || reset=="") {
		errors.push('One or more of the required text field is empty');
	}
	
// check email address
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		errors.push('Invalid E-mail address')
	}

// check if both emails are the same
	if (email!=email2) {
		errors.push('Your email address do not match');
	}
	
	if (errors.length>0) {
		alert('Please correct the form \n' + errors.join('\n'));
			return false; // stops the form from being submitted
		} else {
			alert ('Thank you, an email will be sent to you email address at ' + email);
			return true; // allow the form to be submitted
		}
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=680');");
}
