﻿/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*                            Error Messages                                       * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var $ERROR_INVALID_MINIMUM_HORSEBET = 'To make a Win/Place/Show bet check at least one box of horse(s).\n';
var $ERROR_INVALID_MINIMUM_EXACTA = 'To make an Exacta, check at least one horse to win and one horse to place.\n';
var $ERROR_INVALID_SELECTION_EXACTA = 'Invalid selections for Exacta. Please check that your selections include at least one different runner per leg.\n';
var $ERROR_INVALID_MINIMUM_QUINELLA = 'To make a Quinella bet check at least two runners.\n';
var $ERROR_INVALID_MINIMUM_TRIFECTA = 'To make a trifecta bet, check at least one runner to win, one to place and one to show.\n';
var $ERROR_INVALID_SELECTION_TRIFECTA = 'Invalid selections for Trifecta. Please check that your selections include at least one different runner per leg.\n';
var $ERROR_INVALID_MINIMUM_SUPERFECTA = 'To make a superfecta, check at least one runner to win, one to place, one to show and one for fourth place.\n';
var $ERROR_INVALID_SELECTION_SUPERFECTA = 'Invalid selection for Superfecta. Please check that your selections include at least four runners.\n';
var $ERROR_INVALID_MINIMUM_DOUBLE = 'To make a Daily Double bet, check at least one runner to win from two races.\n';
var $ERROR_INVALID_MINIMUM_PICK = 'To make a pick bet, check at least one runner to win each race.\n';
var $ERROR_INVALID_MAXIMUM = 'No more than 10 runners can be checked.\n';
var $ERROR_NO_LOGIN = 'You must log in to place wagers.\n';

var CHECKBOX_WIN = 'chkWin';
var CHECKBOX_PLACE = 'chkPlace';
var CHECKBOX_SHOW = 'chkShow';
var CHECKBOX_FOURTH = 'chkFourth';
var CHECKBOX_BOX = 'chkBox';

var BETYPE_EXACTA = 'Exacta';
var BETYPE_TRIFECTA = 'Trifecta';
var BETYPE_SUPERFECTA = 'Superfecta';

var CHECKBOX_FOURTH = 'chkFourth';
var CHECKBOX_BOX = 'chkBox';

var $MAXIMUM_HORSES_CHECKED = 21;

var IsCustomer = false;


function BetType_OnInitialize($iscustomer) {
    IsCustomer = $iscustomer;
}

function ValidateLogin() {
    var $res = true;
    if (IsCustomer == false) {
        alert($ERROR_NO_LOGIN);
        $res = false;
    }
    return $res;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Validate if an object is a checkbox                                             * 
* Returns true if is checkbox                                                     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function IsCheckbox($checkboxname) {
    var $ischeckbox = false;

    var $checkbox = document.all ? document.all[$checkboxname] : document.getElementById($checkboxname);
    $ischeckbox = ($checkbox != null && $checkbox != undefined);
    $checkbox = null;

    return $ischeckbox;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Validate if an checkbox is checked                                              * 
* Returns true if is checkbox                                                     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function IsChecked($checkboxname) {
    var $ischecked = false;

    var $checkbox = document.all ? document.all[$checkboxname] : document.getElementById($checkboxname);
    $ischecked = $checkbox.checked;
    $checkbox = null;

    return $ischecked;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* Validate a win/place/show bet                                                   * 
* The user has to check at least one box of horse                                 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ValidateHorseBet($repeatername, $itemscount) {
    var $continue = ValidateLogin();

    if ($continue == true) {
        $continue = false;
        for (var $itemindex = 1; $itemindex <= $itemscount; $itemindex++) {
            var $checkboxname = $repeatername + '_ctl' + String(($itemindex < 10 ? '0' : '')) + String($itemindex) + '_';

            if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true)
                $continue = $continue || IsChecked($checkboxname + CHECKBOX_WIN);

            if (IsCheckbox($checkboxname + CHECKBOX_PLACE) == true)
                $continue = $continue || IsChecked($checkboxname + CHECKBOX_PLACE);


            if (IsCheckbox($checkboxname + CHECKBOX_SHOW) == true)
                $continue = $continue || IsChecked($checkboxname + CHECKBOX_SHOW);

        }
        if ($continue == false) alert($ERROR_INVALID_MINIMUM_HORSEBET);
    }
    return $continue;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  
* Validate a exacta bet, it requires at least one runner to win and one to place  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ValidateExacta($repeatername, $itemscount, $boxcontrol) {
    var $continue = ValidateLogin();
    var $countcheckeds = 0;
    var $boxhiddenfiled = document.all ? document.all[$boxcontrol] : document.getElementById($boxcontrol);
    var $isbox = (($boxhiddenfiled != null && $boxhiddenfiled != undefined) ? $boxhiddenfiled.value : 0);

    if ($continue == true) {
        $continue = false;
        var $arraywin = new Array();
        var $arrayplace = new Array();
        var $exacta = new Array();

        for (var $itemindex = 1; $itemindex <= $itemscount; $itemindex++) {
            var $checkboxname = $repeatername + '_ctl' + String(($itemindex < 10 ? '0' : '')) + String($itemindex) + '_';

            if ($isbox.toString() == '1') {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true)
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                    $arraywin[$arraywin.length] = $itemindex;
                    $arrayplace[$arrayplace.length] = $itemindex;
                    $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                }
            }

            else {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                        $arraywin[$arraywin.length] = $itemindex;
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                    }
                }

                if (IsCheckbox($checkboxname + CHECKBOX_PLACE) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_PLACE) == true) {
                        $arrayplace[$arrayplace.length] = $itemindex;
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_PLACE) == true ? 1 : 0);
                    }
                }
            }
            if ($countcheckeds > $MAXIMUM_HORSES_CHECKED) {
                alert($ERROR_INVALID_MAXIMUM);
                return false;
            }
        }


        if (($arraywin.length == 0) || ($arrayplace.length == 0))
            alert($ERROR_INVALID_MINIMUM_EXACTA);

        else {
            for ($indexwin = 0; $indexwin < $arraywin.length; $indexwin++) {
                var $item = '[' + String($arraywin[$indexwin]) + ']';

                for ($indexplace = 0; $indexplace < $arrayplace.length; $indexplace++) {
                    if ($item.indexOf('[' + String($arrayplace[$indexplace]) + ']') == -1) {
                        $item += '[' + String($arrayplace[$indexplace]) + ']';
                        $exacta[$exacta.length] = $item;
                    }
                }
            }

            $continue = ($exacta.length > 0);
            if ($continue == false) alert($ERROR_INVALID_SELECTION_EXACTA);
        }
    }
    return $continue;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Validate a trifecta bet.                                                        *   
* The user has to choose at least one runner to win, one to place and one to show *
* Also the user has to choose at leat one valid combination                       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ValidateTrifecta($repeatername, $itemscount, $boxcontrol) {
    var $continue = ValidateLogin();
    var $countcheckeds = 0;
    var $boxhiddenfiled = document.all ? document.all[$boxcontrol] : document.getElementById($boxcontrol);
    var $isbox = (($boxhiddenfiled != null && $boxhiddenfiled != undefined) ? $boxhiddenfiled.value : 0);

    if ($continue == true) {
        $continue = false;
        var $arraywin = new Array();
        var $arrayplace = new Array();
        var $arrayshow = new Array();
        var $trifecta = new Array();

        for (var $itemindex = 1; $itemindex <= $itemscount; $itemindex++) {
            var $checkboxname = $repeatername + '_ctl' + String(($itemindex < 10 ? '0' : '')) + String($itemindex) + '_';

            if ($isbox.toString() == '1') {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true)
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                    $arraywin[$arraywin.length] = $itemindex;
                    $arrayplace[$arrayplace.length] = $itemindex;
                    $arrayshow[$arrayshow.length] = String($itemindex);
                    $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                }
            }

            else {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                        $arraywin[$arraywin.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                    }
                }

                if (IsCheckbox($checkboxname + CHECKBOX_PLACE) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_PLACE) == true) {
                        $arrayplace[$arrayplace.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_PLACE) == true ? 1 : 0);
                    }
                }

                if (IsCheckbox($checkboxname + CHECKBOX_SHOW) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_SHOW) == true) {
                        $arrayshow[$arrayshow.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_SHOW) == true ? 1 : 0);
                    }
                }
            }
            if ($countcheckeds > $MAXIMUM_HORSES_CHECKED) {
                alert($ERROR_INVALID_MAXIMUM);
                return false;
            }
        }

        if (($arraywin.length == 0) || ($arrayplace.length == 0) || ($arrayshow.length == 0))
            alert($ERROR_INVALID_MINIMUM_TRIFECTA);

        else {
            for (var $indexwin = 0; $indexwin < $arraywin.length; $indexwin++) {
                var $arraytovalidate = new Array();
                $arraytovalidate[$arraytovalidate.length] = $arraywin[$indexwin];

                for (var $indexplace = 0; $indexplace < $arrayplace.length; $indexplace++) {
                    if ($arraytovalidate[0] != $arrayplace[$indexplace]) {
                        if ($arraytovalidate.length > 1)
                            $arraytovalidate.splice($arraytovalidate.length - 1, 1);

                        $arraytovalidate[$arraytovalidate.length] = $arrayplace[$indexplace];

                        for (var $indexshow = 0; $indexshow < $arrayshow.length; $indexshow++) {
                            if (($arraytovalidate[0] != $arrayshow[$indexshow]) && ($arraytovalidate[1] != $arrayshow[$indexshow])) {
                                $arraytovalidate[$arraytovalidate.length] = $arrayshow[$indexshow];
                                $indexwin = $arraywin.length;
                                $indexplace = $arrayplace.length;
                                $indexshow = $arrayshow.length;
                                $continue = true;
                            }
                        }
                    }
                }
            }
            if ($continue == false) alert($ERROR_INVALID_SELECTION_TRIFECTA);
        }
    }
    return $continue;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Validate a superfecta bet.                                                      *   
* The user has to choose at least one runner to win, one to place, one to show    *
* and one for fourth place.                                                       * 
* Also the user has to choose at leat one valid combination                       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ValidateSuperfecta($repeatername, $itemscount, $boxcontrol) {
    var $continue = ValidateLogin();
    var $countcheckeds = 0;
    var $boxhiddenfiled = document.all ? document.all[$boxcontrol] : document.getElementById($boxcontrol);
    var $isbox = (($boxhiddenfiled != null && $boxhiddenfiled != undefined) ? $boxhiddenfiled.value : 0);

    if ($continue == true) {
        $continue = false;
        var $arraywin = new Array();
        var $arrayplace = new Array();
        var $arrayshow = new Array();
        var $arrayfourth = new Array();

        for (var $itemindex = 1; $itemindex <= $itemscount; $itemindex++) {
            var $checkboxname = $repeatername + '_ctl' + String(($itemindex < 10 ? '0' : '')) + String($itemindex) + '_';

            if ($isbox.toString() == '1') {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true)
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                    $arraywin[$arraywin.length] = $itemindex;
                    $arrayplace[$arrayplace.length] = $itemindex;
                    $arrayshow[$arrayshow.length] = String($itemindex);
                    $arrayfourth[$arrayfourth.length] = String($itemindex);
                    $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                }
            }

            else {
                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_WIN) == true) {
                        $arraywin[$arraywin.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                    }
                }

                if (IsCheckbox($checkboxname + CHECKBOX_PLACE) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_PLACE) == true) {
                        $arrayplace[$arrayplace.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_PLACE) == true ? 1 : 0);
                    }
                }

                if (IsCheckbox($checkboxname + CHECKBOX_SHOW) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_SHOW) == true) {
                        $arrayshow[$arrayshow.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_SHOW) == true ? 1 : 0);
                    }
                }
                if (IsCheckbox($checkboxname + CHECKBOX_FOURTH) == true) {
                    if (IsChecked($checkboxname + CHECKBOX_FOURTH) == true) {
                        $arrayfourth[$arrayfourth.length] = String($itemindex);
                        $countcheckeds += (IsChecked($checkboxname + CHECKBOX_FOURTH) == true ? 1 : 0);
                    }
                }
            }
            if ($countcheckeds > $MAXIMUM_HORSES_CHECKED) {
                alert($ERROR_INVALID_MAXIMUM);
                return false;
            }
        }

        if (($arraywin.length == 0) || ($arrayplace.length == 0) || ($arrayshow.length == 0) || ($arrayfourth.length == 0))
            alert($ERROR_INVALID_MINIMUM_SUPERFECTA);

        else {
            for (var $indexwin = 0; $indexwin < $arraywin.length; $indexwin++) {
                var $arraytovalidate = new Array();
                $arraytovalidate[$arraytovalidate.length] = $arraywin[$indexwin];

                for (var $indexplace = 0; $indexplace < $arrayplace.length; $indexplace++) {
                    if ($arraytovalidate[0] != $arrayplace[$indexplace]) {
                        if ($arraytovalidate.length > 1)
                            $arraytovalidate.splice(1, $arraytovalidate.length - 1);

                        $arraytovalidate[$arraytovalidate.length] = $arrayplace[$indexplace];

                        for (var $indexshow = 0; $indexshow < $arrayshow.length; $indexshow++) {
                            if (($arraytovalidate[0] != $arrayshow[$indexshow]) && ($arraytovalidate[1] != $arrayshow[$indexshow])) {
                                if ($arraytovalidate.length > 2)
                                    $arraytovalidate.splice(2, $arraytovalidate.length - 2);

                                $arraytovalidate[$arraytovalidate.length] = $arrayshow[$indexshow];

                                for (var $indexfourth = 0; $indexfourth < $arrayfourth.length; $indexfourth++) {
                                    if (($arraytovalidate[0] != $arrayfourth[$indexfourth]) && ($arraytovalidate[1] != $arrayfourth[$indexfourth]) &&
                                        ($arraytovalidate[2] != $arrayfourth[$indexfourth])) {
                                        $arraytovalidate[$arraytovalidate.length] = $arrayfourth[$indexfourth];
                                        $indexwin = $arraywin.length;
                                        $indexplace = $arrayplace.length;
                                        $indexshow = $arrayshow.length;
                                        $indexfourth = $arrayfourth.length;
                                        $continue = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if ($continue == false) alert($ERROR_INVALID_SELECTION_SUPERFECTA);
        }
    }

    return $continue;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Validate a Quinella bet.                                                      *   
* The user has to choose at least one runner to win, one to place, one to show    *
* and one for fourth place.                                                       * 
* Also the user has to choose at leat one valid combination                       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ValidateQuinella($repeatername, $itemscount) {
    var $continue = ValidateLogin();
    var $countcheckeds = 0;

    if ($continue == true) {
        $continue = false;

        for (var $indexquinella = 1; $indexquinella <= $itemscount; $indexquinella++) {
            var $checkboxname = $repeatername + '_ctl' + String(($indexquinella < 10 ? '0' : '')) + String($indexquinella) + '_';

            if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true)
                $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);

            if ($countcheckeds > $MAXIMUM_HORSES_CHECKED) {
                alert($ERROR_INVALID_MAXIMUM);
                return false;
            }
        }

        /* * * * * * * * * * * * * * * * * * * *
        * Check the minimum selected items    *
        * * * * * * * * * * * * * * * * * * * */
        if ($countcheckeds >= 2)
            $continue = true;
        else
            alert($ERROR_INVALID_MINIMUM_QUINELLA);
    }
    return $continue;
}


function ValidateDoublePick($repeatername, $itemscount, $bettype) {
    var $continue = ValidateLogin();
    var $minimunCorrect = 0;
    var $countcheckeds = 0;

    if ($continue == true) {
        $continue = false;
        var $repeaterelements = $repeatername.split(',');
        var $repeatercount = $itemscount.split(',');

        /* * * * * * * * * * * * * * * * * * * * * 
        * Check the items in each datalist      *
        * * * * * * * * * * * * * * * * * * * * */
        for (var $i = 0; $i < $repeaterelements.length; $i++) {
            var $repeateritemname = $repeaterelements[$i];
            var $repeateritemcount = $repeatercount[$i];
            /* * * * * * * * * * * * * * * * * * * *
            *Check the chekboxes in each item     *
            * * * * * * * * * * * * * * * * * * * */
            for (var $index = 1; $index <= $repeateritemcount; $index++) {
                var $checkboxname = $repeateritemname + '_ctl' + String(($index < 10 ? '0' : '')) + String($index) + '_';

                if (IsCheckbox($checkboxname + CHECKBOX_WIN) == true) {
                    $minimunCorrect += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                    $countcheckeds += (IsChecked($checkboxname + CHECKBOX_WIN) == true ? 1 : 0);
                }
                if ($countcheckeds > $MAXIMUM_HORSES_CHECKED) {
                    alert($ERROR_INVALID_MAXIMUM);
                    return false;
                }
            }

            if ($minimunCorrect < 1) {
                $minimunCorrect = 0;
                break;
            }
            else
                if (($i + 1) < $repeaterelements.length)
                $minimunCorrect = 0;
        }
        /* * * * * * * * * * * * * * * * * * * *
        * Check the minimum selected items    *
        * * * * * * * * * * * * * * * * * * * */
        if ($minimunCorrect >= 1)
            $continue = true;
        else {
            if ($bettype.toString().toUpperCase().search('PICK') != -1)
                alert($ERROR_INVALID_MINIMUM_PICK);
            else
                alert($ERROR_INVALID_MINIMUM_DOUBLE);
        }
    }
    return $continue;
}        



