<!--
/******************************************************************************
 *	Name:			validation.js
 *	Date:			29 July 2002
 *	Author:			Daniel Toomey
 *
 *	Description:	Form validation javascript for NRSO web
 *
 ******************************************************************************
 *	MODIFICATION HISTORY
 ******************************************************************************
 *	DATE			CHANGES
 *-----------------------------------------------------------------------------
 *
 *****************************************************************************/

var PHONE = "phone";
var FAX = 	"fax";
var EMAIL = "e_mail";
var POST = "post";

function validateForm(frm)
{
	var choice = null;

	/* First check if minimum field requirements are met */
	if (frm.firstname.value.length == 0) {
		alert('You must provide a first name or initial.');
		frm.firstname.focus();
		return false;
	} else if (frm.lastname.value.length == 0) {
		alert('You must provide a last name.');
		frm.lastname.focus();
		return false;
	}

	/* Determine what contact method user has chosen */
	for (i=0; i<frm.contactMethod.length; i++) {
		if (frm.contactMethod[i].checked) {
			choice = frm.contactMethod[i].value;
			break;
		}
	}

	/* Now validate the specific section of the form
	   according to the user's preferred contact method. */
	switch (choice) {

		case "e_mail":
			return validateEmail(frm.e_mail);
			break;

		case "phone":
		case "fax":
			return validatePhone(frm, choice);
			break;

		case "post":
			return validateAddress(frm);
			break;

		default:
			alert('Page error: invalid contact method submitted.');
			return false;
	}

}


function validateEmail(formField) {

  /*
    A regular expression is constructed to determine that line entered
    email address conforms to the format: x.@x.x where x represents a string,
    and which can also include more dots ("."). For example:

    mick.mollerup@accenture.com
    dis@sam.sdu.dk

    are two examples on valid email addresses.
  */

    var email = formField.value;

    var emailRE = /^((\w+)(\.\w+)*)@((\w+)(\.\w+)+)$/

    if (email.search(emailRE) == -1) {
    	alert("Please enter a valid e-mail address, or choose a different "
    		+ "contact method.\nA valid e-mail address has this "
    		+ "format:\n\nsomeone@someplace.com");
    	formField.focus();
    	return false;
    }

}

function validatePhone(frm, fieldType) {

	/* Area code and number are stored in separate fields */
	var areaCode;
	var number;
	var tail = " or choose a different contact method.";

	/* This function is used to validate both phone and fax numbers
	   in Australia. Determine which type we are working with.   */
	if (fieldType ==  "phone") {

		areaCode = frm.phoneAC;
		number = frm.phoneNum;

	} else if (fieldType == "fax") {

		areaCode = frm.faxAC;
		number = frm.faxNum;

	} else {

		alert("Page error: invalid phone field passed.");
		return false;

	}

	/* Now check for a valid Australian area code and number */
	if (areaCode.value.length != 2 || areaCode.value.search(/\d\d/) == -1) {

		alert("Please enter a valid 2-digit area code number," + tail);
		areaCode.focus();
		return false;

	} else if (number.value.length != 8 ||
				number.value.search(/\d\d\d\d\d\d\d\d/) == -1) {

		alert("Please enter a valid 8-digit " + fieldType + " number," + tail);
		number.focus();
		return false;

	}

	/* If we've made it this far, then all is well */
	return true;

}

function validateAddress(frm) {

	var add1 = frm.address1.value;
	var add2 = frm.address2.value; //not validated
	var sub  = frm.suburb.value;
	var pc   = frm.postcode.value;
	var tail = " or choose a different contact method.";

	/* Now check that the text fields contain at least 2 characters */
	if (add1.length < 2 ) {

		alert("Please enter a valid postal or street address," + tail);
		frm.address1.focus();
		return false;

	} else if (sub.length < 2) {

		alert("Please enter a valid suburb," + tail);
		frm.suburb.focus();
		return false;

	}

	/* Now validate the postcode (should be exactly four digits) */
    if ((pc.length != 4) || (pc.search(/\d{4}/) == -1)) {

      	alert("Please enter a valid four digit post code," + tail);
      	frm.postcode.focus();
      	return false;

	}

	/* If we've made it this far, then all is well */
	return true;

}

function changeStars(formField) {


	/* 	Show/hide the required field stars according to
		the newly selected preferred delivery method.  */

	var selection = formField.value;

	/* Hide all the stars to begin with */
	hide("emailstar");

	if (getElement("poststar1")) {
		hide("poststar1");
		hide("poststar2");
		hide("poststar3");
		hide("poststar4");
	}

	if (getElement("faxstar")) {
		hide("faxstar");
		hide("phonestar");
	}


	/* Now turn on the correct star(s) */
	switch (selection) {

		case "e_mail":

			show("emailstar");
			break;

		case "phone":

			show("phonestar");
			break;

		case "fax":

			show("faxstar");
			break;

		case 'post':

			show("poststar1");
			show("poststar2");
			show("poststar3");
			show("poststar4");

		default:

			//error: do nothing
			return false;
	}


	return true;
}

//-->
