

//======================================
//  FUNZIONE GENERALI TRIM
//======================================
function Ltrim(s){
	
	var s1
	var re;
  
  	re=/^\s+/g;
  	
	s1 = s.replace(re,"");
	
	return s1;
}

function Rtrim(s){
	
	var s1
	var re;
  
  	re=/\s+$/g;
  	
	s1 = s.replace(re,"");
	
	return s1;
}

function trim(s){
	
	var s1
	var re;
	
	re = /\s+$|^\s+/g;
  	s1 = s.replace(re,"");
	
	return s1;
}

//======================================
//  FUNZIONE CHE CONTROLLA LA LOGIN 
//======================================
function checkLogin(){
	
	var userid, psw, msg;
	
	msg = '';
	userid = trim(document.frm_login.userid.value);
	psw = trim(document.frm_login.psw.value);
	
	if (psw.length == 0) msg='Inserire la password';
	if (userid.length == 0) msg='Inserire il nome utente';
	
	if (msg != '' ){
		alert(msg);
		return false;
	}else
		return true;
}

//======================================
//  FUNZIONE CHE APRE UNA FINESTRA 
//======================================
function apripagina(pagina,dx,dy,rsz,scrl,pos,menub){
	
	var settings, TopPosition, LeftPosition;

	//	screen.width    DIMENSIONE DELLO SCHERMO IN LARGHEZZA	
	//	screen.height  	DIMENSIONE DELLO SCHERMO IN ALTEZZA
	//	dx				DIMENSIONE DELLA FINESTRA IN LARGHEZZA
	//	dy				DIMENSIONE DELLA FINESTRA IN ALTEZZA
    if (pos==""){
		pos = "center";
	}
	if (pos=="center"){
		// allineamento della finestra in mezzo
		TopPosition = ((screen.height) / 2) - (dy/2);
		// allineamento della finestra in mezzo
		LeftPosition = ((screen.width) / 2) - (dx/2);
	}
	if (pos=="right"){
		// allineamento della finestra in alto a destra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = (screen.width) - dx -10;
	}
	if (pos=="left"){
		// allineamento della finestra in alto a sinistra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = 0;
	}
	
	if (menub==""){
		menub = "yes";
	}
		
	// settaggio delle caratteristiche della finestra da aprire
	settings ='height='+dy+',width='+dx+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrl+',resizable='+rsz+',titlebar=no,status=yes,menubar='+menub;	
	
	// se esite gia' una finestra aperta allora la chiudo e la distruggo;
	//if (win != null){
	//	win.close();
	//	win = null;
	//}
	
	// apro una nuova finestra e le assegno il focus il nome della nuova finestra
	// (secondo campo nell'elenco parametri) non deve contenere nulla ;
	
	win = window.open(pagina,'ciccio',settings)
	win.window.focus();
}

//==========================================
//  FUNZIONE CHE RIDIMENSIONA UNA FINESTRA 
//==========================================

function ridimensiona(objName, pos) {
	//alert(x + " / " + y);
    var objImg = document.getElementById(objName);
    
    var x = objImg.width + 40;
    var y = objImg.height + 88;
    //alert(new_x + " / " + new_y);
	//self.resizeTo(x,y);
	//self.moveTo((screen.width - x)/2, (screen.height - y)/2)
	
	if (pos==""){
		pos = "center";
	}
	if (pos=="center"){
		// allineamento della finestra in mezzo
		self.moveTo((screen.width - x)/2, (screen.height - y)/2)
	}
	if (pos=="right"){
		// allineamento della finestra in alto a destra
		self.moveTo((screen.width) - x -10, 0)
	}
	if (pos=="left"){
		// allineamento della finestra in alto a sinistra
		self.moveTo(0 , 0)
	}
}

//=====================================================
//  FUNZIONE CHE CONTROLLA IL CAMPO EMAIL DI UN FORM
//=====================================================			
function isEmailOk(data) {
  if (data.indexOf(".") != -1) {
      if (data.indexOf("@") != -1)  { 
	      return true;
		  }
		}
	else return false;	  
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM
//=============================================================		
function isSpace(data) {
	var contr2=0;
   	for(var i=0; i < data.length; i++){
		if(data.substring(i, i+1) != " ")
	   		contr2=1; 
	}   
	if  (contr2==1)   
		return(false);  
return(true);
}



//=============================================================
//  FUNZIONE CHE CONTROLLA LA LOGIN AL WORKSPACE
//=============================================================		
function controlla_login(){

	var message = new Array();
	var msg;
	var errorLevel;
	
	message[0]  = "";
	message[1] = "IL CAMPO LOGIN E' OBBLIGATORIO !";
	message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";

	errorLevel = 0;
	if ((errorLevel == 0) && ((document.FormN.username.value.length == 0) || isSpace(document.FormN.username.value))) errorLevel = 1;	
	if ((errorLevel == 0) && ((document.FormN.password.value.length == 0) || isSpace(document.FormN.password.value))) errorLevel = 2;	
	
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
	}else{
		user = document.FormN.username.value
		pass = document.FormN.password.value
		lingua = document.FormN.lingua.value
		
		apripagina('/'+lingua+'/workspace/login.asp?username='+user+'&password='+pass,'795','480','yes','no','center','no');
	} 
}	
	
//=============================================================
//  FUNZIONE CHE CONTROLLA I CAMPI DI UN FORM ( FormN )
//=============================================================			
function checkRequiredFields() {
		
	var message2 = new Array();
	var errorLevel2;
	
	message2[0]  = "";
	message2[1] = "IL NOME E' OBBLIGATORIO !";
	message2[2] = "IL COGNOME E' OBBLIGATORIO !";
	message2[3] = "L'AZIENDA E' OBBLIGATORIA !";
	message2[4] = "IL COMPO E-MAIL E' OBBLIGATORIO !";
	message2[5] = "INDIRIZZO EMAIL non corretto";
				 
	errorLevel2 = 0;  
	
	if ((errorLevel2 == 0) && ((document.FormN.nome.value.length == 0) || isSpace(document.FormN.nome.value))) errorLevel2 = 1;	
	if ((errorLevel2 == 0) && ((document.FormN.cognome.value.length == 0) || isSpace(document.FormN.cognome.value))) errorLevel2 = 2;	
	if ((errorLevel2 == 0) && ((document.FormN.azienda.value.length == 0) || isSpace(document.FormN.azienda.value))) errorLevel2 = 3;	
	if ((errorLevel2 == 0) && ((document.FormN.email.value.length == 0) || isSpace(document.FormN.email.value))) errorLevel2 = 4;	
	if ((errorLevel2 == 0) && (!(isEmailOk(document.FormN.email.value)))) errorLevel2 = 5;
			
	if (errorLevel2 > 0 ) { 
		alert(message2[errorLevel2]); 
		return false;
	} 
	return true;
}



//=============================================================
//  FUNZIONE CHE CONTROLLA I CAMPI DI UN FORM ( FormN )
//=============================================================			
function checkRequiredFieldsSugg() {
		
	var message2 = new Array();
	var errorLevel2;
	
	message2[0]  = "";
	message2[1] = "IL NOME E' OBBLIGATORIO !";
	message2[2] = "IL COGNOME E' OBBLIGATORIO !";
	message2[3] = "IL COMPO E-MAIL E' OBBLIGATORIO !";
	message2[4] = "INDIRIZZO EMAIL non corretto";
				 
	errorLevel2 = 0;  
	
	if ((errorLevel2 == 0) && ((document.FormN.nome.value.length == 0) || isSpace(document.FormN.nome.value))) errorLevel2 = 1;	
	if ((errorLevel2 == 0) && ((document.FormN.cognome.value.length == 0) || isSpace(document.FormN.cognome.value))) errorLevel2 = 2;	
	if ((errorLevel2 == 0) && ((document.FormN.email.value.length == 0) || isSpace(document.FormN.email.value))) errorLevel2 = 3;	
	if ((errorLevel2 == 0) && (!(isEmailOk(document.FormN.email.value)))) errorLevel2 = 4;
			
	if (errorLevel2 > 0 ) { 
		alert(message2[errorLevel2]); 
		return false;
	} 
	return true;
}


//=============================================================
//  FUNZIONE CHE CONTROLLA I CAMPI DI UN FORM ( FormN )
//=============================================================			
function checkRequiredFieldsOpp() {
		
	var message2 = new Array();
	var errorLevel2;
	
	message2[0]  = "";
	message2[1] = "IL NOME E' OBBLIGATORIO !";
	message2[2] = "IL COMPO DATA DI NASCITA E' OBBLIGATORIO !";
	message2[3] = "IL COMPO CITTA' E' OBBLIGATORIO !";
	message2[4] = "IL COMPO NAZIONALITA' E' OBBLIGATORIO !";
	message2[5] = "IL COMPO RESIDENZA E' OBBLIGATORIO !";
	message2[6] = "IL COMPO CITTA' DI RESIDENZA E' OBBLIGATORIO !";
	message2[7] = "IL COMPO PROVINCIA DI RESIDENZA E' OBBLIGATORIO !";
	message2[8] = "IL COMPO STATO DI RESIDENZA E' OBBLIGATORIO !";
	message2[9] = "IL COMPO CAP E' OBBLIGATORIO !";
	message2[10] = "IL COMPO E-MAIL E' OBBLIGATORIO !";
	message2[11] = "INDIRIZZO EMAIL non corretto";
	message2[12] = "IL COMPO TITOLO DI STUDIO E' OBBLIGATORIO !";
				 
	errorLevel2 = 0;  
	
	if ((errorLevel2 == 0) && ((document.FormN.nome.value.length == 0) || isSpace(document.FormN.nome.value))) errorLevel2 = 1;	
	if ((errorLevel2 == 0) && ((document.FormN.data_nascita.value.length == 0) || isSpace(document.FormN.data_nascita.value))) errorLevel2 = 2;	
	if ((errorLevel2 == 0) && ((document.FormN.nato_a.value.length == 0) || isSpace(document.FormN.nato_a.value))) errorLevel2 = 3;		
	if ((errorLevel2 == 0) && ((document.FormN.stato_nascita.value.length == 0) || isSpace(document.FormN.stato_nascita.value))) errorLevel2 = 4;		
	if ((errorLevel2 == 0) && ((document.FormN.residenza.value.length == 0) || isSpace(document.FormN.residenza.value))) errorLevel2 = 5;	
	if ((errorLevel2 == 0) && ((document.FormN.citta_residenza.value.length == 0) || isSpace(document.FormN.citta_residenza.value))) errorLevel2 = 6;	
	if ((errorLevel2 == 0) && ((document.FormN.prov_residenza.value.length == 0) || isSpace(document.FormN.prov_residenza.value))) errorLevel2 = 7;	
	if ((errorLevel2 == 0) && ((document.FormN.stato_residenza.value.length == 0) || isSpace(document.FormN.stato_residenza.value))) errorLevel2 = 8;	
	if ((errorLevel2 == 0) && ((document.FormN.cap_residenza.value.length == 0) || isSpace(document.FormN.cap_residenza.value))) errorLevel2 = 9;	
	if ((errorLevel2 == 0) && ((document.FormN.email.value.length == 0) || isSpace(document.FormN.email.value))) errorLevel2 = 10;	
	if ((errorLevel2 == 0) && (!(isEmailOk(document.FormN.email.value)))) errorLevel2 = 11;
	if ((errorLevel2 == 0) && ((document.FormN.titolo_studio.value.length == 0) || isSpace(document.FormN.titolo_studio.value))) errorLevel2 = 12;			
			
	if (errorLevel2 > 0 ) { 
		alert(message2[errorLevel2]); 
		return false;
	} 
	return true;
}


//******************************************************************
//                             IsNumber(num)
//
// Controlla che la stringa passata come parametro sia composta
// da soli caratteri numerici. In questo caso restituisce TRUE.
// Questa funzione è utilizzata da IsNumberFields
// 
//******************************************************************
function IsNumber(num, consentiDecimali) {
   var i = 0;
   var letter;
   
   while (i < num.length) {
      letter = num.charAt(i);
 
      if ((letter == '0') || (letter == '1') || (letter == '2') ||
          (letter == '3') || (letter == '4') || (letter == '5') ||
          (letter == '6') || (letter == '7') || (letter == '8') ||
          (letter == '9') || (consentiDecimali && letter == ',' ))
      {
         i++;
      } else {
         return false;
      }
   	}
	return true;
}

function checkRequiredFieldsQTA(nomeform) {
				
	var message = new Array();
	var errorLevel;
	var defaultValue = new Array();
	var data;
	
	message[0]  = "";
	message[1] = "IL CAMPO QUANTITA' E' OBBLIGATORIO !";
	message[2] = "Il parametro inserito deve essere esclusivamente numerico, senza separatori delle migliaia ed intero !";
	
	errorLevel = 0;  
	defaultValue[0] = ""
   	defaultValue[1] = "1"
 			
	if (eval('document.'+nomeform+'.quantita.value.length') == 0) { 
	
		alert(message[1]); 
		return false;
	} else {
		
		data = eval('document.'+nomeform+'.quantita.value');
		consentiDecimali = "";
		
		if (!IsNumber(data, consentiDecimali)){
		
			alert(message[2]); 
			//azione = "document."+nomeform+".submit();";
			//eval(azione); 
			
			azione = "document."+nomeform+".quantita.value = defaultValue[0];";
			eval(azione); 
			
			azione2 = "document."+nomeform+".quantita.focus();";
			eval(azione2); 
			
			azione3 = "document."+nomeform+".quantita.select();";
			eval(azione3); 
			
			return false;
			
		} else {
			azione = "document."+nomeform+".submit();";
			eval(azione); 
			
			return true;
		}
	}
}

function checkRequiredNewsletter(lang, tipo) {
	
	var message = new Array();
	var errorLevel;
	
	message[0]  = "";

	switch (lang) {
		case "it":
			message[1] = "IL CAMPO E-MAIL E' OBBLIGATORIO !";
			message[2] = "INDIRIZZO E-MAIL NON CORRETTO !";
			message[3] = "ACCETTARE IL TRATTAMENTO DEI DATI !";
			break; 
   		case "uk":
    		message[1] = "FIELD E-MAIL REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			message[3] = "ACCETTARE IL TRATTAMENTO DEI DATI !";
			break; 
	} 
	
	errorLevel = 0;  
	
	if ((errorLevel == 0) && ((document.formcontatti5.email.value.length == 0) || isSpace(document.formcontatti5.email.value))) {
		errorLevel = 1;	
		document.formcontatti5.email.focus();
	}
	if ((errorLevel == 0) && (!(isEmailOk(document.formcontatti5.email.value)))) {
		errorLevel = 2;
		document.formcontatti5.email.focus();
	}
	
	if ( (errorLevel == 0) && (document.formcontatti5.chkPrivacy[0].checked != true) ) {
		errorLevel = 3;
	}
		
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			document.formcontatti5.submit();
		} else{
			return true;
		}
	}
}

