// JavaScript Document
/** 
 * checkLogin() checks whether the login form has been completed and returns 
 * an error message otherwise.
 */
function checkLogin() {
	script = "index.php";
	criteria = "?loginUname=" + document.login.username.value; 
 	
	if (document.login.username.value.length == 0 ||
		document.login.password.value.length == 0 ) { // if username and password fields are blank
		location1 = script + criteria + "&loginErr=2";
		window.location.replace(location1); // returns an error message to the login form
		return false;
	} else { // username and password fields are not blank
		return true;
	} // end if
}

/**
 * checkSearch() checks whether search parameters have been entered into the advanced search form 
 * and returns an error message otherwise.
 */
/*
function checkSearch() {
	script = "index.php";
	criteria = "?option=search&params=" + document.advSearch.search_params.value +
		"&cat=" + document.advSearch.cid.value;

	// if search parameters field is blank
	if (document.advSearch.search_params.value.length == 0) {
		location1 = script + criteria + "&err=1";
		window.location.replace(location1); // returns an error message to the advanced search form
		return false;
	} else {
		return true;
	} // end if
}
*/
/** 
 * checkRegister() checks whether the registration form has been completed correctly. 
 * An error message is returned to the form otherwise.
 */
function checkRegister() {
	// check form fields for errors
	first_name_error = checkFirstName();
	last_name_error = checkLastName();
	address_error = checkAddress();
	city_error = checkCity();
	state_error = checkState();
	postcode_error = checkPostcode();
	phone_error = checkPhone();
	fax_error = checkFax();
	mobile_error = checkMobile();
	email_error = checkEmail();
	
	// send error messages back to form via the url
	script = "index.php";

	criteria = "?option=register&fname=" + 
		document.register.first_name.value + "&lname=" + document.register.last_name.value + 
		"&title=" + document.register.job_title.value + "&co=" + document.register.company.value +
		"&street=" + document.register.street.value + "&suburb=" + document.register.suburb.value +
		"&city=" + document.register.city.value + "&state=" + document.register.state.value +
		"&pcode=" + document.register.postcode.value + "&country=" + document.register.country.value +
		"&mail=" + document.register.mailing.checked + "&tel_acode=" + document.register.phone_area_code.value + 
		"&tel=" + document.register.phone.value + "&fax_acode=" + document.register.fax_area_code.value + 
		"&fax=" + document.register.fax.value + "&mob=" + document.register.mobile.value + 
		"&email=" + document.register.email.value + "&email2=" + document.register.email2.value;
	
	if (first_name_error != null) { // if first name input is erroneous
		location1 = script + criteria + "&fnameErr=" + first_name_error;
		
		if (last_name_error != null) { // if last name input is erroneous
			location1 += "&lnameErr=" + last_name_error;
		} // end if
		if (address_error != null) { // if street or suburb input is erroneous
			location1 += "&addressErr=" + address_error;
		} // end if
		if (city_error != null) { // if city input is erroneous
			location1 += "&cityErr=" + city_error;
		} // end if
		if (state_error != null) { // if state input is erroneous
			location1 += "&stateErr=" + state_error;
		} // end if
		if (postcode_error != null) { // if postcode input is erroneous
			location1 += "&pcodeErr=" + postcode_error;
		} // end if
		if (phone_error != null) { // if primary telephone number input is erroneous
			location1 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location1 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location1 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location1 += "&emailErr=" + email_error;
		} // end if
		
		// redirect to location1
		window.location.replace(location1);
		return false;
	} else if (last_name_error != null) { // if last name input is erroneous
		location2 = script + criteria + "&lnameErr=" + last_name_error;
		
		if (address_error != null) { // if street or suburb input is erroneous
			location2 += "&addressErr=" + address_error;
		} // end if
		if (city_error != null) { // if city input is erroneous
			location2 += "&cityErr=" + city_error;
		} // end if
		if (state_error != null) { // if state input is erroneous
			location2 += "&stateErr=" + state_error;
		} // end if
		if (postcode_error != null) { // if postcode input is erroneous
			location2 += "&pcodeErr=" + postcode_error;
		} // end if
		if (phone_error != null) { // if primary telephone number input is erroneous
			location2 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location2 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location2 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location2 += "&emailErr=" + email_error;
		} // end if
		
		// redirect to location2
		window.location.replace(location2);
		return false;
	} else if (address_error != null) { // if street or suburb input is erroneous
		location3 = script + criteria + "&addressErr=" + address_error;

		if (city_error != null) { // if city input is erroneous
			location3 += "&cityErr=" + city_error;
		} // end if
		if (state_error != null) { // if state input is erroneous
			location3 += "&stateErr=" + state_error;
		} // end if
		if (postcode_error != null) { // if postcode input is erroneous
			location3 += "&pcodeErr=" + postcode_error;
		} // end if
		if (phone_error != null) { // if primary telephone number input is erroneous
			location3 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location3 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location3 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location3 += "&emailErr=" + email_error;
		} // end if

		// redirect to location3 
		window.location.replace(location3);
		return false;
	} else if (city_error != null) { // if city input is erroneous
		location4 = script + criteria + "&cityErr=" + city_error;

		if (state_error != null) { // if state input is erroneous
			location4 += "&stateErr=" + state_error;
		} // end if
		if (postcode_error != null) { // if postcode input is erroneous
			location4 += "&pcodeErr=" + postcode_error;
		} // end if
		if (phone_error != null) { // if primary telephone number input is erroneous
			location4 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location4 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location4 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location4 += "&emailErr=" + email_error;
		} // end if

		// redirect to location4 
		window.location.replace(location4);
		return false;
	} else if (state_error != null) { // if state input is erroneous
		location5 = script + criteria + "&stateErr=" + state_error;

		if (postcode_error != null) { // if postcode input is erroneous
			location5 += "&pcodeErr=" + postcode_error;
		} // end if
		if (phone_error != null) { // if primary telephone number input is erroneous
			location5 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location5 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location5 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location5 += "&emailErr=" + email_error;
		} // end if

		// redirect to location5 
		window.location.replace(location5);
		return false;
	} else if (postcode_error != null) { // if postcode input is erroneous
		location6 = script + criteria + "&pcodeErr=" + postcode_error;

		if (phone_error != null) { // if primary telephone number input is erroneous
			location6 += "&telErr=" + phone_error;
		} // end if
		if (fax_error != null) { // if fax number input is erroneous
			location6 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location6 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location6 += "&emailErr=" + email_error;
		} // end if

		// redirect to location6 
		window.location.replace(location6);
		return false;
	} else if (phone_error != null) { // if primary telephone number input is erroneous
		location7 = script + criteria + "&telErr=" + phone_error;

		if (fax_error != null) { // if fax number input is erroneous
			location7 += "&faxErr=" + fax_error;
		} // end if
		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location7 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location7 += "&emailErr=" + email_error;
		} // end if

		// redirect to location7 
		window.location.replace(location7);
		return false;
	} else if (fax_error != null) { // if fax number input is erroneous
		location8 = script + criteria + "&faxErr=" + fax_error;

		if (mobile_error != null) { // if mobile/cell phone number input is erroneous
			location8 += "&mobErr=" + mobile_error;
		} // end if
		if (email_error != null) { // if email input is erroneous
			location8 += "&emailErr=" + email_error;
		} // end if

		// redirect to location8 
		window.location.replace(location8);
		return false;
	} else if (mobile_error != null) { // if mobile/cell phone number input is erroneous	
		location9 = script + criteria + "&mobErr=" + mobile_error;
		
		if (email_error != null) { // if email input is erroneous
			location9 += "&emailErr=" + email_error;
		} // end if
		
		// redirect to location9 
		window.location.replace(location9);
		return false;
	} else if (email_error != null) { // if email input is erroneous
		location10 = script + criteria + "&emailErr=" + email_error;
				
		// redirect to location10 
		window.location.replace(location10);
		return false;
	} else {  // no errors
		return true;
    } // end if
}

/** 
 * checkFirstName() checks whether the first name field has been completed.
 *
 * @return 1	
 */
function checkFirstName() {
	if (document.register.first_name.value.length == 0) { // if first name field is blank
		//alert("Please enter your first name");
		return 1;
	} // end if
}

/** 
 * checkLastName() checks whether the last name field has been completed.
 *
 * @return 1	
 */
function checkLastName() {
	if (document.register.last_name.value.length == 0) { // if last name field is blank
		//alert("Please enter your last name");
		return 1;
	} // end if
}

/**
 * checkAddress() checks whether the street and suburb fields have been completed
 *
 * @return 1
 */
function checkAddress() {
	if (document.register.street.value.length == 0 || 
		document.register.suburb.value.length == 0) { // if street or suburb fields are blank
		//alert("Please enter your street address");
		return 1;
	} // end if
}

/**
 * checkCity() checks whether the city field has been completed
 *
 * @return 1
 */
function checkCity() {
	if (document.register.city.value.length == 0) {
		//alert("Please enter your city");
		return 1;
	} // end if
}

/**
 * checkState() checks whether the state field has been completed
 *
 * @return 1
 */
function checkState() {
	if (document.register.state.value.length == 0) {
		//alert("Please enter your state or province");
		return 1;
	}
}

/**
 * checkPostcode() checks whether the postcode field has been completed
 *
 * @return 1
 */
function checkPostcode() {
	if (document.register.postcode.value.length == 0) {
		//alert("Please enter your ZIP or postcode");
		return 1;
	}
}

/**
 * checkPhone() checks whether the primary telephone area code and telephone number fields have been completed and contain 
 * numeric characters
 *
 * @return 	error	integer value determining error message
 */
function checkPhone() {

	if (document.register.phone.value.length == 0) { // if phone field is blank
		//alert("Please enter a primary telephone number");
		var error = 1;
	// if phone area code field is not blank and phone area code and phone fields contain non-numeric characters
	} else if ((document.register.phone_area_code.value.length > 0 && !isInteger(document.register.phone_area_code.value)) &&
		!isInteger(document.register.phone.value)) {
		//alert("The primary telephone number entered is incorrect");
		var error = 2;
	// if phone area code field is not blank and contains non-numeric characters
	} else if (document.register.phone_area_code.value.length > 0 && !isInteger(document.register.phone_area_code.value)) { 
		//alert("The primary telephone area code entered is incorrect");
		var error = 3;
	// if phone field contains non-numeric characters
	} else if (!isInteger(document.register.phone.value)) {
		//alert("The primary telephone number entered is incorrect");
		var error = 4;
	} 
	return error;
}

/**
 * checkFax() checks whether the fax area code and fax number fields contain numeric characters if they 
 * have been completed
 * 
 * @return 	error	integer value determining error message
 */
function checkFax() {
	// if fax area code or fax number fields have been completed
	if (document.register.fax_area_code.value.length > 0 || document.register.fax.value.length > 0) {
		if (document.register.fax.value.length == 0) { // if fax number field is blank
			//alert("Please enter a fax number");
			var error = 1;
		// if fax area code field is not blank and fax area code and fax fields contain non-numeric characters 
		} else if ((document.register.fax_area_code.value.length > 0 && !isInteger(document.register.fax_area_code.value)) && 
			!isInteger(document.register.fax.value)) {
			//alert("The fax number entered is incorrect");
			var error = 2;
		// if fax area code field is not blank and contains non-numeric characters
		} else if (document.register.fax_area_code.value.length > 0 && !isInteger(document.register.fax_area_code.value)) { 
			//alert("The fax area code entered is incorrect");
			var error = 3;
		// if fax field contains non-numeric characters
		} else if (!isInteger(document.register.fax.value)) {
			//alert("The fax number entered is incorrect");
			var error = 4;
		} // end if
	} // end if 
	return error;
}

/**
 * checkMobile() checks whether the mobile/cell phone number field has been completed and contains
 * numeric characters
 *
 * @return	error	integer value determining error message
 */
function checkMobile() {
	if (document.register.mobile.value.length > 0) {
		if (!isInteger(document.register.mobile.value)) {
			//alert("The cell or mobile phone number entered is incorrect");
			var error = 1;
		}
	}
	return error;
}

/** 
 * checkEmail() checks whether the email field has been completed, the email
 * address has the correct form and the email fields match.
 *
 * @return 	error	integer value determining error message
 */
function checkEmail() {
	if (document.register.email.value.length == 0) { // if email field is blank
		//alert("Please enter your email address");
		var error = 1;
	} else if (document.register.email.value.search(
		"[a-zA-Z0-9][a-zA-Z0-9._]*@[a-z]+(.[a-z]+)*") == -1) { // if email address has the incorrect form
		// alert("You have entered an incorrect email address");
		var error = 2;
	// if email field has been completed & re-enter email field is blank
	} else if (document.register.email.value.length > 0 &&
		document.register.email2.value.length == 0) {
		//alert("Please re-enter your email address");
		var error = 3; 
	// if email & re-enter email fields don't match
	} else if (document.register.email.value != document.register.email2.value) { 
		//alert("Your email entries must match");
		var error = 4;
	} // end if
	
	return error;
}

/** 
 * checkRegister2() checks whether the second registration form has been completed correctly. 
 * An error message is returned to the form otherwise.
 */
function checkRegister2() {
	// character minimum for username and password
	var min_length = 6;
	// character maximum for username and password
	var max_length = 20;

	// check form fields for errors
	username_error = checkUsername("register2", min_length, max_length);
	password_error = checkPassword("register2", min_length, max_length);
	question_error = checkQuestion();
	answer_error = checkAnswer("register2");

	// send error messages back to form via the url
	script = "index.php";
	
	criteria = "?option=register2&id=" +  document.register2.id.value + 
	"&uname=" + document.register2.username.value + 
	"&q=" + document.register2.question.value + "&a=" + document.register2.answer.value + 
	"&min=" + min_length + "&max=" + max_length;

	if (username_error != null) {
		location1 = script + criteria + "&unameErr=" + username_error;

		if (password_error != null) {
			location1 += "&passErr=" + password_error;
		}
		if (question_error != null) {
			location1 += "&qErr=" + question_error;
		}
		if (answer_error != null) {
			location1 += "&aErr=" + answer_error;
		}

		// redirect to location1
		window.location.replace(location1);
		return false;
	} else if (password_error != null) {
		location2 = script + criteria + "&passErr=" + password_error;

		if (question_error != null) {
			location2 += "&qErr=" + question_error;
		}
		if (answer_error != null) {
			location2 += "&aErr=" + answer_error;
		}

		// redirect to location2
		window.location.replace(location2);
		return false;
	} else if (question_error != null) {
		location3 = script + criteria + "&qErr=" + question_error;

		if (answer_error != null) {
			location3 += "&aErr=" + answer_error;
		}

		// redirect to location3
		window.location.replace(location3);		
		return false;
	} else if (answer_error != null) {
		location4 = script + criteria + "&aErr=" + answer_error;

		// redirect to location4
		window.location.replace(location4);
		return false;
	} else { // no errors
		return true;
	}
}

/** 
 * checkUsername(form, min_length, max_length) checks whether the username field has been 
 * completed and the username is between min_length and max_length characters.
 *
 * @param	form		the form requesting input validation
 * @param	min_length	character minimum for username
 * @param	max_length	character maximum for username
 * @return 	error		integer value determining error message
 */
function checkUsername(form, min_length, max_length) {
	if (document.forms[form].username.value.length == 0) { // if username field is blank
		//alert("Please enter a username");
		var error = 1;
	// if username is less than min_length characters
	} else if (document.forms[form].username.value.length < min_length) { 
		//alert("Please enter a username of " + min_length + " or more characters");
		var error = 2;
	// if username is more than max_length characters
	} else if (document.forms[form].username.value.length > max_length) {
		//alert("Please enter a username of less than " + max_length + " characters");
		var error = 3;
	}
	
	return error;
}

/** 
 * checkPassword(form, min_length, max_length) checks whether the password field has been 
 * completed, the password is between min_length and max_length characters and password entries match
 *
 * @param	form		the form requesting input validation
 * @param	min_length	character minimum for password
 * @param	max_length	character maximum for password
 * @return 	error		integer value determining error message
 */
function checkPassword(form, min_length, max_length) {
	// if password field is blank
	if (document.forms[form].password.value.length == 0) {
		//alert("Please enter a password");
		var error = 1;
	// if length of password is less than min_length
	} else if (document.forms[form].password.value.length < min_length) {
		//alert("Please enter a password of " + min_length + " or more characters");
		var error = 2;
	// if length of password is more than max_length
	} else if (document.forms[form].password.value.length > max_length) {
		//alert("Please enter a password of less than " + max_length + " characters");
		var error = 3;
	} else if (form == "resetPwd3" && document.forms[form].password2.value.length == 0) {
		// alert("Please re-enter your new password");
		var error = 4;
	} else if ((form == "resetPwd3") && 
		(document.forms[form].password.value != document.forms[form].password2.value)) {
		// alert("Your password entries must match");
		var error = 5;
	}
	
	return error;
}

/**
 * checkQuestion() checks whether a secret question has been selected
 *
 * @return 1
 */
function checkQuestion() {
	if (document.register2.question.value == 0) {
		//alert("Please select a secret question");
		return 1;
	}
}

/**
 * checkAnswer(form) checks whether a secret answer has been entered
 *
 * @param	form		the form requesting input validation
 * @return 1
 */
function checkAnswer(form) {
	if (document.forms[form].answer.value.length == 0) {
		//alert("Please enter an answer to the secret question");
		return 1;
	} 
}
/**
 * checkResetPwd() checks whether the username field in the reset password form has been 
 * completed correctly. An error message is returned otherwise.
 */
function checkResetPwd() {
	// character minimum for username
	var min_length = 6;
	// character maximum for username
	var max_length = 20;

	// check form fields for errors
	username_error = checkUsername("resetPwd", min_length, max_length);

	script = "index.php";
	criteria = "?option=reset_password&uname=" + document.resetPwd.username.value + 
		"&min=" + min_length + "&max=" + max_length;

	if (username_error != null) { // if username field is erroneous
		location1 = script + criteria + "&unameErr=" + username_error;
		
		// redirect to location1
		window.location.replace(location1);
		return false;
	} else { // no errors
		return true;
	} // end if
}

/**
 * checkResetPwd() checks whether the second reset password form has been completed.
 * An error message is returned otherwise.
 */
function checkResetPwd2() {
	answer_error = checkAnswer("resetPwd2");

	script = "index.php";
	criteria = "?option=reset_password2&id=" + document.resetPwd2.id.value + 
		"&a=" + document.resetPwd2.answer.value;	

	if (answer_error != null) { // answer field is erroneous
		location1 = script + criteria + "&aErr=" + answer_error;

		// redirect to location1
		window.location.replace(location1);
		return false;
	} else { // no errors
		return true;
	}
}

function checkResetPwd3() {
	// character minimum for password
	var min_length = 6;
	// character maximum for password
	var max_length = 20;

	password_error = checkPassword("resetPwd3", min_length, max_length);

	script = "index.php";
	criteria = "?option=reset_password3&id=" + document.resetPwd3.id.value + 
		"&min=" + min_length + "&max=" + max_length;

	if (password_error != null) { // password field/s are erroneous
		location1 = script + criteria + "&passErr=" + password_error;

		// redirect to location1
		window.location.replace(location1);
		return false;
	} else { // no errors
		return true;
	} // end if
}

/**
 * checkEmailList() checks whether all required fields in the email shortlist form have
 * been completed. An error message is returned otherwise.
 */
function checkEmailList() {
	recipient_error = checkRecipient();
	name_error = checkName();
	sender_error = checkSender();

	script = "index.php";
	criteria = "?option=email_list&id=" + document.emailList.id.value + "&recip=" + 
		document.emailList.recipient.value + "&name=" + document.emailList.name.value + 
		"&sender=" + document.emailList.sender.value + "&msg=" +
		document.emailList.message.value;

	if (recipient_error != null) { // if recipient field is erroneous
		location1 = script + criteria + "&recipErr=" + recipient_error;

		if (name_error != null) { // if name field is erroneous
			location1 += "&nameErr=" + name_error;
		}
		if (sender_error != null) { // if sender field is erroneous
			location1 += "&senderErr=" + sender_error;
		}
		// redirect to location1
		window.location.replace(location1);
		return false;
	} else if (name_error != null) { // if name field is erroneous
		location2 = script + criteria + "&nameErr=" + name_error;

		if (sender_error != null) { // if sender field is erroneous
			location2 += "&senderErr=" + sender_error;
		}
		// redirect to location2
		window.location.replace(location2);
		return false;
	} else if (sender_error != null) { // if sender field is erroneous
		location3 = script + criteria + "&senderErr=" + sender_error;

		// redirect to location3
		window.location.replace(location3);
		return false;
	} else { // no errors
		return true;
	} // end if
}

function checkRecipient() {
	if (document.emailList.recipient.value.length == 0) { // if recipient field is blank
		//alert("Please enter the recipient's email address");
		var error = 1;
	} else if (document.emailList.recipient.value.search(
		"[a-zA-Z0-9][a-zA-Z0-9._]*@[a-z]+(.[a-z]+)*") == -1) { // if recipient field has the incorrect form
		// alert("You have entered an incorrect recipient email address");
		var error = 2;
	}
	return error;
}

function checkSender() {
	if (document.emailList.sender.value.length == 0) { // if sender field is blank
		//alert("Please enter your email address");
		var error = 1;
	} else if (document.emailList.sender.value.search(
		"[a-zA-Z0-9][a-zA-Z0-9._]*@[a-z]+(.[a-z]+)*") == -1) { // if sender field has the incorrect form
		// alert("You have entered an incorrect email address");
		var error = 2;
	}
	return error;
}

function checkName() {
	if (document.emailList.name.value.length == 0) { // if name field is blank
		//alert("Please enter your name");
		return 1;
	} // end if
}

function isInteger(inputVal) {
    inputStr = inputVal.toString();
	for (var i = 0; i < inputStr.length; i++) {
	    var oneChar = inputStr.charAt(i);
	    if ((oneChar < "0" || oneChar > "9") && oneChar != " ") {
		    return false;
		}
	}
	return true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}