

//======================================
//  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 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 ;
	// se il campo nomepagina non è vuoto apro sempre nella stessa finestra che 
	// ha sempre lo stesso nome 
	
	//win = window.open(pagina,'',settings)
	win = window.open(pagina,'ccc',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_login2(nomeform, lang, tipo, action){
	
	var message = new Array();
	var msg;
	var errorLevel;
	
	message[0]  = "";
	
	switch (lang) {
		case "it":
			message[1] = "IL CAMPO NOME UTENTE E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
			break; 
	   	case "uk":
			message[1] = "FIELD USER NAME REQUIRED !";
			message[2] = "FIELD PASSWORD REQUIRED !";
			break; 
		default :
	    	message[1] = "IL CAMPO USER NAME E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
		break; 
	} 	
		
	errorLevel = 0;
	
	stringa1 = "document."+nomeform+".username.value.length";
	stringa2 = "document."+nomeform+".username.value";
	stringa3 = "document."+nomeform+".username.focus();";
	
	stringa4 = "document."+nomeform+".password.value.length";
	stringa5 = "document."+nomeform+".password.value";
	stringa6 = "document."+nomeform+".password.focus();";
	
	lingua = "document."+nomeform+".lingua.value";
	
	
	if ((errorLevel == 0) && ((eval(stringa1) == 0) || isSpace(eval(stringa2)))) {
		errorLevel = 1;	
		eval(stringa3);
	}
	if ((errorLevel == 0) && ((eval(stringa4) == 0) || isSpace(eval(stringa5)))) {
		errorLevel = 2;	
		eval(stringa6);
	}
		
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			azione = "document."+nomeform+".submit();";
		} else{
			return true;
		}
	}	
}	
	
//=====================================================================
//  FUNZIONE CHE CONTROLLA LA LOGIN AL WORKSPACE O AL B2B
//  nomeform => nome del form
//  lang => lingua: valori possibili "it" o "uk"
//  tipo => tipo di invio: valori possibili "link" o "submit"
//  action => identita del form: valori possibili "b2b" o "workspace"
//=====================================================================		
function controlla_login(nomeform, lang, tipo, action){
	
	var message = new Array();
	var msg;
	var errorLevel;
	
	message[0]  = "";
	
	switch (lang) {
		case "it":
			message[1] = "IL CAMPO USER NAME E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
			break; 
	   	case "uk":
			message[1] = "FIELD USER NAME REQUIRED !";
			message[2] = "FIELD PASSWORD REQUIRED !";
			break; 
		default :
	    	message[1] = "IL CAMPO USER NAME E' OBBLIGATORIO !";
			message[2] = "IL CAMPO PASSWORD E' OBBLIGATORIO !";
		break; 
	} 	
		
	errorLevel = 0;
	
	stringa1 = "document."+nomeform+".username.value.length";
	stringa2 = "document."+nomeform+".username.value";
	stringa3 = "document."+nomeform+".username.focus();";
	
	stringa4 = "document."+nomeform+".password.value.length";
	stringa5 = "document."+nomeform+".password.value";
	stringa6 = "document."+nomeform+".password.focus();";
	
	lingua = "document."+nomeform+".lingua.value";
	
	
	if ((errorLevel == 0) && ((eval(stringa1) == 0) || isSpace(eval(stringa2)))) {
		errorLevel = 1;	
		eval(stringa3);
	}
	if ((errorLevel == 0) && ((eval(stringa4) == 0) || isSpace(eval(stringa5)))) {
		errorLevel = 2;	
		eval(stringa6);
	}
		
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			if (action == "b2b") {
				azione = "document."+nomeform+".submit();";
				eval(azione); 
			} else {
				apripagina('/'+eval(lingua)+'/workspace/login.asp?username='+eval(stringa2)+'&password='+eval(stringa5),'795','480','yes','no','center','no');
			}
		} else{
			if (action == "b2b") {
				return true;
			} else {
				apripagina('/'+eval(lingua)+'/workspace/login.asp?username='+eval(stringa2)+'&password='+eval(stringa5),'795','480','yes','no','center','no');
				return false;
			}
		}
	}	
}	
		
	
//=============================================================
//  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] = "I CAMPI CON (*) SONO OBBLIGATORI !";
			message[2] = "INDIRIZZO E-MAIL NON CORRETTO !";
			break; 
	   	case "uk":
	    	message[1] = "FIELD (*) REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			break; 
		case "fr":
	    	message[1] = "FIELD (*) REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			break; 
		case "es":
	    	message[1] = "FIELD (*) REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			break; 	
		case "01":
	    	message[1] = "FIELD (*) REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			break; 	
		case "02":
	    	message[1] = "FIELD (*) REQUIRED !";
			message[2] = "FIELD E-MAIL IS WRONG";
			break; 				
	} 
		
				 
	errorLevel = 0;  
		
	if ((errorLevel == 0) && ((document.FormN1.nome.value.length == 0) || isSpace(document.FormN1.nome.value))) {
		errorLevel = 1;	
		document.FormN1.nome.focus();
	}
	if ((errorLevel == 0) && ((document.FormN1.azienda.value.length == 0) || isSpace(document.FormN1.azienda.value))) {
		errorLevel = 1;	
		document.FormN1.azienda.focus();
	}
	if ((errorLevel == 0) && ((document.FormN1.indirizzo.value.length == 0) || isSpace(document.FormN1.indirizzo.value))) {
		errorLevel = 1;	
		document.FormN1.indirizzo.focus();
	}
	if ((errorLevel == 0) && ((document.FormN1.email.value.length == 0) || isSpace(document.FormN1.email.value))) {
		errorLevel = 1;	
		document.FormN1.email.focus();
	}
	if ((errorLevel == 0) && (!(isEmailOk(document.FormN1.email.value)))) {
		errorLevel = 2;
		document.FormN1.email.focus();
	}
	if ((errorLevel == 0) && ((document.FormN1.telefono.value.length == 0) || isSpace(document.FormN1.telefono.value))) {
		errorLevel = 1;	
		document.FormN1.telefono.focus();
	}
			
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			document.FormN1.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;
	min_len_keywords = document.frm_ricerca.min_len_keywords.value;
	
	if (min_len_keywords == ""){
		min_len_keywords = 2;
	}
	
	if ((keywords == "") || (isSpace(keywords)) || (keywords.length < min_len_keywords)){
		
		switch (lang) {
			case "IT":
		    	alert("Inserire una chiave di ricerca con almeno "+min_len_keywords+" caratteri");
				break; 
		   	case "UK":
		    	alert("Insert keywords");
				break;
		   	default :
		    	alert("Inserire una chiave di ricerca con almeno "+min_len_keywords+" caratteri");
				break;
		} 
	}else{
		document.frm_ricerca.submit();
	}
}



//=============================================================
//  FUNZIONE CHE CONTROLLA LA QUANTITA MAX ORDINABILE
//=============================================================			
function controlla_qta(nomeform) {
		
	var message = new Array();
	var errorLevel;
	
	stringa = "document." + nomeform + ".qta_max.value";
	stringa2 = "document." + nomeform + ".quantita.value";
	stringa3 = "document." + nomeform + ".quantita.selectedIndex="+(eval(stringa)-1);
	
	message[0]  = "";
	message[1] = "La quantita massima ordinabile è di "+eval(stringa)+" unità!";

	errorLevel = 0;  

	if (eval(stringa2) > eval(stringa)) {
		errorLevel = 1;	
		eval(stringa3);
	}

	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		return false;
	} else{
		return true;
	}
}	

//=============================================================
//  FUNZIONE CHE SVUOTA IL CAMPO DI UN FORM DALL'IMPOSTAZIONE
//  DI DEFAULT INSERISCI ....
//=============================================================		
function compilazione1(t){

	switch (t.name){

		case "username":
			if (t.value == "Inserisci E-mail") t.value="";
			//if (document.form_catalogo.username.value == "") document.form_catalogo.password.value="";
			break;
		case "password":
			if (t.value=="Inserisci Password") t.value="";
			//if (document.form_catalogo.username.value=="") document.form_catalogo.username.value="Inserisci E-mail";
			break;
	}
}


//=============================================================
//						WORKSPACE
//  FUNZIONE CHE SVUOTA IL CAMPO DI UN FORM DALL'IMPOSTAZIONE
//  DI DEFAULT INSERISCI ....
//=============================================================		
function delete_folder(id,nome_cartella){
			
	var str;
	
	str = "folder_del.asp?id_cartella=" + id;
	
	if (lang=='it'){
		if (confirm("Eliminare la cartella \"" + nome_cartella + "\" ed i documenti che contiene?"))
			apripagina(str,'300','150','no','no','center','no');
	}
	if (lang=='uk'){
		if (confirm("To Cancel the folder \"" + nome_cartella + "\" and its documents?"))
			apripagina(str,'300','150','no','no','center','no');
	}
	if (lang=='fr'){
		if (confirm("Décommander la Dossiers \"" + nome_cartella + "\" et ses documents?"))
			apripagina(str,'300','150','no','no','center','no');
	}
	if (lang=='de'){
		if (confirm("Annullieren Sie das Ordner \"" + nome_cartella + "\" und seine Dokumente?"))
			apripagina(str,'300','150','no','no','center','no');
	}
	if (lang=='es'){
		if (confirm("Cancele la carpeta \"" + nome_cartella + "\" y sus documentos?"))
			apripagina(str,'300','150','no','no','center','no');
	}
	if (lang=='pt'){
		if (confirm("Cancele o dobrador \"" + nome_cartella + "\" e seus originais?"))
			apripagina(str,'300','150','no','no','center','no');
	}
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM
//=============================================================		
function checkData(giorno, mese, anno) {
		
	// verifico se l'anno è bisestile
	if ((anno % 4 == 0 && anno % 100 != 0) || anno % 400 == 0) {
		var giorni_mensili = new Array('',31,29,31,30,31,30,31,31,30,31,30,31);
	} else {
		var giorni_mensili = new Array('',31,28,31,30,31,30,31,31,30,31,30,31);
	}
	
	if (giorno > giorni_mensili[parseInt(mese)]) {
		return false;
	} else {
		return true;
	}
}


//=============================================================
//  FUNZIONE CHE CONTROLLA LA DATA
//=============================================================		
function checkValiditaDate(nome_form, giorno, mese, anno) {
		
	var message = new Array();
	var errorLevel;
	
	message[0]  = "";
	message[1] = "LA DATA INSERITA NON E' CORRETTA !";
	errorLevel = 0;  
	
	giorno = eval("document." + nome_form + "." + giorno + ".value");
	mese = eval("document." + nome_form + "." + mese + ".value");
	anno = eval("document." + nome_form + "." + anno + ".value");
	
	data = anno + mese + giorno; 
	
	if ((errorLevel == 0) && (!(checkData(giorno, mese, anno)))) {
		errorLevel = 1;
	}
	
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		return false;
	} else{
		return true;
	}
}



//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM
//=============================================================		
function checkIntervalloDate(nome_form, giorno_da, mese_da, anno_da, giorno_a, mese_a, anno_a) {
		
	var message = new Array();
	var errorLevel;
	
	message[0]  = "";
	message[1] = "LA DATA DA NON E' CORRETTA !";
	message[2] = "LA DATA A NON E' CORRETTA !";
	message[3] = "L'INTERVALLO SELEZIONATO NON E' CORRETTO !";
	errorLevel = 0;  
	
	giorno_da = eval("document." + nome_form + "." + giorno_da + ".value");
	mese_da = eval("document." + nome_form + "." + mese_da + ".value");
	anno_da = eval("document." + nome_form + "." + anno_da + ".value");
	
	data_da = anno_da + mese_da + giorno_da; 
	
	giorno_a = eval("document." + nome_form + "." + giorno_a + ".value");
	mese_a = eval("document." + nome_form + "." + mese_a + ".value");
	anno_a = eval("document." + nome_form + "." + anno_a + ".value");
	
	data_a = anno_a + mese_a + giorno_a; 
		
	if ((errorLevel == 0) && (!(checkData(giorno_da, mese_da, anno_da)))) {
		errorLevel = 1;
	}
	
	if ((errorLevel == 0) && (!(checkData(giorno_a, mese_a, anno_a)))) {
		errorLevel = 2;
	}
	
	differenza = parseInt(data_a) - parseInt(data_da);
		
	if ((errorLevel == 0) && (parseInt(differenza) < 0 )) {
		errorLevel = 3;	
	}
			
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		return false;
	} else{
		return true;
	}
}

//=============================================
//  FILTRA IL NOME DEL FILE UPLODATO SUL SERVER 
//=============================================
// si possono uplodare file che contengono solo lettere, numeri e underscore
function check_allegato(campo_input){
	path_array = campo_input.split("\\");
	var filtro_allegato=/^[a-zA-Z0-9\.\-\_]+$/;
	if (!filtro_allegato.test(path_array[path_array.length-1])) {
		return false;
	}
	return true;
}



