/* INIZIO CONTROLLI CAMPI RICERCA */

function Trim(stringa)
{
   reTrim=/\s+$|^\s+/g;
   return stringa.replace(reTrim,"");
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
  Char = sText.charAt(i); 
  if (ValidChars.indexOf(Char) == -1) 
	 {
	 IsNumber = false;
	 }
  }
return IsNumber;
   
}



function CheckFormRicerca()
{
	var ok;
	var errors=false;
	errors = "";
	var txtLemma, txtAutore, txtGiorno, txtMese, txtAnno, txtTitolo;
	txtLemma = document.getElementById("Lemma");
	txtAutore = document.getElementById("Autore");
	txtGiorno = document.getElementById("Giorno");
	txtMese = document.getElementById("Mese");
	txtAnno = document.getElementById("Anno");
	txtTitolo = document.getElementById("Titolo1");
	

	if((Trim(txtLemma.value) == "") &&
	  (Trim(txtAutore.value) == "") &&
	  (Trim(txtGiorno.value) == "") &&
 	  (Trim(txtMese.value) == "") &&
  	  (Trim(txtAnno.value) == "") &&
	  (Trim(txtTitolo.value) == "") 
	  )
	{
		errors = true;
		txtLemma.className = "Input-Text-Red";
		txtAutore.className = "Input-Text-Red";
		txtGiorno.className = "Input-Day-Red";
		txtMese.className = "Input-Month-Red";
		txtAnno.className = "Input-Year-Red";
		txtTitolo.className = "Input-Text-Red";		
	}
	else
	{
		txtLemma.className = "Input-Text";
		txtAutore.className = "Input-Text";
		txtGiorno.className = "Input-Day";
		txtMese.className = "Input-Month";
		txtAnno.className = "Input-Year";
	}
	
	if(IsNumeric(txtGiorno.value) == false)
		{
			errors = true;
			txtGiorno.className = "Input-Day-Red";
			txtGiorno.value = "";
		}

	if(IsNumeric(txtMese.value) == false)
		{
			errors = true;
			txtMese.className = "Input-Month-Red";
			txtMese.value = "";
		}

	if(IsNumeric(txtAnno.value) == false)
		{
			errors = true;
			txtAnno.className = "Input-Year-Red";
			txtAnno.value = "";
		}


	if(!errors)
	{
		document.forms[0].submit();
	}
}


/* RICERCA GOOGLE */

function RicercaGoogle(page)
{
	var classNameOk, classNameKo
	
	if (page == "home")
	{
		classNameOk = "Input-Text"
		classNameKo = "Input-Text-Red"
	}
	else
	{
		classNameOk = "Input-Text-Ricerca"
		classNameKo = "Input-Text-Red-Ricerca"
	}	
	
	var ok;
	var errors=false;
	errors = "";
	var txtCampoLibero;
	txtCampoLibero = document.getElementById("RicercaGoogle");

	if(Trim(txtCampoLibero.value) == "")

	{
		errors = true;
		txtCampoLibero.className = classNameKo;
	}
	else
	{
		txtCampoLibero.className = classNameOk;

	}
	
	if(!errors)
	{
		var action, cx, ie, q, sa

		action = "http://www.google.com/cse";
        cx = escape("015021533249531507785:rfq7jjxjmwy");
		ie = escape("UTF-8");
        q = escape(txtCampoLibero.value);
		sa = escape("Cerca");
  
	    document.location.href = action + "?cx=" + cx + "&ie=" + ie + "&q=" + q + "&sa=" + sa
	    
	}
}

/* VALIDAZIONE FORM PER PAGINA RICERCA */

function CheckFormRicerca2()
{
	var ok;
	var errors=false;
	errors = "";
	var txtLemma, txtAutore, txtGiorno, txtMese, txtAnno;
	txtLemma = document.getElementById("Lemma");
	txtAutore = document.getElementById("Autore");
	txtGiorno = document.getElementById("Giorno");
	txtMese = document.getElementById("Mese");
	txtAnno = document.getElementById("Anno");
	txtTitolo = document.getElementById("Titolo1");
	

	if((Trim(txtLemma.value) == "") &&
	  (Trim(txtAutore.value) == "") &&
	  (Trim(txtGiorno.value) == "") &&
 	  (Trim(txtMese.value) == "") &&
  	  (Trim(txtAnno.value) == "") &&
	  (Trim(txtTitolo.value) == "") 
	  )

	{
		errors = true;
		txtLemma.className = "Input-Text-Red-Ricerca";
		txtAutore.className = "Input-Text-Red-Ricerca";
		txtGiorno.className = "Input-Day-Red-Ricerca";
		txtMese.className = "Input-Month-Red-Ricerca";
		txtAnno.className = "Input-Year-Red-Ricerca";
		txtTitolo.className = "Input-Text-Red-Ricerca";		
	}
	else
	{
		txtLemma.className = "Input-Text-Ricerca";
		txtAutore.className = "Input-Text-Ricerca";
		txtGiorno.className = "Input-Day-Ricerca";
		txtMese.className = "Input-Month-Ricerca";
		txtAnno.className = "Input-Year-Ricerca";
	}
	
	if(IsNumeric(txtGiorno.value) == false)
		{
			errors = true;
			txtGiorno.className = "Input-Day-Red-Ricerca";
			txtGiorno.value = "";
		}

	if(IsNumeric(txtMese.value) == false)
		{
			errors = true;
			txtMese.className = "Input-Month-Red-Ricerca";
			txtMese.value = "";
		}

	if(IsNumeric(txtAnno.value) == false)
		{
			errors = true;
			txtAnno.className = "Input-Year-Red-Ricerca";
			txtAnno.value = "";
		}


	if(!errors)
	{
		document.forms[0].submit();
	}
}

function KeyCheckRicerca(e,provenienza)
{
    e = e || window.event;
    var code = e.keyCode || e.which;
    if(code == 13)
    {
		if (provenienza == 'home')
			CheckFormRicerca();
		else
			CheckFormRicerca2();
    }		
}

function KeyCheckRicercaLibera(e,provenienza)
{
    e = e || window.event;
    var code = e.keyCode || e.which;
    if(code == 13)
       RicercaGoogle(provenienza);
}

