﻿/*/
GESTIONE E-SHOP
file:
shop.ashx           - elaborazione richieste Ajax
shop.js             - gestione eventi lato client
shop.ascx           - carrello e file .NET
menu.ascx           - carrello piccolo
conferma.ascx       - conferma ordine ed elaborazione paypal
shopping_cart.cs    - classe con tutti i metodi di calcolo, info pagamenti, generazione random order
registrazione.ascx  - registrazione utente
shop.mdb            - database

1) Inserire <div id='shop'></div> su menu.ascx (per carrello piccolo) e shop.ascx (carrello completo)
2) Quando siamo su shop.ascx, rendere non visibile il <div> su menu.ascx (utilizzano gli stessi ID)
3) Solo email e password per accesso sono lato server(ctl_15), questo per la validazione dei form
4) <div id='my_alert'></div> (per le finestre di alert) è su menu.ascx, se non si usa il carrello piccolo metterlo su shop.ascx
/*/

var my_xhr;
var my_xhr_shop;
var my_xhr_svuota;
var my_xhr_remove;
var my_xhr_qta;
var my_xhr_pay;
var my_xhr_confirm;
var my_xhr_accesso;
var my_timer;
var pay_type;
var url = location.href;
var get_channel;
var get_pay_id;
var get_country;
var lingua;
lingua = url.split('/')[3];
if (lingua == "default.aspx") {
    lingua = "it";
}

// CARRELLO (piccolo e completo)
function shop(channel) {
    //document.getElementById('shop').innerHTML = "<img src='/images/preloader.gif' alt=''/>";
    if (window.XMLHttpRequest) {
        my_xhr = new XMLHttpRequest();
    }
    else {
        my_xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr.onreadystatechange = do_shop;
    my_xhr.open("GET", "/shop.ashx?refresh=yes&lingua=" + lingua + "&channel=" + channel);
    my_xhr.send(null);

    get_channel = channel;
}

function do_shop() {
    if (my_xhr.readyState == 4) {
        var data = my_xhr.responseText;
        document.getElementById('shop').innerHTML = data;
        if (document.getElementById("payment") != null) {
            if (document.getElementById("payment").value != "") {
                pay(get_pay_id, get_country);
            }
        }
    }
}

//AGGIUNGO PRODOTTO
function add(what) {
    if (window.XMLHttpRequest) {
        my_xhr_shop = new XMLHttpRequest();
    }
    else {
        my_xhr_shop = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr_shop.onreadystatechange = do_add;
    my_xhr_shop.open("GET", "/shop.ashx?add=yes&lingua=" + lingua + "&id=" + what);
    my_xhr_shop.send(null);
}

function do_add() {
    if (my_xhr_shop.readyState == 4) {
        document.getElementById("alert_text").innerHTML = my_xhr_shop.responseText;
        document.getElementById("my_button").value = "OK";
        document.getElementById("div_confirm").style.display = "none";
        openAlert('my_alert');
        shop(get_channel);
    }
}

//MODIFICO QTA
function qta(qta_id, operator) {
    if (window.XMLHttpRequest) {
        my_xhr_qta = new XMLHttpRequest();
    }
    else {
        my_xhr_qta = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr_qta.onreadystatechange = do_qta;
    my_xhr_qta.open("GET", "/shop.ashx?qta=yes&lingua=" + lingua + "&id=" + qta_id + "&mode=" + operator);
    my_xhr_qta.send(null);
}

function do_qta() {
    if (my_xhr_qta.readyState == 4) {
        document.getElementById("alert_text").innerHTML = my_xhr_qta.responseText;
        document.getElementById("my_button").value = "OK";
        document.getElementById("div_confirm").style.display = "none";
        openAlert('my_alert');
        shop(get_channel);
    }
}

//ELIMINO PRODOTTO
function conferma_remove(the_id) {
    if (lingua == "it") {
        document.getElementById("alert_text").innerHTML = "Eliminare il prodotto?";
        document.getElementById("my_button").value = "Annulla";
    }
    else {
        document.getElementById("alert_text").innerHTML = "Delete product?";
        document.getElementById("my_button").value = "Cancel";
    }
    document.getElementById("div_confirm").style.display = "block";
    document.getElementById("my_btn_confirm").onclick = "remove(" + the_id + ")";
    openAlert('my_alert');
}

function remove(remove_id) {
    closeAllModalWindows();
    if (window.XMLHttpRequest) {
        my_xhr_remove = new XMLHttpRequest();
    }
    else {
        my_xhr_remove = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr_remove.onreadystatechange = do_remove;
    my_xhr_remove.open("GET", "/shop.ashx?remove=yes&lingua=" + lingua + "&id=" + remove_id);
    my_xhr_remove.send(null);
}

function do_remove() {
    if (my_xhr_remove.readyState == 4) {
        shop(get_channel);
    }
}

//SVUOTA CARRELLO
function conferma_svuota() {
    if (lingua == "it") {
        document.getElementById("alert_text").innerHTML = "Svuotare il carrello?";
        document.getElementById("my_button").value = "Annulla";
    }
    else {
        document.getElementById("alert_text").innerHTML = "Delete Shopping Cart?";
        document.getElementById("my_button").value = "Cancel";
    }
    document.getElementById("div_confirm").style.display = "block";
    document.getElementById("my_btn_confirm").onclick = "svuota()";
    openAlert('my_alert');
}

function svuota() {
    closeAllModalWindows();
    if (window.XMLHttpRequest) {
        my_xhr_svuota = new XMLHttpRequest();
    }
    else {
        my_xhr_svuota = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr_svuota.onreadystatechange = do_svuota;
    my_xhr_svuota.open("GET", "/shop.ashx?svuota=yes&lingua=" + lingua);
    my_xhr_svuota.send(null);
}

function do_svuota() {
    if (my_xhr_svuota.readyState == 4) {
        cassa();
    }
}


//VADO ALLA CASSA
function cassa() {
    location.href = "/" + lingua + "/shop.aspx";
}

//SELEZIONO PAGAMENTO
function pay(pay_id, country) {
    get_pay_id = pay_id;
    get_country = country;
    pay_type = document.getElementById("payment").value;
    if (pay_type != "seleziona") {
        if (window.XMLHttpRequest) {
            my_xhr_pay = new XMLHttpRequest();
        }
        else {
            my_xhr_pay = new ActiveXObject("Microsoft.XMLHTTP");
        }
        my_xhr_pay.onreadystatechange = do_pay;
        my_xhr_pay.open("GET", "/shop.ashx?pay=yes&lingua=" + lingua + "&id=" + pay_id + "&country=" + country + "&type=" + pay_type);
        my_xhr_pay.send(null);
    }
    else {
        document.getElementById("show_payment").innerHTML = "";
    }
}

function do_pay() {
    if (my_xhr_pay.readyState == 4) {
        document.getElementById("show_payment").innerHTML = my_xhr_pay.responseText;
        //creo pulsante di conferma
        document.getElementById("show_payment").innerHTML += "<INPUT TYPE='button' id='btn_pay' VALUE='Conferma Ordine' onclick='confirm_order()' style='margin:20px 0px 0px 0px; cursor:pointer;'/>";
        document.getElementById("btn_pay").focus();

    }
}

// CONFERMO ORDINE
function confirm_order() {
    if (window.XMLHttpRequest) {
        my_xhr_confirm = new XMLHttpRequest();
    }
    else {
        my_xhr_confirm = new ActiveXObject("Microsoft.XMLHTTP");
    }
    my_xhr_confirm.onreadystatechange = do_confirm;
    my_xhr_confirm.open("GET", "/shop.ashx?confirm=yes&lingua=" + lingua + "&pay_id=" + get_pay_id + "&type=" + pay_type);
    my_xhr_confirm.send(null);
}

function do_confirm() {
    if (my_xhr_confirm.readyState == 4) {
        if (my_xhr_confirm.responseText != "") {
            location.href = "page.aspx?ch=conferma&lang=" + lingua + "&mode=conferma&ordine=" + my_xhr_confirm.responseText;
        }
        else {
            document.getElementById("alert_text").innerHTML = "ERRORE!";
            document.getElementById("my_button").value = "OK";
            document.getElementById("div_confirm").style.display = "none";
            openAlert('my_alert');
        }
    }
}


//ACCESSO UTENTE GIA' REGISTRATO
function do_accesso() {
    var email = trim(document.getElementById("ctl15_email").value);
    var pass = trim(document.getElementById("ctl15_password").value);
    if (email != "" && pass != "") {
        if (window.XMLHttpRequest) {
            my_xhr_accesso = new XMLHttpRequest();
        }
        else {
            my_xhr_accesso = new ActiveXObject("Microsoft.XMLHTTP");
        }
        my_xhr_accesso.onreadystatechange = get_access;
        my_xhr_accesso.open("GET", "/shop.ashx?accesso=yes&lingua=" + lingua + "&email=" + email + "&pass=" + pass);
        my_xhr_accesso.send(null);
    }
}

function get_access() {
    if (my_xhr_accesso.readyState == 4) {
        if (my_xhr_accesso.responseText == "yes") {
            cassa();
        }
        else {
            document.getElementById("alert_text").innerHTML = "LOG Error";
            document.getElementById("my_button").value = "OK";
            document.getElementById("div_confirm").style.display = "none";
            openAlert('my_alert');
        }
    }
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function show_info() {
    var pay_info = document.getElementById("ctl15_div_info").innerHTML;
    document.getElementById("alert_text").innerHTML = pay_info;
    openInfo('my_alert');
}

//FINESTRE DI ALERT
var win = null;
var index = 1;
function openAlert(id) {
    document.getElementById("alert_container").style.width = "150px";
    document.getElementById("alert_container").style.height = "120px";
    Dialog.alert($(id).innerHTML, { className: "alphacube", width: 200, height: 250, id: "d" + index })
    index++;
}
function openInfo(id) {
    document.getElementById("alert_container").style.width = "600px";
    document.getElementById("alert_container").style.height = "340px";
    Dialog.alert($(id).innerHTML, { className: "alphacube", width: 640, height: 420, id: "d" + index })
    index++;
}
function closeAllModalWindows() {
    Windows.closeAllModalWindows();
    return true;
}