/*=====================================================
    functions JavaScript
    
=====================================================*/


/*=====================================================
   homepage - box ricerca + gradi meteo
=====================================================*/

function ricercaHPlabel() {
	document.write('<div class="left"><label for="DESQueryString1">Cerca</label></div>');
}

function ricercaHPinput() {
	document.write('<div class="left"><input class="frmtxt" id="DESQueryString1" name="DESQueryString1" type="text" value="" tabindex="2" /></div>');
}

function meteo() {
	document.write("&deg;C");
}


/*=====================================================
   tasto stampa
=====================================================*/

function btnPrint() {
	document.write('<div class="funzioni"><p><input type="button" class="frmbtn" onclick="javascript:window.print();" value="Stampa" name="btnprint" title="Stampa questo contenuto" /></p></div>');
}


/*=====================================================
   allineamento se menu sx > menu dx
=====================================================*/

function alignDown() {
	objsx = document.getElementById("wpsportletsx");
	objdx = document.getElementById("wpsportletdx");
	hisx = objsx.offsetHeight;
	hidx = objdx.offsetHeight;
	if (objdx.className!="big" && hisx>=hidx) objdx.style.paddingBottom=(hisx-hidx)+20+"px";
}


/*=====================================================
   motore ricerca - submit + navigazione
=====================================================*/

function hitGo() {
	if (document.forms["ESSimpleSearch"].DESQueryString1.value=="") timeWait(0);
	else timeWait(1);
	document.forms["ESSimpleSearch"].DESGetPage.value = 1;
	document.forms["ESSimpleSearch"].action.value = "DisplayPage";
	document.forms["ESSimpleSearch"].submit();
}

function displayPage(number) {
	timeWait(1);
	document.forms["ESSimpleSearch"].DESGetPage.value = number;
	document.forms["ESSimpleSearch"].action.value = "DisplayPage";
	document.forms["ESSimpleSearch"].submit();
}

function displayPage2(maxPage,index) {
	timeWait(1);
	var number = -1;
	if (index==1) number = document.getElementById("txtnavigator1").value;
	else if (index==2) number = document.getElementById("txtnavigator2").value;
	else return false;
	if (isNaN(number)) {
		alert('Inserire un valore numerico!');
		return false;
	}
	if (number<1 || number>maxPage) {
		alert("Inserire un valore numerico compreso tra 1 e "+maxPage+"!");
		return false;
	}
	document.forms["ESSimpleSearch"].DESGetPage.value = number;
	document.forms["ESSimpleSearch"].action.value = "DisplayPage";
	document.forms["ESSimpleSearch"].submit();
}

function hitItem(action) {
	document.forms["ESSimpleSearch"].action.value = action;
	document.forms["ESSimpleSearch"].submit();
}

function simple() {
	document.forms["ESSimpleSearch"].avanzata.value = "false";
	document.forms["ESSimpleSearch"].adv_sim.value = "true";
	document.forms["ESSimpleSearch"].DESGetPage.value = 1;
	document.forms["ESSimpleSearch"].action.value = "DisplayPage";
	document.forms["ESSimpleSearch"].submit();
}
    
function hitGoAdv(type) {
	timeWait(2);
	document.forms["ESSimpleSearch3"].DESGetPage.value = 1;
	document.forms["ESSimpleSearch3"].DESQueryId.value = "";
	document.forms["ESSimpleSearch3"].action.value = "DisplayPage";
	document.forms["ESSimpleSearch3"].type.value = type;
	var text1 = document.forms["ESSimpleSearch1"].text_and.value;
	var text2 = document.forms["ESSimpleSearch1"].text_exact.value;
	var text3 = document.forms["ESSimpleSearch1"].text_or.value;
	var text4 = document.forms["ESSimpleSearch1"].text_not.value;
	document.forms["ESSimpleSearch3"].text_and.value = text1;
	document.forms["ESSimpleSearch3"].text_exact.value = text2;
	document.forms["ESSimpleSearch3"].text_or.value = text3;
	document.forms["ESSimpleSearch3"].text_not.value = text4;       
	if (type=="complex") {
		if (text1=="" && text2=="" && text3!="") document.forms["ESSimpleSearch3"].type.value = "or";
			else document.forms["ESSimpleSearch3"].type.value = "and";
		if (text2!="" || text4!="") {
			document.forms["ESSimpleSearch3"].web.value = "web";
		}
		if (text1!="") document.forms["ESSimpleSearch3"].DESQueryString1.value = text1;
		else if (text2!="") document.forms["ESSimpleSearch3"].DESQueryString1.value = text2;
		else if (text3!="") document.forms["ESSimpleSearch3"].DESQueryString1.value = text3;
	}
	else document.forms["ESSimpleSearch3"].DESQueryString1.value = document.forms["ESSimpleSearch2"].text_gql.value;
	if (type=="gql" || text1!="" || text2!="" || text3!="") document.forms["ESSimpleSearch3"].submit();
}
    
function SetChecks(str_dir) {
	var objForm=document.forms["ESSimpleSearch3"];
	for (i=0;i<objForm.elements.length;i++){
		var objEl=objForm.elements[i];
		if (objEl){
			if (objEl.type=="checkbox")
				if (str_dir=="on" && !objEl.checked) objEl.checked=true;
				if (str_dir=="off" && objEl.checked) objEl.checked=false;
		}
	}
}

function hitItemAdv(type) {
	if (type=="gql") document.forms["ESSimpleSearch2"].text_gql.value="";
	else if (type=="complex") {
		document.forms["ESSimpleSearch1"].text_and.value="";
		document.forms["ESSimpleSearch1"].text_exact.value="";
		document.forms["ESSimpleSearch1"].text_or.value="";
		document.forms["ESSimpleSearch1"].text_not.value="";
	}
}

function searchNav(obj,pagenumber,totalpages) {
	status = "";
	if (totalpages==1) status = "disabled";
	if (obj=="noPageFirst") document.write('<div class="hidden">&nbsp;</div><img src="/wps/themes/html/PcrTheme/images/pgfirst.gif" alt="Nessuna pagina precedente" />');
	if (obj=="PageFirst") document.write('<div class="hidden">&nbsp;</div><a href="#" onclick="javascript:displayPage(1);"><img src="/wps/themes/html/PcrTheme/images/pgfirst.gif" alt="Vai alla prima pagina" /></a>');
	if (obj=="noPagePrev") document.write('<img src="/wps/themes/html/PcrTheme/images/pgprevious.gif" alt=" " />');
	if (obj=="PagePrev") document.write('<a href="#" onclick="javascript:displayPage('+pagenumber+'-1);"><img src="/wps/themes/html/PcrTheme/images/pgprevious.gif" alt="Vai alla pagina precedente" /></a>');
	if (obj=="noPageNext") document.write('<div class="hidden">&nbsp;</div><img src="/wps/themes/html/PcrTheme/images/pgnext.gif" alt=" " />');
	if (obj=="PageNext") document.write('<div class="hidden">&nbsp;</div><a href="#" onclick="javascript:displayPage('+pagenumber+'+1);"><img src="/wps/themes/html/PcrTheme/images/pgnext.gif" alt="Vai alla pagina successiva" /></a>');
	if (obj=="noPageLast") document.write('<div class="hidden">&nbsp;</div><img src="/wps/themes/html/PcrTheme/images/pglast.gif" alt="Nessuna pagina successiva" />');
	if (obj=="PageLast") document.write('<a href="#" onclick="javascript:displayPage('+totalpages+');"><img src="/wps/themes/html/PcrTheme/images/pglast.gif" alt="Vai all ultima pagina" /></a>');
	if (obj=="navGo1") document.write('<div class="hidden">&nbsp;</div><span class="hidden">Inserisci il numero di </span><label for="txtNavigator1">pagina</label><span class="hidden"> desiderato:&nbsp;&nbsp;</span> <input type="text" class="frmtxt small nospaceb nospacel" size="4" id="txtnavigator1" value="'+pagenumber+'" maxlength="4" '+status+' /><input type="button" class="frmbtn" name="btnnavigator1" value="Vai" alt="Vai al numero di pagina scelto" onclick="javascript:displayPage2('+totalpages+',1);" '+status+'/>');
	if (obj=="navGo2") document.write('<div class="hidden">&nbsp;</div><span class="hidden">Inserisci il numero di </span><label for="txtNavigator2">pagina</label><span class="hidden"> desiderato:&nbsp;&nbsp;</span> <input type="text" class="frmtxt small nospaceb nospacel" size="4" id="txtnavigator2" value="'+pagenumber+'" maxlength="4" '+status+' /><input type="button" class="frmbtn" name="btnnavigator2" value="Vai" alt="Vai al numero di pagina scelto" onclick="javascript:displayPage2('+totalpages+',2);" '+status+' />');
}

function waitDiv(val) {
	var divLoadId='<div id="divloadcont">'
	var divLoad='<div class="hidden">&nbsp;</div><p class="txtcenter"><span class="testostandard"><strong>Elaborazione dei risultati in corso...</strong><br />(il tempo di attesa dipende dalla complessit&agrave; della ricerca)</span></p><br /><br /></div>'
	if (val==1) divLoadId=divLoadId+"<br />";
	document.write(divLoadId+divLoad);	
}

function timeWait(val) {
	if (val==0) {
		document.getElementById("divloadcont").style.display="none";
	}
	else if (val==2) {
		document.getElementById("divloadcont").style.display="block";
		document.getElementById("inputtext").style.display="none";
		document.getElementById("helplink").style.display="none";
	}	
	else {
		document.getElementById("divloadcont").style.display="block";
		document.getElementById("divresults").style.display="none";
	}
}

function designForm1(s1,s2,s3,s4) {
	document.write('<fieldset id="fieldand"><legend><span class="testotitolo"><strong>Trova risultati che...</strong></span></legend><p class="left testostandard"><label for="text_and">Contengano tutte le seguenti parole:</label></p><p class="txtright"><input class="frmtxt big" id="text_and" name="text_and" type="text" value="'+s1+'" /></p><p class="left testostandard"><label for="text_exact">Contengano la seguente frase:</label></p><p class="txtright"><input class="frmtxt big" id="text_exact" name="text_exact" type="text" value="'+s2+'" /></p><p class="left testostandard"><label for="text_or">Contengano una qualunque delle seguenti parole:</label></p><p class="txtright"><input class="frmtxt big" id="text_or" name="text_or" type="text" value="'+s3+'" /></p><p class="left testostandard"><label for="text_not">NON contengano le seguenti parole:</label></p><p class="txtright"><input class="frmtxt big" id="text_not" name="text_not" type="text" value="'+s4+'" /></p><div class="funzioniint"><p><input type="button" value="Cerca" class="frmbtn" title="Avvia la ricerca" onclick="javascript:return hitGoAdv(&quot;complex&quot;);" /><input type="button" title="Annulla la ricerca" value="Annulla" class="frmbtn" onclick="javascript:hitItemAdv(&quot;complex&quot;);" /></p></div></fieldset>');
}

function designForm2(s1) {
	document.write('<fieldset id="fieldgql"><legend><span class="testotitolo"><strong>Trova risultati che...</strong></span></legend><p class="left testostandard"><label for="text_gql">Corrispondano alla seguente espressione <acronym title="Generalized Query Language">GQL</acronym>:</label></p><p class="txtright"><input class="frmtxt big" id="text_gql" name="text_gql" type="text" value="'+s1+'" /></p><div class="funzioniint"><p><input type="button" title="Avvia la ricerca" value="Cerca" class="frmbtn" onclick="javascript:hitGoAdv(&quot;gql&quot;);" /><input type="button" title="Annulla la ricerca" value="Annulla" class="frmbtn" onclick="javascript:hitItemAdv(&quot;gql&quot;);" /></p></div></fieldset>');
}

function designForm3Open() {
	document.write('<div class="divstandard"><input type="hidden" name="avanzata" value="true" /><input type="hidden" name="risultati" /><input type="hidden" name="action" value="" /><input type="hidden" name="DESQueryId" value="" /><input type="hidden" name="DESGetPage" value="" /><input type="hidden" name="type" value="" /><input type="hidden" name="web" value="" /><input type="hidden" name="text_and" value="" /><input type="hidden" name="text_exact" value="" /><input type="hidden" name="text_or" value="" /><input type="hidden" name="text_not" value="" /><input type="hidden" name="DESQueryString1" value="" /></div><fieldset id="fieldsett" class="nomargb"><legend><span class="testotitolo"><strong>Cerca nei seguenti settori...</strong></span></legend><div class="divStandard">');
}

function designForm3ChkOpen() {
	document.write('<div class="settoriadv"><p class="left">');
}

function designForm3ChkRight() {
	document.write('<p class="right">');
}

function designForm3ChkRadio(name,value,sector) {
	document.write('<input type="checkbox" name="'+name+'" id="'+name+'" value="'+value+'" checked="checked" />&nbsp;<span class="hidden">Settore&nbsp;</span><label for="'+name+'">'+sector+'</label></p>');
}

function designForm3ChkClose() {
	document.write('<div class="clear"></div></div>');
}

function designForm3Close(counter) {
	document.write('<div class="funzioniint"><input type="hidden" name="counter" value="'+counter+'" /><p><input type="button" class="frmbtn" value="Seleziona tutto" name="btnselectall" onclick="SetChecks(&quot;on&quot;);" /><input type="button" class="frmbtn" value="Deseleziona tutto" name="btnDeselectAll" onclick="SetChecks(&quot;off&quot;);" /></p></div></fieldset>');
}


/*=====================================================
   newsletter
=====================================================*/

function newsletterSelect(id) {
	if (document.getElementById("n"+id+"check").checked==true) {
		document.getElementById("n"+id+"a").style.display="block";
		document.getElementById("n"+id+"b").style.display="block";
	}
	else {
		document.getElementById("n"+id+"a").style.display="none";
		document.getElementById("n"+id+"b").style.display="none";
	}
}

function aggiornaEmail() {
	if (document.forms["emailform"].newemail.value == null || document.forms["emailform"].newemail.value == "") {
		alert ("Inserire un'indirizzo e-mail");
		return false;
	}
	else {
		appoMail = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$");
		if (appoMail.test(document.forms["emailform"].newemail.value)== false) {
			alert("Formato dell'indirizzo e-mail non corretto");
			document.forms["emailform"].newemail.value="";
			document.forms["emailform"].newemail.focus();
			return false; 
		} 
		else {
			return true;
		}
	}
}


/*=====================================================
   santi del giorno - invio mail (temporanea)
=====================================================*/

function SaintSend(str_date) {
	var objForm=document.getElementById("frmSaint");
	var objEl;
	var strMail="mailto:?subject=dal sito del Comune di Roma: Tanti auguri!&body=Oggi, "+str_date+" e' ";
	for (i=0; i<objForm.elements.length; i++) {
		objEl=objForm.elements[i];
		if (objEl.type=="radio") {
			if (objEl.checked) {
				strMail+=objEl.value+".  Tanti auguri!";
				location.href=strMail;
			}
		}
	}
}


/*=====================================================
   cerco e offro (temporaneo)
=====================================================*/

function trim(arg) {
      var trimvalue = "";
      arglen = arg.length;
      if (arglen < 1) return trimvalue;
      i = 0;
      pos = -1;
      while (i < arglen) {
          if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
              pos = i;
              break;
          }
          i++;
      }
      var lastpos = -1;
      i = arglen;
      while (i >= 0) {
          if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
              lastpos = i;
              break;
          }
          i--;
      }
      trimvalue = arg.substring(pos,lastpos + 1);
      return trimvalue;
}

function imgfile(sw) {
	if(sw==1) document.getElementById("image").style.display="block";
	else document.getElementById("image").style.display="none";
}

function invia() {
	var subm = true;
	var message = "";
	var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	var fileimmagine = trim(document.getElementById("imagefile").value);
	if (trim(document.getElementById("title").value)=="") {
		message = message + "Inserire un titolo\n";
		subm = false;	
	}
	if (trim(document.getElementById("content").value)=="") {
		message = message + "Inserire una descrizione\n";
		subm = false;	
	}
	if (trim(document.getElementById("email").value)=="") {
		message = message + "Inserire un indirizzo e-mail\n";
		subm = false;	
	}
	else if (!emailFilter.test(trim(document.getElementById("email").value))) {
		message = message + "Il formato dell'indirizzo e-mail non è corretto\n";
		subm = false;
	}
	if (isNaN(trim(document.getElementById("phone").value))) {
		message = message + "Il formato del numero telefonico non è corretto\n";
		subm = false;	
	}
	if (isNaN(trim(document.getElementById("price").value))) {
		message = message + "Il formato del prezzo non è corretto";
		subm = false;	
	}
	if (fileimmagine!="") {
		if (document.getElementById("imagefile").value!=fileimmagine) {
			message = message + "Il nome del file non può iniziare o terminare con uno spazio vuoto\n";
			subm = false;		
		}
		if (!(fileimmagine.lastIndexOf('.gif')==fileimmagine.length-4 || fileimmagine.lastIndexOf('.jpg')==fileimmagine.length-4 || fileimmagine.lastIndexOf('.png')==fileimmagine.length-4)) {
			message = message + "Formato del file immagine non valido\n";
			subm = false;	
		}
	}
	if (subm) return true;
	else {
			alert(message);
			return false;
	}
}