$(document).ready(function(e) {
    $.ajaxSetup({
        cache: false
    });
});

var CustomerList = {
    init:function(proxy) {
        $('#customer-search table td a').click(function(e) {
            e.preventDefault();

            if (proxy)
            {
                $('#customer-proxy').load(
                    baseUrl + "/services/customer-proxy",
                    {
                        shipto:$(this).attr('rel')
                    },
                    function()
                    {
                        window.location.reload();
                    }
                );

                
            } else {
                $('#billto').val($(this).attr('rel'));
                $('#billto-display').html($(this).attr('rel'));
                $.facebox.close();
                ShiptoList.update($('#billto').val());
            }
        });

        $('.pagination a').click(function(e) {
            e.preventDefault();
            $('#customer-search').parent().load($(this).attr('href'));

        });
    },

    update:function() {
        $('#customer-search').parent().load(
            $('#customer-search-filter').attr('action'),
            {
                customer:$('#search-customer').val(),
                name:    $('#search-name').val(),
                address: $('#search-address').val(),
                city:    $('#search-city').val(),
                state:   $('#search-state').val(),
                zip:     $('#search-zip').val()
            }
        );
    },

    clear:function() {
        $('#customer-search').parent().load(
            $('#customer-search-filter').attr('action')
        );
    }
};

var ShiptoList = {
    init:function(shipto) {
        if ($('#billto').val() != '')
        {
            this.update($('#billto').val(), shipto);
            $('#shipto-display').show();
        }
    },

    update:function(billto, shipto) {
        $('#shipto-selection').load(
            baseUrl + '/services/shipto-list',
            {
                billto:billto,
                shipto:shipto
            },
            function() {
                $('#shipto-display').show();
            }
        );
    }
};

var Cart = {
    init:function(serviceCode, tpbFlag) {
        Cart.loadServiceList(
            $('#shipping-carrier').val(),
            serviceCode, tpbFlag);

        $('#shipping-panel input').keyup(function(e) {
            $('#drop-ship-flag').val('1');
            $('#tpb_checkbox').show();
        });
    },
    productSearch:function(lineNumber) {
        $('#facebox .content').load(
            baseUrl + '/services/product-lookup',
            {
                partNumber:$('#search-part-number').val(),
                description:$('#search-description').val(),
                lineNumber:(typeof(lineNumber) == "undefined" ? "0" : lineNumber)
            }
        );
    },
    
    confirmEmpty:function() {
        if (confirm('Are you sure you want to empty your cart?'))
        {
            document.location = baseUrl + '/cart/index/empty';
        }
    },

    loadServiceList:function(carrier, defaultService, ignoreTPB) {
        var carrierArray = carrier.split("|");
        var value = carrierArray[0];

        $('#shipping-carrier').attr('disabled', 'disabled');
        $('#carrierService').show();
        $('#carrierServiceDropdown').html('<img src="' + baseUrl + '/images/progress_anim.gif" />...Searching');
        
        $('#carrierServiceDropdown').load(
            baseUrl + '/services/carrier-service-lookup',
            {
                carrier:value,
                defaultService:defaultService
            },
            function(result) {
                $('#shipping-carrier').attr('disabled', '');

                if ($.trim($('#carrierServiceDropdown').html()) == '')
                {
                    $('#carrierService').hide();
                }
            }
        );

        if (value in Cart.checklist(['FEDEX', 'FEDEXAIR','FEF','UPS','UPS SUPPLY']))
        {
            $('#collectAccountDisplay').show();

            if (!ignoreTPB)
            {
                $('#tpb_checkbox').hide();
            }
        }
        else
        {
            $('#collectAccountDisplay').hide();
        }
    },

    checklist:function(arrayList)
    {
        var result = {};
        for(var i = 0; i < arrayList.length; i++)
        {
            result[arrayList[i]]='';
        }
        return result;
    },

    checkTerms:function()
    {
        if (typeof($('#read-tos:checked').val()) != "undefined")
        {
            window.location='cartLoad.do';
            return true;
        }
        else
        {
            alert('You must agree to the Terms and Conditions to continue.');
            return false;
        }
    },

    setDropShipping:function() {
        dropAddress = $('#drop-shipping').val().split("|");

        shipAddress = {
            address1:   dropAddress[0],
            address2:   dropAddress[1],
            city:       dropAddress[2],
            state:      dropAddress[3],
            zip:        dropAddress[4],
            country:    dropAddress[5],
            name:       dropAddress[6],
            attn:       dropAddress[7],
            shipTo:     dropAddress[8],
            client:     dropAddress[9]
        };
        
        if ($('#shipping-carrier').val() in Cart.checklist(['FEDEX', 'FEDEXAIR','FEF','UPS','UPS SUPPLY']))
        {
            $('#tpb_checkbox').hide();
        }
        else
        {
            $('#tpb_checkbox').show();
        }	
        
        $('#drop-ship-flag').val("1");

        Cart.updateShipAddress(shipAddress);
        $(document).trigger('close.facebox');
    },

    setCustomerShipping:function() {
        $('#tpb_checkbox').hide();
        $('#drop-ship-flag').val("");
 
        $.getJSON(
            baseUrl + '/services/get-shipping',
            {
                shipTo:$('#customer-shipping').val()
            },
            function(address) {
              Cart.updateShipAddress(address);
              $(document).trigger('close.facebox');
            }
        );
    },

    updateShipAddress:function(address) {
        $('#shipto-name').val(address.name);
        $('#shipto-contact').val(address.contact);
        $('#shipto-address').val(address.address1);
        $('#shipto-address2').val(address.address2);
        $('#shipto-city').val(address.city);
        $('#shipto-state').val(address.state);
        $('#shipto-zip').val(address.zip);
        $('#shipto-country').val(address.country);
        $('#drop-ship-client').val(address.client);
        $('#shipto-cust').val(address.shipTo);
    },

    toggleExtraBilling:function(isChecked) {
        if (isChecked) {
            $('#extra_billing').show();
        } else {
            $('#extra_billing').hide();
        }
    },
    
    tpbOff:function() {
        $('#drop-ship-flag').val('0');
        $('#tpb_checkbox').hide();
        $('#shipto-contact').val('jim');
    }
};

var Qcart = {
    init:function(serviceCode, tpbFlag) {
        Cart.loadServiceList(
            $('#shipping-carrier').val(),
            serviceCode, tpbFlag);

        $('#shipping-panel input').keyup(function(e) {
            $('#drop-ship-flag').val('1');
            $('#tpb_checkbox').show();
        });
    },
    productSearch:function(lineNumber) {
        $('#facebox .content').load(
            baseUrl + '/services/product-lookup',
            {
                partNumber:$('#search-part-number').val(),
                description:$('#search-description').val(),
                lineNumber:(typeof(lineNumber) == "undefined" ? "0" : lineNumber)
            }
        );
    },
    
    confirmEmpty:function() {
        if (confirm('Are you sure you want to empty your cart?'))
        {
            document.location = baseUrl + '/qcart/index/empty';
        }
    },

    loadServiceList:function(carrier, defaultService, ignoreTPB) {
        var carrierArray = carrier.split("|");
        var value = carrierArray[0];
	        $('#shipping-carrier').attr('disabled', 'disabled');
        $('#carrierService').show();
        $('#carrierServiceDropdown').html('<img src="' + baseUrl + '/images/progress_anim.gif" />...Searching');
        
        $('#carrierServiceDropdown').load(
            baseUrl + '/services/carrier-service-lookup',
            {
                carrier:value,
                defaultService:defaultService
            },
            function(result) {
                $('#shipping-carrier').attr('disabled', '');
	                if ($.trim($('#carrierServiceDropdown').html()) == '')
                {
                    $('#carrierService').hide();
                }
            }
        );
	        if (value in Cart.checklist(['FEDEX', 'FEDEXAIR','FEF','UPS','UPS SUPPLY']))
        {
            $('#collectAccountDisplay').show();
	            if (!ignoreTPB)
            {
                $('#tpb_checkbox').hide();
            }
        }
        else
        {
            $('#collectAccountDisplay').hide();
        }
    },

    checklist:function(arrayList) {
        var result = {};
        for(var i = 0; i < arrayList.length; i++)
        {
            result[arrayList[i]]='';
        }
        return result;
    },

    checkTerms:function() {
	    if (typeof($('#read-tos:checked').val()) != "undefined")
	    {
	        window.location='cartLoad.do';
	        return true;
	    } else {
	        alert('You must agree to the Terms and Conditions to continue.');
	        return false;
	    }
	},

    setDropShipping:function() {
        dropAddress = $('#drop-shipping').val().split("|");

        shipAddress = {
            address1:   dropAddress[0],
            address2:   dropAddress[1],
            city:       dropAddress[2],
            state:      dropAddress[3],
            zip:        dropAddress[4],
            country:    dropAddress[5],
            name:       dropAddress[6],
            attn:       dropAddress[7],
            shipTo:     dropAddress[8],
            client:     dropAddress[9]
        };
	        
        if ($('#shipping-carrier').val() in Cart.checklist(['FEDEX', 'FEDEXAIR','FEF','UPS','UPS SUPPLY']))
        {
            $('#tpb_checkbox').hide();
        }
        else
        {
            $('#tpb_checkbox').show();
        }	
	        
        $('#drop-ship-flag').val("1");
        Cart.updateShipAddress(shipAddress);
        $(document).trigger('close.facebox');
    },

    setCustomerShipping:function() {
        $('#tpb_checkbox').hide();
        $('#drop-ship-flag').val("");
	 
        $.getJSON(
            baseUrl + '/services/get-shipping',
            {
                shipTo:$('#customer-shipping').val()
            },
            function(address) {
                Qcart.updateShipAddress(address);
                $(document).trigger('close.facebox');
            }
        );
    },

    updateShipAddress:function(address) {
        $('#shipto-name').val(address.name);
        $('#shipto-contact').val(address.contact);
        $('#shipto-address').val(address.address1);
        $('#shipto-address2').val(address.address2);
        $('#shipto-city').val(address.city);
        $('#shipto-state').val(address.state);
        $('#shipto-zip').val(address.zip);
        $('#shipto-country').val(address.country);
        $('#drop-ship-client').val(address.client);
        $('#shipto-cust').val(address.shipTo);
    },

    toggleExtraBilling:function(isChecked) {
        if (isChecked) {
            $('#extra_billing').show();
        } else {
            $('#extra_billing').hide();
        }
    },
    
    tpbOff:function() {
        $('#drop-ship-flag').val('0');
        $('#tpb_checkbox').hide();
        $('#shipto-contact').val('jim');
    }
};


