var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function Trim(TRIM_VALUE){
  if(TRIM_VALUE.length < 1){
    return"";
  }
  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);
  if(TRIM_VALUE==""){
    return "";
  }
  else{
   return TRIM_VALUE;
  }
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("S\'il vous plait entrez un mois valide")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("S\'il vous plait entrez un jour valide")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("S\'il vous plait entrez une année valide sur quatre chiffres entre "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("S\'il vous plait entrez une date valide")
		return false
	}
return true
}

function digit(number)
{
var myinteger = number;
var mystring = myinteger + '';

  if (mystring.length==1)
  {
  mystring='0'+mystring;
  }
  return mystring;
}

function confirmErase()	
{	
   if (confirm("Etes vous sûr de vouloir supprimer cet enregistrement?"))
        return(true);
   return(false);
}

function EnvoiForm()
{document.form.submit();}

function checkNull(obj,msg){
  if(obj.value==''){
    alert(msg);
    obj.focus();
    obj.style.background="#ffaaaa";
    return false;
  }
  else
  {
    obj.style.background="#ffffff";
    return true;
  }
}

function checkSelect(obj,msg){
  if(obj.options[0].selected){
    obj.focus();
    obj.style.background="#ffaaaa";
    alert(msg);
    return false;
  }
else
  {
    obj.style.background="#ffffff";
    return true;
  }
}

function checkMail(obj, msg)
{
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(obj.value)==false){
    alert(msg);
    obj.focus();
    obj.style.background="#ffaaaa";
    return false;
  }
  else
  {
    obj.style.background="#ffffff";
    return true;
  }
}

function checkSelectNew(obj1,obj2,msg){
  if(obj1.options[0].selected){
    if (obj2.value==''){

    obj1.focus();
    obj1.style.background="#ffaaaa";
    obj2.style.background="#ffaaaa";
    alert(msg);
    return false;
    }
  }
  obj1.style.background="#ffffff";
  if (obj2.style.background!="#dddddd"){obj2.style.background="#ffffff";}
  return true
}



function checkVal(obj,typeMasque,msg)  //type de masque : Numeric ou Decimal ou Alphanumeric
{ 
  
  var text;
  var cpt;
  text=obj.value;
  
  cpt=0;
  if (typeMasque=="Numeric")
  {
   masque="0123456789";
  }
  else if (typeMasque=="Decimal")
  {
   masque="0123456789.,";
  }
  else if (typeMasque=="Alphanumeric")
  {
   masque="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  }  
  var car; 
  var booleen = true;

  for (var i=0;i<text.length;i++)
  {
   if(booleen)
              { 
    		car=text.charAt(i);
               /* if ( (typeMasque=="Decimal") && (car=='.' || car==',') )
                {
                 cpt=cpt+1;
                 
                }*/
                booleen=(masque.indexOf(car) != -1);
              }
  }
  if (cpt>1)
  {booleen=false;}
 
  if(booleen==false)
  {
   obj.focus();
   obj.style.background="#ffaaaa";
   alert(msg);
  }
  else
  {
   obj.style.background="#ffffff";
  }
  return booleen;
}


function checkDate(obj,msg) {
	var DateData;
	DateData= obj.value;
	correct = true;
	if (DateData!='' && DateData!=' ' && DateData!='  ' && DateData!='   ' && DateData!='    ')
	{
	  if ( (DateData.length < 11) && (DateData.length > 7) ) {
	    if (DateData.substring(2,3)=="/" && DateData.substring(5,6)=="/"){
	  	j = DateData.substring(0,2);
		m = DateData.substring(3,5);
		a = DateData.substring(6,DateData.length);
		bi = ((parseInt(a) % 4) == 0);


		if (((m == "01")||(m == "03")||(m == "05")||(m == "07")||(m == "08")||(m == "10")||(m == "12"))&&((Math.round(j)>=1)&&(Math.round(j)<=31))){
			correct = true;
		}
		else {
			if (((m == "04")||(m == "06")||(m == "09")||(m == "11"))&&((Math.round(j)>=1)&&(Math.round(j)<=30))){
				correct = true;
			}
			else {
				if (bi){correct=(((Math.round(j)>=1) && (Math.round(j)<=29)) && (m=="02"));}
				else   {correct=(((Math.round(j)>=1) && (Math.round(j)<=28)) && (m=="02"));}
			}
		}
	    }
	    else
	    {correct = false;}
	  }
	  else 
	  {correct = false;}
	  
	  
          if (correct==false)
          {obj.style.background="#ffaaaa";alert(msg);}
          else
          {obj.style.background="#ffffff";}
	  return correct;
	}
	else
	{
	 obj.style.background="#ffffff";
	 return correct;
	}
}
