// JavaScript Document



	//////////////////////////////////// ajax core

var http_request = false;
var MyPostSend = "";


    function makeRequestPost(url) {

        http_request = false;
		//MyPostSend = "pippo=" + escape(document.getElementById('pippo').value);

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        //http_request.onreadystatechange = alertContents;
        http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
		http_request.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
        http_request.send(MyPostSend);		
    }
	
	function makeRequestGet(url) {

        http_request = false;
		//MyPostSend = "pippo=" + escape(document.getElementById('pippo').value);

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        //http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
		http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
		http_request.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
        http_request.send(null);		
    }

    function alertContents() {
	 	if (http_request.readyState > 0 && http_request.readyState <4) {
			document.getElementById('allerta').style.display = 'none';
		} else {
			document.getElementById('allerta').style.display = 'block';
		}
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                alert(http_request.responseText);
				/*var xmldoc = http_request.responseXML;
				var root_node = xmldoc.getElementsByTagName('root').item(0);
				alert(root_node.firstChild.data);
				document.getElementById('writer').innerHTML += "<br>"+root_node.firstChild.data;
				//alert(http_request.responseText);
				document.getElementById('allerta').style.display = 'none';*/
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
	
	
	//////////////////////////////////// Funzioni che usano ajax:
	
	function set_error(id, text){
		document.getElementById(id).innerHTML = text;
		document.getElementById(id).style.display = 'block';
		document.getElementById(id).className = 'msg_error';
		//new Effect.Highlight('mensaje',{duration:1.5});
	}
	function set_info(id, text){
		document.getElementById(id).innerHTML = text;
		document.getElementById(id).style.display = 'block';
		document.getElementById(id).className = 'msg_info';
		//new Effect.Highlight('mensaje',{duration:1.5});	
	}
	
	function contactRequest(){
		if(checkForm()){
			MyPostSend = "&nome="+document.getElementById('nome').value;
			MyPostSend += "&citta="+document.getElementById('citta').value;
			MyPostSend += "&telefono="+document.getElementById('telefono').value;
			MyPostSend += "&email="+document.getElementById('email').value;
			MyPostSend += "&info="+document.getElementById('info').value;
			makeRequestPost('include/ajax_query/email_contatti.php');
			http_request.onreadystatechange = get_Contact;
		}else{
			return false;
		}
	}
	
	function get_Contact(){
		if (http_request.readyState == 0){
			//set_info('msg_contact_box', 'Contattando il server di posta..');
		}
		if (http_request.readyState == 1){
			//set_info('msg_contact_box', 'Contattando il server di posta..');
		}
		if (http_request.readyState == 4){
			if (http_request.status == 200){
				if(http_request.responseText == 'true'){
					set_info('msg_contact_box', 'Dati inviatti. Recibir&aacute; una risposta a pi&ugrave; presto posibile.');
					return true;
				}else{
					set_error('msg_contact_box', 'Non &egrave; stato posibile inviare la mail.');
					return false;
				}				
			} else {
				set_error('msg_contact_box', 'Non &egrave; stato posibile contattare il server di posta.');
				return false;
			}
		}
	}
	
	
	
	function adesioneRequest(){
		// Controllo i campi obligatori
		if(document.getElementById('azione1').checked == false && document.getElementById('azione2').checked == false){
			set_error('msg_contact_box', 'Atenzione: manca il tipo di vinculo (adesione o rinnovo).');
			return false;
		}		
		if(document.getElementById('cognome').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca il suo cognome.');
			return false;
		}		
		if(document.getElementById('nome').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca il suo nome.');
			return false;
		}		
		if(document.getElementById('indirizzo').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca il indirizzo.');
			return false;
		}
		if(document.getElementById('cap').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca il CAP.');
			return false;
		}
		if(document.getElementById('comune').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca il comune di residenza.');
			return false;
		}
		if(document.getElementById('anno').value == ''){
			set_error('msg_contact_box', 'Atenzione: manca specificare un anno di validesa.');
			return false;
		}		
		if(document.getElementById('socio_rad1').checked == false && document.getElementById('socio_rad2').checked == false){
			set_error('msg_contact_box', 'Atenzione: debi specificare il tipo di socio che vuoi diventare.');
			return false;
		}
		if(document.getElementById('socio_rad2').checked == true && document.getElementById('socio2_prezzo').value == ''){
			set_error('msg_contact_box', 'Atenzione: debi specificare un contributo in euro.');
			return false;
		}
		if(document.getElementById('privacy').checked == false){
			set_error('msg_contact_box', 'Atenzione: debi leggere ed accettare la privacy.');
			return false;
		}

		// Preparo i dati per essere inviati attraverso POST
		if(document.getElementById('azione1').checked == true){ // ordinario
			MyPostSend += "&azione=adesione";
		}else if(document.getElementById('azione2').checked == true){ // sostenitore
			MyPostSend += "&azione=rinuovo";
		}
		MyPostSend += "&cognome="+document.getElementById('cognome').value;
		MyPostSend += "&nome="+document.getElementById('nome').value;
		MyPostSend += "&luogo="+document.getElementById('luogo').value;
		MyPostSend += "&giorno="+document.getElementById('giorno').value;
		MyPostSend += "&mese="+document.getElementById('mese').value;
		MyPostSend += "&anno_nas="+document.getElementById('anno_nas').value;
		MyPostSend += "&indirizzo="+document.getElementById('indirizzo').value;
		MyPostSend += "&cap="+document.getElementById('cap').value;
		MyPostSend += "&comune="+document.getElementById('comune').value;
		MyPostSend += "&provincia="+document.getElementById('provincia').value;
		MyPostSend += "&professione="+document.getElementById('professione').value;			
		MyPostSend += "&telefono="+document.getElementById('telefono').value;
		MyPostSend += "&teluff="+document.getElementById('teluff').value;
		MyPostSend += "&cell="+document.getElementById('cell').value;
		MyPostSend += "&email="+document.getElementById('email').value;
		MyPostSend += "&anno="+document.getElementById('anno').value;		
		if(document.getElementById('socio_rad1').checked == true){ // ordinario
			MyPostSend += "&socio=ordinario";
			MyPostSend += "&prezzo="+document.getElementById('socio1_prezzo').value;
		}else if(document.getElementById('socio_rad2').checked == true){ // sostenitore
			MyPostSend += "&socio=sostenitore";
			MyPostSend += "&prezzo="+document.getElementById('socio2_prezzo').value;
		}
		makeRequestPost('include/ajax_query/email_adesione.php');
		http_request.onreadystatechange = function(){
			set_info('msg_contact_box', 'Contattando il server di posta..');
			if (http_request.readyState == 4){
				if (http_request.status == 200){
					if(http_request.responseText == 'true'){
						set_info('msg_contact_box', 'Dati inviatti. Recibir&aacute; una risposta a pi&ugrave; presto posibile.');
						return true;
					}else{
						set_error('msg_contact_box', 'Non &egrave; stato posibile inviare la mail.');
						return false;
					}				
				} else {
					set_error('msg_contact_box', 'Non &egrave; stato posibile contattare il server di posta.');
					return false;
				}
			}
		}//end subfunction
	}//end function
	
	
	
	
	function loginRequest(obj1,obj2){
		MyPostSend = "&obj1="+document.getElementById(obj1).value+"&obj2="+document.getElementById(obj2).value;
		makeRequestPost('include/ajax_query/login.php');
		http_request.onreadystatechange = get_LoginResponse;
	}
	
	function get_LoginResponse(){
		set_info('msg_login_box', 'Inviando i dati..');
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if(http_request.responseText.match('true_')){
					set_info('msg_login_box', 'Benvenuto, ' + http_request.responseText.substring(5) + '.');
					document.getElementById('forum_li').style.display = 'block';
					new Effect.Highlight('forum_li',{duration:1});
				}else{
					set_error('msg_login_box', 'Dati incorretti.');
					return false;
				}				
			} else {
				msg_error('login_msg_box', 'Errore al caricare i dati.');
				return false;
			}
		}
	}


function NoError()
	{
	return true;
	}
window.onerror=NoError;



