//  You may not remove or change this notice.
//  Copyright Bret Burkett 1998-2006. All rights reserved.
//
// This states the conditions under which the code may be copied, such that 
// the Copyright Holder maintains some semblance of artistic control over the 
// development of the code, while giving the users of the code the right to use 
// and distribute the code in a more-or-less customary fashion, plus the right to 
// make reasonable modifications.
//
// "Freely Available" means that no fee is charged for the item itself, though 
// there may be fees involved in handling the item. It also means that recipients 
// of the item may redistribute it under the same conditions they received it.
//
// You may make and give away copies of the source code without restriction, provided
// that you duplicate all of the original copyright notices and disclaimers.
// You may otherwise modify your copy of this code in any way, provided that you 
// insert a prominent notice in each changed stating how and when you changed that 
// file. You may use the modified code only within your corporation or organization.
//
//  Do not sell this as your own work or remove this copyright notice. 
//  Please give credit on sites that use this code
//  Revision: 3.0                 Date: 2006/02/01 10:41am 

// globel variables
var err_win;
var err_Msg_Array;
var err_Loc_Array;
var idx_errors = 0;
var frame_loc;
var form_loc;
var chg_occured = false;
var save_type = 'nosave';
var form_save_type;

// ERROR VARIABLES
var white_spc = " \t\n\r";// whitespace characters
var dec_pnt = "."
var date_dvdr = "/-.";
var time_dvdr = ":";
var astrk = "*"
var days_mm = new Array(12);
days_mm[1] = 31;
days_mm[2] = 29;   // must programmatically check this
days_mm[3] = 31;
days_mm[4] = 30;
days_mm[5] = 31;
days_mm[6] = 30;
days_mm[7] = 31;
days_mm[8] = 31;
days_mm[9] = 30;
days_mm[10] = 31;
days_mm[11] = 30;
days_mm[12] = 31;


// ************************************************************
// controling functions
// ************************************************************

function submitCall(buttonclicked)
{
whereto=buttonclicked+'.htm';
var skip_cd=false;
if ((save_type== "save") && (chg_occured == true))// has not been updateded or submited
 {
 var mess_part1="If you continue you will LOSE your data!\n\n";
 var mess_part2="To save your data, return and select update!\n\n";
 var mess_part3="OK to continue     Cancel to return.";
 Continue = window.confirm(mess_part1 + mess_part2 + mess_part3);
 if (Continue)// the user wants to lose data
  {
  chg_occured = false;// dont want to save
  save_type= 'nosave';// bypass validating
  }
 else
  {
  skip_cd = true;
  }
 }
if (save_type== "saved")
 {
 skip_cd = validationRoutine();
 }
if (form_loc.elements[0].value=="ERRORPAGE")
 {
 if (!(buttonclicked == "LOGOFF"))
  {
  skip_cd = true;
  alert("A major error has occured please log off. ");
  }
 }
if (skip_cd != true)
 {
 //form_loc.submit();// submit  page to sever program
 whereto_loc.location = whereto;// go to next page wont use for prog calls!!!!!!!!!!!!
 }
}	// end of SubmitCall function



// ************************************************************
// button functions
// ************************************************************

// clears all input type on a form
function clearForm(frmNm)
{
var ele_type;
var doc_loc = document.forms[frmNm];
for (var idx = 0; idx < doc_loc.elements.length; idx++)
 {
 ele_type =  doc_loc.elements[idx].type;
 if (ele_type == 'checkbox')
  {
  doc_loc.elements[idx].checked = false;
  }
 if ((ele_type == "text") || (ele_type == "textarea") || (ele_type == "password"))
  {
  doc_loc.elements[idx].value = "";
  }
 if ((ele_type == "select-one") || (ele_type == "select-multiple"))
  {
  doc_loc.elements[idx].options[0].selected = true;
  for (var idx_opt = 1; idx_opt < doc_loc.elements[idx].options.length; idx_opt++)
    {
    doc_loc.elements[idx].options[idx_opt].selected = false;
    }
  }
 }
}

// ************************************************************
// common error functions
// ************************************************************

// initializes the error fields
function initErr()
{
idx_errors = 0;
err_Msg_Array = new Array();
err_Loc_Array = new Array();
}

//funtion to check if its open and close if necessary
function closeErrWin()
{
   if (err_win != null)
      {
      if (!err_win.closed) err_win.close();
      }
}

// called before submiting a page to say if the page passed validation
// or displays the errors
function validationRtn()
{
var retVal = true;
initErr();
if (!validForm())// there were errors
   {
   save_type = form_save_type;// initialize back to starting value
   retVal = false;// no longer allowed to update
   displayErrors();
   }
else
   {//make sure error window is closed
   closeErrWin();
   }
return retVal;
}

// loads the error array assumes form 0
function errRtn(page_loc,ele_loc,msg_type)
{
err_Msg_Array[idx_errors] = msg_type;
err_Loc_Array[idx_errors] = page_loc.document.forms[0].elements[ele_loc];
idx_errors++;
}

// loads the error array uses the form passed
function errRtnLoad(page_loc,ele_loc,msg_type,form_nm)
{
err_Msg_Array[idx_errors] = msg_type;
err_Loc_Array[idx_errors] = page_loc.document.forms[form_nm].elements[ele_loc];
idx_errors++;
}

// this function will open the errorpage in a new window
function displayErrors()
{
defaultStatus = "Validation Error check Error Window";
err_win= window.open('','ErrorWin','scrollbars,width=500,height=150,screenX=15,screenY=100,resizable=no');
err_win.document.open();
err_win.document.writeln('<html>');
err_win.document.writeln('<script type="text/javascript">');
err_win.document.writeln('var loc_name;loc_name = self.opener;');
err_win.document.writeln('function goToErrors(goTo_idx) {');
err_win.document.writeln('loc_name.err_Loc_Array[goTo_idx].focus();');
err_win.document.writeln('var loc_type=loc_name.err_Loc_Array[goTo_idx].type');
err_win.document.write('if ((loc_type=="text") || (loc_type=="textarea") ||');
err_win.document.writeln(' (loc_type=="password")) {');
err_win.document.writeln('loc_name.err_Loc_Array[goTo_idx].select();}');
err_win.document.writeln('} </scr'+'ipt>');
err_win.document.writeln('<style type="text/css">');
err_win.document.write('Table {padding-top: 1; padding-bottom: 2; p');
err_win.document.writeln('adding-left: 1; padding-right: 1;}');
err_win.document.writeln('TD {font: 9pt/9pt arial;}');
err_win.document.writeln('Body {font: 9pt/9pt arial black;}');
err_win.document.write('.b1 {text-decoration: none; ');
err_win.document.write('border-right: 2px outset; border-top: 2px outset; ');
err_win.document.write('border-left: 3px outset; border-bottom: 2px outset; ');
err_win.document.writeln('background-color: #dcdcdc; color: black;}');
err_win.document.writeln('.err {color: red; text-decoration: none;}');
err_win.document.writeln('</style>');
err_win.document.writeln('<head><title>Error Window</title></head>');
err_win.document.writeln('<body style="background-color: #fefcf3">');
err_win.document.writeln('<table width="100%">');
for(var idx_array = 0; idx_array < err_Msg_Array.length; idx_array++)
   {
   err_win.document.write('<tr><td align="left" valign="top" width="85%">');
   err_win.document.writeln(err_Msg_Array[idx_array]);
   err_win.document.write('</td><td align="right" width="15%">');
   err_win.document.write('<a class="err" href="javascript:goToErrors(');
   err_win.document.write(idx_array);
   err_win.document.write(')">Go To Error</a>');
   err_win.document.writeln('</td></tr>');
   }
err_win.document.write('<tr><td colspan="2">&nbsp;</td></tr><tr>');
err_win.document.writeln('<td align="center" colspan="2" style="height: 30px"><br />')
err_win.document.write('<a href="javascript:self.close()" class="b1"');
err_win.document.writeln('>&nbsp;&nbsp;Close&nbsp;&nbsp;</a></td>');
err_win.document.write('</tr></table></body></html>');
err_win.document.close();
err_win.focus();
}

// ********************************************************************************************
// functions for internal use are below
// ********************************************************************************************

// Returns true if character chr is an English letter (A .. Z, a..z).
function isLetter(chr)
{return (((chr >= "a") && (chr <= "z")) || ((chr >= "A") && (chr <= "Z")));}

// Returns true if character chr is a digit (0 .. 9).
function isDigit(chr)
{return ((chr >= "0") && (chr <= "9"));}

// Returns true if character chr is a letter or digit.
function isLetterOrDigit(chr)
{return (isLetter(chr) || isDigit(chr));}

// Given integer argument year, returns number of days in February of that year.
function daysInFebruary(year)	// daysInFebruary (INTEGER )
{// 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 );
}

// isIntInRng returns true if string str is an integer within the range of integer
// arguments int1 and int2 !!!!USE INTERNALLY only validate str as an integer first
// isIntInRng(STRING str,INTEGER int1,INTEGER int2)
function isIntInRng(str, int1, int2)
{// 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).
var num = parseInt(str, 10);
return ((num >= int1) && (num <= int2));
}

// calculates and returns the number of postions after the decimal
function expLgth(num)
{
var str_char;
var start_remainder = 0;
var exp_lgth = 0;
var seen_dec = false;
var hold_str = new String(num);
for (var idx_loop = 0; idx_loop < hold_str.length; idx_loop++)
   {
   if (seen_dec)
      {
      start_remainder = idx_loop;
      exp_lgth = exp_lgth + 1;
      }
   str_char = hold_str.charAt(idx_loop);
   if (str_char == dec_pnt)
      {
      seen_dec = true;
      }
   }
return exp_lgth;
}

// ********************************************************************************************
// functions for validation are below
// ********************************************************************************************
// CONSTANT STRING DECLARATIONS
var twodates_req = "The date fields require information in them. ";
var priceformat = "The price must be in a decimal format. ";

// Check whether string is empty.
function isEmpty(str)
{
if ((str == null) || (str.length == 0)) return true;
for (var idx = 0; idx < str.length; idx++)
   {
   if (white_spc.indexOf(str.charAt(idx)) == -1) return false;
   }
return true;    // All characters are whitespace.
}

// returns true if is zeroes or 0.0
function isZero(str)// isZero (STRING)
{
var seen_dec_pnt = false;
if (isEmpty(str) == true) return true;
if (str == dec_pnt) return "A decimal point is in the field. ";
for (var idx = 0; idx < str.length; idx++)
   {
   var chr = str.charAt(idx); // Check that current character is zero or dot
   if ((chr == dec_pnt) && !seen_dec_pnt) seen_dec_pnt = true;
   else if (!(chr == 0)) return "The field has more than zeroes in it. ";
   }
return true;    // All characters are zeros
}

// validates the string for an integer
// uses flgReq to see if the field is required and fldNm for the error message
function isInteger(str, flgReq, fldNm)
{
var strInt = new String(str);
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
//  if (flgReq == true)
//    {    return "The " + fldNm + " requires a number in it. ";    }
//  else
//    {    return true;    }
  }
for (var idx = 0; idx < strInt.length; idx++)
   {
   if (!isDigit(strInt.charAt(idx))) return "The " + fldNm + " must be in a number format. ";
   }
return true;    // All characters are numbers.
}

// True if string is an unsigned floating point (real) number.
// Also returns true for unsigned integers. If you wish to distinguish between integers
// and floating point numbers, first call isInteger, then call isFloat.
// uses flgReq to see if the field is required and fldNm for the error message
// max_dec and max_pre are coded optionally you don't have to pass
function isFloat(str, flgReq, fldNm, max_decimal, max_pre)
{
var idx, ct_limit = 0, ct_max = 0;
var seen_dec_pnt = false;
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
if (str == dec_pnt) return "The " + fldNm + " is only a decimal point. ";
for (idx = 0; idx < str.length; idx++)
   {
   var chr = str.charAt(idx);     // Check that current character is number.
   if ((chr == dec_pnt) && !seen_dec_pnt) seen_dec_pnt = true;
   else if (!isDigit(chr)) return "There is a nonnumeric character in the " + fldNm + ". ";
   if (isFloat.arguments.length >= 4)
      {
      if (seen_dec_pnt)
         {
         ct_limit++;
         if (ct_limit > (max_decimal+1)) return fldNm + " can only have " + max_decimal + " numbers after the decimal point. ";
         }
      }
   if (isFloat.arguments.length == 5)
      {
      if (!seen_dec_pnt)
         {
         ct_max++;
         if(ct_max > max_pre) return fldNm + " can only have " + max_pre + " numbers before the decimal point. ";
         }
      }
   }
return true;    // All characters are numbers.
}

// Returns true if string str is English letters (A .. Z, a..z) only.
// uses flgReq to see if the field is required and fldNm for the error message
function isAlphabetic(str, flgReq, fldNm)	// isAlphabetic (STRING)
{
var idx;
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " requires a word in it. " : true;
  }
for (idx = 0; idx < str.length; idx++)
   {
   if (!isLetter(str.charAt(idx))) return "The " + fldNm + " must have letters only. ";
   }
return true;    // All characters are letters.
}

// Returns true if string is English letters (A .. Z, a..z) or numbers only
// uses flgReq to see if the field is required and fldNm for the error message
function isAlphanumeric(str, flgReq, fldNm)// isAlphanumeric (STRING)
{
var idx;
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " requires Alphanumeric data in it. " : true;
  }
for (idx = 0; idx < str.length; idx++)
   {
   if (! (isLetter(str.charAt(idx)) || isDigit(str.charAt(idx)))) return "The " + fldNm + " can only have letters or numbers in it. ";
   }
return true;    // All characters are numbers or letters.
}

// returns true if string str is an integer within the range of integer
// arguments int1 and int2. isIntegerInRange(STRING str,INTEGER int1,INTEGER int2)
function isIntegerInRange(str, int1, int2, flgReq, fldNm)
{
if (isEmpty(fldNm) == true) fldNm = "Number";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
// Catch non-integer strings to avoid creating a NaN below
var retVal = isInteger(str, true, fldNm);
if (retVal != true) return retVal;
// 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).
var num = parseInt(str, 10);
if ((num >= int1) && (num <= int2)) return true;
else return fldNm + " entered is out of the approved range. (" + int1 + " - " + int2 + ") ";
}

// Returns true if string is numbers or a - dash
// uses flgReq to see if the field is required and fldNm for the error message
function isZip(str, flgReq, fldNm)
{
var idx;
var zip_int = "0123456789-";
if (isEmpty(fldNm) == true) fldNm = "Zip Code";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " requires numbers or a dash in it. " : true;
  }
for (idx = 0; idx < str.length; idx++)
   {
   if (zip_int.indexOf(str.charAt(idx)) == -1) return "The " + fldNm + " can only have numbers or a dash in it. ";
   }
return true;    // All characters are numbers or letters or periods.
}

// Returns true if string is numbers or  - . ( ) or a space
// uses flgReq to see if the field is required and fldNm for the error message
function isPhoneNo(str, flgReq, fldNm)
{
var idx;
var arrChkLst = "0123456789-.() ";
if (isEmpty(fldNm) == true) fldNm = "Zip Code";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " requires numbers or . - ( ) in it. " : true;
  }
for (idx = 0; idx < str.length; idx++)
   {
   if (arrChkLst.indexOf(str.charAt(idx)) == -1) return "The " + fldNm + " can only have numbers or a dash in it. ";
   }
return true;    // All characters are numbers or   - . ( ) or a space.
}

// used for validation of file name to be uploaded
// Returns true if string is English letters (A .. Z, a..z) or numbers or a dot only
// uses flgReq to see if the field is required and fldNm for the error message
function isFileNm(str, flgReq, fldNm)
{
var idx;
var file_chr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._";
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " requires Alphanumeric data and a period in it. " : true;
  }
for (idx = 0; idx < str.length; idx++)
   {
   if (file_chr.indexOf(str.charAt(idx)) == -1) return "The " + fldNm + " can only have letters, numbers or periods in it. ";
   }
return true;    // All characters are numbers or letters or periods.
}

// uses flgReq to see if the field is required and fldNm for the error message
function isDate(date_text, flgReq, fldNm)
{
var month = new String();
var day = new String();
var year = new String();
var start_pos = 0;
var seen_dvdr = false;
if (isEmpty(fldNm) == true) fldNm = "date";
var dd_rng = fldNm + ": Day can only be from 1 to maximum days in the month. ";
if (isEmpty(date_text) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? fldNm + " can not be empty. Put in mm/dd/yyyy format with dividers. " : true;
  }
for (var idx = 0; idx < date_text.length; idx++)
   {
   if (date_dvdr.indexOf(date_text.charAt(idx)) != -1)
      {
      if (idx == 0) return fldNm + ": Month is required for a valid date. ";
      if (seen_dvdr)
         {day = date_text.slice(start_pos,idx);}// retrieved second
      else
         {month = date_text.slice(start_pos,idx);}// retrieved first
      start_pos = idx+1;
      seen_dvdr = true;
      }
   }
year = date_text.slice(start_pos);
if (year.length != 4) return fldNm + ": Year must be a four digit number. ";
if (isZero(year) == true) return fldNm + ": Year is required for a valid date. ";
for (var idx = 0; idx < year.length; idx++)
   {
   if ( !isDigit(year.charAt(idx))) return fldNm + ": Year can only be a numeric value. ";
   }
if ((month.length > 2) || (month.length < 1)) return fldNm + ": Month can only be one or two numbers. Like 12, 01, or 1. ";
for (var idx = 0; idx < month.length; idx++)
   {
   if ( !isDigit(month.charAt(idx))) return fldNm + ": Month can only be a numeric value. ";
   }
if ((day.length > 2) || (day.length < 1)) return fldNm + ": Day can only be one or two numbers. Like 30, 01, or 1. ";
for (var idx = 0; idx < day.length; idx++)
   {
   if ( !isDigit(day.charAt(idx))) return fldNm + ": Day can only be a numeric value. ";
   }
if (!isIntInRng (day, 1, 31)) return dd_rng; // range check
if (!isIntInRng (month, 1, 12)) return fldNm + ": Month can only be from 1 to 12. ";

var int_year = parseInt(year, 10);
var int_month = parseInt(month, 10);
var int_day = parseInt(day, 10);
if (int_day > days_mm[int_month]) return dd_rng;
if ((int_month == 2) && (int_day > daysInFebruary(int_year))) return dd_rng;
return true;
}

// uses flgReq to see if the field is required and fldNm for the error message
// must be valid dates before calling this function
function isDateGrt(frst_date_text, date_text, flgReq, fldNm1, fldNm2)
{
var start_pos = 0;
var seen_dvdr = false;
var day = new String();
var month = new String();
var year = new String();
var hold_dvdr = new String();
var pass_dt;
var cal_frst_dt;
var temp_str;
var frst_dd = new String();
var frst_mm = new String();
var frst_yy = new String();
if (isEmpty(fldNm1) == true) fldNm1 = "First Date";
if (isEmpty(fldNm2) == true) fldNm2 = "Second Date";
if (isEmpty(frst_date_text) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm1 + " can not be empty. " : true;
  }
if (isEmpty(date_text) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm2 + " can not be empty. " : true;
  }

for (var idx = 0; idx < frst_date_text.length; idx++)
   {
   if (date_dvdr.indexOf(frst_date_text.charAt(idx)) != -1)
      {
      switch(seen_dvdr)
         {
         case false:
            frst_mm = frst_date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            hold_dvdr = frst_date_text.charAt(idx);
            break;
         case true:
            frst_dd = frst_date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            break;
         }
      seen_dvdr = true;
      }
   }
frst_yy = new String(frst_date_text.slice(start_pos));
frst_mm = new String(returnInteger(frst_mm,2));
frst_dd = new String(returnInteger(frst_dd,2));
temp_str = new String(frst_yy + frst_mm + frst_dd);
cal_frst_dt = parseInt(temp_str,10);

start_pos = 0;
seen_dvdr = false;
for (var idx = 0; idx < date_text.length; idx++)
   {
   if (date_dvdr.indexOf(date_text.charAt(idx)) != -1)
      {
      switch(seen_dvdr)
         {
         case false:
            month = date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            hold_dvdr = date_text.charAt(idx);
            break;
         case true:
            day = date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            break;
         }
      seen_dvdr = true;
      }
   }
year = new String(date_text.slice(start_pos));
month = new String(returnInteger(month,2));
day = new String(returnInteger(day,2));
temp_str = new String(year + month + day);
pass_dt = parseInt(temp_str,10);
if (cal_frst_dt > pass_dt)
 {
 return fldNm2 + " must be greater than " + fldNm1 + ". ";
 }
else
 {// opional argument of amount of difference allowed
 if (arguments.length > 6 )
   {// arguments start at 0 but the length is the actual number starting at 1
   var diff_allwd_cnt = parseInt(arguments[5],10);
   var curr_diff_cnt = pass_dt - cal_frst_dt;
   // discription is an additional argument that is passed after the difference
   if (curr_diff_cnt > diff_allwd_cnt) return "The Date difference can not be greater than " + arguments[6] + ". ";
   }
 }
return true;
}

// uses flgReq to see if the field is required and fldNm for the error message
function isTime(time_text, flgReq, fldNm)
{
var start_pos = 0;
var seen_dvdr = false;
if (isEmpty(fldNm) == true) fldNm = "time";
if (isEmpty(time_text) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? fldNm + " can not be empty. Put in hh:mm format with divider. " : true;
  }
for (var idx = 0; idx < time_text.length; idx++)
   {
   if (time_text.charAt(idx) == time_dvdr)
      {
      if (idx==0) return fldNm + ": Hours are required for a valid time. ";
      hours = time_text.slice(start_pos,idx);
      start_pos = idx + 1;
      if (seen_dvdr) return fldNm + ": Must be in hh:mm format with one divider. ";
      else seen_dvdr = true;
      }
   }
if (!seen_dvdr) return fldNm + ": Minutes are required for a valid time. ";
minutes = time_text.slice(start_pos);
if (!isIntInRng (minutes, 0, 59)) return fldNm + ": Minutes must be two digits. Like 59 or 01. ";
if (!isIntInRng (hours, 0, 12)) return fldNm + ": Hours can only be one or two numbers. Like 12, 01, or 1. ";
return true;
}

// validated list of numbers with commas
// uses flgReq to see if the field is required and fldNm for the error message
function isCommaLst(str, flgReq, fldNm)
{
var comma_no_chr = "0123456789,";
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
for (var idx = 0; idx < str.length; idx++)
   {
   if (comma_no_chr.indexOf(str.charAt(idx)) == -1) return "The " + fldNm + " can only be numbers with a comma. i.e. 1,2 ";
   }
return true;    // All characters are allowed
}

// validates for an email address
// uses flgReq to see if the field is required and fldNm for the error message
function isEMail(str, flgReq, fldNm)
{
var invld_char = "/:,; \t\n\r";
var email_str = new String(str);
if (isEmpty(fldNm) == true) fldNm = "Email address";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
for (var idx = 0; idx < invld_char.length; idx++)
 {
 if (email_str.indexOf(invld_char.charAt(idx),0) > -1)
  {
  return fldNm + " must not contain invalid characters. ";
  }
 }
var at_pos = email_str.indexOf("@",1);
if (at_pos == -1)
 {
 return fldNm + " must contain one '@' symbol. ";
 } 
if (email_str.indexOf("@",at_pos+1) != -1)
 {
 return fldNm + " can not contain more than one '@' symbol. ";
 }
var period_pos = email_str.indexOf(".",at_pos);
if (period_pos == at_pos + 1)
 {
 return fldNm + " must have a character seperating the '@' and the '.'. ";
 }
if (period_pos == -1)
 {
 return fldNm + " must contain at least one '.' after the '@' symbol. ";
 }
if (period_pos + 3 > email_str.length)
 {
 return fldNm + " must be at least 2 characters long after the '.'. ";
 }
return true;
}

// validates what is allowed for line no
// uses flgReq to see if the field is required and fldNm for the error message
function isLineNo(str, flgReq, fldNm)
{
var line_chr = "0123456789,-";
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
for (var idx = 0; idx < str.length; idx++)
   {
   if (line_chr.indexOf(str.charAt(idx)) == -1) return "The " + fldNm + " can only be numbers with , or - . ";
   }
return true;    // All characters are allowed
}

// validates if it is a int with or without * at a lenght of lgth_ch
// uses flgReq to see if the field is required and fldNm for the error message
function isWildInt(str,lgth_ch, flgReq, fldNm)
{
var wild_int = "0123456789*";
var strInt = new String(str);
var calc_lgth = strInt.length;
if (isEmpty(fldNm) == true) fldNm = "field";
if (isEmpty(str) == true)
  {// if required true then send error message else value ok send true
  return flgReq == true ? "The " + fldNm + " can not be empty. " : true;
  }
for (var idx = 0; idx < strInt.length; idx++)
   {
   if (wild_int.indexOf(strInt.charAt(idx)) == -1)
    {
    return "The " + fldNm + " must be in a number/asterisk format. ";
    }
   else
    {
    if (strInt.charAt(idx) == astrk)
     {
     if (idx == (strInt.length-1)) calc_lgth = lgth_ch;
     else return "The " + fldNm + " can not have and asterisk in the middle of it. ";
     }
    }
   }
if (calc_lgth != lgth_ch) return "The " + fldNm + " length is not long enough. ";
return true;    // All characters are numbers or asterisk.
}


// ************************************************************
// format functions
// ************************************************************

// replaces brackets in the string and return the string
function replaceBrackets(strText)
{
var strTemp = new String(strText);
strTemp = strTemp.replace(/</g,"&lt;");
strTemp = strTemp.replace(/>/g,"&gt;");
return strTemp;
}

// returns a float number with the appropiate number of zeros of the remainder_num
function returnFloat(var_str,remainder_num)
{
var str_char;
var start_remainder = 0;
var seen_dec = false;
var returning_str = new String(var_str);
var decimal_pstn = Number(remainder_num) + 1;
var remainder_str = returning_str.slice(-decimal_pstn);

for (var idx_loop = 0; idx_loop < remainder_str.length; idx_loop++)
   {
   if (seen_dec)
      {
      start_remainder = start_remainder + 1;
      }
   str_char = remainder_str.charAt(idx_loop);
   if (str_char == dec_pnt)
      {
      seen_dec=true;
      }
   }
if (seen_dec != true)
   {
   returning_str = returning_str.concat('.');
   }
for (var loop_num = start_remainder; loop_num < remainder_num; loop_num++)
   {
   returning_str = returning_str.concat('0');
   }
return returning_str;
}

// returns a number with specific leading zeros if any need added
function returnInteger(var_str,lead_num)
{
var str_char;
var num_pres = 0;
var seen_dec = false;
var returning_str = new String(var_str);
var beg_str = new String();

for (var idx_loop = 0; idx_loop < returning_str.length; idx_loop++)
   {
   var str_char = returning_str.charAt(idx_loop);
   if (str_char == dec_pnt)
      {
      seen_dec = true;
      }
   else
      {
      if (!seen_dec)
         {
         num_pres++;
         }
      }
   }
for (var loop_num = num_pres; loop_num < lead_num; loop_num++)
   {
   beg_str = beg_str.concat('0');
   }
if (num_pres < lead_num)
   {
   returning_str = beg_str.concat(returning_str);
   }
return returning_str;
}

// returns a date like 04/04/1999
function returnDate(date_text)
{
var start_pos = 0;
var seen_dvdr = false;
var day = new String();
var month = new String();
var year = new String();
var hold_dvdr = new String();
for (var idx = 0; idx < date_text.length; idx++)
   {
   if (date_dvdr.indexOf(date_text.charAt(idx)) != -1)
      {
      switch(seen_dvdr)
         {
         case false:
            month = date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            hold_dvdr = date_text.charAt(idx);
            break;
         case true:
            day = date_text.slice(start_pos,idx);
            start_pos = idx + 1;
            break;
         }
      seen_dvdr = true;
      }
   }
year = date_text.slice(start_pos);
month = returnInteger(month,2);
day = returnInteger(day,2);
//return (month+hold_dvdr+day+hold_dvdr+year);
return (month + "/" + day + "/" + year);//use sql server divider
}

// returns month year format 05/99
function returnMMYY(mmyy_text)
{
var start_pos = 0;
var seen_dvdr = false;
var str = new String(mmyy_text);
var month = new String();
var year = new String();
var hold_dvdr = new String();
for (idx = 0; idx < str.length; idx++)
 {   
 if ((date_dvdr.indexOf(str.charAt(idx)) != -1) && !seen_dvdr)
  {
  seen_dvdr = true;
  hold_dvdr = str.charAt(idx);
  }
 if (!seen_dvdr)
  {start_pos++;}
 }
year = str.slice(start_pos+1);
month = str.slice(0,start_pos);
month = returnInteger(month,2);
return (month + hold_dvdr + year);
}

// returns time format 01:05
function returnTime(time_text)
{
var start_pos = 0;
var seen_dvdr = false;
var hours = new String();
var minutes = new String();
for (var idx = 0; idx < time_text.length; idx++)
   {
   var chr = time_text.charAt(idx);
   if (chr == time_dvdr)
      {
      hours = time_text.slice(start_pos,idx);
      start_pos=idx + 1;
      }
   }
minutes = time_text.slice(start_pos);
minutes = returnInteger(minutes,2);
hours = returnInteger(hours,2);
return (hours + ':' + minutes);
}

// puts spaces at the end of the passed string
function padSpaces(var_str,trail_num)
{
var str_char;
var num_pres = 0;
var returning_str = new String(var_str);
var beg_str = new String();

for (var idx_loop = 0; idx_loop < returning_str.length; idx_loop++)
   {
   num_pres++;
   }
for (var loop_num = num_pres; loop_num < trail_num; loop_num++)
   {
   beg_str = beg_str.concat(' ');
   }
if (num_pres < trail_num)
   {
   returning_str = returning_str.concat(beg_str);
   }
return returning_str;
}

// Removes initial (leading) whitespace characters from str.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripBegSpace(str)
{
var idx = 0;
while ((idx < str.length) && (white_spc.indexOf(str.charAt(idx)) != -1))
   {idx++;}
return str.substring(idx, str.length);
}

// Removes endding whitespace characters from str.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripEndSpace(str)
{
var idx = str.length - 1;
while ((idx >= 0) && (white_spc.indexOf(str.charAt(idx)) != -1))
   {idx--;}
return str.substring(0, idx + 1);
}

// returns a string 
function returnWildInt(str,lgth_ch,cnvt)
{
var idx;
var strInt = new String(str);
var beg_str;
if (strInt.indexOf(astrk) != -1)
	{
	beg_str = new String(strInt.slice(0,-1));
	if (cnvt == "min")
	  {
	  for (var loop_num = beg_str.length; loop_num < lgth_ch; loop_num++)
	   {
	   beg_str = beg_str.concat('0');
	   }
	  }
	if (cnvt=="max")
	  {
	  for (var loop_num = beg_str.length; loop_num < lgth_ch; loop_num++)
	   {
	   beg_str = beg_str.concat('9');
	   }
	  }
    }
return beg_str;
}

// ********************************************************************************************
// common functions are below 
// ********************************************************************************************

// does mathmatical stament according to what was passed
// in the actn_prfm the action to perform 
// for two numbers specificaly to compinsate for floats
// which can give incorrect calculations with mult and division
function calcFltInt(num1,actn_prfm,num2)
{
var hold_int1 = new String(num1);
var hold_int2 = new String(num2);
var hold_exp1 = expLgth(num1);
var hold_exp2 = expLgth(num2);
var hold_fld = 1;
var hold_str = new String();
var dec_pos;
if (hold_exp1 < hold_exp2)
 {
 hold_fld = hold_exp1;
 hold_exp1 = hold_exp2;
 hold_exp2 = hold_fld;
 hold_fld = 2;
 }
for (var loop_num = hold_exp2; loop_num < hold_exp1; loop_num++)
 {
 hold_str = hold_str.concat('0');
 }
if (hold_fld == 2)
 {
 hold_int1 = hold_int1.concat(hold_str);
 }
else
 {
 hold_int2 = hold_int2.concat(hold_str);
 }
dec_pos = hold_int1.indexOf(dec_pnt);
if (dec_pos != -1)
 {
 hold_int1 = hold_int1.slice(0,dec_pos) + hold_int1.slice(dec_pos+1);
 }
dec_pos = hold_int2.indexOf(dec_pnt);
if (dec_pos != -1)
 {
hold_int2 = hold_int2.slice(0,dec_pos) + hold_int2.slice(dec_pos+1);
 }
eval('hold_fld=' + hold_int1 + actn_prfm + hold_int2);
hold_str = new String(hold_fld);
hold_fld = new String();
if (hold_str.length < hold_exp1)
 {
 for (var loop_num = 0; loop_num + hold_str.length < hold_exp1; loop_num++)
  {
  hold_fld = hold_fld.concat('0');
  }
 hold_str = hold_fld.concat(hold_str);
 }
hold_fld = hold_str.slice(0,-hold_exp1).length;
return hold_str.slice(0,-hold_exp1) + '.' + hold_str.slice(hold_fld);
}

// opens a new window
function openwindow(url,name,height,width,toolbar,location,status,menubar,scrollbars)
{
var features = '';
var top = 0;
var left = 0;
if ( height < screen.availHeight )
	{top = Math.round( ( screen.availHeight - height ) / 2 );}
else
    {
    top = 0;
    height = screen.availHeight - 5;
    }
if ( width < screen.availWidth )
	{left = Math.round( ( screen.availWidth - width ) / 2 );}
else
    {
    left = 0;
    width = screen.availWidth - 5;
    }
features = 'height=' + height + ',width=' + width;
features += ',screenX=' + left +  ',screenY=' + top;//try both
features += ',left=' + left + ',top=' + top;//try both
features += ',resizable=1,menubar=' + menubar;
features += ',status=' + status + ',toolbar=' + toolbar;
features += ',location=' + location + ',scrollbars=' + scrollbars;	

return open(url,name,features);
//window.open(age1.htm","directories=yes,location=yes,menubar=yes,resizable=yes,status=yes,toolbar=yes,scrollbars=yes,width=400,height=300"); 
}

// function to set the statusbar so you dont see the javascript calls
function StBar(msg)
{
defaultStatus = msg;
return true;
}

//sets up an object for quick reference to an element mainly used for older Netscape 
function objectSetUp(obj_loc,div_name,ele_name)
{
if (navigator.appName == "Netscape")
   {return obj_loc.layers[div_name].document.forms[0].elements[ele_name];}
else if (navigator.appName.indexOf("Microsoft") != -1)
   {return obj_loc.all[ele_name];}
else
   {return 'error';}
}

//transfers the options from one select box to the other
//the options must have values assigned to them inaddition to the text
function TransOpt(SelFrm,SelTo,PassFrmNm)
{
var frmDoc = this.document.forms[PassFrmNm];
if (frmDoc.elements[SelFrm].selectedIndex == -1 )
	{
	alert("Please Select a Column to Add.");
	return;
	}
// define after the if so it will not crash on empty lists
var toStartPos = frmDoc.elements[SelTo].options.length;
var frmStartPos = frmDoc.elements[SelFrm].options.length -1;
var hldValArr = new Array(frmStartPos);
var hldTxtArr = new Array(frmStartPos);
var opt_val = "";
var opt_txt = "";
var optGroup;
	
for (var idx = 0; idx < frmDoc.elements[SelFrm].length; idx++)
	{
	opt_val = frmDoc.elements[SelFrm].options[idx].value;
	opt_txt = frmDoc.elements[SelFrm].options[idx].text;
	hldValArr[idx] = opt_val;
	hldTxtArr[idx] = opt_txt;
	if (frmDoc.elements[SelFrm].options[idx].selected == true)
		{
		optGroup = new Option(opt_txt, opt_val);
		frmDoc.elements[SelTo].options[toStartPos] = optGroup;
		hldValArr[idx] = "";
		hldTxtArr[idx] = "";
		frmDoc.elements[SelFrm].options[idx].selected = false;
		toStartPos++;
		}
	}
var idxOpt = 0;
for (var idx = 0; idx < frmDoc.elements[SelFrm].length; idx++)
	{
	if (hldValArr[idx] != "")
		{
		frmDoc.elements[SelFrm].options[idxOpt].text  = hldTxtArr[idx];
		frmDoc.elements[SelFrm].options[idxOpt].value = hldValArr[idx];
		idxOpt++;
		}
	}
frmDoc.elements[SelFrm].options.length = idxOpt;
}

//auto selects / highlight all options in the select box that is passed
function SelAll(SelNm,PassFrmNm)
{
var frmDoc = this.document.forms[PassFrmNm];
for (var idx = 0; idx < frmDoc.elements[SelNm].length; idx++)
	{
	frmDoc.elements[SelNm].options[idx].selected = true;
	}
}

// gets all the options in the select list box and concatenates the
// values together with a pipe divider and returns the string 
function InCodeLst(LstNm,PassFrmNm)
{
var slct_nm = document.forms[PassFrmNm].elements[LstNm];
var max_ids = slct_nm.options.length;
var hld_ids = "";
var seen_flg = false;
for (var idx = 0; idx < max_ids; idx++)
	{
	if (seen_flg) hld_ids += "|";
	hld_ids += slct_nm.options[idx].value;
	seen_flg = true;
	}
return hld_ids;
}

// ********************************************************************************************
// functions specific for the application this file is attacked to 
// ********************************************************************************************


function bttnPrntVrsnInv()
{
var HldVal;
var NewWnd = openwindow("","NewWin",500,800,1,0,1,1,1)
//var NewWnd = openwindow("InvRpt.aspx","NewWin",400,400,1,1,1,1,1)
 //curLoc.submit();
NewWnd.document.open();
NewWnd.document.writeln('<html>');
NewWnd.document.writeln('<script type="text/javascript">');
NewWnd.document.writeln('function fnctLoad() {');
NewWnd.document.writeln('this.document.forms[0].submit();');
NewWnd.document.writeln('} </scr'+'ipt>');
NewWnd.document.writeln('<head><title>Print Window</title></head>');
NewWnd.document.writeln('<body onload="JavaScript:fnctLoad();">');
NewWnd.document.writeln('<form action="InvRpt.aspx" method="post" id="FrmMain">');
NewWnd.document.write('<input name="LstItem" type="hidden" value="');
HldVal = curLoc.LstItem.options[curLoc.LstItem.selectedIndex].value;
NewWnd.document.write(HldVal);
NewWnd.document.writeln('" />');
NewWnd.document.write('<input name="TxtLstItem" type="hidden" value="');
HldVal = curLoc.LstItem.options[curLoc.LstItem.selectedIndex].text;
NewWnd.document.write(HldVal);
NewWnd.document.writeln('" />');
NewWnd.document.write('<input name="LstDptn" type="hidden" value="');
HldVal = curLoc.LstDptn.options[curLoc.LstDptn.selectedIndex].value;
NewWnd.document.write(HldVal);
NewWnd.document.writeln('" />');
NewWnd.document.write('<input name="LstSort" type="hidden" value="');
HldVal = curLoc.LstSort.options[curLoc.LstSort.selectedIndex].value;
NewWnd.document.write(HldVal);
NewWnd.document.writeln('" />');
NewWnd.document.write('</form></body></html>');
NewWnd.document.close();
NewWnd.focus();
}