// JavaScript Document
// Fichier javascript général pour tout le site sur les formulaires uniquement
// --------------------------------------------------------------
// 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)) {
        	return false;
      } else {
        return true;
      }
}

/*********************************************************************
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 = "DD/MM/YYYY";
	
	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);
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDatev2(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[3]; // p@rse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        //alert("Month " + month + " doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            //alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

/********************************************
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));
}

/********************************************
Gestion de champs de quantité
********************************************/
function AjouterQuant(IntValeur) {
	if ((IntValeur==null) || !(isInteger(IntValeur))) {
		IntValeur=1;
	} else {
		IntValeur++; 
	}
	return IntValeur;
}

function EnleverQuant(IntValeur) {
	if ((IntValeur==null) || !(isInteger(IntValeur))) {
		IntValeur=1;
	} else if (IntValeur>1) {
		IntValeur--; 
	}
	return IntValeur;
}

function numberPlease(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		var charCode = (evt.charCode || evt.charCode==0) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : evt.which);
		if (charCode>13 && (charCode<48 || charCode>57)) {
			alert('chiffres uniquement !');
			if (evt.returnVale) {
				evt.returnValue = false;
			} else if (evt.preventDefault) {
				evt.preventeDefault();
			} else {
				return false;
			}
		}
	}
}
