<!--// This is a common javascript file for cimnet

var defaultEmptyOK = false;

// whitespace characters
var whitespace = " \t\n\r";

// decimal point character differs by language and culture
var decimalPointDelimiter = "."

//================== 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;

    // find a non-whitespace character, return false; else 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:

function isEmail (s, em)
{
    if (isEmpty(s)) {
       if(em) return em; 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    }
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    var emailReg="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex= new RegExp(emailReg);
    if (!regex.test(s))   return false;
    if (s.indexOf("@")<3) return false;
    if (s.charAt(s.indexOf("@")+1)=='.') return false;
    if  ((s.indexOf(".com")<5) && (s.indexOf(".org")<5)
        &&(s.indexOf(".gov")<5) && (s.indexOf(".net")<5)
        &&(s.indexOf(".mil")<5) && (s.indexOf(".edu")<5)
        &&(s.indexOf(".co")<5) && (s.indexOf(".per")<5)){
                 if (! confirm("Warning: The Email address format does not "
                 + "contain the standard character code,\n such as"
                 + "'.com', '.edu', '.net', '.org', '.gov', '.co', 'per', or '.mil'\n"
                 + "Do you want to continue to use it?" ))
                        return  false;
        }
    return true;
}


//================== isLessThan (STRING s, INTEGER value)
// isLessThan returns true if string s is less than value.

function isLessThan(s, value) {
  if( s.length > value )
    return true;
  else
    return false;
}

//================== isIntegerInRange (STRING s, INTEGER a, INTEGER b)
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.

function isIntegerInRange (s, a, b) { 
  if (s == null) return false;
  if (!isInteger(s, false)) return false;
  var num = parseInt (s);
  return ((num >= a) && (b >= num));
}

//==================  isInteger (STRING s)================================
// Returns true if all characters in string s are numbers.
// Accepts non-signed integers only. Does not accept floating point, exponential notation, etc.

function isInteger(s) {
  if (s == null) return false;
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (!isDigit(c)){
        return false;
    }
  }
  return true;
}

// =================  isDigit (char c) =========================
// isDigit Returns true if character c is a digit

function isDigit(c) {
  if (c=='0'){
      return true;
  } else if (c=='1'){
      return true;
 } else if (c=='2'){
      return true;
 } else if (c=='3'){
      return true;
 } else if (c=='4'){
      return true;
 } else if (c=='5'){
      return true;
 } else if (c=='6'){
      return true;
 } else if (c=='7'){
      return true;
 } else if (c=='8'){
      return true;
 } else if (c=='9'){
      return true;
 }else{
      return false;
 }
}

// ================= isFloat (STRING s [, BOOLEAN emptyOK])==================================
// True if string s is an unsigned floating point (real) number. 
// Also returns true for unsigned integers. 
// Does not accept exponential notation.

function isFloat (s)
{
    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//================== checkFloat(STRING s)====================================================
//return true if the numeric is in the format of number(argument 2, argument 3)

function checkFloat(s, a, b, sign)
{
        if (isEmpty(s))
           return false;
        if (s.charAt(0)== '-'){
            if (sign) s = s.substring(1);
            else return sign;
        }
        if (! isFloat(s))
		return false;
	if(s.charAt(s.length-1) == decimalPointDelimiter)
		return false;
	var c=0;
	while (c < s.length && (s.charAt(c)!= decimalPointDelimiter)) c++;
	if (c < s.length) 
	{
   		var aDecStr=s.substring(c+1, s.length);
		if (aDecStr.length > b) return false;
	}
	var bDecStr=s.substring(0, c);
	return (bDecStr.length <= a);
}




//====================== isDay(STRING s)======================
//isDay returns true if string s is a valid day number between 1 and 31

function isDay(s) {
  if (s == null) return false;
  return isIntegerInRange(s,1,31);
}

//===================  isMonth (STRING s)=================================
// isMonth returns true if string s is a valid month number between 1 and 12.

function isMonth(s) {
  if (s == null) return false;
  return isIntegerInRange (s, 1, 12);
}

// ===================== isYear (STRING s)=================================
// isYear returns true if string s is a valid Year number.  
// For Year 2000 compliance, you are advised to use 4-digit year numbers.

function isYear(s) {
  if (s == null) return false;
  if (!isInteger(s)) return false;
  if (s.length == 4) {
    var yr = parseInt(s);
    return (yr>=1900);
  } else{
    return false;
  }
}


//==================== isDate (String day, String month, String year)==========
//return true if the date given is a valid date

function isDate(day, month, year) {
  var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  if (! (isYear(year) && isMonth(month) && 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 daysInFebruary(year) {
  return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

//====================== isMMYYYY (STRING s)======================
//isMMYYYY returns true if string s can be converted to numbers and is in a valid date format mm/yyyy

function isMMYYYY(s)
{
        strDD = "01"; //Default to first day of the month, we only want to check if the month and year is valid
        if (s.length !=7){
           return false;
        }else{
             strMM = s.substring(0,2);
             for (var m=0; m<strMM.length; m++) {
                  var mm = strMM.charAt(m);
                  if (mm=="0"){
                     strMM = strMM.charAt(m+1);
                  }
             }
             if (isMonth(strMM)==false){
                 return false;
             }
             strSeperator = s.substring(2,3);
             strYYYY = s.substring(3,7);
             if (isYear(strYYYY)==false){
                 return false;
             }
             return ( isDate(strDD, strMM, strYYYY) );
       }
}

//====================== isDDMMYYYY (STRING s)======================
//isDDMMYYYY returns true if string s can be converted to numbers and is in a valid date format dd/mm/yyyy

function isDDMMYYYY(s)
{
        if (s.length !=10){
           return false;
        }else{
             strDD = s.substring(0,2);
             for (var d=0; d<strDD.length; d++) {
                  var dd = strDD.charAt(d);
                  if (dd=="0"){          
                     strDD = strDD.charAt(d+1);
                  }
             }
             if (isDay(strDD)==false){
                 return false;
             }
             strSeperator = s.substring(2,3);
             strMM = s.substring(3,5);
             for (var m=0; m<strMM.length; m++) {
                  var mm = strMM.charAt(m);
                  if (mm=="0"){
                     strMM = strMM.charAt(m+1);
                  }
             }
             if (isMonth(strMM)==false){
                 return false;
             }
             strSeperator = s.substring(5,6);
             strYYYY = s.substring(6,10);
             if (isYear(strYYYY)==false){
                 return false;
             }
             return ( isDate(strDD, strMM, strYYYY) );
       }
}

//-->
