// rende visibile/invisibile un div //
// parametro: id del div            //
// NB. nel div ci dev'essere:       //
// style= "display: none;"          //

function mostra(blocco){
    var el=document.getElementById(blocco);
    el.style.display=(el.style.display=='none')?'block':'none';
}

// funzioni per la googleMaps API   //
//                                  //
//                                  //
//                                  //

// ===== request the directions =====
function getDirections() {
    // ==== Set up the walk and avoid highways options ====
    var opts = {};
    if (document.getElementById("walk").checked) {
       opts.travelMode = G_TRAVEL_MODE_WALKING;
    }
    if (document.getElementById("highways").checked) {
       opts.avoidHighways = true;
    }
    // ==== set the start and end locations ====
    var saddr = document.getElementById("saddr").value
    var daddr = document.getElementById("daddr").value
    gdir.load("from: "+saddr+" to: "+daddr, opts);
}

// This function picks up the click and opens the corresponding info window
function myclick(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// functions that open the directions forms
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

// A function to create the marker and set up the event window
function createMarker(point,name,html) {  
    var marker = new GMarker(point);  
    
    var i = gmarkers.length;
    
    // The info window version with the "to here" form open
    to_htmls[i] = html + '<br />indicazioni stradali: <strong>Fino a qui<\/strong> oppure <a href="javascript:fromhere(' + i + ')">Da qui<\/a>' +
    '<br />indirizzo di partenza:<form action="javascript:getDirections()">' +
    '<input type="text" size="40" maxlength="40" name="saddr" id="saddr" value="" /><br />' +
    '<input value="Get Directions" type="submit" /><br />' +
    'a piedi <input type="checkbox" name="walk" id="walk" /> &nbsp; non autostrada <input type="checkbox" name="highways" id="highways" />' +
    '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
    '" />';
    // The info window version with the "from here" form open
    from_htmls[i] = html + '<br />indicazioni stradali: <a href="javascript:tohere(' + i + ')">Fino a qui<\/a> - <b>Da qui<\/b>' +
    '<br />indirizzo di arrivo:<form action="javascript:getDirections()">' +
    '<input type="text" size="40" maxlength="40" name="daddr" id="daddr" value="" /><br />' +
    '<input value="Get Directions" type="submit" /><br />' +
    'a piedi <input type="checkbox" name="walk" id="walk" /> &nbsp; non autostrada <input type="checkbox" name="highways" id="highways" />' +
    '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
    '"/>';
    // The inactive version of the direction info
    html = html + '<br />indicazioni stradali: <a href="javascript:tohere('+i+')">Fino a qui<\/a> - <a href="javascript:fromhere('+i+')">Da qui<\/a>';
    
    GEvent.addListener(marker, "click", function() {  
        marker.openInfoWindowHtml(html);  
    });  
    // save the info we need to use later for the side_bar
    gmarkers.push(marker);
    htmls[i] = html;
    return marker;
}


// per abilitare/disabilitare un campo - CampoTarget - a seconda del valore di un altro canpo - CampoTest//
function abilita(CampoTest,CampoTarget,Valore)
	{

	CampoTarget.disabled=true;
	if (CampoTest.value==Valore) {
		CampoTarget.disabled = false;
		CampoTarget.focus();
		}
	else {
		CampoTarget.disabled = true;
		}
	}


// verifica della correttezza dell'indirizzo email tramite regular expression. il nome del campo DEVE finire per "email"//
//il name del campo email deve finire obbligatoriamente per "email"!!!!!//
function verificaEmail(nomeForm)
{
	//creo variabile filtro per la regular expression di verifica email//
	var Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

		//questo fa il parsing dell'array degli elementi del form//
		for (var j=0; j<nomeForm.elements.length; j++)
			{
				objTest = nomeForm.elements[j];
				var strNome = objTest.name
				var i = strNome.length
				
				if (strNome.substring(i,i-5) == "email")
				{
					if (!Filtro.test(objTest.value))
					{
					alert("l'indirizzo di e-mail inserito non &egrave; un indirizzo valido");
 	    			objTest.focus();
					return false;
					}
					else
					{
						return true;
					}
				}
			}
	
}
// verifica che non ci siano campi obbligatori lasciati vuoti e poi chiama la function verificaEmail//
//devo aggiungere "req" ai nomi di campi obbligatori!!!!!!!!//
//un giorno o l'altro ci aggiungo anche i radiobutton e i checkbox....//
function validazione(nomeForm)
{
    //creo l'oggetto array dove mettere i campi obbligatori//
	var objTest;

	//creo un flag per il test finale del return, inizializzato a true//
	var flag = true;

// label per interrompere il ciclo ogni volta che c'č un campo obbligatorio vuoto//
fine:
		//ciclo gli oggetti del form//
		for (var j=0; j<nomeForm.elements.length; j++)
		{
		flag = false;

			//se il nome dell'elemento comincia con "req" allora viene testato//
			if (nomeForm.elements[j].name.substring(0,3)=="req")
			{
				objTest = nomeForm.elements[j];

				//se si tratta di un campo di testo//
				if (objTest.type=="text"||objTest.type=="textarea")
				{			
					//controllo che il campo testo non sia vuoto!//
					if (objTest.value=="")
					{
						alert("manca un campo obbligatorio, per favore controlla!");					
						objTest.focus();
						flag = false;
						break fine;
					}
					else
					{
						flag = true;
					}
				}
				else
				{
					//se si tratta di una select//
					if (objTest.type.substring(0,6)=="select")
					{
						//controllo che non sia settata l'option-civetta (vedi commenti file config.php)//
						if (objTest.selectedIndex == 0)
						{
							alert("manca un campo obbligatorio, per favore controlla!");					
							objTest.focus();
							flag = false;
							break;
						}
						else
						{
							flag = true;
						}
					}
				}
			}
			else
			{
				flag = true;
			}
		}
		//se tutti i campi obbligatori sono riempiti, passo alla verifica dell'indirizzo email//
		if (flag == true)
		{
			flag = verificaEmail(nomeForm);
		}

	//passo il valore del flag a return//
	if (flag)
		return true;
	else
		return false;
}

// apre una finestra pop-up, grande e con scrollbar
// attenzione: i file da richiamare vanno nominati cosė: popup1.html, popup2.html etc.!!!

	function avviso(tab){
		var num=null
		num=String(tab)
		pop1=window.open('popup'+num+'.htm','','scrollbars=no,resizable=no,toolbar=no,width=280,height=160,top=50,left=0')
		pop1.creator=self
	}
	
//questo serve per chiudere un popup di avviso e aprire la pagina a cui linkava nella finestra principale, al click su 
//un link

	function close_avviso(url) {
		window.creator.location=url
		self.close()
	}

// indirizza sul css adatto al browser
	
//	if(document.layers){ 
//		document.writeln("<link rel='stylesheet' href='linkns.css' type='text/css'>"); 
//	} 
	
//	if(document.all){ 
//		document.write("<link rel='stylesheet' href='link.css' type='text/css'>"); 
//	}

//	if(document.getElementById){
//		document.write("<link rel='stylesheet' href='linkns6.css' type='text/css'>");
//	}

function checkBrowser(){
        this.ver=navigator.appVersion
        this.dom=document.getElementById?1:0
        this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
        this.ie4=(document.all && !this.dom)?1:0;
        this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
        this.ns4=(document.layers && !this.dom)?1:0;
        this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
        return this
}
var bw=new checkBrowser();
//var message="Apre una nuova finestra";
var fromX=30;
var fromY=0;

function makeObj(obj){                                                          
        this.css=bw.dom? document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?document.layers[obj]:0; 
        this.wref=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj].document:0; 
        this.writeIt=b_writeIt;                                                                                                                         
        return this
}

function b_writeIt(text)
{if(bw.ns4){this.wref.write(text);this.wref.close()}
else this.wref.innerHTML=text}

var descx,descy;

function popmousemove(e)
{descx=bw.ns4?e.pageX:event.x; 
descy=bw.ns4?e.pageY:event.y}

var isLoaded;

function popupInit(){
     oDesc=new makeObj('divDescription')
     if(bw.ns4)document.captureEvents(Event.MOUSEMOVE)
     document.onmousemove=popmousemove;
     isLoaded=true;
}       
// aggiungere nell'A HREF: onmouseover="popup('TESTODELMESSAGGIO')"  onmouseout="popout()"
// aggiungere alla fine del body: <div id="divDescription">			<!-- Div vuoto, serve per lo script del popup apre nuova finestra -->		</div>
function popup(message){
    if(isLoaded){
       oDesc.writeIt('<span class="clDescription">'+message+'</span>')
 	if(bw.dom) descy=descy+document.body.scrollTop
	oDesc.css.left=descx+fromX; oDesc.css.top=descy+fromY
	oDesc.css.visibility='visible'
     }
}

function popout(){
        if(isLoaded) oDesc.css.visibility='hidden'
}
// questo č lo script generato da ImageReady per un rollover. Riutilizzabile!//
	//controllo che il brws sia una versione superiore a 3, altrimenti niente rollover!!//
function newImage(arg)
{
	if (window.document.images)
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

var preloadFlag = false;

function preloadImages(segn,addr) {
	if (window.document.images) {
		segn = newImage(addr);
		preloadFlag = true;
	}
}
function scambia(ml,addr)
{
	checkbr = (parseInt(navigator.appVersion) > 2)?true:false;
	if (checkbr && window.document.images)
	{
		window.document.ml.src = addr;
	}
}


//fine script di ImageReady per rollover//

// per abilitare i campi disabilitati - CampoTarget - se si vogliono modificare i valori//
function modifica()
{
	for (var j=0; j<document.forms[0].elements.length; j++)
	{
		x=document.forms[0].elements[j]
			x.disabled = ! x.disabled;
	}
	for (var j=0; j<document.framesets[0].elements.length; j++)
	{
		x=document.framesets[0].elements[j]
			x.disabled = ! x.disabled;
	}
	document.forms[0].elements["avanti"].disabled = true;
	document.forms[0].elements["indietro"].disabled = true;
}

//per sigest
function attiva()
{

	if (document.sigest.tipo.value==2 || document.sigest.tipo.value==3)
	{
			document.sigest.progesterna.disabled=false;
	}
	else
	{
			document.sigest.progesterna.disabled=true;
	}
}

//per sigest
//funzione per il form di gestione lavori: a seconda del tipo di atto visualizza il numero e tipo di campi giusto
//prima o poi lo standardizzo!
//function showObject(obj) {
//        if (ns4) obj.visibility = "show"
//        else if (ie4) obj.visibility = "visible"
//}

//function hideObject(obj) {
//        if (ns4) obj.visibility = "hide"
//        else if (ie4) obj.visibility = "hidden"
//}
function showObject(obj) {
        if (ns4) obj.visibility = "show"
        else if (ie4) obj.visibility = "visible"
}

function hideObject(obj) {
        if (ns4) obj.visibility = "hide"
        else if (ie4) obj.visibility = "hidden"
}

//per sigest
function visi(vl)
{
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	if (ns4)
	{
		 tipocontrdiv1 = document.tipocontrdiv;
		 repdiv1 = document.repdiv;
		 importodiv1 = document.importodiv;
	}
	if (ie4)
	{
		tipocontrdiv1 = tipocontrdiv.style;
		repdiv1 = repdiv.style;
		importodiv1 = importodiv.style;
	}

	hideObject(tipocontrdiv1);
	hideObject(repdiv1);
	hideObject(importodiv1);
	vl=eval(vl);
	switch (vl)
	{
		case 13:
			showObject(tipocontrdiv1);
			showObject(repdiv1);
			break;
		case 12:
			showObject(repdiv1);
		case 9:
		case 10:
		case 11:
			showObject(importodiv1);
		default:
	}
}

//per sigest
function progesterna(vl)
{
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	if (ns4)
	{
		 progesternadiv1 = document.progesternadiv;
	}
	if (ie4)
	{
		progesternadiv1 = progesternadiv.style;
	}

	hideObject(progesternadiv1);
	vl=eval(vl);
	if (vl==2 || vl==3)
	{
		showObject(progesternadiv1);
	}
}

//per sigest
function valida(nomeForm)
{
	//creo l'oggetto dove mettere i campi obbligatori//
	var objTest;

	//creo un flag per il test finale del return, inizializzato a true//
	var flag = true;
	function avverti(objTest2)
	{
		alert("manca un campo obbligatorio, per favore controlla!");					
		objTest2.focus();
			flag= false;
	
	}

	if (nomeForm.id_atto)
	{
		switch (nomeForm.id_atto.value)
		{
			case "0":
				alert("scegli il tipo di atto");
				flag= false;
				break;
	
			case "12":
				objTest = nomeForm.importo;
				if (objTest.value=="")
				{
					avverti(objTest);
				}
			case "13":
				if (nomeForm.rep.value=="" && flag==true)
				{
					objTest = nomeForm.rep;
					avverti(objTest);
				}
				if (nomeForm.data.value=="" && flag==true)
				{
					objTest = nomeForm.data;
					//				alert(objTest.name);
					avverti(objTest);
				}
				if (nomeForm.id_atto.value=="13" && nomeForm.tipo_contr.value=="" && flag==true)
				{
					objTest = nomeForm.tipo_contr;
					//				alert(objTest.name);
					avverti(objTest);
				}
				
				break;
			case "9":
			case "10":
			case "11":
				objTest = nomeForm.importo;
				if (objTest.value=="")
				{
					avverti(objTest);
				}
			default:
				if (nomeForm.data.value=="" && flag==true)
				{
				objTest = nomeForm.data;
					//				alert(objTest.name);
					avverti(objTest);
				}
		}		
	}
	else if (nomeForm.data_decr)
	{
		if (nomeForm.data_decr.value=="")
		{
			objTest = nomeForm.data_decr;
			//				alert(objTest.name);
			avverti(objTest);
		}
		if (nomeForm.num_decr.value=="" && flag==true)
		{
			objTest = nomeForm.num_decr;
			//				alert(objTest.name);
			avverti(objTest);
		}
	}		
	else
	{
		ctrls1[0]
		fine:
		//ciclo gli oggetti del form//
		for (var j=0; j<nomeForm.elements.length; j++)
		{
			objTest = nomeForm.elements[j];
			//controllo che il campo testo non sia vuoto!//
			if (objTest.value=="")
			{
				avverti(objTest);
				break fine;
			}
			else
			{
				flag = true;
			}
		}
	}		

	//passo il valore del flag a return//
	if (flag)
		return true;
	else
		return false;
}


