function updateBasket_onclick(updateDB) {
	return updateBasket(true);
}

function updateBasket(updateDB) {
    var frags = {};
    frags['dynamicBasket'] = 'true';
    if (updateDB) frags['updateDB'] = 'true';
    var pos = 0;
    var elements = false;
    jQuery(".quantity").each(function() {
        var name = jQuery(this).attr('name');
        var options = jQuery(this).attr('options');
        var qty = options[jQuery(this).attr('selectedIndex')].text;
        frags[name] = qty;
        elements = true;
    });
    if (elements) {
        jQuery.getJSON("account",frags, function(j){
            for (var i = 0; i < j.length; i++) {
                jQuery("#"+j[i].field).html(j[i].value);
            }
	        updateBudget(false);
			try{
				calcTotal();
			}catch(e){}
        })
    }
}

function updateBudget(manual){
	if(jQuery("#budgetBalance").length){
		var balance = parseFloat(manual ? jQuery("#budgetAmount").attr('value') : jQuery("#budgetBalance").attr('value'));
		var total = parseFloat(jQuery("#order_net").html());
		var gross = parseFloat(jQuery("#order_gross").html());
		
		if(isNaN(balance) || balance < 0)balance = 0;

		if(balance >= gross){
			if(!manual || balance >= gross)jQuery("#budgetAmount").attr('value',formatCurrency(gross));
			jQuery("#cardAmount").html('0.00');
			jQuery("#budgetString").html(formatCurrency(gross));
			jQuery("#budgetNotify").css("display","none");
			jQuery("#balanceInfo").css("display","");
		}
		else{
			if(!manual || isNaN(jQuery("#budgetAmount").attr('value')))jQuery("#budgetAmount").attr('value',0);
			jQuery("#cardAmount").html(formatCurrency(total+parseFloat(jQuery("#superstoreVAT").attr("value"))));
			jQuery("#budgetNotify").css("display","");
			jQuery("#balanceInfo").css("display","none");
		}
	}
}

function formatCurrency(n){
	if(isNaN(n))n = 0;
	n += '';
	n += n.match(/\./) ? "00" : ".00";
	n = n.split('.');
	n = n[0]+'.'+n[1].substr(0,2);
	return n;
}

function showBDMs(handle) {

    var val = 0;
	if (handle) {
		val = handle.value;
	}

	$('bdmlist').load('/home/menu-creator/account?bdmlist='+val);
    //$(document).ready(function() {
    //    document.getElementById('bdmLoginId').Attributes.add('onchange', "showPubs(this);");
    //})
    //$('bdmLoginId').attr('onchange', "showPubs(this);");
	showPubs();
}

function showPubs(handle) {
	var val = 0;
	if (handle) {
		val = handle.value;
	}

	$('publist').load('/home/menu-creator/account?publist='+val);
}

function checkPubSelected() {
    control = document.getElementById('pubLoginId');
    if (control.selectedIndex == 0) {
        alert('Please select a pub');
        return false;
    } else {
        return true;
    }
}

function checkBasket() {
	var gt = $('#grand_total')[0].value;
	
	if (!gt || gt == 0) {
		alert('Please choose the quantities for the items you would like to order');
		return false;
	}
	return true;
}

    jQuery.noConflict();
    jQuery('document').ready(function() {
        jQuery('#odLoginId').change(function() {
            jQuery("#bdmLoginId").html = '';
            jQuery.getJSON("account",{bdmlist: jQuery(this).val()}, function(j){
                var options = '';
                //alert(j);
                for (var i = 0; i < j.length; i++) {
                    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                }
                jQuery("#bdmLoginId").html(options);
                jQuery('#bdmLoginId option:first').attr('selected', 'selected');
                
            })
            jQuery("#pubLoginId").html = '<option value="0">Choose a pub...</option>';
            jQuery('#pubLoginId option:first').attr('selected', 'selected');
        });
    });

    jQuery('document').ready(function() {
        jQuery('#bdmLoginId').change(function() {
            jQuery("#pubLoginId").html = '';
            jQuery.getJSON("account",{publist: jQuery(this).val()}, function(j){
                var options = '';
                for (var i = 0; i < j.length; i++) {
                    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                }

                jQuery("#pubLoginId").html(options);
                jQuery('#pubLoginId option:first').attr('selected', 'selected');
            })
        });
    });

    jQuery('document').ready(function() {
        updateBasket();
    });


    //pic1= new Image(32,32);
    //pic1.src="/static/images/pos/icon_loading.gif";


    var globalPdfDownloadInProgress = false;

    function SetPreviewSpinner(entity) {
        if (globalPdfDownloadInProgress) {
            alert('Generating the document from your designs can take a few minutes, please be patient.');
            return false;
        } else {
            //entity.src = pic1.src;
            globalPdfDownloadInProgress = true;
            return true;
        }
    }
