function FormManager(oForm,errorDivName,noErrorFromServer){
	this.formName  = "";
	this.errorFromServer = (noErrorFromServer=="False")
	if(oForm){
	  if(!typeof(oForm) == "string") this.initForm(oForm)
		else{
			this.formName = oForm;
			if(errorDivName) this.errorDivName = errorDivName;
			else this.errorDivName = "errorMessage";
		}
	}
	this.lang = "fr";
}

FormManager.prototype.init = function(oForm){
	if(!oForm) oForm = document.getElementById(this.formName);
	this.form = oForm;
 	this.checkRestrictions();
	this.errorDiv =  document.getElementById(this.errorDivName);
	
	if(this.errorFromServer && this.errorDiv)
	  this.errorDiv.style.display = "";
}

FormManager.prototype.showError = function(o,flag){
	var str="";
	if(typeof(o) == "object"){
		if(this.lang=="fr") str = o.messagefr;
		else str = o.message;
	}else str = o;
	
	if (this.errorDiv){
  	this.errorDiv.innerText = str;
	  if(flag!=false) this.errorDiv.style.display = "";
		else this.errorDiv.style.display = "none";
  } else alert(str);
}

FormManager.prototype.setInputError = function(o){
	o.onfocus = function(){this.style.backgroundColor="white"};
  o.style.backgroundColor= "#FFC1C1";
}

FormManager.prototype.testFields = function(onlyRequired){
	var oTest = {
		passed:true,
		message: "You have left the highlighted fields empty.",
		messagefr: "Vous n'avez rien écrit dans les champs mis en évidence."
	}

	for (var each in this.form.elements){
    var o = each
    if (o!=null && o.value=="" && (!onlyRequired || (onlyRequired && o.required!=undefined))){
      this.setInputError(o);
    	oTest.passed = false;
	 	}
  }
	
	if(!oTest.passed) this.showError(oTest);
	
	return oTest.passed;  
}

FormManager.prototype.validatePassword = function(tf1,tf2,m){
	if(!m) m = 4;
	var o = { passed:true, message:"", messagefr:""	}
	
	if (tf1.value!=tf2.value){
		o.messagefr = "Les deux champs de votre mot de passe ne sont pas identiques."; 
		o.message = "The two passwords you have entered are not the same."; 
		o.passed = false;
	}	else if (tf1.value.length<m){
		o.messagefr = "Votre mot de passe doit avoir au moins "+m+" caractères."; 
		o.message = "Your password needs to have at least "+m+" characters."; 
		o.passed = false;
	}	  
	
	if(!o.passed){
		this.setInputError(tf1);
		this.setInputError(tf2);
		this.showError(o);
	}
	
	return o.passed;
}



FormManager.prototype.validateEmail = function(tf){
	var o = validateEmail(tf.value);
	if(!o.passed){
		this.setInputError(tf);
		this.showError(o);
	}
	
	return o.passed;
}

FormManager.prototype.checkRestrictions=function(){
	for (var each in this.form.elements){
    var o = each
    if (o!=null && o.restriction!=undefined){
      this.setInputRestriction(o,o.restriction);
	 	}
  }
}

FormManager.prototype.setInputRestriction = function(obj,str,numMax){
  str.replace("\\^","#94");
  str.replace("\\-","#45");
  
  obj.inputRestrictions =str;
  
  if (numMax!=undefined) obj.maxInputChars = numMax;
  
  obj.onkeypress=function(){
    arrRules = this.inputRestrictions.split(" ");
    var fail=true;
	var sGroups=false;
	for (var ctr=0;ctr<arrRules.length;ctr++){
	  var notFlag=false;
	  var to=false;
	  str = arrRules[ctr];
	  if (str.charAt(0)=="^"){
	    str=str.substr(1,str.length-1);
		notFlag = true;
	  }
	  
	  arrStr = str.split("-");
	  if (arrStr[0]!=undefined) from=arrStr[0];
	  else from = arrStr;
	  if (from.charAt(0)=="#") from = parseInt(from.substring(1,from.length));
	  else from = from.charCodeAt(0);
		
	  if (arrStr[1]!=undefined){
	    to= arrStr[1];
        if (to.charAt(0)=="#") to = parseInt(to.substring(1,to.length));
	 	else to = to.charCodeAt(0);		  
	  }
	    
	  if (notFlag){
	    if (to){
		  if (event.keyCode>=from && event.keyCode<=to){ fail=true; break;}
		}else if (event.keyCode==from){fail=true; break;}
		fail=false;
	  }
	  if (!notFlag){
		sGroups=true;
	    if (to){
		  if (event.keyCode>=from && event.keyCode<=to){fail=false;}
		}else  if (event.keyCode==from) {fail=false;}
	  }
		  
	}
	
	// if the maximum size is reached
	if(!fail)
	  if (this.innerText.length>=this.maxInputChars)
	   if (document.selection.type=="Text"){
	     tRange = document.selection.createRange();
	     tRange.text="";
	   }else fail = true;
 
	if (fail) event.keyCode="";  
  }
}

