/*
 *THIS IS FOR THE CART ON THE STORE PAGE
 */

//from church store --http://www.lcmuk.org/store

function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function show_cart(){
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Your browser does not support XMLHTTP!");
        return;
    }
    var url="shoppingcart.php";

    xmlhttp.onreadystatechange = stateCartChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);


}

function stateCartChanged(){
    if (xmlhttp.readyState==4)
    {
        //document.getElementById("home_content").style.display='none';
        var txt ='<h4> Welcome to Life Changing International Ministry Online Store</h4><br/> <br />' + xmlhttp.responseText +'<div class="box466_bottom"></div>';

        document.getElementById("content").innerHTML= txt;//xmlhttp.responseText;
    }

}


function DeleteProd(id)
{
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Your browser does not support XMLHTTP!");
        return;
    }
    var url= 'cscart.php?action=remove_item&pid='+id;

    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4)
        {

            var objDetails = xmlhttp.responseText;
            document.getElementById('basketInformation').innerHTML = objDetails;
            show_cart();
            del_pop_up();
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);

}

function UpdateQty(item)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Your browser does not support XMLHTTP!");
        return;
    }
    itemId = item.name;
    newQty = item.options[item.selectedIndex].text;
    var url='cscart.php?action=update_item&pid='+itemId+'&qty='+newQty;
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4)
        {
            var objDetails = xmlhttp.responseText;
            document.getElementById('basketInformation').innerHTML = objDetails;
            show_cart();
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);

}


function AddToStoreCart(prod_id)
{
    
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Your browser does not support XMLHTTP!");
        return;
    }

    //var pid;
    //pid = prod_id;
    var qtty;
    qtty = document.getElementById('qty');
    items = parseInt(qtty.options[qtty.selectedIndex].value);
    var url = 'cscart.php?action=add_item&pid=' + prod_id + '&qty=' + items;
 
    xmlhttp.onreadystatechange = cartstateChanged;
    
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function cartstateChanged()
{
    if (xmlhttp.readyState == 4 )
    {
        var objDetails = xmlhttp.responseText;       
        document.getElementById('basketInformation').innerHTML = objDetails;
        pop_up();
        
    }
}

function visToggle(objId) {
    obj = document.getElementById(objId)
    if (obj.style.display == 'block') {
        obj.style.display = 'none';
    }
    else {
        obj.style.display = 'block';
    }
}

function pop_up()
{
    var cart;
    cart = "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
    cart= cart+ "<tr><td height='306' valign='middle' align='center'><div id='productListingTop' style='height: 13px; width: 333px; background: url(images/prod_large_top.jpg);'>";
    cart= cart+ "</div><div id='productListingMid' style='height: 120px; width: 333px; background: url(images/prod_large_mid.jpg);'>";
    cart= cart+ "<table width='330' cellspacing='0' cellpadding='3' border='0' align='center'>";
    cart= cart+ "<tr><td align='center' width='203' valign='middle'><div id='productListingName' style='padding-left: 3px; padding-right: 3px;";
    cart= cart+ " line-height: 16px;'><span class='bodyText' style='font-weight: bold;'>The item has been added to your basket.</span><br />";
    cart= cart+ " <br /></div></td></tr><tr><td colspan='2' align='center'>";
    cart= cart+ "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td width='50%' valign='top'>";
    cart= cart+ "&nbsp;</td><td width='50%' valign='top'>&nbsp;</td></tr> </table><table width='100%' cellpadding='0' cellspacing='0' border='0'>";
    cart= cart+ " <tr><td><a href='index.php'><img src='images/continue_shopping.jpg' border='0' alt='Continue Shopping' height='70px'  width='150px' title='Continue Shopping' /></a>&nbsp;&nbsp;&nbsp;&nbsp;<a";
    cart= cart+ " href='index.php'></a><a href='index.php?p=shoppingcart'><img src='images/go_to_checkout.jpg' border='0' width='150px' alt='Goto Checkout'";
    cart= cart+ " title='Goto Checkout' /> </a> </td> </tr> </table> </td></tr></table></div> <div id='productListingBot' style='height: 33px; width: 333px; background: url(images/prod_large_bot.jpg);'>";
    cart= cart+ " </div></td> </tr></table>";

    document.getElementById('contineOrCheckout').innerHTML = cart;
    visToggle('contineOrCheckout');

}


function del_pop_up()
{
    var cart;
    cart = "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
    cart= cart+ "<tr><td height='306' valign='middle' align='center'><div id='productListingTop' style='height: 13px; width: 333px; background: url(images/prod_large_top.jpg);'>";
    cart= cart+ "</div><div id='productListingMid' style='height: 120px; width: 333px; background: url(images/prod_large_mid.jpg);'>";
    cart= cart+ "<table width='330' cellspacing='0' cellpadding='3' border='0' align='center'>";
    cart= cart+ "<tr><td align='center' width='203' valign='middle'><div id='productListingName' style='padding-left: 3px; padding-right: 3px;";
    cart= cart+ " line-height: 16px;'><span class='bodyText' style='font-weight: bold;'>The item has been deleted from your basket.</span><br />";
    cart= cart+ " <br /></div></td></tr><tr><td colspan='2' align='center'>";
    cart= cart+ "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td width='50%' valign='top'>";
    cart= cart+ "&nbsp;</td><td width='50%' valign='top'>&nbsp;</td></tr> </table><table width='100%' cellpadding='0' cellspacing='0' border='0'>";
    cart= cart+ " <tr><td colspan='2' align='center'>";
    cart= cart+ " <a href='javascript:closepop();'><img src='images/continue.gif' border='0' width='150px' alt='Close'";
    cart= cart+ " title='Close' /> </a> </td> </tr> </table> </td></tr></table></div> <div id='productListingBot' style='height: 33px; width: 333px; background: url(images/prod_large_bot.jpg);'>";
    cart= cart+ " </div></td> </tr></table>";

    document.getElementById('contineOrCheckout').innerHTML = cart;
    visToggle('contineOrCheckout');

}
function closepop(){
    visToggle('contineOrCheckout');

}


