var whitespace = ' \t\n\r' ;
var defaultEmptyOK = false

charList = new Array("<" , ">" , "{" , "}" , "[" , "]" , "?" , "!" , "#" , "-" , "=" , "+", "*" , "(", ")" , "&" , "^" , "%" , "$" , "@" , "/", "\\" , "\"", "'" ) ;
charList1 = new Array("<" , ">" , "{" , "}" , "[" , "]" , "?" , "!" , "#" , "-" , "=" , "+", "*" , "(", ")" , "&" , "^" , "%" , "$" , "@" , "/", "\\" , "\"", "'" , " " ) ;


//declare the character array for checking

var  num_list = new Array("1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0");

//function numchk(b,list) {
function numchk(element, list) {
      //check for illegal text
      //k = document.form[0].element[b].value;
	  k = element.value;
	  
      var checker = "0";
      numlist = list
     
      for( i = 0; i < numlist.length; i++) 
	  {
	  		
	  		for(a = 0; a < k.length; a++) {
				  if(k.charAt(a)  == numlist[i]) 
				  {
                        checker = eval(checker + 1);
                  }
            }
      }
      
      if (checker == k.length && checker != 0) {
	//  if (checker == element.length && checker != 0) {
            return true;
      }
      else {
            return false;
      }
}//end numchk


var daysInMonth =  new Array(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;

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));
}

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);

}

//Return true if the length of the character is less than num
function isLengthLess(str, num)
{
 if(str.length < num)
  return true;
 return false;
}

//Return true if the firstdate is less than seconddate
function validdiffdate(firstdate, seconddate)
{
 if( seconddate.substring(6, 10) >= firstdate.substring(6, 10))
 {
 	if(	seconddate.substring(3, 5) >= firstdate.substring(3, 5))
	{ 
	 if( seconddate.substring(0, 2) >= firstdate.substring(0, 2))
	 {
	 	return true;
	 }
	 else
	 {
	 	return false;
	 }	 	 	 
	}
	else
	{
	 return false;
	}
 }
 else
 {
  return false; 
 } 
}

function checkChar(checkStr)
{
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%^&*@!~$`z=|\/':;,;";
  for (i = 0;  i < checkStr.length;  i++)
  {
   ch = checkStr.charAt(i);
   for (j = 0;  j < checkOK.length;  j++)
	 {
    if (ch == checkOK.charAt(j))
		{
     return true;
		 break;
    }
	 }
	}
	return false;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// 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;
}

// added by soujern, for the need of checking decimal value in UPM_Other_detail Form
function isNumber (s)

{   var i;

    if (isEmpty(s)) 
       if (isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isNumber.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)) || (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))
    }
}

// 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));
}

// 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 isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) 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 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 );
}



function checkInvalidChar( str ) 
{ 
/*Checks for invalid char in str passed in. Returns TRUE if an invalid character is found.
   List of invalid caracters is contained in global array charList
*/  
  for (var i=0 ;  i<charList.length ; i++) {
    //alert(charList[i]);
    if (str.indexOf(charList[i]) != -1)
	    return true;
  } //end for
  return false
}//end checkInvalidChar

function checkInvalidUserName( str ) 
{ 
/*Checks for invalid char in str passed in. Returns TRUE if an invalid character is found.
   List of invalid caracters is contained in global array charList1
*/  
  for (var i=0 ;  i<charList1.length ; i++) {
    //alert(charList[i]);
    if (str.indexOf(charList1[i]) != -1)
	    return true;
  } //end for
  return false
}//end checkInvalidChar

function isEmpty(str) {
  if ((str==null) || (str.length==0))  return true ;

  return false ;

} //end isEmpty

function isWhitespace(str) {

if (isEmpty(str))  return true ;

for( var i=0 ; i<str.length ; i++) {
  var chr = str.charAt(i) ;
  if (whitespace.indexOf(chr) == -1) return false ;
}//end for

return true

}//end isWhitespace

function checkEmail(email)
{
 if ((email == null) || (isWhitespace(email)))
 return false;
 idx = email.indexOf("@");
 if((idx==-1) || (idx==0))
 return false;
 idx=email.lastIndexOf(".")
 if((idx==-1) || (idx==0) || (idx==email.length-1))
 return false;
 else
 return true;
}

function alertUser(msg , fieldobj , notTextField ) {

    if (notTextField==0)  
	    fieldobj.select() ;

  fieldobj.focus() ;

  alert(msg) ;
  window.status = msg ;
  
}  //end alertUser



