<!--  funzioni javascript generali -->
/*
// Per eliminare un elemento da un array monodimensionale
Array.prototype.RemoveByValue = function(value) {
  for(var _x = 0; _x < this.length; _x++)
   if(this[_x].toUpperCase() == value.toUpperCase()) {
	this.splice(_x, 1);
	return;
   }
 } //RemoveByValue 
*/

function cfr(n) {
  return parseInt(n) < 10 ? "0" + n : n;
}

function trim(s) {
  while (s.substring(0, 1) == " ")  s = s.substring(1, s.length);
  while (s.substring(s.length - 1, s.length) == " ") s = s.substring(0, s.length - 1);
  return s;
}
function jumpMenu(targ, selObj, restore) { 
  eval(targ + ".location='" + selObj.options.value + "'");
  if (restore) selObj.selectedIndex = 0;
}
function resetForm(formX) {
	if (formX) {
		var coll = formX.all.tags("input");
		for (var cont = 0; cont < coll.length; cont ++) {
			iTxt = 0;
			if (coll[cont].type == "text") {
				coll[cont].value = "";
				if (!coll[cont].disabled && !coll[cont].readOnly && iTxt==0) coll[cont].focus();
				iTxt++;
			}
			if (coll[cont].type == "checkbox") coll[cont].checked=false;
		}
		coll = formX.all.tags("select");
		for (var cont = 0; cont < coll.length; cont ++) {
			coll[cont].value = "";
		}
	}
}
function setFormFocus(formX) {
	if (formX) {
		var coll = formX.all.tags("input");
		for (var cont = 0; cont < coll.length; cont ++) {
			iTxt = 0;
			if (coll[cont].type == "text") {
				if (iTxt==0 && !coll[cont].disabled && coll[cont].style.visibility!="hidden") coll[cont].focus();
				iTxt++;
			}
		}
	}
}

function captureEnter (bkShld) {
    if (event.keyCode == 13) {
        var current = document.activeElement;
        if (current) 
            switch (current.tagName) {
                case "TEXTAREA": 
                case "SELECT" : break;
                default : 
                    if (typeof(buttonToCapture) == "undefined") {
                        buttonToCapture = [];
                        var coll = document.getElementsByTagName("INPUT");
                        
                        for (var index = 0; index < coll.length; index ++)
                            if (coll[index].captureenter == "true") 
                                buttonToCapture.push(coll[index].id);
                    }
                    
                    if (buttonToCapture.length > 0)
                        for (var index = buttonToCapture.length - 1; index >= 0; index --) {
                            var button = document.getElementById(buttonToCapture[index]);
                            if (button) 
                                try 
                                { 
                                    button.focus();
                                    if (!isParentVisible())
                                        return;
                                    if (bkShld)
                                        ShieldForModalPopup.show("Loading...");
                                } 
                                catch (e) {};
                        }
            }
    }
};

// rende visibile o no un oggetto
function ShowVisible(idName, bMostra) {
  var el = document.getElementById(idName);
  if (el) {
    el.disabled = !bMostra;	
    el.style.visibility = bMostra? 'visible' : 'hidden';	
  }
}
// aggiunge o toglie un oggetto dalla pagina
function AddDisplay(idName, bMostra) {
  var el = document.getElementById(idName);
  if (el) {
    el.disabled = false;
    el.style.display = bMostra? '' : 'none';	
  }
}

function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i < args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

// Controlla se i valori inseriti sono numerici
function ValoreNumerico(input,minimo,massimo) {
	var OK = true
	if (input == "") {
		alert("Inserire un valore numerico.");
		return false;
	}
	if (!parseInt(input) && input != 0) {
		alert("Inserire un valore numerico.");
		OK = false;
	}
	else {
		for (var i=0; i < input.length; i++) {
			if (!parseInt(input.charAt(i)) && input.charAt(i) != 0) {
				alert("Inserire un valore numerico.");
				OK = false;
				break;
			}
		}
	}
	if (OK && (input < minimo || input > massimo)) {
		alert("Il numero deve essere compreso tra " + minimo + " e " + massimo + "!"); 
		OK = false;
	}
	
	return OK;
}

//conta le occorrenze di un carattere in una stringa
function countCharOcc(ch, str) {
	var count=0;
	if (!str) return count;
	for (var i=0; i<str.length; i++)
		if (str.charAt(i)==ch) count++;
	return count;
}

// Controlla se i valori inseriti sono numerici
function checkInput(type, key, text) {
	var ch=String.fromCharCode(key);
	if (ch==" ") return "";
	switch (type) {
		case "n":	// number
			if (ch=="." || ch=="," || ch=="-") {
				if ((ch=="." || ch==",") && countCharOcc(".",text)==0) return ".".charCodeAt(0);
				if (ch=="-" && text.length==0) return key;
			}
			else if (!isNaN(ch)) return key;
			break;
		case "i":	// integer
			if (ch=="-" && text.length==0) return key;
			else if (!isNaN(ch)) return key;
			break;
		case "p":	// positive integer >= 0
			if (ch=="0" && text.length==0) return "";
			else if (!isNaN(ch)) return key;
			break;
		case "d":	// dd/MM/yyyy date
			if (isNaN(ch) && ch=="/" && (text.length==2 || text.length==5)) return key;
			else if (text.length<2 || (text.length>2 && text.length<5) || text.length>5) return key;
			break;
		case "%":	// percentage >=0 && <=100
			if (parseFloat(text)<0 || parseFloat(text+ch)>100) return "";
			else if (!isNaN(ch)) {
				if (parseFloat(text+ch)>=100) return "";
				else return key;
			}
			else if (ch=="." || ch==",") {
				if (text.length>0 && parseFloat(text)!=100 && countCharOcc(".",text)==0) return ".".charCodeAt(0);
			}
			break;
	} 
	return "".charCodeAt(0);
}


function ApriFinestraModale(URL, width, height, scrollbars, resizable, root, args) {
	if (!root) root = "/";
	ShieldForModalPopup.show("<img src='" + root + "images/loading.gif'>"); 
	if (URL == "") return false;
	var option = "dialogWidth: " + width + "px; dialogHeight: " + height + "px; help: no; status: no; scroll: " + scrollbars + "; resizable: " + resizable + ";";
	//return window.showModalDialog(URL, args, option);
	//ShieldForModalPopup.hide();
	var retVal = window.showModalDialog(URL, args, option);
	if (retVal == 'logout') document.location.href = '../logout.asp' 
	else { 
		if ((""+retVal).indexOf(".") == -1) ShieldForModalPopup.hide();
		return retVal; 
	}
}

function ApriFinestraNonModale(URL, width, height, position, scrollbars, resizable, root, args) {
	if (!root) root = "/";
	ShieldForModalPopup.show("<img src='" + root + "images/loading.gif'>"); 
	if (URL == "") return false;
	var dialogLeft = 1;
	var dialogTop = 1;
	
	switch (position) {
		case 'lt':
		dialogLeft = 1; windowTop = 1;
		break;
	case 'rt':
		dialogLeft = (screen.availWidth) - (width + 10);
		dialogTop = 1;
		break;
	case 'lb':
		dialogLeft = 1; 
		dialogTop = (screen.availHeight) - (height + 30);
		break;
	case 'rb':
		dialogLeft = (screen.availWidth) - (width + 10);
		dialogTop = (screen.availHeight) - (height + 30);
		break;
	}
	var option = "dialogWidth: " + width + "px; dialogHeight: " + height + "px; edge: Raised; help: No; status: No; scroll: " + scrollbars + "; resizable: " + resizable + ";";
	if (position == 'c') 
		option += "center: Yes;"; 
	else 
		option += "dialogTop: " + dialogTop + "px; dialogLeft: " + dialogLeft + "px; center: No;"; 
	//return window.showModalDialog(URL, args, option);
	//ShieldForModalPopup.hide();
	var retVal = window.showModelessDialog(URL, args, option);
	//var retVal = window.showModalDialog(URL, args, option);
	if (retVal == 'logout') document.location.href = '../logout.asp' 
	else { 
		if ((""+retVal).indexOf(".") == -1) ShieldForModalPopup.hide();
		return retVal; 
	}
}

function ApriPaginaModale(URL, targ, width, height, scrollbars, resizable, root) {
	if (!root) root = "/";
	ShieldForModalPopup.show("<img src='" + root + "images/loading.gif'>"); 
	if (URL == "") return false;
	var option = "dialogWidth: " + width + "px; dialogHeight: " + height + "px; help: no; status: no; scrollbars: " + scrollbars + "; resizable: " + resizable + ";";
	//return window.open(URL, targ, option);
	//ShieldForModalPopup.hide();
	var retVal = window.open(URL, targ, option);
	if (retVal == 'logout') document.location.href = '../logout.asp' 
	else { 
		if ((""+retVal).indexOf(".") == -1) ShieldForModalPopup.hide();
		return retVal; 
	} 
}

var win = '';
function ApriPagina(URL, width, height, position) {
	var windowLeft = 1;
	var windowTop = 1;
	switch (position) {
	case 'lt':
		windowLeft = 1; windowTop = 1;
		break;
	case 'rt':
		windowLeft = (screen.availWidth) - (width + 20);
		windowTop = 1;
		break;
	case 'lb':
		windowLeft = 1; 
		windowTop = (screen.availHeight) - (height + 40);
		break;
	case 'rb':
		windowLeft = (screen.availWidth) - (width + 20);
		windowTop = (screen.availHeight) - (height + 40);
		break;
	default:
		windowLeft = 1; windowTop = 1;
	}
 
	if (win != 'undefined') {
		if (win && win.open && !win.closed)
			win.location.href = URL;
		else
			win = window.open(URL, "", "height=" + height + ",width=" + width + ",top=" + windowTop + ",left=" + windowLeft + ",scrollbars=yes,resizable=yes"); // + location
		if (win) win.focus();
		else {
			// alert("Non è possibile chiamare una pagina non modale da una finestra modale");
			ApriFinestraModale(URL, width, height, 'no', 'yes', '', '');
		}
	}
	return win;
}

function zoom(image, width, height, modal, position) {
	if (modal) ApriFinestraModale("../zoom.asp?image="+image, width, height, "no", "yes");
	else win = ApriPagina("../zoom.asp?image="+image, width, height, position);
}

function targetOpener(myLink, closeMe, closeOnly){
	if (!window.focus || !window.opener) return true;
	window.opener.focus();
	if (!closeOnly) window.opener.location.href = myLink.href;
	if (closeMe) window.close();
	return false;
}
 
function DataValida(giorno, mese, anno) {
	var ok = true;
	if (giorno == "" && mese == "" && anno == "") ok = true;
	else {
		if (giorno == "" || mese == "" || anno == "") ok = false;
		else {
			// giorni mensili
			if ((mese == 4 || mese == 6 || mese == 9 || mese == 11) && giorno > 30) ok = false;
			if (mese == 2 && giorno > 28) {
				if ((anno % 4 != 0) || (anno % 400  == 0)) ok = false;
				else 
					if (giorno > 29) ok = false; // anno bisestile
			}
		}
	}
	return ok;
}

function ArrayUguali(arr1, arr2) {
	if (arr1 instanceof Array && arr2 instanceof Array) {
		if (arr1.length == arr2.length) {
			for (var i=0; i<arr1.length; i++) {
				if (!ArrayUguali(arr1[i], arr2[i])) return false;
			}
		}
	}
	else if (!arr1 instanceof Array && !arr2 instanceof Array) return arr1 == arr2;
	return false;
}

// CLASSE per la visualizzazione di uno schermo scuro
var ShieldForModalPopup = {
	// -----------------------------
	// PRIVATE
	
	_shield: null,		// Scudo
	
	// MOSTRA/NASCONDE I DROPDOWN DELLA PAGINA
	_allDropDown: function (show) {
		if (document) {
			// Collection dei dropdown
			var coll = document.getElementsByTagName("select");
			for (var cont = 0; cont < coll.length; cont ++)
				if (show) coll[cont].style.visibility = "visible";
				else coll[cont].style.visibility = "hidden";
		}
	},
	
	// CREA GLI ELEMENTI
	_init: function (innerHTML) {
		if (!document) return ;
		if (!this._shield) {
			var scrollWidth = (document.body.scrollWidth > document.body.clientWidth)? (document.body.scrollWidth - document.body.clientWidth): 0;
			var scrollHeight = (document.body.scrollHeight > document.body.clientHeight)? (document.body.scrollHeight - document.body.clientHeight): 0;
			
			// Crea il div per lo sfondo
			this._shield = document.createElement("div");
			this._shield.style.position = "absolute";
			this._shield.style.backgroundColor = "#333333";
			this._shield.style.left = 0;
			this._shield.style.top = 0;
			this._shield.style.width = document.body.clientWidth + scrollWidth;
			this._shield.style.height = document.body.clientHeight + scrollHeight;
			this._shield.style.filter = "alpha(opacity=60)";
			this._shield.style.display = "";
			this._shield.zIndex = 4;
			this._shield.unselectable = "on";
			this._shield.innerHTML = '<table width="100%" height="100%" onClick="ShieldForModalPopup.hide();"><tr><td class="warning" align="center">'+innerHTML+'</td></tr></table>';	//<td align='center' valign='center'><img src='../images/loading.gif' border=0>";
			//this._shield.innerHTML = '<table width="100%" height="100%" onMouseOver="ShieldForModalPopup.hide();"><tr><td class="warning" align="center">'+innerHTML+'</td></tr></table>';	//<td align='center' valign='center'><img src='../images/loading.gif' border=0>";
			// Attacca alla pagina
			document.body.appendChild(this._shield);
		}
	},
	
	// -----------------------------
	// PUBBLICHE
	
	// Mostra il Popup
	show: function (innerHTML) {
		// Inizializa
		this._init(innerHTML);
		// Nasconde i dropdown
		this._allDropDown(false);
	},
	
	// Nasconde il Popup
	hide: function () {
		// Visualizza i dropdown
		this._allDropDown(true);
		// Nasconde i div
		this._shield.style.display = "none";
		this._shield.removeNode(true);
		this._shield = null;
	}
}
