// FUNCTION DE VALIDATION DES FORMS

function checkEcrismoiForm(theForm) {
    var why = "";

	// Check mandatory fields
	why += checkEmail(theForm.Email.value,"FR","Courriel");
	why += checkEmail(theForm.Email2.value,"FR", "Courriel 2");
	
	var email1 = theForm.Email.value;
	var email2 = theForm.Email2.value;
	
	// check if emails are identical
	if (why == "") {
		if ( email1 != email2) {
			why += "Oups! Les courriels non sont pas identiques.";	
		}
	}

	if (why != "") {
	
       alert(why);
       return false;
    }
	
return true;
}


// Drop down list validation
function checkDropdown(choice,listname,langue) {
var error = "";
    if (choice == 0) {
		if 	(langue == "FR") { error = "Vous n'avez pas fait de s&eacute;lection dans la liste " + listname + ".\n"; }
		else { error = "You didn't choose an option from the " + listname + " drop-down list.\n"; }
		
    }    
return error;
}

// non-empty textbox
function isEmpty(strng, fieldname, langue) {
var error = "";
  if (strng.length == 0) {
	  if (langue == "FR")
		{
			error = "Le champ obligatoire " + fieldname + " n'a pas &eacute;t&eacute; saisie.\n";
		}
		else
		{
			error = "The mandatory text area " + fieldname + " has not been filled in.\n";
		}
     
  }
return error;	  
}

// e mail validation
function checkEmail (strng,langue, fieldname) {
var error="";
if (strng == "") {
	if (langue == "FR")
	{
		error = "Tu dois saisir une adresse de courriel. (" + fieldname + ")\n";
	}
	else
	{
		error = "You didn't enter an email address.\n";
	}
   
}
else {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
		if (langue == "FR")
		{
			error = "Tu dois saisir une adresse de courriel valide. (" + fieldname + ")\n";
		}
		else
		{
			error = "Please enter a valid email address.\n";
		}

    }
    else {
		//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          	if (langue == "FR")
			{
				error = "Tu dois saisir une adresse de courriel valide.(" + fieldname + ")\n";
			}
			else
			{
				error = "Please enter a valid email address.\n";
			}
       }
    }
}
return error;    
}

// Phone number - strip out delimiters and check for 10 digits
function checkPhone (strng,langue) {
var error = "";

if (strng == "") {
	if (langue == "FR")
	{
		error = "Vous devez saisir votre num&eacute;ros de t&eacute;l&eacute;phone.\n";
	}
	else
	{
		error = "You didn't enter a phone number.\n";
	}
   
}
else
{
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   if (langue == "FR")
			{
				error = "Vous n'avez pas saisie un num&eacute;ros de t&eacute;l&eacute;phone valide.\n";
			}
			else
			{
				error = "You didn't enter a valid phone number.\n";
			}
	  
		}
		else {
			if (!(stripped.length == 10)) {
				if (langue == "FR")
				{
					error = "La longueur du num&eacute;ros de t&eacute;l&eacute;phone n'est pas valide (nnn) nnn-nnnn.\n";
				}
				else
				{
					error = "The lengt of the phone number is invalid (nnn) nnn-nnnn.\n";
				}
			}
		}
}
return error;
}

function checkPassword (strng,langue) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 10)) {
		if (langue == "FR") {
			error = "Le mot de passe n'a pas le bon nombre de caractères (6 à 10).\n";}
		else {
	       error = "The password is the wrong length (6 to 10).\n";}
    }
    else if (illegalChars.test(strng)) {
		if (langue == "FR") {
			error = "Le mot de passe contient des caractères ill&eacute;gaux.\n";}
		else {
	      error = "The password contains illegal characters.\n";}
    }
return error;    
}    


//// password - between 6-8 chars, uppercase, lowercase, and numeral
//
//function checkPassword (strng) {
//var error = "";
//if (strng == "") {
//   error = "You didn't enter a password.\n";
//}
//
//    var illegalChars = /[\W_]/; // allow only letters and numbers
//    
//    if ((strng.length < 6) || (strng.length > 8)) {
//       error = "The password is the wrong length.\n";
//    }
//    else if (illegalChars.test(strng)) {
//      error = "The password contains illegal characters.\n";
//    } 
//    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
//       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
//    }  
//return error;    
//}    
//
//
//// username - 4-10 chars, uc, lc, and underscore only.
//
//function checkUsername (strng) {
//var error = "";
//if (strng == "") {
//   error = "You didn't enter a username.\n";
//}
//
//
//    var illegalChars = /\W/; // allow letters, numbers, and underscores
//    if ((strng.length < 4) || (strng.length > 10)) {
//       error = "The username is the wrong length.\n";
//    }
//    else if (illegalChars.test(strng)) {
//    error = "The username contains illegal characters.\n";
//    } 
//return error;
//}       
//
//
//
//
//// was textbox altered
//
//function isDifferent(strng) {
//var error = ""; 
//  if (strng != "Can\'t touch this!") {
//     error = "You altered the inviolate text area.\n";
//  }
//return error;
//}
//
//// exactly one radio button is chosen
//
//function checkRadio(checkvalue) {
//var error = "";
//   if (!(checkvalue)) {
//       error = "Please check a radio button.\n";
//    }
//return error;
//}
//
// 
//
///*function checkValidDate(iMonth, iDay, langue,theForm) {
//var error = "";
//var today = new Date();
//var chosenDate = new Date(today.getYear() + 1900, iMonth - 1, iDay);
//var outputdate = "";
//
//    if (today.getTime() > chosenDate.getTime()) {
//		if 	(langue == "FR") {error = "Vous devez s&eacute;lectionner un date ult&eacute;rieure à aujourd'hui.\n"; }
//		else { error = "You must choose a date later then today.\n"; }
//		
//    }    
//
//return error;	
//}*/
//
//// ------------------------------------------------------------------
//// formatDate (date_object, format)
//// Returns a date in the output format specified.
//// The format string uses the same abbreviations as in getDateFromFormat()
//// ------------------------------------------------------------------
///*function formatDate(date,format) {
//	format=format+"";
//	var result="";
//	var i_format=0;
//	var c="";
//	var token="";
//	var y=date.getYear()+"";
//	var M=date.getMonth()+1;
//	var d=date.getDate();
//	var E=date.getDay();
//	var H=date.getHours();
//	var m=date.getMinutes();
//	var s=date.getSeconds();
//	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
//	// Convert real date parts into formatted versions
//	var value=new Object();
//	if (y.length < 4) {y=""+(y-0+1900);}
//	value["y"]=""+y;
//	value["yyyy"]=y;
//	value["yy"]=y.substring(2,4);
//	value["M"]=M;
//	value["MM"]=LZ(M);
//	value["MMM"]=MONTH_NAMES[M-1];
//	value["NNN"]=MONTH_NAMES[M+11];
//	value["d"]=d;
//	value["dd"]=LZ(d);
//	value["E"]=DAY_NAMES[E+7];
//	value["EE"]=DAY_NAMES[E];
//	value["H"]=H;
//	value["HH"]=LZ(H);
//	if (H==0){value["h"]=12;}
//	else if (H>12){value["h"]=H-12;}
//	else {value["h"]=H;}
//	value["hh"]=LZ(value["h"]);
//	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
//	value["k"]=H+1;
//	value["KK"]=LZ(value["K"]);
//	value["kk"]=LZ(value["k"]);
//	if (H > 11) { value["a"]="PM"; }
//	else { value["a"]="AM"; }
//	value["m"]=m;
//	value["mm"]=LZ(m);
//	value["s"]=s;
//	value["ss"]=LZ(s);
//	while (i_format < format.length) {
//		c=format.charAt(i_format);
//		token="";
//		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
//			token += format.charAt(i_format++);
//			}
//		if (value[token] != null) { result=result + value[token]; }
//		else { result=result + token; }
//		}
//	return result;
//	}	*/