// JavaScript Document
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

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_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 MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=4) { test=args[i+2]; val=MM_findObj(args[i]);
	if (test=="RRADIO"){//Modifica per Radio
		valoreCheck1 = MM_findObj(args[i]+'1');
		valoreCheck2 = MM_findObj(args[i]+'2');
		if ((valoreCheck1.checked==false)&&(valoreCheck2.checked==false)) { errors += '- '+ args[i+3] + ' es obligatorio.\n';}
	};
    if (val) { nm=args[i+3]; if ((val=val.value)!="") {
		if (test.indexOf('CHECKDATA')!=-1) {
			if (check_date(val)) errors += '- '+nm+'- The format of the date controls (ex. 31/01/2003).\n';}
		if (test.indexOf('NOME_FILE')!=-1) { //Modifica per Inserimento File
			if (checkNomeFile(val)) errors += '- '+nm+' - not contain any of the following characters: (\',:).\n';}
	  if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
	  	charNotValid=val.indexOf('	');
        if ((charNotValid>-1)||(p<1 || p==(val.length-1))) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if ((test!='R') && (test.indexOf('CHECKDATA')==-1)) { 
	  	num = parseFloat(val);
	  	if (isNaN(val) && (test!="RRADIO")&& (test!="RNOME_FILE")) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between  '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function check_date(valore_campo) {
	var error = false;
	var controllo_prima_parte = 31;
	var controllo_seconda_parte = 12;
	arrayOfStrings = valore_campo.split("/");
	if (((isNaN(arrayOfStrings[0]) == true) || (arrayOfStrings[0] > controllo_prima_parte)) ||((isNaN(arrayOfStrings[1]) == true) || (arrayOfStrings[1] > controllo_seconda_parte)) || ((isNaN(arrayOfStrings[2]) == true) || (arrayOfStrings[2].length != 4)))
	{	
		error = true;
	}
	return error;
}

//CONTROLLO NOME FILE
function check_nome_file(valore_campo) {
	var elenco_caratteri_validi = 'abcdefghijklmnopqrstuvxywz1234567890_ -.!+&()=?|';
	var percorso_file = valore_campo.split("\\");
	var num_elementi = percorso_file.length;
	var nome_file_completo = percorso_file[num_elementi-1].toLowerCase();
	var nome_file = nome_file_completo.split(".");
	var num_elementi_file = nome_file.length;
	var nome_file_no_ext = "";
	var errore = false;
	if (num_elementi_file > 2) {
		errore = true;
	} else {
		nome_file_no_ext = nome_file[0];
	}
	if (errore == false) {
		for (var i=0;i<nome_file_no_ext.length;i++) {
			if (elenco_caratteri_validi.indexOf(nome_file_no_ext.substr(i,1)) < 0) {
				errore = true;
				break;
			}
		}
	}
	return errore;
}

//CONTROLLO ESTENSIONE FILE
function check_estensione_file(valore_campo) {
	var elenco_estensioni_valide= '.txt,.rtf,.doc,.xls,.ppt,.pps,.pdf,.jpg,.gif,.bmp,.zip,.rar,.tif,.html,.htm,.swf,.xml';
	var percorso_file = valore_campo.split("\\");

	var num_elementi = percorso_file.length;
	var nome_file_completo = percorso_file[num_elementi-1].toLowerCase();
	var nome_file = nome_file_completo.split(".");
	var num_elementi_file = nome_file.length;
	var estensione_file = "";
	var errore = false;
	if (num_elementi_file > 2) {
		errore = true;
	} else {
		estensione_file = "." + nome_file[1];
	}
	if (errore == false) {
		if (elenco_estensioni_valide.indexOf(estensione_file) < 0) {
			errore = true;
		}
	}
	return errore;
}

function checkNomeFile (valore_campo){
	var error = false;
	caratteri = "'";
	for (var i=0;i<valore_campo.length;i++) {
		if (!caratteri.indexOf(valore_campo.substr(i,1)) < 0) {
			error = true;
			break;
		}
	}
	return error;
}


function setSelectSezione(parId,nameForm,nameArray,nameCampo,nomeCampoAbilitare,valueSelect){
	if ((parId == "")||(parId == "0")) {
		document.forms[nameForm].elements[nameCampo].options.length=0;
		document.forms[nameForm].elements[nameCampo].disabled="disabled";
	}else{
		myArCombo = eval(nameArray+parId);	
		//Set Option a Null
		document.forms[nameForm].elements[nameCampo].disabled="disabled";
		document.forms[nameForm].elements[nameCampo].options.length=0;
		for (i = 0; i < myArCombo.length; i++){
			document.forms[nameForm].elements[nameCampo].options[i]= new Option(myArCombo[i][1],myArCombo[i][0],false,false);
			if (valueSelect==myArCombo[i][0]) document.forms[nameForm].elements[nameCampo].options[i].selected=true;
		}
		document.forms[nameForm].elements[nameCampo].disabled="";
		if (nomeCampoAbilitare!=""){ 
			document.forms[nameForm].elements[nomeCampoAbilitare].options.length=0;
			document.forms[nameForm].elements[nomeCampoAbilitare].disabled="disabled";
		}
	}	
}

//Cambio Azione ed Esecuzione Form
function changeActionForm(nameForm,action){
	document.forms[nameForm]['action'].value = action;
	document.forms[nameForm].submit();
}

//Cambio Valore di un campo del form e eseguo form
function changeValueForm(nameForm,nameCampo,Value){
	document.forms[nameForm][nameCampo].value = Value;
	document.forms[nameForm].submit();
}

function openNewWindow(url,windowW,windowH,windowName,visualizza_scroll) {
	var windowX = Math.ceil( (window.screen.width  - windowW) / 2 );
	var windowY = Math.ceil( (window.screen.height - windowH) / 2 );
	if (visualizza_scroll == '') {
		visualizza_scroll = 'no';
	}
	if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion)>=4)
		{windowH = windowH + 30; windowW = windowW + 10;}
	if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)>=5)
		{windowH = windowH + 29; windowW = windowW + 7;}
	var splashWin = window.open(url, windowName, 'status=no, toolbar=no, scrollbars='+visualizza_scroll+', resizable=no, width=' + windowW + ', height= ' + windowH);
	splashWin.resizeTo (Math.ceil(windowW), Math.ceil(windowH))
	splashWin.moveTo (Math.ceil(windowX), Math.ceil(windowY))
	splashWin.focus();
	if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion)>=4)
		{windowH = windowH - 30; windowW = windowW - 10;}
	if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)>=5)
		{windowH = windowH - 29; windowW = windowW - 7;}
}

//Gestione Allegati
function setAllegatiForm(nameForm,idAnnesso,action){
	if (document.forms[nameForm]['TITOLO'].value!=""){
		document.forms[nameForm]['action'].value = action;
		document.forms[nameForm]['ID_ANNESSO_UPDATE'].value = idAnnesso;
		document.forms[nameForm].submit();
	}else{
		errors = 'Titulo es obligatorio.\n';
		alert('El error(s) siguiente ocurrió:\n'+errors);
	}
}

//Eliminazione Allegati
function deleteAllegatiForm(nameForm,idAnnesso,action,strAlert){
	if (confirm(strAlert)) {
		document.forms[nameForm]['action'].value = action;
		document.forms[nameForm]['ID_ANNESSO_UPDATE'].value = idAnnesso;
		document.forms[nameForm].submit();
	} else {
		return false
	}
}

//Funzione di Notifica by Mail
function setInvioMail(TypeTable,index,nomeForm){
	if (TypeTable=='STATUS'){
		if (index==2){ 
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = false;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = true;
		}else{
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = true;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = false;
		}
	}
	if (TypeTable=='group_id' ){
		
		if (index>1 && arguments[3]==1){ 
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = false;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = true;
		}else{
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = true;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = false;
		}
	}
	if (TypeTable=='PUBBLICATO'){
		if (document.forms[nomeForm].PUBBLICATO.checked == true){ 
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = false;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = true;
		}else{
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = true;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = false;
		}
	}
	if (TypeTable=='abilitato' ){
		
		if (document.forms[nomeForm].abilitato.checked == true){ 
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = false;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = true;
		}else{
			document.forms[nomeForm].NOTIFICA_MAIL.disabled = true;
			//document.forms[nomeForm].NOTIFICA_MAIL.checked = false;
		}
	}
}