/* Copyright(C) 2006 Antonio Trigiani * All Rights Reserved. */
function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
    try {
        // Fix for some version of Mozilla browser.
        http_request.overrideMimeType('text/xml');
    } catch(e) {
    }
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

// An handler that does nothing, used for AJAX requests that
// don't require a reply and are non-critical about error conditions.
function DummyHandler() {
    return true;
}

// Shortcut for creating a GET request and get the reply
// This few lines of code can make Ajax stuff much more trivial
// to write, and... to avoid patterns in programs is sane!
var ajax_req = null;

function ajaxGet(url,handler) {
    var a = new Array("placeholder");
    for (var j=2; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var myhandler = function() {
        var content = ajaxOk();
        if (content !== false) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
    ajax_req = CreateXmlHttpReq(myhandler);
    ajax_req.open("GET",url);
    ajax_req.send(null);
}

// IE 5.0 does not support the apply() method of the function object,
// we resort to this eval-based solution that sucks because it is not
// capable of preserving 'this' and is ugly as hell, but it works for us.
function myDummyApply(funcname,args) {
    var e = "funcname(";
    for (var i = 0; i < args.length; i++) {
        e += "args["+i+"]";
        if (i+1 != args.length) {
            e += ",";
        }
    }
    e += ");"
    return eval(e);
}

// Add a random parameter to the get request to avoid
// IE caching madness.
function ajaxGetRand(url,handler) {
    url += (url.indexOf("?") == -1) ? "?" : "&";
    url += "rand="+escape(Math.random());
    arguments[0] = url;
    try {
        return ajaxGet.apply(this,arguments);
    } catch(e) {
        return myDummyApply(ajaxGet,arguments);
    }
}

function ajaxOk() {
    if (ajax_req.readyState == 4 && ajax_req.status == 200) {
        return ajax_req.responseText;
    } else {
        return false;
    }
}

var request = false; 



function submitForm() {
document.miaform.submit(); } 


function sendContact() { 
var myfunc = function() { 
generalAjaxHandler("msgcontatti1","Problemi invio contatti, si consiglia di attendere qualche secondo e riprovare",null, submitForm); } 
request = CreateXmlHttpReq(myfunc);
ajaxGetRand('/invia.php?nome='+document.miaform.nome1.value+'&cognome='+document.miaform.cognome1.value+'&citta='+document.miaform.citta1.value+'&tel='+document.miaform.tel.value+'&email='+document.miaform.email1.value+'&msgcontatti='+document.miaform.mexage.value+'&rand='+escape(Math.random()),Handler);

function doRedirect() { //funzione con il link alla pagina che si desidera raggiungere
location.href = "http://www.nozzedincanto.it/ok";
}


function Handler(aa){ 
	if(aa!='ERROR'){
	document.getElementById('dinamico').innerHTML="";
	document.getElementById('dinamail').innerHTML="<h3>OK EMAIL INVIATA CORRETTAMENTE</h3><p>Grazie per averci contatto</p>";
	return true; }
	else
	document.getElementById('dinamico').innerHTML=aa;
	return false; } 
} 



function checkFormContact() { 

disableButtonContatti();

if (!validate(document.miaform.nome1.value, "^[a-zA-Z0-9]+$", "Il nome risulta essere vuoto o non corretto \n .","nome1"))
{ 
ableButtonContatti(); 
return false; 
} 

if (!validate(document.miaform.cognome1.value, "^[a-zA-Z0-9]+$", "Il cognome risulta essere vuoto o non corretto \n .","cognome1"))
{ 
ableButtonContatti(); 
return false; 
} 



//if(document.miaform.informativa.checked==false){
//alert('Per proseguire devi dichiarare di aver letto e preso visione dell\'informativa privacy'); ableButton();
//document.miaform.informativa.focus(); 
//ableButtonContatti(); 
//return false; 
//}

if (!isValidEmail(document.miaform.email1.value)) { 
alert("Email non valida!");
warnField("email1"); 
ableButtonContatti(); 
return false;
}


if (document.miaform.mexage.value == '') {
alert("Messaggio non valido!");
warnField("mexage"); 
ableButtonContatti(); 
return false;
}
else {
submitForm();
}

}

function disableButtonContatti(){
document.getElementById("sendButton").disabled=true;
document.getElementById("sendButton").value="Attendere..."; 


}
function ableButtonContatti(){
document.getElementById("sendButton").disabled=false;
document.getElementById("sendButton").value="Invia Messaggio"; }


function clearLight() {
document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';
document.getElementById('light').innerHTML='';
}


