/**
 * Recherche l'�l�ment info bulle d'un champ de saisie
 * 
 * @param id = Identifiant du champ de saisie
*/
function infoElement(id) {
 info=$("info_"+id);
 return info;
}

/**
 * Recherche l'�lement info bulle sur erreur d'un champ de saisie
 *
 * @param id = Identifiant du champ de saisie
*/
function checkElement(id) {
 return $("check_"+id);
}

 
/**
 * Rechercher l'�lement de saisie
 *
 * @param id - Identifiant du champ de saisie 
 */
 function inputElement(id) {
  return $(id);
 }
 
 
/**
  * Cacher l'info bulle
  *
  * @param infoBulle - La bulle a cacher
 */
function hideInfo(info) {
 if ( info != undefined ) {
    Effect.Fade(info);
	info.style.position="absolute";
	info.style.left="-1800px";
	info.style.top="-1800px";
  }
}


/**
 * Monter l'info bulle
*/
function showInfo(info,input) {
 if ( info != undefined && input != undefined ) {
  inputPosition=input.positionedOffset();
  inputWidth=input.getWidth();
  inputHeight=input.getHeight();
 	
  infoTop=inputPosition.top;
  infoLeft=inputPosition.left+inputWidth+4;

  info.style.display="none";
  info.style.top=infoTop+"px";
  info.style.left=infoLeft+"px";
  Effect.Appear(info);
  }
 
}

/**
 * Check info
*/
function inputCheck(input,info,check) {
 hideInfo(info)
 showInfo(check,input);
}

function putCheck(id) {
 var input=inputElement(id);
 var info=infoElement(id);
 var check=checkElement(id);
 hideInfo(info);
 showInfo(check,input);
} 
  

/**
 * Focus sur input
 * 
 * @param id - input
 */
function inputFocus() {
 
 id=this.id;
 var input=inputElement(id);
 var info=infoElement(id);
 var check=checkElement(id);
 
 if ( check != undefined )
  hideInfo(check);
 
 
 if ( info != undefined )
  showInfo(info,input);
}

function isEmpty(value) {
 return value==undefined || value.length==0;
}

function badEmail(value) {
var reg= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;

 return (value == undefined) || (reg.test(value)!=true);

}

function badDateCanNull(value) {

 if  ( value == undefined || value.length == 0 )
  return false;
var reg1= /^[0-9][0-9]$/;
var reg2= /^[0-9][0-9][0-9][0-9]$/;
 
 fields=value.split('/');
 
 if ( fields.length != 3 ) {
  return true;
 }
  
 day=fields[0];
 
 if ( day.length != 2 ) {
  return true;
  }
 
 if ( reg1.test(day)!=true ) {
  return true;
  }
  
 month=fields[1];
 
 if ( month.length != 2 ) {
  return true;
 }
 
 if ( reg1.test(month)!=true ) {
  return true;
  }
  
 year=fields[2];
 
 if ( year.length != 4 ) {
  return true;
 }
 
 if ( reg2.test(year)!=true ) {
  return true;
 }
 
 iyear=Number(year);
 imonth=Number(month);
 iday=Number(day);
 
 if ( iyear<1900 || iyear>2050 ) 
  return true;
  
 if ( imonth<1 || imonth>12 ) 
  return true;
  
 if ( iday < 1 || iday > 31)
  return true;
  
 
 switch(imonth) {
  case 1,3,5,7,8,10,12 : if ( iday > 31 ) 
                          return true;
                         break;
  case 4,6,9,11 : if ( iday > 30 )
                   return true;
                  break;
  case 2 : if ( iyear % 4 != 0 ) {
            if ( iday > 28 )
             return true;
           } else {
              if ( iyear % 100 == 0 ) {
               if ( iday > 28 )
                return true;
              } else {
                 if ( iday > 29 )
                  return true;
              	}
           	}
           
           break;
 }  
 
  
 return false;
}

function badDate(value) {

	 if  ( value == undefined || value.length == 0 )
	  return true;
	var reg1= /^[0-9][0-9]$/;
	var reg2= /^[0-9][0-9][0-9][0-9]$/;
	 
	 fields=value.split('/');
	 
	 if ( fields.length != 3 ) {
	  return true;
	 }
	  
	 day=fields[0];
	 
	 if ( day.length != 2 ) {
	  return true;
	  }
	 
	 if ( reg1.test(day)!=true ) {
	  return true;
	  }
	  
	 month=fields[1];
	 
	 if ( month.length != 2 ) {
	  return true;
	 }
	 
	 if ( reg1.test(month)!=true ) {
	  return true;
	  }
	  
	 year=fields[2];
	 
	 if ( year.length != 4 ) {
	  return true;
	 }
	 
	 if ( reg2.test(year)!=true ) {
	  return true;
	 }
	 
	 iyear=Number(year);
	 imonth=Number(month);
	 iday=Number(day);
	 
	 if ( iyear<1900 || iyear>2050 ) 
	  return true;
	  
	 if ( imonth<1 || imonth>12 ) 
	  return true;
	  
	 if ( iday < 1 || iday > 31)
	  return true;
	  
	 
	 switch(imonth) {
	  case 1,3,5,7,8,10,12 : if ( iday > 31 ) 
	                          return true;
	                         break;
	  case 4,6,9,11 : if ( iday > 30 )
	                   return true;
	                  break;
	  case 2 : if ( iyear % 4 != 0 ) {
	            if ( iday > 28 )
	             return true;
	           } else {
	              if ( iyear % 100 == 0 ) {
	               if ( iday > 28 )
	                return true;
	              } else {
	                 if ( iday > 29 )
	                  return true;
	              	}
	           	}
	           
	           break;
	 }  
	 
	  
	 return false;
	}


function isNotSame(value,confirm) {
 return value!=confirm;
}

function badNumber(value,min,max) {
 
 if ( value == "" )
  return false;
  
 if ( isNaN(value) ) {
  return true;
 }
   
 val=parseInt(value,10);
 

 
 

 if ( val < min || val > max )
  return true;
  
 return false;
}

function checkValue(input) {
 value=input.value;
 rule=input.className;
 
 if ( rule == "required" ) {
  return isEmpty(value);
 }
  
 if ( rule == "email" )
  return badEmail(value);
  
  if ( rule == "is_same" ) {
   same=input.title;
   return isNotSame(value,$F(same));
  }
  
  if ( rule == "date_null" ) {
   return badDateCanNull(value);
  }
  
  if ( rule == "date" ) {
	   return badDate(value);
	  }
  
  if ( rule == "number" ) {
   range=input.title;
   ranges=range.split(',');
   min=parseInt(ranges[0],10);
   max=parseInt(ranges[1],10);
   return badNumber(value,min,max);
  }
 return false;
}

function inputBlur() {
 id=this.id;
 var input=inputElement(id);
 
 if ( input!= undefined ) {
  var info=infoElement(id);
  var check=checkElement(id);
 
  if ( info != undefined )
   hideInfo(info);

  rule=input.className;
  value=input.value;
 
  if ( checkValue(this) ) {
   inputCheck(input,info,check);
   Form.Element.activate(input);
  }
 }
}

function validInput(input) {
 var id=input.id;
 var info=infoElement(id);
 var check=checkElement(id);
 
 hideInfo(info);

 rule=input.className;
 value=input.value;
 
 if ( checkValue(input) ) {
  inputCheck(input,info,check);
  input.focus();
  return false;
 }
 
 return true;

}

function validForm(form) {
 ret=true;
 if ( form != undefined ) {
  elementCount=form.elements.length;
  
  for (index=0;index<elementCount;index=index+1) {
   element=form.elements[index];
   if ( validInput(element) == false )
    ret=false;
  }
 }
 return ret;
}

function formSubmit() {
 ret=validForm(this);
 return ret; 
}

function validateForm(id) {
 form=$(id);
 if ( form != undefined ) {
  
  elementCount=form.elements.length;
  
  for (index=0;index<elementCount;index=index+1) {
   element=form.elements[index];
   element.onfocus=inputFocus;
   if ( element.onblur == undefined ) 
    element.onblur=inputBlur;
  
  }
  
   form.onsubmit=formSubmit;
  
  
 }
 
 
 
}

