
function SubmitSearch(id, idName)
{
	
	var len=id.indexOf(idName)
	var prefix=id.substr(0,len);
	var keyWordField=prefix + "ddlKeyword";
	var typeField = prefix + "ddlType";
	var keyWordValueField=prefix + "txtInput";
	var sSearchAll = document.getElementById(prefix + 'radSearchAll').checked ? "1" : "0";
	var sYourCat = document.getElementById(prefix+'radYourCatalog').checked ? "1" : "0";
	var sStockOnly = document.getElementById(prefix + 'chkBranchStocked').checked ? "1" : "0";
	var sGreenOnly ="0";
	if(document.getElementById(prefix + 'chkGreen') != null)
	{
	   sGreenOnly = document.getElementById(prefix + 'chkGreen').checked ? "1" : "0";
	   }
	var sCatalog = document.getElementById(prefix + "ddlCatalogs").value;
	
	if(sYourCat == "1")
	{
		sSearchAll = "1";
	}
	
	var expires=new Date();
	expires.setYear(expires.getYear()+30);

	var keyWord=document.getElementById(keyWordField).value;
	var keyWordValue=document.getElementById(keyWordValueField).value;
	var searchType = document.getElementById(typeField).value;	
	
		var sortField = "";
		
		switch( keyWord)
		{
			case "partnum":
			sortField="partnumber";
			break
			case "simus":
			sortField = "simus";
			break
			case "simcdn":
			sortField = "simcdn";
			break
		
		}
			
	document.cookie="SearchKeyWord=" + keyWord + "; path=/; expires=" + expires.toGMTString();
	document.cookie="SearchType=" + searchType + "; path=/; expires=" + expires.toGMTString();
	document.cookie="SearchValue=" + keyWordValue + "; path=/";
	document.cookie="SearchCatalog=" + sCatalog  + "; path=/";
	document.cookie="Gr=" + sGreenOnly  + "; path=/";
	
	var param="?advSearch=1&searchFlag=2&partNoKeyword=" + keyWord + "&partNoKeywordValue=" + keyWordValue + "&searchAll=" 
		+ sSearchAll + "&stringPos=" + searchType + "&yourCat=" + sYourCat + "&stockOnly=" + sStockOnly + "&paperCatalog=" + sCatalog +  "&gr=" + sGreenOnly;
	
	if(sortField != "")
	{
	  param += 	 "&sortfield=" + sortField + "&sortDirection=asc";
	  }
	window.location ="/direct/Search/SearchResults.aspx" + param;
	
	
	return false;
}
function SubmitAdvancedSearch(id)
{
	var sStocked;
	var sGreen;
	if (id=="1")
	{	
		sStocked = document.getElementById('chkBranchStocked').checked ? "1" : "0";
		
		var param="?advSearch=1"
			+ "&searchFlag=1"
			+ "&allWords=" +  document.getElementById("txtAllWords").value 
			+ "&anyWords=" + document.getElementById("txtAnyWords").value 
			+ "&exactPhrase=" + document.getElementById("txtExactPhrase").value
			+ "&excludedWords=" + document.getElementById("txtExcludedWords").value 
			+ "&includeOrExclude=" + document.getElementById("ddlKeywordsIncludeOrExclude").value
			+ "&mfr=" + document.getElementById("txtKeywordsMfr").value
			+ "&stockOnly=" + sStocked;
			
		

		document.cookie="AllWords=" + document.getElementById("txtAllWords").value  + "; path=/";
		document.cookie="AnyWords=" + document.getElementById("txtAnyWords").value + "; path=/";
		document.cookie="ExactPhrase=" + document.getElementById("txtExactPhrase").value + "; path=/";
		document.cookie="ExcludedWords=" + document.getElementById("txtExcludedWords").value + "; path=/";
		document.cookie="IncludeOrExclude=" + document.getElementById("ddlKeywordsIncludeOrExclude").value + "; path=/";
		document.cookie="Mfr=" + document.getElementById("txtKeywordsMfr").value + "; path=/";
		window.location.href="/direct/Search/SearchResults.aspx" + param;
		return false;
		

	}
	else
	{
		sStocked = document.getElementById('chkBranchStocked1').checked ? "1" : "0";
		
		var param="?advSearch=1"
			+ "&searchFlag=2"
			+ "&partNoKeyword=" + document.getElementById("ddlPartsKeyword").value 
			+ "&stringPos=" + document.getElementById("ddlPartsStringPos").value 
			+ "&partNoKeywordValue=" + document.getElementById("txtPartsKeywordValue").value
			+ "&includeOrExclude=" + document.getElementById("ddlPartsIncludeOrExclude").value 
			+ "&mfr=" + document.getElementById("txtPartsMfr").value
			+ "&stockOnly=" + sStocked;
		
		
		document.cookie="partNoKeyword=" + document.getElementById("ddlPartsKeyword").value  + "; path=/";
		document.cookie="stringPos=" + document.getElementById("ddlPartsStringPos").value  + "; path=/";
		document.cookie="partNoKeywordValue=" + document.getElementById("txtPartsKeywordValue").value  + "; path=/";
		document.cookie="partsincludeOrExclude=" + document.getElementById("ddlPartsIncludeOrExclude").value  + "; path=/";
		document.cookie="partsmfr=" + document.getElementById("txtPartsMfr").value  + "; path=/";
		
		window.location.href="/direct/Search/SearchResults.aspx" + param;
		return false;
	}
}
function SubmitSearchOnEnter(e,id, idName)
{
	
	var keycode = e.keyCode ? e.keyCode : e.which;	
	if(keycode == "13")
	{
		e.returnValue = false;
		e.cancel = true;
		if(e.preventDefault)e.preventDefault();
		SubmitSearch(id, idName);
	}
}

function DefaultSearchKeyword(searchField)
{

	var keyword=GetCookie("SearchKeyWord");
	
	if (keyword!=null&&keyword.length>0)
	{
		if (searchField==null)
		{
			searchField="Search1_ddlKeyword";
		}
		
		if (document.getElementById(searchField)!=null)
		{
			document.getElementById(searchField).value=keyword;
			if(document.getElementById(searchField).value=="")
			{
				// if custpartnum is in the cookie and the user isn't logged in, we need to set the value to ""
				// to default the search to use "Keyword is..."
				document.getElementById(searchField).value="";
			}
		}
	}
	
	var catalog = GetCookie("SearchCatalog");
	
	
	if(catalog != null && catalog.length>0)
	{

	
		if (document.getElementById("Search1_ddlCatalogs")!=null)
		{
			
			document.getElementById("Search1_ddlCatalogs").value=catalog;
	
		}
	}
	
	var sGreen = GetCookie("Gr");
	
	
	
	if(document.getElementById('Search1_chkGreen') != null)
	{
		if(sGreen != null && sGreen.length>0)
		{
			document.getElementById('Search1_chkGreen').checked = (sGreen == "1");
		}
	}
	
	

	
	return true;
}


function DefaultSearchType(target)
{
	
	var sType=GetCookie("SearchType");
	
	if (sType!=null&&sType.length>0)
	{
		if (target==null)
		{
			target="Search1_ddlType";
		}
		
		if (document.getElementById(target)!=null)
		{
			document.getElementById(target).value=sType;
		}
	}
	
	return true;
}
function GetCookie(cookieName)
{
	var allcookies=document.cookie;
	var pos=allcookies.indexOf(cookieName + "=");
	
	if (pos != -1)
	{
		//var start=pos + 14;
		var start=(pos-0) + (cookieName.length-0) + 1;
		
		var end=allcookies.indexOf(";", start);
		
		if (end==-1)
		{
			end=allcookies.length;
		}
		var value=allcookies.substring(start, end);
		value=unescape(value);
		
		return value;
	}
}
