// Controles javascript compatibles NS2> IE3>

// VARIABLE DECLARATIONS

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var accentuatedLetters = "???????????"

var pureWhitespace=" ";
var whitespace = " \t\n\r";
var special = "-\' ";
var veryspecial = "-,;\' ()";
var defaultEmptyOK = false;

var regAscii = /^[a-zA-Z0-9 ??????????? \'\,.;\(\)\-]*$/i;
var regSimpleAscii = /^[a-zA-Z0-9 ???????????\'\-]*$/i;
var regAlphabetic = /^[a-zA-Z \-]*$/i;
var regAlphanumeric = /^[a-zA-Z0-9 \-]*$/i;
//var regNumeric = /^[0-9 \-]*$/i;
var regNumeric = /^[0-9]*$/i;
var regPrice = /^[0-9]+([\.\,]{1}[0-9]{1,2}){0,1}$/i;
var regEmail = /^[_a-z0-9-'???????????]+(\.[_a-z0-9-'???????????]+)*@[a-z0-9-'???????????]+(\.[a-z0-9-'???????????]+)+$/i;
var regWeb = /^(http:\/\/){0,1}[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}/;
var regPassword = /^[a-zA-Z_0-9\-]*$/i;

var old_regNumPhone = /^(\(\+?\d{2,3}\))?\d{3}-?\d{3,8}$/;
/*******************************************************************
 * Up to 20 char ext to 50Minimum 8 digit Number(0-9) , +, ( ), space, -
 *******************************************************************/
var regNumPhone = /^([\(?\-?\)?\ ?\+?]*\d){8,20}[\(?\-?\)?\ ?\+?]*$/;

/***************************************************************************
 *  extends Object String with trim
 *
 *  skip leading and trailing whitespace and return everything in between
 *
 ***************************************************************************/

String.prototype.trim = function() {
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag){
    var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}



// Removes all characters which do NOT appear in string bag
// from string s.

function stripCharsNotInBag (s, bag) {

    var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}



// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s){
	return stripCharsInBag (s, whitespace)
}


// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripLeading (s) {
	var i = 0;
	while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
       	i++;
    	return s.substring (i, s.length);
}

// Removes trailing whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripTrailing (s) {
	var i = s.length-1;
	while ((i >= 0) && (whitespace.indexOf(s.charAt(i)) != -1))
       	i--;
    	return s.substring (0, i+1);
}

// Removes leading and trailing whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function strip (s) {
	if (window.RegExp){
		return s.trim();
	} else {
		return stripLeading(stripTrailing(s));
	}
}

// for backward compatibility with ES

function Trim (s) {
	return strip (s);
}

// Check whether string s is empty.

function isEmptyOrNull(s){
	if (window.RegExp){
		return ((s==null)||(s.trim().length == 0))
	} else {
		var r=strip(s);
		return ((r==null)||(r.length == 0));
	}
}

function isLetter (c) {
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

function isAccentuated (c) {
	return ((accentuatedLetters.indexOf(c) != -1))
}

function isWhitespace (c) {
	return ((whitespace.indexOf(c) != -1))
}

function isSpecialChar (c) {
	return ((special.indexOf(c) != -1))
}
function isVerySpecialChar (c) {
	return ((veryspecial.indexOf(c) != -1))
}

function isLetterOrDigit (c) {
	return (isLetter(c) || isDigit(c))
}

function isLetterOrDigitOrAccentuated (c) {
	return (isLetter(c) || isDigit(c) || isAccentuated (c))
}

function isInteger (s){
	var i;
    	for (i = 0; i < s.length; i++){
        	// Check that current character is number or letter.
        	var c = s.charAt(i);
        	if (! isDigit(c) ) {
        		return false;
        	}
    	}
    	return true;
}


function isIntegerInRange (s, a, b) {
    if (! isInteger(s)) return false;
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function isNumeric (s){
	if (window.RegExp){
		return (regNumeric.exec(s) != null)
    	} else {
		var i;
	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! isDigit(c) ) {
	        		return false;
	        	}
	    	}
	    	return true;
    	}
}

function isPrice (s) {
//	if (window.RegExp){
//		return (regPrice.exec(s) != null)
//    	} else {
		var i;
	  var j;

	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! isDigit(c) ) {
	        		if (c=='.')
	        		{
	        			if (i==0)
	        			{return false;}
	        			for (j = i+1; j < s.length; j++){
	        				c = s.charAt(j);
	        				if (! isDigit(c) )
	        				{return false;}
	        			}
	        			if (j>i+3)
	        			{return false}
	        			i = j;
	        		}
	        		else if (c==',')
	        		{
	        			if (i==0)
	        			{return false;}
	        			for (j = i+1; j < s.length; j++){
	        				c = s.charAt(j);
	        				if (! isDigit(c) )
	        				{return false;}
	        			}
	        			if (j>i+3)
	        			{return false}
	        			i = j;
	        		} else
	        			{return false;}
	        	}
	    	}
	    	return true;
 //   	}
}

function isAlphanumeric (s){
	if (window.RegExp){
		return (regAlphanumeric.exec(s) != null)
    	} else {
		var i;
	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! (isLetter(c) || isDigit(c) || isWhitespace(c) ) ){
	        		return false;
	        	}
	    	}
	    	return true;
    	}
}

function isAlphabetic (s){
	if (window.RegExp){
		return (regAlphabetic.exec(s) != null)
    	} else {
		var i;
	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! isLetter(c)) {
	        		return false;
	        	}
	    	}
	    	return true;
    	}
}

/*  ================================================================
    FUNCTION:  isAscii(st)
    RETURNS:  true, if the st is alphabetic + Accentuated + "- "
	      false, otherwise
    ================================================================ */


function isAscii (s){
	if (window.RegExp){
		return (regAscii.exec(s) != null)
    	} else {
		var i;
	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! (isLetter(c) || isAccentuated(c) || isWhitespace(c) || isNumeric(c) || isVerySpecialChar(c)) ){
	        		return false;
	        	}
	    	}
	    	return true;
    	}
}

function isSimpleAscii (s){
	if (window.RegExp){
		return (regSimpleAscii.exec(s) != null)
    	} else {
		var i;
	    	for (i = 0; i < s.length; i++){
	        	// Check that current character is number or letter.
	        	var c = s.charAt(i);
	        	if (! (isLetter(c) || isAccentuated(c) || isWhitespace(c) || isNumeric(c) || isSpecialChar(c)) ){
	        		return false;
	        	}
	    	}
	    	return true;
    	}
}


/*  ================================================================
    FUNCTION:  isCreditCard(st)
    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  if (st.length > 19)
    return (false);
  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}


//----------------------------------------
// isEmail (STRING s )
// Email address must be of form a@b.c
//----------------------------------------



function isValidEmail (s) {
	var email = stripTrailing(s);
	if ( isEmptyOrNull(email)) {
		return false;
	}else{
		if (window.RegExp){
			return ((regEmail.exec(email) != null));
		}else{
			var arobase = email.indexOf("@");
			var point = email.lastIndexOf(".");
			if((arobase < 3)||(point + 2 > email.length)||(point < arobase+3)){
				return false;
			}
			return true;
		}
	}
}

function isValidWebAddress (s) {
	var web = stripTrailing(s);
	if ( isEmptyOrNull(web)) {
		return false;
	}else{
		if (window.RegExp){
			return ((regWeb.exec(web) != null));
		}else{
			return true;
		}
	}
}

function isValidPassword (s) {
	var pwd=stripTrailing(s);

	if(isEmptyOrNull(pwd)){
		return false;
	} else {
		if (window.RegExp) {
			if (pwd.length < 5 || pwd.length > 50){
				return false;
			} else {
				return (regPassword.exec(pwd) != null);
			}
		} else {
			if (pwd.length < 5 || pwd.length > 50){
				return false;
			}
		}
	}
}

function isValidFirstName (s) {
	var name=stripTrailing(s);
	if ( isEmptyOrNull(name)) {
		return false;
	} else {
		if ( name.length > 50 ){
			return false;
		} else {
			return ( isSimpleAscii(name));
		}
	}
}

function isValidLastName (s) {
	return isValidFirstName(s);
}

function isValidStreetAddress(s) {
	var street=stripTrailing(s);
	if ( isEmptyOrNull(street)) {
		return false;
	} else {
		if ( street.length > 29 ){
			return false;
		} else {
			return ( isAscii(street));
		}
	}
}

function isValidCity(s) {
	var city=stripTrailing(s);
	if ( isEmptyOrNull(city)) {
		return false;
	} else {
		if ( city.length > 32 ){
			return false;
		} else {
			return ( isAscii(city));
		}
	}
}

function isValidZipCode(s) {
	var zip=stripTrailing(s);
	if ( isEmptyOrNull(zip)) {
		return false;
	} else {
		if ( zip.length > 10 ){
			return false;
		} else {
			return ( isAlphanumeric(zip));
		}
	}
}

function isValidPhoneNumber(s) {
	var num=stripWhitespace(s);
	if(isEmptyOrNull(num)){
		return false;
	} else {
		if (window.RegExp) {
			return (regNumPhone.exec(num) != null);
		} else {
			if ( num.length < 6 ){
				return false;
			} else {
				return ( isNumeric(num));
			}
		}
	}
	return true;
}

function isValidFtpPin (s){
	var pin=stripTrailing (s);
	if ( isEmptyOrNull (pin)) {
		return false;
	} else {
		return ( isAlphanumeric (pin));
	}
}

function isValidFtpNumber(cc){


	/*

exemple FTP : 83254210221212500 de test qui ne passe pas

  first4digs = cc.substring(0,4);
  if ((cc.length == 17) && (first4digs == "8325"))
    return isCreditCard(cc);
  return false;

  	*/
  if (!isNumeric(cc))
   return false;
  else {
		return isCreditCard(cc);
	}
}


function areEquals(s1,s2){
	return ((stripTrailing(s1) == stripTrailing(s2)));
}


// Validate the date
// two formats are accepted :
// - format = 1 : "dd/mm/yy"
// - format = 2 :"dd/mm/yyyy"
// - format = 3 : d(d)/m(m)/(yy)yy

function isDateValid(dateToTest,format)
{

   if (window.RegExp){
	var date = dateToTest;
	var separator = "/";
	var day;
	var month;
	var year;
	var leap;
	var regDate = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}$/;
	if (regDate.exec(dateToTest) == null)
		return false;

	if (   (format == 1 && date.length != 8)
			|| (format == 2 && date.length != 10)
			|| (format == 3 && date.length <6)
	)
		return false;

	// Looking for separators
	if (format!= 3) {
		if (date.indexOf(separator,1) != 2 || date.indexOf(separator,4) != 5)
			return false;
	} else {
		firstSepar = dateToTest.indexOf(separator,1) ;

   		if (firstSepar != 2)
    		dateToTest = "0"+dateToTest ;

   		secondSepar = dateToTest.indexOf(separator,4) ;
   		if (secondSepar != 5)
			dateToTest = dateToTest.substr(0,3)+"0"+dateToTest.substr(3) ;

	}

	// Validation of year
	if (date.length == 8) {
		year = date.substr(6,2);
		leap = isLeapYear("20" + year);
	}
	else {
		year = date.substr(6,4);
		leap = isLeapYear(year);
	}
	
	// the year 00 is not a valid date
	if (year == "00" || year == "0000"){
		return false;
	}

	// Validation of month
	month = date.substr(3,2);
	if ((month < 1) || (month > 12))
		return false;

	// Validation of day
	day = date.substr(0,2);
	if (day < 1)
		return false;

	if ( (month == 2) && (leap) && (day > 29) ) {
		return false;
	}
	if ( (month == 2) && (!leap) && (day > 28) ) {
		return false;
	}

	// Validation of other months
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
		return false;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
		return false;
	}
   }
   return true;
}

// Test if the year is a leap year
// a year is a leap year if it could be divided by 4
// and not by 100 or by 400
// year format : yyyy
function isLeapYear(year)
{
	if ( (year%4==0 && year%100!=0) || (year%400==0))
		return true;
	else
		return false;
}

function convertStringsToDate(dateString, hourString) {

	if (!isDateValid(dateString,3))
		return null;

	firstSepar = dateString.indexOf('/',1) ;

   	if (firstSepar != 2)
    	dateString = "0"+dateString ;

   	secondSepar = dateString.indexOf('/',4) ;
   	if (secondSepar != 5)
		dateString = dateString.substr(0,3)+"0"+dateString.substr(3)

	// here the string is dd/mm/YYYY
	dayStr = dateString.substr(0,2) ;
	monthStr = dateString.substr(3,2) ;

	if (dateString.length == 8)
		yearStr = "20" + dateString.substr(6,2) ;
	else
		yearStr = dateString.substr(6,4) ;

	// hourString = hh:mm
	hourStr = hourString.substr(0,2);
	minStr = hourString.substr(3,2);

	// new Date(yy,mm,dd,hh,mm,ss)
	convertedString = new Date (yearStr, eval(monthStr)-1,dayStr,hourStr,minStr,'00')

	return convertedString ;

}

function convertStringToDate(dateString) {

	if (!isDateValid(dateString,3))
		return null;

	firstSepar = dateString.indexOf('/',1) ;

   	if (firstSepar != 2)
    	dateString = "0"+dateString ;

   	secondSepar = dateString.indexOf('/',4) ;
   	if (secondSepar != 5)
		dateString = dateString.substr(0,3)+"0"+dateString.substr(3)

	dayStr = dateString.substr(0,2) ;
	monthStr = dateString.substr(3,2) ;

	if (dateString.length == 8)
		yearStr = "20"+dateString.substr(6,2) ;
	else
		yearStr = dateString.substr(6,4) ;

	convertedString = new Date (yearStr, eval(monthStr)-1,dayStr)

	return convertedString ;

}

function convertStringWithHourToDate(dateString){
	firstSepar = dateString.indexOf('/',1) ;

   	if (firstSepar != 2)
    	dateString = "0"+dateString ;

   	secondSepar = dateString.indexOf('/',4) ;
   	if (secondSepar != 5)
		dateString = dateString.substr(0,3)+"0"+dateString.substr(3)

	// here the string is dd/mm/YYYY
	dayStr = dateString.substr(0,2) ;
	monthStr = dateString.substr(3,2) ;
	yearStr = dateString.substr(6,4) ;
	hourStr = dateString.substr(11,2);
	minStr = dateString.substr(14,2);

	// new Date(yy,mm,dd,hh,mm,ss)
	convertedString = new Date (yearStr, eval(monthStr)-1,dayStr,hourStr,minStr,'00')

	return convertedString ;
}


// Format : dd/mm/yy
function convertDateToString(theDate) {
	d = (theDate.getDate()<10)?"0" + theDate.getDate() : theDate.getDate();
	m = (theDate.getMonth()+1 < 10)? "0" + (theDate.getMonth()+1): (theDate.getMonth()+1);
//	y = (theDate.getFullYear() - 2000 < 10)?"0" + (theDate.getFullYear() - 2000):theDate.getFullYear() - 2000;
	y = theDate.getFullYear();

	return d + "/" + m + "/" + y;
}
// dd/mm/yyyy,hh:mm
function convertDateToStringWithHour(theDate) {
	d = (theDate.getDate()<10)?"0" + theDate.getDate() : theDate.getDate();
	m = (theDate.getMonth()+1 < 10)? "0" + (theDate.getMonth()+1): (theDate.getMonth()+1);
	y = theDate.getFullYear();
	h = (theDate.getHours()<10?"0" + theDate.getHours() : theDate.getHours());
	min = (theDate.getMinutes()<10?"0" + theDate.getMinutes() : theDate.getMinutes());

	return d + "/" + m + "/" + y + "," + h + ":" + min;
}

function isDateBeforeToday(dateString) {

	dateConverted = convertStringToDate(dateString) ;

	return dateConverted < new Date() ;

}

function isDateAfterToday(dateString) {

	dateConverted = convertStringToDate(dateString) ;

	return dateConverted > new Date() ;

}

function isValidPnr (s) {
	return true;
}

// like getLabel(local, type, code, params)
// insert a parameter (String) in place of {0} in the label
function insertParamInLabel(labell, param)
{
	firstSepar = labell.indexOf('{',1) ;
	if (firstSepar > 1)
	{
		return labell.substr(0,firstSepar) + param + labell.substr(firstSepar+3);
	} else {
		return labell ;
	}
}

// convert time with format : "hh:00" to time with format : "hh am" or "hh pm"
function convertEstarTimeFormatToExpedia(time) {
	tmpHour = "";
	for(i=0;i<2;i++){
		tmpHour+=time.charAt(i);
	}
	if(tmpHour.charAt(0)=='0'){
		tmpHour=tmpHour.charAt(1)+"am";
	 }else if(tmpHour.charAt(0)=='1'){
		if(tmpHour.charAt(1)=='0' || tmpHour.charAt(1)=='1')
			tmpHour+="am";
		else {
			tmp = 1*tmpHour%12;
			tmpHour=tmp+"pm";
			}
	} else if(tmpHour.charAt(0)=='2'){
		tmp = 1*tmpHour%12;
		if(tmpHour.charAt(1)=='4')
			tmpHour=tmp+"am";
		else
			tmpHour=tmp+"pm";
	}
	return tmpHour;

}
