var browser
if ( navigator.appName == "Netscape" ) {
	browser = "netscape"
} else
{
	browser = "ie"
}

function ConfermaCancellazione(szD, szC, szP) {
	var testo = "Eliminare il prodotto dal carello ?\n" + szD
	if (window.confirm (testo) == true) {
		window.location = szP;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ProdottoMPC(status,sz_link) {
		MM_openBrWindow(sz_link,'','width:1000px;scrollbars=yes;status=no');
}
/*****************************************
	Controllo della Partita I.V.A.
	Linguaggio: JavaScript
******************************************/





function showLayer(id) {
	layer = document.all[id];
   	layer.style.visibility = "visible"
}

function hideLayer(id) {
	layer = document.all[id];
   	layer.style.visibility = "hidden"
}


function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	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 "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}


/**************************************
	Controllo del Codice Fiscale
	Linguaggio: JavaScript
***************************************/

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del codice fiscale non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	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 "Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}


function ControllaLogin(fo) {
	var ute = fo.utente.value;
	var pwd = fo.password.value;
	
	if ( (ute == "") || (pwd == "") ) {
		alert("Controlla i dati");
		return false;
	} else {
		fo.action = "moduli/ctrl_login.asp";
		fo.method = "post";
		return true;
	}
}
function ControllaLogin2(fo) {
	var ute = fo.utente.value;
	var pwd = fo.password.value;
	
	if ( (ute == "") || (pwd == "") ) {
		alert("Controlla i dati");
		return false;
	} else {
		fo.action = "../moduli/ctrl_login.asp";
		fo.method = "post";
		fo.submit();
	}
}

function formatCurrency(num, divisa) {
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
	return (divisa + num); // + '.' + cents);
}

function formatCurrency2(num) {
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
	return num
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	//alert(theURL);  
  if (browser == "netscape") {	 
	  var finestra = window.open("",winName , features);
  }
  else {
	  var finestra = window.open("",winName, "resizable=yes," + features );
  }
  finestra.location = theURL;
  if(finestra.window.focus){finestra.window.focus();
  }
}


function verificaNumero(n,testo) {

	// verifica lettere
	for (c = 0; c < (n.length) ; c++)	
	{
		if ( ( n.charAt(c).toString() != "," ) && ( n.charAt(c).toString() != "."  )  ) {
			if ( parseInt(n.charAt(c)).toString() == 'NaN' ) {
				// ci sone lettere in posisizone diverse
				alert(testo);
				return false
			}
		}
  }

	var f
	f = parseInt(n)
	
	if ( isNaN(f) ) {
		// primo carattere non numerico
		alert(testo);
		return false			
	}
	
	return true
}

function stampa() {
	window.print()
	return ""
}

function CambiaVirInPunto(n) {
	// verifica lettere
	var x
	var y
	x = n.value
	if (x.length == 0) {
		return 0;
	}
	y = x.replace(/,/i, ".");
	return y			
}


function CambiaVirInPunto2(n) {
	// verifica lettere
	var x
	var y
	x = n.value
	if (x.length == 0) {
		return "";
	}
	y = x.replace(/,/i, ".");
	return y			
}

function VerificaPIVA(obj){
	x = obj.value
	if (x.length == 0){
		return true
	} else 		{
		if ( verificaNumeroFocus(obj) ) {			
			if (x.length != 11){
				alert("Verifica PIVA")
				return false
			} else 		{
				return true
			}	
		} else	{
			obj.focus();
			return false
		}	
	}
}


function verificaNumeroFocus(n) {
	// verifica lettere
	//alert(n.value)
	n.value = CambiaVirInPunto2(n)
//	alert(n.value);
	var x
	x = n.value
	if (x.length == 0) {
		n.value = "";
		return true
	}
	for (c = 0; c < (x.length) ; c++)	
	{
		if ( ( x.charAt(c).toString() != "," ) && ( x.charAt(c).toString() != "."  )  ) {
			if ( parseInt(x.charAt(c)).toString() == 'NaN' ) {
				// ci sone lettere in posisizone diverse
				alert(n.id + " deve essere un numero!!");
				n.focus();
				return false
			}
		}
  }

	var f
	f = parseInt(x)
	
	if ( isNaN(f) ) {
		// primo carattere non numerico
		alert(n.id + " deve essere un numero!!");
		return false			
	}
	
	return true
}


function ConfermaEVai(link,testo) {	
	if (window.confirm (testo) == true) {
		window.location = link
	}
}

function ConfermaEVaiResp(link,testo,testo2) {	
	if (window.confirm (testo) == true) {
		alert(testo2)
		window.location = link
	}
}

function SegnaCheck(fo) {	
	var i
	for (i=0;i<fo.elements.length;i++){		
		if  (fo.elements[i].type == 'checkbox') {			
			fo.elements[i].checked = !(fo.elements[i].checked)			
		}
	}
}


function verificaMail(em) {
var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if ( r.test( em ) ) {
	return true; }
else {
	return false;
}
}

function VerificaObbligatoriForm(fo) {
	var i
	var bo 
	bo = true
	for (i=0;i<fo.elements.length;i++){		
		obj = fo.elements[i]
		if  (obj.id != '') {	
			if ((obj.type == "text" ) && ( obj.value=='' )) { bo  = false  }
			if ((obj.type == "textarea" ) && ( obj.value=='' )) { bo  = false  }
			if ((obj.type == "password" ) && ( obj.value=='' )) { bo  = false  }	
			if ((obj.type == "checkbox" ) && ( obj.checked == false )) { bo  = false  }			
			if ((obj.type.toString().charAt(0)=="s") && ( obj.options[obj.selectedIndex].text == "")) { bo  = false  }
			if (!(bo)) {
				alert("Impossibile preseguire!\nIl campo " + obj.id + " é obbligatorio."  )
				return false
			}
		}
	}
	return true
}




function popup(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

