String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
function submit_directory_onclick()
{
	if(document.dirForm.drpCommunity.value != "-1")
	{
		document.dirForm.hidAction.value = "submit_community";
		document.dirForm.submit();
	}
	else
	{
		alert("Please pick a community.");
	}
}
function submit_keyword_onclick()
{
	if(document.dirForm.txtSearchText.value.trim() != "")
	{
		document.dirForm.hidAction.value = "submit_search";
		document.dirForm.submit();
	}
	else
	{
		alert("Please enter a keyword.");
	}
}
function prev_onclick()
{
	document.dirForm.hidPrevNext.value = "prev";
	document.dirForm.submit();
}
function next_onclick()
{
	document.dirForm.hidPrevNext.value = "next";
	document.dirForm.submit();
}
function full_onclick()
{
	document.dirForm.hidPrevNext.value = "full";
	document.dirForm.submit();
}
	
function followLink(link,target,prms) {
	var params = prms;
	if (document.all) {
		if (target == "new") {
			var win = window.open(link,"",params);
		} else window.location.href = link;
	} else {
		if (document.layers) {
			if (target == "new") {
				var win = "javascript:window.open(link,'',params)";
				eval(win);
			} else window.location.href = link;
		} else {
			if (target == "new") {
				var win = window.open(link,"",params);
			} else window.location.href = link;
		}
	}
}
function clear_action()
{
	document.dirForm.hidAction.value = "";
}
/*
if (document.all) {
	document.onkeypress=keypress;
} else {
	if (document.layers) {
		document.captureEvents(Event.KEYPRESS);
		document.onkeypress=keypress;
	} else {
		document.onkeypress=keypress;
	}
}

function keypress(e) {
	var keyPressed = null;
	if (document.all) {
   		keyPressed = String.fromCharCode(window.event.keyCode);
		if (keyPressed == "\r") {
			//alert("i hit enter");
			//submit_form();
			if(document.dirForm.txtSearchText.value.trim() != "")
			{
				document.dirForm.hidAction.value = "submit_search";
				document.dirForm.submit();
			}
		}
	} else {
		if (document.layers) {
			keyPressed = e.which;
			if (keyPressed == "13") {
				//alert("i hit enter");
				if(document.dirForm.txtSearchText.value.trim() != "")
				{
					document.dirForm.hidAction.value = "submit_search";
					document.dirForm.submit();
				}
			}
		} else {
			keyPressed = String.fromCharCode(e.keyCode);
			if (keyPressed == "\r") {
				//alert("i hit enter");
				if(document.dirForm.txtSearchText.value.trim() != "")
				{
					document.dirForm.hidAction.value = "submit_search";
					document.dirForm.submit();
				}
			}
		}
	}
}*/
if (document.all) {
	document.onkeypress=BodyKeyPress;
} else {
	document.onkeypress=BodyKeyPress;
}
//submit the search only when there is a non-empty value in the keyword box
function BodyKeyPress(e) 
{
	if (document.all) //IE
	{
		if (window.event.keyCode == 13 && document.dirForm.txtSearchText.value.trim() == "")
			window.event.keyCode =0;
		else if (window.event.keyCode == 13 && document.dirForm.txtSearchText.value.trim() != "")
			document.dirForm.hidAction.value = "submit_search";
	} 
	else //NS 6.0+
	{
		if (e.which == 13)
			document.dirForm.hidAction.value = "submit_search";
		/*
		if (e.which == 13 && document.dirForm.txtSearchText.value.trim() == "")
			return false;		
		else if (e.which == 13 && document.dirForm.txtSearchText.value.trim() != "")
			document.dirForm.hidAction.value = "submit_search";
			*/
	}
} 
function printResult()
{
	//encode the values
	if (document.dirForm.hidAction.value == "submit_search")
		followLink("PrintResult.aspx?action=submit_search&keyword=" + encodeProblemCharacters(document.dirForm.hidKeyword.value.trim()),'new','top=0,left=0,scrollbars=yes,height=500,width=800,status=no,toolbar=no,menubar=no,location=no,resizable=yes')
	if (document.dirForm.hidAction.value == "submit_community")
	{
		var strDirectoryText = encodeProblemCharacters(document.dirForm.drpDirectory.options[document.dirForm.drpDirectory.selectedIndex].text);
		var strDirectoryValue = encodeProblemCharacters(document.dirForm.drpDirectory.value);
		var strSubDirectoryValues = "", strSubDirectoryTexts = "", strCategory = "";
		
		for (var i=0; i<document.dirForm.lstSubDirectory.options.length; i++)
		{
			if (document.dirForm.lstSubDirectory.options[i].selected)
			{
				strSubDirectoryValues += document.dirForm.lstSubDirectory.options[i].value + "~|~";
				strSubDirectoryTexts += encodeProblemCharacters(document.dirForm.lstSubDirectory.options[i].text) + ", ";
			}
		}
		
		for (var i=0; i<document.dirForm.drpDirectory.options.length; i++)
		{
			if (document.dirForm.drpDirectory.options[i].selected)
			{
				strCategory += document.dirForm.drpDirectory.options[i].value + "~|~";
			}
		}
		
		var strCommunity = encodeProblemCharacters(document.dirForm.drpCommunity.value);
		followLink("PrintResult.aspx?action=submit_community&category=" + strCategory + "&directory=" + strDirectoryText + "&sub-directory-values=" + strSubDirectoryValues + "&sub-directory-texts=" + strSubDirectoryTexts + "&community=" + strCommunity,'new','top=0,left=0,scrollbars=yes,height=500,width=800,status=no,toolbar=no,menubar=no,location=no,resizable=yes')
		//window.open("PrintResult.aspx?action=submit_community&directory=" + strDirectoryText + "&sub-directory-values=" + strSubDirectoryValues + "&sub-directory-texts=" + strSubDirectoryTexts + "&community=" + strCommunity);
	}
}

function encodeProblemCharacters(str) 
{
	if (str.indexOf('&') > - 1) str = encodeCharacter(str, "&", "0026;");
	if (str.indexOf('#') > - 1) str = encodeCharacter(str, "#", "0023;");
	if (str.indexOf('+') > - 1) str = encodeCharacter(str, "+", "002B;");
	if (str.indexOf("'") > - 1) str = encodeCharacter(str, "'", "0060;");
	if (str.indexOf('%') > - 1) str = encodeCharacter(str, "%", "0025;");
	if (str.indexOf('=') > - 1) str = encodeCharacter(str, "=", "003D;");
	if (str.indexOf('|') > - 1) str = encodeCharacter(str, "|", "007C;");
	if (str.indexOf(' ') > - 1) str = encodeCharacter(str, " ", "0020;");
	return str;
}

function encodeCharacter(str, chr, encStr) 
{
	var n = str.indexOf(chr);
	while (n > - 1) 
	{
		str = str.substring(0, n) + encStr + str.substring(n+1, str.length);
		n = str.indexOf(chr);
	}
	return str;
}
