/*
 *  All JavaScript and HTML on http://www.cutthroats.com is
 *  Copyright (C) 1996,1997 by Julius Cisek
 *   unless otherwise noted
 *
 *  author: Julius "Boa" Cisek
 *
 *  You are free to copy and use this code provided that this notice of
 *   copyright is included with EVERY copy
 */

// This file contains all functions and class definitions

function myround (number, places) {
//    alert ('before: ' + number + ', ' + places + '\n' +
//           'after:  ' + ((Math.round (number / places)) * places));
    return ((Math.floor (number / places)) * places);
}

function calcratio (flown, leave, missed, awol) {
    return (myround((flown / (flown + leave/8 + missed/2 + awol*2))*100, 1));
}

// will display an image that's in the js database.
//  if number == 0 then image will be random
//  if title == true then title is displayed
//  if detatils == true then description and copyright is displayed
//  moretag contains any special IMG tag extension stuff
function displayimage (number, title, details, moretag) {
    if (number == 0) {
        with (Math) {
            number = round (random () * (nCMCImage - 1) + 1);
        }
    }

    if (title || details)
        document.writeln ('<CENTER>');

    if (title) {
        if (CMCImage [number][dnci] != "")
            document.write ('<FONT SIZE="-1"><B>' + CMCImage [number][dnci]);

        if (CMCImage [number][auci] != "")
            document.write ('</B> by <B>' + CMCImage [number][auci]);

        document.writeln ('</B></FONT><BR>');
    }

    document.writeln ('<IMG src="' + CMCImage [number][fnci] + '" ALT="' + CMCImage [number][dnci] + '" ' + moretag + '>');

    if (details) {
        if (CMCImage [number][dsci] != "")
            document.writeln ('</CENTER><FONT SIZE="-1">' + CMCImage [number][dsci] + '</FONT>');

        if (CMCImage [number][cpci] != "")
            document.writeln('<BR><CENTER><FONT SIZE="-2">&copy;' + CMCImage [number][cpci] + '</FONT>');
    }

    if (title || details)
        document.writeln ('</CENTER>');
}

function displayMedalGif (medal, urlpart, gifpart) {
    document.write ('<a href="roster.html#' + urlpart +
        '" onMouseOver="top.status=\'Read about the ' + medal + '\'; return true;"' +
        ' onMouseOut="top.status=top.defaultStatus; return true;">' +
        '<img src="art/' + gifpart + '" height="40" width="24" hspace="1" vspace="" border="0" alt="' + medal +'" align="center"></a>');
}

function displayMedalSimple (medal) {
    if (medal == "Air Medal" || medal == "AirMedal")
        displayMedalGif ("Air Medal", "airmedal", "airmedal.gif");
    if (medal == "Purple Heart" || medal == "PurpleHeart")
        displayMedalGif ("Purple Heart", "pheart", "purplehe.gif");
    if (medal == "Distinguished Flying Cross" || medal == "DFC")
        displayMedalGif ("Distinguished Flying Cross", "dfc", "distingu.gif");
    if (medal == "Legion of Merit" || medal == "Legion")
        displayMedalGif ("Legion of Merit", "legion", "legionof.gif");
    if (medal == "Silver Star" || medal == "SilverStar")
        displayMedalGif ("Silver Star", "silverstar", "silverst.gif");
}

function displaySquad (section) {
    for (var i=0; i < CMCsquad[section].length; i++) {
        CMCsquad[section][i].displayLink();
        document.write ("<br>");
    }
}

function displayWingToc (wing) {
    document.write ('<center><font size="-1">');
    for (var i=0; i < CMCsquad [wing].length; i++) {
        document.write ('<a href="#' + CMCsquad [wing][i].tail + '"' +
            ' onMouseOver="top.status=\'Jump to 8=X ' + CMCsquad [wing][i].handle + '\'; return true;" onMouseOut="top.status=top.defaultStatus; return true;">' +
            CMCsquad [wing][i].handle + '</a>');
        if (i < CMCsquad [wing].length - 1)
            document.write (' - ');
    }
    document.write ('</font></center>');
}

function displayWing (wing) {
    for (var i=0; i < CMCsquad [wing].length; i++) {
        CMCsquad [wing][i].display ();
        document.write ('<br>');
    }
}

function displayOpResults (OA, title) {
    var cSuccess = 0;
	var cPartial = 0;
	var cFailed = 0;

	for (var i = 0; i < OA.length; i++) {
	    if (OA [i].result == "success")
		    cSuccess++;
		else if (OA [i].result == "partial")
		    cPartial++;
		else if (OA [i].result == "failure")
		    cFailed++;
	}

    var Ratio = myround ((cSuccess + cPartial * 0.5) / (cSuccess + cPartial + cFailed), 0.01);
    var szRatio = Ratio;
    

    document.writeln ("<br><br><b>" + title + "</b><br><ul>" + 
                      "<font size=-1>Completely successful operations: " + cSuccess + "<br>" +
	                  "Partially successful operations: " + cPartial + "<br>" +
					  "Failed operations: " + cFailed + "<br>" +
					  "<b>Total ops: " + (cSuccess + cPartial + cFailed) + "</b><br>" +
                                          "<i>Success/Failure ratio (partial counts as .5): " + szRatio + "</i><br>" +
                                          "</font></ul><br>");
}

function colorCountry (country) {
    if (country == "A")
        return "<FONT COLOR=\"#66FFFF\">A-land</FONT>";
    if (country == "B")
        return "<FONT COLOR=\"#FF0000\">B-land</FONT>";
    if (country == "C")
        return "<FONT COLOR=\"#33FF33\">C-land</FONT>";
    
    return "???";
}

function displayPilotLinkFromTail (tail) {
    for (var i=0; i < CMCroster.length; i++)
        if (tail == CMCroster [i].tail)
            CMCroster [i].displayLink();
}

function displayOpMedals (medals) {
    if (medals != "") {
        var medalRecipient;
        var medalName;

        for (var i=0; i < medals.length;) {
            var j = medals.indexOf (":", i);

            medalRecipient = medals.substring (i, j);

            i += ((j-i) + 1);

            j = medals.indexOf (",", j);
            if (j == -1)
                j = medals.length;

            medalName = medals.substring (i, j);

            i += ((j-i) + 1);

            displayMedalSimple (medalName);
            displayPilotLinkFromTail (medalRecipient);
            document.writeln (" ");
        }
    } else
        document.writeln ("none");
}

/*
 * DATABASE classes
 *
 * these are use for the pilot/op database
 *
 */

/*
 * class CMCop
 *  data:
 *   name = string name of op
 *   date = string Date
 *   result = string representing final outcome: "success" "partial" "failure"
 *   urlRibbon = URL of GIF ribbon
 *   urlOp = URL of op
 *   location = string where op was flown (what server)
 *   country = string what country was CMC
 *   target = string our target
 *   mission = string our mission
 *   awards = string medals/awards for this mission
 *   assistance = string assisting pilots/squads
 *   description = string description of mission
 *   present = string of concatenated tail numbers (3 digit)
 *   leave = string of concatenated tail numbers (3 digit)
 *   excused = string of concatenated tail numbers (3 digit)
 *   awol = string of concatenated tail numbers (3 digit)
 *  methods: 
 *   countRoster (property):
 *    property = string name of property to count: "present" "leave" "excused" "awol"
 *    returns number of pilots listed
 *   displayRibbon (small):
 *    small = if true, then normal size, otherwise size X2
 *    displays the ribbon for op and a link to the op
 *   displayLink ():
 *    displays the name of the op and a link to the detailed op
 *   link ():
 *    returns string that contains the link code
 */

function countRoster (property) {
    return this [property].length / 3;
};

function displayOpRibbon (small) {
    if (this.urlRibbon != null) {
        document.write ('<a href="' + this.urlMain + "#" + this.urlAnchor + '" onMouseOver="top.status = \'Read about Operation ' +
            this.name + '\'; return true;"' +
            'onMouseOut="top.status = top.defaultStatus; return true;">' +
            '<img src="ribbons/' + this.urlRibbon + '" alt="Operation ' + this.name + '"');
    
        if (small)
            document.write (' height="4" width="12" hspace="1" vspace="1" border="0"');
        else
            document.write (' height="8" width="24" hspace="4" vspace=4"');

        document.write ('></a>');
        return true;
    } else
        return false;
}

function getOpLink () {
    if (this.urlMain != null) {
        return '<a href="/' + this.urlMain + "#" + this.urlAnchor + '" onMouseOver="top.status = \'Read about Operation ' +
            this.name + '\'; return true;"' +
            'onMouseOut="top.status = top.defaultStatus; return true;" target="CMCMain">Operation ' +
            this.name + '</a>';
    } else {
        return 'Operation ' + this.name;
    }
}

function displayOpLink () {
    document.write (this.link());
}

function displayOp () {
    document.writeln ('<UL><TT>');
    document.write ("<a name=\"" + this.urlAnchor + "\"><FONT size=+1><b>" + this.name + "</b></FONT>");
    this.displayRibbon (true);
    document.writeln ('<br>' +
        '<table cellspacing=0 cellpadding=0 border="0">' + 
        '<tr><td><tt>Date: </tt></td><td><tt>' + this.date + '</tt></td></tr>' +
        '<tr><td><tt>Location: </tt></td><td><tt>' + this.location + '</tt></td></tr>' +
        '<tr><td><tt>Country: </tt></td><td><tt>' + colorCountry (this.country) + '</tt></td></tr>' +
        '<tr><td><tt>Target: </tt></td><td><tt>' + this.target + '</tt></td></tr>' +
        '<tr><td><tt>Mission: </tt></td><td><tt>' + this.mission + '</tt></td></tr>' +
        '<tr><td><tt>Results: </tt></td><td><tt>' + this.result + '</tt></td></tr>' +
        '<tr><td><tt>Awards: </tt></td><td><tt>');
    displayOpMedals (this.awards);
    document.writeln ('</tt></td></tr>' +
        '<tr><td><tt>Assistance: </tt></td><td><tt>' + this.assistance + '</tt></td></tr>' +
        '<tr><td><tt>Attendance: </tt></td><td><tt>Present: ' + this.countRoster ("present") +
            ', On Leave: ' + this.countRoster ("leave") +
            ', Excused: ' + this.countRoster ("excused") +
            ', AWOL: ' + this.countRoster ("awol") +
        '</table><br>' + this.description);
    document.writeln ('</TT></UL>');
}

function CMCop (name, date, result, urlRibbon, urlMain, urlAnchor, location, country, target, mission, awards, assistance, description, present, leave, excused, awol) {
    this.name = name;
    this.date = date;
    this.result = result;
    this.urlRibbon = urlRibbon;
    this.urlMain = urlMain;
    this.urlAnchor = urlAnchor;
    this.location = location;
    this.country = country;
    this.target = target;
    this.mission = mission;
    this.awards = awards;
    this.assistance = assistance;
    this.description = description;
    this.present = present;
    this.leave = leave;
    this.excused = excused;
    this.awol = awol;
    this.countRoster = countRoster;
    this.display = displayOp;
    this.displayRibbon = displayOpRibbon;
    this.displayLink = displayOpLink;
    this.link = getOpLink;
};

/*
 * Class CMCpilot
 *  data:
 *   cpid = string cpid
 *   handle = string handle
 *   tail = string tail number (MUST BE 3 digits ALWAYS)
 *   email = string email address
 *   urlHomepage = string url to homepage or null
 *   titles = string titles or null
 *   status = string status: "active" "reserve" "cadet" "gone" "leave"
 *   joined = string Date
 *   left = string Date (ignored unless status == "gone")
 *   wing = string wing (ignored unless status == "active" || "leave")
 *   rank = string rank
 *   ac = string favourite aircraft
 *   quote = string quote
 *   haward = string awards or null
 *   hrank = string history
 *   bounty = string bounty
 *   brights = string bragging rights or null
 *   description = string description
 *  methods:
 *   countAttendance (property)
 *    property = string name of property to count: "present" "leave" "excused" "awol"
 *    returns number of ops pilot appeared in this property
 *   attendanceOps (property)
 *    returns array of ops objects this pilot was [property] for
 *   display ()
 *    display everything about the pilot
 *   displayRibbons ()
 *    shows ribbons for missions this pilot flew
 *   displayMedals ()
 *    shows medals pilot received
 *   displayHeader()
 *    displays name with e-mail link plus rank and titles
 *   displayInsigniaAndDescription()
 *    shows rank insignia, ribbons, and medals in a table
 *   displayTable ()
 *    displays big table with complete pilot info
 *   displayLink ()
 *    displays rank and handle, linked to page
 *   displayOpsAttendance ()
 *    displays status at all ops
 *   link ()
 *    return string containing link
 */

function displayPilotAttendance (win, pilot) {
    for (var i = 0; i < CMCops.length; i++) {
        var found = false;

        win.document.write (
            '<tr>' +
            '<td><font size="-1">' + CMCops [i].date + '</font></td>' +
            '<td><font size="-1"><b>' + CMCops [i].name + '</b></font></b>' +
            '<td><font size="-1">');

        if (findPilot (pilot, "present", CMCops [i])) {
            win.document.writeln ('<FONT COLOR="#00CC00">present</FONT>');
            found = true;
        }

        if (findPilot (pilot, "leave", CMCops [i])) {
            win.document.writeln ('<FONT COLOR="#999900">on leave</FONT>');
            found = true;
        }

        if (findPilot (pilot, "excused", CMCops [i])) {
            win.document.writeln ('<FONT COLOR="#3333FF">excused</FONT>');
            found = true;
        }

        if (findPilot (pilot, "awol", CMCops [i])) {
            win.document.writeln ('<FONT COLOR="#CC0000">AWOL</FONT>');
            found = true;
        }

        if (!found)
            win.document.writeln ('<FONT COLOR="#999999">NO RECORD</FONT>');

        win.document.writeln ('</font></td></tr>');
    }
}

function displayPilotHistory (tail) {
    var win = window.open ("", "pilot" + tail, "width=400,height=400,toolbar=no,scrollbars=yes,menubar=no,status=no,directories=no,location=no");

    win.document.close(); win.document.open();

    win.document.write (
        '<BODY BGCOLOR="#000000" TEXT="#FFFFFF">' +
        '<SCRIPT language="JavaScript" SRC="javascript/jsinc.js"></SCRIPT>' +
        '<SCRIPT language="JavaScript" SRC="javascript/cmcpilotdb.js"></SCRIPT>' +
        '<SCRIPT language="JavaScript" SRC="javascript/cmcopsdb.js"></SCRIPT>');

    displayPilotHistoryWindow (win, tail);

    win.document.writeln ('</FONT></BODY>');
}
    
function displayPilotHistoryWindow (win, tail) {
    for (var i=0; i < CMCroster.length; i++) {
        if (CMCroster[i].tail == tail) {
            win.document.writeln ('<TABLE><TR><TD COLSPAN="3">' +
                '<FONT SIZE="-1"><b>' + CMCroster[i].handle +
                '\'s Squad Night Attendance</b><br>' +
                'NOTE: A NO RECORD signifies an op that was unofficial or ' +
                'perhaps the pilot was not yet with the CMC' +
               '<center><form><input type="button" value=" Close " onClick="self.close();"></form></center>' +
               '</TD></TR>');

            displayPilotAttendance (win, CMCroster [i]);

            win.document.writeln ('</table>');
            return;
        }
    }

    win.document.write ("<blink><b>INTERNAL ERROR</b></blink>");
}

function displayPilot () {
    document.writeln ('<HR SIZE=1 WIDTH="80%">');

    this.displayHeader ();

    document.writeln ('<br>');

    this.displayInsigniaAndDescription ();

    document.writeln ('<br>');

    // Participation history
    document.writeln (
        '<center><font size="-1"><b><a href="javascript:displayPilotHistory(\'' +
        this.tail + '\');" onMouseOver="top.status=\'See participation history for ' +
        this.handle + '\'; return true;" ' +
        'onMouseOut="top.status=top.defaultStatus; return true;">' +
        'Complete Participation History</a></b></font></center>');

    document.writeln ('<br>');

    this.displayTable ();

    if (this.quote != null)
	    document.writeln ('<font size=-1><br>Quote: \"<i>' + this.quote + '</i>\"<br></font>');

    if (this.ac != null)
	    document.writeln ('<font size=-1>Favourite Aircraft: \"<i>' + this.ac + '</i>\"<br></font>');

    if (this.urlHomepage != null)
        document.writeln ('<br><a href="' + this.urlHomepage + '" TARGET="OtherStuff">Visit ' + this.handle + '\'s homepage</a><br>');

    if (this.brights != null)
        document.writeln ('<br><font size="-1"><b>Bragging rights:</b> <br><ul>' + this.brights + '</ul></font>');

    document.writeln ('<br><center><font size="-1"><a href="#top" onMouseOver="top.status=\'Back to the top of this page\'; return true;" onMouseOut="top.status=top.defaultStatus; return true;">[back to the top]</a></font></center><br>');
}

function findPilot (pilot, property, op) {
    for (var i = 0; i < op[property].length; i+=3)
        if (op[property].substring (i, i+3) == pilot.tail)
            return true;

    return false;
};

function countAttendance (property, weighted) {
    var count = 0;
    var cRecordedOps = 0;
    var cTotalRecordedOps = 0;
    var RecordedOp = new Array ();

    if (weighted) {
        for (var i = 0; i < CMCops.length; i++) {
            if (findPilot (this, "present", CMCops [i])) {
                cTotalRecordedOps++;
                RecordedOp [i] = true;
            } else
                RecordedOp [i] = false;
        }
    }

    for (var i = 0; i < CMCops.length; i++) {
        if (weighted)
            cRecordedOps += RecordedOp [i] ? 1 : 0;

        if (findPilot (this, property, CMCops [i]))
            if (weighted)
                count+= myround ((cRecordedOps*1.5)/cTotalRecordedOps, 0.001);
            else
                count++;
    }

    return count;    
};

function flownOps (property) {
    var flown = new Array();
    var opi = 0;

    for (var i = 0; i < CMCops.length; i++)
        if (findPilot (this, property, CMCops [i]))
            flown [opi++] = CMCops [i];

    return flown;
}

function displayPilotRibbons () {
    var flown = this.flownOps ("present");
    var shown = 0;
    document.write ('<TABLE cellspacing=0 cellpadding=0 width="100%"><TR><TD><CENTER><pre>');
    for (var i = 0; i < flown.length; i++) {
        if (flown [i].displayRibbon(true)) {
            shown++;
            if (!(shown % 8))
                document.write ('<br>');
        }
    }
    document.write ('</pre></CENTER></TD></TR></TABLE>');
}

function displayMedal (medalName, medalDate) {
    var urlPic;
    var urlPage;
    var nameMedal;

    if (medalName == "AirMedal") {
        urlPic = "airmedal.gif";
        urlPage = "airmedal";
        nameMedal = "Air Medal";
    } else if (medalName == "PurpleHeart") {
        urlPic = "purplehe.gif";
        urlPage = "purpleheart";
        nameMedal = "Purple Heart";
    } else if (medalName == "DFC") {
        urlPic = "distingu.gif";
        urlPage = "dfc";
        nameMedal = "Distinguished Flying Cross";
    } else if (medalName == "Legion") {
        urlPic = "legionof.gif";
        urlPage = "legion";
        nameMedal = "Legion of Merit";
    } else if (medalName == "SilverStar") {
        urlPic = "silverst.gif";
        urlPage = "silverstar";
        nameMedal = "Silver Star";
    }

    document.writeln ('<TABLE cellspacing=0 cellpadding=0 width="100%"><TR><TD><CENTER>' +
        '<a href="roster.html#' + urlPage + '" onMouseOver="top.status=\'Read about the ' + nameMedal +'\'; return true;" onMouseOut="top.status=top.defaultStatus; return true;">' +
        '<img src="art/' + urlPic + '" height="40" width="24" hspace="" vspace="2" border="0" alt="' + nameMedal + '"></a><br>' +
        '<font size="-2">' + medalDate + '</font>' +
        '</CENTER></TD></TR></TABLE>');
}

function displayPilotMedals () {
    document.writeln ('<TABLE cellspacing=0 cellpadding=0 width="100%"><TR>');

    if (this.haward != null) {
        var medalDate;
        var medalName;

        for (var i=0; i < this.haward.length;) {
            var j = this.haward.indexOf (":", i);

            medalDate = this.haward.substring (i, j);

            i += ((j-i) + 1);

            j = this.haward.indexOf (" ", j);
            if (j == -1)
                j = this.haward.length;

            medalName = this.haward.substring (i, j);

            i += ((j-i) + 1);

            document.write ('<TD>');
            displayMedal (medalName, medalDate);
            document.write ('</TD>');
        }
    }

    document.writeln ('</TR></TABLE>');
}

function displayPilotHeader () {
    document.write ('<a name="' + this.tail + '">' +
        '<TABLE cellspacing=0 cellpadding=0 width="100%" border="0"><TR><TD>' +
        '<TT>[' + this.tail + ']</TT><FONT SIZE="+2">' + 
        '<a href="mailto:' + this.email + '?subject=8=X ' + this.handle + '" onMouseOver="top.status=\'E-mail 8=X ' + Rank (this.rank, false) + ' ' + this.handle + '\'; return true;" onMouseOut="top.status=top.defaultStatus; return true;">' +
        this.handle + '</a></FONT>' +
        '<FONT SIZE="+1"> - ' + Rank (this.rank, false) + '</FONT>');
    if (this.status == "reserve")
        document.write (' [Reserve] ');
    if (this.titles != null)
        document.write (' [' + this.titles + ']');
    document.write ('<BR></TD></TR></TABLE>');
}

function displayPilotInsigniaAndDescription () {
    var openHeader = '<CENTER><B><FONT SIZE=-1>';
    var closeHeader = '</FONT></B></CENTER>';
    var openData = '<FONT SIZE=-1>';
    var closeData = '</FONT>';

    // start insignia table
    document.write ('<TABLE cellspacing=0 cellpadding=0 width="100%" border="0">');

    // headers    
    document.write ('<TR>' +
        '<TD>' + openHeader + 'Rank' + closeHeader + '</TD>' +
        '<TD>' + openHeader + 'Mission Ribbons' + closeHeader + '</TD>' +
        '<TD>' + openHeader + 'Medals' + closeHeader + '</TD>' +
        '<TD>' + openHeader + 'Description' + closeHeader + '</TD>' +
        '</TR>');

    var urlRank = "i" + this.rank + ".gif";
    var nameRank = Rank (this.rank, false);

    // rank, ribbons, and medals
    document.write ('<TR>' +
        '<TD><CENTER><a href="roster.html#ranks" onMouseOver="top.status=\'Read about CMC Ranks\'; return true" onMouseOut="top.status=top.defaultStatus; return true;"><img src="rostmaps/' + urlRank + '" border="0" alt="' + nameRank + '"></a></CENTER></TD>' +
        '<TD>');
    this.displayRibbons();
    document.write ('</TD><TD>');
    this.displayMedals();

    document.write ('<TD>' + openData + this.description + closeData + '</TD>');

    // end insignia table
    document.write ('</TD></TR></TABLE>');
}

function displayPilotTable () {
    // header
    var openHeader = '<CENTER><B><FONT SIZE=-1>';
    var closeHeader = '</FONT></B></CENTER>';
    var openData = '<CENTER><FONT SIZE=-1>';
    var closeData = '</FONT></CENTER>';

    var present = this.countAttendance ("present");
    var leave = this.countAttendance ("leave");
    var leaveadj = this.countAttendance ("leave", true);
    var excused = this.countAttendance ("excused");
    var excusedadj = this.countAttendance ("excused", true);
    var awol = this.countAttendance ("awol");
    var awoladj = this.countAttendance ("awol", true);

    // start data table
    document.write ('<TABLE cellspacing=0 cellpadding=0 width="100%" border="0">');

    var withCMCDate;
    var withCMC;

    if (this.status == "gone") {
        withCMCDate = this.joined + '-' + this.left;
        withCMC = 'With CMC';
    } else {
        withCMCDate = this.joined;
        withCMC = 'CMC Since';
    }

    // first set of headers
    document.write ('<TR>' +
               '<TD>' + openHeader + withCMC + closeHeader + '</TD>' +
               '<TD>' + openHeader + 'Attended' + closeHeader + '</TD>' +
               '<TD>' + openHeader + 'on Leave' + closeHeader + '</TD>' +
               '<TD>' + openHeader + 'Missed' + closeHeader + '</TD>' +
               '<TD>' + openHeader + 'AWOL' + closeHeader + '</TD>' +
               '<TD>' + openHeader + 'Rating' + closeHeader + '</TD>' +
               '</TR>');

    // data
    document.write ('<TR>' +
               '<TD>' + openData + withCMCDate + closeData + '</TD>' +
               '<TD>' + openData + present + closeData + '</TD>' +
               '<TD>' + openData + leave + ' [' + leaveadj + ']' + closeData + '</TD>' +
               '<TD>' + openData + excused + ' [' + excusedadj + ']' + closeData + '</TD>' +
               '<TD>' + openData + awol + ' [' + awoladj + ']' + closeData + '</TD>' +
               '<TD>' + openData +
                calcratio (present, leave, excused, awol) +
                ' [' + calcratio (present, leaveadj, excusedadj, awoladj) + ']' + 
                closeData + '</TD>' +
               '</TR></TABLE><br>');

    // history headers
    document.write ('<TABLE cellspacing=0 cellpadding=0 width="100%" border="0"><TR>' +
               '<TD COLSPAN="3">' + openHeader + 'Promotion History' + closeHeader + '</TD>' +
               '<TD COLSPAN="3">' + openHeader + 'Loot from Bounties' + closeHeader + '</TD>' +
               '</TR>');

    // history data
    document.write ('<TR>' +
               '<TD COLSPAN="3">' + openData + this.hrank + closeData + '</TD>' +
               '<TD COLSPAN="3">' + openData + this.bounty + closeData + '</TD>' +
               '</TR></TABLE>');

    //other
    if (this.status == "leave")
        document.writeln ("<CENTER><FONT SIZE=+1><B>This pilot is currently on extended leave</B></FONT></CENTER><BR>");
}

function Rank (rank, abbv) {
    if (rank == "cadet") {
        if (abbv)
            return "Cadet";
        else
            return "Cadet";

    } else if (rank == "lt2") {
        if (abbv)
            return "Lt2";
        else
            return "2nd Lieutenant";

    } else if (rank == "lt1") {
        if (abbv)
            return "Lt1";
        else
            return "1st Lieutenant";

    } else if (rank == "cpt") {
        if (abbv)
            return "Cpt";
        else
            return "Captain";

    } else if (rank == "maj") {
        if (abbv)
            return "Maj";
        else
            return "Major";

    } else if (rank == "ltcol") {
        if (abbv)
            return "LtCol";
        else
            return "Lieutenant Colonel";

    } else if (rank == "col") {
        if (abbv)
            return "Col";
        else
            return "Colonel";

    } else if (rank == "bgen") {
        if (abbv)
            return "BGen";
        else
            return "Brigadier General";
    }
}

function getPilotLink() {
    var sLink = '<a href="/';

    if (this.status == "reserve" || this.status == "gone")
        sLink += 'reserve';
    else
        sLink += this.wing;

    sLink += '.html#' + this.tail + '" ' + 
        'TARGET = "CMCMain" onMouseOver = "top.status = \'Read about 8=X ' +
        Rank (this.rank, false) + ' ' + this.handle + '\'; return true;"' +
        'onMouseOut = "top.status = top.defaultStatus; return true;">' +
        this.handle + '</a>';

    return sLink;
}

function displayPilotLink () {
    document.writeln (this.link());
}

function CMCpilot (cpid, handle, tail, email, homepage, titles, status, joined, left, wing, rank, haward, hrank, bounty, brights, ac, quote, description) {
    this.cpid = cpid;
    this.handle = handle;
    this.tail = tail;
    this.email = email;
    this.urlHomepage = homepage;
    this.titles = titles;
    this.status = status;
    this.joined = joined;
    this.left = left;
    this.wing = wing;
    this.rank = rank;
    this.haward = haward;
    this.hrank = hrank;
    this.bounty = bounty;
    this.brights = brights;
    this.ac = ac;
    this.quote = quote;
    this.description = description;
    this.countAttendance = countAttendance;
    this.flownOps = flownOps;
    this.display = displayPilot;
    this.displayRibbons = displayPilotRibbons;
    this.displayMedals = displayPilotMedals;
    this.displayHeader = displayPilotHeader;
    this.displayInsigniaAndDescription = displayPilotInsigniaAndDescription;
    this.displayTable = displayPilotTable;
    this.displayLink = displayPilotLink;
    this.link = getPilotLink;
};


/*
 * Class objHistory
 *  data:
 *   date_yy: \
 *   date_mm:  - date of factoid (integer)
 *   date_dd: /
 *   date: date object
 *   description: string
 *  methods:
 */

function displayHistory (year, HA) {
    var displayme = new Array ();
    var j = 0;

    for (var i = 0; i < HA.length; i++)
        if (year == HA [i].date_yy)
            displayme [j++] = HA [i];

    document.writeln ('<dl compact><font size="-1">');

    for (var i = 0; i < displayme.length; i++)
        document.writeln ('<dt><b>' +
            displayme [i].date_mm + '/' +
            displayme [i].date_dd + '</b><dd>' +
            displayme [i].description);

    document.writeln ('</font></dl>');
}
            
function displayHistoryToday (HA, title) {
    var today = new Date ();
    var today_mm = today.getMonth () + 1;
    var today_dd = today.getDate ();
    var displayme = new Array ();
    var j = 0;
    var gotit = false;
    var istoday = "Today, ";
    var original = today_dd;
    var hitlow = false;
    var hithigh = false;
    var modifier = -1;

    while (!gotit) {
        for (var i = 0; i < HA.length; i++)
            if (today_mm == HA [i].date_mm && today_dd == HA [i].date_dd)
                displayme [j++] = HA [i];

        if (displayme.length != 0) {
            gotit = true;

            document.writeln ('<font size="-1"><center><b>' + istoday +
                today_mm + '/' + today_dd + ', in ' + title + ' history</b></center><br>');

            document.write ('<dl compact>');

            for (var i = 0; i < displayme.length; i++)
                displayme [i].display ();

            document.writeln ('</dl></font>');
        } else {
            today_dd = original + modifier;

            if (modifier > 0)
                modifier += 1;

            modifier = -modifier;

            if (today_dd == original)
                istoday = "Today, ";
            else if (today_dd == original - 1)
                istoday = "Yesterday, ";
            else if (today_dd == original + 1)
                istoday = "Tomorrow, ";
            else
                istoday = "On ";

            if (today_dd < 2)
                hitlow = true;

            if (today_dd > 30)
                hithigh = true;

            if (hitlow && hithigh) {           
                document.write ('<br>INTERNAL_ERROR: Please contact <a href="mailto:boa@cutthroats.com?Subject=Today in " + title + " History failed, out of bounds">Boa</a><br>');
                return;
            }
        }
    }
}

function displayHistoryDetail () {
    document.write ('<dt><b>19' + this.date_yy + '</b><dd>' + this.description);
}
        
function objHistory (date_yy, date_mm, date_dd, description) {
    this.date_yy = date_yy;
    this.date_mm = date_mm;
    this.date_dd = date_dd;
    this.description = description;
    this.display = displayHistoryDetail;
};
