// JavaScript Document

///////////////////////////////////////
////       Create XML Doc          ////
///////////////////////////////////////

function createXMLObject(){

 var xmlDoc;

        //Create the DOM object for IE

        if (window.ActiveXObject){

        //document.write("Is IE");

            var versions =["Msxml2.DOMDocument.6.0","Msxml2.DOMDocument.3.0"];

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

                try {

                    xmlDoc = new ActiveXObject(versions[i]);

                    return xmlDoc;

                }catch (error) {//do nothing here

                }

            }

        }else{

          xmlDoc = document.implementation.createDocument("","",null);

          return xmlDoc;



        }

        return null;

}

var xmlDoc = createXMLObject();

xmlDoc.async = false;







///////////////////////////////////////

////  Display Countries by Letter  ////

///////////////////////////////////////

function countries_byletter(n)

	{

	var countries = "ajax/get_countries_by_letter.php?letter="+n;

	xmlDoc.load(countries);

	display_countries();

	}

	

function display_countries()

	{

	var str = '';

	parent.document.getElementById("country_listing").innerHTML = '';

	var countryList = xmlDoc.getElementsByTagName("countries");

	

	if (countryList.length == 0)

		{

		parent.document.getElementById("country_listing").innerHTML = 'No countries starting with this letter exist.';

		}

	else

		{

		str += '<center>';

		str += '<table width="900" cellspacing="0" cellpadding="5">';

		var count = 0;



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

			{

			if ((count%5 == 0))

				{

				str += '<tr>';

				}

			var country_id = countryList[i].firstChild.firstChild.nodeValue;

			var country_name = countryList[i].firstChild.nextSibling.firstChild.nodeValue;

			str += '<td bgcolor="#5e6a5b" width="180" align="left"><a href="nation.html?country='+country_id+'">'+country_name+'</a></td>';

			

			if (i == countryList.length-1)

				{

				var extracolumns = (countryList.length%5);

				var columnstoadd = 5 - extracolumns;

				

				if ((columnstoadd != 5) && (columnstoadd != 0))

					{

					for (var j = 0; j < columnstoadd; j++)

						{

						str += '<td bgcolor="#5e6a5b" width="180"></td>';

						}

					}

				}

				

			if (count%5 == 4)

				{

				str += '</tr>';

				}

			count++;

			}

		

		str += '</table></center>';

		parent.document.getElementById("country_listing").innerHTML = str;

		}

	}

	

function countries_hide()

	{

	parent.document.getElementById("country_listing").innerHTML = '';	

	}







///////////////////////////////////////

////    Advanced Search Function   ////

///////////////////////////////////////

function advanced_search()

	{

	var country = parent.content.document.advanced_search.country.value;

	var issue = parent.content.document.advanced_search.issue.value;

	var denomination = parent.content.document.advanced_search.denomination.value;

	var pick = parent.content.document.advanced_search.pick.value;

	var year = parent.content.document.advanced_search.year.value;

	

	if ((country=='')&&(issue=='')&&(denomination=='')&&(pick=='')&&(year==''))

		{

		parent.content.document.getElementById("search_results").innerHTML = "<font class=\"alert\">You must enter are least one search criteria.</font><br /><br />";	

		}

	else

		{

		var adv_search = "ajax/advanced_search.php?country="+country+"&issue="+issue+"&denomination="+denomination+"&pick="+pick+"&year="+year;

		xmlDoc.load(adv_search);

		display_search();

		}

	}



function display_search()

	{

	var str = '';

	var notesList = xmlDoc.getElementsByTagName("notes");	

	if (notesList.length == 0)

		{

		parent.content.document.getElementById("search_results").innerHTML = "<font class=\"alert\">No notes matching your criteria in our records.</font><br /><br />";

		}

	else

		{

		parent.document.getElementById("search_results").innerHTML = '';

		str += "<center><br />";

		str += "<table width=\"600\" cellspacing=\"0\" cellpadding=\"3\"><tr><td class=\"lightgreen\"><center><font color=\"#55828d\"><strong><h2>Search Results</h2></strong></font></center></td></tr></table><br />";

		str += "<table width=\"550\" cellspacing=\"0\" cellpadding=\"3\">";

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

			{

			var id_note = notesList[i].firstChild.firstChild.nodeValue;

			var country = notesList[i].firstChild.nextSibling.firstChild.nodeValue;

			var pick = notesList[i].firstChild.nextSibling.nextSibling.firstChild.nodeValue;

			var year = notesList[i].firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;

			var denomination = notesList[i].firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;

			var currency = notesList[i].firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;

			var issue = notesList[i].firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;

			var countryname = notesList[i].firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;

			str += "<tr><td><a href=\"viewnote.html?banknote="+id_note+"\">"+countryname+" pick "+pick+" ("+Math.round(denomination)+" "+currency+", "+issue+" issue from "+year+")</a></td></tr>";

			}

		str += "</table>";

		str += "<br /><table width=\"600\" cellspacing=\"0\" cellpadding=\"5\"><tr><td class=\"lightgreen\"><center><font color=\"#55828d\"><strong><a href=\"#search\" onClick=\"parent.clearSearch()\">New Search</a></strong></font></center></td></tr></table><br />";

		str += "<center>";

		parent.document.getElementById("search_results").innerHTML = str;

		}

	}



function clearSearch()

	{

	parent.document.getElementById("search_results").innerHTML = '';	

	}
