//document.topsearch.search_str.focus();
function post_search()
{
	var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var search_str = LTrim(RTrim(document.topsearch.search_str.value));
	var section = document.topsearch.cid.value;
	for(x=0;x<document.topsearch.search_cat.length;x++)
	{
		if(document.topsearch.search_cat[x].checked==true)
		{
			var search_cat=document.topsearch.search_cat[x].value;
			break;
		}
	}
	
	if(search_cat=="News" || search_cat=="Keyword" || search_cat=="Opinions")
	{
		if(search_str.length < 3)
		{
			alert("Please enter minimum 3 characters for search");
			return false;
		}
	}
	else
	{
		if(search_str.length < 1)
		{
			alert("Please enter minimum 1 character for search");
			return false;
		}
	}
	
	if(search_cat=="")
	{
		
		switch (section)
		{
			case "3": // News
			{
				document.topsearch.action = "/mccode/news/searchresult.php";
				break;
			}
			case "0": //Markets
			{
				document.topsearch.action = "/stocks/cptmarket/compsearchnew.php?fname=price&companyname="+(document.topsearch.search_str.value).replace(/&/,"%26");
				break;
			}
			case "7": //MMB
			{
				document.topsearch.action = "/india/messageboard/mmb_search_result.php?f_criteria=Topic&f_Period=4&f_search="+document.topsearch.search_str.value;
				break;
			}
			case "2": //MF
			{
				document.topsearch.action = "/mf/mf_info/mfsearch.php?AMCname="+document.topsearch.search_str.value;
				break;
			}
			default:
			{
				//document.topsearch.action = "/news/newssearch/newscat.php";
				document.topsearch.action = "/stocks/cptmarket/compsearchnew.php?fname=price&companyname="+(document.topsearch.search_str.value).replace(/&/,"%26");
				break;
			}
		}
	}
	else
	{
		var search_val=document.topsearch.search_str.value
		if(search_cat == "News")
		{
			//document.topsearch.action = "/news/newssearch/newscat.php";
			document.topsearch.action = "/mccode/news/searchresult.php";
		}
		else if(search_cat == "Price")
		{
            var sText=document.topsearch.search_str.value;
			sText=sText.toUpperCase();
			if(search_str.length <2)
			{
				Char = sText.charAt(0); 	 
				if (ValidChars.indexOf(Char) != -1) 
					document.topsearch.action ="http://www.moneycontrol.com/india/stockmarket/pricechartquote/"+Char;
			}
			else
				document.topsearch.action = "/stocks/cptmarket/compsearchnew.php?fname=price&companyname="+(document.topsearch.search_str.value).replace(/&/,"%26");
		}
		else if(search_cat == "MFs")
		{
			var sText=document.topsearch.search_str.value;
			sText=sText.toUpperCase();
			if(search_str.length <2)
			{
				Char = sText.charAt(0); 	 
				if (ValidChars.indexOf(Char) != -1) 
					document.topsearch.action ="http://www.moneycontrol.com/india/mutualfunds/mutualfundsinfo/snapshot/"+Char;
				else
					document.topsearch.action = "/mf/mf_info/mfsearch.php?AMCname="+document.topsearch.search_str.value;
			}	
			else
				document.topsearch.action = "/mf/mf_info/mfsearch.php?AMCname="+document.topsearch.search_str.value;
		}
		else if(search_cat == "Topic")
		{
			document.topsearch.mbsearch_str.value = search_val;
			document.topsearch.action = "/msgboard/viewmsg/find_topic.php";
		}
		else if(search_cat == "Boarder")
		{
			document.topsearch.mbsearch_str.value = search_val;
			document.topsearch.action="/msgboard/viewmsg/find_boarders.php";
		}
		else if(search_cat == "Stock")
		{
			document.topsearch.mbsearch_str.value = search_val;
			document.topsearch.action="/msgboard/viewmsg/find_topic_stock.php";
		}
		else if(search_cat == "Keyword")
		{
			document.topsearch.mbsearch_str.value = search_val;
			document.topsearch.action="/india/messageboard/mmb_search_result.php?f_criteria=Keyword&f_Period=1&f_search="+document.topsearch.search_str.value;
		}
		else if(search_cat == "Opinions")
		{
			document.topsearch.action = "/mccode/news/searchresult.php?expert=1";
		}

		else if(search_cat == "Notices")
		{
			document.topsearch.action = "/stocks/cptmarket/compsearchnew.php?notice_q=1&fname=price&companyname="+(document.topsearch.search_str.value).replace(/&/,"%26");
		}

		else if(search_cat == "Web")
		{
			document.topsearch.action = "/mccode/news/searchresult_archive.php?domains=www.moneycontrol.com&q=" + document.topsearch.search_str.value + "&client=pub-2140165247056050&forid=1&channel=7271894659&ie=ISO-8859-1&oe=ISO-8859-1&flav=0000&cof=GALT%3A%230066CC%3BGL%3A1%3BDIV%3A%23999999%3BVLC%3A336633%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3AFF9900%3BALC%3A0066CC%3BLC%3A0066CC%3BT%3A000000%3BGFNT%3A666666%3BGIMP%3A666666%3BFORID%3A11&hl=en&x=38&y=9";
		}
		
		return true;
	}
}
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}