/**
 * Cart
 * 
 * @package    wsCat Jx
 * @version    1.0
 * @since      20.06.2007
 * @copyright  2004-2007 5Dev
 * @link       http://5dev.com
 */
function Orders_Cart(div_obj, form_obj) 
{ 
    this.__construct(div_obj, form_obj);
}
(
    function() 
    {
        Orders_Cart.prototype = {

        mFormObj: null,
        mDivObj:  null,
        mInfo:    new Array(),

        __construct: function(div_obj, form_obj)
        {
            this.mDivObj  = div_obj;
            this.mFormObj = form_obj;
        },

        GenerateList: function()
        {
            var cnt = this.mInfo['it'].length;
            if (0 < cnt)
            {
                var str  = '';
                var str2 = '';
                for (var i = 0; i < cnt; i++)
                {
                    str  += '<tr>';
                    str  += '<td class="good_name"><a href="' + this.mInfo['it'][i]['uprefix'] + '.html">' + this.mInfo['it'][i]['name'] + '</a></td>';
                    str  += '<td class="good_price">$&nbsp;' + this.mInfo['it'][i]['price'] + '</td>';
                    str  += '<td><a href="javascript:void(0);" onClick="cartObj.DeleteItem('+this.mInfo['it'][i]['i_id']+');"><img src="/img/del.gif" width="7" height="7" alt="Remove item" /></a></td>';
                    str  += '</tr>';

                    str2 += '<input type="hidden" name="c_prod_' + (i+1) + '" value="' + this.mInfo['it'][i]['i_id'] + '" />';
                    str2 += '<input type="hidden" name="c_name_' + (i+1) + '" value="' + this.mInfo['it'][i]['name'] + '" />';
                    str2 += '<input type="hidden" name="c_description_' + (i+1) + '" value="' + this.mInfo['it'][i]['description'] + '" />';
                    str2 += '<input type="hidden" name="c_price_' + (i+1) + '" value="' + this.mInfo['it'][i]['price'] + '" />';
                    str2 += '<input type="hidden" name="c_tangible_' + (i+1) + '" value="N" />';
                }
                
                var str = str2 + '<table cellSpacing="0">' + str;

                this.mFormObj.total.value = this.mInfo['total'];

                str += '<tr>';
                str += '<td class="good_name" style="padding-top:5px;"><strong>Total</strong></td>';
                str += '<td class="good_price"><strong>$&nbsp;' + this.mInfo['total'] + '</strong></td>';

                str += '</table>';
                this.mFormObj.cart_submit.disabled = false;
                this.mDivObj.innerHTML = str;
            }
            else
            {
                this.mDivObj.innerHTML = 'Your shopping cart is empty';
                this.mFormObj.cart_submit.disabled = true;
            }
        },

        NewOrder: function()
        {
            this.mFormObj.cart_submit.disabled = true;

            var req     = new Subsys_JsHttpRequest_Js();
            var MainObj = this;

            req.onreadystatechange = function()
            {
                if (4 == req.readyState)
                {

                    var oinfo = req.responseJS['oinfo'];

                    if (oinfo)
                    {
                        MainObj.mFormObj.check_hash.value        = oinfo['check_hash'];
                        MainObj.mFormObj.in_order.value          = oinfo['order_id'];
                        MainObj.mFormObj.cart_order_id.value     = ' Icons # '+ oinfo['order_number'];
                        MainObj.mFormObj.merchant_order_id.value = oinfo['order_number'];
                        MainObj.mFormObj.total.value             = oinfo['amount'];

                        var uinfo = req.responseJS['uinfo'];
                        if (uinfo)
                        {
                            MainObj.mFormObj.card_holder_name.value = uinfo['fname'];
                            MainObj.mFormObj.street_address.value   = uinfo['street_address'];
                            MainObj.mFormObj.city.value             = uinfo['city'];
                            MainObj.mFormObj.state.value            = uinfo['iso3_div'];
                            MainObj.mFormObj.zip.value              = uinfo['zip'];
                            MainObj.mFormObj.country.value          = uinfo['iso2_cntr'];
                            MainObj.mFormObj.email.value            = uinfo['email'];
                            MainObj.mFormObj.phone.value            = uinfo['phone'];
                        }

                        MainObj.mFormObj.submit();
                    }
                    else
                    {
                        MainObj.mInfo = req.responseJS['cinfo'];
                        MainObj.GenerateList();
                        MainObj.mFormObj.cart_submit.disabled = false;
                        alert('Error in your order. Try again.');
                    }
                }
            }
            
            req.caching = false;
            req.open('GET', '/', true);
            req.send({mod: 'pmt', action: 'new'});
        },

        Sync: function()
        {
            var req     = new Subsys_JsHttpRequest_Js();
            var MainObj = this;

            req.onreadystatechange = function()
            {
                if (4 == req.readyState)
                {
                    MainObj.mInfo = req.responseJS['cinfo'];
                    MainObj.GenerateList();
                }
            }
            
            req.caching = false;
            req.open('GET', '/', true);
            req.send({mod: 'cart', action: 'sync'});
        },

        DeleteItem: function(i_id)
        {
            var req     = new Subsys_JsHttpRequest_Js();
            var MainObj = this;

            req.onreadystatechange = function()
            {
                if (4 == req.readyState)
                {
                    MainObj.mInfo = req.responseJS['cinfo'];
                    MainObj.GenerateList();
                }
            }
            
            req.caching = false;
            req.open('GET', '/', true);
            req.send({mod: 'cart', action: 'del', i_id: i_id});
        },

        AddItem: function(i_id)
        {
            var req     = new Subsys_JsHttpRequest_Js();
            var MainObj = this;

            req.onreadystatechange = function()
            {
                if (4 == req.readyState)
                {
                    MainObj.mInfo = req.responseJS['cinfo'];
                    MainObj.GenerateList();
                }
            }
            
            req.caching = false;
            req.open('GET', '/', true);
            req.send({mod: 'cart', action: 'add', i_id: i_id, cnt: 1});
        }
    }
})();