/* (c) Sam Gratrix */


// The Birth, Baptism, Marriage and Death classes have similar code, so often Cert will be uses as a generic
// reference to these four classes, mainly to aid diffing. Yes, I could be more clever about code reuse, but
// I think given the suble differences that crop up, diffing will do for now.


// ===========================================================================================================
// CertEntry and CertLook used to map a persons id to an entry in the CertSet and CertGroup. This is so that
// we can look up other people mentioned in the certificate.

function BaptismEntry(id, group, type)
{
	this.id    = id;
	this.group = group;
	this.type  = type;
}


// -----------------------------------------------------------------------------------------------------------

BaptismEntry.prototype.debug = function()
{
	return 'BaptismEntry: id = ' + this.id + ', group = ' + this.group + ', type = ' + this.type + '. ';
}


// ===========================================================================================================
// CertSet - Collection of Certs objects. Access functions take a person id and automatically do a lookup for
// the actual cert. Assumes that each person can only exist in the cert set once. Each entry in the group
// is an array so that mutiple Certs can be attributed to a persons id.

function BaptismSet()
{
  this.group_length = 0;
  this.group        = new Array();

  this.look_length  = 0;
  this.look         = new Array();

  for(var i=0; i<arguments.length; i++)
  {
    if(typeof(arguments[i]) != 'undefined')
    {
      // If a new id is enountered, then make a new group.

      if(typeof(this.group[arguments[i].id]) == 'undefined')
      {
        this.group[arguments[i].id] = new Array();

        this.group_length++;
      }

      // Add the object to the appropiate group.

      var g = this.group[arguments[i].id].push(arguments[i]) - 1;

      // Now create lookup information for fast querying.

      var people = arguments[i].people;

      if(people != '')
      {
        if(people.child.id != '')
        {
          if(typeof(this.look[people.child.id]) == 'undefined')
          {
            this.look[people.child.id] = new Array();

            this.look_length++;
          }

          this.look[people.child.id].push(new BaptismEntry(arguments[i].id, g, 0));
        };

        if(people.dad.id != '')
        {
          if(typeof(this.look[people.dad.id]) == 'undefined')
          {
            this.look[people.dad.id] = new Array();

            this.look_length++;
          }

          this.look[people.dad.id].push(new BaptismEntry(arguments[i].id, g, 1));
        };

        if(people.mum.id != '')
        {
          if(typeof(this.look[people.mum.id]) == 'undefined')
          {
            this.look[people.mum.id] = new Array();

            this.look_length++;
          }

          this.look[people.mum.id].push(new BaptismEntry(arguments[i].id, g, 2));
        };
      }
    }
  }
}


// -----------------------------------------------------------------------------------------------------------
// Returns a filtered and sorted CertLook object which tells you where the person is in the certificates.
// Use .length to determine if there are any results.
//
// To Do: Ordering.

BaptismSet.prototype.get = function(person_id, simple)
{
	var look = this.look[person_id];

	var filter = new Array();

	if(typeof(look) != 'undefined')
	{
		for(var i=0; i<look.length; i++)
		{
			if(simple && look[i].type != 0)
			{
				continue;
			}

			var add = 1;

			for(var j=0; j<filter.length; j++)
			{
				if( (filter[j].id != look[i].id) || (filter[j].group != look[i].group) )
				{
					continue;
				}

				add = 0;

				break;
			}

			if(add)
			{
				filter.push(look[i]);
			}
		}
	}

	return filter;
}


// -----------------------------------------------------------------------------------------------------------
// Display all the certificates related to the person.
//
// TO DO: Allow passing of filter control.

BaptismSet.prototype.display = function(person_id, simple)
{
	var filter = this.get(person_id, simple);

	var html = '';

	for(var i=0; i<filter.length; i++)
	{
		var entry = filter[i];

		var group = this.group[entry.id];

		var cert  = group[entry.group];

		html += cert.display(person_id);
	}

	delete filter;

	return html;
}


// -----------------------------------------------------------------------------------------------------------
// Get history data for use in the summary lines of the Person display.
//
// TO DO: Allow passing of filter control.

BaptismSet.prototype.history = function(person_id)
{
	var filter = this.get(person_id);

	var histroy = new Array();

	for(var i=0; i<filter.length; i++)
	{
		var entry  = filter[i];

		var cert   = this.group[entry.id][entry.group];

		histroy[i] = cert.history(entry.type);
	}

	delete filter;

	return histroy;
}


// -----------------------------------------------------------------------------------------------------------

BaptismSet.prototype.dump = function()
{
	var html = '';

	for(var id in this.group)
	{
		html += this.group[id].display();
	}

	return html;
}


// -----------------------------------------------------------------------------------------------------------

BaptismSet.prototype.report = function()
{
	return(this.group_length);
}


// ===========================================================================================================
// Cert

function Baptism(id, reference, event, people, note)
{
	this.id           = id;

	this.reference    = reference;
	this.event        = event;
	this.people       = people;
	this.note         = note;

	this.reference.id = this.id;
	this.event.id     = this.id;
	this.people.id    = this.id;
}


// -----------------------------------------------------------------------------------------------------------

Baptism.prototype.display = function(person_id)
{
	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%">';

	html += '<tr><td>' + this.reference.display(this.people.child.address.full()) + '</td></tr>';  // the data is not where i want to display it...
	html += '<tr><td>' + this.event.display() + '</td></tr>';
	html += '<tr><td>' + this.people.display(person_id) + '</td></tr>';

	if(this.note != '')
	{
		html += '<tr><td><table border="0" cellspacing="0" cellpadding="0" width="100%" class="certbaptism">';
		html += '<tr><th class="adr6">My Notes</th><td class="adr6" width="100%">' + this.note + '</td></tr></table></td></tr>';
	}

	html += '</table></td></tr></table>';

	return html;
}


// -----------------------------------------------------------------------------------------------------------

Baptism.prototype.history = function(person_type)
{
	if(person_type == 1 || person_type == 2)
	{
		     if(this.people.child.sex == 'M') var event = 'Son Bapt';
		else if(this.people.child.sex == 'F') var event = 'Dau Bapt';
		else                                  var event = 'Baby';
	}
	else
	{
		var event = 'Baptised';
	}

	switch(person_type)
	{
		case 0 : var histroy = new Array(
									   this.event.date,
									   event,
									   this.people.child.address.nice(),
									   this.people.child.first + ' ' + this.people.child.last,
									   '',
									   '',
									   '',
									   0,
									   'Child of ' + this.people.dad.first.split(' ')[0] + ' &amp; ' + this.people.mum.first.split(' ')[0],
									   '',
									   '<a>'); break;

		case 1 : var histroy = new Array(
									   this.event.date,
									   event,
									   this.people.child.address.nice(),
									   this.people.dad.first + ' ' + this.people.dad.last,
									   'Father',
									   '',
									   '',
									   0,
									   'Baptism of ' + this.people.child.first.split(' ')[0],
									   this.people.dad.occ,
									   '<a>'); break;

		case 2 : var histroy = new Array(
									   this.event.date,
									   event,
									   this.people.child.address.nice(),
									   this.people.mum.first + ' ' + this.people.mum.last + ' nee ' + this.people.mum.maiden,
									   'Mother',
									   '',
									   '',
									   0,
									   'Baptism of ' + this.people.child.first.split(' ')[0],
									   '',
									   '<a>'); break;
	}

	return histroy;
}


// ===========================================================================================================
// CertReferenceGroup - Typically a person can be found in both the local records and the general records.

function BaptismReferenceSet(lro, gro)
{
	this.id  = '';

	this.lro = lro;
	this.gro = gro;

	if(this.lro != '')
	{
		this.country = lro.country;
		this.year    = lro.year;
	}
	else if(this.gro != '')
	{
		this.country = gro.country;
		this.year    = gro.year;
	}
	else
	{
		this.country = '';
		this.year    = 0;
	}
}


// -----------------------------------------------------------------------------------------------------------

BaptismReferenceSet.prototype.display = function(address)
{
	var html = '';

	html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="certbaptism"><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 + ' Entry of Baptism</b></td>';
	html += '<th class="adr5">Source</th><td class="adr3nr" width="100%">';

	//if(this.lro != ''                  ) html += this.lro.display();
	//if(this.lro != '' && this.gro != '') html += '<br>';
	//if(                  this.gro != '') html += this.gro.display();

    html += '[todo]';

	html += '</td>';

	//if(tree_private == 1)
	//{
	//	html += '<td class="adr3"><img onclick="popcertbaptism(\'' + this.id + '\')" 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 += '</tr></table>';

	return html;
}


// ===========================================================================================================
// CertReference - Not all entries need to be use, whichever are appropriate to the LRO, GRO, etc.

function BaptismReference(type, source, year, country, office, subdistrict, register, county, district, vol, page, region, ref)
{
	this.id          = '';

	this.type        = type;
	this.source      = source;
	this.year        = year;
	this.country     = country;
	this.office      = office;
	this.subdistrict = subdistrict;
	this.register    = register;
	this.county      = county;
	this.district    = district;
	this.vol         = vol;
	this.page        = page;
	this.region      = region;
	this.ref         = ref;
}


// -----------------------------------------------------------------------------------------------------------

BaptismReference.prototype.display = function()
{
	var html = '';

	if(this.type        != '') html += this.type        + ' - ';
//	if(this.source      != '') html += this.source      + ' - ';
	if(this.year        != '') html += this.year        + ' - ';
	if(this.country     != '') html += this.country     + ' - ';
	if(this.office      != '') html += this.office      + ' - ';
	if(this.subdistrict != '') html += this.subdistrict + ' - ';
	if(this.register    != '') html += this.register    + ' - ';
	if(this.county      != '') html += this.county      + ' - ';
	if(this.district    != '') html += this.district    + ' - ';
	if(this.vol         != '') html += this.vol         + ' - ';
	if(this.page        != '') html += this.page        + ' - ';
	if(this.region      != '') html += this.region      + ' - ';
	if(this.ref         != '') html += this.ref         + ' - ';

    var l = html.length - 3; if(l < 0) len = 0;

	return html.substr(0, l);
}


// ===========================================================================================================
// CertLocation - This is just the stuff from the boxes at the top of the cert 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 cert location data it should be redervied from CertReference. CertLocation is just as it is
// transcribed.

function BaptismEvent(date, address, religion, act, baptiser, position)
{
	this.id   = '';
	this.year = 0;

	this.date     = date;
	this.address  = address;
	this.religion = religion;
	this.act      = act;
	this.baptiser = baptiser;
	this.position = position;
}


// -----------------------------------------------------------------------------------------------------------

BaptismEvent.prototype.display = function()
{
	var html  = '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="certbaptism"><tr valign="top">';

	html += '<th class="adr3">Date</th><td class="adr3">'+this.date.nice()+'</td>';
	html += '<th class="adr5">At</th><td class="adr3">'+this.address.nice()+'</td>';

	if(this.religion.length) html += '<th class="adr5">Religion</th><td class="adr3">'+this.religion+'</td>';
	if(this.act.length     ) html += '<th class="adr5">Act</th><td class="adr3">'+this.act+'</td>';
	if(this.baptiser.length) html += '<th class="adr5">By</th><td class="adr3">'+this.baptiser+'</td>';
	if(this.position.length) html += '<th class="adr5">Position</th><td class="adr3">'+this.position+'</td>';

	html += '</tr></table>';

	return html;
}


// ===========================================================================================================
// CertCol - As written on the certifficate, use for display on cert.

function BaptismChild(id, first, last, sex, date, address)
{
	this.id      = id;
	this.first   = first;
	this.last    = last;
	this.sex     = sex;
	this.date    = date;
	this.address = address;
}

function BaptismDad(id, first, last, occ)
{
	this.id      = id;
	this.first   = first;
	this.last    = last;
	this.occ     = occ;
}

function BaptismMum(id, first, last, maiden)
{
	this.id      = id;
	this.first   = first;
	this.last    = last;
	this.maiden  = maiden;
}


// ===========================================================================================================
// CertPeople - Collects the above.

function BaptismPeople(child, dad, mum)
{
	this.id      = '';

	this.child   = child;
	this.dad     = dad;
	this.mum     = mum;
}


// -----------------------------------------------------------------------------------------------------------

BaptismPeople.prototype.display = function(person_id)
{
	var html = '';

	html += '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="certbaptism"><tr valign="top">';
	html += '<th>Name';
	html += '<th class="adr4">Sex</th>';
	html += '<th class="adr4">Born</th>';
	html += '<th class="adr4">Abode</th>';
	html += '<th class="adr4">Father</th>';
	html += '<th class="adr4">Mother</th>';
	html += '<th class="adr4">Father\'s Occupation</th>';
	html += '<th class="adr4">Signature</th><tr>';

	var c0 = ''; var c1 = ''; if(person_id == this.child.id) { c0 = '<span class="certmatch">'; c1 = '</span>'; }
	var d0 = ''; var d1 = ''; if(person_id ==   this.dad.id) { d0 = '<span class="certmatch">'; d1 = '</span>'; }
	var m0 = ''; var m1 = ''; if(person_id ==   this.mum.id) { m0 = '<span class="certmatch">'; m1 = '</span>'; }
	var z = '--';

	var cn = this.child.first + ' ' + this.child.last;
	var dn =   this.dad.first + ' ' +   this.dad.last;
	var mn =   this.mum.first + ' ' +   this.mum.last; if(this.mum.maiden.length) mn += ' (' + this.mum.maiden + ' )';

	html += '<td><a href="#" onclick=\'paste_update("' + this.child.id + '");\' title="' + this.child.id + '">' + c0 + cn + c1 + '</td>';
	html += '<td class="adr4">' + c0 + this.child.sex + c1 + '</td>';
	html += '<td class="adr4">' + c0 + this.child.date.nice() + c1 + '</td>';
	html += '<td class="adr4">' + c0 + this.child.address.nice() + c1 + '</td>';
	html += '<td class="adr4"><a href="#" onclick=\'paste_update("' + this.dad.id + '");\' title="' + this.dad.id + '">' + d0 + dn + d1 + '</a></td>';
	html += '<td class="adr4"><a href="#" onclick=\'paste_update("' + this.mum.id + '");\' title="' + this.mum.id + '">' + m0 + mn + m1 + '</a></td>';
	html += '<td class="adr4">' + d0 + this.dad.occ + d1 + '</td>';
	html += '<td class="adr4">' + z + '</td>';
	html += '</tr></table>';

	return html;
}

