// JavaScript Document


// -----------------------Valide un courriel------------------------------------

function FDK_ValidateExtension( FormElement,Extension,ErrorMsg)
{
	var goodextension = false; 
	var valeur = FormElement.value
	if(valeur!=''){
		var point = valeur.lastIndexOf(".")
		var ext = valeur.substr((point+1))
		switch(Extension.toLowerCase())
		{
			case "images" :
				switch(ext.toLowerCase())
				{
					case "jpg":
					case "jpeg": 
					case "gif":
					case "png":
					case "tif":
					case "eps":
					case "ai":
					case "psd":
					case "bmp":
						goodextension = true;
						break;					
				}
				break;
			case "texte" :
				switch(ext.toLowerCase())
				{
					case "txt":
					case "doc" :
					case "pdf" :
					case "rtf" :
					case "html":
					case "htm":
					case "xls":
					case "xml":
					case "xhtml":
					case "log":
					case "ini":
						goodextension = true;
						break;
				}
				break;
			case "compresser" :
				switch(ext.toLowerCase())
				{
					case "ace" :
					case "cab" :
					case "zip" :
					case "rar" :
					case "jar" :
					case "war" :
					case "gz" :
					case "tgz" :
						goodextension = true;
						break;
				}
				break;
		}	
	}
	
	if(goodextension||valeur==''){
		return "";
	}else{
		return ErrorMsg;
	}
}

function FDK_ValidateEmail(FormElement,ErrorMsg)
{
   var msg = "";
   var val = FormElement.value;
   if(val==undefined)val="";
   var msgInvalid = ErrorMsg;
   var pattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"
   var reg = new RegExp(pattern,'gim')
   var arr = val.match(reg) 
   if(arr==null){
   		msg = ErrorMsg;
   }  
   return msg; 
}
// Ajoute un array de courriel
function FDK_AddArrayExtension(FormName, FormElementName, Extension, ErrorMsg){
  var obj = "document."+FormName+"['"+FormElementName+"']"	 
  var ValString = "FDK_ValidateExtension(" + obj +",'" + Extension + "','"+ErrorMsg+"')"
  FDK_AddToValidateArray(FormName,eval(obj),ValString,false)
}


function FDK_AddEmailValidation(FormName,FormElementName,SetFocus,ErrorMsg)  {
	
  var obj = "document."+FormName+"['"+FormElementName+"']"
  var ValString = "FDK_ValidateEmail(" + obj +",'"+ErrorMsg+"')"
  FDK_AddToValidateArray(FormName,eval(obj),ValString,SetFocus)
}
//--------------------------Valide un champs null ou vide ------------------------
function FDK_ValidateNonBlank(FormElement,ErrorMsg)
{
  var msg = ErrorMsg;
  FormElement = eval(FormElement)
  var valeur = FormElement.value;  
  if(valeur!=' '&&valeur!='null'&&valeur!=null&&valeur!=''&&valeur.length!=0){
	msg = "" 
  }else{
	msg = ErrorMsg
  }
  return msg;
}

function FDK_AddNonBlankValidation(FormName,FormElementName,SetFocus,ErrorMsg)  {
  var obj = "document."+FormName+"['"+FormElementName+"']"
  var ValString = "FDK_ValidateNonBlank("+obj+",'"+ErrorMsg+"')"
  FDK_AddToValidateArray(FormName,eval(obj),ValString,SetFocus)
}
//---------------------------------------------------------


function FDK_StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}


function FDK_AddToValidateArray(FormName,FormElement,Validation,SetFocus)
{
	
	if(eval(FormElement)){		
		var TheRoot=eval("document."+FormName); 
		if (!TheRoot.ValidateForm) 
		{
			TheRoot.ValidateForm = true;
			eval(FormName+"NameArray = new Array()")
			eval(FormName+"ValidationArray = new Array()")
			eval(FormName+"FocusArray = new Array()")
		}
			var ArrayIndex = eval(FormName+"NameArray.length");
		eval(FormName+"NameArray[ArrayIndex] = FormElement");
		eval(FormName+"ValidationArray[ArrayIndex] = Validation");
		eval(FormName+"FocusArray[ArrayIndex] = SetFocus");
	}
}


function ValidateInteger(FormElement,Required,Minimum,Maximum,ErrorMsg)
{
	var theString = FormElement.value;
	theString = FDK_StripChars(" ",theString);
	var min = Minimum;
	var max = Maximum;
	var pean = ErrorMsg;

	if (theString.length == 0)	
	{
		if (!Required) return ""		
		else return pean;
	}

	// remove leading zeros (zeros are only leading if there is more than one char)
	while (theString.length > 1 && theString.substring(0,1) == "0")
	{
		theString = theString.substring(1, theString.length);
	}

	var val = parseInt(theString);
	if (isNaN(val)) return pean;
	
	// check for non-digits (and minus sign). Do this by converting number
	// back to a string and comparing it to original string.
	if (val.toString() != theString) return pean;
	
	if (min < max)
	{
		if ((val < min) || (val > max))
		{
			return ErrorMsg;
		}
	}
	   
	// reset the entered string after removal of spaces and leading zeros.
	FormElement.value=theString;
	return "";
}

function FDK_AddIntegerValidation(FormName,FormElementName,Required,Minimum,Maximum,SetFocus,ErrorMsg)  {
  var ValString = "ValidateInteger("+FormElementName+","+Required+","+Minimum+","+Maximum+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}


// -------------------------------------------- FONCTION DE VALIDATION DES ARRAYS -----------------------------
function FDK_Validate(FormName, stopOnFailure, AutoSubmit, ErrorHeader)
{
 var theFormName = FormName;
 var theElementName = "";

 if (theFormName.indexOf(".")>=0)  
 {
   theElementName = theFormName.substring(theFormName.indexOf(".")+1)
   theFormName = theFormName.substring(0,theFormName.indexOf("."))
 }

  var theNameArray = eval(theFormName+"NameArray")
  var theValidationArray = eval(theFormName+"ValidationArray")
  var theFocusArray = eval(theFormName+"FocusArray")
  var ErrorMsg = "";
  var FocusSet = false;
  var i
  var valeurretour = true;
  var msg

        // Go through the Validate Array that may or may not exist
        // and call the Validate function for all elements that have one.	
  if (String(theNameArray)!="undefined")
  {
	
   for (i = 0; i < theNameArray.length; i ++)
   {
    msg="";
    if (theNameArray[i].name == theElementName || theElementName == "")
    { msg = eval(theValidationArray[i]);}
    if (msg != "")
    {
     ErrorMsg += "\n"+msg;                   
     if (stopOnFailure == "1") 
     {
       if (theFocusArray[i] && !FocusSet)  
      {	   
       FocusSet=true;	
       theNameArray[i].focus();
      }
      alert(ErrorHeader+ErrorMsg);
      valeurretour = false; 
      break;
     }
     else  
     {
      if (theFocusArray[i] && !FocusSet)  
      {
       FocusSet=true;
	   if (theNameArray[i].focus)
	       theNameArray[i].focus()
	   else
	   	   theNameArray[i][0].focus();
      }
     }
    }   
  }
  if (ErrorMsg!="" && stopOnFailure != "1") 
  {
   alert(ErrorHeader+ErrorMsg);
  }  
  if (valeurretour && AutoSubmit)  
  {
   eval("document."+FormName+".submit()")
  }
 }
 if(valeurretour){
	 eval(FormName+"NameArray = null")
	 eval(FormName+"ValidationArray = null")
	 eval(FormName+"FocusArray = null")
 }
 return valeurretour;
}
