function cerca(frase,theForm){
	theForm.qlphrase.value=frase;
	theForm.submit()
}

function cambiaCriterio(criterio,theForm){
	for(i=0;i<theForm.length;i++){
		if(theForm.elements[i].name == 'ordinamento'){
			if(criterio == 'XML')
				theForm.elements[i].value = theForm.elements[i].value.replace('xml',criterio)
			if(criterio == 'xml')
				theForm.elements[i].value = theForm.elements[i].value.replace('XML',criterio)
		}
	}
return true;
}
function apriIdx(objVoc,maxresult,theArch,varAlias,varTipology){

	alias=varAlias;
	typology=varTipology;
	start_param=objVoc.value;
	return_id=objVoc.id;

	phrase=document.getElementById('parsedphrase').value;
	appendice = ""
	if(theArch != null)
		appendice = "&theArch="+theArch
	
	if(objVoc.start_param != null){
		if(typology == 'double'){
				start_param = " "+start_param
		}
		else{
			if(start_param == '')
				start_param = objVoc.start_param
		}
	}

	if(start_param.indexOf('"') == 0)
		start_param = start_param.substring(1)
	up_down='up';

	if(phrase == null)
		phrase = "";
	if(typology == null)
		typology = "multi";
	window.open("VocabolarioServlet?urlAfterSave=/public/application/jsp/vocabolario.jsp&urlErrorPage=/public/application/jsp/vocabolario.jsp&vocabolario_alias="+alias+"&vocabolario_maxresult="+maxresult+"&vocabolario_up_down="+up_down+"&vocabolario_start_param="+start_param+"&vocabolario_return_id="+return_id+"&vocabolario_tipology="+typology+"&vocabolario_phrase="+phrase+appendice,"vocabolario","toolbar=0,status=0,width=600,height=480,scrollbars=yes");
	return false
}
function query(theForm,msg){
	if(theForm==null){
		theForm = document.theForm;
	}
	ritorno = false;
	ritornoElemento = false;
	elementoQlPhrase = "";
	for(i=0;i<document.theForm.length;i++){
		if(document.theForm.elements[i].name.indexOf('[') == 0 && document.theForm.elements[i].value !=''){
			ritornoElemento = true;
			if(document.theForm.elements[i].value.indexOf("xquery:")==0){
				document.theForm.qlphrase.value = document.theForm.elements[i].value.substring(7);
				document.theForm.elements[i].value = '';
				}
		}else{
			if(document.theForm.elements[i].name == 'anno_ini'){
				ritorno = findDate(theForm,'collect',theForm['dataField'],'')
			}
		}
	}
	if(ritorno || ritornoElemento || document.theForm.qlphrase.value != ''){
	
		document.theForm.submit()
	}
	else
		alert(msg);
	return true
}
function findIt(thInput){
	laFrase = thInput.value;
	//document.theForm.reset();
	document.theForm.qlphrase.value = laFrase;
	//document.theForm.submit()
	return true;
}
function findItErase(theArch){
	window.open("eraseDbSession.jsp?db="+theArch,"gestioneErase","toolbar=0,status=0,width=10,height=10,scrollbars=no,resizable=no,status=no");
	return true;
}
function ricercaLibera(theArch){

	if(parent.frames['ricercaSemplice'] != null)
		if(parent.frames['ricercaSemplice'].document.theForm == null)
			parent.frames['ricercaSemplice'].location = 'ricercaSemplice.jsp?db='+theArch;
	return true
}

function ricercaEstesa(db){
	percorso = "ricercaEstesa.jsp?theDb="+db
	window.open(percorso,"ricercaEstesa","toolbar=0,status=0,width=700,height=500,scrollbars=yes,resizable=yes");
	return false;
}

function vocabolario(objVoc,maxresult,theArch){

	alias=objVoc.alias;
	//alert(alias)
	typology=objVoc.typology;
	start_param=objVoc.value;
	return_id=objVoc.id;
	phrase=document.getElementById('parsedphrase').value;
	appendice = ""

	if(theArch != null)
		appendice = "&theArch="+theArch
	
	if(objVoc.start_param != null){
		if(typology == 'double'){
				start_param = " "+start_param
		}
		else{
			if(start_param == '')
				start_param = objVoc.start_param
		}
	}

	if(start_param.indexOf('"') == 0)
		start_param = start_param.substring(1)
	up_down='up';

	if(phrase == null)
		phrase = "";
	if(typology == null)
		typology = "multi";
	window.open("VocabolarioServlet?urlAfterSave=/public/application/jsp/vocabolario.jsp&urlErrorPage=/public/application/jsp/vocabolario.jsp&vocabolario_alias="+alias+"&vocabolario_maxresult="+maxresult+"&vocabolario_up_down="+up_down+"&vocabolario_start_param="+start_param+"&vocabolario_return_id="+return_id+"&vocabolario_tipology="+typology+"&vocabolario_phrase="+phrase+appendice,"vocabolario","toolbar=0,status=0,width=600,height=480,scrollbars=yes");
	return false
}


function findDate(theForm,action,elemento,tipologia){
 
	if(action == 'normalize' && elemento.value != ''){
			valore = elemento.value;
			valoreInt = parseInt(valore);
			if(tipologia == 'day'){
				if(valoreInt > 31 || valoreInt < 1){
					alert('Attenzione: data non valida!');
					elemento.value = "";
					elemento.focus();
				}else{
					if(valore.length == 1)
						valore = '0'+valore
					elemento.value = valore;					
				}
			}else{
				if(tipologia == 'month'){
					if(valoreInt > 12 || valoreInt < 1){
						alert('Attenzione: data non valida!');
						elemento.value = "";
						elemento.focus();
					}else{
					if(valore.length == 1)
						valore = '0'+valore
					elemento.value = valore;						
					}		
				}
				else{
					if(valore.length < 4){
						alert('Attenzione: data non valida!');
						elemento.value = "";
						elemento.focus();
					}					
				}
			}
		return false			
	}else{

		if(action == 'collect'){
			if(theForm['anno_ini'].value != ''){
				dataEsatta = false;
				for(i=0;i<theForm.length;i++){
					if(theForm.elements[i].name == 'dataEsatta'){
						dataEsatta = theForm.elements[i].checked;
					}
				}		
				anno_ini = theForm['anno_ini'].value;
				anno_fin = theForm['anno_fin'].value;
				mese_ini = theForm['mese_ini'].value;
				mese_fin = theForm['mese_fin'].value;			
				giorno_ini = theForm['giorno_ini'].value;
				giorno_fin = theForm['giorno_fin'].value;			
				if(dataEsatta){
					if(mese_ini == '')
						mese_ini = '??';
					if(giorno_ini == '')
						giorno_ini = '??';	
					
					if(anno_fin != ''){									
						if(giorno_fin == '')
							giorno_fin = '??';				
						if(mese_fin == '')
							mese_fin = '??';	
					}else{
						anno_fin = '';	
						mese_fin = '';	
						giorno_fin = '';											
					}
				}else{
					if(mese_ini == '')
						mese_ini = '01';
					if(giorno_ini == '')
						giorno_ini = '01';				
					if(anno_fin != ''){
						if(giorno_fin == '')
							giorno_fin = '31';				
						if(mese_fin == '')
							mese_fin = '12';	
					}else{
					anno_fin = '';	
					mese_fin = '';	
					giorno_fin = '';											
					}
				}
				searchString = anno_ini+mese_ini+giorno_ini;
				if(!dataEsatta){
					searchString += "^"			
				}
				if(anno_fin != '')
					if(dataEsatta)
						searchString += "-"+anno_fin+mese_fin+giorno_fin;
					else
						searchString += parseInt(anno_fin+mese_fin+giorno_fin)+1;
				else{
					if(dataEsatta)
						searchString += ""; //searchString += anno_fin+mese_fin+giorno_fin;
					else
						searchString += "";
				}
				
				if(dataEsatta){
					 
					theForm[elemento.value].value = ""+searchString+"";
					
				}	
				else{
					theForm[elemento.value].value = "{"+searchString+"}";
				}	
				//alert(searchString)
			return true;
			}
		
			else 
				return false;
			}


	}

}


function Trims(strText)
{
	var s=strText;
	if(typeof(s)!='undefined' && s.length != 0){
		while ((s.indexOf(" ")==0) && (s.length > 0)) s=s.slice(1);
		if(s.length != 0){
			while (s.lastIndexOf(" ")==s.length-1) s=s.slice(0,s.length-1);
		}
	}
	return s;
 }


function inc_doQuery(){

	document.inc_theForm.selid.value = ""
	document.inc_theForm.ramoDoc.value = ""

	ritorno = false
	raffinaRicerca = false
	tipoRaffina = "libera"

	
	for(i=0;i<document.inc_theForm.length;i++){
		if(document.inc_theForm.elements[i].name.indexOf('[') == 0 && document.inc_theForm.elements[i].value !='' ){
		
		ilValore = Trims(document.inc_theForm.elements[i].value);
		
		while(ilValore.indexOf("*")>-1 || ilValore.indexOf("?")>-1){
			ilValore = ilValore.replace('*','').replace('\?','')
		}
	 
		
		if(ilValore!='')
			ritorno = true;
			
			
		}
		
		if(document.inc_theForm.elements[i].name == 'raffina' && document.inc_theForm.elements[i].checked && !document.inc_theForm.elements[i].disabled){
		   if(document.leftArea != null){
			raffinaRicerca = true
			if(document.inc_theForm.elements[i].value=="esito"){
				if(document.esitoForm != null)
					document.inc_theForm.selid.value = document.esitoForm.selid.value
			}
			if(document.inc_theForm.elements[i].value=="tree"){
				if(schedaBreve.document.theForm != null){
					document.inc_theForm.ramoDoc.value = schedaBreve.document.theForm.physDoc.value
					}
				//alert(document.inc_theForm.ramoDoc.value)
			}
		   }
		}
	}
	if(ritorno){
 		return true;
		}
	else{
		alert('Inserire almeno un valore nei campi di ricerca');
		document.inc_theForm.selid.value = ""
		document.inc_theForm.ramoDoc.value = ""
		return false
		}
}


function modRicerca(azione, elemento){
	//alert()
	azioneToDo = true;
	if(azione == 'abilita')
		azioneToDo = false

	if(azione == 'disabilita')
		azioneToDo = true
	for(i=0;i<document.inc_theForm.length;i++){
		if(document.inc_theForm.elements[i].value == elemento){
			document.inc_theForm.elements[i].disabled = azioneToDo
		}
	}
	document.getElementById('primoElemento').checked = true;
return true;
}
