var ColorError = '#EFEBD7';
var Color = 'white';

var dt, jj, mm, yyyy, today;

dt = new Date();

jj = dt.getDate().toString();
if (jj.length==1){jj = '0'+jj;}
mm = (dt.getMonth()+1).toString();
if (mm.length==1) {mm = '0'+mm;}
yyyy = dt.getYear();
today= jj+'/'+mm+'/'+yyyy;

var constEmpty = "";
var constWhiteSpace = " \t\n\r";	// whitespace characters
var daysInMonth = makeArray(12);

daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0;
   }
   return this;
}

function stripWhiteSpace(sw1sStr)
{   
	var sw1iCnt;
	var sw1sChr;
	var sw1sReturn = "";
	
	if (isEmpty(sw1sStr))
		return "";
	
    // Search through string's characters one by one.
    // If character is not whilespace, append to sw1sReutrn.
    for (sw1iCnt = 0; sw1iCnt < sw1sStr.length; sw1iCnt++)
    {   
        // Check that current character isn't whitespace.
        sw1sChr = sw1sStr.charAt(sw1iCnt);
        if (constWhiteSpace.indexOf(sw1sChr) == -1) sw1sReturn += sw1sChr;
    }
    return sw1sReturn;
}

function isEmpty(ie1s)
{   
	return ((ie1s == null) || (ie1s.length == 0));
}

function isDigit(id1sChar)
{   
	return ((id1sChar >= "0") && (id1sChar <= "9"));
}

// isIntegerINRange (STRING ii1s [, BOOLEAN emptyOK])
// Returns true if all characters in string ii1s is between is1iFrom and is1iTo.
// Accepts non-signed integers only.
// is1iFrom and is1iTo must be non-signed integers.
function isIntegerInRange(is1sNum, is1iFrom, is1iTo)
{   
	if (isEmpty(is1sNum)) 
	{
		if (isIntegerInRange.arguments.length == 3) 
			return constEmpty;
		else 
			return (isIntegerInRange.arguments[3] == true);
	}

	if (!isInteger(is1sNum, false)) 
		return false;

    var is1iNum = parseInt(is1sNum);

		return ((is1iNum >= is1iFrom) && (is1iNum <= is1iTo));
}

// isInteger (STRING ii1s [, BOOLEAN emptyOK])
// Returns true if all characters in string ii1s are numbers.
// Accepts non-signed integers only
function isInteger(ii1s)
{   
	var ii1iCnt;

    if (isEmpty(ii1s)) 
		if (isInteger.arguments.length == 1) 
			return constEmpty;
		else 
			return (isInteger.arguments[1] == true);

		for (ii1iCnt = 0; ii1iCnt < ii1s.length; ii1iCnt++)
		{   
			var ii1sChar = ii1s.charAt(ii1iCnt);
			if (!isDigit(ii1sChar)) return false;
		}
    // All characters are numbers.
    return true;
}

// isYear (STRING iy1s [, BOOLEAN emptyOK])
// isYear returns true if string iy1s is a valid 
// Year number.  Must be 4 digits only.
function isYear(iy1s)
{   if (isEmpty(iy1s)) 
		if (isYear.arguments.length == 1) 
			return constEmpty;
		else 
			return (isYear.arguments[1] == true);
    
    if (!isInteger(iy1s)) 
		return false;
  
	return iy1s.length == 4;
}

// isMonth (STRING im1s [, BOOLEAN emptyOK])
// isMonth returns true if string im1s is a valid 
// month number between 1 and 12.
function isMonth(im1s)
{   if (isEmpty(im1s)) 
		if (isMonth.arguments.length == 1) 
			return constEmpty;
		else 
			return (isMonth.arguments[1] == true);

	return isIntegerInRange (im1s, 1, 12);
}

// isDay (STRING id1s [, BOOLEAN emptyOK])
// isDay returns true if string id1s is a valid 
// day number between 1 and 31.
function isDay(id1s)
{   if (isEmpty(id1s)) 
		if (isDay.arguments.length == 1) 
			return defaultEmptyOK;
		else 
			return (isDay.arguments[1] == true);   
		
	return isIntegerInRange (id1s, 1, 31);
}

// daysInFebruary (INTEGER year)
// Given integer argument year,
// returns number of days in February of that year.
function daysInFebruary (di1iYear)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((di1iYear % 4 == 0) && ( (!(di1iYear % 100 == 0)) || (di1iYear % 400 == 0) ) ) ? 29 : 28 );
}

// isDate (STRING date)
// isDate returns true if string date
// form a valid date.
function isDate(date)
{
    if (isEmpty(date)) 
		if (isDate.arguments.length == 1) 
			return constEmpty;
		else 
			return (isDate.arguments[1] == true);
			
	var year, month, day, iYear, iMonth, iDay, icnt, itmp, tmpdate, separator, arrDate
	
	// find out what separator is
	date = stripWhiteSpace(date)
	tmpdate = date;
	while (isDigit(tmpdate.charAt(0)))	
		tmpdate=tmpdate.substring(1,tmpdate.length);
	separator = tmpdate.charAt(0);
	
	arrDate = date.split(separator);
	
	if (arrDate.length != 3)
		return false;
		
	if (isInteger(arrDate[0],false) && isInteger(arrDate[1],false) && isInteger(arrDate[2],false)) 	
		;
	else
		return false;	
	// parse day, month and year
	// default format is DD/MM/YYYY
	day = "";
	month = "";
	year = "";
	iDay = 0;		
	iMonth = 0;
	iYear = 0;
    for (icnt=0; icnt<3; icnt++)
    {
		// get rid of 0 in front of a number
		itmp = arrDate[icnt];
		while (itmp.charAt(0) == "0" && itmp.length > 1)
			itmp=itmp.substring(1,itmp.length);		
		
		itmp = parseInt(itmp,10);		
		if (itmp == 0)
		{
			iYear++;	
			year = itmp+"";				
		}	
		else if (itmp < 13)
		{
			iMonth++;
			if (isEmpty(day))
				day = itmp+"";
			else if (isEmpty(month))
				month = itmp+"";
			else
				year = itmp+"";					
		}	
		else if (itmp < 32)	
		{
			iDay++;
			if (isEmpty(day))
				day = itmp+"";
			else
				year = itmp+"";				
		}		
		else
		{
			iYear++;	
			year = itmp+"";				
		}	
	}	

	if (iYear > 1 || iDay > 2)
	{
		return false;
	}	

if (parseInt(arrDate[0],10)==month)	
	{
	if (parseInt(arrDate[1],10)!=day)
	return false;
	}
		
//alert("m="+month+" d="+day+" y="+year);

//catch invalid years and invalid months and days.
	if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
// Explicitly change type to integer to make code work in both
// JavaScript 1.1 and JavaScript 1.2.
	var intYear = parseInt(year,10);
	var intMonth = parseInt(month,10);
	var intDay = parseInt(day,10);
// catch invalid days, except for February
	if (intDay > daysInMonth[intMonth]) return false; 
	if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
	return true;
}


function dateToNB(ladate)	
{
	stripWhiteSpace(ladate);
	tmpdate = ladate;
	while (isDigit(tmpdate.charAt(0)))	
		tmpdate=tmpdate.substring(1,tmpdate.length);
	separator = tmpdate.charAt(0);
	arrDate = ladate.split(separator);
	la=arrDate[2];
	da=arrDate[1];
	te=arrDate[0];
	//alert (la+da+te);
	return (parseInt(la+da+te));
}

function comparedate(bdt,edt,sCaption)	
{
	if ((dateToNB(edt)) <= (dateToNB(bdt)))
		{
		alert ("Cette date ne doit pas précéder ou être égal a " + sCaption);
		return false;
		} else {
		return true;
		}	
}

function ValidateDate(oElement, sCaption) {
	if (!isDate(oElement.value))
		{
		alert(sCaption + " n\'est pas une date valide");
		return false;
		}
		else
		{
		return true;
		}
	
}

function ChampVide(strNom,strMsg)
{
//Verifie si le champ passé en parametre est vide ou nom
	if (document.all[strNom].value.replace(/[ ]{1}/g,'')=='')
		{document.all[strNom].style.backgroundColor=ColorError;
		document.all[strNom].focus();
		if (strMsg!=='')
			{alert(strMsg);}
		return true;}
	else
		{
		document.all[strNom].style.backgroundColor= Color;
		return false;
		}
}

function VerifCnt(strNom,strType,strMsg)
//VERIFIE LE CONTENU DES CHAINES
//strNom=Nom du champ dont on évalue le contenu
//strType=pattern à utiliser AlphaNum,Num ou Mny : NUM-ALN-ALP-MNY
//strMsg=chaine à renvoyer
{
	Alpha = new RegExp('[^a-z^\' -]','gi');
	Num = new RegExp('[^0-9]','gi');
	AlphaNum = new RegExp('[^0-9a-z éèêêîïàùç,\']{1}','gi');
	Mail = new RegExp('^.+[@]{1}.+[\.]{1}.+$','i');
	Mny = /^\d{1,7}\.?\d*$/
	Avr = /^\-?\d{1,7}\.?\d*$/
	if (strType=='MNY'){
		if (Mny.test(document.all[strNom].value)){
			if ((document.all.pk_cur && document.all.pk_cur.value=='EUR' && document.all[strNom].value>327360312)||(document.all.pk_cur && document.all.pk_cur.value=='FRF' && document.all[strNom].value>2147483646)||(!document.all.pk_cur && document.all[strNom].value>327360312))
			{
				document.all[strNom].style.backgroundColor='#EFEBD7';
				document.all[strNom].focus();
				if (strMsg!=='') alert(strMsg);
				return true;
			}
			else {
			document.all[strNom].style.backgroundColor='white';
			return false;
			}
		}
		else
		{
		document.all[strNom].style.backgroundColor='#EFEBD7';
		document.all[strNom].focus();
		if (strMsg!=='') alert(strMsg);
		return true;
		}
	}

	if (strType=='AVR'){
		if (Avr.test(document.all[strNom].value)){
			document.all[strNom].style.backgroundColor='white';
			return false;
		}
		else
		{
		document.all[strNom].style.backgroundColor='#EFEBD7';
		document.all[strNom].focus();
		if (strMsg!=='') alert(strMsg);
		return true;
		}
	}

if (strType=='NUM')
	{if (Num.test(document.all[strNom].value))
		{document.all[strNom].style.backgroundColor=ColorError;
		document.all[strNom].focus();
		if (strMsg!=='')
			{alert(strMsg);}
		return true;}
	else
		{document.all[strNom].style.backgroundColor= Color;
		return false;}
	}

if (strType=='ALN')
	{if (AlphaNum.test(document.all[strNom].value.toUpperCase()))
		{document.all[strNom].style.backgroundColor=ColorError;
		document.all[strNom].focus();
		if (strMsg!=='') {
			alert(strMsg);}
			return true;
		}
	else
		{
		document.all[strNom].style.backgroundColor= Color;
		return false;
		}
	}
if (strType=='ALP')
	{if (Alpha.test(document.all[strNom].value.toUpperCase()))
		{document.all[strNom].style.backgroundColor=ColorError;
		document.all[strNom].focus();
		if (strMsg!=='')
			{alert(strMsg);}
		return true;}
	else
		{document.all[strNom].style.backgroundColor= Color;
		return false;}
	}
if (strType=='MAI')
	
	{if (!Mail.test(document.all[strNom].value.toUpperCase()))
		{document.all[strNom].style.backgroundColor=ColorError;
		document.all[strNom].focus();
		if (strMsg!=='')
			{alert(strMsg);}
		return true;}
	else
		{document.all[strNom].style.backgroundColor= Color;
		return false;}
	}
}



function formatNumber(num,places)
{
	num=parseFloat(num);
	if (places < 0 || Math.floor(places) != places) return num;
	num = (Math.round(num * Math.pow(10,places)) / Math.pow(10,places)) + '';
	if (num.indexOf('.') < 0) num += '.';
	while (num.length - num.indexOf('.') -1 < places) num+=0;
	return (num.charAt(num.length - 1) == '.') ? num.substring(0,num.length-1) : num;
}

function checkLengthTA( oTextArea, maxLength, sMsg){
	if (oTextArea.value.length>maxLength){
		if (sMsg.length>0)
		{
			alert(sMsg);
			oTextArea.style.backgroundColor=ColorError;
			oTextArea.focus();		
		}else{
			oTextArea.style.backgroundColor=Color;
		}
		return false;
	}
	return true;
}


function limitLengthTA( oTextArea, maxLength){
	var str;
	if (oTextArea.value.length>maxLength){
		str  = oTextArea.value.substring(0,maxLength);
		oTextArea.value = str; 
	}
}
