<!--
// Constants for IE "cookie" operations
var ieDataStore = 'CCCC';
var ieDataObj = 'ieCookie';

/*****************************************************************************
*        File: driveline.js
* Description: Code to operate the driveline
*****************************************************************************/

/*****************************************************************************
*    Function: browserFireFox
* Description: Determine if the browser is FireFox
*  Parameters: None
*     Returns: True if the browser is FireFox
*****************************************************************************/
function browserFireFox() {

    return (navigator.userAgent.toLowerCase().indexOf('firefox') >= 0);

}

/*****************************************************************************
*    Function: browserIE
* Description: Determine if the browser is Internet Explorer
*  Parameters: None
*     Returns: True if the browser is Internet Explorer
*****************************************************************************/
function browserIE() {

    return (navigator.userAgent.toLowerCase().indexOf('msie') >= 0);

}

/*****************************************************************************
*    Function: browserOther
* Description: Determine if the browser is not FireFox or IE
*  Parameters: None
*     Returns: True if the browser is not IE or FireFox
*****************************************************************************/
function browserOther() {

    return !(browserFireFox() || browserIE());

}

/*****************************************************************************
*    Function: addText
* Description: Add a line of text to the list of lines in an article
*  Parameters: sText - The line of text to add
*****************************************************************************/
function addText(sText) {
    this.text.push(sText);
}

/*****************************************************************************
*    Function: Article
* Description: Object to old each article for the President's Page and
*              Secretaty's Report.
*  Parameters: nMonth - Month of the article
*              nDay - Day of the article
*              nYear - Year of the article
*              aArticle - Text of the article.  One entry per paragraph <p>
*****************************************************************************/
function Article(nMonth, nDay, nYear, aArticle) {
    this.month = nMonth;
    this.day = nDay;
    this.year = nYear;
    this.text = aArticle;
    this.add = addText;
}

/*****************************************************************************
*    Function: displayArticle
* Description: Hide all of the articles and display the requested article
*  Parameters: divName - Name of the <div> to make visible
*****************************************************************************/
function displayArticle(divName) {
    var noDisplay = 'none';
    var showDisplay = '';
    var classRoot = 'article';
    var divs = document.getElementsByTagName('div');
    var i;

    // Hide all articles
    for (i = 0; i < divs.length; i++) {
        if (divs[i].className.indexOf(classRoot) >= 0)
            divs[i].style.display = noDisplay;

    }

    // Display the selected article
    document.getElementById(divName).style.display = showDisplay;

}

/*****************************************************************************
*    Function: generatePP
* Description: Generate the President's Pages
*  Parameters: None
*****************************************************************************/
function generatePP() {
    var presPagePrefix = 'artPP';
    var aArticles = buildPP();
    var html = '';
    var past;
    var i;
    var j;

    // Generate the Table of Contents for the President's Page
    past = 'View Past Pages: ';
    for (i = 0; i < aArticles.length; i++) {
        past += '<a href=\'javascript:void(0);\' onClick=\'javascript: displayArticle(\"' + presPagePrefix + i + '\");\'>';

        if (i == 0)
            past += 'Current';
        else
            past += monthName(aArticles[i].month - 1) + ' ' + aArticles[i].year;

        past += '</a>&nbsp;';

    }

    // Generate the President's Page articles
    for (i = 0; i < aArticles.length; i++) {
        html += '<div id=\'' + presPagePrefix + i + '\' class=\'articlePP\' style=\'display: none;\'>';
        html += past;
        html += '<br><hr>';
        html += adsBannerAds.showVert(adsBannerAds.president);
        html += '<p class=\'headlinePP\'>President\'s Page<br>' + monthName(aArticles[i].month - 1) + ' ' + aArticles[i].year + '</p>'

        for (j = 0; j < aArticles[i].text.length; j++)
            html += '<p>' + aArticles[i].text[j] + '</p>';

        html += '</div>';

    }

    return html;

}

/*****************************************************************************
*    Function: generateSR
* Description: Generate the Secretary's Report
*  Parameters: None
*****************************************************************************/
function generateSR() {
    var srPrefix = 'artSR';
    var aArticles = buildSR();
    var html = '';
    var past;
    var i;
    var j;

    // Generate the Table of Contents for the Secretary's Report
    past = '<b>Past Reports: </b>';
    for (i = 0; i < aArticles.length; i++) {
        past += '<a href=\'javascript:void(0);\' onClick=\'javascript: displayArticle(\"' + srPrefix + i + '\");\'>';

        if (i == 0)
            past += 'Current';
        else
            past += monthName(aArticles[i].month - 1) + ' ' +  aArticles[i].day + ', ' + aArticles[i].year;

        past += '</a>&nbsp;';

    }

    // Generate the Secretary's Report articles
    for (i = 0; i < aArticles.length; i++) {
        html += '<div id=\'' + srPrefix + i + '\' class=\'articleSR\' style=\'display: none;\'>';
        html += past;
        html += '<br><hr>';
        html += adsBannerAds.showVert(adsBannerAds.secretary);
        html += '<p class=\'headlineSR\'>Secretary\'s Report<br>' + monthName(aArticles[i].month - 1) + ' ' +
                 aArticles[i].day + ', ' + aArticles[i].year + '</p>'

        for (j = 0; j < aArticles[i].text.length; j++)
            html += '<p>' + aArticles[i].text[j] + '</p>';

        html += '</div>';

    }

    return html;

}

/*****************************************************************************
*    Function: parseMember
* Description: Parse an entry into the membership list
*  Parameters: member - A line of the membership list
*****************************************************************************/
function parseMember(member) {
    var fieldSep = '~';
    var i = 1;

    while (member.indexOf(fieldSep) > 0) {
        if (i == 1)
            member = member.replace(fieldSep, '&nbsp;');
        else if (i == 2 || i == 3 || i == 5)
            member = member.replace(fieldSep, '<br>');
        else
            member = member.replace(fieldSep, '</td><td>');

        i++;

    }

    return member;

}

/*****************************************************************************
*    Function: formatMoney
* Description: Format a value to money style (XXX.XX)
*  Parameters: amount - The value to format
*****************************************************************************/
function formatMoney(amount) {
    var rtn = '' + Math.round(100 * amount);

    return rtn.substring(0, rtn.length - 2) + '.' + rtn.substring(rtn.length - 2, rtn.length);

}

/* Section to manage cookies */
var testCookie = 'CCCC';
var keyCookie = 'key';

/*****************************************************************************
*    Function: setExprDate
* Description: Set the standard expiration date for the cookies to
*              2/1/Year + 1.  All memberships must be renewed by 2/1.
*  Parameters: None
*     Returns: Date - 2/1 of next year
*****************************************************************************/
function setExprDate() {
    var expDate = new Date();

    with (expDate) {
        setMonth(1);                 // Feburary
        setDate(1);                  // 1st
        setYear(getFullYear() + 1);  // Next year
        setHours(0);                 // 12:00:00.000 AM
        setMinutes(0);
        setSeconds(0);
        setMilliseconds(0);

    }

    return expDate;

}

/*****************************************************************************
*    Function: generateCookie
* Description: Generate the string that will be used to define the cookie
*  Parameters: name - The name of the cookie
*              value - The value to assign to the name
*     Returns: Nothing
*****************************************************************************/
function generateCookie(name, value, exprDate) {

    return name + '=' + escape(value) + '; expires=' +
           exprDate.toGMTString() + '; path=/';

}


/*****************************************************************************
*    Function: setCookie
* Description: Set a cookie
*  Parameters: name - The name of the cookie
*              value - The value to assign to the name
*     Returns: Nothing
*****************************************************************************/
function setCookie(name, value) {
    var data;

    //if (browserIE()) {
    //    data = document.getElementById(ieDataObj);

    //    data.load(ieDataStore);
    //    data.expires = setExprDate().toGMTString();
    //    data.setAttribute(name, escape(value));
    //    data.save(ieDataStore);

    //}
    //else
        document.cookie = generateCookie(name, value, setExprDate())

}

/*****************************************************************************
*    Function: deleteCookie
* Description: Delete a cookie
*  Parameters: name - The name of the cookie to delete
*     Returns: Nothing
*****************************************************************************/
function deleteCookie(name) {
    var threeDaysAgo = 3 * 24 * 3600 * 1000;
    var expDate = new Date();
    var data;

    //if (browserIE()) {
    //    data = document.getElementById(ieDataObj);

    //    data.removeAttribute(name);
    //    data.save(ieDataStore);

    //}
    //else {
        // Set the expiration date of the cookie to three days ago
        expDate.setTime(expDate.getTime() - threeDaysAgo);

        // Change the value and set to expire to 3 days ago
        document.cookie = generateCookie(name, 'X', expDate);

    //}

}

/*****************************************************************************
*    Function: getCookie
* Description: Get the value of a cookie
*  Parameters: name - The name of the cookie
*     Returns: String - The value assigned to name
*****************************************************************************/
function getCookie(name) {
    var cookies = ' ' + document.cookie + ';'; // Pad to make searching easier
    var cookie = ' ' + name + '=';
    var cookieVal = null;
    var data;
    var i;
    var j;

    i = cookies.indexOf(cookie);

    if (i >= 0) {
        i += cookie.length;  // Move to the beginning of the value
        j = cookies.indexOf(';', i);  // Find the end of the value
        cookieVal = unescape(cookies.substring(i, j));
    }

    return cookieVal;

}

/*****************************************************************************
*    Function: clearMailCookies
* Description: Clear out the mail cookies
*  Parameters: None
*     Returns: Nothing
*****************************************************************************/
function clearMailCookies(cookiePrefix) {
    var maxCookies = 20;

    deleteCookie(testCookie);
    deleteCookie(keyCookie);

    for (var i = 0; i < maxCookies; i++)
       deleteCookie(cookiePrefix + i);

}

/*****************************************************************************
*    Function: canLoadCookies
* Description: Check to see if the user's browser will accept cookies
*  Parameters: None
*     Returns: true if cookies are accepted, false otherwise
*****************************************************************************/
function canLoadCookies() {
    var testValue = 'Test';
    var actualVal;
    var data;
    var ok;

    //if (browserIE()) {
    //    data = document.getElementById(ieDataObj);
    //    ok = (browserIE() && data != null);

    //}
    //else {
        deleteCookie(testCookie);  // Delete already existing cookie
        setCookie(testCookie, testValue);  // Set cookie
        actualVal = getCookie(testCookie);
        deleteCookie(testCookie);  // Delete the test cookie

        ok = (testValue == actualVal);

    //}

    return ok;

}

/* Section to generate the objects used to display the member list,         */
/* birthdays and Treasurer's Report.                                        */
var fieldSep = '~';
var recTerm = ';';
var lblTerm = ':';

/*****************************************************************************
*    Function: Member
* Description: Object to hold parsed membership data for one member
*  Parameters: lastName - Member's last name
*              firstName - Member's first name
*              address - Member's address
*              csz - Memeber's city, state and zip code
*              phone - Member's phone number
*              email - Member's email
*              car - Member's car(s)
*              sendPaper - 1 if the member gets the paper newsletter,
*                          otherwise 0
*****************************************************************************/
function Member(lastName, firstName, address, csz,
                phone, email, car, sendPaper) {
    this.lastName = lastName;
    this.firstName = firstName;
    this.address = address;
    this.csz = csz;
    this.phone = phone;
    this.email = email;
    this.car = car;
    this.sendPaper = sendPaper;

}

/*****************************************************************************
*    Function: Birthday
* Description: Class to hold parsed birthday data for one birthday
*  Parameters: member - The person whose birthday it is
*              month - Month of the birthday
*              day - Day of the month of the birthday
*****************************************************************************/
function Birthday(member, month, day) {
    this.member = member;
    this.month = month;
    this.day = day;
}

/*****************************************************************************
*    Function: Deposit
* Description: Class to hold parsed deposit data for one deposit
*  Parameters: label - The description of the deposit
*              amount - The amount of the deposit
*****************************************************************************/
function Deposit(label, amount) {
    this.label = label;
    this.amount = amount;
}

/*****************************************************************************
*    Function: Disbursement
* Description: Class to hold parsed disbursement data for one disbursement
*  Parameters: label - The description of the disbursement
*              amount - The amount of the disbursement
*****************************************************************************/
function Disbursement(label, amount) {
    this.label = label;
    this.amount = amount;
}

/*****************************************************************************
*    Function: treasurersReport
* Description: Object to hold parsed Treaserer's Report data
*  Parameters: reportDate - Date of the report
*              bb - Beginning balance
*              tdp - Total of the deposits
*              tdb - Total of the disbursements
*              eb - Ending balance
*****************************************************************************/
function treasurersReport(reportDate, bb, tdp, tdb, eb) {
    this.date = reportDate;
    this.beginBalance = bb;
    this.totalDeposits = tdp;
    this.totalDisbursements = tdb;
    this.endBalance = eb;
    this.deposits = new Array();
    this.disbursements = new Array();
}

/*****************************************************************************
*    Function: fillArray
* Description: Fill out an array to the requested length
*  Parameters: array - The array to fill out
*              length - The length the array should be
*****************************************************************************/
function fillArray(array, length) {

    while (array.length < length)
        array.push('');

    return array;

}

/*****************************************************************************
*    Function: parseRec
* Description: Parses a cookie string record into the appropriate object
*  Parameters: cookieRec - A record from a cookie
*              recType - The type of the record to return: m - member,
*                        t - Treasurer's Report, dp - Deposit,
*                        db - Disbursement, b - Birthday
*****************************************************************************/
function parseRec(cookieRec, recType) {
    var data = new Array();
    var rtn = null;
    var e;

    // Remove the record type, if it's present
    e = cookieRec.indexOf(lblTerm);
    if (e >= 0 && e < 3)
        cookieRec = cookieRec.substring(e + 1, cookieRec.length);

    // Remove the trailing record terminator if present
    if (cookieRec.substring(cookieRec.length - 1, cookieRec.length) == recTerm)
        cookieRec = cookieRec.substring(0, cookieRec.length - 1);

    while (cookieRec.length > 0) {

        e = cookieRec.indexOf(fieldSep);

        if (e >= 0) {
            data.push(cookieRec.substring(0, e));
            cookieRec = cookieRec.substring(e + 1, cookieRec.length);
        }
        else {
            data.push(cookieRec);
            cookieRec = '';
        }

    }

    if (recType.toLowerCase() == 'm') {
        data = fillArray(data, 8);
        rtn = new Member(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
    }
    else if (recType.toLowerCase() == 't')
        rtn = new treasurersReport(data[0], data[1], data[2], data[3], data[4]);
    else if (recType.toLowerCase() == 'dp')
        rtn = new Deposit(data[0], data[1]);
    else if (recType.toLowerCase() == 'db')
        rtn = new Disbursement(data[0], data[1]);
    else if (recType.toLowerCase() == 'bd')
        rtn = new Birthday(data[0], data[1], data[2]);

    return rtn;

}

/*****************************************************************************
*    Function: parseRecords
* Description: Parses the  cookie records into a list of the requested type
*  Parameters: cookiePrefix - The root part of the cookie identifier
*              recType - The type of the record to return: m - member,
*                        t - Treasurer's Report, dp - Deposit,
*                        db - Disbursement, b - Birthday
*              key - The decryption key to use
*****************************************************************************/
function parseRecords(cookiePrefix, recType, key) {
    var prefix = recType + lblTerm;
    var list = new Array();
    var cookie = getCookie(cookiePrefix + '0');
    var record;
    var i = 0;
    var s;
    var e;

    // If no records are found, attempt to load the encrypted data
    if (cookie == null && MTBData)
        cookie = decrypt(new MTBData(), key).data;

    // Traverse the cookie list
    while (cookie != null) {

        // Get the records out of the cookie
        s = 0;
        while (s < cookie.length) {
            s = cookie.indexOf(prefix, s);

            if (s >= 0) {
                e = cookie.indexOf(recTerm, s);

                if (e > s) {
                    record = parseRec(cookie.substring(s, e), recType);

                    if (record != null)
                        list.push(record);

                    s = e + 1;

                }
                else
                    s = cookie.length;

            }
            else
                s = cookie.length;

        }

        i++;
        cookie = getCookie(cookiePrefix + i);

    }

    return list;

}

/*****************************************************************************
*    Function: loadTreasurerReport
* Description: Loads the Treasurer's Report data
*  Parameters: cookiePrefix - The root part of the cookie identifier
*              key - The decryption key to use
*****************************************************************************/
function loadTreasurerReport(cookiePrefix, key) {
    var tr = parseRecords(cookiePrefix, 't', key);
    var dp = parseRecords(cookiePrefix, 'dp', key);
    var db = parseRecords(cookiePrefix, 'db', key);

    if (dp.length >  0)
        tr[0].deposits = dp;

    if (db.length > 0)
        tr[0].disbursements = db;

    return tr[0];

}

/*****************************************************************************
*    Function: notAvailable
* Description: Returns an explaination of why certain Driveline information is
*              not available, and how the user can obtain the means to get the
*              data.
*  Parameters: None
*     Returns: A string
*****************************************************************************/
function notAvailable() {

    return '<br>In order to view the information on this page, you must meet ' +
           'the following requirements:<br>' +
           '1. Are a member of the Capital City Corvette Club.<br>' +
           '2. Have one of the following browsers: Internet Explorer v5.5 or ' +
           'later, Netscape/AOL v4.0 or later, or Mozilla FireFox v1.0 or later.<br>' +
           '3. Have your browser set to allow cookies.  Click ' +
           '<a href="javascript: showInstructions();">here for instructions</a> ' +
           'on how to setup your browser.<br>' +
           '4. Have received the monthly email from the webmaster.<br>' +
           '5. Clicked on the link in the email, and received confirmation in a browser ' +
           'window.<br>' +
           '6. Are viewing the web page with the same type of browser that ran in ' +
           'step 5.<br>' +
           '7. Refreshed your browser while viewing this page if you\'re not seeing ' +
           'the latest information.<br><br>' +
           'If you have any further questions, please ' +
           '<a href="mailto:swanecke@earthlink.net">email the CCCC webmaster</a>.'

}

/*****************************************************************************
*    Function: showInstructions
* Description: Display the instructions in a popup window.
*  Parameters: None
*****************************************************************************/
function showInstructions() {
    var windowName = 'ShowInstructions';
    var windowParms = 'width=700,height=400,dependant=yes,scrollbars=yes,left=50,top=50,resizable=yes,location=no';

    var docWindow = window.open('cookieInst.htm', windowName, windowParms);

}

/*****************************************************************************
*    Function: runPopupClick
* Description: Run the onClick event for the "Load Data" button of the popup
*              version of the data load.
*  Parameters: data - An array of the data to load
*              button - The button to disable if we cannot run cookies
*              msgID - The ID of the hidden messege to display when the load
*                      works
*     Returns: Nothing
*****************************************************************************/
function runPopupClick(data, button, msgID) {
    var windowParms = 'width=100,height=100,dependant=yes,scrollbars=yes,left=50,top=50,resizable=yes,location=no,status=yes';
    var base = '';
    var docWindow;
    var data;

    // Load any base path
    if (document.getElementsByTagName('base').length > 0)
        base = document.getElementsByTagName('base')[0].href;

    // Clear any existing cookie
    clearMailCookies('d');

    if (canLoadCookies()) {

        // Get the values that are to be loaded
        for (var i = 0; i < data.length; i++)
            docWindow = window.open(base + 'popData.htm?d=' + data[i],
                                    'd' + i,
                                    windowParms);

        alert('The CCCC Driveline data load is complete.');

        with (document.getElementById(msgID).style) {
            display = '';
            fontWeight = 900;
        }

    }
    else {
        button.disabled = true;
        alert('Warning: Your browser is not setup to accept cookies.  It is not possible to load the ' +
              'Memberlist, Treasurer\'s Report and Birthdays unless your browser can accept cookies.');
    }

}

/*****************************************************************************
*    Function: decrypt
* Description: Decrypt the data in the MTBData class
*  Parameters: mtb - An instance of the MTBData class
*              key - The decryption key
*     Returns: An instance of the MTBData class
*****************************************************************************/
function decrypt(mtb, key) {
    var coded = mtb.data.split(' ');
    var data = '';

    if (key != null)
        for (var i = 0; i < coded.length; i++)
            data += String.fromCharCode((parseInt(coded[i]) & 0x00FF) ^ (key.charCodeAt(i % key.length) & 0x00FF));

    mtb.data = data;

    return mtb;

}

/*****************************************************************************
*    Function: showOfficers
* Description: Display the people serving as an officer
*  Parameters: officerList - Array of class Officer in a single office
*     Returns: An HTML string
*****************************************************************************/
function showOfficers(officerList) {
    var html = '';
    var i;

    for (i = 0; i < officerList.length; i++) {
        html += officerList[i].name;

        if (i + 1 < officerList.length)
            html += '<br>';

    }

    return html;

}

/*****************************************************************************
*    Function: emailList
* Description: Build the list of emails
*  Parameters: ml - The memberlist returned by parseRecords('d', 'm', key)
*     Returns: A list of semi-colon separated email addresses
*****************************************************************************/
function emailList(ml)
{
    var emails = '';

    for (var i = 0; i < ml.length; i++)
    {
        if (ml[i].email.length > 0)
        {
            emails += ml[i].email + '; ';
        }
    }

    return emails.replace(/,/g, ';');

}

/*****************************************************************************
*    Function: showEmailList
* Description: Show or hide the email list
*  Parameters: id - The id of the element to display/hide
*     Returns: Nothing
*****************************************************************************/
function showEmailList(id)
{
    var emails = document.getElementById(id)
    var noDisplay = 'none';

    if (emails != null)
    {
        if (emails.style.display == noDisplay)
            emails.style.display = '';
        else
            emails.style.display = noDisplay;

    }

}

/*****************************************************************************
*    Function: showEmailList
* Description: Show or hide the email list
*  Parameters: id - The id of the element to display/hide
*              ml - The membership list
*     Returns: Nothing
*****************************************************************************/
function showEmailLink(id, ml)
{
    var button = '<span class=\'emailListButton\'><a href=\'javascript: showEmailList("' + id + '")\'>Email Addresses</a></span>';

    if (ml.length > 0)
        return button;
    else
        return '';

}
//-->

