//-----------------------------------------------------------------------------
// Déclaration de variables
//-----------------------------------------------------------------------------
var kDateformat = "DD/MM/YYYY"; // US: MM/DD/YYYY  EURO: DD/MM/YYYY

//-----------------------------------------------------------------------------
// Determine browser.
//-----------------------------------------------------------------------------
var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 && 
	       parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;

// --------------------------------------------------------------
// Formulaires 
// --------------------------------------------------------------
function VerifEmail(monform) {
      if (!/^[\.\w-]+@[\.\w-]+\.\w{2,4}$/g.test(monform.stremail.value)) {
        alert ("Veuillez entrer une adresse email valide.");
        monform.stremail.focus();
        return false;
      }
      return true;
}

function VerifURL(monform) {
      if (monform.substring(0,6)!="http://") {
               return false;
      }
      return true;
}

function VerifSaisEmail(champsemail) {  // Version 2
      if (!/^[\.\w-]+@[\.\w-]+\.\w{2,4}$/g.test(champsemail.value)) {
        if (isMinIE4) {
        	return false;
        } else {
        	return true;
        }
      } else {
        return true;
      }
}

function verifConnexion(monform) {
	var strmsg = " "
	if (monform.strlogin.value.length<1) {
		strmsg = strmsg + "\n - Identifiant trop court ou vide (4 caractères minimum)"
		monform.strlogin.focus();
	}
	
	if (monform.strpwd.value.length<1) {
		strmsg = strmsg + "\n - Mot de passe trop court ou vide (4 caractères minimum)"
		monform.strpwd.focus();
	}
	if (strmsg!=" ") {
		alert("Votre saisie est incorrecte, veuillez vérifier : " + strmsg);
		return false
	} else {
		return true
	}
}

function ChoixUnique(objet, Intindex) {
   // fonction qui empêche la selection multiple dans une collection de checkbox
   var i
   for (i=0; i<objet.length; i++) {
     if (Intindex!=i) {
       objet[i].checked=false
     }
   }
}
 
function ChoixUniqueSelectTab(objet, Intindex) {
   // fonction qui empêche la selection multiple dans une collection de checkbox
   var i
   for (i=0; i<objet.length; i++) {
     if (Intindex!=i) {
       objet[i].checked=false
     }
   }
}

/*********************************************************************
Method:   String.trim
Purpose:  Removing leading and trailing spaces
Inputs:   none
Returns:  string
*********************************************************************/
String.prototype.trim=function () {
	return this.replace(/^\s+|\s+$/g,"");
}

/*********************************************************************
Function: isDate
Purpose:  Check that value is a date of the correct format
Inputs:   oElement - form element
		  sFormat  - string format
Returns:  boolean
*********************************************************************/
function isDate(inDate) {
	var aDaysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var strDatestyle ; //date style
	
	sDate=inDate.trim();
	sFormat=kDateformat;
	
	if (sFormat.substr(0, 1)=="D")	
		strDatestyle = "EU";  //European date style
	else
		strDatestyle = "US"; //United States date style
	
	// Fetch the date separator from the user's input
	var sSepDate=sDate.charAt(sDate.search(/\D/));
	// Fetch the date separator from the format
	var sSepFormat=sFormat.charAt(sFormat.search(/[^MDY]/i));
	// Compare separators
	if (sSepDate!=sSepFormat)
		return false;
	
	// Fetch the three pieces of the date from the user's input and the format
	var aValueMDY=sDate.split(sSepDate,3);
	var aFormatMDY=sFormat.split(sSepFormat,3);
	
	// Assign day, month, year based on format
	var iMonth,iDay,iYear;
	if (strDatestyle == "US") {
		iMonth = aValueMDY[0];
		iDay   = aValueMDY[1];
		iYear  = aValueMDY[2];
	} else {
		iMonth = aValueMDY[1];
		iDay   = aValueMDY[0];
		iYear  = aValueMDY[2];
		}
	
	// Validate that all pieces of the date are numbers
	if (  !isInteger( iMonth )
		||!isInteger( iDay   )
		||!isInteger( iYear  ) )
		return false;
	
	// Require 4 digit year
	if(iYear.length!=4)
		return false;
	
	// Check for leap year
	var iDaysInMonth=(iMonth!=2)?aDaysInMonth[iMonth-1]:
		((iYear%4==0 && iYear%100!=0 || iYear % 400==0)?29:28);
	
	return (iDay!=null && iMonth!=null && iYear!=null
			&& iMonth<13 && iMonth>0 && iDay>0 && iDay<=iDaysInMonth);
}

/********************************************
Function: isInteger
Purpose:  Check that parameter is a number
Returns:  boolean
********************************************/
function isInteger(s) {
	if (s==null)
		s=this.element.value.trim();

	return (s.toString() && /(^-?\d\d*$)/.test(s));
}
