// whole form check
function checkWholeForm(theForm) {
    var why = "";
	why += checkFName(theForm.firstname.value);
	why += checkTitle(theForm.title.selectedIndex);
	why += checkSName(theForm.surname.value);
	for (i=0, n=theForm.gender.length; i<n; i++) {
		if (theForm.gender[i].checked) {
			var checkvalue = theForm.gender[i].value;
			break;
		} 
	}
	why += checkGender(checkvalue);	
	why += checkDate(theForm.dobday.selectedIndex);
	why += checkMonth(theForm.dobmonth.selectedIndex);
	why += checkYear(theForm.dobyear.selectedIndex);
	why += checkEmail(theForm.email.value);
	why += checkAddress(theForm.address.value);
	why += checkSuburb(theForm.suburb.value);
	why += checkState(theForm.state.value);
	why += checkPostcode(theForm.postcode.value);
	why += checkPhone(theForm.phone.value);
	for (i=0, n=theForm.phone_type.length; i<n; i++) {
		if (theForm.phone_type[i].checked) {
			 var checkvalue = theForm.phone_type[i].value;
			 break;
		} 
	}
	why += checkPhoneType(checkvalue);
	why += checkUsername(theForm.userid.value);
	
	
	if (why != "") {
		alert("There was a problem with your registration:\n\n"+ why);
		return false; 
	}
return true;
}


// individual validation routines
function checkFName (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your first name.\n";
	}
return error;
}

function checkSName (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your surname.\n";
	}
return error;
}

function checkGender(checkvalue)  {
var error = "";
   if (!(checkvalue)) {
       error = "You didn't enter your gender.\n";
    }
return error;    
}


function checkAddress (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your address.\n";
	}
return error;
}

function checkSuburb (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your suburb.\n";
	}
return error;
}

function checkState (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your state.\n";
	}
return error;
}

function checkPostcode (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter your postcode.\n";
	}
return error;
}

function checkPhone (strng) {
var error = "";
var stripped = strng.replace(/[\(\)\.\-\ ]/g,'');
//if (stripped.length == 0) {
//	error = "You didn't enter a phone number.\n";
//	return error;
//}
if (stripped.length < 8) {
	error = "The phone number is the wrong length,\n"
	+ "please make sure you enter the area code.\n";
	return error;
}
//if (isNaN(parseInt(stripped))) {
//   error = "The phone number entered contains incorrect characters.";
//	}
return error;
}

function checkPhoneType(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "You didn't select the phone number type.\n";
    }
return error;    
}

function checkDate(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't enter your date of birth.\n";
    }    
return error;
} 

function checkMonth(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't enter your month of birth.\n";
    }    
return error;
} 

function checkYear(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't enter your year of birth.\n";
    }    
return error;
}

function checkTitle(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't select your title (Mr/Mrs/Ms/Miss/Dr).\n";
    }    
return error;
} 

function checkUsername(strng) {
	var error = "";
	var illegalChars = /\W/;
	// allow only letters, numbers, and underscores
	if (strng == "") {
   	error = "You didn't enter a username.\n";
	return error;
	}
if (illegalChars.test(strng)) {
    error = "Your username contains illegal characters.\n";
	return error;
    }
if ((strng.length < 4) || (strng.length > 25)) {
    error = "The username must be between 4 and 25 characters long.\n";
	}
return error;
}

function checkEmail (strng) {
var error = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (strng =="") {
		error = "You didn't enter an email address.\n";
		return error;
	} 
	if (!(emailFilter.test(strng))) { 
		error = "Please enter a valid email address.\n";
	}
return error;
}
