﻿    
function AjaxShowCartPreview()
{ 
    $.ajax
    ({
        type: 'post',
        dataType: 'html',
        data: 
        {
        },
        url: 'ShoppinCartPreview.aspx',
        cache: false,
        success:function (transport)
        {
            var divCartPreview_body=document.getElementById('divCartPreview_body');
            divCartPreview_body.innerHTML = transport;
                
            $('#divCartPreview_body').fadeTo("slow", 0.1);
            $('#divCartPreview_body').fadeTo("slow", 1);
            $('#divCartPreview_body').fadeTo("slow", 0.1);
            $('#divCartPreview_body').fadeTo("slow", 1);
        },
        error: function(transport)
        {
            /*alert('Došlo je do greške.');*/
        }
    });
}

function AcceptCartOrder()
{
    var name=document.getElementById('txtCartName').value;
    var surname=document.getElementById('txtCartSurname').value;
    var address=document.getElementById('txtCartAddress').value;
    var phone=document.getElementById('txtCartPhone').value;
    var email=document.getElementById('txtCartEmail').value;
    
    var payment

    for (var i=0; i<document.forms[0].Payment.length; i++)
    {
        if (document.forms[0].Payment[i].checked)
        {
            payment = document.forms[0].Payment[i].value
        }
    }
    
    if (!isEmpty(name) && !isEmpty(surname) && !isEmpty(address) && !isEmpty(phone) && !isEmpty(email))
    {
        AjaxCreateOrder(name, surname, address, phone, email, payment);
    }
    else
    {
        alert('Morate uneti potrebne podatke.');
    }  
}

function CancelCartOrder()
{
    var mpeOrder=$find('ctl00_mpeOrder');
    mpeOrder.hide();
}

function ShowCartOrder()
{
    document.getElementById('txtCartName').value='';
    document.getElementById('txtCartSurname').value='';
    document.getElementById('txtCartAddress').value='';
    document.getElementById('txtCartPhone').value='';
    document.getElementById('txtCartEmail').value='';
    $('#rbtnPouzecem').attr('checked', 'checked');
    $('#divUplatnicomBanke').hide('fast');
        
    var mpeOrder=$find('ctl00_mpeOrder');
    mpeOrder.show();
}

function AjaxCreateOrder(name, surname, address, phone, email, payment)
{
    ShowBussyModalPopup();
    
    //call service method
    url = 'CartService.asmx';
    
    strMethodName = 'CreateOrder';
    strSoapContent = '<name>' + name + '</name>' +
                    '<surname>' + surname + '</surname>' +
                    '<address>' + address + '</address>' +
                    '<phone>' + phone + '</phone>' +
                    '<email>' + email + '</email>' + 
                    '<payment>' + payment + '</payment>' ;
    
    soapAction = 'http://belcomputers.rs/' + strMethodName ;
    data = createSOAPData(strMethodName, strSoapContent);
    
    getXmlHttpObject();
    
    sendSOAPDataToWebService(url, data, soapAction);
}

//function AjaxCreateOrder(name, surname, address, phone, email)
//{ 
//    ShowBussyModalPopup();
//    
//    $.ajax
//    ({
//        type: 'post',
//        dataType: 'html',
//        data: 
//        {
//            'name' : name,
//            'surname' : surname,
//            'address' : address,
//            'phone' : phone,
//            'email' : email 
//        },
//        url: 'CartService.asmx/CreateOrder',
//        cache: false,
//        success:function (transport)
//        {
//            HideBussyModalPopup();   
//            
//            alert('Vaša porudžbina je uspešno prosleđena.');
//            //hide order window
//            var mpeOrder=$find('ctl00_mpeOrder');
//            mpeOrder.hide();
//            //clear cart
//            ClearCart('tableCart')
//        },
//        error: function(transport)
//        {
//            HideBussyModalPopup(); 
//            //alert(transport);
//            alert('Došlo je do greške');
//        }
//    });
//}

//onmouseover gvItemsRow    
function gvItemsRow_OnMouseOver(row_id)
{
    //find addtocart div
    var divAddToCart=document.getElementById('divAddToCart');

    //find row
    var row=document.getElementById(row_id); 
    
    var subcat_items_grid=document.getElementById('ctl00_ContentPlaceHolder1_subcat_items_grid_containerDiv');
    //set div's position based on row's
    var row_position=GetElementPosition(row_id);
    divAddToCart.style.posTop=row_position.top - subcat_items_grid.scrollTop;
    divAddToCart.style.posLeft=row_position.left + 10;
    divAddToCart.style.left = row_position.left+ 10 + 'px';
    divAddToCart.style.top = row_position.top - subcat_items_grid.scrollTop + 'px';
    divAddToCart.style.display='block';
    
    divAddToCart.onmouseover=function()
                            {
                                //set onclick FF&IE   
                                divAddToCart.onclick = function()
                                {
                                    AjaxAddItem(row_id.replace('item_', ''));
                                };
                                divAddToCart.style.display='block';
                            };
    divAddToCart.onmouseout=function()
                            {
                               $('#divAddToCart').hide();
                            };                        
}

function gvItemsRow_OnMouseOut()
{
    $('#divAddToCart').hide();
}

//hide cart
function AjaxHideCart()
{
    $('#divCart_container').hide("slow");
    HideBussyModalPopup();
}

//show cart
function AjaxShowCart()
{  
   ShowBussyModalPopup();
   
   if ($('#divCart_container').css('display')== 'none')
   { 
        $.ajax
        ({
            type: 'post',
            dataType: 'html',
            data: 
            {
            },
            url: 'ShoppingCart.aspx',
            cache: false,
            success:function (transport)
            {
                HideBussyModalPopup();
                
                var divCart_container=document.getElementById('divCart_container');
                divCart_container.innerHTML = transport;
                    
                $('#divCart_container').show("normal");
            },
            error: function(transport)
            {
                HideBussyModalPopup();
                /*alert('Došlo je do greške.');*/
            }
        });
    }
    else
    {
        AjaxHideCart();   
    }
}

//********************************************************   Clear cart   ***************************************
//Clear all items
function ClearCart(table_id)
{
    ShowBussyModalPopup();
    
    $.ajax
    ({
        type: 'post',
        dataType: 'text',
        data: 
        {
            'action' : 'clear'
        },
        url: 'ShoppingCart.aspx',
        cache: false,
        success:function (transport)
        {
            HideBussyModalPopup();
            
            var divCart_container=document.getElementById('divCart_container');
            divCart_container.innerHTML = transport;
            
            AjaxShowCartPreview();
        },
        error: function(transport)
        {
            HideBussyModalPopup();
            /*alert('Došlo je do greške.');*/
        }
    });
}

//********************************************************   OnDelete    ***************************************
//Remove item
function OnDeleteAction(row_id)
{    
    AjaxDeleteItem(row_id);
}

function AjaxDeleteItem(row_id)
{
    ShowBussyModalPopup();
    
    $.ajax
    ({
        type: 'post',
        dataType: 'text',
        data: 
        {
            'action' : 'remove',
            'item_id' : row_id
        },
        url: 'ShoppingCart.aspx',
        cache: false,
        success:function (transport)
        {
            HideBussyModalPopup();
            
             //set response to div
            var divCart_container=document.getElementById('divCart_container');
            divCart_container.innerHTML = transport;
            
            AjaxShowCartPreview();
        },
        error: function(transport)
        {
            HideBussyModalPopup();
            /*alert('Došlo je do greške.');*/
        }
    });
}

//********************************************************   OnEdit   ***************************************
//SetUp OnEdit action
update_mutex=0;
function OnEditAction(row_id)
{
    if (update_mutex == 0)
    {
        update_mutex+=1;   
        SetUpRowItems(row_id, 'onEdit')
    }
    else
    {
        alert('Završite sa izmenom količine prethodnog artikla.');
    }
}

//********************************************************   OnCancelEdit   ***************************************
//SetUp OnCancelUpdate action
function OnCancelAction(row_id)
{
    SetUpRowItems(row_id, 'onCancelEdit');
    update_mutex = 0;
}

//********************************************************   OnUpdate    ***************************************
//Update item
function OnUpdateAction(row_id)
{
    //find row
    var row=document.getElementById(row_id);
    
    //get new quantity
    var quantity;
    var tdQuantity = GetTdFromRow(row, 'tdQuantity', 'TD');
    var txtQuantity=GetControlFromParent(tdQuantity, 'txtQuantity', 'INPUT');
    quantity = txtQuantity.value;
    
    if (isNonnegativeInteger(quantity))
    {
        SetUpRowItems(row_id, 'onUpdate');
        AjaxUpdateItem(row_id, quantity);
    }
    else
    {
        alert('Količina mora biti ceo pozitivan broj!');
    }
    update_mutex = 0;
}

//Update item in profile
function AjaxUpdateItem(item_id, quantity)
{
    ShowBussyModalPopup();
    
    $.ajax
    ({
        type: 'post',
        dataType: 'text',
        data:
        {
            'action' : 'update',
            'item_id' : item_id,
            'quantity' : quantity 
        },
        url: 'ShoppingCart.aspx',
        cache: false,
        success:function (transport)
        {
            HideBussyModalPopup();
            
            var divCart_container=document.getElementById('divCart_container');
            divCart_container.innerHTML = transport;
            
            AjaxShowCartPreview();
        },
        error: function(transport)
        {
            HideBussyModalPopup();
            /*alert('Došlo je do greške.');*/
        }
    });
   
}

//********************************************************   OnInsert   ***************************************
//Add item to cart
function OnInsertAction(item_id, subcategory, name, quantity, price)
{
    AjaxAddItem(item_id);
}

//Add item to profile
function AjaxAddItem(item_id)
{ 
   ShowBussyModalPopup();
    
   $.ajax
    ({
        type: 'post',
        dataType: 'text',
        data: 
        {
            'action' : 'add', 
            'item_id' : item_id
        },
        url: 'ShoppingCart.aspx',
        cache: false,
        success: function (transport)
        {
            HideBussyModalPopup();
            
            //set response to div
            var divCart_container=document.getElementById('divCart_container');
            divCart_container.innerHTML = transport;
            
            AjaxShowCartPreview();
        },
        error: function(transport)
        {
            HideBussyModalPopup();
            alert('Došlo je do greške prilikom dodavanja artikla.');
        }
    });
}

//SetUp row items
function SetUpRowItems(row_id, action_type)
{
    //find row
    var row=document.getElementById(row_id);
    
    //find cells
    var tdEditUpdate = GetTdFromRow(row, 'tdEditUpdate', 'TD');
    var tdDeleteCancel = GetTdFromRow(row, 'tdDeleteCancel', 'TD');
    var tdQuantity = GetTdFromRow(row, 'tdQuantity', 'TD');
    
    //find controls in cells
    var imgEdit=GetControlFromParent(tdEditUpdate, 'imgEdit', 'IMG');
    var imgUpdate=GetControlFromParent(tdEditUpdate, 'imgUpdate', 'IMG');
    var imgDelete=GetControlFromParent(tdDeleteCancel, 'imgDelete', 'IMG');
    var imgCancel=GetControlFromParent(tdDeleteCancel, 'imgCancel', 'IMG');
    var aQuantity=GetControlFromParent(tdQuantity, 'aQuantity', 'A');
    var txtQuantity=GetControlFromParent(tdQuantity, 'txtQuantity', 'INPUT');
    
    if (action_type == 'onEdit')
    {
        //change visibility
        imgEdit.style.display='none';
        imgUpdate.style.display='';
        imgDelete.style.display='none';
        imgCancel.style.display='';
        //setup quantity
        aQuantity.style.display='none';
        txtQuantity.value = aQuantity.firstChild.nodeValue;
        txtQuantity.style.display='';
    }
    else if (action_type == 'onCancelEdit')
    {
        //change visibility
        imgEdit.style.display='';
        imgUpdate.style.display='none';
        imgDelete.style.display='';
        imgCancel.style.display='none';
        //setup quantity
        aQuantity.style.display='';
        aQuantity.firstChild.nodeValue = aQuantity.firstChild.nodeValue;
        txtQuantity.style.display='none';
    }
    else if (action_type == 'onUpdate')
    {    
        //change visibility
        imgEdit.style.display='';
        imgUpdate.style.display='none';
        imgDelete.style.display='';
        imgCancel.style.display='none';
        //setup quantity
        aQuantity.style.display='';
        aQuantity.firstChild.nodeValue = txtQuantity.value;
        txtQuantity.style.display='none';
    }
}

function ShowBussyModalPopup()
{
    var mpeBussy=$find('ctl00_mpeBussy');
    mpeBussy.show();
}

function HideBussyModalPopup()
{
    var mpeBussy=$find('ctl00_mpeBussy');
    mpeBussy.hide();
}

