var xmlHttp = creaXmlHttpRequestObject();

function creaXmlHttpRequestObject()
{

    var xmlHttp; 

    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        // solo per Explorer
        var versioni = new Array("MSXML2.XMLHTTP.6.0",
        "MSXML2.XMLHTTP.5.0 ",
        "MSXML2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0",
        "MSXML2.XMLHTTP",
        "Microsoft.XMLHTTP");
        for (var i=0; i<versioni.length && !xmlHttp; i++)
        {
            try
            {
                xmlHttp = new ActiveXObject(versioni[i]);
            }
            catch (e) {}
        }
    }

    if (!xmlHttp) 
    alert("Impossibile istanziare l'oggetto XmlHttpRequest");
    else
    return xmlHttp;
}

function valida()
{

    if (xmlHttp)
    {
	   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	   {
	    	var TM= new Array();
			cognome			=	encodeURIComponent(document.getElementById("cognome").value);
			nome			=	encodeURIComponent(document.getElementById("nome").value);
			
			MatrDateDay		=	encodeURIComponent(document.getElementById("MatrDateDay").value);
			MatrDateMonth	=	encodeURIComponent(document.getElementById("MatrDateMonth").value);
			MatrDateYear	=	encodeURIComponent(document.getElementById("MatrDateYear").value);
			
			PartDateDay		=	encodeURIComponent(document.getElementById("PartDateDay").value);
			PartDateMonth	=	encodeURIComponent(document.getElementById("PartDateMonth").value);
			PartDateYear	=	encodeURIComponent(document.getElementById("PartDateYear").value);

			comune			=	encodeURIComponent(document.getElementById("comune").value);
			
			mail			=	encodeURIComponent(document.getElementById("mail").value);
			
			
			tipViaggio 		=	encodeURIComponent(document.getElementById("tipViaggio").value);
			durViaggio		=	encodeURIComponent(document.getElementById("durViaggio").value);
			
			budget			=	encodeURIComponent(document.getElementById("budget").value);
			richieste		=	encodeURIComponent(document.getElementById("richieste").value);
			
			
			try
	        {		
                //document.getElementById("loading").style.visibility="visible";

                xmlHttp.open("POST", "validate.php", true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 	
                xmlHttp.onreadystatechange = risposta_validazione;
				xmlHttp.send("cognome=" + cognome + "&nome=" + nome + "&MatrDateDay=" + MatrDateDay + "&MatrDateMonth=" + MatrDateMonth + "&MatrDateYear=" + MatrDateYear + "&PartDateDay=" + PartDateDay + 
							 "&PartDateMonth=" + PartDateMonth +"&PartDateYear=" + PartDateYear + "&comune=" + comune + "&mail=" + mail + "&tipViaggio=" + tipViaggio + "&durViaggio=" + durViaggio + 
							 "&budget=" + budget + "&richieste=" + richieste);
			}
	        catch (e)
	        {
	            alert ("Errore nella validazione dei campi... Riprova tra qualche secondo ...");
	            setTimeout("valida();", 5000); 
	        }
	    }
	}
}

function risposta_validazione()
{
    if(xmlHttp.readyState == 4){
     
	    if (xmlHttp.status == 200) 
	    {
	     
	        try
	        {
	         /* 
			 	in questo caso, per semplice dimostrazione, utilizzerò
	            responseText in luogo di responseXml: la risposta ritornata
	            al client sarà una stringa di testo costituita da coppie
	            nome_del_div/valore, separate tra di loro dalla stringa
	            [|]
	         */
	            var response = xmlHttp.responseText; 

	            var update = new Array(); 			 
	            var counter = 1;
	         	var index;
				
	
	
	            if(response.indexOf('[|]' != -1)) 
				{
	                update = response.split('[|]');
					
					ctrl=false;
					
					while (update[counter])	   
	                {   
						
						document.getElementById(update[counter]).innerHTML = update[counter+1];
	                   // alert(update[counter] + '---' + update[counter+1]+ '---' + update[counter+2]);
						
						if (update[counter+1]=="")
						{ 	
							
							document.getElementById(update[counter]).style.display="none";
							s= "";
							so_applyStyleString(document.getElementById(update[counter+2]),s);
						}
						else
						{	
							ctrl=true;
							if (update[counter+2]=="messaggio_ok")
							{
								/*document.getElementById(update[counter]).style.display="block";
								s="color:#ffffff;font-weight:bold;";
								so_applyStyleString(document.getElementById(update[counter+2]),s);*/
								if (update[counter+1]=="ok_ins"){
									location.href = "register_ok.php";
								}
							}
						}     
	                    counter=counter+3;
	                }
	                
				  if (ctrl==false)
				  {
						//return cancella_campi();  
				  }
	            }
	
	        }	
	        catch(e)
	        {
				alert(e);
	        }
	        
		}
		
    }
    
}

function so_applyStyleString(obj,str){
if(document.all && !window.opera)
    obj.style.setAttribute("cssText",str);
else
    obj.setAttribute("style",str);
} 

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	/*if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";*/
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
