//-----------------------------------------------------------
//               JavaScript Functions
//-----------------------------------------------------------

// FormChek.js
//
// SUMMARY
//
// This is a set of JavaScript functions for validating input on
// an HTML form.
//
// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation


// VARIABLE DECLARATIONS

var defaultEmptyOK = false

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var decimalPointDelimiter = "."

// whitespace characters
var whitespace = " \t\n\r";


// 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;
}


// 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;
}



// Removes all characters which do NOT appear in string bag
// from string s.

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is 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;
}



// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}

// Check whether string s is empty.

function isEmpty(s)
{  return ((s == null) || (s.length == 0))
}

// 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 (!isInteger(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).
    // *** specify decimal in parseInt; numbers with preceding zero are octal
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

// 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;
}

// Returns true if character c is a digit
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// 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, 1, 12);

}

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;

    // 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 ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// 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, 1, 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 (s)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    // split into month, day, year
    var year, month, day, col;
    col = s.indexOf("/");
    if (col > 0) {
          month = s.substring(0,col);
          s = s.substring(col+1);
          col = s.indexOf("/");
          if (col > 0) {
               day = s.substring(0,col);
               year = s.substring(col+1);
          }
          else {return false};
    }
    else {return false};

    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);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
 //   if (intDay > daysInMonth[intMonth]) return false;

 //   if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


// Get checked value from radio button.

function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {
        if (radio[i].checked) { break }
    }
    return radio[i].value
}


function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function


// 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;
}


//----------------------------------------------------
//   isMMYYYY   -- written by Kathleen Duncan, Washington State University
//----------------------------------------------------

function isMMYYYY (s)
{   // catch invalid years (not 4-digit) and invalid months
    // split into month, year
     var year,mnt,month, col;
     col = s.indexOf("/");

     if (col > 0) {
          month = s.substring(0,col);
          // must have characters after "/"
          if (s.length > col+2) {
               year = s.substring(col+1);
               if (year.length != 4) { return false;}
          }
          else {return false};
     }
     else {return false};

     if (! (isYear(year, false) && isMonth(month, false) )) return false;

     return true;
}
//===========================================================
//
//        AICS Specific Functions
//
//===========================================================

//-----------------------------------------------------------
//   clearStatus
//
//   reload the status frame with a "blank" page
//-----------------------------------------------------------

function clearStatus() {
       parent.frames["statusWindow"].location.href = "blankStatus.htm" ;
}

function errorStatus() {
       parent.frames["statusWindow"].location.href = "errorStatus.htm" ;
}

function successStatus() {
       parent.frames["statusWindow"].location.href = "successStatus.htm" ;
}

//-----------------------------------------------------------
//   helpWindow
//
//   opens a new browser window; target name is helpScreen
//-----------------------------------------------------------

function helpWindow() {
     var helpWin;

     helpWin = window.open('', 'helpScreen','toolbar=no,menubar=yes,width=600,height=400,scrollbars=yes')
     if (window.focus) {window.focus;}

}
//-----------------------------------------------------------
//   contentsWindow
//
//   opens a new browser window; target name is contentsWindow
//-----------------------------------------------------------

function contentsWindow() {
    var contentsWin;

     contentsWin = window.open('', 'contentsWindow','toolbar=no,menubar=no,width=300,height=400,scrollbars=yes')
     if (window.focus) {window.focus;}
}

//-----------------------------------------------------------
// howMany()
//
// inputs:     programName
//             reportID
//             personID
// This function prompts for the number of entries to be added.
// The default is 5.  It verifies for a number between 1 and 15.
// Then it loads the program specified in "programName".
// It calls the program with the number of new entries, the
// reportID and personID.
//-----------------------------------------------------------

function howMany (programName, reportID, personID) {
//  var newCnt = prompt("How many entries do you want to add now?",1);
 var newCnt = 1;
          if (newCnt > 0 & newCnt <=15) {
               parent.frames["main"].location.href = programName + "?newCnt=" + newCnt + "&reportID="+reportID+"&personID="+personID;
          }
          if (newCnt == null) {return false;}

          if (!isInteger(newCnt)) {
               alert("You must enter a number between 1 and 15");
               return false;
          }
          if (!isIntegerInRange(newCnt, 1, 15)){
               alert("You can add a maximum of 15 entries at a time.  If you need to add more, you can do so after you enter these.");
               parent.frames["main"].location.href = programName + "?newCnt=15&reportID="+reportID+"&personID="+personID;
          }

          return false;
     }


//-----------------------------------------------------------
//   reloadMain
//
//   reload the main frame with the program and parameters specified
//   in programStr.
//
//   parameter example:
//   parms = "extProj.asp?reportID=" & Request("reportID") & "&personID=" & Request("personID")
//   <body  onLoad=reloadMain("lt% parms%gt")  >
//-----------------------------------------------------------

     function reloadMain(programStr) {
          parent.frames["main"].location.href = programStr;
     }
     
    function KeyInt(myfield,e)
		{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (((keycode>47) && (keycode<58) )  || (keycode==8)) { return true; }
		else return false;
		}

	function KeyFloat(myfield,e)
		{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (((keycode>47) && (keycode<58) )  || (keycode==8) || (keycode==46)) { return true; }
		else return false;
		}
		
	function KeyPercent(myfield,e)
		{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (((keycode>47) && (keycode<58) )  || (keycode==8) || (keycode==46) || (keycode==37)) { return true; }
		else return false;
		}