function showVideo(id) {

	document.write("<object width=\"292\" height=\"225\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+id+">&amp;hl=en&amp;fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+id+"&amp;hl=en&amp;fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"292\" height=\"225\"></embed></object>");

}

function Trim(StrToTrim){
    // CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
    if (typeof StrToTrim != "string") {
        return StrToTrim;
    }

    // CATTURA IL PRIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    var StrBlank = StrToTrim.substring(0, 1);

    // ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ") {
        StrToTrim = StrToTrim.substring(1, StrToTrim.length);
        StrBlank = StrToTrim.substring(0, 1);
    }

    // CATTURA L'ULTIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    StrBlank = StrToTrim.substring(StrToTrim.length - 1, StrToTrim.length);

    // ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ")  {
        StrToTrim = StrToTrim.substring(0, StrToTrim.length-1);
        StrBlank = StrToTrim.substring(StrToTrim.length-1, StrToTrim.length);
    }

    // ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI ALL'INIZIO ED ALLA FINE DI UNA STRINGA
    while (StrToTrim.indexOf("  ") != -1) {
        StrToTrim = StrToTrim.substring(0, StrToTrim.indexOf("  "));
        StrToTrim += StrToTrim.substring(StrToTrim.indexOf("  ") + 1, StrToTrim.length);
    }

    // RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO
    return StrToTrim;
}

var http = null;

function confermaRichiesta(pwd, id, key, sessionId, token){
	http = new XMLHttpRequest();
	http.open("POST", "ajax_conferma.php?r=" + Math.random(), true);
	http.onreadystatechange = responseConfermaRichiesta;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("pwd=" + pwd + "&sessionId=" + sessionId + "&token=" + token + "&key=" + key + "&id=" + id);
}

function responseConfermaRichiesta(){
	if(http.readyState == 4){
		//alert(http.responseText);
		var ris = http.responseText;
		if(ris.indexOf("OK") != -1){
			document.getElementById("confDivForm").style.display   = "none";
			document.getElementById("confDivFormOK").style.display = "";
		}else{
			if(ris == 'NO'){
				alert("Password Non Valida !");
				document.getElementById("pwd").value='';
				document.getElementById("pwd").focus();
				document.getElementById("inviaB").disabled = false;
				document.getElementById("trMemorizzazione").style.display = "none";
			}else{
				alert("Errore sul Server Code: " + ris + " !");
				document.getElementById("inviaB").disabled = false;
				document.getElementById("trMemorizzazione").style.display = "none";
			}
		}
	}
}

function registrazione(nome, cognome, email, cell, profes, titolo, sessionId, token){
	http = new XMLHttpRequest();
	http.open("POST", "ajax_registra.php?r=" + Math.random(), true);
	http.onreadystatechange = responseRegistrazione;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("nome=" + nome + "&cognome=" + cognome + "&email=" + email + "&cell=" + cell + "&sessionId=" + sessionId + "&token=" + token + 
			  "&profes=" + profes + "&titolo=" + titolo);
}

function responseRegistrazione(){
	if(http.readyState == 4){
		//alert(http.responseText);
		var ris = http.responseText;
		if(ris.indexOf("OK") != -1){
			document.getElementById("regDivForm").style.display = "none";
			document.getElementById("regDivFormOK").style.display = "";
		}else{
			var msg = '';
			var cod = '';
			switch(ris){
				case 'GR' : cod = 'GR'; msg = "Email gia' registrata !"; break;
				case 'GC' : cod = 'GC'; msg = "Cellulare gia' registrato !"; break;
				default   : msg = "Errore sul Server Code: " + ris + " !"; 
			}
			document.getElementById("inviaB").disabled = false;
			document.getElementById("trMemorizzazione").style.display = "none";
			if(cod == 'GR'){
				document.getElementById("email").select();
			}
			if(cod == 'GC'){
				document.getElementById("cell").select();
			}
			alert(msg);
		}
	}
}

function Salva_Utente(){
	var error   = false;
	var name    = Trim(document.getElementById("nome").value);
	var surname = Trim(document.getElementById("cognome").value);
	var email   = Trim(document.getElementById("email").value);
	var cell    = Trim(document.getElementById("cell").value);
	var titolo  = Trim(document.getElementById("tit").value);
	var profes  = Trim(document.getElementById("prof").value);
	var token   = Trim(document.getElementById("token").value);
	var sessId  = Trim(document.getElementById("session_id").value);
	
	var regExp = /^([a-zA-Z\xE0\xE8\xE9\xF9\xF2\xEC\x27]\s?)+$/;
	
	if(surname == ''){
		alert("Cognome Obbligatorio !");
		document.getElementById("cognome").focus();
		return false;
	}
	if(!regExp.test(surname)){
		alert("Cognome Non Valido !");
		document.getElementById("cognome").select();
		document.getElementById("cognome").focus();
		return false;
	}
	if(name == ''){
		alert("Nome Obbligatorio !");
		document.getElementById("nome").focus();
		return false;
	}
	if(!regExp.test(name)){
		alert("Nome Non Valido !");
		document.getElementById("nome").select();
		document.getElementById("nome").focus();
		return false;
	}
	
	if(profes == ''){
		alert("Professione Obbligatoria !");
		document.getElementById("prof").focus();
		return false;
	}
	if(!regExp.test(profes)){
		alert("Professione Non Valida !");
		document.getElementById("prof").select();
		document.getElementById("prof").focus();
		return false;
	}
	
	if(titolo == ''){
		alert("Titolo di Studio Obbligatorio !");
		document.getElementById("tit").focus();
		return false;
	}
	
	if(email == ''){
		alert("Email Obbligatoria !");
		document.getElementById("email").focus();
		return false;
	}
	if(controlloMail(email) == 'N'){
		alert("Email Non Valida !");
		document.getElementById("email").select();
		document.getElementById("email").focus();
		return false;
	}
	if(cell == ''){
		alert("Cellulare Obbligatorio !");
		document.getElementById("cell").focus();
		return false;
	}
	if(cell.indexOf("+") != -1){
		alert("Cellulare Non Valido !");
		document.getElementById("cell").focus();
		return false;
	}
	if(isNaN(cell) || (parseInt(cell) < 0) || (cell.length < 10)){
		alert("Cellulare Non Valido !");
		document.getElementById("cell").select();
		document.getElementById("cell").focus();
		return false;
	}
	
	if(error == false){
		document.getElementById("inviaB").disabled = true;
		document.getElementById("trMemorizzazione").style.display = "";
		registrazione(name, surname, email, cell, profes, titolo, sessId, token);
	}
	return false;
}

function Conferma_Utente(){
	var error  = false;
	var pwd    = Trim(document.getElementById("pwd").value);
	var token  = Trim(document.getElementById("token").value);
	var key    = Trim(document.getElementById("key").value);
	var id     = Trim(document.getElementById("id").value);
	var sessId = Trim(document.getElementById("session_id").value);
	
	if(pwd == ''){
		alert("Password Obbligatoria !");
		document.getElementById("pwd").focus();
		return false;
	}
	
	if(isNaN(pwd) || (parseInt(pwd) < 0) || (pwd.length != 5)){
		alert("Password Non Valida !");
		document.getElementById("pwd").select();
		document.getElementById("pwd").focus();
		return false;
	}
	
	if(error == false){
		document.getElementById("inviaB").disabled = true;
		document.getElementById("trMemorizzazione").style.display = "";
		confermaRichiesta(pwd, id, key, sessId, token);
	}
	return false;
}

function controlloMail(mail){	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
	if (!espressione.test(mail)){
		return "N";	
	}else{
		return "S";
	}
}

function nascondiPages(){
	var div = document.getElementById("list_pages");
	if(div != null){
		var list = div.getElementsByTagName("li");
		if(list && (list.length > 0)){
			//alert("Entro LI");
			for(var i=0; i < list.length; i++){
				var li = list.item(i);
				var lista = li.getElementsByTagName("a");
				if(lista && (lista.length > 0)){
					//alert("Entro A"); break;
					var a = lista.item(0);
					if(a){
						//alert("A");
						if((a.getAttribute("href") != null) && ((a.getAttribute("href").indexOf("page_id=852") != -1) || (a.getAttribute("href").indexOf("page_id=558") != -1))){
							//alert("Trovato!");
							li.style.display = "none";
							//break;
						}
					}
				}
			}
		}
	}else{
		window.setTimeout("nascondiPages();", 250);
	}
}

var contentHTML = "";

function rightSideBar(){
	var div = document.getElementById("login_form");
	if(div != null){
		window.setTimeout("settaRightSideBar();", 250);
	}else{
		window.setTimeout("rightSideBar();", 250);
	}
}

function settaRightSideBar(){
	var div = document.getElementById("login_form");
	if(div != null){
		//div.parentNode.id = "respond";
		
		div.innerHTML = '<h2>Area Riservata</h2>' + 
		                '<table width="100%"><form action="#" method="post" onsubmit="return loggati();">' + 
		                '<tr><td colspan="3" style="font-size:1px; height:1px;"></td></tr>' + 
						'<tr><td><label for="email_login">Email:</label></td>' + 
						'<td><label for="pwd">Password:</label></td>' + 
						'<td></td></tr>' + 
						'<tr><td><input type="text" name="email_login" id="email_login" value="" size="18" maxlength="100" /></td>' + 
						'<td><input type="password" name="pwd" id="pwd" value="" size="15" maxlength="10" /></td>' + 
						'<td><button name="login_button" type="submit" id="login_button">Vai</button></td>' + 
						'</tr>' + 
						'<tr><td colspan="3" style="font-size:1px; height:1px;"></td></tr>' + 
						'</form></table>';
		
		//div.innerHTML = "CIAO";
		
		/*
		'<h2><span>Area Riservata</span></h2><form method="post" action="http://www.iscoadr.it/?_login=840e3dcb93">' + 
			'<p><label for="user_login">Nome utente:</label> <input name="log" value="" class="text" id="user_login" type="text" /></p>' + 
			'<p><label for="user_pass">Parola chiave:</label> <input name="pwd" class="text" id="user_pass" type="password" /></p>' + 
			'<p class="rememberme"><input name="rememberme" class="checkbox" id="rememberme" value="forever" type="checkbox" /> <label for="rememberme">Ricordami</label></p>' + 
			'<p class="submit">' + 
			'<input type="submit" name="wp-submit" id="wp-submit" value="Login &raquo;" />' + 
			'<input type="hidden" name="redirect_to" class="redirect_to" value="http://www.iscoadr.it/" />' + 
			'<input type="hidden" name="sidebarlogin_posted" value="1" />' + 
			'<input type="hidden" name="testcookie" value="1" />' + 
			'</p>' + 
		'</form>' + 
		'<ul class="sidebarlogin_otherlinks"><li><a href="http://www.iscoadr.it/wp-login.php?action=lostpassword" rel="nofollow">Hai perso la parola chiave ?</a></li></ul>';
		*/
	}
}

function loggati(){
	var pwd   = Trim(document.getElementById("pwd").value);
	var email = Trim(document.getElementById("email_login").value);
	
	if(email == ''){
		alert("Email Non Valida !");
		document.getElementById("email_login").focus();
		return false;
	}
	if(controlloMail(email) == 'N'){
		alert("Email Non Valida !");
		document.getElementById("email_login").select();
		document.getElementById("email_login").focus();
		return false;
	}
	if(pwd == ''){
		alert("Passowrd Non Valida !");
		document.getElementById("pwd").focus();
		return false;
	}
	accedi(email, pwd);
	return false;
}

function accedi(email, pwd){
	http = new XMLHttpRequest();
	http.open("POST", "ajax_login.php?r=" + Math.random(), true);
	http.onreadystatechange = responseLogin;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.send("email=" + email + "&password=" + pwd);
}

function responseLogin(){
	if(http.readyState == 4){
		//alert(http.responseText);
		var ris = http.responseText;
		if(ris == '0'){
			document.location.reload(false);
		}else if(ris == 'E1'){
			alert("Email e/o Password Errata !");
		}else if(ris == '-1'){
			alert("Email Non Trovata !");
		}else{
			alert("Errore sul Server Code: " + ris + " !");
		}
	}
}

function verificaInvio(){
	var browserName = navigator.appName.charAt(0);
	var browserVer  = parseInt(navigator.appVersion);
	if (browserName=="M" && browserVer >= 3) abilitato=1;
	if (browserName=="N" && browserVer >= 3) abilitato=2;
	if (browserName=="N" && browserVer >= 4) abilitato=3;
	if (browserName=="M" && browserVer >= 4) abilitato=4;
				
	if (abilitato==3) {
		var nKey    = e.which; var ieKey=0;
		var realkey = String.fromCharCode(e.which);
	}
	
	if (abilitato==4) {
		var ieKey   = event.keyCode; var nKey=0;
		var realkey = String.fromCharCode(event.keyCode);
	}
	
	if(ieKey == 13){
		loggati();
		return false;
	}
	
	return true;
}
