// Create the XML HTTP request object. We try to be
// more cross-browser as possible.
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 securityCodeCheckHandler() { 
 
if (request.readyState == 4 && request.status == 200) { 
var response = request.responseText;
if (response.indexOf("OK") != -1) { 
document.contatti.submit(); 
} 
else {
alert("Codice di sicurezza errato!"); 
xwarnField("qcode","contatti"); 
} } }

function generalAjaxHandler(field,message,onErrorFunction,onSuccessFunction) { 
	if (request.readyState == 4 && request.status == 200) 
	{ 
	var response = request.responseText;
	//alert(response);
	if (response.indexOf("OK") != -1) {
	onSuccessFunction(); } 
	else { 
	if (onErrorFunction == null) { alert(message); xwarnField('qcode',"contatti"); } 
	else {
	onErrorFunction(); 
	} 
	ableButton(); 
	} 
} } 


function checkSecurityCode() { 
var myfunc = function() { 
generalAjaxHandler("code","Codice di sicurezza errato!",null, submitForm); } 
request = CreateXmlHttpReq(myfunc);
request.open("GET","/checkcode.php?do=verify&session_code="+document.contatti.sessione.value+"&code="+document.contatti.code.value+"&rand="+escape(Math.random()));
request.send(null); } 



function check_form_contatti() {
disableButtonId("contattiButtonx");
	var xnumadulti = 0;
	var i = 0;
	if (!validate(document.getElementById("q0").value, "^.*[^ ]+.*$", "Il nome non puo' contenere caratteri speciali o essere vuoto","q0"))
	{
	ableButtonId();
	xwarnField("q0","contatti"); 
//	document.getElementById("errorboxcontatti").innerHTML='<br /><div style="border:1px solid #FF0000;"><p><b>Per proseguire occore inserire almeno il nome</b></p></div>'; 
	return false;
	}

	if (!isValidEmail(document.getElementById("q2").value)) { 
	ableButtonId();
	warnField("q2","contatti"); 
	alert("formato email non valido.");
//	document.getElementById("errorboxcontatti").innerHTML='<br /><div style="border:1px solid #FF0000;"><p><b>Email non valida.</b></p></div>'; 
	return false; 
	}

	checkSecurityCode();
	
}
function disableButtonId(){
document.getElementById("contattiButtonx").disabled=true;
document.getElementById("contattiButtonx").innerHTML='<span style="font-size:11px;font-family:Helvetica,Arial,sans-serif">Attendere...</span>'; 
}
function ableButtonId(id){
document.getElementById("contattiButtonx").disabled=false;
document.getElementById("contattiButtonx").innerHTML='<div align="center" style="margin-left:0px;" id="contattiButtonx" ><input  type="button" style="margin-top:10px;margin-left:1px;height:20px;width:80px;color:#fff;background-color:#aa1f22;" value="Invia" onclick="javascript:check_form_contatti();return false;" /></div>'; 
}


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


function validate(string, regexp, err, fieldName) {
    if (string.match(regexp) == null) {
        alert(err);
        warnField(fieldName);
        return false;
    }
    return true;
}

function validateForm(string, regexp, err, fieldName,formName) {
    if (string.match(regexp) == null) {
//        alert(err);

        xwarnField(fieldName,formName);
        return false;
    }
    return true;
}

function warnField(fieldName)
{
    eval("document.getElementById('"+fieldName+"').style.border='1px solid #E74815';");
    eval("document.getElementById('"+fieldName+"').focus();");
}
 
function xwarnField(fieldName,formname)
{
    eval("document."+formname+"."+fieldName+".style.border='1px solid #E74815';");
    eval("document."+formname+"."+fieldName+".focus();");
ableButtonId();
}

function isValidEmail(a)
{
        var at = a.indexOf("@");
        var name = a.substring(0, at);
        var isp = a.substring(at + 1, a.length);
        var dot = a.lastIndexOf(".");
        if (at == -1 || at == 0 || name == "" || isp == "" || dot == -1 ||
dot== (a.length - 1))
        {
                return false;
        }
        return true;
}

