// -------------------------------------------------------------------------
// Javascript utility functions
// -------------------------------------------------------------------------
//
// Global variables needed for this module
//

// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.

var defaultEmptyOK = false
// whitespace characters
var whitespace = " \t\n\r";
// for NIF validation
var NIFdelimiters = " -";


// IsType (STRING ctl , STRING ptype, [, BOOLEAN emptyOK])
// 
// Returns true if the type of ctl is egal to ptype else returns false 
// ptype = 'n' for a numeric 
// ptype = 'a' for a Alphabetic string
// ptype = 'd' for a day
// ptype = 'm' for a month
// ptype = 'y' for a year
// ptype = 'sec' for a second
// ptype = 'mn' for a minute
// ptype = 'h' for a hour
// ptype = 'mail' for a mail

function isType(ctl, ptype)
{   
	//IsEmpty
	if (isEmpty(ctl)){ 
       if (isType.arguments.length == 2) return defaultEmptyOK;
       else return (isType.arguments[1] == true);
    }   
       
    //Call functions
	if (ptype == 'n') {
		return IsNumeric(ctl, "");
	}
	if (ptype == 's') {
		return isAlphabetic(ctl, "");
	}
	if (ptype == 'd') {
		return isDay(ctl, "");
	}
	if (ptype == 'm') {
		return isMonth(ctl, "");
	}
	if (ptype == 'y') {
		return isYear(ctl, "");
	}
	if (ptype == 'sec') {
		return isMinute(ctl, "");
	}
	if (ptype == 'mn') {
		return isMinute(ctl, "");
	}
	if (ptype == 'h') {
		return isHour(ctl, "");
	}
	if (ptype == 'mail') {
		return isEmail(ctl, "");
	}	
}

// Compare (STRING ctl1 ,STRING ctl2, STRING ptype, STRING op[, BOOLEAN emptyOK])
//
// ptype to indicate the type of the 2 first parameters
// ptype = 'n' for a numeric
// ptype = 's' for a string
// ptype = 'f' for a date (fecha) with the format YYYY/MM/DD !!! 
// ˇˇˇˇFirst Use the function IsDate to test is ctl1 and ctl2 are valided dates!!!!!
//
// op = '==' to test the egality
// op = '!=' to test the difference
// op = '>' to test the superiority
// op = '>=' to test the superiority or the egality
// op = '<' to test the inferiority
// op = '<=' to test the inferiority or the egality
//
function compare(ctl1, ctl2, ptype, op)
{   
	var nctl1, nctl2;
	var sctl1, sctl2;
	var dctl1, dctl2, mctl1, mctl2, yctl1, yctl2;//YYYY/MM/DD
	
	//IsEmpty
	if (isEmpty(ctl1)||isEmpty(ctl2)){ 
       if (compare.arguments.length == 4) return defaultEmptyOK;
       else return (compare.arguments[1] == true);
    }
    
    //generic...	
	if (op == '=='){
		return (ctl1 == ctl2);}
		
    if (op == '!='){
		return (ctl1 != ctl2);}
	
	//Numbers tests
	if (ptype == 'n'){
		nctl1 = parseInt(ctl1, 10);
		nctl2 = parseInt(ctl2, 10);
			
		if (op == '>'){
			return (nctl1 > nctl2);}		
		if (op == '>='){
			return (nctl1 >= nctl2);}
		if (op == '<'){
			return (nctl1 < nctl2);}
		if (op == '<='){
			return (nctl1 <= nctl2);}
	}
	
	//Strings tests
	if (ptype == 's'){
		sctl1 = String(ctl1);
		sctl2 = String(ctl2);
			
		if (op == '>'){
			return (sctl1 > sctl2);}		
		if (op == '>='){
			return (sctl1 >= sctl2);}
		if (op == '<'){
			return (sctl1 < sctl2);}
		if (op == '<='){
			return (sctl1 <= sctl2);}
	}	
			
	//Dates Tests
	if (ptype == 'f'){
		sctl1 = String(ctl1);
		sctl2 = String(ctl2);
		if (sctl1.length == 10){
			yctl1 = parseInt(sctl1.substring(0,4), 10)
			mctl1 = parseInt(sctl1.substring(5,7), 10)
			dctl1 = parseInt(sctl1.substring(8,10), 10)
		}
		else{return false;}
		
		if (sctl2.length == 10){
			yctl2 = parseInt(sctl2.substring(0,4), 10)
			mctl2 = parseInt(sctl2.substring(5,7), 10)
			dctl2 = parseInt(sctl2.substring(8,10), 10)
		}
		else{return false;}
		
		if (op == '>'){
			return ((yctl1 > yctl2) || ((yctl1 == yctl2)&&(mctl1 > mctl2)) || ((yctl1 == yctl2)&&(mctl1 == mctl2)&&(dctl1 > dctl2)))}
		if (op == '>='){
			return ((yctl1 > yctl2) || ((yctl1 == yctl2)&&(mctl1 > mctl2)) || ((yctl1 == yctl2)&&(mctl1 == mctl2)&&(dctl1 >= dctl2)))}
		if (op == '<'){
			return ((yctl1 < yctl2) || ((yctl1 == yctl2)&&(mctl1 < mctl2)) || ((yctl1 == yctl2)&&(mctl1 == mctl2)&&(dctl1 < dctl2)))}
		if (op == '<='){
			return ((yctl1 < yctl2) || ((yctl1 == yctl2)&&(mctl1 < mctl2)) || ((yctl1 == yctl2)&&(mctl1 == mctl2)&&(dctl1 <= dctl2)))}
	}		
}

//
//
// Return true if sNIF is a valid NIF of CIF
//
function isNIF(sNIF)
{
   var tmp, tmpdigits;
   var tmpNIF;
   
   // tmp contains the stripped NIF
   tmp = stripCharsInBag (sNIF, NIFdelimiters);
   
   // Convert to uppercase
   tmp = tmp.toUpperCase();
     
   // Check if we have a NIF or a CIF
   if (isAlphabetic(tmp.substr(0,1))) {
      // it seems a CIF
      // Check disabled from 17/12/99 (JLM)
      //
      // if (IsNumeric(tmp.substr(1,8), "")) {
      //    return true;
      // }
      // else {
      //    return false;
      // }
      
      //
      // everything goes!! 
      return true;
   }
   else {
      // it seems a NIF      
      // Check length, must be 9
      if (tmp.length != 9) {
         return false;
      }
      
      tmpdigits = tmp.substr(0,8);
      
      if (IsNumeric(tmpdigits, "")) {
         tmpNIF = NIFCalculation(parseInt(tmpdigits, 10), 8);

         if (tmpNIF == tmp)
            return true
         else
            return false;
      }
      else {
         return false;
      }      
   }
}

function NIFCalculation( lngDNI, lngdigits) 
{
   var letters = "TRWAGMYFPDXBNJZSQVHLCKEI";
   var pos;
   
   pos = lngDNI % 23;
   
   return padZeros(lngDNI, lngdigits) + letters.substr(pos, 1);
}

// This function returns a string padded with leading zeros
function padZeros(num, totalLen) 
{   
   var numStr = num.toString();             // Initialize return value
                                            // as string   
   var numZeros = totalLen - numStr.length; // Calculate no. of zeros
   
   if (numZeros > 0) {
      for (var i = 1; i <= numZeros; i++) {
         numStr = "0" + numStr;
      }
   }
   
   return numStr;
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true

function isSignedInteger (s)

{
	if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];		
        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;            
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}


//
//
//
function IsFilled(sBuffer, sMessage)
{
   if (sBuffer == "") {
		if (sMessage != "")
      	alert(sMessage);
      return false;
   }
   return true;
}

//
//  check if a field contains only numeric digits
//
function IsNumeric(aVar, sMessage)
{
	var aVarlen;
	var digitos = "0123456789";
	
	aVarlen = aVar.length;
	
	for(var i = 0; i < aVarlen; i++) {
		var subcadena = aVar.substring(i, i + 1);

		if (digitos.indexOf(subcadena) < 0) {
         if (sMessage != "") 
   			alert(sMessage);
			return false;
		}
	} /* end for */
	
	return true;
}

function IsNumericInRange(aVar, sMessage, aVarMin, aVarMax)
{
	if (!IsFilled(aVar, sMessage))
		return false;
	if (!IsNumeric(aVar, sMessage))
		return false;
	//alert(aVar + "|" + aVarMin + "|" + aVarMax);
	if (aVar < aVarMin || aVar > aVarMax ){
			if (sMessage != "")
			{
				alert(sMessage);
			}
			return false;
		}
	return true;
}

//
// Check whether string s is empty.
//
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{   
    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}



// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}



// Returns true if character c is a letter or digit.
function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

// FOR DATA CHECK ------------------------------

// Attempting to make this library run on Navigator 2.0,
// so I'm supplying this array creation routine as per
// JavaScript 1.0 documentation.  If you're using 
// Navigator 3.0 or later, you don't need to do this;
// you can use the Array constructor instead.

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
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;

// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   
	if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}



// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.


function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}



// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 01, 12);
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 01, 31);
}



// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

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 );
}



// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    // 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);

    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isDayMonth (month, day)
{   // catch  invalid months and days.
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);

    if (! (isMonth(month, false) && isDay(day, false))) return false;

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

// isHour (STRING s [, BOOLEAN emptyOK])
// 
// isHour returns true if string s is a valid 
// hour number between 0 and 23.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isHour (s)
{   if (isEmpty(s)) 
       if (isHour.arguments.length == 1) return defaultEmptyOK;
       else return (isHour.arguments[1] == true);   
    return isIntegerInRange (s, 00, 23);
}

// isHour (STRING s [, BOOLEAN emptyOK])
// 
// isHour returns true if string s is a valid 
// hour number between 0 and 23.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMinute (s)
{   if (isEmpty(s)) 
       if (isMinute.arguments.length == 1) return defaultEmptyOK;
       else return (isMinute.arguments[1] == true);   
    return isIntegerInRange (s, 00, 59);
}

//
// Removes all characters which appear in string bag from string s.
//

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}
function ValidaMail(Obj){
	if (Obj.value!="")
	{	
		if (isEmail(Obj.value,true)==false)
		{	
			alert("El EMail introducido es erróneo.");
			Obj.focus();
			return;
		}
	}
}
