<!--

/**************
   MACROMEDIA
 **************/

	function MM_openBrWindow(theURL,winName,features) { //v2.0
	  window.open(theURL,winName,features);
	}
	
	function MM_findObj(n, d) { //v4.0
	  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 && document.getElementById) x=document.getElementById(n); return x;
	}

	function MM_openBrWindow(theURL,winName,features) { //v2.0
	  window.open(theURL,winName,features);
	}
	
	function MM_findObj(n, d) { //v4.0
	  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 && document.getElementById) x=document.getElementById(n); return x;
	}

	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}

	function MM_showHideLayers() { //v3.0	
	  var i,p,v,obj,args=MM_showHideLayers.arguments;
	  for (i=0; i<(args.length-2); i+=3) {
		if ((obj=MM_findObj(args[i]))!=null) {
			v=args[i+2];
			if (obj.style) { 
				obj=obj.style; 
				v=(v=='show')?'visible':(v='hide')?'hidden':v; 
			}
			obj.visibility=v;
		}
	  }
	}

/*************
     JANUS     
 *************/

	function FB_showhidelayers(layername, chelayer, imgname, imgpath){
		var nowlayer = layername+"_"+chelayer;
		var nowimg = imgname+"_"+chelayer;
		var obj;
		var obj2;
		var layerinprocess;

		for(var i=1; i<(nlayer + 1); i++){
			layerinprocess = layername+"_"+i;
			obj = MM_findObj(layerinprocess);
			obj2 = MM_findObj(imgname+"_"+i);

			if(layerinprocess != nowlayer){
				obj.style.display = "none";
				obj2.src = imgpath+imgname+"_close.gif";
			}
			else{
				obj.style.display = (obj.style.display == "block")?"none":"block";
				obj2.src = (obj2.src.indexOf(imgname+"_open.gif") >= 0)?(imgpath+imgname+"_close.gif"):(imgpath+imgname+"_open.gif");
			}
		}
	}
	

	/************
	 * FROM OSC *
	 ************/

	function FB_swapImage(cheImg,cheSrc){
		obj=MM_findObj(cheImg);
		obj.src = cheSrc;		
	}	

	function FB_swapOpenerImage(cheImg,cheSrc){
		if(window.opener){
			obj = window.opener.MM_findObj(cheImg);
			obj.src = cheSrc;
		}
	}

	function FB_swapLoginImage(cheImg, cheStato, chePath){
		obj=MM_findObj(cheImg);
		var cheSrc = obj.src;
		var flaglogged = (cheSrc.indexOf("logged") >= 0);
		
		obj.src = chePath + "img/" + ((flaglogged)?"logged":"login") + cheStato + ".gif";
	}

	function FB_swapOpenerLoginTitle(cheImg, cheTitle){
		if(window.opener){
			obj = window.opener.MM_findObj(cheImg);
			obj.title = cheTitle;
		}
	}

	function FB_opencredits(siteserver,wndfeatures){		
		var wndcredits = window.open(siteserver + "credits.htm","wndcredits",wndfeatures);
	}

	function FB_showistitutional(imagepath){
		if(!vis){
			FB_swapImage('chisiamo',imagepath + 'img/chisiamo-click.gif');
			MM_showHideLayers('m2','','show');
			vis=!vis;  
		}
		else if((bt == -1) || (bt == 8)){
			FB_swapImage('chisiamo',imagepath + 'img/chisiamo-over.gif');
			MM_showHideLayers('m2','','hide'); 
			vis=!vis; 
		}
	}
	
	function FB_trim(chestring){
		var chestring = chestring.split(" ");
		chestring = chestring.join();
		return chestring;
	}

	function FB_isnumeric(chestring){
		if(FB_trim(chestring) == ""){
			return true;
		}
		return (chestring.match(/\d+/g));
	}

	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}

	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}

	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && (((!(year % 100 == 0)) || (year % 400 == 0))))?29:28);
	}
	function DaysArray(n) {
		for (var i = 1; i <= n; i++){
			this[i] = 31;
			if (i==4 || i==6 || i==9 || i==11){
				this[i] = 30;
			}
			if (i==2){
				this[i] = 29;
			}
	   } 
	   return this;
	}

	function FB_isdata(chestring){

		if(FB_trim(chestring) == ""){
			return true;
		}
		if(chestring.length != 10){
			return false;
		}

		var dtCh= "/";
		var daysInMonth = DaysArray(12);
		var pos1=chestring.indexOf(dtCh);
		var pos2=chestring.indexOf(dtCh,pos1+1);
		var strDay=chestring.substring(0,pos1);
		var strMonth=chestring.substring(pos1+1,pos2);
		var strYear=chestring.substring(pos2+1);
		strYr=strYear;
		if (strDay.charAt(0)=="0" && strDay.length>1){
			strDay=strDay.substring(1);
		}
		if (strMonth.charAt(0)=="0" && strMonth.length>1){
			strMonth=strMonth.substring(1);
		}
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
		}
		month=parseInt(strMonth);
		day=parseInt(strDay);
		year=parseInt(strYr);
		if (pos1==-1 || pos2==-1){
			return false;
		}
		if (strMonth.length<1 || month<1 || month>12){
			return false;
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			return false;
		}
		if (strYear.length != 4 || year==0){
			return false;
		}
		if (chestring.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(chestring, dtCh))==false){
			return false;
		}

		return true;
	}

	function FB_checkreferrers(cheform, chetipo, msgincompatibile, msgobbligatorio, msgdelete){
		// Controlli:
		// - F2m = "per aziende" (03) => è obbligatorio rappresentare un'azienda (anche una nuova)
		// - Non si può comunicare rappresentando un'azienda che si è scelto di eliminare

		var inpinprocess = document.forms[cheform].elements["riferimento_attivita"];
		var chetitolo = "";
		var chereturn = true;
		var flagdelete = 0;

		if(inpinprocess){
			if(inpinprocess.length){
				for(var i=0; i<inpinprocess.length; i++){
					if(inpinprocess[i].checked){ chetitolo = inpinprocess[i].value; }
				}
			}
			else{
				if(inpinprocess.checked){ chetitolo = inpinprocess.value; }
			}
		}

		if((chetipo == "03") && ((chetitolo == "") || (chetitolo == "profilo"))){
			alert(msgobbligatorio);
			chereturn = false;
			return chereturn;
		}

		if((chetitolo != "") && (chetitolo != "profilo") && (chetitolo != "0")){
			//controllo le aziende da eliminare.
			inpinprocess = document.forms[cheform].elements["azienda_delete[]"];
			if(inpinprocess.length){
				for(var i=0; i<inpinprocess.length; i++){
					if(inpinprocess[i].checked){
						flagdelete += 1;
						if(inpinprocess[i].value == chetitolo){	chereturn = false; }
					}
				}
			}
			else{
				if(inpinprocess.checked){
					chereturn = false;
				}
			}

			if(!chereturn){
				alert(msgincompatibile);
			}
			else if(flagdelete > 0){
				chereturn = window.confirm(msgdelete);
			}
			return chereturn;
		}
		else if((chetitolo == "profilo") || (chetitolo == "0")){
			return true;
		}
		return false;
	}

	function FB_fieldvalue(inpinprocess,inptype){		
		//determino il "valore" del campo
		var inpvalue = "";		

		switch(inptype){
			case "text":
			case "textarea":
			case "hidden":
			case "file":
			case "password":
				if(inpinprocess.length){
					for(var j=0;j<inpinprocess.length; j++){
						inpvalue += ((inpvalue == "")?"":"§§§") + inpinprocess[j].value;
					}
				}
				else{
					inpvalue = inpinprocess.value;
				}
				break;
			case "radio":
				if(inpinprocess.length){
					for(var j=0;j<inpinprocess.length; j++){
						if(inpinprocess[j].checked){
							inpvalue = inpinprocess[j].value;
						}
					}
				}
				else{
					if(inpinprocess.checked){
						inpvalue = inpinprocess.value;
					}
				}
				break;
			case "checkbox":
				if(inpinprocess.length){
					for(var j=0;j<inpinprocess.length; j++){
						if(inpinprocess[j].checked){
							inpvalue += ((inpvalue == "")?"":"§§§") + inpinprocess[j].value;
						}
					}
				}
				else{
					if(inpinprocess.checked){
						inpvalue = inpinprocess.value;
					}
				}
				break;
			case "select-one":
			case "select-multiple":
				if(inpinprocess[0].name){
					for(var j=0;j<inpinprocess.length; j++){
						for(var k=0; k<inpinprocess[j].options.length; k++){
							if(inpinprocess[j].options[k].selected){
								inpvalue += ((inpvalue == "")?"":"§§§") + inpinprocess[j].options[k].value;								
							}
						}
					}
				}
				else{
					for(var k=0; k<inpinprocess.options.length; k++){
						if(inpinprocess.options[k].selected){
							inpvalue += ((inpvalue == "")?"":"§§§") + inpinprocess.options[k].value;								
						}
					}
				}
				break;
			default:
				break;						
		}

		return inpvalue;
	}

	function FB_checkfields(){
		if((typeof(window['formname']) != "undefined") && (typeof(window['armsgs']) != "undefined") && (typeof(window['arfields_rules']) != "undefined")){
			//presenti controlli sui campi del form
			
			var inp = document.forms[formname].elements;
			var flaginp, flaglength;
			var inpinprocess, inpname, inptype, inpvalue, inplength, inplabel;
			var checkinpinprocess, checkinpname, checkinptyp, checkinpvalue, checkinplabel;
			var msg_err = "";
			var err_obbligatorio = "";
			var err_numerico = "";
			var err_data = "";
			var err_email = "";
			var err_lunghezza = "";
			var err_valore = "";
			var err_retype = "";
			var temp_retype = "";

			var reg_exp = new RegExp("<[\/\!]*?[^<>]*?>","g");

			for(var i=0; i<arfields_rules.length;i++){
				
				flaginp = false;
				if(inp[arfields_rules[i][0]]){
					//il campo esiste; campo "singolo"
					flaginp = true;
					inpinprocess = inp[arfields_rules[i][0]];
				}
				else if(inp[arfields_rules[i][0]+"[]"]){	
					//il campo esiste; campo "multiplo"		
					flaginp = true;
					inpinprocess = inp[arfields_rules[i][0]+"[]"];
				}

				if(flaginp){			
					inpname = inpinprocess.name;
					inptype = inpinprocess.type;			
					if((!inpname) && (inpinprocess.length)){
						inpname = inpinprocess[0].name;
						inptype = inpinprocess[0].type;
					}	

					inpvalue = FB_fieldvalue(inpinprocess,inptype);		

					inplabel = (inp[arfields_rules[i][0]+".label"])?inp[arfields_rules[i][0]+".label"].value:inpname;
					inplabel = inplabel.replace(reg_exp,"");


					if((arfields_rules[i][1] == "required") && (FB_trim(inpvalue) == "")){
						err_obbligatorio += "\n- " + inplabel;
					}
					else if((arfields_rules[i][3] == "numeric") && (!FB_isnumeric(inpvalue))){
						err_numerico += "\n- " + inplabel;
					}
					else if((arfields_rules[i][3] == "data") && (!FB_isdata(inpvalue))){
						err_data += "\n- " + inplabel;
					}
					else if((arfields_rules[i][3] == "email") && (!emailCheck(inpvalue))){
						err_email += ((err_email == "")?"":"\n") + armsgs[4].replace(/§1/,inplabel);
					}
					else if(arfields_rules[i][4]){
						flaglength = true;
						inplength = inpvalue.length;

						if((inpvalue.indexOf("§§§") >= 0) || (FB_trim(inpvalue) == "")){
							//attualmente non controllo il numero di caratteri per campi "multipli"
							//campo vuoto = OK (avrebbe dovuto essere required per scongiurare l'evenienza)
							flaglength = true;
						}
						else{
							if(eval(inplength + arfields_rules[i][4][1] + arfields_rules[i][4][2])){
								flaglength = true;							
							}
							else{
								flaglength = false;							
								err_lunghezza += "\n- " + inplabel;// + " (attualmente: " + inplength + " caratteri)";
							}
						}

						if(flaglength && arfields_rules[i][5]){
							//attualmente non è contemplato un array di valori attesi ma solo un valore
							//(valore atteso = N, non valori attesi = N,M,L,...)
							
							if(arfields_rules[i][5][2].indexOf("$") >= 0){
								//attualmente non è contemplato un confronto fra campi multipli
								checkinpinprocess = inp[arfields_rules[i][5][2].substr(1)];
								checkinpname = checkinpinprocess.name;
								checkinptype = checkinpinprocess.type;	
								checkinpvalue = FB_fieldvalue(checkinpinprocess,checkinptype);
								checkinplabel = (inp[arfields_rules[i][5][2].substr(1)+".label"])?inp[arfields_rules[i][5][2].substr(1)+".label"].value:checkinpname;
								checkinplabel = checkinplabel.replace(/<[\/\!]*?[^<>]*?>/g,"");

								if(!eval("\""+inpvalue+"\""+arfields_rules[i][5][1]+"\""+checkinpvalue+"\"")){
									temp_retype = armsgs[7].replace(/§1/,inplabel);
									temp_retype = temp_retype.replace(/§2/,checkinplabel);
									err_retype += ((err_retype == "")?"":"\n") + temp_retype;
								}
							}
							else{
								//attualmente il confronto "valore campo <=> valore obbligatorio" è 1 a 1
								//(se un campo multiplo ha più valori e sarebbe dovuto essere uguale ad N, è errore)
								if(!eval("\""+inpvalue+"\""+arfields_rules[i][5][1]+"\""+arfields_rules[i][5][2]+"\"")){
									err_valore += "\n- " + inplabel;						
								}
							}
						}
					}
				}
			}				

			if(err_obbligatorio != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + armsgs[1] + err_obbligatorio;
			}
			if(err_numerico != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + armsgs[2] + err_numerico;
			}
			if(err_data != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + armsgs[3] + err_data;
			}
			if(err_email != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + err_email;
			}
			if(err_lunghezza != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + armsgs[5] + err_lunghezza;
			}
			if(err_valore != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + armsgs[6] + err_valore;
			}
			if(err_retype != ""){
				msg_err += ((msg_err == "")?"":"\n\n") + err_retype;
			}

			if(msg_err != ""){
				alert(armsgs[0] + "\n\n" + msg_err);
				return false;
			}
			else{
				return true;
			}
		}
		else{
			//nessun controllo sui campi del form
			return true;
		}
		return false;
	}


	function FB_validateform(cheform, campomail, campoprivacy){
		var err = "";
		var cheform = document.forms[cheform].elements;
		
		if(!emailCheck(cheform[campomail].value)){
			err += "\n\nPer inoltrare il modulo il campo email deve contenere un indirizzo email valido.\n";
		}
		if(!cheform[campoprivacy][0].checked){
			err += "\n\nPer inoltrare il modulo le condizioni sul trattamento dei dati personali devono essere accettate.";
		}
		
		return err;
	}

	function emailCheck (emailStr) {
		if(FB_trim(emailStr) == ""){
			return true;
		}
		var checkTLD=0;
		var knownDomsPat=/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|it|fr|co.uk|de)$/;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				return false;
			}
		}
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				return false;
			}
		}
		if (user.match(userPat)==null) {
			return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray>255) {
					return false;
				}
			}
			return true;
		}
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				return false;
			}
		}
		if (checkTLD && domArr[domArr.length-1].length!=2 &&
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return false;
		}
		if (len<2) {
			return false;
		}
		return true;
	}

	function FB_swaplayer(chelayer){
		obj=MM_findObj(chelayer);
		
		if(obj.style.display == "block"){
			obj.style.display = "none";
		}
		else{
			obj.style.display = "block";
		}
	}

	function FB_copypassword(formSrc, fieldSrc, formDst, fieldDst){
		strSrc = document.forms[formSrc].elements[fieldSrc].value;
		if(FB_trim(strSrc) != ""){
			document.forms[formDst].elements[fieldDst].value = strSrc;
		}
	}

	function FB_changestate(cheForm, cheStatoInput, cheStato, flagSubmitForm){
		document.forms[cheForm].elements[cheStatoInput].value = cheStato;

		if(Boolean(flagSubmitForm)){
			document.forms[cheForm].submit();
		}
	}

	function FB_submitform(formName){
		document.forms[formName].submit();
	}

	function FB_checkradio(cheForm, cheStatoInput, cheStato){
		var cheradio = document.forms[cheForm].elements[cheStatoInput];

		if(cheradio.length){
			for(var i=0; i<cheradio.length; i++){
				cheradio[i].checked = (cheradio[i].value == cheStato)?true:false;
			}
		}
		else{
			cheradio.checked = (cheradio.value == cheStato)?true:false;
		}	
	}

	function FB_confirm(chemsg){
		var checonfirm = window.confirm(chemsg);
		return checonfirm;
	}

	function FB_expandcollapse(cheformlayer, chemessagelayer, chebuttonlayer){
		var obj = MM_findObj(cheformlayer);
		var obj2 = MM_findObj(chemessagelayer);
		var obj3 = MM_findObj(chebuttonlayer)
		obj.style.height = "auto";
		obj2.style.display = "block";
		obj3.style.display = "none";
	}

	/*
	function FB_openLoginWindow(chePath, cheStato, cheTipoForm, cheTabAttivita, cheAttivita){
		var cheUrl = chePath+"login.php?stato="+cheStato+"&tipo="+cheTipoForm+"&tab_attivita="+cheTabAttivita+"&attivita="+cheAttivita;
		var wndLogin = window.open(cheUrl,'wndlogin','width=500,height=400,scrollbars=yes,resizable=no');
	}
	*/

	/*
	function FB_openLoginWindow(chePath, cheStato, cheAttivita){
		var cheUrl = chePath+"login.php?stato="+cheStato+"&attivita="+cheAttivita;
		var wndLogin = window.open(cheUrl,'wndlogin','width=520,height=420,scrollbars=yes,resizable=yes');
	}
	*/

	function FB_openLoginWindow(){
		var arArgs = FB_openLoginWindow.arguments;
		var cheUrl = arArgs[0]+"login.php?stato="+arArgs[1]+"&attivita="+arArgs[2]+((arArgs.length > 3)?"&href="+arArgs[3]:"");
		var wndLogin = window.open(cheUrl,'wndlogin','width=500,height=400,scrollbars=yes,resizable=yes');
	}

	var numero_campi_indirizzo_spedizione = 0;

	/**
	*	Funzione usata nel form del salvataggio Attività
	*/
	function fillForm(id_profili_aziende){
		if (document.forms["frmactivity"].elements["valori_profilo_azienda[" + id_profili_aziende + "]"]!=null) {
			var stringa_profilo_azienda = document.forms["frmactivity"].elements["valori_profilo_azienda[" + id_profili_aziende + "]"].value;
			var stringa_profilo_azienda_array = stringa_profilo_azienda.split("§§");

			numero_campi_indirizzo_spedizione = stringa_profilo_azienda_array.length;

			for(var i=0; i < numero_campi_indirizzo_spedizione; i++){
				document.forms["frmactivity"].elements["indirizzo_spedizione[" + i + "]"].value = stringa_profilo_azienda_array[i];
			} 
		}
	}

	/**
	*	Funzione usata nel form del salvataggio Attività
	*/
	function fillFormOnLoad(){
		if (document.forms["frmactivity"].elements["profili_aziende"]!=null) {
			if (document.forms["frmactivity"].elements["profili_aziende"].length == undefined){
				fillForm(document.forms["frmactivity"].elements["profili_aziende"].value);
			}
			else{
				for(var i=0; i<document.forms["frmactivity"].elements["profili_aziende"].length; i++){

					if (document.forms["frmactivity"].elements["profili_aziende"][i].checked){
						fillForm(document.forms["frmactivity"].elements["profili_aziende"][i].value);
						return;
					}
				}
			}
		}
		else fillForm(0);
	}

//-->