//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
//- - - - - - - - - - - - - - - - - - - - - - - - - - -

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function JumpToURL(DestinationURL)
  {
    window.location = DestinationURL;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function PopUpWindow(freshurl)
   {
     var SmallWin = null;
     var WinID = 'PopUpWin' + parseInt(Math.random() * 10000).toString(10);
     SmallWin = window.open(freshurl, WinID,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=400,height=300');
     //SmallWin = window.open(freshurl, 'PopUpWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=400,height=300');

     /* if (!isIE4())
       {
         if (window.focus)
           {
             SmallWin.focus();
           }
       }

     if (SmallWin.opener == null) SmallWin.opener = window;
     SmallWin.opener.name = "PUMain";   */
   }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function LaunchInNewWindow(freshurl)
  {
    var AnotherWin = null;
    var WinID = 'win' + parseInt(Math.random() * 10000).toString(10);
    AnotherWin = window.open(freshurl, WinID,'toolbar=yes,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
    //AnotherWin = window.open(freshurl, '','toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function LaunchInNamedWindow(freshurl, WinName, WinOptions)
  {
    var WinHandle = null;
    if ((WinName == null) || (WinName == '')) 
      {WinName = 'win' + parseInt(Math.random() * 10000).toString(10);}
    if ((WinOptions == null) || (WinOptions == ''))
      {WinOptions = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no';}
    WinHandle = window.open(freshurl, WinName, WinOptions);
    return WinHandle;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function PopupsAreEnabled()
  {
    var AnotherWin = null;
    var WinID = 'popup' + parseInt(Math.random() * 10000).toString(10);

    //Popup a small window named WinID, send it to the background, then check to see if it's there...
    AnotherWin = window.open('about:blank', WinID,'width=200,height=150,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no');
    self.focus();

    if (AnotherWin != null) {
      AnotherWin.blur();
      AnotherWin.close();
      return true;
    }
    else {
      return false;
    }
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function FormatMoney(mtext)
  {
   if (typeof mtext == "number")
     {mtext = mtext.toString();}
   if (parseInt(mtext) == 0)
     {return "0.00";}
   else
     {
      //make sure it has a decimal!
      if (mtext.indexOf(".",0) < 0)
        {mtext += ".00";}
      //make sure it has two decimal places
      //check for "." too close to end of string (index 1, 0-base)
      if (mtext.indexOf(".",0) > (mtext.length - 3))
        {mtext += "0";}
      //make sure there are only 2 decimal places
      mtext = mtext.substring(0,mtext.indexOf(".",0)+3);

      return mtext;
     }
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidTextField(FieldVal)
  {
   var aCharExists = false;

   for (var i = 0; i < FieldVal.length; i++)
     {
     // spaces don't count as "data entered"
     if (FieldVal.charAt(i) != " ")
       {
       aCharExists = true;
       break;
       }
     }
   return aCharExists;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidIntNumberField(FieldVal)
  {//This routine validates that a field has only numeric digits in it.
   //It will return FALSE on NULL string or a string with any non-numeric
   //characters, including decimal point, comma, negative|positive signs.
   var aCharExists = false;
   for (var i = 0; i < FieldVal.length; i++)
     {
      // spaces don't count as "data entered"
      if (FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9" || (FieldVal.charAt(i) == "-" && i == 0))
        {
         aCharExists = true;
        }
     else
       {
       aCharExists = false;
       break;
       }
     }
   return aCharExists;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidateIntFormField(FormField, FormFieldName)
  {
    var valid = ValidIntNumberField(FormField.value)
    if (!valid) {
      alert('You must enter a valid Integer number in the ' + FormFieldName + ' field.');
      FormField.focus();
      FormField.select();
      return false;
    }
    else {
      return true;
    }
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidFloatNumberField(FieldVal)
  {//This routine validates that a field has only numeric digits in it
   //but also allows for a single decimal point.
   //It will return FALSE on NULL string or a string with any non-numeric
   //characters, including comma, negative|positive signs.
   var aCharExists = false;
   var decimalcnt = 0;
   for (var i = 0; i < FieldVal.length; i++)
     {
      // spaces don't count as "data entered"
      if ((FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9") || (FieldVal.charAt(i) == "." && decimalcnt == 0))
        {
         aCharExists = true;
         if (FieldVal.charAt(i) == ".") {
           decimalcnt++;
         }
        }
     else
       {
       aCharExists = false;
       break;
       }
     }
   return aCharExists;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidateFloatFormField(FormField, FormFieldName)
  {
    var valid = ValidFloatNumberField(FormField.value)
    if (!valid) {
      alert('You must enter a valid Real number in the ' + FormFieldName + ' field.');
      FormField.focus();
      FormField.select();
      return false;
    }
    else {
      return true;
    }
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidCreditCardField(FieldVal)
  {
   var cnt = 0;

   //cc number must be at least 13 digits without spaces
   for (var i = 0; i < FieldVal.length; i++)
     {
     if (FieldVal.charAt(i) != " " && FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9")
       {
       cnt++;
       }
     }
   if (cnt < 13)
     {return false;}

   return true;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Taken from Ziff-Davis JavaScript Library and modified
// slightly by Ken Mosher
function ValidDateField(datein)
  {
   var AOK = false;
   var indate=datein;

   if (indate.indexOf("-")!=-1) {
     var sdate = indate.split("-")
   }
   else {
     var sdate = indate.split("/")
   }

   var chkDate=new Date(Date.parse(indate))
        
   var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getYear())
   var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
   if (indate2!=cmpDate) {
//     alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");
   }
   else {
     if (cmpDate=="NaN/NaN/NaN") {
//       alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");

     }
     else {
       AOK = true;
//       alert("Your date is valid!");
     }       
   }
   return AOK;
}


//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Taken from Ziff-Davis JavaScript Library 
function ValidDateFieldx(datein)
  {
   var indate=datein;

   if (indate.indexOf("-")!=-1){
           var sdate = indate.split("-")
   }
   else {
           var sdate = indate.split("/")
   }

   var chkDate=new Date(Date.parse(indate))
        
   var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(chkDate.getYear())
   var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
   if (indate2!=cmpDate){
           alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");
   }
   else {
           if (cmpDate=="NaN/NaN/NaN"){
                   alert("You've entered an invalid date or date format.  Please use the MM/DD/YY format.");

           }
           else {
                   alert("Your date is valid!");
           }       
   }
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidEmailField(FieldVal)
  {
   var aCharExists = false;

   //the length obviously needs to be longer than 6
   //a@x.de is shortest valid example
   if (FieldVal.length < 6)
     {return false};

   //check for @ in at least 2nd position (index 1, 0-base)
   if (FieldVal.indexOf("@",0) < 1)
     {return false;}

   //check for . in at least 2nd position past @
   if (FieldVal.indexOf(".",FieldVal.indexOf("@",0)) < (FieldVal.indexOf("@",0)+2))
     {return false;}

   //total length of string must be at least 2 past . after @
   if ((FieldVal.length-1) < (FieldVal.indexOf(".",FieldVal.indexOf("@",0))+2))
     {return false;}

   //can't have any spaces!
   for (var i = 0; i < FieldVal.length; i++)
     {
     if (FieldVal.charAt(i) == " ")
       {
       return false;
       }
     }

   return true;
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidZipCodeField(FieldVal)
  {//This routine validates that a field is a validly formatted U.S. zip code.
   //It will return FALSE on NULL string or a string with any non-numeric
   //characters.
   var aCharExists = false;
   var NumericCharsCnt = 0;
   for (var i = 0; i < FieldVal.length; i++)
     {
      // spaces don't count as "data entered"
      if (FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9")
        {
         aCharExists = true;
         NumericCharsCnt++;
        }
     else
       {
        if (i == 5 && FieldVal.charAt(i) == "-")
          //"-" allowed in slot 5 because i is 0-based
          {
          }
        else
          {
           aCharExists = false;
           break;
          }
       }
     }
   return aCharExists && (NumericCharsCnt == 5 || NumericCharsCnt == 9);
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function ValidPhoneNumberField(FieldVal) {
  //This routine validates that a field is a validly formatted U.S. phone number.
  //It will return FALSE on NULL string or a string with any non-numeric
  //characters other than " ()-.x"
  // valid inputs: (800) 555-1212
  //               800-555-1212
  //               800.555.1212
  //               8005551212
  //               any above with " x12345" for extension
  //
  //Phone numbers with more than 10 digits but missing "x" (extension)
  //are NOT valid.
  //
  //Since the routine only checks that each character is in the 'valid' list,
  //and since it only counts numeric characters to determine phone # length,
  //this has the side effect of making ")(800)( 555---1212 xX123" a perfectly
  //valid phone number.

  var ok;
  var legalmask = '0123456789 ()-.xX';
  var numericcharcnt = 0;
  var foundext = false;

  //check to see if ALL characters are legal
  ok = true;
  for (var i = 0; i < FieldVal.length; i++) {
    if (legalmask.indexOf(FieldVal.charAt(i)) < 0) {
      ok = false;
    }
    else {
    }

    //remove ALL non-numeric characters and store in tphone
    //count numeric characters to make sure we have enough
    if (FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9") {
      numericcharcnt++;
    }

    if (FieldVal.charAt(i).toLowerCase() == 'x') {
      foundext = true;
    }
  }

  //alert('numchar = ' + numericcharcnt + '\nok = ' + ok + '\nNum=10 & !ext = ' + (numericcharcnt == 10 && !foundext) + '\nNum>10 & ext = ' + (numericcharcnt > 10 && foundext));
  return ok && ((numericcharcnt == 10 && !foundext) || (numericcharcnt > 10 && foundext));
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function FormatPhoneNumber(FieldVal) {
  //This routine formats a string into a standard phone number format
  // i.e. (800) 555-1212 x12345
  //See ValidPhoneNumberField() function for a description of what
  //are considered valid input numbers.
  //
  //Phone numbers with more than 10 digits but missing "x" (extension)
  //are NOT valid.
  //
  //Since the routine only checks that each character is in the 'valid' list,
  //and since it only counts numeric characters to determine phone # length,
  //this has the side effect of making ")(800)( 555---1212 xX123" a perfectly
  //valid phone number. However, it will be returned in the standard format.
  var ok;
  var foundext = false;
  var legalmask = '0123456789 ()-.xX';
  var numericcharcnt = 0;
  var tphone = '';
  var FormattedValue = '';

  //check to see if ALL characters are legal
  for (var i = 0; i < FieldVal.length; i++) {
    //remove ALL non-numeric characters and store in tphone
    if (FieldVal.charAt(i) >= "0" && FieldVal.charAt(i) <= "9") {
      numericcharcnt++;
      tphone = tphone + FieldVal.charAt(i);
    }

    if (FieldVal.charAt(i).toLowerCase() == 'x') {
      foundext = true;
    }
  }

  FormattedValue = '';
  if (tphone.length >= 10) {
    //it's long enough to be a phone number
    FormattedValue = '(' + tphone.substring(0,3) + ') ' + tphone.substring(3,6) + '-' + tphone.substring(6,10)

    if (tphone.length >= 11 && foundext) {
      //it's long enough to contain an extension
      FormattedValue += ' x' + tphone.substring(10,tphone.length)
    }
  }
  else {
  } 

  return FormattedValue;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function max(val1, val2)
  //Might want to use Math.max(val1,val2) instead
  {
   if (val1 >= val2)
     {return val1;}
   else
     {return val2;}
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Written by Ken Mosher
function min(val1, val2)
  //Might want to use Math.min(val1,val2) instead
  {
   if (val1 <= val2)
     {return val1;}
   else
     {return val2;}
  }


//======================================================================
// These routines were not written by Ken Mosher.  They were lifted
// from various places, including www.devhead.com
//======================================================================


//- - - - - - - - - - - - - - - - - - - - - - - - - - -
//ripped off from http://www.htmlcodetutorial.com/forms/index_famsupp_157.html
function SubmitOnEnter(myfield,ev)
{
var keycode;
//IE and Netscape/Mozilla/Firefox return keypress differently
if (window.event) keycode = window.event.keyCode;
else if (ev) keycode = ev.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}


//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function notNull(str) {
  if (str.length == 0 )
    return false
  else
    return true
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function notBlank(str) {
  for (i = 0; i < str.length; i++) {
    if (str.charAt(i) != " ")
      return true
  }
      return false
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function isSize(str, size) {
  if (str.length == size)
    return true
  else
    return false
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Returns TRUE only if all chars in string are 0 to 9
function isDigits(str) {
  var i
  for (i = 0; i < str.length; i++) {
    mychar = str.charAt(i)
    if (mychar < "0" || mychar > "9")
      return false
  }
  return true
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Returns TRUE if all chars in string are 0 to 9 and
// also allows ONE decimal point "."
function isNumber(str) {
  numdecs = 0
  for (i = 0; i < str.length; i++) {
    mychar = str.charAt(i)
    if ((mychar >= "0" && mychar <= "9") || mychar == ".") {
      if (mychar == ".")
        numdecs++
    }
    else
      return false
  }

  if (numdecs > 1)
    return false

return true
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Returns TRUE if INTEGER string is in range specified by
// NUM1 and NUM2
function isInRange(str, num1, num2) {
  var i = parseInt(str)
  return((i >= num1) && (i <= num2))
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function stripNonDigits(str)
  {
    var i
    var newstring = ""
    for (i = 0;  i < str.length; i++)
      {
        mychar = str.charAt(i)
        if (isDigits(mychar))
          newstring += mychar
      }
    return newstring
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function stripChars(str, chars)
  {
    var i
    var newstring = ""
    for (i = 0;  i < str.length; i++)
      {
        mychar = str.charAt(i)
        if (chars.indexOf(mychar) == -1)
          newstring += mychar
      }
    return newstring
  }
    
//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function init(strval)
  {
    if (strval == null)
      {
      return "";
      }
    else
      {
      return strval;
      }
  }

//- - - - - - - - - - - - - - - - - - - - - - - - - - -
function randDigit(n)
  {
    return parseInt(Math.random() * n + 1);
  }