function enable_indsped(checkform, testcheck) {
	if (testcheck) {
		checkform.presso.disabled = false;
		checkform.indirizzo.disabled = false;
		checkform.cap.disabled = false;
		checkform.citta.disabled = false;
		checkform.provincia.disabled = false;
		checkform.indirizzo.focus();
	} else {
		checkform.presso.disabled = true;
		checkform.indirizzo.disabled = true;
		checkform.cap.disabled = true;
		checkform.citta.disabled = true;
		checkform.provincia.disabled = true;
	}
}

function Popup(pagename,winname,w,h,res,scr) {
	open(pagename, winname, 'width='+w+',height='+h+',left=10,top=10,resizable='+res+',scrollbars='+scr);
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function controlli_utente(checkform) {
	//da fare per utente.tpl
	return true;
}

function controlli_inviocatalogo(checkform) {
	if (!checkemail(checkform.email.value) && checkform.formato[1].checked ) {
		alert("indirizzo e-mail non valido");
		checkform.email.focus();
		checkform.email.select();
		return false;
	}	
	if (checkform.nome.value=="" && checkform.formato[0].checked) {
		alert("Il nome è necessario per il formato cartaceo");
		checkform.nome.focus();
		return false;
	}	
	if (checkform.cognome.value=="" && checkform.formato[0].checked) {
		alert("Il cognome è necessario per il formato cartaceo");
		checkform.cognome.focus();
		return false;
	}		
	if (checkform.indirizzo.value=="" && checkform.formato[0].checked) {
		alert("L'indirizzo è necessario per il formato cartaceo");
		checkform.indirizzo.focus();
		return false;
	}		
	if (checkform.cap.value=="" && checkform.formato[0].checked) {
		alert("Il cap è necessario per il formato cartaceo");
		checkform.cap.focus();
		return false;
	}		
	if (checkform.citta.value=="" && checkform.formato[0].checked) {
		alert("La citta è necessario per il formato cartaceo");
		checkform.citta.focus();
		return false;
	}		
	if (checkform.provincia.value=="" && checkform.formato[0].checked) {
		alert("La provincia è necessario per il formato cartaceo");
		checkform.provincia.focus();
		return false;
	}		
	if (checkform.email.value=="" && checkform.formato[1].checked) {
		alert("L'email è necessario per il formato cartaceo");
		checkform.email.focus();
		return false;
	}		
	
	//tutto ok
	return true;
}

function controlli_login(checkform) {
	if (checkform.login && trim(checkform.login.value) == '') {
		alert("inserire lo user");
		checkform.login.focus();
		return false;
	}	
	if (checkform.password && trim(checkform.password.value) == '') {
		alert("inserire la password");
		checkform.password.focus();
		return false;
	}	
	
	//tutto ok
	return true;
}

function controlli_registrazione(checkform) {
	if (checkform.tipo && trim(checkform.tipo.value) == '') {
		alert("specificare il tipo di utente");
		checkform.tipo.focus();
		return false;
	}
	if (checkform.area_comp && trim(checkform.area_comp.value) == '') {
		alert("area di competenza obbligatoria");
		checkform.area_comp.focus();
		return false;
	}
	if (checkform.login && trim(checkform.login.value) == '') {
		alert("user obbligatorio");
		checkform.login.focus();
		return false;
	}	
	if (checkform.azienda && trim(checkform.azienda.value) == '') {
		alert("azienda obbligatoria");
		checkform.azienda.focus();
		return false;
	}	
	if (checkform.nome && checkform.nome.value == '') {
		alert("nome obbligatorio");
		checkform.nome.focus();
		return false;
	}	
	if (checkform.cognome && checkform.cognome.value == '') {
		alert("cognome obbligatorio");
		checkform.cognome.focus();
		return false;
	}	
	if (checkform.indirizzo && checkform.indirizzo.value == '') {
		alert("indirizzo obbligatorio");
		checkform.indirizzo.focus();
		return false;
	}	
	if (checkform.cap && checkform.cap.value == '') {
		alert("cap obbligatorio");
		checkform.cap.focus();
		return false;
	}	
	if (!checkcap(checkform.cap.value)) {
		checkform.cap.focus();
		return false;
	}
	if (checkform.citta && checkform.citta.value == '') {
		alert("località obbligatoria");
		checkform.citta.focus();
		return false;
	}
	if (checkform.provincia && checkform.provincia.value == '') {
		alert("provincia obbligatoria");
		checkform.provincia.focus();
		return false;
	}
	if (checkform.telefono && checkform.telefono.value == '') {
		alert("telefono obbligatorio");
		checkform.telefono.focus();
		return false;
	}	
	if (checkform.email && trim(checkform.email.value) == '') {
		alert("indirizzo e-mail obbligatorio");
		checkform.email.focus();
		return false;
	}
	if (!checkemail(checkform.email.value)) {
		alert("indirizzo e-mail non valido");
		checkform.email.focus();
		checkform.email.select();
		return false;
	}
	if (checkform.tipo.value=="CA") {
		if (checkform.abbonato_a && checkform.abbonato_a.value == '') {
			alert("inserire a cosa si è abbonati");
			checkform.abbonato_a.focus();
			return false;
		}
		if (checkform.dt_scadenza_abb_gg && checkform.dt_scadenza_abb_gg.value == '') {
			alert("data di scadenza obbligatoria");
			checkform.dt_scadenza_abb_gg.focus();
			return false;
		}	
		if (checkform.dt_scadenza_abb_mm && checkform.dt_scadenza_abb_mm.value == '') {
			alert("data di scadenza obbligatoria");
			checkform.dt_scadenza_abb_mm.focus();
			return false;
		}	
		if (checkform.dt_scadenza_abb_aaaa && checkform.dt_scadenza_abb_aaaa.value == '') {
			alert("data di scadenza obbligatoria");
			checkform.dt_scadenza_abb_aaaa.focus();
			return false;
		}					
	}	
	if (checkform.tipo.value=="N") {
		if (checkform.partita_iva && checkform.partita_iva.value == '') {
			alert("inserire la partita iva");
			checkform.partita_iva.focus();
			return false;
		}
		if (checkform.pos_aziendale && checkform.pos_aziendale.value == '') {
			alert("inserire la posizione aziendale");
			checkform.pos_aziendale.focus();
			return false;
		}		
	} /*else {
		if (checkform.cod_fiscale && checkform.cod_fiscale.value == '') {
			alert("inserire il codice fiscale");
			checkform.cod_fiscale.focus();
			return false;
		}
	}*/
	if (checkform.data_nascita_gg && checkform.data_nascita_gg.value == '') {
		alert("data di nascita obbligatoria");
		checkform.data_nascita_gg.focus();
		return false;
	}	
	if (checkform.data_nascita_mm && checkform.data_nascita_mm.value == '') {
		alert("data di nascita obbligatoria");
		checkform.data_nascita_mm.focus();
		return false;
	}	
	if (checkform.data_nascita_aaaa && checkform.data_nascita_aaaa.value == '') {
		alert("data di nascita obbligatoria");
		checkform.data_nascita_aaaa.focus();
		return false;
	}				
	if (checkform.sesso && trim(checkform.sesso.value) == '') {
		alert("specificare il sesso");
		checkform.sesso.focus();
		return false;
	}	
	if (checkform.domanda && trim(checkform.domanda.value) == '') {
		alert("domanda obbligatoria");
		checkform.domanda.focus();
		return false;
	}		
	if (checkform.risposta && trim(checkform.risposta.value) == '') {
		alert("risposta obbligatoria");
		checkform.risposta.focus();
		return false;
	}	
	if (checkform.datipers[0] && !checkform.datipers[0].checked) {
		alert('Per completare la registrazione è necessario autorizzare il trattamento dei dati personali.');
		checkform.datipers[0].focus();
		return false;
	}
	//tutto ok
	return true;
}

function controlli_reinviapwd(checkform) {
	if (checkform.email && trim(checkform.email.value) == '') {
		alert("indirizzo e-mail obbligatorio");
		checkform.email.focus();
		return false;
	}
	if (checkform.email && !checkemail(checkform.email.value)) {
		alert("indirizzo e-mail non valido");
		checkform.email.focus();
		checkform.email.select();
		return false;
	}
	if (checkform.risposta && trim(checkform.risposta.value) == '') {
		alert("inserire la risposta alla domanda di controllo");
		checkform.risposta.focus();
		return false;
	}
	//tutto ok
	return true;
}

function controlli_attivazione(checkform) {
	if (checkform.password && trim(checkform.password.value) == '') {
		alert("inserire la password temporanea");
		checkform.password.focus();
		return false;
	}
	if (checkform.newpwd && trim(checkform.newpwd.value) == '') {
		alert("inserire la nuova password");
		checkform.newpwd.focus();
		return false;
	}
	if (checkform.confpwd && trim(checkform.confpwd.value) == '') {
		alert("confermare la nuova password");
		checkform.confpwd.focus();
		return false;
	}
	if (checkform.newpwd.value != checkform.confpwd.value) {
		alert("la nuova password non può essere confermata");
		checkform.newpwd.value = '';
		checkform.confpwd.value = '';
		checkform.newpwd.focus();
		return false;
	}
	//tutto ok
	return true;
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

function checkcap(cap) {
	if(cap.match(/\D/)) {
		alert('CAP errato: deve contenere solo numeri.');
		return false;
	}
	else if(cap.length != 5) {
		alert('CAP errato: deve essere di 5 cifre.');
		return false;
	}
	else {
		return true;
	}
}

function checkemail(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		//alert("Indirizzo non valido! (Verificare @ e .)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    //alert("The username doesn't seem to be valid.")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>4) {
	   // the address must end in a two letter or three letter word.
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 	
	return x;
}

var bIE4;
function versioneStampabile() {
	var str = "<html><head><title>Riepilogo ordine Alexandra</title></head>";
	str += "<link href='/stile.css' rel='stylesheet' type='text/css'>";
	str += "<body onload='self.print();'><table width=565><tr><td>";
	var cont = MM_findObj("corpocentrale").innerHTML;
	str += cont;
	str += "</td></tr></table></body></html>";
	win2 = open();
	win2.document.open();
	win2.document.write(str);
	if (bIE4) win2.location.reload();
	else win2.document.close();
}