// JavaScript Document

function controllaSubmit(controlType) {
	if (controlType == 'AND') {
		if (!checkParolaChiave(document.ricercaAnd.txt_parole_chiave.value)) {
			document.ricercaAnd.parole_chiave.value = document.ricercaAnd.txt_parole_chiave.value;
			andQuery();
			document.ricercaAnd.action='/xportal/web/eng/search/search.jsp';
			document.ricercaAnd.submit();
		} 
		else {
			alert("Research not allowed. Verify the text");
		}
	} else if (controlType == 'EXACT') {
		if (!checkParolaChiave(document.ricercaExact.txt_parole_chiave.value) ) {
			document.ricercaExact.parole_chiave.value = document.ricercaExact.txt_parole_chiave.value;
			exactQuery();
			document.ricercaExact.action='/xportal/web/eng/search/search.jsp';
			document.ricercaExact.submit();
		} 
		else {
			alert("Research not allowed. Verify the text");
		}
	} else if (controlType == 'OR') {
		if (!checkParolaChiave(document.ricercaOr.txt_parole_chiave.value)) {
			document.ricercaOr.parole_chiave.value = document.ricercaOr.txt_parole_chiave.value;
			document.ricercaOr.action='/xportal/web/eng/search/search.jsp';
			document.ricercaOr.submit();
		} 
		else {
			alert("Research not allowed. Verify the text");
		}
	} else if (controlType == 'ORL') {
		if (!checkParolaChiave(document.ricercaOrl.txt_parole_chiave.value)) {
			document.ricercaOrl.parole_chiave.value = document.ricercaOrl.txt_parole_chiave.value;
			document.ricercaOrl.action='/xportal/web/eng/search/search.jsp';
			document.ricercaOrl.submit();
		} 
		else {
			alert("Research not allowed. Verify the text");
		}
	}

}
function andQuery() {
	var newValue = document.ricercaAnd.parole_chiave.value;
	var terms = newValue.split(" ");
	var index = 0;
	newValue = '';
	while (index < terms.length) {
	if (terms[index]!=""){
	
	
		if (index != terms.length - 1) {
			newValue += terms[index] + ' AND ';
		} else {
			newValue += terms[index];
		}
		
	}	
		index++;
	}
	document.ricercaAnd.parole_chiave.value = newValue;
}

function checkParolaChiave(type) {	
	if ((type.indexOf(' ') == 0) || (type.length == 0)) {
		return true;
	}
//	for(i=0; i < type.length; i++) {
//		if ((type.charAt(i) == '+')||(type.charAt(i) == '-')||(type.charAt(i) == '&')||(type.charAt(i) == '|')||(type.charAt(i) == '!')||(type.charAt(i) == '(')||(type.charAt(i) == ')')||(type.charAt(i) == '[')||(type.charAt(i) == ']')
//		   || (type.charAt(i) == '^')||(type.charAt(i) == '\"')||(type.charAt(i) == '~')||(type.charAt(i) == '*')||(type.charAt(i) == '?')||(type.charAt(i) == ':')||(type.charAt(i) == '{')||(type.charAt(i) == '}'))
//		return true;
//	}
	regex = /^[A-Za-z0-9\xEC\xE0\xE8\xF2\xF9.,\s"]+$/;
	isValid = type.match(regex);
	if(isValid == null || isValid == "")
		return true;
	return false;
}

//String.prototype.Trim=Trim;

function exactQuery() { 
	var newValue = '\"' + document.ricercaExact.parole_chiave.value + '\"';
	document.ricercaExact.parole_chiave.value = newValue;
}
//-->

