function validate(lang)
{
	var f = document.formSearch;
	var today = getCurrentYear();
	//alert("f.searchExp.value: " + f.searchExp.value);
	var theSearchExp = f.searchExp.value.toLowerCase();
	//alert("theSearchExp: " + theSearchExp);
	
//	var invalidStartingOperator = /^\s*(AND|OR|NOT)\s+/i;				// Impede que os operadores booleanos apareçam no início da expressão
	var invalidStartingOperator = /^\s*(AND|OR)\s+/i;					// Impede que o AND e o OR apareçam no início da expressão
	var invalidEndingOperator = /\s+(AND|OR|NOT)\s*$/i;					// Impede que a expressão termine com um operador booleano
	var onlyOperators = /^\s*(AND|OR|NOT)(\s+(AND|OR|NOT))*\s*$/i;		// Impede que a expressão seja composta apenas por operadores booleanos
	var noOperand = /\s+(AND|OR|NOT)\s+(AND|OR)\s+/i;					// Impede as seguintes combinações AND AND, AND OR, OR AND, OR OR, NOT AND e NOT OR
//	var notOperand = /\s+(OR|NOT)\s+NOT\s+/i;							// Impede OR NOT e NOT NOT
	var notOperand = /\s+NOT\s+NOT\s+/i;								// Impede NOT NOT

//	As seguintes obrigam que o AND apareça antes do NOT
//	var notOperand1 = /[^D]\s+NOT\s+/i;
//	var notOperand2 = /[^N]D\s+NOT\s+/i;
//	var notOperand3 = /[^A]ND\s+NOT\s+/i;
//	var notOperand4 = /\SAND\s+NOT\s+/i;
			
	if (theSearchExp.search(invalidStartingOperator) >= 0 
	    || theSearchExp.search(invalidEndingOperator) >= 0
		|| theSearchExp.search(onlyOperators) >= 0
	    || theSearchExp.search(noOperand) >= 0
	    || theSearchExp.search(notOperand) >= 0
//	|| theSearchExp.search(notOperand1) >= 0
//	|| theSearchExp.search(notOperand2) >= 0
//	|| theSearchExp.search(notOperand3) >= 0
//	|| theSearchExp.search(notOperand4) >= 0
		)
	{
//		alert("Invalid use of boolean operators");
		alert(parent.messages["INVALID_USE_BOOLEAN"][lang]);
		return false;
	}
	
	var replaceNot = /\s+NOT\s+/i;
	var replacement1 = " and not ";
	var replaceNotBack = /\s+(AND|OR)\s+AND\s+NOT\s+/i;
	var replacement2 = " $1 not ";
	
	theSearchExp = theSearchExp.replace(replaceNot, replacement1).replace(replaceNotBack, replacement2);
	
	if ((f.dateStartBox != null) && (f.dateStartBox.value < 1800 || f.dateStartBox.value > today))
	{
//		alert("The initial date should be greater than 1800 and less than todays date");
		alert(parent.messages["INI_DATE_OUT_OF_RANGE"][lang]);
		return false;
	}
	
	if ((f.dateEndBox != null) && (f.dateEndBox.value > today))
	{
//		alert("The end date should be less than todays date");
		alert(parent.messages["END_DATE_OUT_OF_RANGE"][lang]);
		return false;
	}
	
	if ((f.dateStartBox != null) && (f.dateEndBox != null) && (f.dateStartBox.value > f.dateEndBox.value))
	{
//		alert("The initial date should be less than the end date");
		alert(parent.messages["END_DATE_LESS_INI_DATE"][lang]);
		return false;
	}
/*	
	if ((f.dateStartBox != null) 
	    && (f.dateEndBox != null) 
		&& (f.dateStartBox.value == 1800) 
		&& f.dateEndBox.value == getCurrentYear())
	{
		f.dateStartBox.value = "";
		f.dateEndBox.value = "";
	}
*/	
	insertDefaultOperator();
	
	return true;
}

function insertDefaultOperator()
{
	var s = document.formSearch.searchExp;
	var str = s.value.toLowerCase();

	var quotedExpressions = str.match(/("[^"]*")/g);

	// Substitui expressões dentro de aspas por %~~%
	if (quotedExpressions)
	{
		for (var i = 0; i < quotedExpressions.length; i++)
		{
			var qexp = quotedExpressions[i];
			var pos = str.indexOf(qexp);
			
			str = str.slice(0, pos) + "\%~~\%" + str.slice(pos + qexp.length);
		}	
	}
	
	var patterns = [ /^\s+/, /\s+$/, /\s+/g, / AND (AND|OR|NOT) AND /ig, /^NOT AND /i, / NOT AND /ig, / AND /g, / OR /g, /^NOT /, / NOT /g ];
	var replacements = [ "", "", " and ", " $1 ", "not ", " not ", " and ", " or ", "not ", " not " ];

	for (var i = 0; i < patterns.length; i++)
	{
		str = str.replace(patterns[i], replacements[i]);
	}	
	
	// Insere de volta a expressão dentro de aspas
	if (quotedExpressions)
	{
		for (var i = 0; i < quotedExpressions.length; i++)
		{
			str = str.replace(/%~~%/, quotedExpressions[i]);
		}	
	}
	//alert("str: " + str);
	s.value = str;
}

function getCurrentYear()
{
	return new Date().getFullYear();
}

function setEndDate()
{
	var e = document.formSearch.dateEndBox;
	
	if (e != null)
	{
		e.value = getCurrentYear();
	}
}

function clearOptions()
{	
	var f = document.formSearch;
	f.searchExp.value = "";
	f.dateStartBox.value = "1800";
	f.dateEndBox.value = getCurrentYear();
	f.flags.options[0].selected = true;
	f.restriction.options[0].selected = true;
}

function addRestriction(sel, lang)
{
	var s = document.formSearch.searchExp;
	var index = s.value.lastIndexOf(":");
	var invalidSpaceRestriction = /\s+$/i;
	var invalidOperatorRestriction1 = /\s+(AND|OR|NOT)$/i;
	var invalidOperatorRestriction2 = /^\s*(AND|OR|NOT)$/i;
	
	if (s.value.search(invalidSpaceRestriction) >= 0)
	{
//		alert("Cannot apply restriction to whitespace");
		alert(parent.messages["INVALID_SPACE_RESTR"][lang]);
		sel.options[0].selected = true;
		return;
	}

	if (s.value.search(invalidOperatorRestriction1) >= 0 || s.value.search(invalidOperatorRestriction2) >= 0)
	{
//		alert("Cannot apply restriction to boolean operators");
		alert(parent.messages["INVALID_BOOLEAN_RESTR"][lang]);
		sel.options[0].selected = true;
		return;
	}

	if ((index > 0) && (s.value.length - index == 3))
	{
		s.value = s.value.substr(0, index);
	}
	
	if (s.value.length > 0)
	{
		s.value = s.value + sel.options[sel.selectedIndex].value;
	}
	else
	{
//		alert("Cannot apply restriction to empty expression");
		alert(parent.messages["INVALID_EMPTY_RESTR"][lang]);
		sel.options[0].selected = true;
	}
	
	sel.options[0].selected = true;
}

function setFormVisibility(visibility)
{
/*		
	var f = document.formSearch;
	
	f.show.value = visibility;
	f.submit();
*/
	var uri = window.location.href;
	
	var pos = uri.indexOf("#");
	
	if (pos != -1)
	{
		uri = uri.substr(0, pos);
	}
	
	if (uri.indexOf("?") == -1)
	{
		uri += "?";
	}
	
	uri = uri.replace(/\&?show\=(true|false)?/ig, "");
	uri += "&show=" + visibility;
	
	var search = document.formSearch.searchExp.value.toLowerCase();

	if (search != "")
	{
		uri = uri.replace(/\&?editSearch\=[^&]+/ig, "");
		uri += "&editSearch=" + search;
	}
	
	window.location = uri;
	
}