var Sjf = {

	/** Service methods **/

	onloadAction : function() {
		this.focusInput();
	},

	focusInput : function () {
		$('searchinputfield').focus();
	},
	
	/** Business logic **/
	
	checkKey : function(event) {
		alert(event.keycode);
	},
	
	startSearch : function($searchString) {
		alert('Suche nach ' + $searchString);
	},
	
	updateSearchHint : function() {
		text = '';
		smode = $('searchmode').value;
		sfor = $('searchfor').value;
		
		smode 	= this.$RF('searchmode');
		sfor  	= this.$RF('searchfor');
		
		srestrjd = (this.$CF('searchrestrictionjavadoc')) ? true : false;
		srestrcs = (this.$CF('searchrestrictioncodesamples')) ? true: false;
				
		if(smode == 'exact') {
			if(sfor == 'classornamespace') {
				text = 'Please enter an <b>exact</b> class name or a namespace (e.g. <i>"com.sap.tc.logging.Category"</i> or just <i>"Category"</i>)!<BR>';
			} else if(sfor == 'jarfile') {
				text = 'Please enter a <b>exact</b> jar-filename (e.g. <i>"logging.jar"</i>) to search for all classes in that jar-file!<BR>';
			} else if(sfor == 'contributor') {
				text = 'Please enter the <b>full exact name</b> (firstname lastname) or <b>exact</b> email of a Contributor (e.g. Peter Parker or p.p@s.com) to search<BR>for his/her contributions such as Code Samples! ';
			}
		} else if(smode == 'all') {
			if(sfor == 'classornamespace') {
				// the original text
				text = 'Please enter a class name or namespace like <i>"com.sap."</i> to search for the jar files containing matching class. No wildcards needed!<BR>';
			} else if(sfor == 'jarfile') {
				text = 'Please enter any jar-filename (may be partial like <i>"logging"</i> or log) to search for all jar-files matching your<BR> input and all classes in those jar-files! ';
			} else if(sfor == 'contributor') {
				text = 'Please enter any name (may be firstname or lastname only) or email (may be partial) of a Contributor like Peter or Parker or p.p)<BR>to search for his/her contributions such as Code Samples! ';
			}
		}
		// If a  search restriction on JavaDoc is active we notify about that
		if(srestrjd) {
			text += '<span class="infored">Entries without JavaDocs are ignored.</span> ';
		}
		// If a  search restriction on Code Samples is active we notify about that
		if(srestrcs) {
			text += '<span class="infored">Entries without Code Samples are ignored.</span>';
		}
		
		$('searchhint').update(text);
	},	
	
	/**
	* Returns the value of the selected checkbox if checked, null if
	* checbox is not checked, and false if the checkbox doesn't exist
	*
	* @param {checkbox Object} el
	*/
	$CF : function(el) {
		if($(el).type && $(el).type.toLowerCase() == 'checkbox') {
			var checkboxGroup = $(el).name;
			var el = $(el).form;
		} else if ($(el).tagName.toLowerCase() != 'form') {
			return false;
		}
 
		var checked = $(el).getInputs('checkbox', checkboxGroup).find(
			function(re) {return re.checked;}
		);
	
		return (checked) ? $F(checked) : null;
	},
	
	/**
	* Returns the value of the selected radio button in the radio group, null if
	* none are selected, and false if the button group doesn't exist
	*
	* @param {radio Object} or {radio id} el
	* OR
	* @param {form Object} or {form id} el
	* @param {radio group name} radioGroup
	*/
	$RF : function(el, radioGroup) {
		if($(el).type && $(el).type.toLowerCase() == 'radio') {
			var radioGroup = $(el).name;
			var el = $(el).form;
		} else if ($(el).tagName.toLowerCase() != 'form') {
			return false;
		}
 
		var checked = $(el).getInputs('radio', radioGroup).find(
			function(re) {return re.checked;}
		);
		return (checked) ? $F(checked) : null;
	},
	
	SearchContributorInfo : function(location) {
		location = location + "em=" + $('contribemail').value;
		// Call location to search for and load Contributor information from DB
		window.location.href=(location);
	},
	
	
	// Check if a given option exists in a given select list
	// If it does we return the position, otherwise we return -1
	GetOptionPosition : function(selectlist, option) {
		pos = -1;
		for(i=0; i<selectlist.length; i++) {
			// Look for the desired option in the list
			if(selectlist[i].value == option) {
				pos = i; // We save the position to return it
				break; // We found a result so we can exit here to save time :-)
			}
		}
		return pos;
	},
	
	// We add a new Option to our class associations
	// option is the option value to be added or removed
	// action defines the action to be performed (0 = add (default), 1 = remove)
	UpdateClassAssociations : function(option,action) {
		//alert($('classassoc').value);
		// Only add the option if the value in the field is not empty
		if(option != "") {
			// We want to remove an option
			if(action == 1) {
				// Determine amount of list items: $('classassoc').options.length
				// Display the value of one specific list item: $('classassoc').options[X].value
				// Delete one specific list item from list: $('classassoc').options[X] = null
				// Walk through options and delete the one currently selected by setting it to null
				// also see: http://www.quirksmode.org/js/options.html
				lipos = this.GetOptionPosition($('classassoc').options,option);
				// If we found the option in our selectlist we remove it using its position index
				if(lipos != -1) { $('classassoc').options[lipos] = null; }
			} else {
				// We want to add an option (this is default behaviour)
				// When adding we need to check if this association is already contained in the list
				// We clearly only add it if it is not!
				if(this.GetOptionPosition($('classassoc').options,option) == -1) {
					// If the function returns no position (indicated by -1) we can add the option
					// otherwise the option is already contained in the list
					var optn = document.createElement("OPTION");
					optn.text = option;
					optn.value = option;
					$('classassoc').options.add(optn);
				} else {
					alert('Class ' + option + ' already contained in the list of associated classes!');
				}
			}
			// Empty the AutoCompletion field
			$('autocompleteClassAssoc').value = "";
		} else { 
			// No Option provided so we notify the user about this circumstance
			alert('Please specify a class first!');
		}
	},
	
	// Method to select all associated classes 
	// Otherwise they would not be submitted in POST Request
	selectAssociatedClasses : function() {
		// Walk through list of options in select box
		for(i=0; i<$('classassoc').options.length; i++) {
			// Select every single option
			$('classassoc').options[i].selected = "true";
		}
	},
	
	/**
	 * Install a search engine (opensearch)
	 * Returns false in case of success (sic!) because that will keep the file link
	 * from being followed.
	 * Code fetched from: https://addons.mozilla.org/js/addons.js 
	 */
	addSearchPlugin : function(PluginURL)  {
		PluginURLFirefox2 = "http://sapjarfinder.com/opensearch/sapjarfinder.xml";
		PluginURLIE7 = "http://sapjarfinder.com/opensearch/sapjarfinder.xml";
		PluginURL = PluginURLFirefox2;
		
		if (window.external && ("AddSearchProvider" in window.external)) {
			window.external.AddSearchProvider(PluginURL);
			return false;
		} else { 	 
			alert(error_opensearch_unsupported);
			return true;
		} 	 
	},

	updateDownloadCounter : function(dlid) {
		// Update Download Counter in the background
		var url = '/updateDownloadCounter.php?id=' + dlid;
		new Ajax.Request(url, {
				onSuccess: function(response) {
					//alert(response.status);
				},
				OnFailure: function(response) {
					// alert("Failed!");
					// In this case we just do nothing
				}
		});
	},

	TriggerDownload : function(url) {
		//alert("Trigger Download for URL: " + url);
		window.location.href = url;
	}
}