function trimold(str) {
 //return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
 var newstr = str.replace(/^\s+|\s+$/g, '');
 return newstr;
}

function trim (str) {
	return str.substring(Math.max(str.search(/\S/), 0), str.search(/\S\s*$/) + 1);
}


function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isEmail(str) {

  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function checkLoginForm(){

	msg = checkLogin();
	if (msg=="NO_ERRORE"){
		return true;
	}else{ 
		alert(msg);
		return false;
	}
}

function checkLogin(){
        msg = new String();
        msg ="NO_ERRORE";               
		
		if ( (document.getElementById('loginform').pass.value.length<6) ) {
			msg="La password deve contenere almeno 6 caratteri.";
		}	
		if ( isEmpty(document.getElementById('loginform').pass.value) ) {
			msg="Occorre inserire la password.";
		}
		if ( isEmpty(document.getElementById('loginform').user.value) ) {
			msg="Occorre indicare il nome utente.";
		}	
	 return msg;
}   

function checkRegForm(){

	msg = checkReg();
	if (msg=="NO_ERRORE"){
		return true;
	}else{ 
		alert(msg);
		return false;
	}
}

function checkReg(){
        msg = new String();
        msg ="NO_ERRORE";               
        
    if (document.getElementById('trattamento').checked == false){
		   msg="Occorre esprimere il consenso al trattamento dei dati personali.";
		}
		if (document.getElementById('informativa').checked == false){
		   msg="Occorre confermare la lettura dell'informativa sulla privacy.";
		}
        if (isEmail(document.getElementById('regform').email.value) == false){
		   msg="Inserire un indirizzo e-mail valido.";
		}
		if ( (document.getElementById('regform').pass1.value!=document.getElementById('regform').pass2.value) ) {
			msg="I due valori inseriti per la password sono diversi.";
		}
		if ( (document.getElementById('regform').pass1.value.length>10) ) {
			msg="La lunghezza massima della password e' di 10 caratteri.";
		}
		if ( (document.getElementById('regform').pass1.value.length<6) ) {
			msg="La password deve contenere almeno 6 caratteri.";
		}
		if ( isEmpty(document.getElementById('regform').pass1.value) ) {
			msg="Occorre inserire la password.";
		}
		if ( isEmpty(document.getElementById('regform').user.value.length<4) ) {
			msg="Il nome utente deve contenere almeno 4 caratteri.";
		}
		if ( isEmpty(document.getElementById('regform').user.value) ) {
			msg="Occorre indicare il nome utente.";
		}
		       	    		
	 return msg;
}      

function checkModForm(){

	msg = checkMod();
	if (msg=="NO_ERRORE"){
		return true;
	}else{ 
		alert(msg);
		return false;
	}
}

function checkMod(){
        msg = new String();
        msg ="NO_ERRORE";               
        
        if (isEmail(document.getElementById('modform').email.value) == false){
		   msg="Inserire un indirizzo e-mail valido.";
		}
		if ((document.getElementById('modform').pass1.value!='')) {
			
		    if ( (document.getElementById('modform').pass1.value.length>10) ) {
	    		msg="La lunghezza massima della password e' di 10 caratteri.";
	    	}
	    	if ( (document.getElementById('modform').pass1.value.length<6) ) {
	    		msg="La password deve contenere almeno 6 caratteri.";
	    	}
	    	if (((document.getElementById('modform').pasnow.value.length<6))||((document.getElementById('regform').pasnow.value.length>10))) {
			   msg="La lughezza della password attuale non e' corretta.";
		    }				
	    	if ( (document.getElementById('modform').pass1.value!=document.getElementById('modform').pass2.value) ) {
			    msg="I due valori inseriti per la nuova password sono diversi.";
		    }
			if ((document.getElementById('modform').pasnow.value=='')) {
			    msg="Per poter cambiare la password occorre indicare la password attuale.";
		    }
		} else if ((document.getElementById('modform').pasnow.value!='')) {
			    msg="La password attuale deve essere indicata solo se si intende cambiarla.";
		    }
				    		
	 return msg;
}  

function checkPwdForm(){

	msg = checkPwd();
	if (msg=="NO_ERRORE"){
		return true;
	}else{ 
		alert(msg);
		return false;
	}
}

function checkPwd(){
        msg = new String();
        msg ="NO_ERRORE";               
		
        if (isEmail(document.getElementById('pwdform').email.value) == false){
		   msg="Inserire un indirizzo e-mail valido.";
		}
		if ( isEmpty(document.getElementById('pwdform').user.value) ) {
			msg="Occorre indicare il nome utente.";
		}	
	 return msg;
}   

function checkNotifyForm(hasnums) {
  if ((document.getElementById('notify').checked)&&(hasnums == 0)&&(document.getElementById('wichdraw2').checked)) { 
	  alert("Non hai numeri preferiti. Inseriscili nella sezione 'I miei numeri'.");	  
	  return false;
   }
}

function checkNotifyPlusForm(n2,n3) {
  if ((document.getElementById('notify').checked)&&(n2==0)&&(document.getElementById('wichdraw2').checked)) { 
	  alert("Non hai numeri preferiti. Inseriscili nella sezione 'I miei numeri'.");	  
	  return false;
   }
  if ((document.getElementById('notify').checked)&&(n3==0)&&(document.getElementById('wichdraw3').checked)) { 
	  alert("Non hai sistemi in archivio.");	  
	  return false;
   }   
  if ((document.getElementById('notify').checked)&&(n3==1)&&(document.getElementById('wichdraw3').checked)) { 
	  if (document.getElementById('id_sys').selectedIndex==0) { alert("Occorre selezionare un sistema tra quelli in archivio."); return false; }
   }   
}

function deleteConfirm(nome) {
    return confirm("Sei sicuro di voler eliminare il sistema ?");	  
}

function clickAttivazione() {
  if ((document.getElementById('notify').checked==false)) { 
	  //alert("Non hai numeri preferiti. Inseriscili nella sezione 'I miei numeri'.");	  
	  document.getElementById("wichdraw2").disabled=true;
	  document.getElementById("wichdraw1").disabled=true;
   } else if ((document.getElementById('notify').checked)) { 
	  //alert("Non hai numeri preferiti. Inseriscili nella sezione 'I miei numeri'.");	  
	  document.getElementById("wichdraw2").disabled=false;
	  document.getElementById("wichdraw1").disabled=false;
   }
}

function clickAttivazionePlus(n2,n3) {
  if ((document.getElementById('notify').checked==false)) { 
	  if (n3==1) {
        document.getElementById("wichdraw3").disabled=true;
	      document.getElementById("id_sys").disabled=true;
	      }
    if (n2==1) {
        document.getElementById("wichdraw2").disabled=true;
        }
	  document.getElementById("wichdraw1").disabled=true;
	 } else if ((document.getElementById('notify').checked)) { 
	  if (n3==1) {
        document.getElementById("wichdraw3").disabled=false;
        document.getElementById("id_sys").disabled=false;
        }
	  if (n2==1) {
        document.getElementById("wichdraw2").disabled=false;
        }
	  document.getElementById("wichdraw1").disabled=false;	  
   }
}

function openWindow(anchor, options) {

	var args = '';

	if (typeof(options) == 'undefined') { var options = new Object(); }
	if (typeof(options.name) == 'undefined') { options.name = 'win' + Math.round(Math.random()*100000); }

	if (typeof(options.height) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "height=" + options.height + ",";
	}

	if (typeof(options.width) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		args += "width=" + options.width + ",";
	}

	if (typeof(options.fullscreen) != 'undefined') {
		args += "width=" + screen.availWidth + ",";
		args += "height=" + screen.availHeight + ",";
	}

	if (typeof(options.center) == 'undefined') {
		options.x = 0;
		options.y = 0;
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}

	if (typeof(options.center) != 'undefined' && typeof(options.fullscreen) == 'undefined') {
		options.y=Math.floor((screen.availHeight-(options.height || screen.height))/2)-(screen.height-screen.availHeight);
		options.x=Math.floor((screen.availWidth-(options.width || screen.width))/2)-(screen.width-screen.availWidth);
		args += "screenx=" + options.x + ",";
		args += "screeny=" + options.y + ",";
		args += "left=" + options.x + ",";
		args += "top=" + options.y + ",";
	}

	if (typeof(options.scrollbars) != 'undefined') { args += "scrollbars=1,"; }
	if (typeof(options.menubar) != 'undefined') { args += "menubar=1,"; }
	if (typeof(options.locationbar) != 'undefined') { args += "location=1,"; }
	if (typeof(options.resizable) != 'undefined') { args += "resizable=1,"; }

	var win = window.open(anchor, options.name, args);
	return false;

}