//TRANSLATIONS
var allFieldsNecessary = "Tous les champs sont requis."; //english: All the fields are required
var requiredMsg = "* Requis"; 
var emailMsg = "* Email invalide";
var incorrectMsg = "* Incrorrecte";
var validatePasswordMsg = "* Mot de passe invalide";
var passwordLengthMsg = "* Au moins 5 caracteres";
var unregWarning = "ÃŠtes-vous sÃ»r vous souhait pour effacer votre compte?";

var emptyString = /^\s*$/ ;

function trim(string)
{
  return string.replace(/^\s+|\s+$/g, '');
}

function checkWholeForm(theForm) {
    
    var noErrors = 0;
    
    if (!checkBoxes('newsletterMsg', document.getElementById('Daily Newsletter'), document.getElementById('B2B Newsletter'), document.getElementById('Thema Newsletter'))) noErrors += 1;

    if (!validateFields(theForm.title, "titleMsg")) noErrors += 1;
  		if (!validateFields(theForm.firstName, "firstNameMsg")) noErrors += 1;
    if (!validateFields(theForm.surname, "surnameMsg")) noErrors += 1;
    if (!validateFields(theForm.company, "companyMsg")) noErrors += 1;
    if (!validateFields(theForm.activity, "activityMsg")) noErrors += 1;
    if (!validateFields(theForm.position, "positionMsg")) noErrors += 1;
    if (!validateFields(theForm.sector, "sectorMsg")) noErrors += 1;
    if (!validateFields(theForm.employees, "employeesMsg")) noErrors += 1;
    if (!validateFields(theForm.address, "addressMsg")) noErrors += 1;
    if (!validateFields(theForm.city, "cityMsg")) noErrors += 1;
    if (!validateFields(theForm.postcode, "postcodeMsg")) noErrors += 1;
    if (!validateFields(theForm.country, "countryMsg")) noErrors += 1;
    if (!validateFields(theForm.fax, "faxMsg")) noErrors += 1;
    if (!validateFields(theForm.telephone, "telephoneMsg")) noErrors += 1;
    
    if (!validateEmail(theForm.regMail, "regMailMsg")) noErrors += 1;
    if (!validateConfirmEmail(theForm.confirmRegMail, "confirmRegMailMsg", theForm)) noErrors += 1;
    if (!validatePassword(theForm.userPass, "userPassMsg")) noErrors += 1;
    if (!validateConfirmPassword(theForm.confirmUserPass, "confirmUserPassMsg", theForm)) noErrors += 1;
    
   	if (noErrors > 0) 
    {
      alert(allFieldsNecessary + "\n");
			return false;
    }
    else
    {
    		return true;
    }
}

function checkPartialForm(theForm) {
    
    var noErrors = 0;
    
    //if (!checkBoxes("newsletterMsg", theForm.option01, theForm.option02, theForm.option03)) noErrors += 1;

    if (!validateFields(theForm.title, "titleMsg")) noErrors += 1;
  		if (!validateFields(theForm.firstName, "firstNameMsg")) noErrors += 1;
    if (!validateFields(theForm.surname, "surnameMsg")) noErrors += 1;
    if (!validateFields(theForm.company, "companyMsg")) noErrors += 1;
    if (!validateFields(theForm.activity, "activityMsg")) noErrors += 1;
    if (!validateFields(theForm.position, "positionMsg")) noErrors += 1;
    if (!validateFields(theForm.sector, "sectorMsg")) noErrors += 1;
    if (!validateFields(theForm.employees, "employeesMsg")) noErrors += 1;
    if (!validateFields(theForm.address, "addressMsg")) noErrors += 1;
    if (!validateFields(theForm.city, "cityMsg")) noErrors += 1;
    if (!validateFields(theForm.postcode, "postcodeMsg")) noErrors += 1;
    if (!validateFields(theForm.country, "countryMsg")) noErrors += 1;
    if (!validateFields(theForm.fax, "faxMsg")) noErrors += 1;
    if (!validateFields(theForm.telephone, "telephoneMsg")) noErrors += 1;
    
    //if (!validateEmail(theForm.regMail, "regMailMsg")) noErrors += 1;
    //if (!validateConfirmEmail(theForm.confirmRegMail, "confirmRegMailMsg", theForm)) noErrors += 1;
    if (!validateUpdatePassword(theForm.userPass, "userPassMsg")) noErrors += 1;
    if (!validateConfirmPassword(theForm.confirmUserPass, "confirmUserPassMsg", theForm)) noErrors += 1;
    
   	if (noErrors > 0) 
    {
      alert(allFieldsNecessary + "\n");
			return false;
    }
    else
    {
    		return true;
    }
}

function msg(field,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(field);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}


function validateEmail (string, msgField) {
  //var stat = commonCheck (valfield, infofield, required);
  //if (stat != proceed) return stat;

  var tfld = trim(string.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  var comma = /,/; 
  
  if (string.value == "") {
  		msg (msgField, "withError", requiredMsg);
  		return false;
  }
  else if (!email.test(tfld) || comma.test(tfld)) {
    msg (msgField, "withError", emailMsg);
    //setfocus(valfield);
    return false;
  }
  else {
    msg (msgField, "noError", "*");
  	  return true;
  }
}

function validateConfirmEmail (string, msgField, theForm) {

	if (string.value == "") {
		msg (msgField, "withError", requiredMsg);
	  	return false;
	}
	else if (theForm.regMail.value != string.value) {
		//var elem = document.getElementById(msgField);
		msg(msgField, "withError", incorrectMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}

}

// password - greater than 5 chars, uppercase, lowercase, and numeral
function validatePassword (string, msgField) {
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if (illegalChars.test(string.value)) {
		msg(msgField, "withError", validatePasswordMsg);
		return false;
	}
	else if (string.value == "" || string.value.length < 5) {
		msg(msgField, "withError", passwordLengthMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

// password - greater than 5 chars, uppercase, lowercase, and numeral
function validateUpdatePassword (string, msgField) {
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if (illegalChars.test(string.value)) {
		msg(msgField, "withError", validatePasswordMsg);
		return false;
	}
	else if (string.value.length > 0 && string.value.length < 5) {
		msg(msgField, "withError", passwordLengthMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function validateConfirmPassword (string, msgField, theForm) {
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if (illegalChars.test(string.value)) {
		msg(msgField, "withError", validatePasswordMsg);
		return false;
	}
	else if (theForm.userPass.value != string.value) {
		//var elem = document.getElementById(msgField);
		msg(msgField, "withError", incorrectMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function validateFields (string, msgField) {
	if (string.value == "" ) {
		msg(msgField, "withError", requiredMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function checkBoxes (msgField, box1, box2, box3) {
	if (!box1.checked && !box2.checked && !box3.checked ) {
		msg(msgField, "withError", "* Sélectionner au moins une newsletter");
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function validateAddress (string, msgField) {
	removeCommas(string);
	if (string.value == "" ) {
		msg(msgField, "withError", requiredMsg);
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function validatePostcode (string, msgField) {
	if (string.value == "" ) {
		msg(msgField, "withError", requiredMsg);
		return false;
	}
	else if (string.value.length != 5 ) {
		msg(msgField, "withError", "* Invalid Postcode");
		return false;
	}
	else {
		msg(msgField, "noError", "*");
		return true;
	}
}

function validateAddressFields (streetAddress, city, postcode, country) {
	if (streetAddress.value == "" && city.value == "" && postcode.value == "" && country.value == "") {
		//msg(msgField, "noError", "*");
		//alert("its ok1!");
		return true;
	}
	else if (streetAddress.value != "" && city.value != "" && postcode.value != "" && country.value != "") {
		//msg(msgField, "noError", "*");
		//alert("its ok2!");
		return true;
	}
	else {
		//msg(msgField, "withError", "* Requis");
		//alert("its bad3!");
		validateFields(streetAddress, "addressMsg");
    validateFields(city, "cityMsg");
    validateFields(postcode, "postcodeMsg");
    validateFields(country, "countryMsg");
    //validateFields(fax, "faxMsg");
    //validateFields(phone, "telephoneMsg");
		return false;
	}

}

function removeCommas(string) {
	var newString = string.value.replace(/,/g, "");
	string.value = newString;
}

function confirmDelete() {
	var answer = window.confirm(unregWarning)
	
	if (answer)
		return true;
	else
		return false;
}