var Search = {
	
	searchType : "Articles",
	searchResultsPage : "EricssonSearch",
	
	SubmitSearch : function () {
		if(General.clientBrowser == "Microsoft Internet Explorer"){
			if (event.keyCode == 13) {
				event.cancelBubble = true;
				event.returnValue = false;
				Search.Search();
			}
		}else{
			// do nothing
		}
	},
	
	Paging : function (intPage){
		var strUrl = document.getElementById("url").value;
		var strLanguage = document.getElementById("lang").value;
		var strSearchString = document.getElementById("searchstring").value;
		var strSearchType = document.getElementById("typeOfSearch").value;
		var intPagingSize = document.getElementById("pagingSize").value;
		var strSortBy = document.getElementById("sortBy").value;
		var strDirection = document.getElementById("sortDirection").value;
		var intHits = document.getElementById("totalHits").value;		
		var URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intPage +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection;
		
		Ajax.GetResultBlock(URL);
	},
	
	AdvancedPaging : function (intPage){
		var strUrl = document.getElementById("url").value;
		var strLanguage = document.getElementById("lang").value;
		var strSearchString = document.getElementById("searchstring").value;
		var strSearchType = document.getElementById("typeOfSearch").value;
		var intPagingSize = document.getElementById("pagingSize").value;
		var strSortBy = document.getElementById("sortBy").value;
		var strDirection = document.getElementById("sortDirection").value;
		var intHits = document.getElementById("totalHits").value;
		var strAdvancedSearch = document.getElementById("advancedSearch").value;
		var strCountry = document.getElementById("country").value;
		var strTimePeriod = document.getElementById("timePeriod").value;
		var strLocation = document.getElementById("location").value;
		var strBusiness = document.getElementById("business").value;
		var strMotifCategory = document.getElementById("motifCategory").value;
		var strArticleCategory = document.getElementById("articleCategory").value;
		var URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intPage +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection + "&AdvancedSearch=" + strAdvancedSearch + "&Country=" + strCountry + "&TimePeriod=" + strTimePeriod + "&Location=" + strLocation + "&Business=" + strBusiness + "&MotifCategory=" + strMotifCategory + "&ArticleCategory=" + strArticleCategory;
		Ajax.GetResultBlock(URL);
	},
	
	Search : function (){
		var field = document.getElementById("search-field");
		var searchValue = field.value;

		strA = searchValue.replace(/å/g,"%C3%A5");
		strB = strA.replace(/Å/g,"%C3%85");
		strC = strB.replace(/ä/g,"%C3%A4");
		strD = strC.replace(/Ä/g,"%C3%84");
		strE = strD.replace(/ö/g,"%C3%B6");
		strF = strE.replace(/Ö/g,"%C3%96");
		
		var searchString = strF;
		
		var sortDirection = "DESC";
		var sortBy = "PageRank";
		if(Search.searchResultsPage == "EricssonMediaSearch"){
		    sortDirection = "ASC";
		}
		if(Search.searchType == "Image"){
		    sortBy = "MotivID";
		}
		else if(Search.searchType == "Movie"){
		    sortBy = "Object";
		}
		else if(Search.searchType == "Sound"){
		    sortBy = "Object";
		}
		if(searchValue == ""){
			return false;
		}else{
			Ajax.GetResultBlock("/templates/Ericsson/" + Search.searchResultsPage + ".aspx?epslanguage=" + General.Language + "&SearchString=" + searchString + "&SearchType=" + Search.searchType + "&PagingSize=10&PageNumber=1&NrOfHits=200&SortBy=" + sortBy + "&SortDirection=" + sortDirection + "&DoTotalSearch=True");
		}
		
	},
	
	ToggleResultList : function (){
		var field = document.getElementById("search-field");
		var searchValue = field.value;
		
		strA = searchValue.replace(/å/g,"%C3%A5");
		strB = strA.replace(/Å/g,"%C3%85");
		strC = strB.replace(/ä/g,"%C3%A4");
		strD = strC.replace(/Ä/g,"%C3%84");
		strE = strD.replace(/ö/g,"%C3%B6");
		strF = strE.replace(/Ö/g,"%C3%96");
		
		var searchString = strF;
		
		var block = this.className.replace(/.*searchresult-([\w\d-_]+)(\b|$).*/,"$1");
		
		var tabA = document.getElementById("tab-a");
		var tabB = document.getElementById("tab-b");
		var tabC = document.getElementById("tab-c");
		var tabD = document.getElementById("tab-d");
		
		var articleCount = document.getElementById("total-ammount-01").innerHTML;
		var imageCount = document.getElementById("total-ammount-02").innerHTML;
		var soundCount = document.getElementById("total-ammount-03").innerHTML;
		var movieCount = document.getElementById("total-ammount-04").innerHTML;
		
		General.removeClassName(tabA, "tab-a-true");
		General.removeClassName(tabB, "tab-b-true");
		General.removeClassName(tabC, "tab-c-true");
		General.removeClassName(tabD, "tab-d-true");
		if(block == "articles") {
			General.addClassName(this.parentNode, "tab-a-true");
			Ajax.GetResultBlock("/templates/Ericsson/EricssonSearch.aspx?epslanguage=" + General.Language + "&SearchString=" + searchString + "&SearchType=Articles&PagingSize=10&PageNumber=1&NrOfHits=" + articleCount + "&SortBy=PageRank&SortDirection=DESC");
			Search.searchType = "Articles";
			Search.searchResultsPage = "EricssonSearch";
		}
		if(block == "images") {
			General.addClassName(this.parentNode, "tab-b-true");
			Ajax.GetResultBlock("/templates/Ericsson/EricssonMediaSearch.aspx?epslanguage=" + General.Language + "&SearchString=" + searchString + "&SearchType=Image&PagingSize=10&PageNumber=1&NrOfHits=" + imageCount + "&SortBy=MotivID&SortDirection=ASC");
			Search.searchType = "Image";
			Search.searchResultsPage = "EricssonMediaSearch";
		}else{
			General.removeClassName(this.parentNode, "tab-b-true");
		}
		if(block == "video") {
			General.addClassName(this.parentNode, "tab-c-true");
			Ajax.GetResultBlock("/templates/Ericsson/EricssonMediaSearch.aspx?epslanguage=" + General.Language + "&SearchString=" + searchString + "&SearchType=Movie&PagingSize=10&PageNumber=1&NrOfHits=" + movieCount + "&SortBy=Object&SortDirection=ASC");
			Search.searchType = "Movie";
			Search.searchResultsPage = "EricssonMediaSearch";
		}
		if(block == "sound") {
			General.addClassName(this.parentNode, "tab-d-true");
			Ajax.GetResultBlock("/templates/Ericsson/EricssonMediaSearch.aspx?epslanguage=" + General.Language + "&SearchString=" + searchString + "&SearchType=Sound&PagingSize=10&PageNumber=1&NrOfHits=" + soundCount + "&SortBy=Object&SortDirection=ASC");
			Search.searchType = "Sound";
			Search.searchResultsPage = "EricssonMediaSearch";
		}
		return false;
	},
	
	FillSearchTabs : function (){
		var fieldArticle = document.getElementById("article-hits")
		var fieldImage = document.getElementById("image-hits");
		var fieldMovie = document.getElementById("movie-hits");
		var fieldSound = document.getElementById("sound-hits");
		var ammountArticle = document.getElementById("total-ammount-01");
		var ammountImage = document.getElementById("total-ammount-02");
		var ammountMovie = document.getElementById("total-ammount-03");
		var ammountSound = document.getElementById("total-ammount-04");

		if(fieldArticle){
			articleValue = fieldArticle.value;
		}
		if(fieldImage){
			imageValue = fieldImage.value;
		}
		if(fieldMovie){
			movieValue = fieldMovie.value;
		}
		if(fieldSound){
			soundValue = fieldSound.value;
		}
		if(fieldArticle){
			ammountArticle.innerHTML = articleValue;
		}
		if(fieldImage){
			ammountImage.innerHTML = imageValue;
		}
		if(fieldMovie){
			ammountMovie.innerHTML = movieValue;
		}
		if(fieldSound){
			ammountSound.innerHTML = soundValue;
		}
	},
	
	HitsPerPage : function (){
		var strUrl = document.getElementById("url").value;
		var strLanguage = document.getElementById("lang").value;
		var strSearchString = document.getElementById("searchstring").value;
		var strSearchType = document.getElementById("typeOfSearch").value;
		var intPagingSize = document.getElementById("pagingSize").value;
		var strSortBy = document.getElementById("sortBy").value;
		var strDirection = document.getElementById("sortDirection").value;
		var intHits = document.getElementById("totalHits").value;	
		var intPagingSize;
		var intCurrentPage;
		var URL;
		var selectList = document.getElementById("hits-per-page");
		
		if (selectList.options[selectList.selectedIndex].value != "none") {
			intPagingSize = selectList.options[selectList.selectedIndex].value;
			intCurrentPage = selectList.className;
			URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intCurrentPage +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection;
			Ajax.GetResultBlock(URL);
		}
	},
	
	HitsPerPageAdvanced : function (){
		var strUrl = document.getElementById("url").value;
		var strLanguage = document.getElementById("lang").value;
		var strSearchString = document.getElementById("searchstring").value;
		var strSearchType = document.getElementById("typeOfSearch").value;
		var intPagingSize = document.getElementById("pagingSize").value;
		var strSortBy = document.getElementById("sortBy").value;
		var strDirection = document.getElementById("sortDirection").value;
		var intHits = document.getElementById("totalHits").value;
		var strAdvancedSearch = document.getElementById("advancedSearch").value;
		var strCountry = document.getElementById("country").value;
		var strTimePeriod = document.getElementById("timePeriod").value;
		var strLocation = document.getElementById("location").value;
		var strBusiness = document.getElementById("business").value;
		var strMotifCategory = document.getElementById("motifCategory").value;
		var strArticleCategory = document.getElementById("articleCategory").value;
		var intPagingSize;
		var intCurrentPage;
		var URL;
		var selectList = document.getElementById("hits-per-page");
		
		if (selectList.options[selectList.selectedIndex].value != "none") {
			intPagingSize = selectList.options[selectList.selectedIndex].value;
			intCurrentPage = selectList.className;
			var URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intCurrentPage +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection + "&AdvancedSearch=" + strAdvancedSearch + "&Country=" + strCountry + "&TimePeriod=" + strTimePeriod + "&Location=" + strLocation + "&Business=" + strBusiness + "&MotifCategory=" + strMotifCategory + "&ArticleCategory=" + strArticleCategory;
			Ajax.GetResultBlock(URL);
		}
	},
	
	ToggleSortHeader : function (strSortHeader){
		var strUrl = document.getElementById("url").value;
		var strLanguage = document.getElementById("lang").value;
		var strSearchString = document.getElementById("searchstring").value;
		var strSearchType = document.getElementById("typeOfSearch").value;
		var intPagingSize = document.getElementById("pagingSize").value;
		var intPageNumber = document.getElementById("pageNumber").value;
		var strSortBy = strSortHeader;
		var strDirection = document.getElementById("sortDirection").value;
		var intHits = document.getElementById("totalHits").value;
		var strAdvancedSearch = document.getElementById("advancedSearch").value;
		var strCountry = document.getElementById("country").value;
		var strTimePeriod = document.getElementById("timePeriod").value;
		var strLocation = document.getElementById("location").value;
		var strBusiness = document.getElementById("business").value;
		var strMotifCategory = document.getElementById("motifCategory").value;
		var strArticleCategory = document.getElementById("articleCategory").value;
		
		if (strDirection == "DESC") {
		    strDirection = "ASC";
			var URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intPageNumber +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection + "&AdvancedSearch=" + strAdvancedSearch + "&Country=" + strCountry + "&TimePeriod=" + strTimePeriod + "&Location=" + strLocation + "&Business=" + strBusiness + "&MotifCategory=" + strMotifCategory + "&ArticleCategory=" + strArticleCategory;
			Ajax.GetResultBlock(URL);
		}
		else {
		    strDirection = "DESC";
			var URL = strUrl + "?epslanguage=" + strLanguage + "&SearchString=" + strSearchString + "&SearchType=" + strSearchType + "&PagingSize=" + intPagingSize + "&PageNumber=" + intPageNumber +"&NrOfHits=" + intHits + "&SortBy=" + strSortBy + "&SortDirection=" + strDirection + "&AdvancedSearch=" + strAdvancedSearch + "&Country=" + strCountry + "&TimePeriod=" + strTimePeriod + "&Location=" + strLocation + "&Business=" + strBusiness + "&MotifCategory=" + strMotifCategory + "&ArticleCategory=" + strArticleCategory;
			Ajax.GetResultBlock(URL);
		}
	}
}
