function sendpasswdmail() {

	var retcode=0;
	var username;
	username= document.form1.username.value;
	if (!username) {
		alert("inserisci username");
		return;
	}

	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)		
		req = new ActiveXObject("Microsoft.XMLHTTP");
	req.open('GET', './sendpasswdmail.php?username='+username,false); 
	req.send(null);
	if(req.status == 200)
	{
		retcode = req.responseText;
	}
	if (retcode < "0") alert("errore nell'esecuzione dello script");
	if (retcode > "0") alert("errore n. "+retcode+" nell'esecuzione dello script");
	if (retcode == "0")
	{
		alert("mail spedita!");
		window.close();
	}
}

    function contqta(qta,id) {
//        alert("passo qta="+qta+" id="+id);
				
				var max_qta=1;
				if (window.XMLHttpRequest)
					req = new XMLHttpRequest();
				else if (window.ActiveXObject)		
					req = new ActiveXObject("Microsoft.XMLHTTP");
				req.open('GET', './contqta.php?id='+id+'&qta='+qta, false); 
				req.send(null);
				if(req.status == 200)
				{
					var max_qta = req.responseText;
				}
				if (Number(qta.value) > Number(max_qta))
				{ 
					alert("puoi selezionare massimo "+max_qta+" oggetto/i");
					qta.value=max_qta;
				}
    }

    function fuoco() {
        window.focus;
    }

    function OpenWindow(URL,name,att) {
       
        var me;
        me=window.open(URL,name,att);
        if (window.focus) {me.focus();}
    }

	function ricarica() {
		var casella;
		casella = document.form1.mymbox.value;
		window.open("./LoadMbox.php?casella="+casella, "bottom","");
	}

	function color(action) {
		for (i=1;i<6;i++) {
			eval("document.getElementById("+i+").style.setProperty(\"border-top\",\" 1px solid #999\",null)");
			eval("document.getElementById("+i+").style.setProperty(\"border-bottom\",\" 1px solid #999\",null)");
		}

		eval("document.getElementById("+action+").style.setProperty(\"border-top\",\" 2px solid red\",null)");
		eval("document.getElementById("+action+").style.setProperty(\"border-bottom\",\" 2px solid red\",null)");
	}


	function init(cmd) {
		if (!cmd) {
			top.main.location.href="centro.php?casella=<?=$casella?>";
		} else {
			apri(cmd);
		}
	}

	function init_import() {
			window.open("./top.php?casella=<?=$casella?>&command=Leggi", "top","");
	}

	function apri(cmd) {
		switch (cmd) {
			case 'none':
			break;

			case 'Import':
				window.open("./mailimport.php?scelta_mbox=<?=$casella?>","import","width=400, height=300, location=no, menubar=no, status=no, toolbar=yes, scrollbars=yes, resizable=yes");
			break;

			case 'Leggi':
				color(1);
				top.main.location.href="./mail_list.php?filtro=common/default.ini&azione=leggi&casella=<?=$casella?>&del=0&spam=0";
			break;

			case 'Spam':
				color(2);
				top.main.location.href="./mail_list.php?filtro=common/spam.ini&azione=spam&casella=<?=$casella?>&del=0&spam=1";
			break;

			case 'Archivio':
				color(3);
				top.main.location.href="./archivio.php?azione=archivio&casella=<?=$casella?>";
			break;

			case 'Cestino':
				color(4);
				top.main.location.href="./mail_list.php?filtro=common/ripri.ini&azione=cestino&casella=<?=$casella?>&del=1&spam=0";
			break;
		}
	}

	function debug() {
		var mess = document.fcanc.messaggio.value;
		document.location.href="./bottom.php?messaggio="+mess;
	}

	function init_top(cmd) {
		window.open("./top.php?casella=<?=$casella?>&command="+cmd,"top","");
		if (cmd!="none") {
			window.open("./centro.php?casella=<?=$casella?>&command="+cmd,"main","");
		}
	}

	function hide_tr(msgno,fuffa) {
		eval("document.getElementById(\"tr_"+msgno+"\").style.display=\"none\"");
		window.open("./top.php?casella=<?=$casella?>&command=<?=command?>","top","");
	}

	function letto_td(id) {
		eval("document.getElementById(id).style.color=\"red\"");
	}

	function check(mycheck,id) {
		hide_tr(id,id);
	}

	function checkall2(radix) {
		if (document.mail.chk_all_.checked == true) {
			document.mail.chk_all_.checked = false;
		} else {
			document.mail.chk_all_.checked = true;
		}
		checkall(radix);
	}

	function checkall(radix) {
		var arr_ck = Array();
		var i;
		var lenradix;
		var chk;
		var ifchk;
		lenradix = radix.length;
		eval("chk=document.getElementsByName(\""+radix+"_\")");
		ifchk = chk[0].checked;
		arr_ck = document.getElementsByTagName("input");
		for (i=0;i<arr_ck.length;i++) {
			if (arr_ck[i].name.substr(0,lenradix) == radix) {
				if (ifchk) {
					arr_ck[i].checked = true;
				} else {
					arr_ck[i].checked = false;
				}
			}
		}
	}

	function ricarica_listdb(scelta) {
        document.mail.modo.value="esegui";
		if (scelta == "scelta1") {
			document.mail.scelta.value = document.mail.scelta1.value;
		} else {
			document.mail.scelta.value = document.mail.scelta2.value;
		}
		document.mail.submit();
	}

	function ricarica_list(scelta) {
		var arr_ck = Array();
		var i;
		var mymsgno;
		var lenmsgno;
		mymsgno = "";
		arr_ck = document.getElementsByTagName("input");
		for (i=0;i<arr_ck.length;i++) {
			if (arr_ck[i].name.substr(0,7) == "chk_all") {
				if (arr_ck[i].checked) {
					lenmsgno = arr_ck[i].name.substring(8);
					if (lenmsgno.length > 0) {
						mymsgno = mymsgno+","+arr_ck[i].name.substring(8);
					}
					document.mail.arr_msgno.value=mymsgno;
					document.mail.modo.value="esegui";
				}
			}
		}
		if (scelta == "scelta1") {
			document.mail.scelta.value = document.mail.scelta1.value;
		} else {
			document.mail.scelta.value = document.mail.scelta2.value;
		}
		document.mail.submit();
	}

	function sorta(tiposort) {
		document.mail.sort.value=tiposort;
		var order;
		order = document.mail.order.value;
		if (!order) {
			document.mail.order.value = "ASC";
		} else {
			if (order == "DESC") {
				document.mail.order.value = "ASC";
			} else {
				document.mail.order.value = "DESC";
			}
		}
		document.mail.submit();
	}

	function svuota() {
		document.mail.action="./empty.php";
		document.mail.submit();
	}
	
	function init_list() {
	//	top.top.location.href="./top.php?casella=<?=$casella?>&command=<?=$command?>";
	}

	function init_load() {
		top.bottom.location.href="./LoadMbox.php?casella=<?=$casella?>&command=Cestino";
	}

   function handleFile(nr)   {
		setTimeout("this.location.reload()",8000);
        location.href="maildownload.php?casella=<?=$casella?>&msgno=<?=$id?>&parte="+nr;
   }

   function handleFileDB(nr)   {
		setTimeout("this.location.reload()",8000);
        location.href="maildbdownload.php?msgno=1&parte="+nr;
   }

	function cancellarec(fullpath,webpath,tabella,id) {
	
		if (confirm("sei sicuro di voler cancellare il record?")) {
			var rtc=delrec(fullpath,webpath,tabella,id);
			window.location.reload();
		}
	}
	
	function delrec(fullpath,webpath,tabella,id) {

		if (window.XMLHttpRequest)
			req = new XMLHttpRequest();
		else if (window.ActiveXObject)		
			req = new ActiveXObject("Microsoft.XMLHTTP");
		var server = fullpath+'?webpath='+webpath+'&tabella='+tabella+'&id='+id;
//		alert(server);
		req.open('GET', server, false); 
		req.send(null);
		if(req.status == 200)
		{
			return req.responseText;
		} 
	}
