
function validate_annonce(theForm) {
var reason = "";
	
	reason += validateField(theForm.titre, 40, 1, 'Titre', 0, 0); //40
	reason += validateField(theForm.desc, 400, 1, 'Description', 0, 0); // 400
	reason += validateField(theForm.nom, 30, 0, 'Nom', 0, 0); //30
	reason += validateField(theForm.phone, 30, 0, 'Phone', 0, 0); //30
	reason += validateField(theForm.imagetitle, 40, 0, 'Image Title', 0, 0); //40
	reason += validateField(theForm.email, 40, 0, 'Image Title', 1, 1);
}

function validate_user_registration(theForm) {
var reason = "";
 
  reason += validateField(theForm.fname, 40, 0, 'First Name', 0, 1);
  reason += validateField(theForm.lname, 40, 0, 'Last Name', 0, 1);
  reason += validateField(theForm.login1, 40, 0, 'User Name', 0, 1);
  reason += validatePassword(theForm.password);
  reason += validatePassword(theForm.cpassword);
  reason += PasswordMatch(theForm.password, theForm.cpassword);
  reason += validateField(theForm.email, 40, 0, 'Email', 1, 1);
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
 
  return true;
}
function validateField(field, MaxLength, IsMust, FieldName, IsEmail, IsValidChars) {
    var error = ""; 
	var tfield = trim(field.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; 
	field.style.background = 'White';
	if (IsMust == 1)
	{
		if (field.value == "") {
			field.style.background = '#666'; 
			error = "Enter ".FieldName.".\n";
		}
		else if (illegalChars.test(field.value)) {
        	error = "The contains illegal characters in ".FieldName.".\n";
        	field.style.background = '#666';
    	}
		} else if (!emailFilter.test(tfield)) {              //test email for illegal characters
			field.style.background = '#666';
			error = "Please enter a valid email address.\n";
		} else if (field.value.match(illegalChars)) {
			field.style.background = '#666';
			error = "Contains illegal characters ".FieldName.".\n";
		}		
	}
    if (field.value.length > MaxLength) {
        field.style.background = '#666'; 
        error = "Maximum length is ".MaxLength." - ".FieldName.".\n";	
    }
    return error;
}



// Field, Length, Must?, Name of the Field, Email?, ValidChars? 
function validate_member(theForm) {
var reason = "";
 
  reason += validateField(theForm.fname, 40, 0, "First Name", 0, 1);
  reason += validateField(theForm.lname, 40, 0, "Last Name", 0, 1);
  reason += validateField(theForm.login1, 40, 0, "User Name", 0, 1);
  reason += validatePassword_Resave(theForm.oldpassword);
  reason += validatePassword_Resave(theForm.password);
  reason += validatePassword_Resave(theForm.cpassword);
  reason += PasswordMatch_Resave(theForm.password, theForm.cpassword);
  reason += validateField(theForm.email, 40, 0, "Email", 1, 1);
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
 
  return true;
}  

// Field, Length, Must?, Name of the Field, Email?, ValidChars? 
function validate_contact(theForm) {
var reason = "";
 
  reason += validateField(theForm.fname, 40, 0, "First Name", 0, 0);
  reason += validateField(theForm.lname, 40, 0, "Last Name", 0, 0);
  reason += validateField(theForm.email, 40, 0, "Email", 1, 1);
  reason += validateField(theForm.sujet, 40, 0, "Sujet", 0, 0);
  reason += validateField(theForm.message, 40, 0, "Message", 0, 0);
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
 
  return true;
}  

// Field, Length, Must?, Name of the Field, Email?, ValidChars? 
function validate_forgot(theForm) {
var reason = "";
 
  reason += validateField(theForm.email, 40, 0, "Email", 1, 1);

  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
 
  return true;
}

// Field, Length, Must?, Name of the Field, Email?, ValidChars? 
function validate_login(theForm) {
var reason = "";
 
  reason += validateUserName(theForm.login1, 40, 0, "User Name", 0, 1);
  reason += validatePass(theForm.password);

  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
 
  return true;
}  
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


function validatePassword_Resave(field) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
    
	if (trim(field.value) == "") {
        field.style.background = '#666';
        error = "Password is empty.\n";
    } 
    else if (field.value != "") {
		if ((field.value.length < 6) || (field.value.length > 15)) {
			error = "The password is the wrong length. \n";
			field.style.background = '#666';
		} else if (illegalChars.test(field.value)) {
			error = "The password contains illegal characters.\n";
			field.style.background = '#666';
		} else {
			field.style.background = 'White';
		}
	}
   return error;
}   
function PasswordMatch_Resave(field1, field2)
{
  var error="";
  if(field1.value != field2.value) {
    field1.style.background = '#666';
    field2.style.background = '#666';
    error = "Passwords do not match!\n";
  }
  return error;
}
 


function validatePass(field) {
    var error = "";
	var tfield = trim(field.value);                        // value of field with whitespace trimmed off
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (tfield == "") {
        field.style.background = '#666';
        error = "You didn't enter a password.\n";
    } else if (illegalChars.test(field.value)) {
        error = "The password contains illegal characters.\n";
        field.style.background = '#666';
    } else {
        field.style.background = 'White';
    }
   return error;
}   


function PasswordMatch(field1, field2)
{
  var error="";
  if(field1.value != field2.value) {
    field1.style.background = '#666';
    field2.style.background = '#666';
    error = "Passwords do not match!\n";
  }
  return error;
}
function validatePassword(field) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (trim(field.value) == "") {
        field.style.background = '#666';
        error = "Ppassword is empty.\n";
    } else if ((field.value.length < 6) || (field.value.length > 15)) {
        error = "The password is the wrong length. \n";
        field.style.background = '#666';
    } else if (illegalChars.test(field.value)) {
        error = "The password contains illegal characters.\n";
        field.style.background = '#666';
    } else {
        field.style.background = 'White';
    }
   return error;
}   
