﻿



function addToOrder() 
{
    document.getElementById('btnAddToOrder').disabled = true;
    document.getElementById('btnAddToOrderAndCheckout').disabled = true;
     
    // this function takes  look at the order form for a product and builds
    // a string to format into XML to go off to the webservice to build the order
    var xmlString = '<product>';
    var tblOrderForm = document.getElementById('tblOrderForm');
    var tableRows = tblOrderForm.rows.length;
    var tableCols = tblOrderForm.rows[0].cells.length;
    var anyZeroBalances = false;
    for (var i = 1; i < tblOrderForm.rows.length; i++) 
    {
        // this look has one iteration per orderLineItem
        var currTblRow = tblOrderForm.rows[i];
        var currentRcn = (currTblRow.id).substring(7);
        var theQtyBox = currTblRow.getElementsByTagName("input")[0];
        var stkCol = (theQtyBox.id).substring((currTblRow.id).length);

            xmlString += '<orderLine>';
            xmlString += '<rcn>' + currentRcn + '</rcn>';
            xmlString += '<stkCol>' + stkCol + '</stkCol>';
            xmlString += '<qty>' + theQtyBox.value + '</qty>';
            xmlString += '</orderLine>';
            // a row has a been requested, need to check stockLevel to make sure its not out of stock.
            if ((currTblRow.className == "stockOut" || currTblRow.className == "stockOut")&&(parseInt(theQtyBox.value) > 0))
             {
                anyZeroBalances = true;
            }
            if ((currTblRow.className == "stockLow" || currTblRow.className == "stockLow") && (parseInt(theQtyBox.value) > 0)) {
                anyZeroBalances = true;
            }
    }
     xmlString += '</product>';

     if (anyZeroBalances) {
         var answer = confirm("You have requested some item(s) which are either out of stock or limited stock. \n\nClick OK to accept balances on the order if we can't supply all your items, or cancel to change quantities. \n\n\n*Note : You can always add order notes to let us know more information when you confirm your order");
         if (answer) {
             ret = orderService.addItemsToWebOrder(xmlString, OnAddItemsToWebOrderComplete, OnWebServiceTimeOut, OnWebServiceError);
         }
         else {
             document.getElementById('btnAddToOrder').disabled = false;
             document.getElementById('btnAddToOrderAndCheckout').disabled = false;
         }
     }
     else {
         ret = orderService.addItemsToWebOrder(xmlString, OnAddItemsToWebOrderComplete, OnWebServiceTimeOut, OnWebServiceError);
     }
 }


function OnAddItemsToWebOrderComplete(args) {
    //alert("Your order has been updated successfully. \n\n You will now be directed to your current cart.");
    document.getElementById('btnAddToOrder').disabled = false;
    document.getElementById('btnAddToOrderAndCheckout').disabled = false;
    DialogOrder.Close();
    updateCart();
    //window.location='/order/step1/';
}

//############################################################################
//############################################################################
//############################################################################


function addToOrderAndCheckout() 
{
    document.getElementById('btnAddToOrder').disabled = true;
    document.getElementById('btnAddToOrderAndCheckout').disabled = true;
    // this function takes  look at the order form for a product and builds
    // a string to format into XML to go off to the webservice to build the order
    var xmlString = '<product>';
    var tblOrderForm = document.getElementById('tblOrderForm');
    var tableRows = tblOrderForm.rows.length;
    var tableCols = tblOrderForm.rows[0].cells.length;
    var anyZeroBalances = false;
    for (var i = 1; i < tblOrderForm.rows.length; i++) 
    {
        // this look has one iteration per orderLineItem
        var currTblRow = tblOrderForm.rows[i];
        var currentRcn = (currTblRow.id).substring(7);
        var theQtyBox = currTblRow.getElementsByTagName("input")[0];
        var stkCol = (theQtyBox.id).substring((currTblRow.id).length);

            xmlString += '<orderLine>';
            xmlString += '<rcn>' + currentRcn + '</rcn>';
            xmlString += '<stkCol>' + stkCol + '</stkCol>';
            xmlString += '<qty>' + theQtyBox.value + '</qty>';
            xmlString += '</orderLine>';
            // a row has a been requested, need to check stockLevel to make sure its not out of stock.
            if ((currTblRow.className == "stockOut" || currTblRow.className == "stockOut")&&(parseInt(theQtyBox.value) > 0))
             {
                anyZeroBalances = true;
            }
            if ((currTblRow.className == "stockLow" || currTblRow.className == "stockLow") && (parseInt(theQtyBox.value) > 0)) {
                anyZeroBalances = true;
            }
    }
     xmlString += '</product>';

     if (anyZeroBalances) {
         var answer = confirm("You have requested some item(s) which are either out of stock or limited stock. \n\nClick OK to accept balances on the order if we can't supply all your items, or cancel to change quantities. \n\n\n*Note : You can always add order notes to let us know more information when you confirm your order");
         if (answer) {
             ret = orderService.addItemsToWebOrder(xmlString, addToOrderAndCheckoutComplete, OnWebServiceTimeOut, OnWebServiceError);
         }
         else {
             document.getElementById('btnAddToOrder').disabled = false;
             document.getElementById('btnAddToOrderAndCheckout').disabled = false;
         }
     }
     else {
         ret = orderService.addItemsToWebOrder(xmlString, addToOrderAndCheckoutComplete, OnWebServiceTimeOut, OnWebServiceError);
     }
 }


 function addToOrderAndCheckoutComplete(args) {
    //alert("Your order has been updated successfully. \n\n You will now be directed to your current cart.");
    DialogOrder.Close();
    window.location='/order/step1/';
}




function OnWebServiceComplete(arg) {
    alert("Generic OnWebServiceComplete");
}

function OnWebServiceTimeOut(arg) {
    alert("TimeOut encountered : " + arg);
}

function OnWebServiceError(arg) {
    alert("Error encountered : " + arg);
}




function cbxAgreeClick() {
   //  this allows the order to be placed if the cbx is checked

    var allowProgress = document.getElementById('cbxAgree').checked;
    if (allowProgress)
    { document.getElementById('btnConvertToOrder').disabled = false; }
   else { document.getElementById('btnConvertToOrder').disabled = true; }

}




function gridCallbackComplete(sender) 
{
    ret = orderService.getOrderSubTotal("", OnGetOrderSubTotalComplete, OnWebServiceTimeOut, OnWebServiceError);
}


function OnGetOrderSubTotalComplete(arg) 
{
    document.getElementById('spanSubTotal').innerHTML = arg;
}
