﻿function addItemToCart(productID)
{
    var getQty = document.getElementById('brandOverview_tbxQty'+productID).value;
    //try{document.getElementById('quickcart').style.backgroundColor = "#abcfed";}
    //catch(Error){}
    
    var getColor = '';
    try{getColor = document.getElementById('brandOverview_ddlColourOptions'+productID).value;}
    catch(Error){}
    
    var xmlstring = "";
    xmlstring += "<addShoppingItem>";
    xmlstring += "<productID>"+productID+"</productID>";
    xmlstring += "<productQty>"+getQty+"</productQty>";
    xmlstring += "<productColor>"+getColor+"</productColor>";
    xmlstring += "</addShoppingItem>";
    ret = Shopping.AddItemToCart(xmlstring, OnAddItemComplete, OnWebServiceTimeOut, OnWebServiceError);
}


function OnAddItemComplete(arg)
{
    var msg = arg.toString().substr(7);
    //alert(msg);
    // here we will want to update the update panel and some kind of animation to show it updates (progress spinner)
   //document.getElementById('hfTriggerCartUpdate').value = (new Date()).getTime();
  //  __doPostBack('hfTriggerCartUpdate','');
  window.location="checkout.htm";
}

function OnWebServiceComplete(arg) 
{
    alert("Generic OnWebServiceComplete");
}

function OnWebServiceTimeOut(arg) 
{
    alert("TimeOut encountered : " + arg);
}

function OnWebServiceError(arg) 
{
    alert("Error encountered : " + arg );
}

function clearCart()
{
    ret = Shopping.ClearCart("<clearcart></clearcart>", OnClearCartComplete, OnWebServiceTimeOut, OnWebServiceError);
}

function OnClearCartComplete()
{
   document.getElementById('hfTriggerCartUpdate').value = (new Date()).getTime();
    __doPostBack('hfTriggerCartUpdate','');
}

function removeFromCart(prodIx)
{
    ret = Shopping.RemoveFromCart("<removeFromCart>"+prodIx+"</removeFromCart>", OnRemoveFromCartComplete, OnWebServiceTimeOut, OnWebServiceError);
}

function OnRemoveFromCartComplete(arg)
{
    //   document.getElementById('checkout_hfTriggerCheckoutUpdate').value = (new Date()).getTime();
    // __doPostBack('checkout_hfTriggerCheckoutUpdate','');
  window.location="checkout.htm";

    //    alert(arg)
}


function updateCart()
{

    // update the checkout for qtys...
    var checkoutTable = document.getElementById('checkout_tblCart');
    var tableRows = checkoutTable.rows.length;
    var tableCols = checkoutTable.rows[0].cells.length;
    //alert('there are '+ tableRows + ' table rows and ' +  tableCols + ' table columns');
    var xmlstring = "";
    xmlstring += "<updateItemQty>";

    for(y=1;y<tableRows-5;y++)
    {
            try
            {
                // tbxAttendData11
                var cellID = checkoutTable.rows[y].cells[3].firstChild.id;
                var prodIx = cellID.substr(5);
                var prodName = checkoutTable.rows[y].cells[1].firstChild.nodeValue;
                // then get the id of this to get the prodID
                var prodQty = document.getElementById(cellID).value;
                 
                xmlstring += "<item><productIx>"+prodIx+"</productIx><productQty>" + prodQty + "</productQty><productName>"+prodName+"</productName><sendAsGift>" + document.getElementById('checkout_tbxSendAsGift').checked+ "</sendAsGift></item>";
            }
            catch(err)
            {
            alert('updateCheckout - we had a little problem..' + err);
            }
    }
    xmlstring += "<postage>" + $("#checkout_ddlDelivery").val() + "</postage>";
     
    xmlstring += "</updateItemQty>";
    //alert(xmlstring);
    ret = Shopping.UpdateCart(xmlstring, OnUpdateCartComplete, OnWebServiceTimeOut, OnWebServiceError);
}


function OnUpdateCartComplete(arg)
{
    var msg = arg.toString().substr(7);
    //alert(msg);
    // here we will want to update the update panel and some kind of animation to show it updates (progress spinner)
   
     //document.getElementById('hfTriggerCartUpdate').value = (new Date()).getTime();
     // __doPostBack('hfTriggerCartUpdate','');

     //  document.getElementById('hfTriggerMainPanelUpdate').value = (new Date()).getTime();
     // __doPostBack('hfTriggerMainPanelUpdate','');
  
  window.location="checkout.htm";

}


function addDeliveryDetails()
{
//       Profile.Name = tbxCustomerName.Text;
//       Profile.Email = tbxCustomerEmail.Text;
//       Profile.BillingAddress = tbxBillingAddress.Text;
//       Profile.BillingPostCode = tbxBillingPostCode.Text;
//       Profile.DeliveryAddress1 = tbxDeliveryAddress.Text;
//       Profile.DeliveryPostCode = tbxDeliveryPostCode.Text;
//       Profile.Telephone = tbxContactNumber.Text;
//       Response.Redirect("placeorder.aspx");
//

    var name = document.getElementById('customerDetails_tbxCustomerName').value;
    var email = document.getElementById('customerDetails_tbxCustomerEmail').value;
    var billingAddress = document.getElementById('customerDetails_tbxBillingAddress').value;
    var billingPostCode = document.getElementById('customerDetails_tbxBillingPostCode').value;
    var deliverySame = document.getElementById('customerDetails_cbxDeliverySame').checked;
    var deliveryAddress = document.getElementById('customerDetails_tbxDeliveryAddress').value;
    var deliveryPostCode = document.getElementById('customerDetails_tbxDeliveryPostCode').value;
    var telephone = document.getElementById('customerDetails_tbxContactNumber').value;
    var billingCountry = document.getElementById('customerDetails_ddlBillingCountry').value;
    var deliveryCountry = document.getElementById('customerDetails_ddlDeliveryCountry').value;
    
    var xmlstring = "";
    xmlstring += "<addDeliveryDetails>";
    xmlstring += "<name>"+name+"</name>";
    xmlstring += "<email>"+email+"</email>";
    xmlstring += "<billingAddress>"+billingAddress+"</billingAddress>";
    xmlstring += "<billingPostCode>"+billingPostCode+"</billingPostCode>";
    xmlstring += "<deliverySame>"+deliverySame+"</deliverySame>";
    xmlstring += "<deliveryAddress>"+deliveryAddress+"</deliveryAddress>";
    xmlstring += "<deliveryPostCode>"+deliveryPostCode+"</deliveryPostCode>";
    xmlstring += "<billingCountry>"+billingCountry+"</billingCountry>"; 
    xmlstring += "<deliveryCountry>"+deliveryCountry+"</deliveryCountry>";
    xmlstring += "<telephone>"+telephone+"</telephone>";
    xmlstring += "</addDeliveryDetails>";
    ret = Shopping.AddDeliveryDetails(xmlstring, OnAddDeliveryDetailsComplete, OnWebServiceTimeOut, OnWebServiceError);
}

function OnAddDeliveryDetailsComplete(arg)
{

    var msg = arg.toString().substr(7);
    
    
    if(arg.substr(0,5)=='ALERT')
    {
        alert(msg);
    }
    
       if(arg.substr(0,4)=='TRUE')
    {
          window.location="placeorder.aspx";
    }
 
 


}