/* ======================================================================

     JavaScript Source File

NAME: WorldSearch.js

AUTHOR: Denis Reilly

PURPOSE: Handles all World Interface functionality

====================================================================== */
// Instance variables
var ivSearchOptions		= new Array ("Name", "Number", "Keyword", "Postcode");
var ivSearchFunctions	= new Array ("A06110", "A06115", "A06120", "A06125");

var ivCountryTitles		= new Array ("United Kingdom",	"Netherlands",	"Belguim",	"France",	"Italy",	"Portugal",	"Austria",	"Germany",	"Ireland",	"Denmark",	"Spain",	"Poland",	"Czech Republic",	"Hungary",	"Canada",	"Japan",	"Slovenia",	"Romania",	"Slovakia",	"Sweden", "Bulgaria",	"Croatia",  "USA",   "Other Countries", "Other Countries");
var ivCountryDefaults	= new Array ("A06110",			"A06120",		"A06120",	"A06120",	"A06110",	"A06110",	"A06110",	"A06110",	"A06110",	"A06110",	"A06110",	"A06110",	"A06110",			"A06110",	"A06110",	"A06110", 	"A06110",	"A06110",	"A06110",	"A06110", "A06110",	    "A06110",   "A06110","A06110",	        "A06110" );
var ivCountryCodes 		= new Array ("044",				"031",			"032",		"033",		"039",		"351",		"043",		"049",		"353",		"045",		"034",		"048",		"042",				"036",		"124",		"081", 		"038",		"040",		"422",		"046",	  "359",	    "385",      "001",   "998",		        "999");

var ivSearchInputSize;
var ivSearchInputMaxLength;
var ivDefaultCountry	= "";
var ivDefaultSearch		= "";
var ivInitial 			= true;
var ivFlow 				= true;

var ivNameHelpText	= new Array ("You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You should enter the full name of the company, this will ensure a better match will be made. It is necessary to enter spaces in between each word or any punctuation.",
					"You do not need to type the full name as long as you specify the city.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is not necessary to enter spaces in between each word or any punctuation",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"You do not need to type the full name of the company, just enough to ensure a match will be made. It is necessary to enter spaces in between each word.",
					"Reports on all other countries are also available. For more information please <a href='javascript:document.contactUs.submit()'>contact us.</a>",
					"Reports on all other countries are also available. For more information please <a href='http://www.graydon-group.com/servlet/GraydonGroupServlet?&ACCESS=REDISPLAY&PAGEID=8' target='contactWin'>contact us.</a>"
					);
var ivNumberHelpText	= new Array (	"The number inserted must be 8 digits long, so you must add the prefix (such as SC,NI) and/or the appropriate number of zeros.",
					"Specify the companies national number and include any initial zeros.",
					"Specify companies 9 digit VAT number (Tax Number).",
					"Specify companies 9 digit SIRET number. Please note the comapy's VAT number incorporates the SIRET number. Use the last 9 digits.",
					"Specify companies 8 digit number. Add any initial zeros.",
					"This can only be used if you know the internal numbering system used by our Eurogate partner.",
					"This can only be used if you know the internal numbering system used by our Eurogate partner.",
					"This can only be used if you know the internal numbering system used by our Eurogate partner.",
					"Specify the companies national number and include any initial zeros.",
					"If known, enter the companies national number",
					"Specify the company's VAT number. This should be in a 9 character alpha-numeric format. e.g(A47000000)",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number.",
					"The number inserted must be the company's national number."
					);
var ivKeywordHelpText	= new Array (	"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword.",
					"For best results specify a city or town with your keyword."
					);
var ivPostcodeHelpText	= new Array ("The complete Postcode is advisable but not essential as the system will automatically list all the companies with postcodes which begin with or match the details entered.");

// This function is for initialisation
function init(defaultCountry, defaultSearch, defaultFlow) 
	{
		// create all the search options
		ivSearchInputSize 		= document.world.GSRCH.size;
		ivSearchInputMaxLength 	= document.world.GSRCH.maxLength;
		ivDefaultCountry		= defaultCountry;
		ivDefaultSearch			= defaultSearch;
		if (ivDefaultCountry == "") ivDefaultCountry = "044";
		if (ivDefaultSearch == "") ivDefaultSearch = "A06110";
		if (defaultFlow != null)
			ivFlow = defaultFlow;

		// set the default country
		for (var x = 0; x < document.world.GCCDE.length; x++)
			{
				if (document.world.GCCDE.options[x].value == ivDefaultCountry)
					{
						document.world.GCCDE.options[x].selected = true;
						break;
					}
			}

		// set the country
 	   	changeCountry();
	}

// This function changes the country defaults
function changeCountry() 
	{
		// get the value of the country selected, retrieve the country title object (layer)
		var mvSelectedIndex	= document.world.GCCDE.selectedIndex
		var mvCountryCode 	= document.world.GCCDE.options[mvSelectedIndex].value;
		var mvCountryTitle 	= document.getElementById("countryTitle");
		
		document.world.GFUNNO.disabled = 0;
		document.world.GSRCH.disabled = 0;
		document.world.GSRCH1.disabled = 0;
		if (document.world.GSUBMIT != null)
			document.world.GSUBMIT.disabled = 0;
		
		// set the text for the country title
		for (var i = 0; i < ivCountryCodes.length; i++)
			{
				if (ivCountryCodes[i] == mvCountryCode)
					break;
			}
		mvCountryTitle.innerHTML = ivCountryTitles[i];
		if (ivInitial == false)
			ivDefaultSearch = ivCountryDefaults[i];
		else
			ivInitial = false;

		// hardcoding to ristrict the search types for certain countries
		if (mvCountryCode == "044")
			buildSearch(4);
		else if (mvCountryCode == "034" || mvCountryCode == "045")
			buildSearch(2);
		else if (mvCountryCode == "124" || mvCountryCode == "046" || mvCountryCode == "001")
			buildSearch(1);
		else if (mvCountryCode == "998" || mvCountryCode == "999")
			{
				buildSearch(1);
				document.world.GFUNNO.disabled = 1;
				document.world.GSRCH.disabled = 1;
				document.world.GSRCH1.disabled = 1;
				document.world.GSUBMIT.disabled = 1;
				mvCountryTitle.innerHTML = "Other Countries";
				changeHelp();
				if (mvCountryCode == "998")
					document.contactUs.submit();
				return;
			}
		else
			buildSearch(3);

		// set the default search option
		for (var x = 0; x < document.world.GFUNNO.length; x++)
			{
				if (document.world.GFUNNO.options[x].value == ivDefaultSearch)
					{
						document.world.GFUNNO.options[x].selected = true;
						break;
					}
			}

		// change the search input 
		changeSearch();
	}


// This function changes the visible search options depending on search type
function changeSearch() 
	{
		var mvSearchInput		= document.getElementById("searchInput");
		var mvSearchInput1		= document.getElementById("searchInput1");
		var mvSearchTitle		= document.getElementById("searchTitle");
		var mvSearchTitle1		= document.getElementById("searchTitle1");
		var mvCountryCode 		= document.world.GCCDE.options[document.world.GCCDE.selectedIndex].value;
		var mvSelectedSearchValue	= document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value;
		mvSearchTitle.innerHTML 	= document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].text;
		if (mvCountryCode == "001")
			mvSearchTitle1.innerHTML	= "Zip";
		else
			mvSearchTitle1.innerHTML	= "City";

		// hardcoding to set the visible search titles and input
		if (mvSelectedSearchValue == "A06110")
		{
			document.world.GSRCH.size 	= ivSearchInputSize;
			document.world.GSRCH.maxLength 	= ivSearchInputMaxLength;
			if (mvCountryCode == "044" || mvCountryCode == "081")
				{
					mvSearchTitle1.style.visibility = "hidden";
					mvSearchInput1.style.visibility = "hidden";
					if (!ivFlow)
					{
						mvSearchTitle1.style.display 	= "none";
						mvSearchInput1.style.display 	= "none";
					}
				}
			else
				{
					mvSearchTitle1.style.visibility 	= "visible";
					mvSearchInput1.style.visibility 	= "visible";
					mvSearchTitle1.style.display 		= "inline";
					mvSearchInput1.style.display 		= "inline";
				}
		}
		else if (mvSelectedSearchValue == "A06115")
		{
			document.world.GSRCH.size 			= 15;
			document.world.GSRCH.maxLength 		= 21;
			mvSearchTitle1.style.visibility 	= "hidden";
			mvSearchInput1.style.visibility 	= "hidden";
			if (!ivFlow)
			{
				mvSearchTitle1.style.display 	= "none";
				mvSearchInput1.style.display 	= "none";
			}
		}
		else if (mvSelectedSearchValue == "A06120")
		{
			document.world.GSRCH.size 			= ivSearchInputSize;
			document.world.GSRCH.maxLength 		= ivSearchInputMaxLength;
			mvSearchTitle1.style.visibility 	= "visible";
			mvSearchInput1.style.visibility 	= "visible";
			mvSearchTitle1.style.display 		= "inline";
			mvSearchInput1.style.display 		= "inline";
		}
		else if (mvSelectedSearchValue == "A06125")
		{
			mvSearchTitle1.style.visibility 	= "hidden";
			mvSearchInput1.style.visibility 	= "hidden";
			document.world.GSRCH.size 			= 8;
			document.world.GSRCH.maxLength 		= 8;
			if (!ivFlow)
			{
				mvSearchTitle1.style.display 	= "none";
				mvSearchInput1.style.display 	= "none";
			}
		}

		//document.world.GSRCH.focus();
		// change the help text
		changeHelp();
	}

// This function changes to the help text for search types
function changeHelp() 
	{
		// get the value of the country selected, retrieve the country title object (layer)
		var mvSelectedIndex		= document.world.GCCDE.selectedIndex
		var mvCountryCode 		= document.world.GCCDE.options[mvSelectedIndex].value;
		var mvHelpText 			= document.getElementById("helpText");
		var mvSelectedSearchValue	= document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value;

		// set the text for the country title
		for (var i = 0; i < ivCountryCodes.length; i++)
			{
				if (ivCountryCodes[i] == mvCountryCode)
					break;

			}

		// depending on search type (Function no) show text
		if (mvSelectedSearchValue == "A06110") mvHelpText.innerHTML = ivNameHelpText[i];
		if (mvSelectedSearchValue == "A06120") mvHelpText.innerHTML = ivKeywordHelpText[i];
		if (mvSelectedSearchValue == "A06115") mvHelpText.innerHTML = ivNumberHelpText[i];
		if (mvSelectedSearchValue == "A06125") mvHelpText.innerHTML = ivPostcodeHelpText[i];
	}

// This function builds the search type options
function buildSearch(optionsLength) 
	{
		document.world.GFUNNO.options.length = 0;
		for (var i = 0; i < optionsLength; i++)
			{
				document.world.GFUNNO.options[document.world.GFUNNO.length] = new Option(ivSearchOptions[i], ivSearchFunctions[i], false, false);
			}
	}

// This function overrides the country defaults
function submitForm() 
	{
		// Check for UK and override
		var mvCountryCode = document.world.GCCDE.options[document.world.GCCDE.selectedIndex].value;
		var mvSelectedSearchValue = document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value;
		
		document.world.GFUNNO1.value = mvSelectedSearchValue;
		
		if (mvCountryCode == "001")
			{
				mvSelectedSearchValue = "A90060";
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
		if (mvCountryCode == "044")
			{
				mvSelectedSearchValue = "A060" + mvSelectedSearchValue.substr(4, 2);
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
		if (mvCountryCode == "046")
			{
				mvSelectedSearchValue = "A90070";
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
		if (mvCountryCode == "124")
			{
				mvSelectedSearchValue = "A90050";
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
		if (mvCountryCode == "041")
			{
				mvSelectedSearchValue = "A06311";
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
		if (mvCountryCode == "081")
			{
				if (mvSelectedSearchValue == "A06110")
					mvSelectedSearchValue = "A06312";
				else if (mvSelectedSearchValue == "A06115")
					mvSelectedSearchValue = "A06317";
				else if (mvSelectedSearchValue == "A06120")
					mvSelectedSearchValue = "A06322";
				document.world.GFUNNO.options[document.world.GFUNNO.selectedIndex].value = mvSelectedSearchValue;
			}
	}
 
// This function is for displaying sample reports
function SampleReport()
	{
		mvCountryId = document.euro.GCCDE.value;
		
		// hardcoding for Eurogate UK
		if (document.euro.GCCDE.value == "044") mvCountryId = "944";
		window.open("/Samples/Reports/" +  mvCountryId + "SampleReport.html", "SampleWin","top=50,flyout,toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=1,width=650,height=500"); 
	}	