//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
/* Pokemon core data and functions */

//--------------------------------------------------------------------------------
// software version

var version = "2.2 Lite";

//--------------------------------------------------------------------------------
// unique id counter - incremented each time a new row is added

var id_count = 0;

//--------------------------------------------------------------------------------
// checkbox counters - inc/dec each time checkboxes are clicked

var ck_count = {"pokemon":0, "trainer":0, "energy":0};

//--------------------------------------------------------------------------------
// data entry cell and node child numbers

var cell_num_ck    = 0;
var cell_num_qty   = 1;
var cell_num_name  = 2;
var cell_num_info  = 3;
var cell_num_card  = 3;
var cell_num_img   = 3;
var cell_num_note  = 3;
var child_num_ck   = 0;
var child_num_qty  = 0;
var child_num_name = 0;
var child_num_info = 0;
var child_num_card = 1;
var child_num_img  = 2;
var child_num_note = 3;

//--------------------------------------------------------------------------------
// pre-load rarity images

var R_image = new Image();  R_image.src = rarity_images_loc + "R.gif";
var U_image = new Image();  U_image.src = rarity_images_loc + "U.gif";
var C_image = new Image();  C_image.src = rarity_images_loc + "C.gif";

//--------------------------------------------------------------------------------
// pre-load event images

var No_Event_image  = new Image();  No_Event_image.src  = event_images_loc + "No_Event.png";
var BR_Autumn_image = new Image();  BR_Autumn_image.src = event_images_loc + "BR_Autumn.png";
var BR_Spring_image = new Image();  BR_Spring_image.src = event_images_loc + "BR_Spring.png";
var City_image      = new Image();  City_image.src      = event_images_loc + "City.png";
var State_image     = new Image();  State_image.src     = event_images_loc + "State.png";
var Regional_image  = new Image();  Regional_image.src  = event_images_loc + "Regional.png";
var National_image  = new Image();  National_image.src  = event_images_loc + "National.png";
var World_image     = new Image();  World_image.src     = event_images_loc + "World.png";
var OP_POP_image    = new Image();  OP_POP_image.src    = event_images_loc + "OP_POP.png";

//--------------------------------------------------------------------------------
// pre-load set symbol images (this is done in the data.js file)

//--------------------------------------------------------------------------------
// trim string function

function trim (str) {

	return str.replace (/^\s+|\s+$/g,"");

} //function

//--------------------------------------------------------------------------------
// provide a sort function to sort cards by name, then by order of declaration

function sort_cards (a, b) {

	if      (a[0]          < b[0])          return -1;
	else if (a[0]          > b[0])          return  1;
	else if (a[a.length-1] < b[b.length-1]) return -1;
	else if (a[a.length-1] > b[b.length-1]) return  1;
	else                                    return  0;

} //function

//--------------------------------------------------------------------------------
// combine the sets into one array for each type
// sort the cards (sort by card name, then by modified first - misc/newest first - ordered in data.js)

var ALL_cards_list = {"pokemon":[], "trainer":[], "energy":[]};

for (var type in ALL_cards_list) {

	for (var i=0;  i<ALL_sets_list.length;  i++)

		ALL_cards_list[type] = ALL_cards_list[type].concat (ALL_sets_list[i][type]);

	// add a sequence number to keep the arrays sorted by order of declaration (after sorted by name)
	for (var i=0;  i<ALL_cards_list[type].length;  i++)

		ALL_cards_list[type][i].push (i);

	ALL_cards_list[type].sort (sort_cards);

} //for

//--------------------------------------------------------------------------------
// create the suggestion lists

var aaSugg = {"pokemon":[], "trainer":[], "energy":[]};

for (var type in aaSugg)

	for (var i=0;  i<ALL_cards_list[type].length;  i++)

		aaSugg[type].push (sugg_item (ALL_cards_list[type][i]));

//--------------------------------------------------------------------------------
// create the actual array items for the suggestion

function sugg_item (card_data) {
var card   = card_data[0];
var num    = card_data[1];
var size   = card_data[2];
var rarity = card_data[3];
var set    = card_data[4];
var info   = card_data[5];
var note   = card_data[6];

	var font = 'font-size:10px; font-style:italic;';
	var sugg = card + (note || '');
	var sCardMatchFill = card;

	var sCardDisplay = '<div align="left" style="font-size:12px">' + card + '</div>';

	var sNumSet = '<div align="right" style="' + font + '">' + num + ((size=='') ? '' : '/') + size + '</div>';

	var sRarity = '<div align="center">';
	var sSymbol = '<div align="center">';
	if (show_rarity_set_images) {
		if (rarity != '')  sRarity += '<img src="' + rarity_images_loc + rarity + '.gif" />';
		else               sRarity += '&nbsp;';
		sSymbol += '<img src="' + card_images_loc + set + '/set_symbol.gif" />';
	}
	else {
		sRarity += '&nbsp;';
		sSymbol += set;
	} // else
	sRarity += '</div>';
	sSymbol += '</div>';

	// pad numbers with leading zeros
	if      (num < 10)   num = '00' + num;
	else if (num < 100)  num = '0'  + num;
	else;

	var sImg = '<div align="center">';
	var sImgSrc = card_images_loc + set + '/' + num + '.jpg';
	if (show_card_images)
		sImg += '<img src="' + sImgSrc + '" />';
	else
		sImg += '<!--src="' + sImgSrc + '"-->';
	sImg += '</div>';

	var sInfo = info;
	var sNote = note;
	var sCard = sCardMatchFill + '\t' + sInfo;

	return (new Array (sCardMatchFill, sCardDisplay, sNumSet, sRarity, sSymbol, sImg, sInfo, sNote, sCard, sImgSrc));

} //function

//--------------------------------------------------------------------------------
// create the auto-suggest objects

var objSugg = {
	"pokemon": new clsAutoSuggestControl ('pokemon', aaSugg.pokemon),
	"trainer": new clsAutoSuggestControl ('trainer', aaSugg.trainer),
	"energy" : new clsAutoSuggestControl ('energy' , aaSugg.energy )
} //objSugg array

//--------------------------------------------------------------------------------
// initialize the auto-suggest object (once during body.onload)

var objSugg_loaded = false;

function init_sugg () {

	// only run this once
	if (objSugg_loaded)  return;

	// set to true so this only runs once
	objSugg_loaded = true;

	// set the drop-down highlight colors
	objSugg['pokemon'].highlightColor = 'Pink';
	objSugg['trainer'].highlightColor = 'LightGreen';
	objSugg['energy' ].highlightColor = 'PowderBlue';

	// go thru each of the card types
	for (type in objSugg)
	with (objSugg[type]) {

		// hide the non-displayed items
		objHiddenItems[0] = true;  //sugg match text
		objHiddenItems[6] = true;  //info
		objHiddenItems[7] = true;  //note
		objHiddenItems[8] = true;  //complete card name/info
		objHiddenItems[9] = true;  //img src

		// set the height with some buffer room
		// limit the number of displayed items and suggestions
		iMinRowHeight       = (show_card_images ? 80 : (show_rarity_set_images ? 40 : 20));
		iMaxNumDisplayItems = (show_card_images ?  4 : (show_rarity_set_images ?  8 : 12));
		iMaxNumSuggestionItems = Math.min (16, 2*iMaxNumDisplayItems);

		// require at least 2 chars before suggestions occur
		iMinNumCharacters = 2;

		// set the index of the displayed suggestion
		iSuggestionDisplayIndex = 1;

		// widen the drop-down to extend over INFO field
		iWidenDropDown = 62;

		// don't widen when scrollbar appears
		bWidenScrollbar = false;

		// do this last after all the settings
		createDropDown();

	} //with

} //function

//--------------------------------------------------------------------------------
// connect the textbox/info to the auto-suggest control

function connect (objText, sType, ID) {

	var objNote = document.getElementById (ID + '_card_note');
	var objInfo = document.getElementById (ID + '_info');
	var objCard = document.getElementById (ID + '_card_hidden');
	var objImg  = document.getElementById (ID + '_card_img');

	objSugg[sType].connect (objText,
		[objInfo, 'value', 6, ''],
		[objNote, 'value', 7, ''],
		[objCard, 'value', 8, ''],
		[objImg,  'value', 9, '']);

} //function

//--------------------------------------------------------------------------------
// input box style

var input_style = 'display:inline; font-family:verdana,arial,helvetica; font-size:12px;';

//--------------------------------------------------------------------------------
// create the checkbox html

function ck_html (id) {

	return ('<input type="checkbox" id="' + id + '_ck" tabindex="-1"' +
		' onclick="click_ck_count(this)"' + 
		' style="' + input_style + '" />');

} //function

//--------------------------------------------------------------------------------
// create the qty input text box

function qty_input_html (id) {

	return ('<input type="text" style="' + input_style + ' width:40px;' +
		'" id="' + id + '_qty" value="" onchange="update_totals()" />');

} //function

//--------------------------------------------------------------------------------
// create the name input text box

function name_input_html (id, type) {

	return ('<input type="text" style="' + input_style + ' width:100%;"' +
		' id="' + id + '" value=""' +
		' onfocus="connect(this,\'' + type + '\',\'' + id + '\')" />');

} //function

//--------------------------------------------------------------------------------
// create the input text box for additional card display info

function info_html (id) {

	return ('<input type="text" style="' + input_style + ' width:60px;' +
		'" id="' + id + '_info" value="" />' +
		'<input type="hidden" id="' + id + '_card_hidden" value="" />' +
		'<input type="hidden" id="' + id + '_card_img" value="" />' +
		'<input type="hidden" id="' + id + '_card_note" value="" />');

} //function

//--------------------------------------------------------------------------------
// update the add/delete buttons depending on the given table id

function update_buttons (table) {

	// update all the buttons if no table was entered
	if (!table) {
		update_buttons ('pokemon');
		update_buttons ('trainer');
		update_buttons ('energy');
		return;
	} //if

	// get the checkbox count for the given table
	var count = ck_count[table];

	// get the button objects
	var obj_add_button = document.getElementById (table + '_add_row');
	var obj_del_button = document.getElementById (table + '_del_row');

	// update the button object labels
	obj_add_button.value = ((count==0) ? 'Voeg een rij toe'     : 'Voeg een rij toe');
	obj_del_button.value = ((count==0) ? 'Verwijder de laatste rij' : 'Verwijder de geselecteerde rij' + (count>1?'en':''));

} //function

//--------------------------------------------------------------------------------
// update the checkbox count when clicked

function click_ck_count (obj_ck) {

	// determine the table from the obj id
	var table = '';
	if      (obj_ck.id.search('pokemon')>=0)  table = 'pokemon'
	else if (obj_ck.id.search('trainer')>=0)  table = 'trainer'
	else if (obj_ck.id.search('energy') >=0)  table = 'energy'
	else                                      table = '';

	// increment or decrement the count
	if (obj_ck.checked)  ck_count[table]++;
	else                 ck_count[table]--;

	// update the appropriate add/delete buttons
	update_buttons (table);

} //function

//--------------------------------------------------------------------------------
// dynamically add a row to the give line of the given table

function add_row_at (row, table) {

	// get the table object
	var obj_table = document.getElementById (table + '_table');

	// create a new row object
	var obj_newRow = obj_table.insertRow (row+1);

	// create the object ID for the new row
	var id = table + (++id_count);

	// insert the checkbox cell
	var obj_ck = obj_newRow.insertCell(-1);
	obj_ck.innerHTML = ck_html (id);

	// insert the QTY cell
	var obj_qty = obj_newRow.insertCell(-1);
	obj_qty.innerHTML = qty_input_html (id);

	// insert the NAME cell and suggestion widget
	var obj_name = obj_newRow.insertCell(-1);
	obj_name.innerHTML = name_input_html (id, table);

	// insert the INFO cell
	var obj_info = obj_newRow.insertCell(-1);
	obj_info.innerHTML = info_html (id);

} // function

//--------------------------------------------------------------------------------
// dynamically add a row to the end of the given table

function add_row (table) {

	// get the table object
	var obj_table = document.getElementById (table + '_table');

	// insert a row at the first checked box
	var ck_flag = false;
	for (var i=1; i<obj_table.rows.length-2; i++) {
		var obj_ck = obj_table.rows[i+1].cells[cell_num_ck].childNodes[child_num_ck];
		if (obj_ck.checked) {
			ck_flag = true;
			add_row_at (i, table);
			break;
		} //if
	} //for

	// create a new row object
	if (!ck_flag)  add_row_at (obj_table.rows.length-2, table);

	// update the buttons
	update_buttons(table);

	// activate the DELETE ROW button
	var obj_del_button = document.getElementById (table + '_del_row');
	obj_del_button.disabled = false;

	// deactivate the ADD ROW button if needed
	if (obj_table.rows.length > 32)
	{
		var obj_add_button = document.getElementById (table + '_add_row');
		obj_add_button.disabled = true;
	} //if

} // function

//--------------------------------------------------------------------------------
// dynamically delete the given row in the given table

function del_row_at (row, table) {

	// get the table object
	var obj_table = document.getElementById (table + '_table');

	// delete the given row
	obj_table.deleteRow(row+1);

} // function

//--------------------------------------------------------------------------------
// dynamically delete a row at the end of the given table or checked rows

function del_row (table) {

	// get the table object
	var obj_table = document.getElementById (table + '_table');

	// delete all the rows that are checked, starting at the bottom
	var ck_flag = false;
	for (var i=obj_table.rows.length-3; i>0; i--) {
		var obj_ck = obj_table.rows[i+1].cells[cell_num_ck].childNodes[child_num_ck];
		if (obj_ck.checked) {
			ck_flag = true;
			del_row_at (i, table);
			ck_count[table]--;
		} //if
	} //for

	// delete the last row
	if (!ck_flag)  del_row_at (obj_table.rows.length-3, table);

	// update the totals and buttons
	update_totals();
	update_buttons(table);

	// activate the ADD ROW and INSERT ROW buttons
	var obj_add_button = document.getElementById (table + '_add_row');
	obj_add_button.disabled = false;

	// deactivate the DELETE ROW button if needed
	if (obj_table.rows.length < 4)
	{
		var obj_del_button = document.getElementById (table + '_del_row');
		obj_del_button.disabled = true;
	} //if

} // function

//--------------------------------------------------------------------------------
// get the total for the given table and update the display

function get_total(table) {

	// get the applicable total and table objects
	var obj_total      = document.getElementById(table + '_total');
	var obj_total_left = document.getElementById(table + '_total_left');
	var obj_table      = document.getElementById(table + '_table');

	// start with a null qty object
	var obj_qty = null;

	// initialize the total and qty
	var total = 0, qty = 0;

	// get the qty objects and add up the total
	for (var i=1; i<obj_table.rows.length-2; i++) {
		obj_qty = obj_table.rows[i+1].cells[cell_num_qty].childNodes[child_num_qty];
		qty = parseInt(obj_qty.value);
		total += (isNaN(qty) ? 0 : qty);
	} //for

	// update the total object display value
	obj_total.innerHTML      = (total==0 ? '' : total);
	obj_total_left.innerHTML = (total==0 ? '' : total);

	// return the total for this table
	return (total);

} //function

//--------------------------------------------------------------------------------
// update the checkbox counts

function update_ck_counters() {

	for (table in ck_count) {

		// get the applicable table object
		var obj_table = document.getElementById(table + '_table');

		// initialize the ck total
		ck_count[table] = 0;

		// get the ck objects and add up the ck count
		for (var i=1; i<obj_table.rows.length-2; i++) {
			obj_ck = obj_table.rows[i+1].cells[cell_num_ck].childNodes[child_num_ck];
			if (obj_ck.checked)  ck_count[table]++;
		} //for

	} //for

} //function

//--------------------------------------------------------------------------------
// update the totals

function update_totals() {

	// get the grand total object
	var obj_total = document.getElementById('deck_total');

	// add up the total for all tables
	var total = get_total('pokemon') + get_total('trainer') + get_total('energy');

	// update the grand total object display value
	obj_total.innerHTML = (total==0 ? '' : total);

} //function

//--------------------------------------------------------------------------------
// update the event logo

function update_logo (obj_event_name) {

	var obj_event_logo = document.getElementById ('event_logo');

	if (obj_event_name == null)
		obj_event_name = document.getElementById ('event_name');

	obj_event_logo.src = event_images_loc + obj_event_name.value;

} //function

//--------------------------------------------------------------------------------
// check the date and update the age division

function check_birthdate (obj_birthdateString) {

	var obj_birthdate = new Date();
	obj_birthdate.setTime (Date.parse (obj_birthdateString.value));
	var birthyear = obj_birthdate.getFullYear();
	var obj_age_division = document.getElementById("age_division");

	if      (birthyear >= JR_yr                     )  obj_age_division.value = "junior"
	else if (birthyear >= SR_yr && birthyear < JR_yr)  obj_age_division.value = "senior"
	else if (                      birthyear < SR_yr)  obj_age_division.value = "master"
	else                                               obj_age_division.value = ""

} //function

//--------------------------------------------------------------------------------
// update the form

function update_form () {

	init_sugg();
	update_totals();
	update_logo();
	update_ck_counters();
	update_buttons();

} //function

//--------------------------------------------------------------------------------
// reset the form

function reset_form () {

	if (!confirm ('Clear the current deck list?')) return;

	document.forms[0].reset();
	update_form();

} //function

//--------------------------------------------------------------------------------
// submit the form

function submit_form () {

	// do nothing - for Safari compatibility

} //function

//--------------------------------------------------------------------------------
// compare card names in order to determine rule-of-4 errors

function same_names (type, name1, name2) {

	// remove blanks
	name1 = (name1.replace (/ /g, '')).toUpperCase();
	name2 = (name2.replace (/ /g, '')).toUpperCase();

	switch (type) {

		case 'pokemon':

			// remove level x text
			name1 = name1.replace (/LV.X/g, '');
			name2 = name2.replace (/LV.X/g, '');

		case 'trainer':
		case 'energy':

			break;

		default:;

	} //switch

	return (name1 == name2);

} //function

//--------------------------------------------------------------------------------
// strip Basic/Special text on the energy card name

function strip_energy_type (card) {

	return (card.replace(/ - basic/gi, '').replace(/ - special/gi, ''));

} //function

//--------------------------------------------------------------------------------
// check the given card list for errors

function check_cardlist (type, cards) {

	var decklist_errors = '';

	// check for card errors (different rules for basic energy)
	for (var i=0;  i<cards.length;  i++) {

		var qty1  = cards[i].qty;
		var name1 = cards[i].name;
		var info1 = cards[i].info;
		var note1 = cards[i].note;

		var is_arceus = ((type == 'pokemon') && (name1.search(/arceus/gi) >= 0));
		var is_basic  = ((type == 'energy')  && (name1.search(/basic/gi)  >= 0));
		var ignore_4  = is_arceus || is_basic;

		// check for incomplete entry
		var entry = qty1 + name1 + info1;
		if (entry.replace(/ /gi,'') == '') continue;
		if (qty1.replace(/ /gi,'') == '' || name1.replace(/ /gi,'') == '' || (!is_basic && info1.replace(/ /gi,'') == '')) {
			decklist_errors += '<li>Incomplete ' + type + ' entry. <b>line&nbsp;#' + (i+1) + '</b></li>';
			continue;
		} //if

		// check for possible unknown cards caused by altering the suggestions
		var card1 = cards[i].card;
		if (card1.replace(/ /gi,'').toUpperCase() != (name1+'\t'+info1).replace(/ /gi,'').toUpperCase()) {
			decklist_errors += '<li>Card possibly not allowed in modified format. <b>' +
				name1 + ': ' + info1 + '</b></li>';
			continue;
		} //if

		// check for notes
		if (note1.replace (/ /gi,'') != '') {
			decklist_errors += '<li>Note: ' + note1 + ' <b>' + name1 + ': ' + info1 + '</b></li>';
		} //if

		// check for quantity errors
		var qty = parseInt(qty1);
		if      (isNaN(qty))              decklist_errors += '<li>Invalid quantity (not a number). <b>' + name1 + '</b></li>';
		else if (qty < 1)                 decklist_errors += '<li>Invalid quantity (too few). <b>'      + name1 + '</b></li>';
		else if ((qty > 4) && !ignore_4)  decklist_errors += '<li>Je mag maximum 4 <b>'             + name1 + '</b> spelen!</li>';
		else if (i==cards.length-1)  continue; // don't do any more checking the last time thru
		else {
			for (var j=i+1;  j<cards.length;  j++) {

				var qty2  = cards[j].qty;
				var name2 = cards[j].name;
				var info2 = cards[j].info;

				// check for duplicates
				if (same_names (type, name1, name2)) {
					var qty2 = parseInt(qty2);
					if (!isNaN(qty2))  qty += qty2;
					if (info1.replace(/ /gi,'') == info2.replace(/ /gi,''))
						decklist_errors += '<li>Duplicate ' + type + ' entry. <b>' + name1 + '</b></li>';
				} //if

			} //for j

			if ((qty > 4) && !ignore_4)  decklist_errors += '<li>Je mag maximum 4 <b>' + name1 + '</b> spelen!</li>';

		} //else

	} //for i
	return (decklist_errors);

} //function

//--------------------------------------------------------------------------------
// get the cards from the display and put into an array

function get_cards (type) {

	// get the table data
	var obj_table = document.getElementById (type + '_table');

	// initialize the card array
	var cards = new Array();

	// get the table content objects
	for (var i=1;  i<obj_table.rows.length-2;  i++) {

		var qty  = obj_table.rows[i+1].cells[cell_num_qty ].childNodes[child_num_qty ].value;
		var name = obj_table.rows[i+1].cells[cell_num_name].childNodes[child_num_name].value;
		var info = obj_table.rows[i+1].cells[cell_num_info].childNodes[child_num_info].value;
		var card = obj_table.rows[i+1].cells[cell_num_card].childNodes[child_num_card].value;
		var img  = obj_table.rows[i+1].cells[cell_num_img ].childNodes[child_num_img ].value;
		var note = obj_table.rows[i+1].cells[cell_num_note].childNodes[child_num_note].value;

		cards.push ({"qty":qty, "name":name, "info":info, "card":card, "img":img, "note":note});

	} //for

	return cards;

} //function

//--------------------------------------------------------------------------------
// create the given card list html

function cardlist_html (type, cards) {

	var html = '';

	// set the color and title based on the card type
	var color = '';
	var title = '';
	switch (type) {
		case 'pokemon':  title = 'Pokemon';                    color = 'red';    break;
		case 'trainer':  title = 'Trainer/Supporter/Stadium';  color = 'green';  break;
		case 'energy' :  title = 'Energie';                     color = 'blue';   break;
		default:;
	} //switch

	// safari needs "-" instead of "/"
	var is_safari  = ((navigator.vendor) && (navigator.vendor.indexOf('Apple') != -1)) ? true : false;
	if (is_safari && type=='trainer')  title = 'Trainer-Supporter-Stadium';

	// start the card list HTML (different for each type)
	if (type == 'pokemon')
		html += '<div><table style="width:100%; font-size:12px; border-collapse:collapse; border:solid 2px ' + color + ';">' +
			'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td align="center" colspan="3">' + title + '</td></tr>' +
			'<tr valign="center" style="font-size:12px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td style="width:10%; white-space:nowrap;" align="center">Aantal</td>' +
				'<td style="width:72%; white-space:nowrap;" align="left">&nbsp;&nbsp;Naam</td>' +
				'<td style="width:18%; white-space:nowrap;" align="center">Serie-#</td></tr>';

	else if (type == 'trainer')
		html += '<div><table style="width:100%; font-size:12px; border-collapse:collapse; border:solid 2px ' + color + ';">' +
			'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td align="center" colspan="2">' + title + '</td></tr>' +
			'<tr valign=center style="font-size:12px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td style="width:10%; white-space:nowrap;" align="center">Aantal</td>' +
				'<td style="width:90%; white-space:nowrap;" align="left">&nbsp;&nbsp;Naam</td></tr>';

	else  // energy
		html += '<div><table style="width:100%; font-size:12px; border-collapse:collapse; border:solid 2px ' + color + ';">' +
			'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td align="center" colspan="4">' + title + '</td></tr>' +
			'<tr valign=center style="font-size:12px; font-weight:bold; color:white; background-color:' + color + '">' +
				'<td style="width:10%; white-space:nowrap;" align="center">Aantal</td>' +
				'<td style="width:60%; white-space:nowrap;" align="left">&nbsp;&nbsp;Naam</td>' +
				'<td style="width:15%; white-space:nowrap; font-size:8px;" align="center">Basis</td>' +
				'<td style="width:15%; white-space:nowrap; font-size:8px;" align="center">Speciaal</td></tr>';

	// table cell style
	var style = 'border-style:solid; border-width:1px;';

	// get the table content objects (different for pokemon and energy), strip any ref. req. text, energy type
	for (var i=0; i<cards.length; i++) {

		var qty  = cards[i].qty;
		var name = cards[i].name;

		html += '<tr>' +
			'<td style="' + style + '" align="center">' + qty  + '</td>' +
			'<td style="' + style + '">&nbsp;' + strip_energy_type(name) + '</td>';

		if (type == 'pokemon') {
			var info = cards[i].info;
			html += '<td style="' + style + ' white-space:nowrap;">&nbsp;' + info + '</td>';
		} //if
		else if (type == 'energy') {
			html += '<td style="' + style + '" align="center">' + (name.search(/basic/i)   >= 0 ? '&radic;' : '') + '</td>';
			html += '<td style="' + style + '" align="center">' + (name.search(/special/i) >= 0 ? '&radic;' : '') + '</td>';
		} //else if
		else {}

		html += '</tr>';

	} //for

	// close the table
	html += '</table></div>';

	return (html);

} //function

//--------------------------------------------------------------------------------
// printview the decklist

function printview_deck () {

	var decklist_errors = '';

	// start the HTML document
	var html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';

	// create the HTML head
	html += '<html><head><title>' + document.title + ' - View Deck List</title></head><body>';

	// start the decklist HTML
	html += '<table align="center" style="width:680px; border-collapse:collapse; border-style:solid; border-width:1px; border-color:black; ' +
		'font-family:verdana,arial,helvetica;">';

	// start the left column
	html += '<tr><td id="left_column" align="center" valign="top" style="width:45%; padding:10px">';

	// get the event/player data
	var logo  = document.getElementById('event_logo');
	var name  = document.getElementById('player_name');
	var id    = document.getElementById('pop_id');
	var dob   = document.getElementById('dob');
	var age   = document.getElementById('age_division');

	// check for event/player errors
	if (name.value == '')  decklist_errors += '<li>Geen naam ingegeven.</li>';
	if (id.value == '')    decklist_errors += '<li>Geen POP ID ingegeven.</li>';
	if (dob.value == '')   decklist_errors += '<li>Geen geboortedatum ingegeven.</li>';
	if (age.value == '')   decklist_errors += '<li>Geen Leeftijds Categorie aangeduid.</li>';

	// check for mismatching birthdate and age division
	var obj_birthdate = new Date();
	obj_birthdate.setTime (Date.parse (dob.value));
	var birthyear = obj_birthdate.getFullYear();
	if (((birthyear >= JR_yr                     ) && (age.value != "junior")) ||
	    ((birthyear >= SR_yr && birthyear < JR_yr) && (age.value != "senior")) ||
	    ((                      birthyear < SR_yr) && (age.value != "master")))
		decklist_errors += '<li>Mismatching birthdate and age division.</li>';

	// create the event logo
	html += '<img src="' + logo.src + '" /><br /><br /><br />';

	// create the event/player HTML
	html += '<table style="width:100%; font-size:10px;">' +
		'<tr valign="top"><td width="1%" style="white-space:nowrap;"><b>Naam:</b></td><td style="font-size:14px;">'      + name.value + '</td></tr>' +
		'<tr valign="top"><td width="1%" style="white-space:nowrap;"><b>POP ID:</b></td><td style="font-size:14px;">'    + id.value   + '</td></tr>' +
		'<tr valign="top"><td width="1%" style="white-space:nowrap;"><b>Geboortedatum:</b></td><td style="font-size:14px;">' + dob.value  + '</td></tr>' +
		'<tr valign="top"><td width="1%" style="white-space:nowrap;"><b>Leeftijds Categorie:</b></td><td style="font-size:14px;">'  + age.options[age.selectedIndex].text + '</td></tr>' +
		'</table>';

	// get the card totals
	var total_pokemon = document.getElementById('pokemon_total').innerHTML;
	var total_trainer = document.getElementById('trainer_total').innerHTML;
	var total_energy  = document.getElementById('energy_total').innerHTML;
	var total_deck    = document.getElementById('deck_total').innerHTML;

	// check for card total errors
	if (isNaN (parseInt(total_pokemon)))  decklist_errors += '<li>Minimum 1 Pokemon is noodzakelijk!</li>';
	if (isNaN (parseInt(total_deck)))     decklist_errors += '<li>Je Deck moet uit 60 kaarten bestaan!</li>';
	else if (parseInt(total_deck) < 60)   decklist_errors += '<li>Je Deck moet uit 60 kaarten bestaan!</li>';
	else if (parseInt(total_deck) > 60)   decklist_errors += '<li>Je Deck moet uit 60 kaarten bestaan!</li>';
	else;

	// create the place-holder DIV for the decklist errors
	html += '<div id="decklist_errors"></div>';

	// start the right column
	html += '</td><td id="right_column" align="center" valign="top" style="width:55%; padding:10px">';

	// get the deck name
	var deck_name = document.getElementById('deck_name').value;

	// create the card list title
	html += '<div><table style="width:100%; font-size:12px; border-collapse:collapse; border:solid 2px orange;">' +
		'<tr style="font-size:16px; font-weight:bold; color:black; background-color:orange">' +
			'<td align="center">Deck Registratie Formulier</td></tr>' +
		'<tr><td align="center">' + (trim(deck_name)=='' ? '&nbsp;' : deck_name) + '</td></tr>' +
		'</table></div><br />';

	// get the cards to a new card array
	var cards_pokemon = get_cards ('pokemon');
	var cards_trainer = get_cards ('trainer');
	var cards_energy  = get_cards ('energy');

	// get all the cards
	html += cardlist_html ('pokemon', cards_pokemon) + '<br />';
	html += cardlist_html ('trainer', cards_trainer) + '<br />';
	html += cardlist_html ('energy',  cards_energy)  + '<br />';

	// check for card errors
	decklist_errors += check_cardlist ('pokemon', cards_pokemon);
	decklist_errors += check_cardlist ('trainer', cards_trainer);
	decklist_errors += check_cardlist ('energy',  cards_energy);

	// start the administrative notes HTML
	html += '<div><table width="100%" style="font-size:12px; border-collapse:collapse; border-style:solid; border-width:2px; border-color:black">' +
		'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:black">' +
			'<td align="center">Enkel voor Administratief gebruik</td></tr>';

	// display the notes
	var admin_notes = (document.getElementById('notes').value).replace(/  /g,'&nbsp; ');
	var admin_notes_lines = admin_notes.split(/\n/g);
	html += '<tr><td>';
	for (var i=0;  i<admin_notes_lines.length-1;  i++)  html += admin_notes_lines[i] + '<br />';
	for (var i=admin_notes_lines.length;  i<2;    i++)  html += '&nbsp;<br />';
	html += '&nbsp;</td></tr>';

	// close the table
	html += '</table></div>';

	// close the right column and decklist table
	html += '</td></tr>';
	html += '</table>';

	// insert the footnote
	html += footnote_html;

	// close the HTML and include the print dialog object
	html += '</body></html>';

	// open the window
	var obj_window = window.open ('about:blank','_blank',
		'top=10, left=10, height=640, width=800, resizable=yes, scrollbars=yes, ' +
		'location=no, titlebar=yes, toolbar=yes, menubar=yes');
	obj_window.document.open('text/html');
	obj_window.document.write(html);

	// list the card totals/errors
	if (decklist_errors == '')  decklist_errors = '<li>&radic; OK</li>';

	// show the card totals at the top of the decklist checklist
	decklist_errors = '<li>' +
		'<b>Deck:&nbsp;'   + ((total_deck    == '') ? '0' : total_deck)    + '</b>' +
		'; Pokemon:&nbsp;'  + ((total_pokemon == '') ? '0' : total_pokemon) +
		', Trainer:&nbsp;'  + ((total_trainer == '') ? '0' : total_trainer) +
		', Energie:&nbsp;'   + ((total_energy  == '') ? '0' : total_energy)  + '</li>' + decklist_errors;
	obj_window.document.getElementById('decklist_errors').innerHTML =
		'<br /><br /><br /><div align=left style="font-family:verdana,arial,helvetica; font-size:10px;">' +
		'<span style="font-size:12px"><b><u>Decklist Controle:</u></b></span>' +
		'<ul style="list-style-type:circle; margin:10; padding:5;">' + decklist_errors + '</ul></div>';

	// close the window
	obj_window.document.close();
	obj_window.focus();

} //function

//--------------------------------------------------------------------------------
// create the menu buttons

function create_menu (vertical) {

	// create the menu div
	document.write ('<div id="menu_div" align="center" style="font-size:12px; ' +
		'border:1px solid black; padding:5px; background-color:#FAFAD2;">');

	// create the event code for mouse hovering over the menu text/links
	var hover_code =
		' onmouseover="this.style.textDecoration=\'underline\'"' +
		' onmouseout ="this.style.textDecoration=\'none\'"';

	// create the menu items
	var separator = (vertical ? '<br />' : '&nbsp;&nbsp;&nbsp;');
	document.write (
		separator + '<input type="button" tabindex="-1" value="Alles wissen" style="width:100px" onclick="javascript:reset_form()" />'     + separator +
		separator + '<input type="button" tabindex="-1" value="Decklist tonen"  style="width:100px" onclick="javascript:printview_deck()" />' + separator +
		(vertical ? separator : ''));

	// close the menu div
	document.write ('</div>');

} //function

//--------------------------------------------------------------------------------
// create the event entry html

function create_event_entry () {

	// create the event entry div/table
	document.write (
		'<div id="event_div">' +
		'<table id="event_table" style="width:100%;">');

	// create the default event image
	document.write (
		'<tr><td align="center"><img id="event_logo" src="' + event_images_loc + 'No_Event.png" /></td></tr>');

	// create the event list
	document.write (
		'<tr><td align="left" style="font-size:10px; font-weight:bold">' +
			'Type Toernooi:<br />' +
			'<select id="event_name" style="width:100%"' +
				' onchange="update_logo(this)">' +
				'<option value="No_Event.png" selected="selected"></option>' +
				'<option value="BR_Autumn.png">Battle Roads - Autumn</option>' +
				'<option value="BR_Spring.png">Battle Roads - Spring</option>' +
				'<option value="City.png">City Championship</option>' +
				'<option value="State.png">State Championship</option>' +
				'<option value="Regional.png">Regional Championship</option>' +
				'<option value="National.png">National Championship</option>' +
				'<option value="World.png">World Championship</option>' +
				'<option value="OP_POP.png">Andere Toernooien</option>' +
			'</select>' +
		'</td></tr>');

	// close the event entry div/table
	document.write ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the player info entry html

function create_player_entry () {

	// start the player entry div/table
	document.write (
		'<div id="player_div">' +
		'<table id="player_table" style="width:100%;">');

	// create the player name, ID, DOB, age division input fields
	document.write (
		'<tr><td align="left" style="font-size:10px; font-weight:bold">' +
			'Naam:<br /><input type="text" id="player_name" style="width:100%" value="" />' +
				'<div id="div_unowns" style="display:none;"></div><br /><br />' +
			'POP ID:<br /><input type="text" id="pop_id" style="width:100%" value="" /><br /><br />' +
			'Geboorte datum:<br /><input type="text" id="dob" style="width:100%" value=""' +
				' onchange="check_birthdate(this)" /><br /><br />' +
			'Leeftijds Categorie:<br />' +
				'<select id="age_division" style="width:100%">' +
					'<option value=""></option>' +
					'<option value="junior">Junior</option>' +
					'<option value="senior">Senior</option>' +
					'<option value="master">Master</option>' +
				'</select>' +
			'</td></tr>');

	// close the player entry div/table
	document.write ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the totals summary

function create_totals_summary () {

	// start the totals div/table
	document.write (
		'<div id="totals_div">' +
		'<table id="totals_table" align="center" style="font-size:12px; border:1px solid black; background-color:#FFFF66;">');

	// create the totals area
	document.write (
		'<tr><td>Pokemon:&nbsp;&nbsp;</td><td align="right"><span id="pokemon_total_left"></span></td></tr>' +
		'<tr><td>Trainer/Supp/Stad:&nbsp;&nbsp;</td><td align="right"><span id="trainer_total_left"></span></td></tr>' +
		'<tr><td>Energy:&nbsp;&nbsp;</td><td align="right"><span id="energy_total_left"></span></td></tr>' +
		'<tr><td colspan="2"><hr /></td></tr>' +
		'<tr style="font-weight:bold;"><td>Totaal aantal kaarten:&nbsp;&nbsp;</td><td align="right"><span id="deck_total"></span></td></tr>');

	// close the totals div/table
	document.write ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the deck title and deck name html

function create_deck_entry () {

	// start the title/name div/table
	document.write (
		'<div id="title_div">' +
		'<table id="title_table" style="width:100%; border-collapse:collapse; border:2px solid orange; background-color:white;">');

	// create the title banner and deck name entry
	document.write (
		'<tr valign="center" style="font-size:16px; font-weight:bold; color:black; background-color:orange;">' +
		'<td align="center">Deck Registratie Formulier</td></tr>' +
		'<tr><td><input type="text" id="deck_name" value="" style="width:100%;" /></td></tr>');

	// close the deck title/name div/table
	document.write ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the card entry html of the given card type

function create_card_entry (type, num_rows) {

	// set the color and title based on the card type
	var color = '';
	var title = '';
	switch (type) {
		case 'pokemon':  title = "Pokemon";                    color = "red";    break;
		case 'trainer':  title = "Trainer/Supporter/Stadium";  color = "green";  break;
		case 'energy' :  title = "Energie";                     color = "blue";   break;
		default:;
	} //switch

	// create the div and table headings
	document.write (
		'<div id="' + type + '_div">' +
		'<table id="' + type + '_table" style="width:100%; border-collapse:collapse; border:2px solid ' + color + '; background-color:white;">');
	document.write (
		'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:' + color + ';">' +
		'<td align="center" colspan=4>' + title + '</td></tr>');
	document.write (
		'<tr valign="center" style="font-size:12px; font-weight:bold; color:white; background-color:' + color + ';">' +
		'<td></td>' +
		'<td align="center" style="width:40px">Aantal</td>' +
		'<td style="width:99%">&nbsp;&nbsp;Naam</td>' +
		'<td align="center" style="width:60px">Serie - #</td></tr>');

	// create the card entry rows
	for (var i=1; i<=num_rows; i++) {

		// create the row html
		var id = type + (++id_count);
		document.write ('<tr><td>' +
			ck_html (id) + '</td><td>' +
			qty_input_html (id) + '</td><td>' +
			name_input_html (id, type) + '</td><td>' +
			info_html (id) + '</td></tr>');

	} //for

	// create the card total html and add/delete buttons
	document.write (
		'<tr align="center" style="font-size:16px; font-weight:bold; color:white; background-color:' + color + '">' +
		'<td></td><td><span id="' + type + '_total"></span></td><td colspan=2>' +
		'<input type="button" id="' + type + '_add_row" tabindex="-1" value="Add New Row"' +
			' onclick="add_row(\'' + type + '\')" />' +
		'&nbsp;&nbsp;' +
		'<input type="button" id="' + type + '_del_row" tabindex="-1" value="Delete Last Row"' +
			' onclick="del_row(\'' + type + '\')" />' +
		'</td></tr>');

	// close the table/div
	document.writeln ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the note entry html for admin use only

function create_note_entry (num_rows) {

	// create the notes div/table
	document.write (
		'<div id="notes_div">' +
		'<table id="notes_table" style="width:100%; border-collapse:collapse; border:2px solid black; background-color:white;">');

	// create the admin notes
	document.write (
		'<tr valign="center" style="font-size:16px; font-weight:bold; color:white; background-color:black">' +
			'<td align="center">Enkel voor Administratief gebruik</td></tr>' +
		'<tr><td><textarea id="notes" rows=' + num_rows + ' style="width:100%"></textarea></td></tr>');

	// close the notes div/table
	document.write ('</table></div>');

} //function

//--------------------------------------------------------------------------------
// create the entire decklist html from the given player-supplied data

function create_decklist_entry() {

	// start the form and decklist table
	document.write (
		'<div id="pokemon_deck_list_div">' +
		'<form id="pokemon_deck_list_form" action="javascript:submit_form()">' +
		'<table id="pokemon_deck_list_table" align="center"' +
		' style="width:750px; border-collapse:collapse; border:2px solid black; background-color:white;">');

	// create the top menu row
	document.write ('<tr><td colspan="2">');
	create_menu (false);  //horizontal
	document.write ('</td></tr>');

	// start the decklist row and left column
	document.write ('<tr><td valign="top" style="width:35%; padding:10px;">');

	// create all the left column displays
	create_event_entry();     document.write ('<br \>');
	create_player_entry();    document.write ('<br \><br \><br \>');
	create_totals_summary();  document.write ('<br \><br \><br \>');
	create_menu (true);  //vertical

	// close the left column
	document.write ('</td>');

	// start the right column
	document.write ('<td valign="top" style="width:65%; padding:10px;">');

	// create all the right column displays
	create_deck_entry ();               document.write ('<br \>');
	create_card_entry ('pokemon', 12);  document.write ('<br \>');
	create_card_entry ('trainer', 12);  document.write ('<br \>');
	create_card_entry ('energy',   6);  document.write ('<br \>');
	create_note_entry (3);

	// close the right column and decklist row
	document.write ('</td></tr>');

	// close the decklist table and form
	document.write ('</table></form></div>');

} //function
