// JavaScript Document
var problem=0

function validateForwardToFriend(){
	var lvstrMessage = "The form could not be submitted for the following reasons:\n";
	with (document.main){		
		//lvstrMessage+=warnIfEmpty("FullName");
		//lvstrMessage+=warnIfEmpty("MailingAddress");
		if (yourName.value==""){
			problem++
			lvstrMessage+="\nYour name is required."
		}
		if (!verifyEmail(yourEmail.value)){
			problem++
			lvstrMessage+="\nYour valid email address is required."
		}
		if (friendName.value==""){
			problem++
			lvstrMessage+="\nYour friend's name is required."
		}
		if (!verifyEmail(friendEmail.value)){
			problem++
			lvstrMessage+="\nYour friend's valid email address is required."
		}		
		if (problem > 0){
			alert(lvstrMessage);
			problem=0;
			return false;
		}
		else{
			return true;
		}
	}
}

function warnIfEmptySelect(paramField){
//Where paramField is the name of a Select element and
//it's inside a form named "Main".
	var selectedIndex = document.Main[paramField].options.selectedIndex
		if (document.Main[paramField].options[selectedIndex].value==""){
			problem++
			var lvstrwarning = "\n" + paramField + " is required.";
		}
		else{
			lvstrwarning="";
		}
		return lvstrwarning;
}

function warnIfEmptySelectIndicated(paramField){
//Where paramField is the name of a Select element and
//it's inside a form named "Main".
	var selectedIndex = document.Main[paramField].options.selectedIndex
		if (document.Main[paramField].options[selectedIndex].value==""){
			problem++
			var lvstrwarning = "\n" + paramField + " must be indicated.";
		}
		else{
			lvstrwarning="";
		}
		return lvstrwarning;
}

function warnIfEmpty(paramField){
//Where paramField is the name of a text or textarea element and
//it's inside a form named "Main".
	alert(document.Main[paramField].value);
	if(document.Main[paramField].value==""){
		var lvstrwarning = "\n" + paramField + " is required.";
		problem++
	}
	else{
		lvstrwarning="";
	}
	return lvstrwarning;	 
}

function verifyEmail(addr) { 
    var atPos = addr.indexOf('@'); 
    var dotPos = addr.lastIndexOf('.') 
    if (atPos>0 && dotPos>atPos+1) return true; 
    else
    	return false;
} 

function validateCheckBox(paramBox){
	var lvstrWarning = ""
	if (document.Main[paramBox].value !="yes" && document.Main[paramBox].value !="no"){
		problem++
		lvstrWarning = "\nState registration status must be indicated."
	}
	return lvstrWarning;
	//return document.Main[paramBox].checked
}

function validateRegistrationNZ(){
	var message = "Your application could not be submitted for the following reasons:\n";
	with (document.Main){		
		//message+=warnIfEmpty("FullName");
		//message+=warnIfEmpty("MailingAddress");
		if (FirstName.value==""){
			problem++
			message+="\nFirst name is required."
		}
		if (LastName.value==""){
			problem++
			message+="\nLast name is required."
		}
		if (AddressStreet.value==""){
			problem++
			message+="\nStreet address is required."
		}
		if (AddressCity.value==""){
			problem++
			message+="\nCity is required."
		}
		if (AddressCountry.value==""){
			problem++
			message+="\nCountry is required."
		}
		if (!verifyEmail(ReturnEmailAddress.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (problem > 0){
			alert(message);
			problem=0;
			return false;
		}
		else{
			return true;
		}
	}
}

function validateRegistration(){
	with (document.Main){
		var message = "Your application could not be submitted for the following reasons:\n";
		message+=warnIfEmpty("Title");
		message+=warnIfEmpty("FirstName");
		message+=warnIfEmpty("LastName");
		message+=warnIfEmpty("Address1");
		message+=warnIfEmpty("Town");
		message+=warnIfEmpty("Country");
		message+=warnIfEmpty("PostCode");
		if (!verifyEmail(Email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		message+=warnIfEmptySelect("Profession");
		message+=warnIfEmpty("CurrentGrade");
		message+=warnIfEmpty("GeographicalArea");
		message+=warnIfEmpty("Experience");
		message+=warnIfEmpty("Qualifications");
		//if(!validateCheckBox("StateRegistration")){
		//	problem++
		//	message+="\nState registration status must be indicated."
		//}
		message+=warnIfEmptySelectIndicated("StateRegistration");
		message+=warnIfEmptySelectIndicated("RightToWorkInTheUK");
		message+=warnIfEmpty("GraduationDate");
		message+=warnIfEmpty("AvailableFrom");
		if (problem > 0){
			alert(message);
			problem=0;
		}
		else{
			document.Main.submit();
		}
	}
}


function checkBoxes(action){
	var aCBList = new Array()
	var currentPos = 0
	for(var i = 0; i < document.forms[0].length; i++) {
		vType = document.forms[0].elements[i].type;
		vName = document.forms[0].elements[i].name;
		//alert(document.forms[0].elements[i].checked);
		var t = new String(vName);
		//alert(document.forms[0].elements[i].value);
			if((vType == 'checkbox') && (t.indexOf('delete') != -1) && (document.forms[0].elements[i].checked==true)){
				// Append the form element index value to the end of the array
				aCBList[currentPos++] = i;
			}
	}
	//alert(aCBList);
	if (aCBList.length<1){
		if (action=="modify"){
			alert("You must select a booking to modify.");
			return true;
		}
		if (action=="delete"){
			alert("You must select a booking to delete.");
			return true;
		}
	}
	if (aCBList.length>1 && action=="modify"){
		alert("Select only one booking to modify at a time.");
		return true;
	}
	if (aCBList.length>1 && action=="delete"){
		alert("Select only one booking to delete at a time.");
		return true;
	}
	else{
		return false;
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}