/******************************************************************************
 IOSea.com Data Validation Routines

 Author:    Roger Hall (roger@iosea.com)
 Copyright: 1999-2002 by IOSea.com. All rights reserved
******************************************************************************/

function ValidText(objField, blnRequired, strFieldName) {
   if (blnRequired && isWhitespace(objField.value)) {
      alert("'" + strFieldName + "' is required.");
      objField.focus();
      return false;
   }
   return true;
}

function ValidPositiveNumber(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (!isPosNumber(objField.value))
      {
      alert("You must supply a valid number" + strName + ".");
      objField.focus();
      objField.select();
      return false;
      }

   return true;
}

function ValidDate(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
      {
      if (!isDate(objField.value))
         {
         alert("You must supply a valid date" + strName + " with a 4 digit year.");
         objField.focus();
         objField.select();
         return false;
         }
      }
   return true;
}

function ValidSSN(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isSSN(objField.value))
            {
            alert("You must supply a valid Social Security Number" + strName + ". The format is NNN-NN-NNNN");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function ValidPhoneNumber(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isPhone(objField.value))
            {
            //CMK - 13 Apr 2000 - changed error message to use more common dash format.
            alert("You must supply a valid phone number" + strName + ". The format should be ###-###-####");  //CMK - 13 Apr 2000  //FTR (N's->#'s) 9/7/00
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function ValidPositiveInteger(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isPosInteger(objField.value))
            {
            alert("You must supply a valid number" + strName + ".");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function ValidInteger(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isInteger(objField.value))
            {
            alert("You must supply a valid number" + strName + ".");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function ValidPostalCode(objField, blnRequired, strFieldName) {
   var strName = strFieldName

   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }
   if (objField.value != "")
         {
         if (!isPostalCode(objField.value))
            {
            alert("Invalid postal code entered "  + strName + ".  Valid Format is '99999' or '99999-9999'" + ".");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}


function ValidEmail(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isEmail(objField.value))
            {
            alert("You must supply a valid email address" + strName + ". (<user>@<organization>.<domain>)");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function ValidCurrency(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (!isCurrency(objField.value))
      {
      alert("You must supply a valid currency value" + strName + ".>");
      objField.focus();
      objField.select();
      return false;
      }

   return true;
}

function ValidTime(objField, blnRequired, strFieldName) {
   var strName = strFieldName
   if (strFieldName == "") {strName = ""} else {strName = " for " + strFieldName}

   if (blnRequired && isWhitespace(objField.value))
      {
      alert("A value is required" + strName + ".");
      objField.focus();
      return false;
      }

   if (objField.value != "")
         {
         if (!isTime(objField.value))
            {
            alert("You must supply a valid time" + strName + ". (HH:MM AM)>");
            objField.focus();
            objField.select();
            return false;
            }
         }
   return true;
}

function isSelectionMade(objField, indexExcluded) {
   // Contact Method Edit

   var i
   var bSelection = false
   var nEntries = objField.length
   for (i=0;(i<nEntries);i++) {
      if (objField[i].selected == true) {
         if (i != indexExcluded) { bSelection = true; }
      }
   }
   if (bSelection == false) { return false; }
   else                     { return true; }
}


function ValidUNCPath(objField, blnRequired, strFieldName) {
   if (blnRequired && UNCCheck(objField.value)) {
      alert("The objField '" + strFieldName + "' must specify a UNC path (\\machine name\...)")
      objField.focus();
      return false;
   }
   return true;
}


// Simple UNC path validation
// Checks only first two characters for "//"
function UNCCheck(sPath) {
   if ((sPath.charAt(0) == "/") && (sPath.charAt(1) == "/")) {
      return true
   }
   else {
      //alert("This objField must specify a UNC path (\\machine name\...)")
      return false
   }
}

// general purpose function to see if a suspected numeric input
// is a positive integer
function isPosInteger(inputVal) {
    inputStr = inputVal.toString()
    for (var i = 0; i < inputStr.length; i++)
        {
        var oneChar = inputStr.charAt(i)
        if (oneChar < "0" || oneChar > "9")
            {
            return false
            }
        }
    return true
}

// general purpose function to see if a suspected numeric input
// is a positive or negative integer
function isInteger(inputVal) {
    inputStr = inputVal.toString()
    for (var i = 0; i < inputStr.length; i++)
        {
        var oneChar = inputStr.charAt(i)
        if (i == 0 && oneChar == "-")
            {
            continue
            }
        if (oneChar < "0" || oneChar > "9")
            {
            return false
            }
        }
    return true
}

// general purpose function to see if a suspected numeric input
// is a positive or negative number
function isNumber(inputVal) {
    oneDecimal = false
    inputStr = inputVal.toString()
    for (var i = 0; i < inputStr.length; i++)
        {
        var oneChar = inputStr.charAt(i)
            if (i == 0 && oneChar == "-")
            {
            continue
            }
        if (oneChar == "." && !oneDecimal)
            {
            oneDecimal = true
            continue
            }
        if (oneChar < "0" || oneChar > "9")
            {
            return false
            }
        }

    return true
}

// general purpose function to see if a suspected numeric input
// is a positive or negative number
function isNumber2(inputValue) {
    if (isNaN(parseFloat(inputValue)))
        {
        alert("The value you entered is not a number.")
        return false
        }
    return true
}

// general purpose function to see if a suspected numeric input
// is a positive number
function isPosNumber(inputVal) {
    oneDecimal = false
    inputStr = inputVal.toString()
    for (var i = 0; i < inputStr.length; i++)
        {
        var oneChar = inputStr.charAt(i)

        if (oneChar == "." && !oneDecimal)
            {
            oneDecimal = true
            continue
            }
        if (oneChar < "0" || oneChar > "9")
            {
            return false
            }
        }

    return true
}

// function to see if input is valid US Social Security Number
function isSSN(strInput) {
    // SSN should be nnn-nn-nnnn

    // Be sure there are two dashes
    var dash1 = strInput.indexOf("-")
    var dash2 = strInput.lastIndexOf("-")
    if (dash1 == -1 || dash1 == dash2)
        {return false}

    // Extract the tree paqrts of the SSN
    if (dash1 == 3 && dash2 == 6)
        {
        var SSN1 = parseInt(strInput.substring(0, dash1), 10)
        var SSN2 = parseInt(strInput.substring(dash1+2, dash2), 10)
        var SSN3 = parseInt(strInput.substring(dash2+2, strInput.length), 10)
        if (isNaN(SSN1) || isNaN(SSN2) || isNaN(SSN3))
            {
            // there is a non-numeric character in one of the component values
            alert("NaN: The SSN entry is not in an acceptable format.\n\nYou should enter SSN as nnn-nn-nnnn.")
            return false
            }

        }
    else
        {
            // there are no dashes or they are in the wrong places
            alert("Bad Dash: The SSN entry is not in an acceptable format.\n\nYou should enter SSN as nnn-nn-nnnn.")
            return false
         }
    return true;
}

// Replaces the first occurrance of chFind with chReplacement in strInput
// and returns the result.
//
// Used in isDate() and isPhone()
function replaceString(strInput, chFind, chReplacement) {
    var i = strInput.indexOf(chFind)
    var str = strInput.substring(0, i) + chReplacement + strInput.substring(i + 1, strInput.length)

    return str;
}

// date objField validation
function isDate(inputValue) {
    var inputStr = inputValue.toString()

    if (isNaN(Date.parse(inputStr)))   {
       return(false);
    }
    var datInput = new Date(Date.parse(inputStr))
    if (datInput.getFullYear() < 1920) {
       return(false);
    }
    return(true);
}

// Function to see  if input is valid US phone number
function isPhone(strInput) {
    // Phone numbers can be
    // AAA.EEE.NNNN or AAA-EEE-NNNN    //CMK - 13 Apr 2000 (Added dash format)

    var inputStr = strInput.toString()
    //CMK - 13 Apr 2000  - Changed Dots to dashes in error message...
    var strFormatMsg = "The phone entry is not in an acceptable format.\n\nPlease enter phone numbers in the following format ###-###-####." //CMK - 13 Apr 2000  //FTR (N's->#'s) 9/7/00

   if (inputStr.length != 12) {return false;}

    //CMK - 13 Apr 2000 - Added this line to change dos to dashes in supplied phone number
    //CMK - 13 Apr 2000
   inputStr = inputStr.replace(/\./g, "-"); //EJH - 5 Sept 2000 This line replaces dots with dashes - The slashes are a regular expression, the backslash negate the period as a wildcard, and the g makes the replace global.


    // Find the delimiters, if any
    //CMK - 13 Apr 2000 - Changed these indexOfs to look for dashes instead of dots.
    var delim1 = inputStr.indexOf("-");   //CMK - 13 Apr 2000
    var delim2 = inputStr.lastIndexOf("-")   //CMK - 13 Apr 2000
    if (delim1 != -1 && delim1 == delim2)
        {
        // there is only one delimiter in the string
        //alert(strFormatMsg)
        return false
        }

    // dashes must be in the right places
    if (delim1 != 3 || delim2 != 7)
        {
        //alert(strFormatMsg)
        return false;
        }

    if (delim1 != -1)
        {
        // there are delimiters; extract component values
        var AreaCode = parseInt(inputStr.substring(0, delim1), 10)
        var CO = parseInt(inputStr.substring(delim1 + 1, delim2), 10)
        var Number = parseInt(inputStr.substring(delim2 + 1, inputStr.length), 10)
        }
    else
        {
        // there are no delimiters; extract component values
        //alert(strFormatMsg)
        return false
        }

    if (isNaN(AreaCode) || isNaN(CO) || isNaN(Number))
        {
        // there is a non-numeric character in one of the component values
        //alert(strFormatMsg)
        return false
        }

    if (AreaCode < 100 || AreaCode > 999)
        {
        //alert(strFormatMsg)
        return false
        }
    if (CO < 100 || CO > 999)
        {
        //alert(strFormatMsg)
        return false
        }

    if (Number < 0 || Number > 9999)
        {
        //alert(strFormatMsg)
        return false
        }

    return true;

}

// 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;

   // whitespace characters
   var whitespace = " \t\n\r";

    // 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;
}

// function to see if input is valid US or Canadian Postal Code
function isPostalCode(s)
{
        var sPart1
        var sPart2
        var sPart3

   // OK if US Zip Code
   if (s.length == 5 && isPosInteger(s)) {return true;}
        if (s.length == 10) {

                // Is it an extended zip code
                sPart1 = s.substring(0,5);
                sPart2 = s.substring(5,6);
                sPart3 = s.substring(6,10);

                if (isPosInteger(sPart1) == true && isPosInteger(sPart3) == true && sPart2 == "-")
                        {return true;
                        }
                else
                {return false;}
        }

   // OK if Canadian Postal Code
   //if (s.length == 6) {return true;}

   // Otherwise its not OK!
   return false;
}

//
function isEmail (s) {
    // 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 nLength = s.length;

    // look for @
    while ((i < nLength) && (s.charAt(i) != "@"))
    { i++
    }
   // The must be something after the @
    if ((i >= nLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < nLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= nLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

//
// Function isTime()
// Returns true if the given string contains a valid 12 hour time.
//
function isTime(s) {
   // HH:MM AM or HH:MM am or HH:MM PM or HH:MM pm
   // HH = 1 - 12, MM = 0 - 59

   var aApP = "aApP"

   // Find colon and space -- be sure they're in the right places
    var iColon = s.indexOf(":")
    var iSpace = s.lastIndexOf(" ")

   // h:mm am or hh:mm am
   if ((iColon != 2 && iColon != 1) || iSpace != 5 && iSpace != 4) {return false;}

   // Get the hours, minutes and am/pm
    var hh = parseInt(s.substring(0, iColon), 10)
    var mm = parseInt(s.substring(iColon + 1, iSpace), 10)
    var ap = s.charAt(iSpace + 1)

    if (isNaN(hh) || isNaN(mm))
        {
        // there is a non-numeric character in one of the component values
        return false
        }

   // Be sure values are in range
   if (hh < 1 || hh > 12) { return false; }
    if (mm < 0 || mm > 59) { return false; }
    if (aApP.indexOf(ap) == -1) return false;

   return true;
}

function isCurrency(s) {
   // Allow $ -- ignore it
   return isNumber(replaceString(s, "$", ""));
}

// End of validation.js
