


    var base_url = "/includes/modules/bettingslip.asp";
    var stakes_url = "/includes/modules/bettingstakes.asp";
    var site_url = "http://www.brucebetting.com/";

    var basket_state = "selecting";

    var wagertype_stakes = {
        "wagertype_1": 0,
        "wagertype_2": 0,
        "wagertype_3": 0,
        "wagertype_4": 0,
        "wagertype_5": 0,
        "wagertype_6": 0,
        "wagertype_7": 0,
        "wagertype_8": 0,
        "wagertype_9": 0,
        "wagertype_10": 0,
        "wagertype_11": 0,
        "wagertype_12": 0,
        "wagertype_13": 0,
        "wagertype_14": 0,
        "wagertype_15": 0,
        "wagertype_16": 0,
        "wagertype_17": 0,
        "wagertype_18": 0,
        "wagertype_19": 0,
        "wagertype_20": 0,
        "wagertype_21": 0,
        "wagertype_22": 0,
        "wagertype_23": 0,
        "wagertype_24": 0,
        "wagertype_25": 0,
        "wagertype_26": 0,
        "wagertype_27": 0,
        "wagertype_28": 0,
        "wagertype_29": 0,
        "wagertype_30": 0
    };

    var wagertype_ew = {
        "ew_1": "",
        "ew_2": "",
        "ew_3": "",
        "ew_4": "",
        "ew_5": "",
        "ew_6": "",
        "ew_7": "",
        "ew_8": "",
        "ew_9": "",
        "ew_10": "",
        "ew_11": "",
        "ew_12": "",
        "ew_13": "",
        "ew_14": "",
        "ew_15": "",
        "ew_16": "",
        "ew_17": "",
        "ew_18": "",
        "ew_19": "",
        "ew_20": "",
        "ew_21": "",
        "ew_22": "",
        "ew_23": "",
        "ew_24": "",
        "ew_25": "",
        "ew_26": "",
        "ew_27": "",
        "ew_28": "",
        "ew_29": "",
        "ew_30": ""
    };

// Betting Slip Widget FUNCTIONS =============================================================================================================================================================

/*
 *  This function is only activated when a user is validated
 * It searches for Links that have a "place-bet-link" class
 * - mines the url from the onclick attr
 * - disables the onclick action
 * - creates a href attr and adds the url
 * all links rendered in the default.asp page will be converted to ajax calls
 */
function bettingslipLoad() {
    /*
    $("a.place_bet").each(function(i) {
        var str = $(this).attr("onclick");
        if (str!=null) {
            str = str.toString();
            var i_start = str.indexOf("MM_openBrWindow(", 0)+17;
            var i_end = str.indexOf(",", i_start+2)-1;
            var new_url = str.substring(i_start, i_end);
            var new_link = '<a href="'+new_url+'" class="place-bet-link"> AB</a>';
            $(this).after(new_link);
        }
    });
    */

    var temp = new Array();
    $(".place_bet").bind("click", function() {
        basket_state = "selecting";
        temp = $(this).attr("href").split("?");
        $.ajax({
            type: "GET",
            url: base_url,
            data: temp[1],
            beforeSend: function(xmlc) { /* DEBUG alert("bet params = "+temp[1]+" base_url = "+base_url); */ },
            success: function(msg){	$("#bettingslip").html(msg); bettingslipSetup(); },
            error: function() { alert("ajax:error"); }
        });
        return false;
    });

    // apply the toggle to the h2
    $("#bettingslip_holder h2, #bettingslip_holder a.open_close" ).bind("click", function() {
        toggleOpenClose();
	});

    if ($("#bettingslip_holder").attr("class")=="lowlight")
        $("#bettingslip_holder a.open_close").html("+");
    else
        $("#bettingslip_holder a.open_close").html("-");
}

/*
 * repeat functions and actions for each reload of the betting slip
 */
function bettingslipSetup(html) {
    if (html!="")
        $("#bettingslip").html(html);


    // display the count in the header bar
    var mode = $("#bettingslip_holder").attr("class");
    var count = $("#bettingslip #bets tr").length -1;


    // adjust the header bet count
    if (count<0)
        $("#bettingslip_holder .title span").text("No Selections");
    else if (count==1)
        $("#bettingslip_holder .title span").text("1 Selection");
    else
        $("#bettingslip_holder .title span").text(count+" Selections");


    // fade out message
    if ($("#bettingslip_holder .message").length > 0) {
        $("#bettingslip_holder .message").animate({
            opacity: "0.0"
        },5000, function() {
            $("#bettingslip_holder .message").remove();
        });
    }


    // removing bet actions
    removeAction("#bettingslip .remove", "Unable to remove selection");
    removeAction("#bettingslip .remove_all", "Unable to remove all selections");


    // display the login form when this link is clicked
    $("#betting-slip .open_login").bind("click", function() {
        $(this).hide();
        $("#ajax_login").show();
        $("#ajax_login").css("display","block");
        adjustSlipHeight();
    });


    // respond to the ajax login form
    $("#ajax_login").submit(function() {
        $(this).ajaxSubmit({
            target: "blank",
            beforeSubmit: loginChecking,
            success: function(txt) { loginResponse(txt); },
            dataType: 'text',
            timeout: 5000,
            error: errorResponse });
        return false;
    });

    // respond to the placing (clicking) of a bet
    // - first remove all stakes from the session
    $("#place_bet_now").submit(function() {
        var stake_test = "";
        basket_state = "checkout";
        $("#wagers :text").each(function() { stake_test = stake_test+$(this).val(); });
        if (stake_test=="")
            alert("Please make at least one selection before submitting your bet.");
        else {
            $(this).ajaxSubmit({
                target: "blank",
                beforeSubmit: function() { beforeBetIsPlaced(); },
                success:function(txt) { afterBetIsPlaced(txt); },
                error:function() { alert("Bet Error Response"); },
                dataType: "text"
            });
        }
        return false;
    });

    // update the totals every second
    $.timer(2000, function (timer) { writeTotals(); });
}

/*
 * Respond to the remove action.
 */
function removeAction(selector, error_message) {
    $(selector).bind("click", function() {
        var temp = new Array();
        temp = $(this).attr("href").split("?");
        $.ajax({
            type: "GET",
            url: base_url,
            data: temp[1],
            success: function(txt) { bettingslipSetup(txt);  },
            error: function() { alert(error_message); }
        });
        return false;
    });
}


function beforeBetIsPlaced() {
    basket_state = "bet_submitting";
    $("#bettingslip #place_bet_button").attr("value", "Your Bet Is Being Processed").css("font-weight", "700");
}

/*
 * Object returned will be outputted to the BettingSlip holder as a string.
 * Resulting string will be searched for wagers placed and not
 * Correct header message is written to the banner
 * "basket_state" and "account balance" are updated
 * Remove all details apart from result from wagers that have has be placed
 * Possible values for bet_message;
 */
function afterBetIsPlaced(ooo) {
    bettingslipSetup(ooo);
    var text = ooo.toString();

    var message = "Bet Placed";
    basket_state = "bet_placed";
    if (text.indexOf("Liability Exceeded")!=-1 && text.indexOf("Ticket Number")!=-1) {
        message = "Bet Partially Placed";
        basket_state = "bet_partially_placed";
    }
    else if (text.indexOf("Liability Exceeded")!=-1 && text.indexOf("Ticket Number")==-1) {
        message = "Bet Not Placed";
        basket_state = "bet_not_placed";
    }

    $("#bettingslip_holder .title span").text(message);
    $("#content_top1 .form_box1a strong").html("&euro;"+$("#account_balance").html());

    if (basket_state=="bet_placed") {
        $("#bets a.remove").css("display","none");
        $("#wagers tr").css("display","none");
        $("#wagers tr.bet_message").css("display","block");
    }
    else if (basket_state=="bet_partially_placed") {
        $("#bets a.remove").css("display","none");
        $("#wagers tr").css("display","none");
        $("#wagers tr.bet_message").css("display","block");
    }
}

/*
 * Find out what state the bettingslip is in
 * Toggle the bettingslip its opposite state
 */
function toggleOpenClose() {
	var open_it = false;
	if ($("#bettingslip_holder").attr("class")=="lowlight")
		open_it = true;
	if (open_it) {
		$("#bettingslip").show("slow");
        $("#bettingslip_holder").attr("class", "highlight");
        $("#bettingslip_holder a.open_close").html("-");
    }
	else {
        $("#bettingslip").hide("fast");
        $("#bettingslip_holder").attr("class", "lowlight");
        $("#bettingslip_holder a.open_close").html("+");
    }
}

/*
 * store the data  and update the totals
 */
function setWagerChange(id, value) {
	$.ajax({ type: "GET", url: stakes_url, data: "task=store&"+id+"="+value });
}

/*
 * store the data  and update the totals
 */
function setEachwayChange(id, value) {
	$.ajax({ type: "GET", url: stakes_url, data: "task=storeew&"+id+"="+value });
}

/*
 * Reload the #bettingslip - AJAX
 */
function reloadBettingSlip() {
    $.ajax({
        type: "GET",
        url: base_url,
        success: function(msg){ $("#bettingslip").html(msg); bettingslipSetup(); },
        error: function() {	/*alert("Unable to reload Betting Slip");*/	}
    });
    return false;
}



function getStake(ooo, ttt) {
    if ($("#EW_"+ttt).fieldValue()==1)
        return parseFloat(ooo.attr("value")) * 2;
    else
        return parseFloat(ooo.attr("value"));
}

/*
 * Calculate the totals
 */
function writeTotals() {
    var potential_return = 0;
    var total_stake = 0;
    var line_count = parseInt($("#bettingslip #nolines_13").attr("value"));

    $("#bettingslip #wagers :text").each(function(i) {
        if ($(this).attr("value")!="") {

            // current stake info
            var id = $(this).attr("id");
            var stake = $(this).attr("value");
            var temp = id.split("_");
            id = temp[1];
            var ew = $("#EW_"+id).fieldValue()+"";

            // if the current wager type is singles
            if (id=="13") {
                if (ew=="1")
                    total_stake += parseFloat(stake) * line_count * 2;
                else
                    total_stake += parseFloat(stake) * line_count;
            }

            // all other wager types
            else {
                if (ew=="1")
                    total_stake += parseFloat(stake) * 2;
                else
                    total_stake += parseFloat(stake);
            }

            // if the stake has changed save it
            if (wagertype_stakes["wagertype_"+id] != stake) {
                wagertype_stakes["wagertype_"+id] = stake;
                setWagerChange("wagertype_"+id, stake);
                $(this).attr("value", parseFloat(stake).toFixed(2));
            }

            // test if the EW option has changed
            if (wagertype_ew["ew_"+id] != ew) {
                log("ew change before, array = -"+wagertype_ew["ew_"+id]+"-, current = -"+ew+"-, ["+id+"]");
                wagertype_ew["ew_"+id] = ew+"";
                //log("ew change after, array = -"+wagertype_ew["ew_"+id]+"-, current = -"+ew+"-, ["+id+"]");
                setEachwayChange("EW_"+id, wagertype_ew["ew_"+id]);
            }
        }





/*
            // [13] singles
            if ($(this).attr("id") == "wagertype_13") {
                if ($("#EW_13").fieldValue()==1)
                    total_stake += parseFloat($(this).attr("value")) * line_count * 2;
                else
                    total_stake += parseFloat($(this).attr("value")) * line_count;
            }

            // [14] doubles
            else if ($(this).attr("id") == "wagertype_14")
                total_stake += getStake($(this), "14");

            // [15] trebles
            else if ($(this).attr("id") == "wagertype_15")
                total_stake += getStake($(this), "15");

            // [6] super yankee
            else if ($(this).attr("id") == "wagertype_6")
                total_stake += getStake($(this), "6");

            // [11] lucky 31
            else if ($(this).attr("id") == "wagertype_11")
                total_stake += getStake($(this), "11");

            // [16] perm 4 folds
            else if ($(this).attr("id") == "wagertype_16")
                total_stake += getStake($(this), "16");

            // [17] perm 5 folds
            else if ($(this).attr("id") == "wagertype_17")
                total_stake += getStake($(this), "17");

            // [5] yankee 5
            else if ($(this).attr("id") == "wagertype_5")
                total_stake += getStake($(this), "5");

            // [10] lucky 15
            else if ($(this).attr("id") == "wagertype_10")
                total_stake += getStake($(this), "10");

            // [3] Trixie
            else if ($(this).attr("id") == "wagertype_3")
                total_stake += getStake($(this), "3");

            // [4] Patent or Twist
            else if ($(this).attr("id") == "wagertype_4")
                total_stake += getStake($(this), "4");



            // all other wager types
            else
                total_stake += parseFloat($(this).attr("value"));
        }



        if ($(this).attr("id")=="wagertype_3" && $(this).attr("value")!=wagertype_3) {
            wagertype_3 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_4" && $(this).attr("value")!=wagertype_4) {
            wagertype_4 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_5" && $(this).attr("value")!=wagertype_5) {
            wagertype_5 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_6" && $(this).attr("value")!=wagertype_6) {
            wagertype_6 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_10" && $(this).attr("value")!=wagertype_10) {
            wagertype_10 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_11" && $(this).attr("value")!=wagertype_11) {
            wagertype_11 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_13" && $(this).attr("value")!=wagertype_13) {
            wagertype_13 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_14" && $(this).attr("value")!=wagertype_14) {
            wagertype_14 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_15" && $(this).attr("value")!=wagertype_15) {
            wagertype_15 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_16" && $(this).attr("value")!=wagertype_16) {
            wagertype_16 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_17" && $(this).attr("value")!=wagertype_17) {
            wagertype_17 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_21" && $(this).attr("value")!=wagertype_21) {
            wagertype_21 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_22" && $(this).attr("value")!=wagertype_22) {
            wagertype_22 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_23" && $(this).attr("value")!=wagertype_23) {
            wagertype_23 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_24" && $(this).attr("value")!=wagertype_24) {
            wagertype_24 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }

        if ($(this).attr("id")=="wagertype_28" && $(this).attr("value")!=wagertype_28) {
            wagertype_28 = $(this).attr("value");
            setWagerChange($(this).attr("id"), $(this).attr("value"));
        }
            */
    });

    if (basket_state=="selecting") {
        $("#bettingslip_holder .return span").html(potential_return.toFixed(2));
        $("#bettingslip_holder .stake span").html(total_stake.toFixed(2));
    }
}

