var map_zoom=1;

// JavaScript Document
// Serve a passare da una pagina all'altra dell'elenco degli utenti
function jump(n) {			  
 document.formfiltri.limit.value = n;
 document.formfiltri.submit();
}

function Trim(stringa) {
return stringa.replace(/\s+$|^\s+/g,"");
}

function apri(url) 
{
 params  = 'width='+screen.width;
 params += ', height='+screen.height;
 params += ', top=0, left=0'
 params += ', scrollbars=1'

 newwin=window.open('win_img.php?url='+url,'windowscreen', params);
 if (window.focus) {newwin.focus()}
 return false;
}


function verifica_data (gg,mm,aa) {
   var strdata = gg+"/"+mm+"/"+aa;
   data = new Date(aa,mm-1,gg);
   daa=data.getFullYear().toString();
   dmm=(data.getMonth()+1).toString();
      dmm=dmm.length==1?"0"+dmm:dmm
   dgg=data.getDate().toString();
      dgg=dgg.length==1?"0"+dgg:dgg
   dddata=dgg+"/"+dmm+"/"+daa
   if (dddata!=strdata) return false;
	else return true;
}

function checkEmail(email)
{
  var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.biz)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (goodEmail) good = true;
  else good = false;
  
  if (Trim(email) == "") good = true;
  return good;
}

function noHtml(txt) {
    a = txt.indexOf('<');
    b = txt.indexOf('>');
    len = txt.length;
    c = txt.substring(0, a);
    if(b == -1) {
       b = a;
    }
    d = txt.substring((b + 1), len);
    txt = c + d;
    cont = txt.indexOf('<');
    if (cont != -1) {
      txt = noHtml(txt);
    }
    return txt;
}
function remove_badchar (str) {
 if (Trim(str) != "") {
 // per prima cosa elimino i tag html
 res = noHtml(str);
 res = res.replace ('\"', '&quot;');
 // trasformo le & in un codice riconoscibile: "*:and:*"
 res = res.replace ('&', '*:and:*');
 } else res=str;
return res;	
}
function waiting_msg() {
 return '<div class="waiting"><p><img src="images/ajax-loader_small.gif" alt="please wait" /></p><p>Loading, please wait...</p></div>';
}
function genera_news(tipo) {
 var mydiv = document.getElementById("div_news_content");
 mydiv.innerHTML = waiting_msg();
 agent.call('','genera_news','genera_news_ret', tipo);
}
function genera_news_ret(ret) {
 var mydiv = document.getElementById("div_news_content");
 mydiv.innerHTML = ret;
}

function zoom_map(s_zoom) {
	map_zoom = map_zoom+s_zoom;
	if (map_zoom==0) map_zoom = 3;
	else if (map_zoom != 1 && map_zoom != 2 && map_zoom != 3) map_zoom = 1;
	var myimg = document.getElementById("mappa_alba");
	myimg.src = 'images/Contattaci/dove'+map_zoom+'.gif';
}

function check_prev1 () {

var err=0;

var str_err_1 = "";

var nummoduli = document.getElementById('nummoduli').value;

if (document.form1.privacy[0].checked==false) err =4;

var security_code = document.form1.security_code.value;
var security_code_id = document.form1.security_code_id.value;
codici_sicurezza = Array();
codici_sicurezza[1] = "xn7vbc";
codici_sicurezza[2] = "sjhpbx";
codici_sicurezza[3] = "fng9sf";
codici_sicurezza[4] = "nx7dkk";
codici_sicurezza[5] = "3jszj3";
codici_sicurezza[6] = "y85bps";
if (security_code != codici_sicurezza[security_code_id]) err=3;

var bottone = document.form1.sub1;

var ragsoc1 = document.form1.ragsoc1.value;
var locali = document.form1.locali.value;
var cap = document.form1.cap.value;
var provi = document.form1.provi.value;
var piva = document.form1.piva.value;
var rifper = document.form1.rifper.value;
var telefo = document.form1.telefo.value;
var mese_acq = document.form1.mese_acq.value;
var anno_acq = document.form1.anno_acq.value;
var mese_pass = document.form1.mese_pass.value;
var anno_pass = document.form1.anno_pass.value;
var email_prev = document.form1.email_prev.value;

if (Trim(ragsoc1)=="") 	str_err_1+= "Ragione Sociale\n";
if (Trim(locali)=="") 	str_err_1+= "Localita\n";
if (Trim(cap)=="") 		str_err_1+= "CAP\n";
if (Trim(provi)=="") 	str_err_1+= "Provincia\n";
if (Trim(piva)=="") 	str_err_1+= "P.IVA\n";
if (Trim(rifper)=="") 	str_err_1+= "Persona Riferimento\n";
if (Trim(telefo)=="") 	str_err_1+= "Telefono\n";
if (Trim(mese_pass)=="") str_err_1+= "Mese installazione\n";
if (Trim(anno_pass)=="") str_err_1+= "Anno installazione\n";
if (Trim(email_prev)=="") str_err_1+= "Email\n";
if (str_err_1!="") err=1;

else if (!checkEmail(email_prev))  err=5;

//verifico moduli
if (err==0) {
 var debug_txt = "";
 var mymodulo;
 var mycat;
 var myobbli;
 var categorie_sel = new Array;
 for (m=1;m<=nummoduli;m=m+1) {
	mymodulo = document.getElementById('modulo_'+m);
	mycat = document.getElementById('cat_mod_'+m).value;
	myobbli = document.getElementById('obbli_mod_'+m).value;
	if (categorie_sel[mycat]==undefined) categorie_sel[mycat] = 0;
	
	if (mymodulo===null) continue;
	//se il modulo è 'S' o un numero>0, allora incremento il valore della categoria corrispondente
	if (mymodulo.value=="S" || parseInt(mymodulo.value)>0) categorie_sel[mycat] = categorie_sel[mycat]+1;
	//se è obbligatorio verifico l'inserimento
	if (myobbli==1) {
		if (Trim(mymodulo.value)=='' || mymodulo.value==0) {
		 err=1;
		 str_err_1+= document.getElementById('modulo_eti_'+m).innerHTML+"\n";
		}
	}//myobbli
	
 }
 //ciclo per le categorie
 for (var Categoria in categorie_sel) {
  //se per quella categoria è stato selezionato almeno un modulo
  // e esiste il campo "ute_cat_"
  // allora verifico che sia >0
  if (categorie_sel[Categoria]>0 && document.getElementById('ute_cat_'+Categoria)!==null) {
	if (!(parseInt(document.getElementById('ute_cat_'+Categoria).value)>0)) {
		alert(document.getElementById('cat_descr_'+Categoria).value+"\nIndicare il numero di utenti con accesso contemporaneo."); 
		err=999;
	}
  }
 }
 
} //err==0
// fine verifica moduli


if (err == 1) {
	alert("Inserire tutti i campi obbligatori:\n"+str_err_1);
	return false;
} else if (err == 2) {
	alert("Seleziona almeno un prodotto");
	return false;
} else if (err == 3) {
	alert("Inserire correttamente il codice di sicurezza");
	return false;
} else if (err == 4) {
	alert("Occorre accettare la normativa sul trattamento dei dati personali.");
	return false;
} else if (err == 5) {
	alert("Inserire un'indirizzo email valido");
	return false;
} else if (err == 6) {
	alert("Licenze aggiuntive: inserire un numero");
	return false;
} else if (err == 7) {
	alert("Numero PC: inserire un numero");
	return false;
//errore 999: il messaggio è già stato inviato, devo solo ritornare FALSE
} else if (err == 999) {
	return false;	
} else {
	bottone.disabled=true;
	return true;
}

}




function check_prev2 () {

var err=0;

var str_err_1 = "";

if (document.form2.privacy[0].checked==false) err =4;

var security_code = document.form2.security_code.value;
var security_code_id = document.form2.security_code_id.value;
codici_sicurezza = Array();
codici_sicurezza[1] = "xn7vbc";
codici_sicurezza[2] = "sjhpbx";
codici_sicurezza[3] = "fng9sf";
codici_sicurezza[4] = "nx7dkk";
codici_sicurezza[5] = "3jszj3";
codici_sicurezza[6] = "y85bps";

if (security_code != codici_sicurezza[security_code_id]) err=3;

var bottone = document.form2.sub2;

var ragsoc1 = document.form2.ragsoc1.value;
var locali = document.form2.locali.value;
var cap = document.form2.cap.value;
var provi = document.form2.provi.value;
var piva = document.form2.piva.value;
var rifper = document.form2.rifper.value;
var telefo = document.form2.telefo.value;
var mese_acq = document.form2.mese_acq.value;
var anno_acq = document.form2.anno_acq.value;
var mese_pass = document.form2.mese_pass.value;
var anno_pass = document.form2.anno_pass.value;
var email_prev = document.form2.email_prev.value;

var n_ute = document.form2.n_ute.value;

var e_bus = document.form2.e_bus.value;
var e_sca = document.form2.e_sca.value;
var e_ord = document.form2.e_ord.value;
var e_fat = document.form2.e_fat.value;
var e_mag = document.form2.e_mag.value;
var e_ces = document.form2.e_ces.value;
var e_com = document.form2.e_com.value;
var e_doc = document.form2.e_doc.value;

if (Trim(ragsoc1)=="") 	str_err_1+= "Ragione Sociale\n";
if (Trim(locali)=="") 		str_err_1+= "Localita\n";
if (Trim(cap)=="") 			str_err_1+= "CAP\n";
if (Trim(provi)=="") 		str_err_1+= "Provincia\n";
if (Trim(piva)=="") 		str_err_1+= "P.IVA\n";
if (Trim(rifper)=="") 		str_err_1+= "Persona Riferimento\n";
if (Trim(telefo)=="") 		str_err_1+= "Telefono\n";
if (Trim(mese_pass)=="") 	str_err_1+= "Mese installazione\n";
if (Trim(anno_pass)=="") 	str_err_1+= "Anno installazione\n";
if (Trim(n_ute)=="" || isNaN(n_ute)) 	str_err_1+= "Numero utenti\n";
if (Trim(email_prev)=="") 	str_err_1+= "Email\n";
if (str_err_1!="") err=1;

else if (Trim(e_bus)=="" && 
Trim(e_sca)=="" && 
Trim(e_ord)=="" && 
Trim(e_fat)=="" && 
Trim(e_mag)=="" && 
Trim(e_ces)=="" && 
Trim(e_com)=="" && 
Trim(e_doc)=="") err=2;

else if (!checkEmail(email_prev)) err=5;

if (err == 1) {
	alert("Inserire tutti i campi obbligatori:\n"+str_err_1);
	return false;
} else if (err == 2) {
	alert("Seleziona almeno un prodotto");
	return false;
} else if (err == 3) {
	alert("Inserire correttamente il codice di sicurezza");
	return false;
} else if (err == 4) {
	alert("Occorre accettare la normativa sul trattamento dei dati personali.");
	return false;
} else if (err == 5) {
	alert("Inserire un'indirizzo email valido");
	return false;
} else {
	bottone.disabled=true;
	return true;
}

}

/*
function show_div_prev (n) {
 var div_prev_main = document.getElementById("div_prev_main");
 var div_prev1 = document.getElementById("div_prev1");
 var div_prev2 = document.getElementById("div_prev2");
 if (n==1) {
 	div_prev_main.style.display="none";
 	div_prev1.style.display="block";
 	div_prev2.style.display="none";
 } else if (n == 2) {
 	div_prev_main.style.display="none";
 	div_prev1.style.display="none";
 	div_prev2.style.display="block";
 } else {
 	div_prev_main.style.display="block";
 	div_prev1.style.display="none";
 	div_prev2.style.display="none";
 }
}*/
