// JavaScript Document
function validatePhone(number) {
/*	new version by Laurence 2004 Nov 26
	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches groups of 3, 3 and 4 digits found in that order anywhere in the string.
*/
	var myRegExp = /(\d{3}).*(\d{3}).*(\d{4})/;	
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
	//	number = "(" + RegExp.$1 + ")" + RegExp.$2 + "-" + RegExp.$3; this would format as (000)000-0000
		return true;
	} else {
		return false;
	}
}

function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
//alert("Date is not in a valid format.")
return false;
}
var month = matchArray[1]; // parse date into variables
var day = matchArray[3];
var year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

function validateInternationalPhone(number) {
/*	by Laurence 2005 Apr 29
	similar to validatePhone(), above
	The pattern matches a total of 10 digits found anywhere in the string; it's better than nothing...
*/
	var myRegExp = /(\d.*){9,}\d/;
	var sPos = number.search(myRegExp);
	if ( sPos >= 0 ) {
		return true;
	} else {
		return false;
	}
}

function validateEmail(email) {
/*	new version by Laurence 2004 Nov 26
	revised 2005 Nov 04 by Laurence: allow hyphens before @ symbol; rename 'verifyEmail' to 'validateEmail'

	RegExp is a Javascript1.2 feature; tested successfully in WinIE 5.0+, MacIE 5.1, Safari 1.0, WinNN 4.7, WinNN 6.2, WinMoz 1.7
	The pattern matches: 
	- at least one word character ( a-zA-Z0-9_ )
	- followed by any number more including periods and hyphens
	- then a single @
	- then at least one alphanumeric character plus any number more including hyphens and periods
	- plus an ending of an alphanumeric char, a single period, and at least 2 more letters. 
	There can be nothing before or after the pattern; all whitespace, punctuation, etc. not explicitly allowed is forbidden.
*/
	var myRegExp = /^[\w][\w\.\-]*@[a-zA-Z0-9]+[\w\.\-]*[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
	var sPos = email.search(myRegExp);
	if (sPos >= 0) {
		return true;
	} else {
		return false;
	}
}

function isPostCode(entry){ // CANADIAN CODES ONLY
	var strlen = entry.length; 
	if (strlen != 7) {return false;}
	entry=entry.toUpperCase();        // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if ('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(1)) < 0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(4)) < 0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(5)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(6)) < 0) {return false;}
	
	return true; 
}

function validateStep1(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	if ((theForm.membershipType[0].checked == true) && ((theForm.MemberType[0].checked == false) && (theForm.MemberType[1].checked == false))) {
		emptyFields += "You must select either arbitrator or mediator to become a member." + "\n" ;
	}

	if (theForm.FirstName.value == ""){
		emptyFields += "The First Name cannot be blank." + "\n" ;
	} 
	
	 if (theForm.LastName.value == ""){
		emptyFields += "The Last Name cannot be blank." + "\n" ;
	} 
	
	if (theForm.Address.value == ""){
		emptyFields += "The Address cannot be blank." + "\n" ;
	} 
	
	if (theForm.City.value == ""){
		emptyFields += "The City cannot be blank." + "\n" ;
	} 
	
	if (theForm.fkProvinceId.value == ""){
		emptyFields += "You must select a province." + "\n" ;
	}
	
	if (theForm.PostalCode.value == ""){
		emptyFields += "The Postal Code cannot be blank." + "\n" ;
	} else if (!isPostCode(theForm.PostalCode.value)) {
		emptyFields += "The postal code must be correctly formatted." + "\n" ;
	}
	
	if (theForm.Phone.value == ""){
		emptyFields += "The Phone cannot be blank." + "\n" ;
	}
	
	 if (theForm.Email.value == ""){
		emptyFields += "The Email cannot be blank." + "\n" ;
	} else if (!validateEmail(theForm.Email.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if(theForm.Email2.value != ""){
		if (!validateEmail(theForm.Email2.value)) {
		emptyFields += "The secondary email address must be correctly formatted." + "\n" ;
		}
	}
		
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}

function validateContactInfo(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	if (theForm.FirstName.value == ""){
		emptyFields += "The First Name cannot be blank." + "\n" ;
	} 
	
	 if (theForm.LastName.value == ""){
		emptyFields += "The Last Name cannot be blank." + "\n" ;
	} 
	
	if (theForm.Username.value == ""){
		emptyFields += "The Username cannot be blank." + "\n" ;
	} 
	
	 if (theForm.Password.value == ""){
		emptyFields += "The Password cannot be blank." + "\n" ;
	} 
	
	if (theForm.PasswordConfirm.value == ""){
		emptyFields += "You must confirm your password." + "\n" ;
	} 
	
	if (theForm.Password.value != theForm.PasswordConfirm.value){
		emptyFields += "Your password and confirmation password do not match." + "\n" ;
	}
	
		
	if (theForm.Address.value == ""){
		emptyFields += "The Address cannot be blank." + "\n" ;
	} 
	
	if (theForm.City.value == ""){
		emptyFields += "The City cannot be blank." + "\n" ;
	} 
	
	if (theForm.fkProvinceId.value == ""){
		emptyFields += "You must select a province." + "\n" ;
	}
	
/*	
	if (theForm.fkGeographicalId.value == ""){
		emptyFields += "You must select a primary service area." + "\n" ;
	}
*/

	if (theForm.PostalCode.value == ""){
		emptyFields += "The Postal Code cannot be blank." + "\n" ;
	} else if (!isPostCode(theForm.PostalCode.value)) {
		emptyFields += "The postal code must be correctly formatted." + "\n" ;
	}

	if (theForm.Country.value == ""){
		emptyFields += "The Country cannot be blank." + "\n" ;
	}
	
	if (theForm.Phone.value == ""){
		emptyFields += "The Phone cannot be blank." + "\n" ;
	} else if (!validatePhone(theForm.Phone.value)){
		emptyFields += "The Phone must be correctly formatted." + "\n" ;
	}
	
	if (theForm.Fax.value != "" && !validatePhone(theForm.Fax.value)){
		emptyFields += "The Fax must be correctly formatted." + "\n" ;
	}
	
	if (theForm.TollFreeNumber.value != "" && !validatePhone(theForm.TollFreeNumber.value)){
		emptyFields += "The Toll Free Number must be correctly formatted." + "\n" ;
	}
	
	if (theForm.Cell.value != "" && !validatePhone(theForm.Cell.value)){
		emptyFields += "The Cellphone must be correctly formatted." + "\n" ;
	}

	 if (theForm.Email.value == ""){
		emptyFields += "The Email cannot be blank." + "\n" ;
	} else if (!validateEmail(theForm.Email.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if(theForm.Email2.value != ""){
		if (!validateEmail(theForm.Email2.value)) {
		emptyFields += "The secondary email address must be correctly formatted." + "\n" ;
		}
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}

function validateStep2(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	if((theForm.DesignationDate1.value != "") && (!isValidDate(theForm.DesignationDate1.value))){
			emptyFields += "Please ensure that your first designation date is in the format mm/dd/yy." + "\n" ;
	}
	
	if((theForm.DesignationDate2.value != "") && (!isValidDate(theForm.DesignationDate2.value))){
			emptyFields += "Please ensure that your second designation date is in the format mm/dd/yy." + "\n" ;
	}
	
	if((theForm.DesignationDate3.value != "") && (!isValidDate(theForm.DesignationDate3.value))){
			emptyFields += "Please ensure that your third designation date is in the format mm/dd/yy." + "\n" ;
	}
	
	if((theForm.DesignationDate4.value != "") && (!isValidDate(theForm.DesignationDate4.value))){
			emptyFields += "Please ensure that your fourth designation date is in the format mm/dd/yy." + "\n" ;
	}
	
	if((theForm.DesignationDate5.value != "") && (!isValidDate(theForm.DesignationDate5.value))){
			emptyFields += "Please ensure that your fifth designation date is in the format mm/dd/yy." + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
}

function validateForgot(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	 if (theForm.Email.value == ""){
		emptyFields += "The Email cannot be blank." + "\n" ;
	} else if (!validateEmail(theForm.Email.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
	
}

function validateLogin(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	
	 if (theForm.username.value == ""){
		emptyFields += "The Email cannot be blank." + "\n" ;
	} else if (!validateEmail(theForm.username.value)) {
		emptyFields += "The email address must be correctly formatted." + "\n" ;
	}

	if (theForm.password.value == ""){
		emptyFields += "The Password cannot be blank." + "\n" ;
	}
	
	if (emptyFields.length >= 1) {
		alert("Please ensure that the following fields are completed correctly:" + "\n" + emptyFields);
		return false;
	} else {
		return true;
	}
	
}


function genericConfirmSubmit(confirmString)
{
var agree=confirm(confirmString);
if (agree)
	return true ;
else
	return false ;
}

function forceSaveContact(theFormContactInfo)
{
	var theForm = theFormContactInfo;
	if (validateContactInfo(theForm))
		{
		theForm.submit();	
		return true;
		}
	else {
		return false;
	}
}

function forceSaveExperience(theFormExperience)
{
	var theForm = theFormExperience;
	theForm.submit();
	return true;
/*
--- All validation is removed

	var theForm = theFormExperience;
	if (validateStep2(theForm))
		{
//		alert('SAVE THE FORM');
		theForm.submit();
		return true;
		}
	else {
		return false;
	}
*/	
}

function forceSaveAnyForm(theFormObj)
{
	var theForm = theFormObj;
	theForm.submit();
	return true;
}

function forceSaveProfile(theFormObj)
{
	var theForm = theFormObj;
	theForm.submit();
	return true;
}

function validateAgreement(thisForm) {
	var theForm = thisForm;
	var emptyFields = "";
	//alert(theForm.acceptAgreement.checked);
	 if (theForm.acceptAgreement.checked){
		return true;
	} else {
		alert("You have to Accept the agreement to be able to download the resources files.");
		return false;
	}
		
}


