/* (c) Sam Gratrix */


// ===========================================================================================================
// CensusSet - Collection of Census objects. Access functions take a person id and automatically do a lookup
// for the actual census. Assumes that each person can only exist in the census set once.

function CensusSet()
{
    this.census_length = 0;
    this.census        = new Array();

    this.people_length = 0;
    this.people        = new Array();

    for(var i=0; i<arguments.length; i+=2)
    {
        if(typeof(arguments[i+1]) != 'undefined')
        {
            this.census[arguments[i]] = arguments[i+1];

            this.census_length++;
        }
    }

    for(var census_id in this.census)
    {
        var people = this.census[census_id].people.people;

        for(var person_index in people)
        {
            var person_id = people[person_index].person_id;

            if(person_id != '')
            {
                this.people[person_id] = new Array(census_id, person_index);

                this.people_length++;
            }
        }
    }
}


// -----------------------------------------------------------------------------------------------------------

CensusSet.prototype.has = function(person_id)
{
    return typeof(this.people[person_id]) != 'undefined';
}


// -----------------------------------------------------------------------------------------------------------

CensusSet.prototype.get = function(person_id)
{
    return this.census[this.people[person_id][0]];
}


// -----------------------------------------------------------------------------------------------------------
// Get person history - I should really push this stuff into lower classes, and also get rid of that switch
// statement! But it's 02:22 so I don't care...

CensusSet.prototype.history = function(person_id)
{
    var census_id    = this.people[person_id][0];
    var person_index = this.people[person_id][1];

    var census = this.census[census_id];
    var person = census.people.people[person_index];

    var d = 0;
    var m = 0;

    switch(census.year)
    {
        case 1841 : m = 6; d =  6; break;
        case 1851 : m = 3; d = 30; break;
        case 1861 : m = 4; d =  7; break;
        case 1871 : m = 4; d =  2; break;
        case 1881 : m = 4; d =  3; break;
        case 1891 : m = 4; d =  5; break;
        case 1901 : m = 3; d = 31; break;
    }

    return(new Array(
        new TDate(census.year, m, d),
        'Census',
        census.address.brief(),
        person.name,
        person.relation,
        person.married,
        person.age,
        '',
        'Born in ' + person.birth,
        person.occupation,
        '<a>'));
}


// -----------------------------------------------------------------------------------------------------------

CensusSet.prototype.display = function(person_id)
{
    if(typeof(this.people[person_id]) == 'undefined')
    {
        return '';
    }
    else
    {
        return this.census[this.people[person_id][0]].display();
    }
}


// -----------------------------------------------------------------------------------------------------------

CensusSet.prototype.dump = function()
{
    var html = '';

    for(var census_id in this.census)
    {
        html += this.census[census_id].display();
    }

    return html;
}


// -----------------------------------------------------------------------------------------------------------

CensusSet.prototype.report = function()
{
    return('Households = ' + this.census_length + '&nbsp;&nbsp;&nbsp;People = ' + this.people_length);
}


// ===========================================================================================================
// Census

function Census(id, year, reference, location, address, people, note)
{
    this.id             = id;     // id and
    this.year           = year;   // year unique

    this.reference      = reference;
    this.location       = location;
    this.address        = address;
    this.people         = people;
    this.note           = note;

    this.reference.id   = this.id;
    this.location.id    = this.id;
    this.people.id      = this.id;

    this.reference.year = this.year;
    this.location.year  = this.year;
    this.people.year    = this.year;
}


// -----------------------------------------------------------------------------------------------------------

Census.prototype.display = function()
{
    var html = '';

    html += '<table border="0" cellspacing="1" cellpadding="0" width="748" class="bor1"><tr><td>';
    html += '<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td>';

    html += this.reference.display(this.address.full());  // the data is not where i want to display it...
    html += this.location.display();
    html += this.people.display();

    if(this.note != '')
    {
      html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cenEng' + this.year + '">';
      html += '<tr><th class="adr6">My Notes</th><td class="adr6" width="100%">' + this.note + '</td></tr></table>';
    }

//  html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cenEng'+year+'">';
//  html += '<tr><th class="adr6">Test</th><td class="adr6" width="100%">' + this.address.full() + '</td></tr></table>';

    html += '</td></tr></table></td></tr></table>';

    return html;
}


// ===========================================================================================================
// CensusReference

function CensusReference(country, FHLfilm, RefNum, PROref, GROrefvol, piece, ed, folio, page, schedule)
{
    this.id        = '';
    this.year      = 0;

    this.country   = country;
    this.FHLfilm   = FHLfilm;
    this.RefNum    = RefNum;
    this.PROref    = PROref;
    this.GROrefvol = GROrefvol;
    this.piece     = piece;
    this.ed        = ed;
    this.folio     = folio;
    this.page      = page;
    this.schedule  = schedule;
}


// -----------------------------------------------------------------------------------------------------------

CensusReference.prototype.display = function(address)
{
    var html = '';

    html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cenEng' + this.year + '"><tr valign="center">';

    html += '<td valign="center" class="adr3x">';
    html += '<img border=0 width=23 height=15 align="absmiddle" src="../images/' + this.country + '.gif" alt="">';
    html += '<b> ' + this.year + ' Census</b></td>';

    html += '<th class="adr5">Source</th><td class="adr3" width="100%">';

    if(this.FHLfilm   != '') html += 'FHLFilm '              + this.FHLfilm   + ' - ';
    if(this.RefNum    != '') html += 'RefNum '               + this.RefNum    + ' - ';
    if(this.PROref    != '') html += 'PRORef '               + this.PROref    + ' - ';
    if(this.GROrefvol != '') html += 'GROrefvol '            + this.GROrefvol + ' - ';
    if(this.piece     != '') html += 'Piece '                + this.piece     + ' - ';
    if(this.ed        != '') html += 'Enumeration District ' + this.ed        + ' - ';
    if(this.folio     != '') html += 'Folio '                + this.folio     + ' - ';
    if(this.page      != '') html += 'Page '                 + this.page    ;
    if(this.schedule  != '') html += ' - Schedule '          + this.schedule;

    html += '</td>';

    if(tree_private == 1)
    {
      html += '<td class="adr3"><img onclick="popcensus(' + this.year + ', \'' + this.id + '\', '+1+')" border=0 width=20 height=16 src="../images/ff_image.gif" title="Open Image"></td>';
    }

    html += '<td class="adr3"><img onclick="map_search_mark_center(\'' + address + '\')" border=0 width=22 height=12 src="../images/os.gif" title="Add To Map"></td>';

//  html += '<td class="adr3"><img border=0 width=16 height=15 src="../images/people.gif" title="Go To Tree"></td>';

    html += '</tr></table>';

    return html;
}


// ===========================================================================================================
// CensusLocation - This is just the stuff from the boxes at the top of the census form. It is not really used
// for anything other than display. For the proper address of the person you should really use the Address
// class. For proper census location data it should be redervied from CensusReference. CensusData is just as
// it is transcribed, and since often the boxes were not filled in it's not much use!

function CensusLocation(data)
{
    this.id   = '';
    this.year = 0;

    this.data = data;
}


// -----------------------------------------------------------------------------------------------------------

CensusLocation.prototype.display = function()
{
    var html = '';

    html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cenEng' + this.year + '"><tr valign="top">';
    html += '<th class="adr0">' + this.data[0][0] + '</th><td class="adr0" width="33%">' + this.data[0][1] + '</td>';
    html += '<th class="adr4">' + this.data[1][0] + '</th><td class="adr0" width="33%">' + this.data[1][1] + '</td>';
    html += '<th class="adr4">' + this.data[2][0] + '</th><td class="adr0" width="33%">' + this.data[2][1] + '</td>';
    html += '</tr><tr valign="top">';
    html += '<th class="adr0">' + this.data[3][0] + '</th><td class="adr0" width="33%">' + this.data[3][1] + '</td>';
    html += '<th class="adr4">' + this.data[4][0] + '</th><td class="adr0" width="33%">' + this.data[4][1] + '</td>';
    html += '<th class="adr4">' + this.data[5][0] + '</th><td class="adr0" width="33%">' + this.data[5][1] + '</td>';
    html += '</tr><tr valign="top">';
    html += '<th class="adr3">' + this.data[6][0] + '</th><td class="adr3" width="33%">' + this.data[6][1] + '</td>';
    html += '<th class="adr5">' + this.data[7][0] + '</th><td class="adr3" width="33%">' + this.data[7][1] + '</td>';
    html += '<th class="adr2">' + this.data[8][0] + '</th><td class="adr1" width="33%">' + this.data[8][1] + '</td>';
    html += '</tr></table>';

    return html;
}


// ===========================================================================================================
// CensusPeople - I have chosen to index the people in the census by integer index rather than by person id
// because I do not always associate the people listed in a given census with an actual id. These people are
// effectively absent from the tree,  generally they will only appear when the census tables are displayed.
// There must exist one person with an id.

function CensusPeople(people)
{
    this.id         = '';
    this.year       = 0;

    this.people     = people;
    this.language   = false;
    this.disability = false;

    for(var person in this.people)
    {
        if(this.people[person].language   != '') this.language   = true;
        if(this.people[person].disability != '') this.disability = true;
    }
}


// -----------------------------------------------------------------------------------------------------------

CensusPeople.prototype.display = function()
{
    var html = '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cenEng' + this.year + '">';

    if(this.year == 1841)
    {
        html += '<tr><th>Name</th><th>Age</th><th>Sex</th><th>Born In County</th><th>Born Elsewhere</th><th>Birthplace</th><th>Occupation</th>';
    }
    else
    {
        html += '<tr><th>Name</th><th>Relation</th><th>Mar</th><th>Age</th><th>Sex</th><th>Birthplace</th><th>Occupation</th>';
    }

    if(this.language  ) html += '<th>Language</th>';
    if(this.disability) html += '<th>Disability</th>';

    html += '</tr>';

    for(var person in this.people)
    {
        html += this.people[person].display(this.year, this.language, this.disability);
    }

    html += '</table>';

    return html;
}


// ===========================================================================================================
// CensusPerson

function CensusPerson(person_id, name, relation, married, age, sex, born_in_county, born_elsewhere, birth, occupation, language, disability)
{
    this.person_id      = person_id;
    this.name           = name;
    this.relation       = relation;
    this.married        = married;
    this.age            = age;
    this.sex            = sex;
    this.born_in_county = born_in_county;
    this.born_elsewhere = born_elsewhere;
    this.birth          = birth;
    this.occupation     = occupation;
    this.language       = language;
    this.disability     = disability;
}


// -----------------------------------------------------------------------------------------------------------

CensusPerson.prototype.display = function(year, show_language, show_disability)
{
    var html = '';

    if(this.person_id == '')
    {
        html += '<tr><td>' + this.name + '</td>';
    }
    else
    {
        html += '<tr><td><a href="#" onclick="paste_update(\'' + this.person_id + '\');">' + this.name + '</a></td>';
    }

    if(year == 1841)
    {
        html += '<td>' + this.age + '</td><td>' + this.sex + '</td><td>' + this.born_in_county + '</td><td>' + this.born_elsewhere + '</td><td>' + this.birth + '</td><td>' + this.occupation + '</td>';
    }
    else
    {
        html += '<td>' + this.relation + '</td><td>' + this.married + '</td><td>' + this.age + '</td><td>' + this.sex + '</td><td>' + this.birth + '</td><td>' + this.occupation + '</td>';
    }

    if(show_language  ) html += '<td>' + this.language   + '</td>';
    if(show_disability) html += '<td>' + this.disability + '</td>';

    html += '</tr>';

    return html;
}

