//add trim function to all String objects
String.trim = function () {
	return String.prototype.replace(/^\s+/, "").replace(/\s+$/, "");
};


var rallyMaps = {};
rallyMaps.siteSearch = {};

rallyMaps.siteSearch.advancedSearch = new function () {
	
	this.isElementValueNotEmpty = function (element) {
		var returnval = false;
		
		try{
			if(element !== null && element.value !== null && element.value !== "")
				returnval = true;
		}catch(err){}
		return returnval;
	};
	
	this.isStringNotEmpty = function (value) {
		return value !== null && value !== "";
	};
	
	this.getGlobalSeachField = function () {
		return document.getElementById("SiteSearchInput");
	};
	
	this.getAdvancedSearchField = function () {
		return document.getElementById("advancedSiteSearchQueryPattern");
	};
	
	this.getAdvancedSearchExcludeField = function () {
		return document.getElementById("advancedSiteSearchExcludePattern");
	};
	
	this.getSearchPath = function () {
		return document.getElementById("searchPath");
	}
	
	this.fillAdvancedSearchInputFields = function () {
		var globField 		= this.getGlobalSeachField();
		var advField		= this.getAdvancedSearchField();
		var advExclField	= this.getAdvancedSearchExcludeField();
		
		if (this.isElementValueNotEmpty(globField) && advField !== null && advExclField !== null) {
			var split = globField.value.split(/\s+/);
			if (split !== null && split.length > 0) {
				
				var advFieldVal 	= "";
				var advExlFieldVal 	= "";
				
				for (var i=0; i<split.length; i+=1) {
					var text = split[i];
					if (text.charAt(0) !== "-") {
						advFieldVal += text + " "; 
					}
					else if(text.length > 1){
						advExlFieldVal += text.substr(1) + " ";
					}
				}
				
				advField.value = advFieldVal;
				advExclField.value = advExlFieldVal;
			}
		}
	};
	
	this.advancedSiteSearch = function () {
		var searchPatternEle 	= this.getAdvancedSearchField();
		var exclPatternEle 		= this.getAdvancedSearchExcludeField();
		var formEle				= document.getElementById("advancedSiteSearchForm");
		
		if (searchPatternEle !== null && searchPatternEle.value !== null && searchPatternEle.value !== "") {
			var searchPattern = searchPatternEle.value;
			var exclPattern = "";
			if (exclPatternEle !== null && exclPatternEle.value !== null && exclPatternEle.value !== "") {
				var split = exclPatternEle.value.split(/\s+/);
				if (split !== null && split.length > 0) {
					for (var i = 0; i < split.length; i += 1) {
						var word = split[i];
						if (word !== null && word !== "") {
						
							if (word.charAt(0) === "-") {
								exclPattern += " " + word;
							}
							else {
								exclPattern += " -" + word;
							}								
						}
					}
				}
			}
	
			if(exclPattern !== "") {
				searchPattern += exclPattern.trim();
			}
			formEle.action="/"+this.getSearchPath().value+"/"+searchPattern;
			formEle.submit();//self.location = "http://localhost/suche/"+searchPattern;
		} 
	};
	
};

rallyMaps.siteSearch.searchField = new function () {
	
	this.onSearchFieldSubmit = function(form, linkappend, input) {
		if(form !== null && linkappend !== null && linkappend !== "" && input !== null) {
			if(input !== null && input.value !== null && input.value !== "") {
				form.action="/"+linkappend+"/"+input.value;
			}
			else {
				form.action="";
			}
		}
	};
	
	this.executeSearch = function(pageNum)
	{
		if(document.getElementById("siteSearchResultsContainer") !== null)
			sepp.siteSearch.core.Search(
					 pageNum == null ? rallyMaps.siteSearch.searchField.elements.getHiddenDisplayPage().value : pageNum
					,rallyMaps.siteSearch.searchField.elements.getHiddenSearchPattern().value
					,rallyMaps.siteSearch.searchField.elements.getHiddenFilterTitleOnly().value
					,eval(rallyMaps.siteSearch.searchField.elements.getHiddenFilterJSON().value)
					,rallyMaps.siteSearch.searchField.elements.getHiddenLanguageName().value
			);
	}
};





rallyMaps.siteSearch.searchField.elements = new function () {
	var hiddenFormName = "site-search-api-form";
	
	this.getSearchFieldForm 		= function () { return document.getElementById("SiteSearchForm"); };
	this.getSearchField 			= function () { return document.getElementById("SiteSearchForm:SiteSearchInput"); };
	
	this.getHiddenForm 				= function () { return document.getElementById(hiddenFormName); };
	this.getHiddenSearchPattern 	= function () { return document.getElementById(hiddenFormName+":site-search-value"); };
	this.getHiddenDisplayPage 		= function () { return document.getElementById(hiddenFormName+":site-search-api-display-page"); };
	this.getHiddenFilterTitleOnly 	= function () { return document.getElementById(hiddenFormName+":site-search-api-search-title-only"); };
	this.getHiddenFilterVideos 		= function () { return document.getElementById(hiddenFormName+":site-search-api-search-videos"); };
	this.getHiddenFilterRallies 	= function () { return document.getElementById(hiddenFormName+":site-search-api-search-rallies"); };
	this.getHiddenFilterStages 		= function () { return document.getElementById(hiddenFormName+":site-search-api-search-stages"); };
	this.getHiddenFilterJSON		= function () { return document.getElementById(hiddenFormName+":site-search-api-search-filter"); };
	this.getHiddenLanguageName		= function () { return document.getElementById(hiddenFormName+":site-search-api-search-language-name"); };
	
}


