/*==================================================
sk_split(string-in,delimiter)

Description: 
   SupportKobe's answer to the JavaScript function "split".
   Because a call to x.split() in some instances of "IE3.02 (4.70 1300)"
   didn't work, we provide functionality which does the same
   thing.  It takes the string you pass to it and creates an
   array, using the delimiter you pass it as the character
   which separates each element in the array.

   This function was added at the last minute, as we were
   going to press with this library, so it is not advertised
   in the 1.0 version of the library.

Input:
   string_in:  the name of the string from which you want to 
      create an array

   delimiter:  the character which separates each element in
      the array

Example: 
   If 'mydate' contains "01/23/1999", and you include this code:
 
          new_array = sk_split(mydate,"/");

   new_array will contain the following:
     new_array[0]: 01
     new_array[1]: 23
     new_array[2]: 1999 

Returns:
   The array and its elements
  
==================================================*/
function sk_split(string_in, delimiter)
{
   var num_chars = string_in.length;
   var index = 0;
   var beg = 0;
   var end = 0;
   var my_array = new Array();

   for (var i=0; i<num_chars;i++)
   {
      if (string_in.charAt(i) != delimiter)
      {
         end++;
      }
      else
      {
         my_array[index] = string_in.substring(beg,end);  
         end++;
         index++;
         beg = end;
      }
   }
   end=num_chars;
   my_array[index] = string_in.substring(beg,end);  
   return my_array;
}

/*==================================================
sk_isValidDate(date)

Description: 
   Strips off leading, trailing blanks. 
   Verifies that the remaining characters constitute valid date.
   Checks for valid leap year if month/day = 2/29.

Input:
   Date in form of mm/dd/yy or mm-dd-yy, with no embedded blanks.  
   mm and dd can be 1 or 2 characters.
   yy can be 1 - 4 characters.

Returns:
   True if valid; false if invalid.
  
==================================================*/
function sk_isValidDate(date_in)
{
   var mydate = sk_trim(date_in);
   var num_chars = mydate.length;
   var returnValue = true;
   var date_array = new Array();

/* Make sure there are valid number of characters */
   if (mydate.length > 10 || mydate.length < 5)
      returnValue = false;

/* Make sure date is numbers, dashes, or slashes */
/* We exit if there are invalid chars, because Netscape 3
   gives JavaScript errors if it encounters them in later
   checks in this function */

  if (!sk_isValidValue(mydate,"0123456789-/"))
      return(false);

/* Determine if delimiters are "-" or "/" */
   if ((mydate.indexOf("-")) != -1)
   
      /* Delimiter is  "-" */
        date_array = sk_split(mydate,"-");
   
   else
   if ((mydate.indexOf("/")) != -1)
   {
      /* Delimiter is  "/" */
        date_array = sk_split(mydate,"/");
   }

/* Split date into month, day and year components */
   var month = date_array[0];
   var day   = date_array[1];
   var year  = date_array[2];

/* See if there are more than 2 delimiters */
   if (date_array[3] != null)
        return(false);

/* Check each for null */
if ((month == null) || (day == null) || (year == null))
    return(false);

/* Check each for numeric */
if (((!sk_isNumeric(month)) || (!sk_isNumeric(day)) || (!sk_isNumeric(year))))
   return(false);
 

/* Check for month 1-12, day 1-31, year 0-9999 */
/* We exit if the length of a field is zero, because some browsers
   give JavaScript errors if they encounter zero-length fields in later
   checks in this function */

   if (month.length == 0) return(false);
   if (day.length == 0)   return(false);
   if (year.length == 0)  return(false);

   if (month<1 || month>12 || month == null)   returnValue = false; 
   if (day<1 || day>31 || day == null)         returnValue = false;
   if (year<0 || year>9999 || year == null)    returnValue = false;
        
/* Check months with 30 days */
   if (month==4 || month==6 || month==9 || month==11)
      {if (day==31)
        returnValue = false;
      }
/* Check for leap year */
   if (month==2 && day==29)
      {
      /* For years 0-3, dividing the year by 4 should result in a first character of "." (non-integer) */
         var g=parseInt(year/4);
         if (isNaN(g)) returnValue = false;
       
        /* for years 4-9999 */
           if (day>29) returnValue = false;

        /* Year must be evenly divisible by 4 */
           if (day==29 && ((year/4)!=parseInt(year/4)))  
             returnValue = false;
           else 
           { /*begin else */
             if (day==29 && ((year/100)==parseInt(year/100)))  
             { /* begin if 'a' */

        /* Centuries over 400 must also be evenly divisible by 400 */ 
               if (year==100 || year==200 || year==300) returnValue=false;
               if (year > 399)
               { /* begin if 'b' */
                 var leap_400 = year/400;
                 var leap_400_converted = leap_400.toString();
                 ll = sk_isNumeric(leap_400_converted);
                 if (!ll)
                   returnValue=false;
               } /* end if 'b' */
             } /* end if 'a' */ 
           } /* end else */
      }

   return(returnValue);
}

/*==================================================
sk_isAlpha(string)

Description: 
   Determines if the passed string consists entirely 
   of alphabetic characters.

Input:
   String to check for alpha

Returns:
   True if string is alpha; false if not.

Comments:
   Valid characters are a-z, A-Z, and spaces.
   Numbers and empty fields are invalid.

==================================================*/

function sk_isAlpha(value) 
{        
/* Check for alpha characters */
        {
            if (sk_isValidValue(value,"abcdefghijklmnopqrstuvwxyz ")) 
               returnValue = true;
            else 
               returnValue = false;
        }
        return(returnValue);
}

/*==================================================
sk_isAlphaNum(string)

Description: 
   Determines if the passed string consists only of alphanumeric
   characters.

Input:
   String to check for alphanumeric

Returns:
   True if string is alphanumeric; false if not.

Comments:
   Valid characters: a-z, A-Z, 0-9, leading spaces,trailing spaces.
   Invalid characters: special characters, empty field 

==================================================*/
function sk_isAlphaNum(value) 
{        
/* Check for alphanumeric characters */
         {
            if (sk_isValidValue(value,"abcdefghijklmnopqrstuvwxyz 1234567890")) 
               returnValue = true;
            else returnValue = false;
         }
         return(returnValue);
}

/*==================================================
sk_isEmail(address)

Description: 
   Checks address passed to it for valid format.
   Alphabetic and numeric characters, @,".", "-", space, and "_"
     are allowed.  
   The minimum characters allowable are:  x@x.x
     At least 1 alpha character must occur before the @;
     At least 2 alpha characters must occur after the @, separated by a "."
     It may contain only one @. 

Input:
   The e-mail address to check.

Returns:
   True if valid; false if invalid.

Comments:
   Examples of valid addresses:
    john.doe2@pss.boeing.com 
    john.x doe@b.y
    j_doe@b.com
    john-doe @boeing.com

==================================================*/
function sk_isEmail(value) 
{
  var address=sk_trim(value);
  var length = address.length;

/* Initialize variables */
  var returnValue = true; 
  var letter_before_at = false;
  var letter_after_at = false;
  var at_found = false;
  var dot_after_at = false;
  var letter_after_dot = false;

/* Verify that address is not empty, and contains, as a minimum, @ and "." */
  if (!(address.indexOf("@") + "" != "-1" &&
        address.indexOf(".") + "" != "-1" && value != ""))
     {
       returnValue = false;
     }
  else

/* Make more detailed validity checks */
     for (var i=0; i<length; i++) 
     { /* begin for */
        var thechar = address.substring(i, i+1);

/* See if it is a character, number, or special character */
        if (!sk_isValidValue(thechar,"abcdefghijklmnopqrstuvwxyz0123456789@.#-_'!~`$%^&*()+={}[]|<>?"))
              returnValue = false;
        else
        { /* begin else A */
/* Valid characters; see if it is an alpha character */
        if (sk_isAlpha(thechar)) 
           {
               if ((letter_before_at==false) && (at_found==true)) returnValue = false;
                  else letter_before_at = true;
               if ((letter_before_at==true) && (letter_after_at==false) && (at_found==true))
                  letter_after_at = true;
               if ((letter_after_dot==false) && (dot_after_at==true))
                  letter_after_dot = true;
           }

        /* Not alpha, see if @ (can't be but one) */
        else
           { /* begin else B */
               if (thechar == "@")
               {
                  if (at_found == true) returnValue = false;
                  else
                     at_found = true;
               } 
               else
               /* Not alpha or @; see if "." */
                  {
                     if (thechar == ".")
                     {
                        if ((at_found == true) && (letter_after_at==true))
                           dot_after_at = true;
                     }
                  }
           } /* end else B */       
        } /* end else A */
     } /* end for */
  if (!(letter_after_dot))
     returnValue = false;
  return(returnValue);
}

/*==================================================
sk_isEmpty(value)

Description: 
   Determines if the passed string contains no non-blank characters.

Input:
   String to check

Returns:
   True if string contains no non-blank characters; false if it 
   contains non-blank characters.

==================================================*/
function sk_isEmpty(value) 
{        
/* Strip leading and trailing spaces, check for zero length  */
   return ((sk_trim(value) == null) || (sk_trim(value).length == 0));
}

/*==================================================
sk_isNumeric(value)

Description: 
   Determines if the passed value consists entirely 
   of numeric characters.

Input:
   Value to check for numeric

Returns:
   True if value is numeric; false if not.

==================================================*/
function sk_isNumeric(value)
{
   var returnValue = true;
   var num_other = 0;
   var value_length = value.length;
     for (var i=0; i<value_length; i++) 
     { /* begin for */
        var thechar = value.substring(i, i+1);
        if (!(sk_isValidValue(thechar,"0123456789")))
          num_other = i+1;
     }/* end for */
   if (num_other == 0)
      returnValue = true;
   else
      returnValue = false;
   return (returnValue);
}


/*==================================================
sk_isPhone(number)

Description: 
   Checks phone number passed to it for 4 valid formats:
   xxxyyyzzzz, xxx-yyy-zzzz, ccxxxyyyzzzz, and cc-xxx-yyy-zzzz

Input:
   The phone number to check

Returns:
   True if valid; false if invalid

==================================================*/
function sk_isPhone(value) 
{
    value = sk_trim(value);
	/* Check for right number of characters */
	var sk_str = value + "";
	var err = 0;
    if ((sk_str.length != 10) && (sk_str.length != 12) 
       && (sk_str.length != 15)) 
       {return false;}

/* Check 10-character for all numbers (aaabbbcccc) */
if (sk_str.length == 10)
   {for (var i = 0; i < 10; i++)
        {var ch1 = sk_str.substring(i,i+1)
        if (!sk_isNumeric(ch1))
           {return false;}
        }
    return true;
   }

/* Check 12-character for all numbers (xxaaabbbcccc) */
if ((sk_str.length == 12) && (sk_str.substring(3,4) != "-"))
   {for (var j = 0; j < 12; j++)
        {var ch2 = sk_str.substring(j,j+1)
        if (isNaN(ch2))
           {return false;}
        }
    return true
   }

/* Check 12-character number for dashes in right places (aaa-bbb-cccc) */
if ((sk_str.length == 12) && (sk_str.substring(3,4) == "-"))
   {{
    if (sk_str.substring(7,8) != "-")
       {return false}
   }

   // Check to see that the rest are all numbers   
   {for (var k = 0; k < 3; k++)
        {var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
   }

   {for (var k = 4; k < 7; k++)
        {var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
   }
   {for (var k = 8; k < 12; k++)
        {var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
   }
   return true}

// Check 15-character number for dashes in right places (xx-aaa-bbb-cccc)

if ((sk_str.length == 15) && (sk_str.substring(2,3) == "-"))
{   {
     if (sk_str.substring(6,7) != "-")
       {err = 1;}
     if (sk_str.substring(10,11) != "-")
       {err = 1;}
    }
   
    {
     for (var k = 0; k < 2; k++)
        {
        var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
    }

    {
     for (var k = 3; k < 6; k++)
        {
        var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
    }
    {
     for (var k = 7; k < 10; k++)
        {
        var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
    }
    {
     for (var k = 11; k < 15; k++)
        {
        var ch3 = sk_str.substring(k,k+1)
        if (isNaN(ch3))
           {return false}
        }
    }
     return true}

}

/*==================================================
sk_isSSN(SSN)

Description: 
   Checks to see if Social Security Number is in correct
   format (xxx-yy-zzzz or xxxyyzzzz), where x,y,z are numeric

Input:
   Social Security Number to validate

Returns:
   True if valid; false if invalid

==================================================*/

function sk_isSSN(value)
{
        var returnValue=true;
        var passedSSN=sk_trim(value);

/* See if SSN is the right number of characters in length */
        if (passedSSN.length != 11 && passedSSN.length !=9) 
            returnValue = false;
        else 
           {/* begin else */
/* If 9 characters input, see if all numeric */
            if (passedSSN.length ==9)
               {var dd = isNumber(passedSSN,9)
                if (!dd) returnValue = false;
               }
/* If 11 characters input, isolate the dashes, check for numeric */
            if (passedSSN.length == 11)
               {         
/* Pick up the components of the SSN */
                SSN1 = passedSSN.substring(0, 3);  // string of 3
                dash1 = passedSSN.substring(3,4);  // '-'
                SSN2 = passedSSN.substring(4,6);   // string of 2
                dash2 = passedSSN.substring(6,7);  // '-'
                SSN3 = passedSSN.substring(7,11);  // string of 4

/* See if the dashes and numbers are in the right positions */
/* The function "isNumber" was created to get around the problem of Netscape 3.0 
   not processing the 'isNaN' function properly. */
                var aa = isNumber(SSN1,3);
                var bb = isNumber(SSN2,2);
                var cc = isNumber(SSN3,4);
                if (!aa || !bb || !cc)   returnValue = false;
                if (dash1 != '-'|| dash2 != '-')  returnValue = false;
               }  /* end if */ 
           }  /* end else */

/* Exit with appropriate return value */
        return (returnValue);
}

/*==================================================*/
function isNumber(field,length) 
{
   var returnValue = true;
   if (length > 0) 
   {
      var inputstr = field;
      for (var i=0; i<length; i++) 
      {
         var thechar = inputstr.substring(i, i+1);
         if ((thechar < "0") || (thechar > "9")) 
         {
            returnValue = false;
         }
      }
   }
   else
      returnValue = false;
   return(returnValue);
}


/*==================================================
sk_isValidValue(string,validchars)

Description: 
   Checks that field value contains only characters 
   from set of valid characters; check is not 
   case-sensitive

Inputs:
   string - the characters to be checked for validity;
   validchars - the string of valid characters

Returns:
   True if valid; false is invalid
   
==================================================*/
function sk_isValidValue(value,validchars) 
{
var returnValue = true;

/* Convert both value and valid characters to upper case */
     value=value.toUpperCase();
     validchars=validchars.toUpperCase();

/* Check each character until either end or invalid char */
     charposn=0;
     while
       ((charposn<value.length)&&(validchars.indexOf(value.charAt(charposn))!=-1)) 
          charposn++;

/* Check if stop was due to end of input string or invalid char and set
return code accordingly */
   if (charposn==value.length) 
      returnValue = true;
   else 
      returnValue = false;
   return(returnValue);
}

/*==================================================
sk_trim(string)

Description: 
   Strips leading and trailing blanks from input string

Input:
   String to trim

Returns:
   Trimmed string

Comments:
 
==================================================*/

function sk_trim(value) 
{
/* Strip leading spaces from input */
		//added by William Shi
        if (value==null || value.length==0)
                return "";
        //end
                
        startposn=0;
        while((value.charAt(startposn)==" ")&&(startposn<value.length)) {
                startposn++;
        }
        if(startposn==value.length) {
                value="";
        } else {

/* If anything left of string after stripping leading spaces, strip
trailing spaces as well */
                value=value.substring(startposn,value.length);
                endposn=(value.length)-1;
                while(value.charAt(endposn)==" ") {
                        endposn--;
                }
                value=value.substring(0,endposn+1);
        }
        return(value);
}


