/*
 * Specific function responsible to create search URL
 */
function generateAdvancedSearchURL(textInputFieldId, locale){
	
	var textSearched=document.getElementById(textInputFieldId).value;

	if (validateTextSearchedImproved(textSearched)) {					
		textSearched = Url.encode(textSearched);
			
		var textSearchURL = portalContext+'/'+locale+'/search/'+textSearched;
		return textSearchURL;
	}
	
	// if value is not valid return a special string instead of the URL
	return 'minLengthNotValid';
}

/*
 * This function checks if the passed key event
 * corresponds to the enter key and if this is true it 
 * returns the outcome of the passed function (called with the passed argument)
 * Need to pass two args (normally n parameter support should be implemented)
 */
function callFunctionOnEnterKey(e, func, arg0, arg1) {
	var evt=(e)?e:(window.event)?window.event:null;
 	if(evt){
        var key=(evt.charCode)?evt.charCode:((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
       // alert("key pressed" + key);
        if(key==13){
        return func(arg0,arg1);
		}	
	
    }
}

/*
 *	This function validates the text strings entered by the
 *	user in the text search box of the web site.
 *  The string must be at least 3 characters long and
 *  should not contain special characters such as '*', '[', etc.
 *  TODO: provide a way to internationalize javascript messages 
*/
function validateTextSearchedImproved(textSearched) {
	
	if (textSearched == null) {
		alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
		return false;
	}
	
	//Trim leading and trailing spaces
	textSearched = textSearched.replace(/^\s*/, "").replace(/\s*$/, "");

	//Check if lenght is less than 3 characters if there is a value
	//Must escape less than
	if (textSearched.length < 3){
		alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
		return false;
	}
											
	//Search for invalid characters
	if (textSearched.indexOf('[') != -1 ||
		textSearched.indexOf(']') != -1 ||
		textSearched.indexOf('"') != -1 ||
		textSearched.indexOf('\'') != -1 ||
		textSearched.indexOf('\\') != -1 ||
		textSearched.indexOf('/') != -1 ||
		textSearched.indexOf('^') != -1 ||
		textSearched.indexOf('$') != -1 ||
		textSearched.indexOf('.') != -1 ||
		textSearched.indexOf('|') != -1 ||
		textSearched.indexOf('?') != -1 ||
		textSearched.indexOf('*') != -1 ||
		textSearched.indexOf('+') != -1 ||
		textSearched.indexOf(')') != -1 ||
		textSearched.indexOf('(') != -1 ||
		textSearched.indexOf('{') != -1 ||
		textSearched.indexOf('}') != -1 ||
		textSearched.indexOf('&amp;') != -1) {
			alert('Η λέξη αναζήτησης περιέχει μη επιτρεπτούς χαρακτήρες:[,],",\',{,},\\,/,^,$,.,|,?,*,+,(,),&amp;');
			return false;
	}
	
	return true;
}
