// BASIC DATA VALIDATION FUNCTIONS:
//
// isWhitespace (s)                    Check whether string s is empty or whitespace.
// isLetter (c)                        Check whether character c is an English letter 
// isDigit (c)                         Check whether character c is a digit 
// isLetterOrDigit (c)                 Check whether character c is a letter or digit.
// isInteger (s [,eok])                True if all characters in string s are numbers.
// isSignedInteger (s [,eok])          True if all characters in string s are numbers; leading + or - allowed.
// isPositiveInteger (s [,eok])        True if string s is an integer > 0.
// isNonnegativeInteger (s [,eok])     True if string s is an integer >= 0.
// isNegativeInteger (s [,eok])        True if s is an integer < 0.
// isNonpositiveInteger (s [,eok])     True if s is an integer <= 0.
// isFloat (s [,eok])                  True if string s is an unsigned floating point (real) number. (Integers also OK.)
// isSignedFloat (s [,eok])            True if string s is a floating point number; leading + or - allowed. (Integers also OK.)
// isAlphabetic (s [,eok])             True if string s is English letters 
// isAlphanumeric (s [,eok])           True if string s is English letters and numbers only.

// isEmail (s [,eok])                  True if string s is a valid email address.
// isYear (s [,eok])                   True if string s is a valid Year number.
// isIntegerInRange (s, a, b [,eok])   True if string s is an integer between a and b, inclusive.
// isMonth (s [,eok])                  True if string s is a valid month between 1 and 12.
// isDay (s [,eok])                    True if string s is a valid day between 1 and 31.
// daysInFebruary (year)               Returns number of days in February of that year.
// isDate (year, month, day)           True if string arguments form a valid date.
// isValidDate(strDate)				   Empty string if string arguments form a valid formatted date.
// isValidTime(strTime)				   Empty string if string arguments form a valid formatted time.


var reLetter = /^[a-zA-Z]$/
var reAlphabetic = /^[a-zA-Z]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/
var reDigit = /^\d/
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var reSignedInteger = /^([+-])?\d+$/
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reSignedFloat = /^((([+-])?\d+(\.\d*)?)|(([+-])?(\d*\.)?\d+))$/
var reEmail = /^.+\@.+\..+$/
var reDate224 = /\b\d{1,2}[\/]\d{1,2}[\/]\d{4}\b/
var reDate422 = /\d{4}\b[\/]\b\d{1,2}[\/]\d{1,2}/
var reTime = /\b\d{1,2}[:]\d{1,2}/
//var rePorcentaje = /^(%)+$/


// whitespace characters as defined by this sample code
var whitespace = " \t\n\r";
var defaultEmptyOK = true

var daysInMonth = Array(13);
daysInMonth[0] = -1;
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;

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{  
    return (isEmpty(s) || reWhitespace.test(s));
}

function isLetter (c)
{   return reLetter.test(c)
}

function isDigit (c)
{   return reDigit.test(c)
}

function isLetterOrDigit (c)
{   return reLetterOrDigit.test(c)
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 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);

    return reInteger.test(s)
}

function isSignedInteger (s)
{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);
    
    else {
       return reSignedInteger.test(s)
    }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) > 0) ) );
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}

function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) < 0) ) );
}

function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) <= 0) ) );
}

function isFloat (s)
{   if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    return reFloat.test(s)
}

function isSignedFloat (s)
{   if (isEmpty(s)) 
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
       return reSignedFloat.test(s)
    }
}

function isAlphabetic (s)
{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    else {
       return reAlphabetic.test(s)
    }
}

// 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 isAlphanumeric (s)
{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    else {
       return reAlphanumeric.test(s)
    }
}

function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isIntegerInRange(s,1900,9999)) return false;
    return (s.length == 4);
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = parseInt(s,10);

    return ((num >= a) && (num <= b));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s) 
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
	   	   
    return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year)
{   return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day){

	if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    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 isValidDate(strDate)
{   var resp="";	
	if (isEmpty(strDate)){
       if (isValidDate.arguments.length == 1) {
		 if(!defaultEmptyOK)
      	    resp= "This date cannot be empty.";}
       else{
	   if (isValidDate.arguments[1] == false) 
      	    resp= "This date cannot be empty.";	}   
	}else{   
	if (reDate224.test(strDate)) {
        var delimChar = "/";
        var delim1 = strDate.indexOf(delimChar);
        var delim2 = strDate.lastIndexOf(delimChar);
        mo = strDate.substring(0, delim1);
        day = strDate.substring(delim1+1, delim2);
        yr = strDate.substring(delim2+1);		
		if (!isDate(yr,mo,day))
      	  resp= "There is a problem with the date entry.";
    } else {
        resp= "Incorrect date format. (mm/dd/yyyy)";// Enter as mm/dd/yyyy.
    }
	}
	return resp;
}

function isValidTime(strTime)
{	var resp="";
	if (isEmpty(strTime)){
       if (isValidTime.arguments.length == 1) {
		 if(!defaultEmptyOK)
      	    resp= "This time cannot be empty.";}
       else{
	   if (isValidTime.arguments[1] == false) 
      	    resp= "This time cannot be empty.";	}   
	}else{   
	if ((reTime.test(strTime))&&(strTime!="0:0")) {
        var delimChar = ":";
        var delim1 = strTime.indexOf(delimChar);
        hour = strTime.substring(0, delim1);
        minu = strTime.substring(delim1+1);
		if (!isIntegerInRange(hour,0,23))
      	  resp= "There is a problem with the hours entry.";
		if (!isIntegerInRange(minu,0,59))
      	  resp= "There is a problem with the minutes entry.";		  
    } else {
        resp= "Incorrect time format. (hh:mm)";// Enter as mm/dd/yyyy.
    }
	}
	return resp;
}

//ST44732
function isValidPorcentaje (s)
{   var resp="";
    var xstrno = s.indexOf("'"); 
	var xstrno2 = s.indexOf('%');
    
	if (xstrno != -1) //valid Comilla Simple
	{ 
		return resp="'";
		//return false;
    }
	else
	{
		if (xstrno2 != -1)//valid Porcentaje
		{
			return resp = "%";
		}		
		else
		{
			return resp = "";
			//return true;
		}
	}
}

