//---------------------------
//validateForm
//Purpose: Called before submitting the form to the server in order to validate the user has filled in all required fields
//Notes: This is a copy of the noEntry() function from functions.js with some modifications for adding ResidentCentral specific validation
//---------------------------

function validateForm() 
{

	var missinginfo = '';

	// find out if any of the ealerts have been selected, if they have then the email address becomes required
	var amount = document.userdets.elements.length;

		cont = 0; //continue
		for (i=0;i<amount;i++) {
			what = document.userdets.elements[i].type;
			if (what == "checkbox") {
				if (document.userdets.elements[i].checked) {
					cont = 1;
				}
			}
		}
			
	// Make email required by setting the cont variable = 1		
	cont = 1;
		
	var da_firstname = trim(document.userdets.usr_firstname.value);
	var da_firstname_valid = validfield(da_firstname,1);
	
		if (da_firstname_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + FIRSTNAME_INVALID_CHAR;
		}
		if (da_firstname.length < 2) 	{
				missinginfo += "\n     -  " + FIRSTNAME_MIN_TWO_CHARS;
		}
	
	var da_lastname = trim(document.userdets.usr_lastname.value);
	var da_lastname_valid = validfield(da_lastname,1);
	
		if (da_lastname_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + LASTNAME_INVALID_CHAR;
		}
		if (da_lastname.length < 2) 	{
		   missinginfo += "\n     -  " + LASTNAME_MIN_TWO_CHARS;
		}
	
	var da_profession = document.userdets.profession[document.userdets.profession.selectedIndex].value;
		if (da_profession == "-88") {
			missinginfo += "\n     -  " + PROFESSION_MANDATORY;
		}
						
	var da_license = document.userdets.license.value;
	var da_license_valid = validfield(da_license);
	
		if (da_license_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + LICENSE_INVALID_CHAR;
		}
		if (da_license.length < 3) {
				missinginfo += "\n     -  " + LICENSE_INVALID_LENGTH;
		}   
		
	var da_email = document.userdets.email.value;

		// if cont = 1 then the email address is required!
		if (cont == "1" && da_email.length == "0") {
			missinginfo += "\n     -  " + EMAIL_MANDATORY;
		}
							
		if (da_email.length != "0") {
		   if (da_email.indexOf("@")<2)  {
				missinginfo += "\n     -  " + EMAIL_INVALID + "*";
			} else {
				if (da_email.indexOf(".",da_email.indexOf("@")) < da_email.indexOf("@")) {
					missinginfo += "\n     -  " + EMAIL_INVALID + "*";
				}
			}			
		}
					
	//var da_country = document.userdets.country[document.userdets.country.selectedIndex].value;
		//if (da_country == "-99") {
			//missinginfo += "\n     -  " + COUNTRY_MANDATORY;
		//}
						
		var da_username = document.userdets.usr_login.value;
		var da_username_valid = validfield(da_username);
		
			if (da_username_valid > 0) {
				//Error - invalid chars found
				missinginfo += "\n     -  " + USERNAME_INVALID_CHAR;
			}
			if (da_username.length < 6) 
				{
					missinginfo += "\n     -  " + USERNAME_INVALID_LENGTH;
				}   
		
		var da_passwd = document.userdets.usr_passwd.value;
		var da_passwd_valid = validfield(da_passwd)
		
			if (da_passwd_valid > 0) {
				//Error - invalid chars found
				missinginfo += "\n     -  " + PASSWORD_INVALID_CHAR;
			}
			if (da_passwd.length < 6)
				{
					missinginfo += "\n     -  " + PASSWORD_INVALID_LENGTH;
				}
				
		var da_passwdTwo = document.userdets.usr_passwd2.value;
		var da_passwdTwo_valid = validfield(da_passwdTwo)
		
			if (da_passwdTwo_valid > 0) {
				//Error - invalid chars found
				missinginfo += "\n     -  " + PASSWORD_CONFIRM_INVALID_CHAR;
			}
			 if (da_passwdTwo != da_passwd) {
				missinginfo += "\n     -  " + PASSWORD_CONFIRM_INVALID_LENGTH;
			}
														 

	if (missinginfo != "") {
		missinginfo ="_______________________________________________\n" +
		FORM_ERROR + "\n" +
		missinginfo + "\n_______________________________________________" ;
		alert(missinginfo);
		return false;
	}
	
	
	if (document.userdets.intro_offer.checked) {
		missinginfo = "";

		var da_address_one = trim(document.userdets.usr_address_one.value);
		var da_city = trim(document.userdets.usr_city.value);
		//var da_province = trim(document.userdets.usr_lastname.value);
		var da_postalcode = trim(document.userdets.usr_postalcode.value);

		var da_address_one_valid = validfield(da_address_one,1);
		if (da_address_one.length == 0) {
			missinginfo += "\n     -  " + ADDRESS_ONE_MANDATORY;
		//} else if (da_address_one_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + ADDRESS_ONE_INVALID_CHAR;
		} else if (da_address_one.length < 2) 	{
			missinginfo += "\n     -  " + ADDRESS_ONE_MIN_TWO_CHARS;
		}

		var da_city_valid = validfield(da_city,1);
		if (da_city.length == 0) {
			missinginfo += "\n     -  " + CITY_MANDATORY;
		} else if (da_city_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + CITY_INVALID_CHAR;
		} else if (da_city_valid.length < 2) 	{
			missinginfo += "\n     -  " + CITY_MIN_TWO_CHARS;
		}
		
		var da_postalcode_valid = validfield(da_postalcode,1);
		if (da_postalcode.length == 0) {
			missinginfo += "\n     -  " + POSTAL_MANDATORY;
		} else if (da_postalcode.length < 6) {
			missinginfo += "\n     -  " + POSTAL_SIXCHARS;
		} else if (da_city_valid > 0) {
			//Error - invalid chars found
			missinginfo += "\n     -  " + POSTAL_INVALID_CHAR;
		}
		if (missinginfo != "") {
			missinginfo ="_______________________________________________\n" +
			FORM_EXTRA_ERROR + "\n" +
			missinginfo + "\n_______________________________________________" ;
			alert(missinginfo);
			return false;
		}

	}

	return true;

}



function validateEmailForm() 
{

	var missinginfo = '';

	var da_email = document.usernotify.usr_email.value;

	if (da_email.length == "0") {
		missinginfo += "\n     -  " + EMAIL_MANDATORY;
	}
						
	if (da_email.length != "0") {
	   if (da_email.indexOf("@")<2)  {
			missinginfo += "\n     -  " + EMAIL_INVALID + "*";
		} else {
			if (da_email.indexOf(".",da_email.indexOf("@")) < da_email.indexOf("@")) {
				missinginfo += "\n     -  " + EMAIL_INVALID + "*";
			}
		}			
	}
													 
	if (missinginfo != "") {
		missinginfo ="_______________________________________________\n" +
		FORM_ERROR + "\n" +
		missinginfo + "\n_______________________________________________" ;
		alert(missinginfo);
		return false;
	}


	return true;

}