﻿//Visualizza allegati immagine all'interno dell apagina

function viewImg(immagine){

if(document.getElementById('viewImg').style.display=='inline'){
document.getElementById('viewImg').style.display='none';
}else{
    document.getElementById('viewImg').innerHTML='<img src=image/'+immagine+'>';
    document.getElementById('viewImg').style.display='inline';
}
}

function segnalaUrl(path){
    testo="?subject=segnalazione url&"
    testo+="Body=Gentile utente,Le consiglio di visionare il seguente indirizzo: " + path;
    parent.location.href='mailto:'+testo;
}

function skan(objname)
{
 var a=new Array(); var k=0; var obj=eval(objname);var h;
 for (i in obj){
  if((typeof obj[i])=='object' && obj[i]!=null)
  {
   h=(parseInt(i)==i)
   a[k]="<a href='javascript:base.skan(\""+objname+(h?"[":".")+i+(h?"]":"")+"\")'>"+i+"</a>";
  }
  else
   a[k]=i.replace(/</g,'&lt;');
  a[k]="<li><b>"+a[k++]+"</b>="+obj[i];
 }
 var w=window.open('','','width=400,height=500,scrollbars=yes,resizable=yes');
 w.document.write("<body style='font:10px Verdana'><h3>"+objname+"</h3><ol>"+a.sort()+"</ol>")
 this.base=w.base=window;
}

function ChangeNote(obj)
{
//alert(obj.value)
    if (obj.value == 2 |obj.value == 3)
    {
        document.getElementById('note').innerHTML = 'Provincia:'

    }
    else
    {
        document.getElementById('note').innerHTML = 'Regione:'
    }
        
}

//Scrive data e ora : caricata nel load del body 

function writeDate(){
    dayName=new Array ("Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato") 

    monName=new Array ("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre") 

    now = new Date();
    
    ora = now.getHours();
    if(ora<10)
        ora = "0" + ora;
    min = now.getMinutes();
    if(min<10)
        min = "0" + min;
   
    sec = now.getSeconds();
    if(sec<10)
        sec = "0" + sec;   
    
    data=dayName[now.getDay()] + " " + now.getDate() + " " + monName[now.getMonth()];
    data+=" " + ora + ":" + min + "." + sec + "&nbsp; ";
    
    document.getElementById("ore").innerHTML =data;  
   }
   

//Apre una pop-up:possibilità di settaggi odi tutti i parametri
   function apri (URL){
	var args=arguments;
	var w=args[1]?args[1]:280;
	var h=args[2]?args[2]:300;
	var l=args[3]?args[3]:((screen.width-w)/2);
	var t=args[4]?args[4]:((screen.height-h)/2);
	var s=args[5]?args[5]:'no';
	var name=args[6]?args[6]:'';
	var f=args[7]?args[7]:'no';
	var features ="menubar=no,toolbar=no,width="+w+",height="+h+",status=no,scrollbars="+s+",top="+t+",left="+l+",resizable=no";
	finestra = window.open(URL,name,features);
	if(f=='yes')
		finestra.focus() 
}

/***************************************************************\
*
* alternateRows() V2.0: funzione da eseguire onLoad della pagina che
* imposta le righe alternate nelle viste, tabelle o altro.
* La funzione va in cerca degli elementi con classe "altern" e setta la classe 
* alternativamente a r0 e r1.
*
\***************************************************************/
function alternateRows(){
	var progressivo = 0;
	for (i=0;i<document.all.length;i++){
		if(document.all[i].className.indexOf("altern")!=-1)
			document.all[i].className = document.all[i].className.replace(/altern/,"r" + ((progressivo++)%2));
	}
}

// FUNZIONI DI VALIDAZIONE DEI FORM

function salva(chiudiIntervento){

	var args=arguments;
	// campi di tipo testo 
	var tx=args[0]?args[0]:0;
	// campi mail
	var ma=args[1]?args[1]:0;
	// campi tipo numero
	var nb=args[2]?args[2]:0;
	
	if(tx!="0"){	
		for(i=0;i<tx.length;i++){
			var ele= tx[i].split('#');
			
			if(!validaTesto(document.getElementById(ele[0]),ele[1]))
				return false;		
		}
	}
	
	if(ma!="0"){	
		for(i=0;i<ma.length;i++){
			var ele= ma[i].split('#');
			
			if(!validaMail(document.getElementById(ele[0]),ele[1]))
				return false;		
		}
	}
	
	if(nb!="0"){	
		for(i=0;i<nb.length;i++){
		var ele= nb[i].split('#');
		var msg="Il formato del campo " + ele[1] + " non è valido";
		alert(document.getElementById(ele[0]).value)
		if(!checkNumber(document.getElementById(ele[0]).value,msg,"/")){     
			alert(ele[1])
			return false;		
			}
		}
	}

	if (document.getElementById("hd_statoIncidente")!=null & chiudiIntervento==1)
	{
	    if(confirm('Chiudendo l\'intervento i dati saranno esportati nella banca dati incidenti. Sei sicuro di voler continuare?'))
	    {
	       
	        document.getElementById("hd_statoIncidente").value=1;
	    }
	    else
	        return false;
	    
	}
	if(document.getElementById("check")!=null)
	{
		document.getElementById("check").value=1;
    }
		
	document.form1.submit();
}


function ControllaOreMinuti(campo, contr)
{   
    var OK = true;
    if (contr == 'int24')
    {
        if (ConvalidaNumeroFull(campo.value,"Attenzione!!! Valore non valido.")==false)
	    {				    
		    OK = false;
	    }   
        if (campo.value < 0 |campo.value > 23)
        {
            alert("Il campo ore deve essere compreso tra 0 e 23");
            OK = false;
        }
        if (campo.value == '')
            campo.value = '00';
    }
    
     if (contr == 'int60')
    {
        if (ConvalidaNumeroFull(campo.value,"Attenzione!!! Valore non valido.")==false)
	    {				    
		    OK = false;
	    }   
        if (campo.value < 0 |campo.value > 59)
        {
            alert("Il campo minuti deve essere compreso tra 0 e 60");
            
            OK = false;
        }
        if (campo.value == '')
            campo.value = '00';
    }
    if (OK==false)
        campo.focus();
}

function SalvaIncidente()
{

    var campo = true;
	
	for (x=0; x<document.aspnetForm.length; x++)
	{
		if (document.aspnetForm[x].obblig == 'vero')
		{
		    //alert('--id=' +document.aspnetForm[x].disabled+ '--val='  + document.aspnetForm[x].value  +'--display='+ document.getElementById(document.aspnetForm[x].id).style.display+ + '--disabled=' + document.aspnetForm[x].disabled + '--contr=' + document.aspnetForm[x].contr )
	  
		  	if (document.aspnetForm[x].value == "" && document.getElementById(document.aspnetForm[x].id).style.display == 'block' && document.aspnetForm[x].disabled != true )
			{			
				alert('Inserire il campo '+document.aspnetForm[x].nome)
				document.aspnetForm[x].focus();
				campo = false;
				break;
			}
			//controllo delle percentuali
			else if (document.aspnetForm[x].contr == "perc" && document.getElementById(document.aspnetForm[x].id).style.display == 'block' && document.aspnetForm[x].disabled != true )
			{
				if (ConvalidaNumeroInt(document.aspnetForm[x].value))
				{
					if (AggiornaCampo(document.aspnetForm[x].id,document.aspnetForm[x].value)!= true)
					{
						alert("La somma dei valori delle colonne deve essere uguale a 100")
						document.aspnetForm[x].focus();
						campo = false;
						break;
					}
				}
				else
				{
						document.aspnetForm[x].focus();
						campo = false;
						break;
				}
			}
			else if (document.aspnetForm[x].contr == "int" && document.aspnetForm[x].disabled != true )
			{
			   
				if (ConvalidaNumeroFull(document.aspnetForm[x].value,"Attenzione!!! Valore non valido.")==false)
				{
					document.aspnetForm[x].focus();
					campo = false;
					break;
				}
			}
			else if (document.aspnetForm[x].contr == "int24" && document.aspnetForm[x].disabled != true )
			{
			   if (document.aspnetForm[x].value < 0 | document.aspnetForm[x].value > 23)
			   {
			        alert("Il campo ore deve essere compreso tra 0 e 23");
			        document.aspnetForm[x].focus();
					campo = false;
					break;
			   }
				if (ConvalidaNumeroFull(document.aspnetForm[x].value,"Attenzione!!! Valore non valido.")==false)
				{				    
					document.aspnetForm[x].focus();
					campo = false;
					break;
				}
				
			}
			else if (document.aspnetForm[x].contr == "int60" && document.aspnetForm[x].disabled != true )
			{
			   if (document.aspnetForm[x].value < 0 | document.aspnetForm[x].value > 59)
			   {
			        alert("Il campo minuti deve essere compreso tra 0 e 59");
			        document.aspnetForm[x].focus();
					campo = false;
					break;
			   }
				if (ConvalidaNumeroFull(document.aspnetForm[x].value,"Attenzione!!! Valore non valido.")==false)
				{				    
					document.aspnetForm[x].focus();
					campo = false;
					break;
				}
				
			}
			else if (document.aspnetForm[x].contr == "data" && document.aspnetForm[x].disabled != true)
			{
				if (isDate(document.aspnetForm[x].value)==false)
				{
					document.aspnetForm[x].focus();
					alert('Attenzione!!! Data non valida.\nIl formato per la data \u00e8 dd/mm/yyyy.'); 
	
					campo = false;
					break;
				}
			}
			else if (document.aspnetForm[x].value == "" && document.aspnetForm[x].disabled != true || document.aspnetForm[x].value == " " && document.aspnetForm[x].disabled != true)
			{
			     
				alert('Inserire il campo '+document.aspnetForm[x].nome)
				document.aspnetForm[x].focus();
				campo = false;
				break;
			}
			campo = true;
			if (document.getElementById('salva')!=null)
				document.getElementById('salva').value=1;
		}
	}
	
	if (campo == true)
		document.aspnetForm.submit();
}

function updateDataSet(){

	//if(document.getElementById("check")!=null)
      //  document.getElementById("check").value=2;
        
    document.form1.submit();
}

function validaTesto(obj) {
	var Label=(arguments[1]?arguments[1]:obj.name);
	if (obj.value==""){
		alert('Il campo ' + Label + ' \u00e8 obbligatorio');
		obj.focus();
		obj.select();
		return false;
	}
	if (obj.value.match(/[>§@<]/)){
		alert('Caratteri non ammessi nel campo ' + Label);
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}

function validaMail(obj){

	var it=obj.value
	
	if (!(it.match(/^\w+((\.|-){1}\w+)*\@{1}\w+((\.|-){1}\w+)*$/))){
		alert("L'indirizzo e-mail non \u00e8 corretto o \u00e8 incompleto");
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}

function checkNumber(obj,label,sep){
	var msg ="Il formato del campo: " + label + " non \u00e8 riconosciuto";  
	if(!ConvalidaNumeroFull(obj.value,msg,sep)){
		obj.focus();
		obj.value='';
	}
}



//*****************************


//Function necessarie per la popUp Calendar

var dateformat = "dd/MM/yyyy";

function getFormatDate(){
  return dateformat;
}


function checkDate(obj){

if(!isDate(obj.value)){

	alert('Attenzione!!! Data non valida.\nIl formato per la data \u00e8 dd/mm/yyyy.'); 
	obj.focus();
	obj.value='';
	}
}


function isDate(sd) {

if (sd!= "")
{
  var sd1 = sd;
  var dateformat1 = dateformat;
  if(sd1.length != dateformat1.length)return false;
  
  var indexyyyy = dateformat1.indexOf("yyyy");
  var indexyy;
  if(indexyyyy<0)
    indexyy = dateformat1.indexOf("yy");
  var indexMM = dateformat1.indexOf("MM"); 
  var indexdd = dateformat1.indexOf("dd");

  //estrazioni valori data
  var giorno = sd1.substring(indexdd,indexdd + 2);
  var mese = sd1.substring(indexMM,indexMM + 2);
  if(indexyyyy >= 0)
    var anno = sd1.substring(indexyyyy,indexyyyy + 4);
  else
    var anno = sd1.substring(indexyy,indexyy + 2);

  //controllo che siano dei numeri
  if(!ConvalidaNumeroInt(anno))return false;
  if(!ConvalidaNumeroInt(mese))return false;
  if(!ConvalidaNumeroInt(giorno))return false;

  //converto in numero
  annonum = anno * 1;
  mesenum = mese * 1;
  giornonum = giorno * 1;

  //gestione date che sono sbagliate
  if(mesenum < 1 || mesenum > 12)return false;
  if(mesenum == 2 && giornonum >=30)return false;
  if( (mesenum == 1 || mesenum == 3 || mesenum == 5 || mesenum == 7 || mesenum == 8 || mesenum == 10 || mesenum == 11 || mesenum == 12 ) && giornonum > 31) return false;
  if( (mesenum == 4 || mesenum == 6 || mesenum == 9) && giornonum > 30) return false;

  //gestione separatori
  //sostituisco nel formato agli indicatori di parte di data con i valori trovati prima, 
  //se dopo la sostituzione la stringa è uguale a quella data allora i separatori sono uguali a quelli del formato presente del DB
  if(indexyyyy < 0)
  {
    var newdate = dateformat1.substring(0,indexyy) + anno + dateformat1.substring(indexyy + 4)
    newdate = newdate.substring(0,indexMM) + mese + newdate.substring(indexMM + 2)
    newdate = newdate.substring(0,indexdd) + giorno + newdate.substring(indexdd + 2)
  }
  else
  {
    var newdate = dateformat1.substring(0,indexyyyy) + anno + dateformat1.substring(indexyyyy + 4)
    newdate = newdate.substring(0,indexMM) + mese + newdate.substring(indexMM + 2)
    newdate = newdate.substring(0,indexdd) + giorno + newdate.substring(indexdd + 2)
  }

  if(newdate != sd1)return false;
}
  return true;
}

function ConvalidaNumeroInt(valore)
{
  return ConvalidaNumeroFull(valore,"Inserire un valore valido.","")
}

function ConvalidaNumeroFull(valore,Messaggio,separatori) 
{

	var checkOK = "0123456789- " + separatori;
	var checkStr = valore;
	var allValid = true;
	
	for (i = 0; i < checkStr.length; i++) 
	{
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) 
		{
			allValid = false;
			break;
		}
	}
	if (!allValid) 
	{
      if(Messaggio.length > 0){
        alert(Messaggio);
      }
	  return false;
	}
  return true; 
}

//***********************+



