var myRequest1;
var myRequest2;
var eventi;

function CreateXmlHttpReq(handler) {
    var xmlhttp = null;
    try {
        // STD BROWSER
        xmlhttp = new XMLHttpRequest();
    } catch(e) {
        // MS
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    xmlhttp.onreadystatechange = handler;
    return xmlhttp;
}


function getRandom() {
    i = Math.round(10000*Math.random());
    return "&rand=" + i;
}

function galleryHandler() {
    if (myRequest2.readyState == 4 && myRequest2.status == 200) {
        document.getElementById("boxGallery").innerHTML = myRequest2.responseText;
        document.body.style.cursor = 'default';
        myLytebox.initialize();
    }
}


function eventiHandler() {
   if (eventi.readyState == 4 && eventi.status == 200) {
        e = document.getElementById("allpage");
        
        if (!e) {
            e= parent.document.getElementById("allpage");
        }
        
        if (!e) {
            e= top.document.getElementById("allpage");
        }
        
        e.innerHTML = eventi.responseText;
        document.body.style.cursor = 'default';
    }
}


function contentHandler() {
    if (myRequest1.readyState == 4 && myRequest1.status == 200) {
        document.getElementById("content").innerHTML = myRequest1.responseText;
        document.body.style.cursor = 'default';
    }
}


function ajaxContentRedraw(action, dhtmlHistory, store) {
    document.body.style.cursor = 'wait';
    myRequest1 = CreateXmlHttpReq(contentHandler);
    s = "" + action;
    if (store)
    //addHist(dhtmlHistory, s, "C");
    myRequest1.open("POST", s);     
    myRequest1.send(null);
}

function ajaxGalleryRedraw(action, dhtmlHistory, store) {
    document.body.style.cursor = 'wait';
    myRequest2 = CreateXmlHttpReq(galleryHandler);
    s = "" + action;
    if (store)
    //addHist(dhtmlHistory, s, "G");    
    myRequest2.open("POST", s);       
    myRequest2.send(null);
}

function showEventi(lingua, dhtmlHistory) {
    document.body.style.cursor = 'wait';
    eventi = CreateXmlHttpReq(eventiHandler);
    i = Math.round(10000*Math.random());
    s = "include/eventi.jsp?lingua=" + lingua + "&rand=" + i;    
    eventi.open("POST", s);   
    eventi.send(null);
}


function addHist(dhtmlHistory, action, chiamante) {
    i = Math.round(10000*Math.random());
    action = chiamante + action ;
    //dhtmlHistory.add("" + i, action);
}

function showOfferte(lingua, id, dhtmlHistory) {
    document.body.style.cursor = 'wait';
    eventi = CreateXmlHttpReq(eventiHandler);
    i = Math.round(10000*Math.random());
    //id = 81;
    s = "webServiceDetail.jsp?record=" +  id + "lingua=" + lingua + "&rand=" + i;    
    //dhtmlHistory.add("" + i, s);
    eventi.open("POST", s);   
    eventi.send(null);
}


function showInterni(lingua, dhtmlHistory) {
    //if (!document.getElementById("boxGallery"))
        preparePageBox(lingua);
    ajaxContentRedraw("include/" + lingua + "_interni.jsp", dhtmlHistory, true);
    ajaxGalleryRedraw("include/rootGalleryInterni.jsp", dhtmlHistory, true);
    
}

function showLocation(lingua, dhtmlHistory) {
    //if (!document.getElementById("boxGallery"))
        preparePageBox(lingua);
    ajaxContentRedraw("include/" + lingua + "_location.jsp", dhtmlHistory, true);
    ajaxGalleryRedraw("include/rootGalleryEsterni.jsp", dhtmlHistory, true);
    
}

function showPrezzi(lingua, dhtmlHistory) {
   // if (!document.getElementById("boxGallery"))
        preparePage(lingua);    
    ajaxContentRedraw("include/condizioni.jsp?lingua=" + lingua, dhtmlHistory, true);
    ajaxGalleryRedraw("include/prezzi.jsp?lingua=" + lingua, dhtmlHistory, true);  
}

function showContatti(lingua, dhtmlHistory) {
    //if (!document.getElementById("boxGallery"))
        preparePage(lingua);    
    e = document.getElementById("content");
    s = "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://maps.google.com/maps/ms?f=q&amp;hl=it&amp;geocode=&amp;ie=UTF8&amp;msa=0&amp;msid=117859528180088561655.00045362ff514a88edfd7&amp;ll=43.320089,11.330853&amp;spn=0.006295,0.006295&amp;output=embed&amp;s=AARTsJruObuwjWSL7XAFpn-zEOtuIcPdqA\"></iframe><br>";

    s+="<table border=0 width='100%'><tr><td valign='top'>";

    if (lingua=="EN") {
        s += "<font face=\"verdana\" size=\"3\"><Strong>I Terzi di Siena</Strong></font><font face=\"verdana\" size=\"2\"><br>di Cincotti Elisabetta<br><br>Via dei Termini, 13<br>Siena (ITALY)<br><br>Phone: +39 339 6699143<br>Mail: <a href=\"mailto:info@terzidisiena.com\">info@terzidisiena.com</A><br>";
    } else {
        s += "<font face=\"verdana\" size=\"3\"><Strong>I Terzi di Siena</Strong></font><font face=\"verdana\" size=\"2\"><br>di Cincotti Elisabetta<br><br>Via dei Termini, 13<br>Siena (ITALY)<br><br>Telefono: +39 339 6699143<br>Mail: <a href=\"mailto:info@terzidisiena.com\">info@terzidisiena.com</A><br>";
    }
    
    s+="<br><font face=\"verdana\" size=\"2\">P.IVA: 01236460521</font><br><br><br>";
    
    s+="</td><td valign='top'><!--PER LUCA..METTI QUI--></td></tr></table>";
    
    e.innerHTML = s;

    e = document.getElementById("boxGallery");
    ajaxGalleryRedraw("include/showLink.jsp", dhtmlHistory, true);
}


function preparePageBox(lingua) {
    e = document.getElementById("allpage");
    
    if (e)
        e.innerHTML = "<table width=\"790\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"326\" valign=\"top\"><table width=\"322\" cellpadding=\"0\" cellspacing=\"0\"><tr bgcolor=\"#a77655\"><td valign=\"top\"><span id=\"boxGallery\"></span>" +
            //"</td></tr><!--<tr><td align=\"center\" class=\"titolo\">&nbsp;</td></tr><tr><td bgcolor=\"#c47a4f\"align=\"center\" class=\"titolo\"><br>Attività<br>&nbsp;</td></tr><!--<tr><td bgcolor=\"#c47a4f\"align=\"center\" valign=\"top\">" +
            "</td></tr>" +
            //"<iframe width=\"310\" height=\"500\" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no id=\"datamain\" scrolling=\"auto\" frameborder=\"0\" src=\"include/getOffertsList.jsp?lingua=" + lingua +"\"></iframe>" +
            //"</td></tr>-->" +
            "<tr><td background=\"images/gradazione.jpg\" align=\"center\"><br><br><br><br><br><br></td>" +
            "</tr></table></td><td valign=\"top\" align=\"left\"><table cellpadding=\"3\" cellspacing=\"3\"><tr><td><span id=\"content\"></span></td></tr></table></td></tr></table>";
}

function preparePage(lingua) {
    e = document.getElementById("allpage");
    
    if (e)
        e.innerHTML = "<table width=\"790\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"326\" valign=\"top\"><table width=\"322\" cellpadding=\"0\" cellspacing=\"0\"><tr bgcolor=\"#c47a4f\"><td valign=\"top\"><span id=\"boxGallery\"></span>" +
            "</td></tr><tr><td valign=\"top\" bgcolor=\"#c47a4f\"align=\"center\">" +
            "</td></tr></table></td><td valign=\"top\" align=\"left\"><table cellpadding=\"3\" cellspacing=\"3\"><tr><td valign=\"top\"><span id=\"content\"></span></td></tr></table></td></tr></table>";
}

function inglese() {
    document.location.href="inglese.jsp";
}

function italiano() {
    document.location.href="italiano.jsp";
}


function mostraDettagli(id, lingua) {
    //showOfferte(lingua,id);
    e = document.getElementById("allpage");
    
    if (!e) {
        e= parent.document.getElementById("allpage");
    }
    
    if (!e) {
        e= top.document.getElementById("allpage");
    }
        
    e.innerHTML = "<iframe width=\"770\" height=\"600\" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 id=\"datamain\" scrolling=\"auto\" frameborder=\"0\" src=\"include/webServiceDetail.jsp?record=" + id + "&lingua=" + lingua +"\"></iframe>";    
}



function openNoHistory(action) {
    alert("openNoHistory:" + action);
        
    if (action.charAt(0)=="G") {
        a = action.substring(1);
        if (document.getElementById("boxGallery")) {
            ajaxGalleryRedraw(a, null, false);
        }
    } else {
        a = action.substring(1);
        if (document.getElementById("content")) {
            ajaxContentRedraw(a, null, false);
        }        
    }
}

