// JavaScript Document
var ajax = null;

function userName() {
	var strNome = document.form1.nome.value;
	var strCognome = document.form1.cognome.value;
	var strUserName = strNome.toLowerCase() + '.' + strCognome.toLowerCase();
	document.form1.username.value = strUserName;
}

function registerUser() {
	if (document.form1.cognome.value == '') {
		alert('Il campo cognome e\' obbligatorio!');
	} else if (document.form1.nome.value == '') {
		alert('Il campo nome e\' obbligatorio!');
	} else if (document.form1.email.value == '') {
		alert('Il campo e-mail e\' obbligatorio!');
	} else if (document.form1.username.value == '') {
		alert('Il campo nick e\' obbligatorio!');
	} else if (document.form1.password.value == '') {
		alert('Il campo password e\' obbligatorio!');
	} else if (!document.form1.privacy.checked) {
		alert('E\' necessario acconsentire al trattamento dei dati personali!');
	} else {
		var strUser = document.form1.username.value;
		
		loadAjax();
		
		if (ajax) {
			ajax.open('get', '/admin/ajax/username.php?user=' + strUser, true);
	
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.statusText == 'OK') {
						var strResponse = ajax.responseText;
						if (strResponse != '') {
							alert('Username gia\' in uso!');
						} else {
							document.form1.submit();
						}
					} else {
					}
				}
			} 
			
			ajax.send(null);
		} else {
			alert('Aggiornare il browser!');
		}
	}
}

function saveUser() {
	if (document.form1.cognome.value == '') {
		alert('Il campo cognome e\' obbligatorio!');
	} else if (document.form1.nome.value == '') {
		alert('Il campo nome e\' obbligatorio!');
	} else if (document.form1.email.value == '') {
		alert('Il campo e-mail e\' obbligatorio!');
	} else {
		document.form1.submit();
	}
}

function cambiaPassword() {
	var strOldPwd = document.form1.oldpwd.value;
	var strNewPwd = document.form1.newpwd.value;
	var strConfPwd = document.form1.confpwd.value;
	
	if (strOldPwd == '') {
		alert('Inserire la vecchia password!');
	} else if (strNewPwd == '') {
		alert('Inserire la nuova password!');
	} else if (strConfPwd == '') {
		alert('Confermare la nuova password!');
	} else {
		if (strNewPwd != strConfPwd) {
			alert('La nuova password non e\' stata confermata correttamente');
		} else {
			document.form1.submit();
		}
	}
}

function modificaPassword(id) {
	var strPwd = window.showModalDialog('popup/password.php',null,'dialogWidth:300px; dialogHeight:184px; center:yes');
	if (strPwd)
		location.href = location.pathname + '?action=pwd&pwd=' + strPwd + '&id=' + id;	
}

function eliminaUtente(id,utente,act) {
	strUrl = 'anagrafica.php?action=del&id=' + id;
	if (act != '') strUrl += '&page=' + act;
	if (window.confirm('Eliminare l\'utente ' + utente + ' (' + id + ')?'))
		location.href = strUrl;
}

function eliminaGrado(id,grado,descr) {
	if (window.confirm('Revocare il grado di ' + descr + '?'))
		location.href = 'scheda_utente.php?action=delgr&id=' + id + '&grado=' + grado + '&#grado';	
}

function eliminaIdoneita(id,ido,descr) {
	if (window.confirm('Revocare l\'idoneità per ' + descr + '?'))
		location.href = 'scheda_utente.php?action=delido&id=' + id + '&ido=' + ido + '&#idoneita';	
}

function eliminaCrociera(id,crociera) {
	strUrl = 'crociere.php?action=del&id=' + id;
	if (window.confirm('Eliminare la crociera ' + crociera + ' (' + id + ')?'))
		location.href = strUrl;
}

function eliminaParagrafo(id,num) {
	strUrl = 'scheda_crociera.php?id=' + id + '&action=delpar&num=' + num;
	if (window.confirm('Eliminare il paragrafo?'))
		location.href = strUrl;
}

function eliminaIscrizione(id,uid,user) {
	strUrl = 'scheda_crociera.php?id=' + id + '&action=delusr&uid=' + uid;	
	if (window.confirm('Eliminare l\'iscrizione di ' + user + ' da questa crociera?'))	
		location.href = strUrl;
}																															
																															
function updateForm(action) {
	var intPage = parseInt(pag);
	switch (action) {
		case -1:
			intPage = intPage - 1;
			break;
		case 1:
			intPage = intPage + 1;
			break;
		case 0:
			intPage = 1;
			break;
	}
	document.form1.action = 'pratica_dettagli.php?act=' + act + '&pag=' + intPage + '&id=' + document.form1.id.value;
	document.form1.submit();
}

function delItem(id) {
	if (window.confirm('Eliminare la pratica ' + id + '?'))
		location.href = 'pratiche.php?action=del&id=' + id;	
}

function printSheet() {
	parent.main.form1[3].focus();
	print();	
}

function pausecomp(millis) {
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while (curDate-date < millis);
} 

function disableButtons() {
	var intPage = parseInt(pag);
	
	var objPrev = null;
	var objNext = null;
	
	try {
		var objPrev = parent.footer.document.getElementById('prev');
		var objNext = parent.footer.document.getElementById('next');
	} catch(err) {
	}
	
	if (objPrev) {
		switch(intPage) {
			case 1:
				objPrev.style.display = 'none';
				objNext.style.display = 'block';
				break;
			case 7:
				objPrev.style.display = 'block';
				objNext.style.display = 'none';
				break;
			default:
				objPrev.style.display = 'block';
				objNext.style.display = 'block';			
				break;
		}
	}
}

// funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers più recenti e diffusi
function assegnaXMLHttpRequest() {
	// lista delle variabili locali
	var XHR = null; // variabile di ritorno, nulla di default
  
	// informazioni sul nome del browser
	browserUtente = navigator.userAgent.toUpperCase();

	// browser standard con supporto nativo 
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
  		XHR = new XMLHttpRequest();
	else if (window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) { // browser Internet Explorer: è necessario filtrare la versione 4 
		// la versione 6 di IE ha un nome differente
		// per il tipo di oggetto ActiveX
		if (browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		else // le versioni 5 e 5.5 invece sfruttano lo stesso nome
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return XHR;
}

function loadAjax() {
	ajax = assegnaXMLHttpRequest();
  	if(ajax) {
   		// applicativo AJAX
	} else {
		alert('Aggiornare il browser!');
	}
}

function loadData(idForn, codCat) {
	if (ajax) {
		//window.open("../ajax/carica_materiali.asp?IdFornitore=" + idForn + '&Cat=' + codCat);
		
		// inizializzo la richiesta in GET
		//window.open("../ajax/carica_materiali.asp?IdFornitore=" + idForn + '&Cat=' + codCat);
		ajax.open("get", "../ajax/carica_materiali.asp?IdFornitore=" + idForn + '&Cat=' + codCat, true);

		// rimozione dell'header "connection" come "keep alive"
    	//ajax.setRequestHeader("connection", "close");

		// impostazione controllo e stato della richiesta
		ajax.onreadystatechange = function() {
			// verifica dello stato
			if (ajax.readyState == 4) {
				// verifica della risposta da parte del server
				if (ajax.statusText == "OK") {
					// operazione avvenuta con successo
					var objCat = document.getElementById('ly_' + codCat);
					var strResponse = ajax.responseText;
					objCat.innerHTML = strResponse;
				} else {
					// errore di caricamento
					//elemento.innerHTML = "Impossibile effettuare l'operazione richiesta.<br />";
					//elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status];
				}
			}
		} 
		
		// effettuo la richiesta
		ajax.send(null);
	} else {
		alert('Aggiornare il browser!');
	}
}

