// BASIC DATA VALIDATION FUNCTIONS:
//
// isWhitespace (s)
// isLetter (c)
// isDigit (c)
// isLetterOrDigit (c)
// isInteger (s [,eok])
// isSignedInteger (s)
// isNonnegativeInteger (s)
// isFloat (s [,ndecimals][,eok])
// isAlphabetic (s [,eok])
// isAlphanumeric (s [,eok])
// isIntegerInRange (s, a, b)
// isEmail (s [,eok])
// isYear (s [,eok])
// isMonth (s [,eok])
// isDay (s [,eok])
// daysInFebruary (year)
// isDate (year, month, day)
// isDateGreater(fDate, tDate)
// isLength (s, lVal, uVal)
// isPassword (s)
// isDateEqual (fDate, tDate) 
// isFromDateGreater (fDate, tDate)
// isSpecialCharacter(s)
// isFirstCharacter(s)
// chkKey(e)

// VARIABLE DECLARATIONS

var whitespace = " \t\n\r";

var decimalPointDelimiter = "."

var defaultEmptyOK = false

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

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 isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }

    return 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];

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isFloat (s)
{   var i;
    var seenDecimalPoint = false;
    var NoOfDecimals=0;
    var decimals = 0;
    

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 3) return defaultEmptyOK;
       else return (isFloat.arguments[2] == true);

	if (isFloat.arguments.length == 2)
	{
		decimals = isFloat.arguments[1];
	}

    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
        if (seenDecimalPoint && c != decimalPointDelimiter && decimals != 0)
        {
			NoOfDecimals++;
			if (NoOfDecimals > decimals) return false;
		}

    }

    return true;
}

function isAlphabetic (s)
{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isLetter(c))
        return false;
    }

    return true;
}

function isAlphanumeric (s)
{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    return true;
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    if (isWhitespace(s)) return false;

	if(!(isSpecialCharacterForEmail(s))) return false; // added by bhadri on 17-04-2003
														// Checking for special characters other than @ and .

    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
//    else return true;

	// Added by bhadri on 17-04-2003
    if(s.substring(s.indexOf('@')+1).indexOf('@')>=0 || s.substring(s.indexOf('@')+1).indexOf('.')< 0)
    {
  		return false;
    }
    else if( (s.charAt(s.length-1)=='.') || (s.charAt(s.length-2)=='.')){
	 	return false;
    }
	else return true;

}

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);
    return ((num >= a) && (num <= b));
}

function isYear (p)
{   var s = p.toString();
	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));
}

function isMonth (p)
{   var s = p.toString();
	if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (p)
{   var s = p.toString();
	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 (pyear, pmonth, pday)
{   
	year = pyear.toString();
	month = pmonth.toString();
	day = pday.toString();
	
	if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isLength (s, lVal, uVal)
{   if (isEmpty(s)) 
       if (isLength.arguments.length == 1) return defaultEmptyOK;
       else return (isLength.arguments[1] == true);

    var sLength = s.length;
    if (sLength < lVal || sLength > uVal) return false;

    return true;
}

function isPassword (s)
{   var i;

    if (isEmpty(s)) 
       if (isPassword.arguments.length == 1) return defaultEmptyOK;
       else return (isPassword.arguments[1] == true);

    var nCount = 0;
    var aCount = 0;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (isLetter(c)) aCount++;
        if (isDigit(c)) nCount++;
    }

    if (aCount < 2 || nCount < 2) return false; 
 
    return true;
}

function isDateEqual (fDate, tDate)
{
    var frmDate = new Date();
    var toDate = new Date();
    
    frmDate.setTime(Date.parse(fDate));
    toDate.setTime(Date.parse(tDate));

	if (	(frmDate.getDate() == toDate.getDate()) 
			&& (frmDate.getMonth() == toDate.getMonth()) 
			&& (frmDate.getYear() == toDate.getYear())
		)
		return true;
	else
		return false;
}

function isDateGreater (fDate, tDate)
{
    var frmDate = new Date();
    var toDate = new Date();
    
    frmDate.setTime(Date.parse(fDate));
    toDate.setTime(Date.parse(tDate));

	if ((frmDate.getYear() < toDate.getYear()) ||
		((frmDate.getMonth() < toDate.getMonth()) && frmDate.getYear() == toDate.getYear()) ||
		((frmDate.getDate() < toDate.getDate()) && (frmDate.getMonth() == toDate.getMonth()) && frmDate.getYear() == toDate.getYear())
		)
		return false;
	else
		return true;
}

function isSingleWord (s)
{   if (isEmpty(s)) 
       if (isSingleWord.arguments.length == 1) return defaultEmptyOK;
       else return (isSingleWord.arguments[1] == true);
     
    var sCount = 0;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
	if (c == " ") sCount++;
    }

    if (sCount > 0) return false;

    return true;
}

function isFromDateGreater (fDate, tDate)
{
    if(isDateEqual (fDate, tDate)) return false;
    else
    var frmDate = new Date();
    var toDate = new Date();
    
    frmDate.setTime(Date.parse(fDate));
    toDate.setTime(Date.parse(tDate));

	if ((frmDate.getYear() < toDate.getYear()) ||
		((frmDate.getMonth() < toDate.getMonth()) && frmDate.getYear() == toDate.getYear()) ||
		((frmDate.getDate() < toDate.getDate()) && (frmDate.getMonth() == toDate.getMonth()) && frmDate.getYear() == toDate.getYear())
		)
		return false;
	else
		return true;
}


function isSpecialCharacter(s)	{
	for (i = 0; i < s.length; i++)
	{   
	    var c = s.charAt(i);
	    if ( (s.charAt(i) == "~") 
	    || (s.charAt(i) == "!") 
	    || (s.charAt(i) == "@") 
	    || (s.charAt(i) == "#") 
	    || (s.charAt(i) == "$") 
	    || (s.charAt(i) == "%") 
	    || (s.charAt(i) == "*")
		|| (s.charAt(i) == ";")		   
		|| (s.charAt(i) == ":") 
	    || (s.charAt(i) == ",") 
	    || (s.charAt(i) == ".") 
	    || (s.charAt(i) == "/") 
	    || (s.charAt(i) == "?") 
	    || (s.charAt(i) == "+") 
	    || (s.charAt(i) == "|") 
	    || (s.charAt(i) == "&") 
	    || (s.charAt(i) == "<") 
  	    || (s.charAt(i) == ">") 
	    || (s.charAt(i) == "”") 
  	    || (s.charAt(i) == "’") 
		)
	    return false;
	}    

    return true;
}	 

function isMalicousCharacter(s)	{
	for (i = 0; i < s.length; i++)
	{   
	    var c = s.charAt(i);
	    if ( (s.charAt(i) == "~") 
	    || (s.charAt(i) == "<") 
  	    || (s.charAt(i) == ">") 
		|| (s.charAt(i) == ")") 
		|| (s.charAt(i) == "(") 
	    || (s.charAt(i) == "”") 
  	    || (s.charAt(i) == "’") 
		|| (s.charAt(i) == ";")	
	    || (s.charAt(i) == "%") 
		|| (s.charAt(i) == "&") 
		)
	    return false;
	}    

    return true;
}

function isSpecialCharacterForEmail(s)	{
	for (i = 0; i < s.length; i++)
	{   
	    var c = s.charAt(i);
	    if ( (s.charAt(i) == "~") 
	    || (s.charAt(i) == "!") 
//	    || (s.charAt(i) == "@") 
	    || (s.charAt(i) == "#") 
	    || (s.charAt(i) == "$") 
	    || (s.charAt(i) == "%") 
	    || (s.charAt(i) == "*")
		|| (s.charAt(i) == ";")		   
		|| (s.charAt(i) == ":") 
	    || (s.charAt(i) == ",") 
//	    || (s.charAt(i) == ".") 
	    || (s.charAt(i) == "/") 
	    || (s.charAt(i) == "?") 
	    || (s.charAt(i) == "+") 
	    || (s.charAt(i) == "|") 
	    || (s.charAt(i) == "&") 
		)
	    return false;
	}    

    return true;
}

function isFirstCharacter(s) {
		var c = s.charAt(0);
		if (!isLetter(c)) return false; 

	return true;
}


function chkKey(e){
	var BRW = (document.all)?"IE":"NS";
	var strKeyCode;
			
	if (BRW == "IE"){		
		strKeyCode = e.keyCode;
	}
	else if (BRW == "NS"){
		strKeyCode = e.which;
	}
			
	if (strKeyCode == 13){			
			return false;	
	}	
	return true;			
}


function isDateDiffLessThanOneYear(fDate, tDate) {
    var fromDate = new Date();
    var toDate = new Date();

    fromDate.setTime(Date.parse(fDate));
    toDate.setTime(Date.parse(tDate));

	if (toDate.getYear() - fromDate.getYear() > 1)
		return false;
		
	if ((toDate.getYear() - fromDate.getYear() == 1) && 
		(toDate.getMonth() > fromDate.getMonth())
		)
		return false;
	
	if (
		(toDate.getYear() - fromDate.getYear() == 1) && 
		(toDate.getMonth() == fromDate.getMonth()) &&
		(toDate.getDate() > fromDate.getDate())
		)
		return false;
	
	return true;
}


//This function finds whether the month of todate is correct 
//depending on today's date (indicated by "day" - see Constants file).

function toDateMonthValidation(tDate, sDate, day) {

    var toDate = new Date();
    var sysDate = new Date();
    
    toDate.setTime(Date.parse(tDate));
	sysDate.setTime(Date.parse(sDate));

	if (sysDate.getDate() < day) 
		if ((toDate.getMonth()>= sysDate.getMonth() - 1) && (toDate.getYear() >= sysDate.getYear()))
			return false;
		else
			return true;
	else 
		if ((toDate.getMonth()>= sysDate.getMonth()) && (toDate.getYear() >= sysDate.getYear()))
			return false;
		else
			return true;
}
