// PKJA-LIB.JS         A library of useful JavaScript functions

// Copyright (c) Circle Software, Philip Karras 2000 - 2006, 01/20/2006

// Our soul waits for the Lord, he is our help and shield. Psalm 33:20

//alert("In lib.js");

// Greeting()    - uses alert
// Message()     - uses alert
// Error()       - uses alert
// SwapStr()     - swaps a multi-part string's parts & sends it back
// ChkChar()     - Checks a string for a char
// AddHead()     - Prints a string as a "level" html <h> header
// IsNum()       - Checks an input to see if it IS a number
// MailMe()      - Starts the e-mail <a href> tag You need the link text, 3 inputs
// MailMe01()    - Starts the e-mail <a href> tag You need the link text, 2 inputs
// SendMail()    - Builds an entire e-mail html page to send email to
// MailTxt()     - Puts together two strings into an email address
// E_Mail()      - Builds & writes the e-mail <a href="..."> tag
// swapImages    - Swaps two images so we can do a roll-over
// RollMeOver    - On Mouse Over displays the txt in status bar, & myimage at...
// RollMeOver2   - Same as RollMeOver expect build an applet code link not an image
// ClearMe()     - On Mouse Over clears status bar, & puts myimage at the lnk pos
// Money()       - Converts a numeric to a money string
// Round()       - Converts a numeric to a rounded number string
// Commas()      - Converts a number into a comma-ed number-string
// ObjSortName   - Sorts two Objects based on the values of a text field
// ObjSortNumb   - Sorts two Objects based on the values of a numeric field
// NumberSort    - Sorts two numbers based on their values.
// PassWord()    - Generates the user's new password
// MonthNumb()   - Converts a month in text to a month number
// Is18()        - Checks to see if the dob entered indicates
// DeBug()       - for infinite & fast loops
// DeBugW()      - Displays large text as a new web-page
// TrimStn()     - Removes all leading & trailing Chrs on the passed-in string.
// TrimFrontEnd  - Removes all leading Chrs or spaces on the passed-in string.
// TrimBackEnd   - Removes all trailing Chrs or spaces on the passed-in string.
// Trim()        - Removes all leading & trailing spaces or spaces from the string
// ReplaceIt()   - Used to replace things like &gt; and &lt; with > and <
// GetLastUpDate - This function correctly gets last update date for IE & NS
// 
// 
// RemoveChr     - Remove all Chr from Stn
// BuildIt()     - to build e-mail address link
// BuildIt2()    - displays the e-mail address
// ChkPassWrd()  - Asks for & checks the prompt entered password

// -------------------------------------------------------------------
// This function displays an alert message
// inputs: a name
// output: Displays the Greeting message and name in an alert window
// Action: Waits for a click of the mouse
// return: nothing
// -------------------------------------------------------------------
function Greeting(who) {
   alert ("Greetings there, " + who);
}


// -------------------------------------------------------------------
// This function displays an alert message
// inputs: the message
// output: Displays the message in an alert window
// Action: Waits for a click of the mouse
// return: nothing
// -------------------------------------------------------------------
function Message(msg) {
   alert (msg);
}


// -------------------------------------------------------------------
// This function displays an alert ERROR * message * ERROR
// inputs: the message
// output: Displays the message in an alert window
// Action: Waits for a click of the mouse
// return: nothing
// -------------------------------------------------------------------
function Error(msg) {
   alert ("ERROR * "+msg+" * ERROR");
}


// -------------------------------------------------------------------
// This function swaps a multi-part string's parts & sends it back
// inputs: a multi part string
// output: nothing
// Action: none
// return: a reverse order multi part string
// -------------------------------------------------------------------
function SwapStr(instr) {
   var outstr;
   var tmpstr;
   var partstr;
   var mmax, max, i;

   partstr = instr.split(" "); // Split string apart on a space
   max = partstr.length; // Get the # of parts
   mmax = max/2;         // Get the # of swaps
   for(i=0; i<mmax; ++i) { // actual swapping code
      tmpstr = partstr[i];           // Put lowest part in tmp
      partstr[i] = partstr[max-i-1]; // put highest part in lowest part
      partstr[max-i-1] = tmpstr;     // put lowest part in highest part
   }
   outstr = partstr.join(" "); // put the reversed string back together

   return (outstr);
}


// -------------------------------------------------------------------
// This function Checks a string for a char
// inputs: a string and a char
// output: nothing
// Action: none
// return: (0/1) = Char (Not found / Found) in string
// -------------------------------------------------------------------
function ChkChar(instr, inchar) {

   Rtn = 0;
   atPos = instr.indexOf(inchar, 1);
   if(atPos > 0)
      Rtn = 1;

   return Rtn;
}


// -------------------------------------------------------------------
// This function Prints a string as a "level" html <h> headder
// inputs: Title, level of headder, "c" == to center it.
// output: none or can return html header as a string
// Action: Prints the headder or none
//
// If we use the printing of the headder string the call must be:
// Call: AddHead("Title Goes Here", 1, "c");
//
// If we use the return of the headder string the call must be:
// Call: document.write(AddHead("Title Goes Here", 1, "c"));
// -------------------------------------------------------------------
function AddHead(text, level, cnt) {
   var c1 = "", c2 = "";

   if(cnt == "c") {
      c1 = "<center>";
      c2 = "</center>";
   }
   html = "H" + level;
   start = c1 + "<" + html + ">";
   stop = c2 + "</" + html + ">";

   document.write(start + text + stop); // call: AddHead("...)

   return; // (start+text+stop); // Call: doc.write(AddHead("...))
}


// -------------------------------------------------------------------
// This function Checks an input to see if it IS a number
// inputs: a string
// output: nothing
// Action: none
// return: (0/1) = (Not a number / Is a number)
// -------------------------------------------------------------------
function IsNum(Vnum) {
   return (!isNaN(Vnum));
}


// -------------------------------------------------------------------
// This function Starts the e-mail <a href> tag You need the link text
// or graphic, and the ending </a>
//
// inputs: two email address strings, and email subject
// output: nothing
// Action: none
// return: complete email html line
// -------------------------------------------------------------------
function MailMe(add1, add2, subj) {
   var atsign = "@";
   var dotcom = ".com";
   var subject = "";
   if(subj.length > 0) {
      subject = "?subject=";
      subject += subj;
   }
   
   var Mail2 = "mailto:";//HA@HA.HA.nothing.here.com!
   var Ovr = " onMouseOver=\"window.status='Send an Email: " + subj + "'; return true;\"";
   var Out = " onMouseOut=\"window.status=''\"";
   return(document.write("<a href=\"",Mail2+add1+atsign+add2+dotcom+subject+"\""+Ovr+Out, ">"));
}


function MailMe01(add1, add2) {
   var atsign = "@";
   var dotcom = ".com";

   var Mail2 = "mailto:";//HA@HA.HA.nothing.here.com!
   var Ovr = " onMouseOver=\"window.status='Send an Email: " + "'; return true;\"";
   var Out = " onMouseOut=\"window.status=''\"";
   return(document.write("<a href=\"",Mail2+add1+atsign+add2+dotcom+"\""+Ovr+Out, ">"));
}


// -------------------------------------------------------------------
// This function Builds an entire e-mail html page to send email to
// the address about the subject. This was a test to see if I could 
// send email even if Yahoo were down. It works!
// inputs: email address string, and email subject
// output: You get moved to the email html-page, click on [Click Here]
//         to get the email fill-in form
// Action: none
// return: nothing
// -------------------------------------------------------------------
function SendMail(add, subj) {
   var subject = "";
   var Page = "<html><body>";  // The complete email web-page!
   if(subj.length > 0) {
      subject = "?subject=";
      subject += subj;
   }
   var Ovr = " onMouseOver=\"window.status='To send an Email to: " + add + " about: " + subj + "'; return true;\"";
   var Out = " onMouseOut=\"window.status=''\"";
   Page += "<br><br><center><font face='arial'><h3>To send email to: ";
   Page += add+"</h3></font><br>";
   Page += "About: "+subj+" - ";
   Page += "<a href=\"mailto:"+add+subject+"\""+Ovr+Out+">";
   Page += "[Click Here]</a>  </center>  <br></body></html>";

   document.write(Page);
}


// -------------------------------------------------------------------
// This function puts together two strings into an email address
// inputs: two email address strings
// output: nothing
// Action: none
// return: complete email html line
// -------------------------------------------------------------------
function MailTxt(add1, add2) {
   var atsign = "@";
   var dotcom = ".com";
   return(document.write(add1+atsign+add2+dotcom));
}


// -------------------------------------------------------------------
// This function builds & writes the e-mail <a href="..."> tag
// inputs: email 1, 2, & 3 as in: email1 @ email2 . email3
// inputs: the two status message strings, in and out
// inputs: the subject string
// output: nothing
// Action: none
// return: complete email "<a href=...>" html line
// -------------------------------------------------------------------
function E_Mail(email1, email2, email3, statMsgIn, statMsgOut, subject) {
   var Mail2 = "\"mailto:";//HA@HA.HA.nothing.here.com!
   Mail2  += email1+"@"+email2+"."+email3;
   Mail2  += "?subject=" + subject + "\"";
   var Ovr = " onMouseOver=\"window.status='";
   Ovr    += statMsgIn + "'; return true;\"";
   var Out = " onMouseOut=\"window.status='";
   Out    += statMsgOut + "'\"";
   // window.alert("<a href="+Mail2+Ovr+Out+">");
   return(document.write("<a href=",Mail2+Ovr+Out,">"));
}


// -------------------------------------------------------------------
// This function swaps two images so we can do a roll-over
// inputs: two image file names
// output: none
// Action: New image put in place of image being used
// return: none
// -------------------------------------------------------------------
function swapImages(ImageName, NewImage) {

   // test to see if the browser understands rollover
   // If it does swap one image for the other
   if (document.images) {
      document[ImageName].src = NewImage;
   }
}


// -------------------------------------------------------------------
// On Mouse Over we display the txt in the status bar, and myimage at
// the lnk position.
// inputs: txt, lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function RollMeOver(txt, lnk, myimage) {
   window.status=txt;
   swapImages(lnk, myimage);
//   return true;  // is this needed? Seems to work without it.
}

// What does this do?
// -------------------------------------------------------------------
// On Mouse Over we display the txt in the status bar, and myimage at
// the lnk position.
// inputs: txt, lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function RollMeOver2(txt, lnk, width, height) {
   window.status=txt;
   // swapImages(lnk, myimage);
   var message = "<applet code=";
   message    += "\""+lnk+"\"" + " width="+width;
   message    += " height="+height+"></applet>";

   document.write(message); // Makes the different window

//   return true;  // is this needed? Seems to work without it.
}


// -------------------------------------------------------------------
// On Mouse Over we clear the status bar, and put myimage at
// the lnk position.
// inputs: lnk, image
// output: none
// Action: calls swapImages to place the sent image at the lnk position
// return: none
// -------------------------------------------------------------------
function ClearMe(lnk, myimage) {
   //window.status="";
   swapImages(lnk, myimage);
}


// -------------------------------------------------------------------
// Converts a numeric to a money string
// Input:  A numeric that's supposed to be money, 2 decimal places
// Return: A Money numeric-string, rounded-up & truncated to 2 places
// -------------------------------------------------------------------
function Money(amount) {
  var a = amount * 100;
  a = Math.ceil(a);
  a /= 100;
  var b = parseInt(a);
  if (a == b) { // If it IS an int then add the decimal point
    a += ".";
  }
  a += "00";  // converts it to a string with exta 0's
  var where = a.indexOf(".");
  var b = a.substring(0, where+3);
  return(b); // returns a STRING in money format
}


// -------------------------------------------------------------------
// Converts a numeric to a rounded number string
// Input: number to be rounded, and the number of places to round to
// Return: A rounded numeric-string, truncated to "places" places
// -------------------------------------------------------------------
function Round(amount, places) {
  var tens = 1;       // no tens yet
  var money = 0;
  if(places == "$") { // If it is money 
     money = 1;       // set the money flag
     places = 2;      // set decimal places to 2
  }
  for(j=0; j<places; ++j) { // Build the correct multiplier/divider
     tens *= 10;
  }
  var a = amount * tens; // adjust number to correct int value for rounding

  if(!money) { // If not money use standard rounding
     a = Math.round(a);
  }
  else {       // otherwise use ceiling function (ALWAYS round up)
     a = Math.ceil(a);
  }
  a /= tens;   // adjust the rounded number to correct decimal places

  var b = parseInt(a); // Get just the integer portion on the number
  if (a == b) { // If it IS an int then add the decimal point
    a += ".";
  }

  for(j=0; j<places; ++j) { // Add correct number of extra 0's
     a += "0";  // converts it to a string with exta 0's
  }

  var where = a.indexOf("."); // find where the "." is and then 
  var b = a.substring(0, where+places+1); // get correct size sub string
  return(b);                  // & return the corrected rounded number-string
}



// -------------------------------------------------------------------
// This function Converts a number into a comma-ed number-string
// inputs: a number
// output: a string of the number with correctly placed commas, if needed
// return: the number-string
/*
Thanks for the great script, Phil. This is one of those things that 
seems like it should be really simple, but isn't.
--------------------
Michael Moncur (mgm) 
Owner and Maintainer, The JavaScript Workshop
09/25/2001
*/
// -------------------------------------------------------------------
  function Commas(num1) {
    var num = new String(num1+""); // make a string of that number
    var end = "";
    point = num.indexOf("."); // Find decimal point, if there is one
    if(point > -1) {
       end = num.substring(point, num.length); // get decimal part
       num = num.substring(0, point);              // get integer part
    }

    var commas = num.length/3; // No of commas needed, sort of
    var ints = parseInt(commas); // Just the integer part

    var left = 3*(commas - ints); // If this is zero we need one less comma
    var nxt = 0;

    if(!left) {
       --commas;
       nxt = 3;
    }
    else {
      nxt = Math.round(left);
    }

    var Snumber = num.substring(0, nxt);

    var last = nxt;
    nxt += 3;
    if(nxt <= num.length)
       Snumber += ",";

    for(j=0; j<commas; ++j) { // add remaining commas
       if(nxt <= num.length) {
          Snumber += num.substring(last, nxt);
       }

       last = nxt;
       nxt += 3;
       if(nxt <= num.length)
          Snumber += ",";
    }

    //document.write("Done - Comma-ed number: "+Snumber+"<br><br>");
    if(point) {
       Snumber += end;
    }
    return(Snumber);
  }


// -------------------------------------------------------------------
// This function Sorts two Objects based on the values of a text field
// called: name.   This function should be called 
// as in: SortObj = ArrayObj.sort(ObjSortName);
// inputs: Two Objects with a text filed called: name
// output: nothing.
// return: integer -1, 0, 1 = Arg1 is sorted: first, same, last
// -------------------------------------------------------------------
function ObjSortName(Arg1, Arg2) {
  var tmp = new Array(2); // Unsorted names
  var srt = new Array(2); // Sorted Names

  tmp[0] = Arg1.name; // Load unsorted names array
  tmp[1] = Arg2.name;
  srt = tmp.sort();   // Get the sorted names array

  // Now find out HOW they were sorted.
  var Rtn = 0; // Returned if Arg1 == Arg2, actually never returned
  if(srt[0] == Arg1.name) {
     Rtn = -1; // Returned if Arg1 sorted before Arg2
  }
  else if(srt[0] == Arg2.name) {
     Rtn = 1;  // Returned if Arg2 sorted before Arg1
  }

  return(Rtn);
}


// -------------------------------------------------------------------
// This function Sorts two Objects based on the values of a numeric
// field called: numb.   This function should be called 
// as in: SortObj = ArrayObj.sort(ObjSortNumb);
// inputs: Two Objects with a text filed called: name
// output: nothing.
// return: integer -1, 0, 1 ==  if Arg1.numb < = > Arg2.numb
// -------------------------------------------------------------------
function ObjSortNumb(Arg1, Arg2) {

  // Now find out HOW they are rated.
  var Rtn = 0; // Returned if Arg1 == Arg2

  // The multiplication by 1 forces the Arg#.numb values to be
  // numbers just in case they aren't.
  if((1*Arg1.numb) < (1*Arg2.numb)) {
     Rtn = -1; // Returned if Arg1.numb sorted before Arg2.numb
  }
  else if((1*Arg2.numb) < (1*Arg1.numb)) {
     Rtn = 1;  // Returned if Arg2.numb sorted before Arg1.numb
  }

  return(Rtn);
}


// -------------------------------------------------------------------
// This function Sorts two numbers based on their values.  This 
// function is called like: Sorted = NumbArray.sort(NumberSort);
// inputs: Two Numbers in an array of numbers
// output: nothing.
// return: integer -1, 0, 1 = Arg1 if Arg1 < = > Arg2
// -------------------------------------------------------------------
function NumberSort(Arg1, Arg2) {

  // Now find out HOW they are rated.
  var Rtn = 0; // Returned if Arg1 == Arg2

  // The multiplication by 1 forces the Arg values to be a number
  // just in case it isn't.
  if((1*Arg1) < (1*Arg2)) {
     Rtn = -1; // Returned if Arg1 sorted before Arg2
  }
  else if((1*Arg2) < (1*Arg1)) {
     Rtn = 1;  // Returned if Arg2 sorted before Arg1
  }

  return(Rtn);
}


// -------------------------------------------------------------------
// Generates the user's new password
// inputs: none
// output: nothing
// Action: none
// return: New password, six characters
// -------------------------------------------------------------------
function PassWord() {
  var PassWord = "";
  var Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDE";
  var tmp1 = 0

  for(j=0; j<3; ++j) {
    tmp1 = Math.random();
    tmp1 *= 100;
    if (tmp1 > 51)
       tmp1 -= 51;
    PassWord += Alpha.substring(tmp1, tmp1+1);

    tmp1 -= Math.floor(tmp1);

    PassWord += Math.floor(tmp1*10);
  }
  // alert(PassWord);
  return(PassWord);
}


// -------------------------------------------------------------------
// This function converts a month in text to a month number
// inputs: the month in three letter text, first letter is upper case
// output: none
// Action: none
// return: corresponding month number (Jan = 1, ..., Dec = 12)
// -------------------------------------------------------------------
function MonthNumb(Month) {
  var Months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  for(j=0; j<12; ++j) {
    if(Month == Months[j]) { // Got a match!
       var MN = j + 1;       // Give the number.
    }
  }
  return (MN); // Return the month number.
}


// -----------------------------------------------------------
// This function checks to see if the dob entered indicates
// that the user is 18 years old or older.
// input:  user's dob in mm/dd/yyyy format (no checking)
// output: none
// Action: none
// return: true/false is the user is/in-not 18 or older
// more:   This function uses function: MonthNumb()
// -----------------------------------------------------------
function Is18() {
   var Rtn = false; // No he/she is NOT 18
   var Today = Date(); // math operations string
   var Each = Today.split(" "); // 1:Month, 2:day, 4:Year

   var dob = document.DOBform.dob.value
   var dobs = dob.split("/"); // 0:mm, 1:dd, 2:yyyy
   var Ydiff = Each[4] - dobs[2];
   var Mdiff = MonthNumb(Each[1]) - dobs[0];

   if(Mdiff < 0) {
      --Ydiff;
      Mdiff += 12;
   }

   var Ddiff = Each[2] - dobs[1];
   if(Ddiff < 0) {
      --Mdiff;
   }

   // --------------------------------------------
   // Now check for 18 years old or older
   // --------------------------------------------
   if(Ydiff >= 19) { // No need to check any more!
     Rtn = true;
   }
   else if (Ydiff == 18 && Mdiff > 0) { // 18 years, check Month
     Rtn = true; // If month > 0 diff then we're done, 18ys x months
   }
   else if(Ydiff == 18 && Mdiff == 0 && Ddiff >= 0) { // If user 18ys 0m
     Rtn = true; // Got to check days, if days-diff >=0 then still 18!
   }
   // --------------------------------------------

//alert("This dob is 18 years old or older:   "+Rtn);
   return Rtn;
}


// ----------------------------------------------------------
// Displays a message & allows an exit from this code
// inputs:  Message text to display
// outputs: Displays the message & wait for key inputs
// actions: Returns to this code, or to previous html page
// returns: none
// ----------------------------------------------------------
function DeBug(txt) {
  A=prompt(txt, "Continue/Exit == Enter/X-key.");
  if(A=="X" || A=="x") { // Go back to the LAST safe page!
    javascript:history.go(-1);
    //javascript:history.G0(-1); // May be needed to cause an ERROR
  }
}


// ----------------------------------------------------------
// Displays large text as a new web-page
// inputs:  Message text to be displayed
// outputs: Displays the text & waits to be close [X]
// actions: Returns to this code, or to previous html page
// returns: none
// ----------------------------------------------------------
function DeBugW(txt) {
   tmpw = window.open("", "tmp");
   tmpw.document.write(txt);
   tmpw.document.close();
}



// ---------------------------------------------------------
// Removes all leading & trailing Chrs on the passed-in string.
//
// inputs:  The string to clean up: Stn & the Chr to remove
// Outputs: none (alert or DeBug messages as needed)
// returns: The cleaned-up string.
// ---------------------------------------------------------
function TrimStn(Stn, Chr) {
   return TrimBackEnd(TrimFrontEnd(Stn, Chr));
}


// -----------------------------------------------------------
// Remove all leading Chrs or spaces on the passed-in string.
//
// inputs:  The string to clean up: Stn & the Chr to remove
//          If Chr is null then assume use of Chr = space.
// Outputs: none (alert or DeBug messages as needed)
// returns: The cleaned-up string.
// -----------------------------------------------------------
function TrimFrontEnd(Stn, Chr) {
   var OldStn = Stn;
   var Slen = Stn.length;
   if(Chr == null) // If no Chr given, remove spaces
      Chr = ' ';
   var First = Stn.indexOf(Chr);

   while (First == 0) {
      Stn = Stn.substring(1, Slen);
      Slen = Stn.length;
      First = Stn.indexOf(Chr);
   }

   return Stn; // return the cleaned-up string
}

// -----------------------------------------------------------
// Remove all trailing Chrs or spaces on the passed-in string.
//
// inputs:  The string to clean up: Stn & the Chr to remove
//          If Chr is null then assume use of Chr = space.
// Outputs: none (alert or DeBug messages as needed)
// returns: The cleaned-up string.
// -----------------------------------------------------------
function TrimBackEnd(Stn, Chr) {
   var OldStn = Stn;
   var Slen = Stn.length;
   if(Chr == null) // If no Chr given, remove spaces
      Chr = ' ';
   var Last = Stn.lastIndexOf(Chr);

   while (Slen-1 == Last) {
      Stn = Stn.substring(0, Slen-1);
      Slen = Stn.length;
      Last = Stn.lastIndexOf(Chr);
   }

   return Stn; // return the cleaned-up string
}



// -----------------------------------------------------------
// Remove all leading & trailing spaces or spaces from the 
// passed-in string.
//
// inputs:  The string to clean up.
// Outputs: none (alert or DeBug messages as needed)
// returns: The cleaned-up string.
// -----------------------------------------------------------
function Trim(s_value) {
   var o_re;

   //right trim
   o_re    = /\s*$/;
   s_value = s_value.replace(o_re, "");

   //left trim   
   o_re    =/^\s*/;
   s_value = s_value.replace(o_re, "");
   //alert(s_value);
   return(s_value);
}


// ------------------------------------------------------
// To replace things like &gt; and &lt; with > and <
//
// Inputs:
//        Stng - the string to be searched & fixed
//        Fnd  - the characters to be replaced (found)
//        Rep  - The character(s) to replace Fnd with.
//
// Return: The fixed string.
// ------------------------------------------------------
function ReplaceIt(Stng, Fnd, Rep) {
   /*
   while(Stng.indexOf(Fnd) != -1) {
      var pntr = Stng.indexOf(Fnd);
      var stn = Stng.substring(0, pntr);
      var flen = Fnd.length;
      stn += Rep;
      stn += Stng.substring(pntr+flen, Stng.length);
      Stng = stn;
   }
   */

   // Or using regular expressions & .replace
   var Tmps = new RegExp(Fnd, "g");
   alert(Stng);
   Stng = Stng.replace(Tmps, Rep);
   alert(Stng);

   return(Stng);
}



// -------------------------------------------------------
// This function correctly gets the last update date for 
// both IE & NS browsers.
//
// inputs:  none
// outputs: none
// actions: Save correct date & year in global vars
// returns: none
// -------------------------------------------------------
var LastUpdate = ""; // These need to be global Or, placed 
var Year = "";       // in your HTML <head><script> section
function GetLastUpDate() {

   var Dt = document.lastModified; // added 01/15/2003
   var Dt02;
   var pnt = Dt.lastIndexOf(" "); // find the last space

   if(Dt.length > 25) { // Needed to get to correct version for NetScape 7.0
      //document.location.href="indexNS.html";
   }

   Dt02 = Dt.substring(0, pnt); // chop off everything after last space
   
   // ----------------------------------------------------
   // If there was a "GTM" at the end of the original time,
   // then we also need to still remove the hrs:mins:sec
   // So, once again remove all past new last space
   // ----------------------------------------------------
   if(Dt.lastIndexOf("GMT") != -1) { // Got GMT!
      pnt = Dt02.lastIndexOf(" ");

      LastUpdate = Dt02.substring(0, pnt);
      Year = LastUpdate.substring(LastUpdate.length-4, LastUpdate.length);
   }
   else {
      LastUpdate = Dt02;
      Year = LastUpdate.substring(LastUpdate.length-4);
   }
   // -------------------------------------------------------


}




// ###################################################################
//                        Test functions...
// ###################################################################

//This is called from the <body onLoad> tag 
function openinfo() {
   displayinfo = window.open("jpole.htm", "JpoleWin", "toolbar=no, status=no, width=200, height=400, scrollbars=yes");
} 

// This is called from a push button Labeled as "Back" 
function cleanup() { 
   window.location.href="index.htm"; 
   displayinfo.window.close(); 
} 


// -------------------------------------------------------------------
// TenLinks - Shows how to hide code from client's eyes & produce 
//            10 links
// inputs: none
// output: nothing.
// return: ten "hidden" lines of links on the html page.
// -------------------------------------------------------------------
function TenLinks() {
  for(var j=0; j<10; ++j) { //List of links...
    document.write('<a href="Thisfile'+j+'.htm">');
    document.write('[Click Here]</a><br>');
  }
}



   // This function inside of a js file
   function conFirm() {
     return confirm("Are you sure you want to log out ?")
   }



// -------------------------------------------------------------------
// factorial!
//
// inputs: n (The number to find factorial of)
// output: nothing.
// return: n!
//
// BY: Richard Edwards, ca_redwards
//     http://www.jsworkshop.com/bb/viewtopic.php?t=951
// -------------------------------------------------------------------
function factorial(n)  { 
  if(n<3) 
    return n;
  else 
    return n*factorial(n-1);
} 



// -------------------------------------------------------------------
// inputs: p (prime)
// output: nothing.
// return: factorization
//
// BY: Richard Edwards, ca_redwards
//     http://www.jsworkshop.com/bb/viewtopic.php?t=951
// -------------------------------------------------------------------
function prime(p) {
  if(!prime[p])  {  // unknown prime factorization? 
    var f=2;        // start with trial factor 2. 
    
    if(p%2)
      for(f=3;(p%f)&&(f*f<=p);f+=2);  // odd number? try odd factors up to root.. 
    
    if(f*f>p) {
      prime[p]=p                 // reasonable factors exhausted? number is prime. 
    }
    else {
      prime[p]=f+'*'+prime(p/f)  // use this factor, recurse for more! 
    }
  }
  return prime[p]                // return known factorization 
} 



// ---------------------------------------------------------
// To get all the info possible in a standard form for
// today's date from the client computer.
//
// Inputs:  valid var new Date
// Outputs: none
// Returns: standardized date info MMM/DD/YYYY (Jan/15/2003)
// Actions: none
// ---------------------------------------------------------
function StandardDateInfo(ThisDay) {
  var now = ThisDay.toString(); // date format to a string
  var parts = new Array();      
  parts = now.split(" ");       // string date into array

  var MyDate = parts[1] + "/";     // get array element #2 (month)
  MyDate += parts[2] + "/";        // attach array element #3 (day)
  MyDate += parts[parts.length-1]; // attach last array element (year)

  return(MyDate);
}



// ---------------------------------------------------------
// Gets the number of days since our time began and the
// number of weeks since our time began.
//
// Inputs:  Year, Month#, Day, Modulus (2003, 05, 21, Mod) = May 21, 2003
// Outputs: none
// Returns: Week number since our time began.
// Actions: none
// ---------------------------------------------------------
function WeekNumbMod(S1, S2, S3, Mod) {

   var Begin = new Date(S1, S2-1, S3); // convert Starting date to internal date
   var Bms = Date.parse(Begin);        // convert Starting date to ms
   var nowDay = new Date();            // convert Today date to internal date
   var MDms = Date.parse(nowDay);      // convert Today date to ms

   // Divide the delta ms-dates (Today - Starting) by #ms/day
   var days = Math.floor((MDms - Bms)/(24*60*60*1000));

   // Find the week number for today
   var weeks = Math.floor(days/7);

   return(weeks % Mod); // To return the week number since Begin
   // Note: if the number of files to be displayed is less
   //       than the number fo weeks in a year we will need
   //       to return the modulo #weeks as in: weeks % Mod, where
   //       Mod < 52 in this case.

}



// -------------------------------------------------------------------
// Removes a char anyplace it is not wanted.
// 
// inputs:  A string that has chars not wanted, & the Chr not wanted
// outputs: none
// actions: removes all unwanted Chr from Stn
// returns: The string with the unwanted char(s) removed.
// -------------------------------------------------------------------
function RemoveChr(Stn, Chr) {
   var OldStn = Stn;
   var Slen = Stn.length;
   var Slen2 = 0;
   var Stn1 = "";

   var First = Stn.indexOf(Chr);
   while (First != -1) {
      Stn1 = Stn.substring(0, First);
      alert(Stn1);
      Slen2 = Stn1.length;
      Stn2 = Stn.substring(Slen2+1, Slen);
      Stn = Stn1 + Stn2;
      First = Stn.indexOf(Chr);
   }
   
   //DeBug("Returning: [" + Stn + "]"); // pktst
   return (Stn);

}



// -------------------------------------------------------------------
// Parses all innerHTML sent to find E: & BuildIt and replace it with
// the actual email address.
// Form of the email address line is:
// Email line form: |ke3fl|hoo|ya|0|
//                     0    2  1  3
//
// inputs:  PgDat - the full html page
// outputs: none
// actions: email addresses are rebuilt and replace the email lines
// returns: The corrected full html page
// -------------------------------------------------------------------
function BuildIt(a, b, c, d) { // pk01

   var Stng = ""; // This line being built up

   Stng = '<a href="mailto:' + a  + "@" + c + b;
   switch (d/1) { // Make sure it's a number!
      case 0:
         Stng += ".com";
         break;
      case 1:
         Stng += ".net";
         break;
      case 2:
         Stng += ".org";
         break;
      case 3:
         Stng += ".gov";
         break;
      case 4:
         Stng += ".us";
         break;
      default:
         Stng += ".ccc";
   }
   Stng +=  '">[E-mail]</a>';

   document.write(Stng);
   //alert(Stng);

} // end of: BuildIt(PgDat)



// -------------------------------------------------------------------
// Parses all innerHTML sent to find E: & BuildIt and replace it with
// the actual email address.
// Form of the email address line is:
// Email line form: |ke3fl|hoo|ya|0|
//                     0    2  1  3
//
// inputs:  PgDat - the full html page
// outputs: none
// actions: email addresses are rebuilt and replace the email lines
// returns: The corrected full html page
// -------------------------------------------------------------------
function BuildIt2(a, b, c, d) { // pk01

   var Stng = ""; // This line being built up

   Stng = a  + "@" + c + b;
   switch (d/1) { // Make sure it's a number!
      case 0:
         Stng += ".com";
         break;
      case 1:
         Stng += ".net";
         break;
      case 2:
         Stng += ".org";
         break;
      case 3:
         Stng += ".gov";
         break;
      case 4:
         Stng += ".us";
         break;
      default:
         Stng += ".ccc";
   }

   //alert(Stng);
   document.write(Stng);

} // end of: BuildI2t()



// -------------------------------------------------------------------
// Gets the person's password & passes it on to the Perl program
// inputs:  the password
// outputs: none
// actions: updates the PassWrd field value
// returns: true/false, false = do not submit required input field empty
// -------------------------------------------------------------------
function ChkPassWrd() { // pk01
   var Rtn = true;
   var PW = document.edit.passwrd.value;
   
   //DeBug("Got to ChkPassWrd");
   //alert("document.edit.passwrd.value = " + document.edit.passwrd.value);
   
   PW = prompt("Enter your password & click");
   
   if(PW.length < 2) {
      Rtn = false;
      alert("The password you entered was not of the correct form.");
   }
   else {
   
      document.edit.passwrd.value = PW;
   }
   
   //alert("Password entered was: ["+document.edit.passwrd.value+"]"); // debugging test
   
   //return false;
   return Rtn;
}   
