
//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM 
//  DI RICERCA NEL CATALOGO
//=============================================================		
function search(lang){
	keywords = document.frm_ricerca2.descrizione_prodotto.value;
	if (trim(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;
		} 
		
		return false;
	}else{
		return true;
	}
}
				

function conferma(msg){
	if (!confirm(''+msg+'')){
		return false;
	}else{
		return true;		
	}
}
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;
}

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){
	
	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;
	}
		
	// settaggio delle caratteristiche della finestra da aprire
	settings ='height='+dy+',width='+dx+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrl+',resizable='+rsz+',titlebar=no,menubar=no';
	
	// 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,'',settings)
	win.window.focus();
}



//==========================================
//  FUNZIONE CHE RIDIMENSIONA UNA FINESTRA 
//==========================================

function ridimensiona(x, y, pos) {
	
	self.resizeTo(x+10,y+28);
	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)
	}
}





 
function apriPopup(nome, titolo, lar, alt){
        window.open(nome, titolo, 'width=' + lar + ',height=' + alt + ',toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}



//==========================================================
//  FUNZIONE CHE CONTROLLA LA VALIDITA' DEL CODICE FISCALE
//==========================================================


function validaCodiceFiscale (obj) {
	var nTempNum;
	var nAppoNum;
	var i;
	var szTempCodFisc;
	var szLastChar;
	var szValued;
	var szInValued;
	 
	szCodFisc = obj.value;
	 
	if (szCodFisc.length < 16) {
	  return false;
	} else if (szCodFisc == "") {
	  return false;
	} else {
		szCodFisc =szCodFisc.toUpperCase();
		szLastChar = szCodFisc.substr(szCodFisc.length - 1,1);
		szTempCodFisc =szCodFisc.substr(0,szCodFisc.length - 1);
		nTempNum = 0;
		i = 0;
		
		while (true) {
			szValued="B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX";
			// I DISPARI
			szInValued=szTempCodFisc.substr(i,1);
			nAppoNum = szValued.search(szInValued);
			nTempNum = nTempNum + (nAppoNum &  0x7FFE) / 2;
			i = i + 1;
			if (i>15) {
				break;
			}
			szValued="A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ";
			// I PARI
			szInValued=szTempCodFisc.substr(i,1);
			nAppoNum = szValued.search(szInValued);
			nTempNum =  nTempNum + (nAppoNum &  0x7FFE) / 2;
			i = i + 1;
		}
		nTempNum = nTempNum % 26; //mod
		szValued="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		szValued= szValued.substr(nTempNum, 1);
		if (szValued==szLastChar) {
			return true;
		} else {
			return false;
		}
	}
}


//=============================================================
//  FUNZIONE CHE CONTROLLA LA PARTITA IVA
//=============================================================		
	
function validaPartitaIva(objPi) {
    pi = objPi.value;
	if( pi == '' )  return false;
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;
	return true;
}
