/* Copyright(C) 2005,2006,2007 Salvatore Sanfilippo <antirez@gmail.com>
 * All Rights Reserved. */

// 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();
  } 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!
function ajaxGet(url,handler) {
    var a = new Array("placeholder");
    for (var j=2; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var ajax_req = CreateXmlHttpReq(DummyHandler);
    var myhandler = function() {
        var content = ajaxOk(ajax_req);
        if (content !== false) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
    ajax_req.onreadystatechange = 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(req) {
    if (req.readyState == 4 && req.status == 200) {
        return req.responseText;
    } else {
        return false;
    }
}

function $(id) {
    return document.getElementById(id);
}

function $html(id) {
    return document.getElementById(id).innerHTML;
}

function $sethtml(id,html) {
    document.getElementById(id).innerHTML = html;
}

function $apphtml(id,html) {
    document.getElementById(id).innerHTML += html;
}

//---------------------------------------------------------------------------------

function santo(){
	ajaxGet("http://www.rosariomonteleone.it/wp/external/santo.php",myHandler,"santo-div");	
}
function imperia(){
	ajaxGet("http://www.rosariomonteleone.it/wp/external/imperia.php",myHandler,"imperia-div");	
}
function savona(){
	ajaxGet("http://www.rosariomonteleone.it/wp/external/savona.php",myHandler,"savona-div");	
}
function genova(){
	ajaxGet("http://www.rosariomonteleone.it/wp/external/genova.php",myHandler,"genova-div");	
}
function spezia(){
	ajaxGet("http://www.rosariomonteleone.it/wp/external/spezia.php",myHandler,"spezia-div");	
}

function myHandler(content,elementid) {
	$sethtml(elementid, content);
}

function controllo(){
	with(document.form1) {
		if($('#tf_nome').val() == "") 		{	$('#n_novalid').html("Inserire nome");			$('#tf_nome').css('border', '1px solid red');		$('#tf_nome').focus();	return false; }else {		$('#n_novalid').html("");		$('#tf_nome').css('border', '1px solid green');}
		if($('#tf_cognome').val() == "")	{	$('#c_novalid').html("Inserire cognome");		$('#tf_cognome').css('border', '1px solid red');	$('#tf_cognome').focus();	return false; }else {	$('#c_novalid').html("");		$('#tf_cognome').css('border', '1px solid green');}
		//if($('#tf_indirizzo').val() == "") 	{	$('#i_novalid').html("Inserire indirizzo");		$('#tf_indirizzo').css('border', '1px solid red');	$('#tf_indirizzo').focus();	return false; }else {	$('#i_novalid').html("");		$('#tf_indirizzo').css('border', '1px solid green');}
		//if($('#tf_mail').val() == "") 		{	$('#m_novalid').html("Inserire mail");			$('#tf_mail').css('border', '1px solid red');		$('#tf_mail').focus();	return false; }else {		$('#m_novalid').html("");		$('#tf_mail').css('border', '1px solid green');}
		//if($('#tf_cell').val() == "") 		{	$('#cell_novalid').html("Inserire cellulare");	$('#tf_cell').css('border', '1px solid red');		$('#tf_cell').focus();	return false; }else {		$('#cell_novalid').html("");	$('#tf_cell').css('border', '1px solid green');}
		
		if($('#tf_mail').val() != "") 		{
			var regexp = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
			var mail2 = $('#tf_mail').val(); if (!regexp.test(mail2)){ $('#m_novalid').html("Inserire mail valida"); $('#tf_mail').css('border', '1px solid red'); $('#tf_mail').focus(); return false; }else {	$('#m_novalid').html(""); $('#tf_mail').css('border', '1px solid green');	}
		}
		if($('#tf_cell').val() != "") 		{
			var regexp2 = /[0-9]{10}/;
			var cell2 = $('#tf_cell').val(); if (!regexp2.test(cell2)){ $('#cell_novalid').html("Inserire numero valido (10 cifre)"); $('#tf_cell').css('border', '1px solid red'); $('#tf_cell').focus(); return false; }else {	$('#cell_novalid').html(""); $('#tf_cell').css('border', '1px solid green');	}
		}
		
	}
	//$('#tasto_verifica').animate({opacity:0}, 500);
	var nomek = $('#tf_nome').val();
	var cognomek = $('#tf_cognome').val();
	var indirizzok = $('#tf_indirizzo').val();
	var mailk = $('#tf_mail').val();
	var cellk = $('#tf_cell').val();
	
	
	//$('#form1').animate({opacity:0}, 500);
	ajaxGet("http://www.rosariomonteleone.it/wp/iscrizione2.php?n="+nomek+"&c="+cognomek+"&i="+indirizzok+"&m="+mailk+"&cell="+cellk, myHandler, "result-iscrizione");
	//$('#form_contatti2').animate({opacity:100}, 500);*/
	return false;
}




