// JavaScript Document

var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)

//alert(browser + " ---> " + version);

function roll(sDivIndex,events) {
	
		buttons = new Array("home","comefunziona","offerta","aderisci","preadesione","contatti");
		for ( singleDivIndex in buttons ) {
			document.getElementById(buttons[singleDivIndex]).src = "image/menu-"+buttons[singleDivIndex]+".gif";
		}
	
	if (events == 'over') {
		//su rollOver
		//alert(sDivIndex);
		document.getElementById(sDivIndex).src = "image/menu-"+sDivIndex+"-up.gif";
		} else {
		//su rollOut
			document.getElementById(sDivIndex).src = "image/menu-"+sDivIndex+".gif";
		}
}
function showTab(sDivIndex) {
		comefunzTabs = new Array("generale","utente","condomini","tecnologia");
		for ( singleDivIndex in comefunzTabs ) {
			//alert(comefunzTabs[singleDivIndex])
			document.getElementById("comefunziona_"+comefunzTabs[singleDivIndex]).style.display = "none";
		}
		document.getElementById("comefunziona_"+sDivIndex).style.display = "block";
}
function showTabAderisci(sDivIndex) {
		comefunzTabs = new Array("premessa","quantocosta","comeaderire");
		for ( singleDivIndex in comefunzTabs ) {
			document.getElementById("aderisci_"+comefunzTabs[singleDivIndex]).style.display = "none";
		}
		if (sDivIndex == 'premessa'){
			$("#body").height(850);
		} else if (sDivIndex == 'quantocosta'){
			$("#body").height(550);
		} else if (sDivIndex == 'comeaderire'){
			$("#body").height(550);
		}
		document.getElementById("aderisci_"+sDivIndex).style.display = "block";
		//$("#body").height(550);
}
function ExpandCollapse(sDivIndex) {
	alert("delete");
	/*if (document.getElementById(sDivIndex).style.display == "none") {
		document.getElementById(sDivIndex).style.display = "block";
		document.getElementById("sector_loading").style.display = "none";
		$("#sector_avvisi").css("top",+70);
		$("#sector_rassegna").css("top",+70);
		$("#body").height(550);
	} else {
		document.getElementById(sDivIndex).style.display = "none";
		document.getElementById("sector_loading").style.display = "none";
		document.getElementById("risposta").style.display = "none";
		$("#sector_avvisi").css("top",-20);
		$("#sector_rassegna").css("top",-20);
		$("#body").height(470);
	}*/
}


function sendForm() {
	if (document.getElementById("sector_loading").style.display == "none") {
		//simple_form_init();
		document.getElementById("sector_loading").style.display = "block";
		document.getElementById("login-area").style.display = "none";
	} else {
		document.getElementById("sector_loading").style.display = "none";
		document.getElementById("login-area").style.display = "block";
	}
	//return false;
}
//FORM DI LOGIN - INIZIO
$(document).ready(simple_form_init);

function simple_form_init () {
	//$('#msgbox').toggle();
	$('#loginForm').submit(
							 function () {
								 $.ajax({
										type: 'POST',
										url: 'login-response.asp',
										data: $("#loginForm :input").serialize(),
										dataType: 'xml',
										success: onSuccess
						              });
						             return false;
							 }
				);
	}
	
	function onSuccess(xmlObj){
		//	$('#risposta').html(msg.getElementsByTagName('messaggio')[0].firstChild.nodeValue);
		//	$('#risposta').html(msg.getElementsByTagName('messaggio')[0].childNodes.lenght);
		//users = xmlObj.getElementsByTagName('users')[0];
		//outHtml = users.getElementsByTagName('utente');
		//$('#risposta').html(outHtml);
		var outHtml = "";
		var lista = xmlObj.getElementsByTagName('property');
			for (var i = 0; i < lista.length; i++) {
					//alert(lista.length);
					property = lista[i];
					user = property.getElementsByTagName('user')[0].firstChild.nodeValue;
					gruppo = property.getElementsByTagName('gruppo')[0].firstChild.nodeValue;
						if (user == "nofound") {
								$('#etichetta').html("<span class='font_red font_bold'>Dati non corretti</span>");
								document.getElementById("sector_loading").style.display = "none";
								document.getElementById("login-area").style.display = "block";
							} else {
								$('#user_box').html("Bentornato <b>"+user+"</b>");
								//$('#riv_box').html("<a href='/v4/'>&raquo; Lista comuni associati</a>");
								$('#etichetta').html("Logged in! <a href='abandon.asp'>Logout</a>");	
								//stato dei block
								document.getElementById("risposta").style.display = "block";
								document.getElementById("sector_loading").style.display = "none";
							}
				}
		}
//FORM DI LOGIN - FINE

//FORM DI PREADESIONE - INIZIO
function form_preadesione () {
				if (!document.getElementById("termini").checked) {
					alert("E\' necessario accettare i termini.");
					return false;
				}
				if (document.getElementById("cognome").value == "") {
					alert("E\' necessario compilare il campo Cognome");
					return false;
				}
				if (document.getElementById("citta").value == "") {
					alert("E\' necessario compilare il campo Citta");
					return false;
				}
				if (document.getElementById("email").value == "") {
					alert("E\' necessario compilare il campo Email");
					return false;
				}
}
/*$(document).ready(form_preadesione);

function form_preadesione () {
	$('#form_preadesione').submit(		  
							 function () {
								 if (!document.getElementById("termini").checked) {
									alert("E\' necessario accettare i termini.");
									return false;
								 }
								 if (document.getElementById("cognome").value == "") {
									alert("E\' necessario compilare il campo Cognome");
									return false;
								}
								if (document.getElementById("citta").value == "") {
									alert("E\' necessario compilare il campo Citta");
									return false;
								}
								if (document.getElementById("email").value == "") {
									alert("E\' necessario compilare il campo Email");
									return false;
								}
								 document.getElementById("preadesione_loading").style.display = "block";
								 document.getElementById("form_table").style.display = "none";
								 //alert("stai inviando " + $("#form_preadesione :input"));
								 $.ajax({
										type: 'POST',
										url: 'send-adesione.asp',
										//data: $("#form_preadesione :input").serialize(),
										data: $("#form_preadesione :input"),
										dataType: 'xml',
										success: onSuccessPre
						              });
						             return false;
							 }
				);
	}
	
	function onSuccessPre(objXML){
			document.getElementById("preadesione_loading").style.display = "none";
			document.getElementById("response_ok").style.display = "block";
		}*/
//FORM DI PREADESIONE - FINE

	function segnalazione(stx) { 
		window.open(stx,'apri','scrollbars=no,resizable=no,width=500,height=500,status=no,location=no,toolbar=no,personalbar=no');
	}
	
//controllo dell'autenticazione in fase di navigazione
function checkLogin(sessionUser){
			if (sessionUser == "nofound") {
					$('#etichetta').html("<span>Entra nell\'area riservata</span>");
					document.getElementById("sector_loading").style.display = "none";
					document.getElementById("risposta").style.display = "none";
					document.getElementById("login-area").style.display = "block";
			} else {
					$('#user_box').html("Bentornato <b>"+sessionUser+"</b>");
					//$('#riv_box').html("<a href='/'>&raquo; Lista comuni associati</a>");
					$('#etichetta').html("Logged in! <a href='/abandon.asp'>Logout</a>");	
					//stato dei block
					document.getElementById("risposta").style.display = "block";
					document.getElementById("sector_loading").style.display = "none";
					document.getElementById("login-area").style.display = "none";
				}
}
function set_region() {
	document.check_regione.action = 'copertura.asp';
	document.check_regione.submit();
}

function promoPresentaAmico () {
		document.getElementById("segnala_promo").style.display = "block";
}
function promoPresentaAmicoDati () {
		document.getElementById("segnala_promo_dati").style.display = "block";
}
function check_email(target_value) {
	if (target_value.length == 0) return true;
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < target_value.length; i++){
		if(ok.indexOf(target_value.charAt(i))<0) { 
			return (false);
		}	
	} 
	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!target_value.match(re) && target_value.match(re_two)) {
		return (-1);		
	}
}

function checkFormDati () {
	errors = '';
	id_mypromo_dati = document.getElementById("id_mypromo_dati").value;
	//alert(id_mypromo)
	nome_cognome = document.getElementById("nome_cognome").value;
	citta = document.getElementById("citta").value;
	telefono = document.getElementById("telefono").value;
	email = document.getElementById("email").value;
	
	if (id_mypromo_dati.length != 5) {
		errors += "- Inserire un codice promo di 5 lettere\n";
	}
	if (!check_email(email)) {
		errors += "- Inserisci un indirizzo email valido\n";
	}
	if (nome_cognome == "") {
		errors += "- Inserisci un nome e un cognome\n";
	}
	if (telefono == "") {
		errors += "- Inserisci una numero di telefono\n";
	}
	
	if (errors != '') {
		alert(errors)
		return false;
	} else {
		return true;
	}
}
function checkForm() {
	errors = '';
	id_mypromo = document.getElementById("id_mypromo").value;
	//alert(id_mypromo)
	nome_mittente = document.getElementById("nome_mittente").value;
	email_destinatario = document.getElementById("email_destinatario").value;
	
	/*if (id_mypromo == "") {
		errors += "E' necessario fornire il tuo codice promozionale\n";
		alert(errors)	
		return false;
	}*/
	if (id_mypromo.length != 5) {
		errors += "- Inserire un codice promo di 5 lettere\n";
	}
	if (nome_mittente == "") {
		errors += "- Inserisci il tuo nome\n";
	}
	if (email_destinatario == "") {
		errors += "- Inserisci almeno un indirizzo email nel campo destinatari\n";
	}
	
	if (errors != '') {
		alert(errors)
		return false;
	} else {
		return true;
	}
	
}