var path = '';
var food = 0;
var banner = 0;


function getPageMail(url, isSubmit) {
    var xmlHttp = false;

    try {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            xmlHttp = false;
        }
    }

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.open('POST', url, true);
    xmlHttp.onreadystatechange=function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById('mail').value = xmlHttp.responseText;
            if (isSubmit) {
                document.getElementById('frmMail').submit();
            }
        }
    }
    xmlHttp.send(null);
}


function getPage(url) {
    var xmlHttp = false;

    var top = (screen.height - 150) / 2;
    var left = (screen.width - 300) / 2; 

    food++
    if (food == 10) {
        food = 1;
    }

    document.getElementById('imgTopo').src = path + '/images/topos/img_topo_0' + food + '.jpg';
    document.getElementById('imgTopo').code = food;
    
    document.getElementById('layerLoading').style.display = 'block';
    document.getElementById('layerLoading').style.left = left;
    document.getElementById('layerLoading').style.top = top;

    document.getElementById('frameLoading').style.display = 'block';
    document.getElementById('frameLoading').style.left = left;
    document.getElementById('frameLoading').style.top = top;

    try {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            xmlHttp = false;
        }
    }

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.open('POST', url, true);
    xmlHttp.onreadystatechange=function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById('layerLoading').style.display = 'none';
            document.getElementById('frameLoading').style.display = 'none';
            document.getElementById('body').innerHTML = xmlHttp.responseText;
            showBanner(2);
        }
    }
    xmlHttp.send(null);
}

function loadBasket(url) {
    var xmlHttp = false;

    try {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            xmlHttp = false;
        }
    }

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.open('POST', url, true);
    xmlHttp.onreadystatechange=function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById('basket').innerHTML = xmlHttp.responseText;
            showBanner(2);
        }
    }
    xmlHttp.send(null);
}

function addProduct(product, quantity) {
    var xmlHttp = false;
    var top = (screen.height - 150) / 2;
    var left = (screen.width - 300) / 2; 

    if (quantity != '' && quantity != 0) {
        document.getElementById('layerLoading').style.display = 'block';
        document.getElementById('layerLoading').style.left = left;
        document.getElementById('layerLoading').style.top = top;

        document.getElementById('frameLoading').style.display = 'block';
        document.getElementById('frameLoading').style.left = left;
        document.getElementById('frameLoading').style.top = top;
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            } catch(e) {
                xmlHttp = false;
            }
        }

        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp = new XMLHttpRequest();
        }

        xmlHttp.open('POST', path + '/ajax/managerproduct.jsp?o=1&p=' + product + '&q=' + quantity, true);
        xmlHttp.onreadystatechange=function() {
            if (xmlHttp.readyState == 4) {
                loadBasket(path + '/ajax/basket.jsp');
                document.getElementById('layerError').innerHTML = xmlHttp.responseText;
                document.getElementById('layerLoading').style.display = 'none';
                document.getElementById('frameLoading').style.display = 'none';
            }
        }
        xmlHttp.send(null);
    } else {
        document.getElementById('layerError').innerHTML = 'Por favor, insira uma quantidade para compra.';
    }
}

function removeProduct(product) {
    var xmlHttp = false;
    var top = (screen.height - 150) / 2;
    var left = (screen.width - 300) / 2; 

    document.getElementById('layerLoading').style.display = 'block';
    document.getElementById('layerLoading').style.left = left;
    document.getElementById('layerLoading').style.top = top;

    document.getElementById('frameLoading').style.display = 'block';
    document.getElementById('frameLoading').style.left = left;
    document.getElementById('frameLoading').style.top = top;

    try {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            xmlHttp = false;
        }
    }

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.open('POST', path + '/ajax/managerproduct.jsp?o=2&p=' + product, true);
    xmlHttp.onreadystatechange=function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById('layerError').innerHTML = xmlHttp.responseText;
            loadBasket(path + '/ajax/basket.jsp');
            getPage(path + '/ajax/listbasket.jsp');
        }
    }
    xmlHttp.send(null);
}

function showBanner(quantidade) {
    for (x = 0; x < quantidade; x++) {
        layer = 'banner' + (x + 1);
        if (document.getElementById(layer) != null) {
            if (banner == 4) {
                banner = 0;
            }
            banner++;
            document.getElementById(layer).innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="132" width="157"><param name="movie" value="' + path + '/flash/ban_0' + banner + '.swf"><param name="quality" value="high"><embed src="' + path + '/flash/ban_0' + banner + '.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" height="132" width="157"></embed></object>';
        }
    }
}
