﻿// JScript File


/////////////////////////// CART FUNCTIONS /////////////////////////////////////////

//Function to return Omniture string of products
function GetOmnitureProducts()
{
   
    return Cart.LoadOmnitureProducts().value; 
}




function LoadCart()
{
    
    if(document.getElementById("divCart")!=null) 
    {  
        document.getElementById("divCart").innerHTML="<img alt='loading' class='loader' src='/img/loader.gif'></img>";
    }
    
   
    Cart.LoadCart(window.location.search,Callback_LoadCart);
}

function Callback_LoadCart(response)
{
  if(document.getElementById("divCart") != null)
    {
    document.getElementById("divCart").innerHTML=response.value;
    //register omniture view
        s.events="scView";
        s.products= GetOmnitureProducts(); 
	    s.tl();
    }
    else
    {
    alert('no items');
    }
 // if(response.value!="You have no items in your Cart.  Sign in  to retrieve your saved basket.")
    CalculateGrandTotal();
}
var Gpid;
function RemoveItem(pId)
{
    Gpid=pId;
    // ASK FOR CONFIRM BOX LATER
   tbl=document.getElementById("tblCart");
    for(i=0;i<tbl.rows.length;i++)
    {
        if(tbl.rows[i].cells.length>=6)
        {
           tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
            if(pId==tId)
            {
                if(navigator.appName != "Microsoft Internet Explorer") // CHECKING IF BROWSER IS NOT IE
                {
                    document.getElementById("tblCart").rows[i].cells[5].innerHTML=document.getElementById("tblCart").rows[i].cells[5].innerHTML.replace("Remove Item","<font style='color:darkred'>Removing...</font>");
                }
                else
                    document.getElementById("tblCart").rows[i].cells[5].innerHTML=document.getElementById("tblCart").rows[i].cells[5].innerText.replace("Remove Item","<font style='color:darkred'>Removing...</font>");
            }
        }
    }
  Cart.RemoveItem(""+pId,Callback_RemoveItem);
}

function Callback_RemoveItem(response)
{
    if(response.value==0)
    {
        document.getElementById("divCart").innerHTML="<div class='no-items'>You have no items in your basket.</div><a href='home.aspx' title='continue shopping'><img src='/img/continue_shopping.gif' class='continue-shopping' border='0' alt='continue shopping' /></a>";
        return;
    }
    tbl=document.getElementById("tblCart");
    for(i=0;i<tbl.rows.length;i++)
    {
        if(tbl.rows[i].cells.length>=4)
        {
            tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
            if(Gpid==tId)
            {
                colHeight=tbl.rows[i].cells[0].style.height.substring(0,2);
               // tbl.rows[i].height=1;
                tbl.deleteRow(i);
                tbl.deleteRow(i);
            }
        }
    }
   CalculateGrandTotal();
}



function IncrementQty(pId) {
    var qty = $("#txt_" + pId).val();
    if (qty < 100) {
        qty++;
        $("#txt_" + pId).val(qty);


        // CODE FOR UPDATING CART DETAILS INTO SESSION ## START ##
        Cart.UpdateCart(pId, $("#txt_" + pId).val(), Callback_UpdateCart);

        unitPrice = $("#Price_" + pId).html().substring(1);
        $("#Subtot_" + pId).html("£ " + Round2Decimal((unitPrice * qty)));
        
    }
    CalculateGrandTotal();
}
function Callback_UpdateCart(response) {

}


function DecrementQty(pId) {
    var qty = $("#txt_" + pId).val();
    if (qty > 1) {
        qty--;
        $("#txt_" + pId).val(qty);

        // CODE FOR UPDATING CART DETAILS INTO SESSION ## START ##
        Cart.UpdateCart(pId, $("#txt_" + pId).val(), Callback_UpdateCart);
        // CODE FOR UPDATING CART DETAILS INTO SESSION ## END ##

        unitPrice = $("#Price_" + pId).html().substring(1);
        $("#Subtot_" + pId).html("£ " + Round2Decimal((unitPrice * qty)));
        
    }
    CalculateGrandTotal();
}

function CalculateGrandTotal() {
    
    var grandTotal = 0;
    $(".subTotal").each(function() {
        grandTotal += parseFloat($(this).html().substring(1));
    });
    $("#product-price").html("£" + Round2Decimal(grandTotal));
    if (grandTotal == 0.00) {
        $("#hypCheckout").hide();
    }
    else {
        $("#hypCheckout").show();
    }   
}



function checkRegistered()
{
    document.getElementById("hypCheckout").style.visibility='hidden';
    response=Cart.CheckRegisteredUser();
    if(response.value==false)
    {
        location.href='Login.aspx?httpurl=ConfirmOrder';
        document.getElementById("hypCheckout").style.visibility='visible';
    }
    else
    {
        document.getElementById("hypCheckout").style.visibility='hidden';
        location.href='ConfirmOrder.aspx';
    }
}

function Round2Decimal(amount) 
{
  var value = "";
  try
  {
    value = (Math.round(amount * 100))/100 + "";
  }
  catch(e)
  {
  }
  dot = value.indexOf(".",0);
  if (dot == -1) 
  {
  	value = value + ".00";
  }
  else if (dot == value.length - 2) 
  {
  	value = value + "0";
  }
  return value;
}

//function DecrementQty(pId)
//{
//    var txtQty=document.getElementById("txt_" + pId);
//    qty=txtQty.value;
//    if(qty>1)
//    {
//        qty--;
//        txtQty.value=qty;
//        
//        // CODE FOR UPDATING CART DETAILS INTO SESSION ## START ##
//        
//        tbl=document.getElementById("tblCart");
//        str="";
//        if(navigator.appName != "Microsoft Internet Explorer") // CHECKING IF BROWSER IS NOT IE
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                    tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
//                    str+= tId+ "~" + document.getElementById("txt_" + tId).value + ",";
//                }
//            }
//            Cart.UpdateCart(pId, document.getElementById("txt_" + pId).value);
//            // CODE FOR UPDATING CART DETAILS INTO SESSION ## END ##
//            
//            unitPrice=document.getElementById("Price_" + pId).innerHTML.substring(1);
//             document.getElementById("Subtot_" + pId).innerHTML="£ " + Round2Decimal((unitPrice * qty));

//        }
//        if(navigator.appName == "Microsoft Internet Explorer")
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                    tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
//                    str+= tId+ "~" + document.getElementById("txt_" + tId).value + ",";
//                }
//            }
//            Cart.UpdateCart(pId, document.getElementById("txt_" + pId).value);
//            // CODE FOR UPDATING CART DETAILS INTO SESSION ## END ##
//            
//            unitPrice=document.getElementById("Price_" + pId).innerText.substring(1);
//            document.getElementById("Subtot_" + pId).innerText="£ " + Round2Decimal((unitPrice * qty));
//        }
//    }
//    CalculateGrandTotal();
//}

//function CalculateGrandTotal()
//{
//    try
//    {
//        tbl=document.getElementById("tblCart");
//        str="";
//        var grandTotal=0;
//        if(navigator.appName == "Microsoft Internet Explorer")
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                    grandTotal+=parseFloat(tbl.rows[i].cells[3].innerText.substring(1));
//                }
//            }
//            grandTotal=Round2Decimal(grandTotal);
//            document.getElementById("product-price").innerText="£" + grandTotal;
//            if(grandTotal == 0.00)
//            {
//                document.getElementById("hypCheckout").style.visibility='hidden';
//            }
//            else
//            {
//                 document.getElementById("hypCheckout").style.visibility='visible';
//            }
//        }
//        
//        if(navigator.appName != "Microsoft Internet Explorer") // CHECKING IF BROWSER IS NOT IE
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                    //alert(tbl.rows[i].cells[3].innerText.substring(1));
//                    grandTotal+=parseFloat(tbl.rows[i].cells[3].innerHTML.substring(1));
//                }
//            }
//            
//            grandTotal=Round2Decimal(grandTotal);
//            document.getElementById("product-price").innerHTML="£" + grandTotal;
//            if(grandTotal == 0.00)
//            {
//                document.getElementById("hypCheckout").style.visibility='hidden';
//            }
//            else
//            {
//                 document.getElementById("hypCheckout").style.visibility='visible';
//            }
//        }
//    }
//    catch(e)
//    {
//        //alert(e.message);
//        if(document.getElementById("product-price") != null)
//         document.getElementById("product-price").innerHTML+="£00.00"
//    }
//}

//function IncrementQty(pId)
//{
//    var txtQty=document.getElementById("txt_" + pId);
//    qty=txtQty.value;
//    if(qty<100)
//    {
//        qty++;
//        txtQty.value=qty;
//       
//        
//// CODE FOR UPDATING CART DETAILS INTO SESSION ## START ##

//        tbl=document.getElementById("tblCart");
//        str="";
//        if(navigator.appName != "Microsoft Internet Explorer") // CHECKING IF BROWSER IS NOT IE
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                    tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
//                    str+= tId+ "~" + document.getElementById("txt_" + tId).value + ",";
//                }
//            }
//            Cart.UpdateCart(pId, document.getElementById("txt_" + pId).value);
//            
//           unitPrice=document.getElementById("Price_" + pId).innerHTML.substring(1);
//             document.getElementById("Subtot_" + pId).innerHTML="£ " + Round2Decimal((unitPrice * qty));

//        }
//        //If browser is IE
//        if(navigator.appName == "Microsoft Internet Explorer")
//        {
//            for(i=0;i<tbl.rows.length;i++)
//            {
//                if(tbl.rows[i].cells.length>=6)
//                {
//                tId=tbl.rows[i].cells[0].id.substring(tbl.rows[i].cells[0].id.indexOf("_")+1).trim();
//                str+= tId+ "~" + document.getElementById("txt_" + tId).value + ",";
//                }
//                //alert(str);
//            }
//            Cart.UpdateCart(pId, document.getElementById("txt_" + pId).value);
//            
//             // CODE FOR UPDATING CART DETAILS INTO SESSION ## END ##
//            
//            unitPrice=document.getElementById("Price_" + pId).innerText.substring(1);
//            document.getElementById("Subtot_" + pId).innerText="£ " + Round2Decimal((unitPrice * qty));
//        }
//    }
//    CalculateGrandTotal();
//}
