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_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_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 CheckFormLogin() {
	if (document.form_login.username.value == "" || document.form_login.password.value == "") {
		alert("ATTENZIONE\n\nRiempire entrambi i campi.");
		return false;
	} else {
		makeRequest('test.php');
	}
}

var http_request = false;

function makeRequest(url) {
	http_request = false;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }   catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('can\'t open http_request, sorry I\'ll give up');
        return false;
    }
	http_request.open('POST', url, true);
	http_request.onreadystatechange = alertContents;
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('username=' + document.form_login.username.value+'&password='+document.form_login.password.value);
	document.form_login.username.value = "";
	document.form_login.password.value = "";
}

function alertContents() {
	if (http_request.readyState == 1) {
		document.getElementById('loading').style.visibility = "visible";
	}
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById('loading').style.visibility = "hidden";
			var xmldoc    = http_request.responseXML;
			var root_node = xmldoc.getElementsByTagName("root");
			var error  = root_node.item(0).getAttribute("error");
			var to   = root_node.item(0).getAttribute("to");
			var messages = xmldoc.getElementsByTagName("message");
			var testo = messages.item(0).firstChild.data;
			document.getElementById('id_login').innerHTML = "<font size=1>"+testo+"</font>";
			timeout = window.setTimeout('RedirectWin("'+to+'")', 2000);
		} else {
			alert('There was a problem with the request.\n'+http_request.status);
		}
	}
}

function RedirectWin(page) {
	self.location.href=page;
}



function Chk_Form_Info() {
var i, n, err = 0;
	n = document.mod_info.elements;
	for (i=0;i<n.length;i++) {
		var valore = new String();
		valore = n[i].value;
		if (valore == "") {
				err = 1;
		}
	}
	if (err == 1) {
		alert("ATTENZIONE\n\nRiempire tutti i campi.");
		return false;
	} else if (document.mod_info.inf_priv.checked == false) {
		alert("ATTENZIONE\n\nE\' necessario accettare l\'informativa sulla privacy.");
		return false;
	} else {
		document.mod_info.submit();
	}
}
function Chk_Form_Inv() {
	var err_prezzo = 0;
	var err_periodo = 0;
	var RegEx_prezzo = /^[0-9]{1,3}(,|\.)[0-9]{1,2}$/;
	var RegEx_data = /^[0-9]{1,2}\.[0-9]{1,2}\s-\s[0-9]{1,2}\.[0-9]{1,2}$/;
	var n = document.form_inv.elements;
	for (var i=0;i<n.length;i++) {
		var nome = new String(n[i].name);
		if (nome.substr(0,1) == "P") {
			if ((n[i].value != "" && n[i].value != "-") && !RegEx_data.test(n[i].value)) {
				err_periodo = 1;
				n[i].style.background = '#FFFF00';
			} else n[i].style.background = '#FFFFFF';
		}
		if (nome.substr(1,1) == "B") {
			if ((n[i].value != "" && n[i].value != "-") && !RegEx_prezzo.test(n[i].value)) {
				err_prezzo = 1;
				n[i].style.background = '#FFFF00';
			} else n[i].style.background = '#FFFFFF';
		}
	}
	if (err_prezzo != 1 && err_periodo != 1) {
		document.form_inv.action = "./action.php?a=prezzi_inv";
		document.form_inv.submit();
	} else {
		alert("ATTENZIONE\n\nI valori evidenziati non sono nel formato corretto.");
		return false;
	}
}

function Chk_Form_Est() {
	var err_prezzo = 0;
	var err_periodo = 0;
	var RegEx_prezzo = /^[0-9]{1,3}(,|\.)[0-9]{1,2}$/;
	var RegEx_data = /^[0-9]{1,2}\.[0-9]{1,2}\s-\s[0-9]{1,2}\.[0-9]{1,2}$/;
	var n = document.form_est.elements;
	for (var i=0;i<n.length;i++) {
		var nome = new String(n[i].name);
		if (nome.substr(0,1) == "P") {
			if ((n[i].value != "" && n[i].value != "-") && !RegEx_data.test(n[i].value)) {
				err_periodo = 1;
				n[i].style.background = '#FFFF00';
			} else n[i].style.background = '#FFFFFF';
		}
		if (nome.substr(1,1) == "B") {
			if ((n[i].value != "" && n[i].value != "-") && !RegEx_prezzo.test(n[i].value)) {
				err_prezzo = 1;
				n[i].style.background = '#FFFF00';
			} else n[i].style.background = '#FFFFFF';
		}
	}
	if (err_prezzo != 1 && err_periodo != 1) {
		document.form_est.action = "./action.php?a=prezzi_est";
		document.form_est.submit();
	} else {
		alert("ATTENZIONE\n\nI valori evidenziati non sono nel formato corretto.");
		return false;
	}
}
function Change_BG(IDfield) {
	var d = document.getElementById(IDfield).style.background;
    if (d.substr(0,16) == 'rgb(255, 255, 0)') {
		document.getElementById(IDfield).style.background = '#FFFFFF';
        return true;
	} else return false;
}