// - - - Start - Configuration - Settings the Agency can modify - - - //
var Enable_Search_Within_This_Site = false;
var Enable_Search_Within_Agency = false;
var Results_Per_Page = 10;
var Search_Text_Box_Size = 15;
var Server_URL = "http://mysearch.internet.gov.sg/UIs/Decentralized.aspx";
var Server_HomePage = "http://mysearch.internet.gov.sg/";
var Banner = "";
var AdditionalDropDownItems = "";
var Catalogue = "";

// Specify additional drop down options using the following //
AdditionalDropDownItems =
[
	["Within Citizens & Residents", 'I80VNN4VY:AUDIENCE_CODES'],
	["Within Business", "I80Y14E8Z:AUDIENCE_CODES"],
	["Within Non-Residents", 'I80VOSC4S:AUDIENCE_CODES']
]

// Specify additional Catalgoue options using the following //
Catalogue =
[
]
// - - - End - Configuration - - - //


// - - - Start - Configuration - Debug Settings - - - //

var SplitResTry;
var URLStr = "http://www.gov.sg";

// - - - End - Configuration - - -//


function enableSearchWithInSite()
{
	return Enable_Search_Within_This_Site;
}

function enableSearchWithInAgency()
{
	return Enable_Search_Within_Agency;
}

function SetResultsPerPage()
{
	return Results_Per_Page;
}

var mtr = 0
function checkInputIsEmpty(){
	if (document.all.TextBoxSearch.value.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2') == "") {
		mtr = 1
	}
	else mtr = 0
}

function SetDDValue()
{
	var varCombo = "";
	var combo = document.forms[0].drop

	for ( i=0;i<combo.options.length;i++)
	{
		varCombo = varCombo + "^" + combo.options[i].text + "," + combo.options[i].value + ",";
		if (i == combo.options.selectedIndex)
			varCombo = varCombo + "SELECTED"
		var varTemp = ",";
		for(j=0;j<AdditionalDropDownItems.length;j++)
		{
			if (AdditionalDropDownItems[j][0] == combo.options[i].text)
			varTemp = "," + AdditionalDropDownItems[j][1];
		}
		varCombo = varCombo + varTemp;
		for(j=0;j<Catalogue.length;j++)
		{
			if (Catalogue[j][0] == combo.options[i].text)
				varTemp = "CATalogue";
		}
		varCombo = varCombo + varTemp;
	}
	document.forms[0].iNPUTcombo.value = varCombo;
}


/****************************************************************************************
* Name			: ShowResults
* Description	: Invoked by SubmitSearch(). All HTML tags that paint out the search are 
*				removed. <input type=text> changed to <input type=hidden> for TextBoxSearch
*				and drop
****************************************************************************************/
function ShowResults()
{
	//<form target> changed to _blank (NS compliant)
	document.writeln("<form method=post target='_blank' onsubmit='SetDDValue(); ' action='"+ Server_URL+ "' name=SearchPlugInfrm style='margin:0' >");
	document.writeln("<input type=hidden name=XMLFlag value=false >");
	document.writeln("<input type=hidden name=SearchSubmit value=Search>");
	//[ TextBoxSearch ] changed: type = hidden
	document.writeln("<input type=hidden name=TextBoxSearch size="+ Search_Text_Box_Size + "></td>");
	document.writeln("<input type=hidden valign='bottom' name=SearchGo id=SearchGo src='header-button-go.gif' value='Search' >");
	//[ drop ] changed: type = hidden
	document.writeln("<input type=hidden name=drop value=SearchInAllGovSites >");

	if( URLStr.match("https://"))
	{
		URLStr = URLstr.replace("https://","");
	}
	else if (URLStr.match("http://"))
	{
		URLStr = URLStr.replace("http://","");
	}
	SplitResTry = URLStr.split("/");
	SplitResTry = SplitResTry[0].split(":");
	document.writeln("<input id=ResultsPerPage type=hidden value=" + SetResultsPerPage() + " name=ResultsPerPage>");
	document.writeln("<input id=currentURL type=hidden value=" + SplitResTry + " name=currentURL>");
	document.writeln("<input id='iNPUTcombo' type=hidden name='iNPUTcombo' />");
	document.writeln("<input id=inputBanner type=hidden style='display:none' value=" + Banner + " name=inputBanner />");
	document.writeln("<input type=hidden value='DecentralizedInternet' name=EntryPage />");
	document.writeln("</form>");
}


/****************************************************************************************
* Name			: SearchPainter
* Description	: Execute when the page is loaded. Paints out the table containing the 
*				label "Search", the textbox "TextBoxSearch", the image "Go" button, the image 
*				"Search Home" button and the dropdown list box.
****************************************************************************************/
function SearchPainter()
{
	document.writeln("<table border=0 cellspacing=1 cellpadding=0> <tr><td><b>Search</b></td><td nowrap>");
	//[ TextBoxSearch ] inserted: onkeypress=JavaScript:SubmitSearchwtEnter()
	document.writeln("<input type=text name=TextBoxSearch size="+ Search_Text_Box_Size + " onkeypress=JavaScript:SubmitSearchwtEnter(event)></td>");
	document.writeln("<td  valign='bottom'>");
	//[ SearchGo ] inserted: onclick=JavaScript:SubmitSearch()
	document.writeln("<input type=image valign='bottom' name=SearchGo id=SearchGo src=" + Server_HomePage + "/images/header-button-go.gif value='Search' onclick=JavaScript:SubmitSearch(event)>");
	document.writeln("</td><td>");
    document.writeln("<a target=new href=" + Server_HomePage + "><img src=" + Server_HomePage + "/images/Dconlinesearch.gif  border=0 alt='Singapore Government Online Search Home'>");
	document.writeln("</td></tr><tr><td>&nbsp;</td><td colspan=3>");
	document.writeln("<SELECT id=drop name=drop>");
	document.writeln("<OPTION VALUE='SearchInAllGovSites' SELECTED>" + "Within All Singapore Gov Websites");

	if(enableSearchWithInSite())
	{
		document.writeln("<OPTION VALUE=SearchWithinThisSite>" + "Within This Website");
	}
	if(enableSearchWithInAgency())
	{
		document.writeln("<OPTION VALUE=SearchWithInAgency>" + "Within This Agency");
	}
	var t = / /g;
	if(AdditionalDropDownItems.length != 0)
	{
		for (i = 0; i < AdditionalDropDownItems.length; i++)
			document.writeln("<OPTION VALUE=" + AdditionalDropDownItems[i][0].replace(t, "") +">" + AdditionalDropDownItems[i][0]);
	}
	if (Catalogue != "")
	{
		for (i = 0; i < Catalogue.length; i++)
			document.writeln("<OPTION VALUE=" + Catalogue[i][1] +">" + Catalogue[i][0]);
	}
	document.writeln("</SELECT>");
	//[ iNPUTcombo ] value to be passed to SearchPlugInfrm
	document.writeln("<input id='iNPUTcombo' type=hidden name='iNPUTcombo' />");
	document.writeln("</td></tr></table>");
}


/****************************************************************************************
* Name			: SubmitSearch
* Description	: Execute when the SearchGo button is pressed. Value from the TextBoxSearch, 
*				drop and iNPUTcombo will be passed to the SGMS Search form. stopPropagation to
*				prevent	event from causing the main page to refresh.
****************************************************************************************/
function SubmitSearch(e)	
{
	document.SearchPlugInfrm.TextBoxSearch.value = document.Form1.TextBoxSearch.value;
	SetDDValue();
	document.SearchPlugInfrm.drop.value = document.Form1.drop.value;
	document.SearchPlugInfrm.iNPUTcombo.value = document.Form1.iNPUTcombo.value;

	if (navigator.userAgent.indexOf("MSIE") != -1)//IE
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	else//NS, Firefox
	{
		e.preventDefault();
		e.stopPropagation();
	}
	document.SearchPlugInfrm.submit();
}


/****************************************************************************************
* Name			: SubmitSearchwtEnter
* Description	: Execute when the Enter button is pressed. Invokes the SubmitSearch().
****************************************************************************************/
function SubmitSearchwtEnter(e)	
{
	if (!e) var e = window.event;
	if ((e.keyCode) && (e.keyCode == 13)) SubmitSearch();
	else if ((e.which) && (e.which == 13)) SubmitSearch();
	else return;
}