

//======================================
//  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) {
	
	var objImg = document.getElementById(objName);
    
    var x = objImg.width;
    var y = objImg.height;
    
	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 IL CODICE FISCALE
//=============================================================		
function codiceFISCALE(cfins) {
	
	   var cf = cfins.toUpperCase();
	   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	   if (!cfReg.test(cf))
	      return false;
	   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	   var s = 0;
	   for( i = 1; i <= 13; i += 2 )
	      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	   for( i = 0; i <= 14; i += 2 )
	      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	   if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
	      return false;
	   return true;
	}
	
//=============================================================
//  FUNZIONE CHE CONTROLLA LA PARTITA IVA
//=============================================================		
	
function partitaIVA(sz_Codice) {

   var n_Val,n_Som1=0,n_Som2=0,lcv;
   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      return false;
   
   for (lcv=0;lcv<9;lcv+=2)
   {
      n_Val=parseInt(sz_Codice.charAt(lcv));
      n_Som1+=n_Val;
      n_Val=parseInt(sz_Codice.charAt(lcv+1));
      n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
   }
   n_Som2 = 10 - (n_Som1 % 10);
   n_Val=parseInt(sz_Codice.charAt(10));
   if (n_Som2==n_Val)
      return true;
   return false;
}


//=============================================================
//  FUNZIONE CHE CONTROLLA LA LOGIN AL WORKSPACE
//=============================================================		
function controlla_login(lang){

	var message = new Array();
	var msg;
	var errorLevel;
	
	message[0]  = "";
	
	switch (lang) {
		case "IT":
			message[1] = "IL CAMPO USERNAME E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
			break; 
	   	case "UK":
			message[1] = "FIELD USERNAME REQUIRED !";
			message[2] = "FIELD PASSWORD REQUIRED !";
			break; 
		default :
	    	message[1] = "IL CAMPO USERNAME E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
		break; 
	} 	
		
	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]); 
		return false;
	}else{
		//user = document.FormN.username.value
		//pass = document.FormN.password.value
		return true;
		//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(lang, tipo) {
		
	var message = new Array();
	var errorLevel;
	
	message[0]  = "";
	
	switch (lang) {
		case "it":
	    	message[1] = "IL NOME E' OBBLIGATORIO !";
			message[2] = "IL COGNOME E' OBBLIGATORIO !";
			message[3] = "IL CAMPO E-MAIL E' OBBLIGATORIO !";
			message[4] = "INDIRIZZO EMAIL non corretto";
			break; 
	   	case "uk":
	    	message[1] = "FIELD NAME REQUIRED !";
			message[2] = "FIELD SURNAME REQUIRED !";
			message[3] = "FIELD E-MAIL REQUIRED !";
			message[4] = "FIELD E-MAIL IS WRONG";
			break; 
	   	default :
	    	message[1] = "IL NOME E' OBBLIGATORIO !";
			message[2] = "IL COGNOME E' OBBLIGATORIO !";
			message[3] = "IL CAMPO E-MAIL E' OBBLIGATORIO !";
			message[4] = "INDIRIZZO EMAIL non corretto";
			break; 
	} 
		
				 
	errorLevel = 0;  
	
	if ((errorLevel == 0) && ((document.FormN.nome.value.length == 0) || isSpace(document.FormN.nome.value))) {
		errorLevel = 1;	
		document.FormN.nome.focus();
	}
	if ((errorLevel == 0) && ((document.FormN.cognome.value.length == 0) || isSpace(document.FormN.cognome.value))) {
		errorLevel = 2;	
		document.FormN.cognome.focus();
	}
	if ((errorLevel == 0) && ((document.FormN.email.value.length == 0) || isSpace(document.FormN.email.value))) {
		errorLevel = 3;	
		document.FormN.email.focus();
	}
	if ((errorLevel == 0) && (!(isEmailOk(document.FormN.email.value)))) {
		errorLevel = 4;
		document.FormN.email.focus();
	}
	
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			document.FormN.submit();
		} else{
			return true;
		}
	}
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM 
//  DI RICERCA NEL CATALOGO
//=============================================================		
function search(lang){
	keywords = document.frm_ricerca.keywords.value;
	if ((keywords == "")|| (isSpace(keywords))){
		
		switch (lang) {
			case "IT":
		    	alert("Inserire almeno una chiave di ricerca");
				break; 
		   	case "UK":
		    	alert("Insert keywords");
				break;
		   	default :
		    	alert("Inserire almeno una chiave di ricerca");
				break;
		} 
	}else{
		document.frm_ricerca.submit();
	}
}
