// Library of JavaScript functions use by SBAPITP

// JavaScript by David Turley <dturley@pobox.com>
//     This JavaScript may be modified and used by anyone as long as this notice
//     remains intact. You may not sell or otherwise profit from this code without
//     my permission.
// Modified by Steve Benson for use on SBAPITP

    function makeArray() 
    {
    
        for (i = 0; i<makeArray.arguments.length; i++)
             this[i] = makeArray.arguments[i];
    }
 
    function getFullYear(d) 
    {
        var y = d.getYear();
    
        if (y < 1000) {y += 1900};
        return y;
    }

    //var zone = "EDT";
    var days = new makeArray("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var months = new makeArray("January","February","March","April","May","June","July","August","September","October","November","December");


    function format_time(t) 
    {
   
        var Hours = t.getHours();
        var Minutes = t.getMinutes();
        var Seconds = t.getSeconds();
        var Day = t.getDay();
        var Date = t.getDate();
        var Month = t.getMonth();
        var Year = getFullYear(t);
    
        var ampm = "AM";
        
        if (Hours > 11) {ampm="PM";}
        if (Hours > 12) {Hours -= 12;}
    	if (Hours == 0) {Hours = 12;}
    
    	if (Minutes < 10) {Minutes = "0"+Minutes;}
    	if (Seconds < 10) {Seconds = "0"+Seconds;}
    
        timeString = "";
        timeString += days[Day];
        timeString += " ";
        timeString += months[Month];
        timeString += " ";
        timeString += Date;
        timeString += ",\&nbsp\;\&nbsp\;";
        timeString += Year;
           
       return timeString;
  
    }   

    // end of JavaScript functions written by David Turley <dturley@pobox.com>

    
    
    // following functions written by Steve Benson
    // to get create the correct date string for use in SBAPITP URL anchors
    
    function makeArray() 
    {
        for (i = 0; i<makeArray.arguments.length; i++)
            this[i] = makeArray.arguments[i];
    }
 
    var Anchor_months = new makeArray("january","february","march","april","may","june","july","august","september","october","november","december");
    
    function getFullYear(d) 
    {
        var y = d.getYear();
        if (y < 1000) {y += 1900};
        return y;
    }   

    
    function format_Anchordate(lDate) 
    {
   
        var Date    = lDate.getDate();
        var Month   = lDate.getMonth();
        var Year    = getFullYear(lDate);
        
        timeString = "#";
        timeString += Anchor_months[Month];
        timeString += Date;
        timeString += "-";
        timeString += Year;
       
        return timeString;
  
    }
    
    // function to create the date construct used in aChangeDates
    function MkDate(year, month, day)
    {
        this.chYear   = year;
        this.chMonth  = month;
        this.chDay    = day;
            
        return this;
    }
                
        
    // Build array used to track site change dates
    // format: (YYYY, MM, DD)
    var aChangeDates = new Array();
      
    aChangeDates[0] = new MkDate(1999, 10, 30);
    aChangeDates[1] = new MkDate(1999, 11, 1);
    aChangeDates[2] = new MkDate(1999, 11, 5);
    aChangeDates[3] = new MkDate(1999, 11, 7);
    aChangeDates[4] = new MkDate(1999, 11, 14);
    aChangeDates[5] = new MkDate(1999, 12, 3);
    aChangeDates[6] = new MkDate(2000, 1, 7);
    aChangeDates[7] = new MkDate(2000, 2, 2);
    aChangeDates[8] = new MkDate(2000, 2, 8);
    aChangeDates[9] = new MkDate(2000, 2, 14);
	aChangeDates[10] = new MkDate(2005, 4, 2);
	aChangeDates[11] = new MkDate(2005, 6, 16);
  
    //  Cookie functions follow:
    
    //  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
    //
    //  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
    //  The following functions are released to the public domain.

   
    // "Internal" function to return the decoded value of a cookie

    function getCookieVal (offset) 
    {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
        endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }

    //  Function to correct for 2.x Mac date bug.  Call this function to
    //  fix a date object prior to passing it to SetCookie.
    //  IMPORTANT:  This function should only be called *once* for
    //  any given date object!  See example at the end of this document.

    function FixCookieDate (date) 
    {
        var base = new Date(0);
        var skew = base.getTime(); // dawn of (Unix) time - should be 0
        
        if (skew > 0)  // Except on the Mac - ahead of its time
            date.setTime (date.getTime() - skew);
    }

    //  Function to return the value of the cookie specified by "name".
    //    name - String object containing the cookie name.
    //    returns - String object containing the cookie value, or null if
    //      the cookie does not exist.

    function GetCookie (name) 
    {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;

        while (i < clen) 
        {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal (j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break; 
        } // while (i < clen) 
        return null;
    }

    //  Function to create or update a cookie.
    //    name - String object containing the cookie name.
    //    value - String object containing the cookie value.  May contain
    //      any valid string characters.
    //    [expires] - Date object containing the expiration data of the cookie.  If
    //      omitted or null, expires the cookie at the end of the current session.
    //    [path] - String object indicating the path for which the cookie is valid.
    //      If omitted or null, uses the path of the calling document.
    //    [domain] - String object indicating the domain for which the cookie is
    //      valid.  If omitted or null, uses the domain of the calling document.
    //    [secure] - Boolean (true/false) value indicating whether cookie transmission
    //      requires a secure channel (HTTPS).  

    //  The first two parameters are required.  The others, if supplied, must
    //  be passed in the order listed above.  To omit an unused optional field,
    //  use null as a place holder.  For example, to call SetCookie using name,
    //  value and path, you would code:
    
    //      SetCookie ("myCookieName", "myCookieValue", null, "/");

    //  Note that trailing omitted parameters do not require a placeholder.

    //  To set a secure cookie for path "/myPath", that expires after the
    //  current session, you might code:

    //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);

    function SetCookie (name,value,expires,path,domain,secure) 
    {
        document.cookie = name + "=" + escape (value) +
            ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                    ((domain) ? "; domain=" + domain : "") +
                        ((secure) ? "; secure" : "");
    }

    //  Function to delete a cookie. (Sets expiration date to start of epoch)
    //    name -   String object containing the cookie name
    //    path -   String object containing the path of the cookie to delete.  This MUST
    //             be the same as the path used to create the cookie, or null/omitted if
    //             no path was specified when creating the cookie.
    //    domain - String object containing the domain of the cookie to delete.  This MUST
    //             be the same as the domain used to create the cookie, or null/omitted if
    //             no domain was specified when creating the cookie.
    //
    function DeleteCookie (name,path,domain) 
    {
        if (GetCookie(name)) 
        {
            document.cookie = name + "=" +
                ((path) ? "; path=" + path : "") +
                    ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

    //  End of Cookie Functions Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>

    ////////////////////////////////////////////////////////////////////////////
    // following functions written by Steve Benson for SBAPITP
    
    // function to create a cookie that contains anchor information
    // to be used by another page to link to proper anchor on history page
    // if user wishes to see history of changes since their last visit to the site
    
    function setAnchorCookie(sAnchorName)
    {
        // call to SetCookie without a expiration date
        // creates a temporary cookie that's deleted
        // when the browser app is exited
        SetCookie("Anchor_Name", sAnchorName, null, "/");
    }

    // function to create a cookie that contains the date of visit
    // to SBAPIT page that will expire in six months

	function setVisitCookie()
    {
		var dThisVisit = new Date();
        var expdate = new Date();

	    expdate.setTime (expdate.getTime() + 365 * (24 * 60 * 60 * 1000)); // 12 months (365 days) from now
        SetCookie("Last_Visit", dThisVisit.toGMTString(), expdate, "/");
    }
	
    // end of the Cookie functions    
     
    // function to check for last visit cookie, see if user has been asked
    // if they want to see the history page before (refresh of page or return to
    // page after initial page load and popup of changes page window if indicated
    
    function CheckLastVisitDate()
    {
        var i;
		var iVal = 0;
        var bWereMods = false;
        var AnchorName = "";
		var bAskedBefore = false;
        
        // get last visit cookie and use value for check against aChangeDates array dates
        // comment out to debug
        dLastVisit = GetCookie("Last_Visit");
		
		// used to debug
		//var dLastVisit = new Date(parseInt(document.TestExpiry.EYear.value), parseInt(document.TestExpiry.EMonth.value)-1, parseInt(document.TestExpiry.EDay.value));
		
		//comment out of logic when debugging
		if ( dLastVisit != null )
		{
			dLastVisit = new Date(dLastVisit);
		}
		
		// used to debug
		//alert("Last visit=" + dLastVisit);
		
		// check to see if user alread asked cookie exists
		bAskedBefore = GetCookie("Already_Asked");

		if ( bAskedBefore == null )
		{
			bAskedBefore = false;
			// used to debug
			//alert("I wasn't asked before");
		}
		
		// used to debug
		//alert("Now Asked Before=" + bAskedBefore);
        
        // check for null return from GetCookie() Or (||) if they have already been asked
        if (dLastVisit == null || bAskedBefore) 
        {
			// if user has no cookie set one
			if ( dLastVisit == null )
			{
                // set a cookie to indicate user shouldn't be 
	            // asked if they want to see the changes should they refresh this page
	            setAlreadyAskedCookie();    
	            
	            // used to debug
	            //alert("Just set the Already_Asked cookie for a visitor without a previous cookie");
	            
	            // set a cookie with today's visit date
                setVisitCookie();
                // used to debug
			    //alert("Just set the cookie for a visitor without a previous cookie");
			}
			else
			{
			    // do nothing visitor has already been asked, used to debug
			    //alert("This visitor has already been asked and I should do nothing now.");
			}
        } 
        else
        {
            for ( i = 0; i < aChangeDates.length; i++ )
            {
                // load the date from aChangeDates array into var for comparison
                // note it is set to 8:00AM to prevent a user returning on the same day
                // of a change from being continually bugged about the changes
                ModDate = new Date(aChangeDates[i].chYear, (aChangeDates[i].chMonth) - 1, aChangeDates[i].chDay, 08, 00, 00);
            
                // check to see if the last visit to site is 
                // before any dates modificaions were made to site
                if (dLastVisit.getTime() < ModDate.getTime())
                {
                    bWereMods = true;
                    break;
                }
            }       
            if (bWereMods)
            {
                // format ModDate into an anchor
                AnchorName = format_Anchordate(ModDate);

                // delete any existing anchor cookie
                DeleteCookie("Anchor_Name");
                
                // place anchor (URL + page anchor name) in a cookie
                setAnchorCookie(AnchorName);
                
                // test anchor cookie
                sAnchor = GetCookie("Anchor_Name");
                
                // pop up window to ask user if they want to go to the history page
                // to see changes since their last visit
                POP = window.open("newmods.html", "POP", "toolbar=0,location=0,directories=0,status=0,scrollbars=no,resizable=0,copyhistory=0,width=550,height=360");
                
            }
            else // if (bWereMods)
            {
                // There were no mods since last visit so set the Already_Asked cookie
                // so they won't flow into this logic if they refresh this page
	            setAlreadyAskedCookie();
	        
                // used to debug
                //alert("Just set the Already_Asked cookie for a visitor with a cookie but no new mods");
                	            
                // used to debug
				//alert("There have been no modifications to this site since your last visit on " + dLastVisit);
            }
            // set a cookie with today's visit date
            setVisitCookie();
            // used to debug
			//alert("Just set the cookie for this visit for visitor that had a cookie already");

        } // if (dLastVisit == null || bAskedBefore) else
        
    } // function CheckLastVisitDate()
    
    
    // function to alter the parent (opener) window source to the history page and the 
    // correct anchor
        
    function GoHistory() 
    {
        // set the contents of the parent window to the history page
        // with correct anchor
        self.opener.document.location=sURLName;

        // we're done with the pop-up window, close it now
        self.close();
    }
	
	// function to create a cookie that indicates the user
    // has already been asked if they want to checkout the changes
    // so don't bug them again with the same question :)
    
	function setAlreadyAskedCookie()
    {
        // call to SetCookie without a expiration date
        // creates a temporary cookie that's deleted
        // when the browser app is exited
        SetCookie("Already_Asked", "true", null, "/");
    }
	
	// This script is (c) copyright 2006 Jim Tucek under the
	// GNU General Public License (http://www.gnu.org/licenses/gpl.html)
	// For more information, visit www.jracademy.com/~jtucek/email/ 
	// Leave the above comments alone!

	// Script it from: http://www.jracademy.com/~jtucek/email/download.php

	var decryption_cache = new Array();

	// This script block stores your encrypted email address(es) in
	// the addresses array.  If you ever want to add new addresses to
	// a page, you can return to http://www.jracademy.com/~jtucek/email/ 
	// and generate more, and then add this block to your existing page.
	
	// addresses are: webinfo, steve, jon, webmaster (0-3) @a-point-in-time.com

	if(!addresses) var addresses = new Array();
	addresses.push("2279 437 1780 2166 847 297 2162 1959 210 2254 2016 1152 847 1819 1096 1959 90 2166 2053 992 1959 847 1819 2162 2053 847 1819 2053 2162 847 1780 2016 630 418 1959 1780 413 2127 1256 1152 1961 2016 418 2162 1180 1587 715 1383 1959 847 1819 2162 715 2033 1819 715 1731 847 1780 2016 715 1383 922 1959 2162 1959 1029 650 2166 992 922 256 715 2206 1959 1780 1780 2016 1819 2162 2127 715 1312 1959 650 1780");
	addresses.push("13943 5863 4814 10634 5658 7312 12184 12909 9681 5051 12184 3077 13275 3077 11671 10634 9041 2761 12909 5658 10370 12184 9041 5658 10370 9041 12184 5658 4814 3077 9517 2696 12909 4814 11001 5051 1499 6503 11633 3077 2696 12184 5717 1452 3468 3899 12909 5658 10370 12184 3468 1241 10370 3468 5661 5658 4814 3077 3468 3899 9841 12909 12184 12909 6504 4124 10634 2761 9841 578 3468 1990 12909 4814 4814 3077 10370 12184 5051 3468 5916 12909 4124 4814");
	addresses.push("13943 5863 4814 10634 5658 7312 12184 12909 9681 11633 12909 10370 11671 10634 9041 2761 12909 5658 10370 12184 9041 5658 10370 9041 12184 5658 4814 3077 9517 2696 12909 4814 11001 5051 1499 6503 11633 3077 2696 12184 5717 1452 3468 3899 12909 5658 10370 12184 3468 1241 10370 3468 5661 5658 4814 3077 3468 3899 9841 12909 12184 12909 6504 4124 10634 2761 9841 578 3468 1990 12909 4814 4814 3077 10370 12184 5051 3468 5916 12909 4124 4814");
	addresses.push("13943 5863 4814 10634 5658 7312 12184 12909 9681 13605 3077 6503 4814 10634 5051 12184 3077 4124 11671 10634 9041 2761 12909 5658 10370 12184 9041 5658 10370 9041 12184 5658 4814 3077 9517 2696 12909 4814 11001 5051 1499 6503 11633 3077 2696 12184 5717 1452 3468 3899 12909 5658 10370 12184 3468 1241 10370 3468 5661 5658 4814 3077 3468 3899 9841 12909 12184 12909 6504 4124 10634 2761 9841 578 3468 1990 12909 4814 4814 3077 10370 12184 5051 3468 5916 12909 4124 4814");

	function decrypt_string(crypted_string,n,decryption_key,just_email_address)
	{
		var cache_index = "'"+crypted_string+","+just_email_address+"'";

		if(decryption_cache[cache_index])					// If this string has already been decrypted, just
			return decryption_cache[cache_index];				// return the cached version.

		if(addresses[crypted_string])						// Is crypted_string an index into the addresses array
			var crypted_string = addresses[crypted_string];			// or an actual string of numbers?

		if(!crypted_string.length)						// Make sure the string is actually a string
			return "Error, not a valid index.";

		if(n == 0 || decryption_key == 0) 
		{					// If the decryption key and n are not passed to the
			var numbers = crypted_string.split(' ');			// function, assume they are stored as the first two
			n = numbers[0];	decryption_key = numbers[1];			// numbers in crypted string.
			numbers[0] = ""; numbers[1] = "";				// Remove them from the crypted string and continue
			crypted_string = numbers.join(" ").substr(2);
		}

		var decrypted_string = '';
		var crypted_characters = crypted_string.split(' ');

		for(var i in crypted_characters) 
		{
			var current_character = crypted_characters[i];
			var decrypted_character = exponentialModulo(current_character,n,decryption_key);
			if(just_email_address && i < 7)				// Skip 'mailto:' part
				continue;
			if(just_email_address && decrypted_character == 63)	// Stop at '?subject=....'
				break;
			decrypted_string += String.fromCharCode(decrypted_character);
		}
		
		decryption_cache[cache_index] = decrypted_string;			// Cache this string for any future calls

		return decrypted_string;
	} // function decrypt_string(crypted_string,n,decryption_key,just_email_address)

	function decrypt_and_email(crypted_string,n,decryption_key) 
	{
		if(!n || !decryption_key) { n = 0; decryption_key = 0; }
		if(!crypted_string) crypted_string = 0;

		var decrypted_string = decrypt_string(crypted_string,n,decryption_key,false);
		parent.location = decrypted_string;
	}

	function decrypt_and_echo(crypted_string,n,decryption_key) 
	{
		if(!n || !decryption_key) { n = 0; decryption_key = 0; }
		if(!crypted_string) crypted_string = 0;

		var decrypted_string = decrypt_string(crypted_string,n,decryption_key,true);
		document.write(decrypted_string);
		return true;
	}

	// Finds base^exponent % y for large values of (base^exponent)
	function exponentialModulo(base,exponent,y) 
	{
		if (y % 2 == 0) 
		{
			answer = 1;
			for(var i = 1; i <= y/2; i++) 
			{
				temp = (base*base) % exponent;
				answer = (temp*answer) % exponent;
			}
		} else 
		{
			answer = base;
			for(var i = 1; i <= y/2; i++) 
			{
				temp = (base*base) % exponent;
				answer = (temp*answer) % exponent;
			}
		}	
		return answer;
	}

// set the var src_loaded for running script functions in HTML docs
var src_loaded = true;