﻿/// <reference name="MicrosoftAjax.js"/>

var shoppingCartWebServicePath = "/ShoppingCart/WebServices/ShoppingCartService.asmx";

////////////////////////////////////////////////////////////////

$(document).ready(function () {

    setTimeout('SelectShoppingCartTotalPrice()', 1000);
    setTimeout('SelectShoppingCartTotalCount()', 1000);

    //DeliveryAddress
    $('#DefaultContent_ctl00_ddlDeliveryTypes').change(function () {
        if ($(this).val() == 7) {
            $('#trDeliveryAddress').show();
        }
        else {
            $('#trDeliveryAddress').hide();
        }
    }).trigger('change');
});

function AddArticleToShoppingCart() {
    var articleId = $('input[id*=txtOrderQuantity]').attr('articleId');
    var quantity = $('input[id*=txtOrderQuantity]').attr('value');

    var re = new RegExp(/^[1-9]+[0-9]*$/);
    if (quantity != null && quantity.length != 0 && quantity.match(re)) {

        Sys.Net.WebServiceProxy.invoke(shoppingCartWebServicePath,
        "AddItem", false,
        { "articleId": articleId, "quantity": quantity },
        OnAddArticleToShoppingCartSucceeded, OnAddArticleToShoppingCartFailed, "User Context", 60000);

        $find('mpeOrder').hide();
    } else {
        alert("Unesite ispravnu vrednost.");
    }
}

function OnAddArticleToShoppingCartSucceeded(result, eventArgs) {
    SelectShoppingCartTotalPrice();
    SelectShoppingCartTotalCount();
    alert('Artikal je dodat u korpu.');
}

function OnAddArticleToShoppingCartFailed(error) {
    alert(error._message);

    if (error._message == "SignOut")
        window.location = '/SignOut.aspx';
}

/////////////////////////////////////////////////////////////

function SelectShoppingCartTotalPrice() {
    Sys.Net.WebServiceProxy.invoke(shoppingCartWebServicePath,
        "SelectShoppingCartTotalPrice", false,
        {},
        OnSelectShoppingCartTotalPriceSucceeded, OnSelectShoppingCartTotalPriceFailed, "User Context", 60000);
}

function OnSelectShoppingCartTotalPriceSucceeded(result, eventArgs) {
    $('#lShoppingCartTotalPriceHeader').html(result);
}

function OnSelectShoppingCartTotalPriceFailed(error) {
    //alert(error._message);
}

/////////////////////////////////////////////////////////////////

function SelectShoppingCartTotalCount() {
    Sys.Net.WebServiceProxy.invoke(shoppingCartWebServicePath,
        "SelectShoppingCartTotalCount", false,
        {},
        OnSelectShoppingCartTotalCountSucceeded, OnSelectShoppingCartTotalCountFailed, "User Context", 60000);
}

function OnSelectShoppingCartTotalCountSucceeded(result, eventArgs) {
    $('#lShoppingCartTotalCountHeader').html(result);
    $('#lShoppingCartTotalCountLogin').html(result);
}

function OnSelectShoppingCartTotalCountFailed(error) {
    //alert(error._message);
}

/////////////////////////////////////////////////////////////

function SaveOrderDetails() {
    var shippingAddress = $('#txtDeliveryAddress')[0].value;
    var deliveryType = $('#DefaultContent_ctl00_ddlDeliveryTypes')[0].options[$('#DefaultContent_ctl00_ddlDeliveryTypes')[0].selectedIndex].value;
    if (deliveryType == -1) {
        alert('Odaberite način isporuke.');
        return;
    }
    else if (deliveryType == 7 && shippingAddress == '') {
        alert('Unesite adresu za dostavu.');
        return;
    }

    var paymentType = $('#selectPaymantType')[0].options[$('#selectPaymantType')[0].selectedIndex].value;
    if (paymentType == -1) {
        alert('Odaberite način plaćanja.');
        return;
    }
    
    var warehouse = 1;  //$('#DefaultContent_ctl00_ddlWarehouses')[0].options[$('#DefaultContent_ctl00_ddlWarehouses')[0].selectedIndex].value;
    
    var comment = $('#txtComment')[0].value;
    
    Sys.Net.WebServiceProxy.invoke(shoppingCartWebServicePath,
        "SaveOrderDetails", false,
        { "deliveryType": deliveryType, "paymentType": paymentType, "warehouse": warehouse, "comment": comment, "shippingAddress": shippingAddress },
        OnSaveOrderDetailsSucceeded, OnSaveOrderDetailsFailed, "User Context", 60000);
}

function OnSaveOrderDetailsSucceeded(result, eventArgs) {
    if (result == 0) {
        alert('Došlo je do greške prilikom snimanja detalja Vaše porudžbine. Izvinjavamo se zbog neprijatnosti. Molimo Vas da kontaktirate komercijalistu.');
    }
    else {
//        $('#selectPaymantType')[0].selectedIndex = 0;
//        $('#DefaultContent_ctl00_ddlDeliveryTypes')[0].selectedIndex = 0;
//        $('#txtComment')[0].value = "";

        SelectShoppingCartTotalPrice();
        SelectShoppingCartTotalCount();

        window.location = 'ShoppingCart/Preview.aspx';
    }
}

function OnSaveOrderDetailsFailed(error) {
    //alert(error._message);
    alert('Došlo je do greške prilikom snimanja detalja Vaše porudžbine. Izvinjavamo se zbog neprijatnosti. Molimo Vas da kontaktirate komercijalistu.');
    if (error._message == "SignOut")
        window.location = '/SignOut.aspx';
}

//////////////////////////////////////////////////////////////////////

function CheckOut() {
    var chkboxAcceptCheckout = $('input[id*=chkboxAcceptCheckout]');
    if (!chkboxAcceptCheckout.is(':checked')) {
        alert('Obavezno je prihvatanje saglasnosti sa opstim pravilima kupovine za nastavak.');
        return false;
    }

    Sys.Net.WebServiceProxy.invoke(shoppingCartWebServicePath,
        "CheckOut", false,
        { },
        OnCheckOutSucceeded, OnCheckOutFailed, "User Context", 60000);
}

function OnCheckOutSucceeded(result, eventArgs) {
    if (result == -1) {
        alert('Korpa je prazna!');
    }
    else if (result == 0) {
        alert('Došlo je do greške prilikom prosleđivanja Vaše porudžbine. Izvinjavamo se zbog neprijatnosti. Molimo Vas da kontaktirate komercijalistu.');
    }
    else if (result.indexOf("http:") >= 0) {
        SelectShoppingCartTotalPrice();
        SelectShoppingCartTotalCount();

        alert('Vaša porudžbina je uspešno prosleđena. Preusmeravamo Vas na stranicu za E-Placanje.');
        window.location = result;
    }
    else {
        SelectShoppingCartTotalPrice();
        SelectShoppingCartTotalCount();

        alert('Vaša porudžbina je uspešno prosleđena pod rednim bojem: ' + result);
        window.location = '/default.aspx';
    }
}

function OnCheckOutFailed(error) {
    //alert(error._message);
    alert('Došlo je do greške prilikom prosleđivanja Vaše porudžbine. Izvinjavamo se zbog neprijatnosti. Molimo Vas da kontaktirate komercijalistu.');
    if (error._message == "SignOut")
        window.location = '/SignOut.aspx';
}

////////////////////////////////////////////////////////////////

function ShowPanelOrder(articleId) {
    $('input[id*=txtOrderQuantity]').attr('articleId', articleId);
    $('input[id*=txtOrderQuantity]').attr('value', '1');
    $find('mpeOrder').show();
    $('input[id*=txtOrderQuantity]').focus();
}

////////////////////////////////////////////////////////////////

//OrderItems
function SelectOrderItemsHTML(orderId, divOrderItemsId) {

    //hide div
    if (document.getElementById(divOrderItemsId).style.display == 'block') {
        document.getElementById(divOrderItemsId).style.display = 'none';
        return;
    }

    Sys.Net.WebServiceProxy.invoke(
        shoppingCartWebServicePath,
        "SelectOrderItemsHTML",
        false,
        { "orderId": orderId },
        function (result, eventArgs) {
            document.getElementById(divOrderItemsId).style.display = 'block';
            document.getElementById(divOrderItemsId).innerHTML = result;
        },
        OnSelectOrderItemsHTMLFailed, "User Context", 60000);
}

function OnSelectOrderItemsHTMLFailed(error) {
    alert(error._message);

    if (error._message == "SignOut")
        window.location = '/SignOut.aspx';
}

///////////////////////////////////////////////////////////////
