﻿jQuery.validator.addMethod("mustbetrue", function (value, element, param) {
    return element.checked;
});


jQuery.validator.unobtrusive.adapters.add("mustbetrue", {}, function (options) {
    options.rules["mustbetrue"] = true;
    options.messages["mustbetrue"] = options.message;
});


jQuery.validator.addMethod("comparepasswords", function (value, element, param) {
    return value == $(param).val();
});


jQuery.validator.unobtrusive.adapters.add("comparepasswords", ["otherfield"], function (options) {
    options.rules["comparepasswords"] = "#" + options.params.otherfield;
    options.messages["comparepasswords"] = options.message;
});

// Username / Password input placeholder text

$(document).ready(function () {

    $('#Username').blur(function () {
        if ($(this).val() == '') {
            $('#fakeuser').show();
        }
    });

    $('#Password').blur(function () {
        if ($(this).val() == '') {
            $('#fakepass').show();
        }
    });
}); 

function RefreshBalanceMainPage() {
    $.get('/Home/CheckIsSessionExpired', function (data) {
        if (data == "True") {
            jAlert("Your session expired. Please log in again", "Information", function () {
                window.location.reload();
            });

        }
        else {
            $.get('/Home/DisplayNewHeader', function (c) {
                $("#logindiv").html(c);
            });
        }
    });

}

function FormSuccess(data, status, xhr) {
    $("#form1").unblock();
    if (xhr.status == 200) {
        jAlert(data, "Confirmation");
    }
    if (xhr.status == 210) {
        jAlert("Operation Failed. \n" + data.error, "Error");
    }

}
function ResponsibleGaminSuccess(data, status, xhr) {
    $("#form1").unblock();
    
    if (xhr.status == 200) {
        jAlert(data, "Confirmation");
    }
    if (xhr.status == 210) {
        jAlert("Operation Failed. \n" + data, "Error");
    }

}

function FormFailed() {
    $("#form1").unblock();

    jAlert('Problem with application.', "Error");
}

function SubmitResponsibleGaming() {
    //$("#ResponsibleGamingForm").block({
    $.blockUI({
        message: '<img src="../../Content/Images/ajax-loader-home.gif" />',
        css: {
            width: '100px',
            height: '100px'
        }
    });
}

function DateOfBirthChanged() {
    if ($("#Day").val() == '' || $("#Month").val() == '' || $("#Year").val() == '') {
        $("#errorDateOfBirth").show();
    }
    else
        $("#errorDateOfBirth").hide();
}


function UpdateHeaderFooter(url) {

    $.get(url, function (c) {
        $("#logindiv").html(c);
    });
}

function UpdateMenuTop() {

    $.get('/Home/UpdateMenuTop', function (c) {
        $("#sportsTopMenu").html(c);
    });
}


function Logout(a, f) {
    $.blockUI({
        message: '<img src="../../Content/Images/ajax-loader-home.gif" />',
        css: {
            width: '100px',
            height: '100px'
        }
    });
    $.ajax({
        url: a,
        type: "Get",
        success: function (data) {
            $.get(f, function (data) {
                $("#logindiv").html(data);
            })
            GetGameCategory('Most Popular');
            $.get('/GameInfo/GetLoggedInView?game=' + getParameterByName('game'), function (c) {
                $("#gameinfolist").html(c);

            });
            $.get('/Home/UpdateSideBanner', function (c) {
                $("#sidebanner").html(c);
            })
            $.get('/Jackpots/GetJackpots', function (c) {
                $("#jackpotslist").html(c);
            })

            $.get('/Home/GetFlashAds', function(c) {
                $("#flashcarousel").html(c);
            })

            UpdateMenuTop();
            $('#myaccountlink').hide();
        }
    });

}
function GetGameCategory(gameCat) {
    if ($("#gameslist") == null || $("#gameslist").length < 1)
        window.location.href = '/Games?category=' + gameCat;
    else {
        $.get('/Games/GetList/?category=' + gameCat, function (c) {
            $("#gameslist").html(c);
        });
        $.get('/Games/GetBreadCrumbs/?category=' + gameCat, function (d) {
            $("#breadcrumbs").html(d);
        });
    }

    $("#sidenav1 > li").removeClass("active");
    $("#" + gameCat.replace(" ", "")).addClass("active");

}
function GetGameCategoryAll(gameCat) {
    $.get('/Games/GetListAll/?category=' + gameCat, function (c) {
        $("#gameslist").html(c);
    });
}

function GetPromoDetails(title) {
    $.get('/Promotions/GetDetails/?title=' + title, function (c) {
        $("#promoholder").html(c);
    });
}

function GetPromoDetailsLeft(title) {
    $.get('/Promotions/GetDetailsLeft/?title=' + title, function (c) {
        $("#promoholder").html(c);
    });
}

function BackToPromotions() {
    $.get('/Promotions/ShowMain', function (c) {
        $("#promoholder").html(c);
    });
}

function OpenAccountPopup() {
    $.get('/Home/Registration', function (data) {
        $("#openAccount").html(data);
        $.validator.unobtrusive.parse($("#openAccount"));
        $("#openAccount").dialog({ 
            open: function(event, ui) {
                window.setTimeout(function() {
                    jQuery(document).unbind('mousedown.dialog-overlay').unbind('mouseup.dialog-overlay');
                }, 100);
            },
            modal: true });
        $(".ui-dialog").css('left', '250px');
        $(".ui-dialog").css('position', 'absolute');
        $(".ui-dialog").css('top', '10px');
        var title = $("#popupTitle").html();
        $(".ui-dialog-titlebar").html(title);
        $("#popupTitle").hide();
    });

}


function CountryChange() {
    var val = $("#Country").val();
    $("#PhonePrefix").val(val.split(":")[1]);
    if (val.split(":")[0] == 'IE') {
        $("#Postcode").attr('disabled', true);
        $("#Postcode").val('N/A');
    }
    else {
        $("#Postcode").attr('disabled', false);
        $("#Postcode").val('');
    }
}

function LoginSuccess(data, status, xhr) {
    if (data.msg == 'Success') {
        UpdateHeaderFooter('/Home/DisplayNewHeader');
        UpdateMenuTop();

        GetGameCategory('Most Popular');
        $('#myaccountlink').show();
        $.get('/GameInfo/GetLoggedInView?game=' + getParameterByName('game'), function (c) {
            $("#gameinfolist").html(c);
        });
        $.get('/Home/UpdateSideBanner', function (c) {
            $("#sidebanner").html(c);
        })
        $.get('/Jackpots/GetJackpots', function (c) {
            $("#jackpotslist").html(c);
        })
        $.get('/Home/GetFlashAds', function (c) {
            $("#flashcarousel").html(c);
        })
    }
    if (data.msg == 'Error') {
        $.unblockUI();
        jAlert("Operation Failed. \n" + data.error, "Error");
    }
}
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function FormFailed(returnUrl) {
    $.unblockUI();
    jAlert('Problem with application. Please contact Customer Service', "Error");
}

function CultureChange(returnUrl) {
    $.get('/Home/ChangeCulture?lang=' + $('#SelectedCulture').val(), function (c) {
        window.open(returnUrl, '_parent');
    });

}

function SubmitForm() {
    $.blockUI({
        message: '<img src="../../Content/Images/ajax-loader-home.gif" />',
        css: {
            width: '100px',
            height: '100px'
        }
    });
}


function RegisterSuccess(data, status, xhr) {
    if (data.msg == 'Success') {
        UpdateHeaderFooter('/Home/DisplayNewHeader');
        UpdateMenuTop();
        $('.Currency').html(data.Currency);
        $("#openAccount").dialog('close');
        $('#confirmationDialog').dialog({ modal: true, dialogClass: 'confirmation' });
        var title = $("#titleConfirmation").html();
        $(".ui-dialog-titlebar").html(title);
        $("#confirmationDialog").css('minHeight', '50px');
        $("#titleConfirmation").hide();
        GetGameCategory('Most Popular');
        $('#myaccountlink').show();
        $.get('/GameInfo/GetLoggedInView?game=' + getParameterByName('game'), function (c) {
            $("#gameinfolist").html(c);
        });
        $.get('/Home/UpdateSideBanner', function (c) {
            $("#sidebanner").html(c);
        })
        $.get('/Jackpots/GetJackpots', function (c) {
            $("#jackpotslist").html(c);
        })
        $.get('/Home/GetFlashAds', function (c) {
            $("#flashcarousel").html(c);
        })
    }
    if (data.msg == 'Error') {
        $.unblockUI();
        jAlert("Operation Failed. \n" + data.error, "Error");
    }
}

function DisplayConfirmation() {
    jAlert("Registration successfully completed", "Confirmation");
}

function OpenMyAccountFromRegistration() {
    $('#confirmationDialog').dialog('close');
    $.unblockUI();
    window.open('/Account/MyAccount', '_blank', 'height=650,width=780, toolbar=no, menubar=no, location=no, resizable = yes, scrollbars = yes');
}

function OpenMyAccount(username, password) {
    $.get('/Home/CheckIsSessionExpired', function (data) {
        if (data == "True") {
            jAlert("Your session expired. Please log in again", "Information", function () {
                window.location.reload();
            });
        }
    });

    window.open('/Account/MyAccount?username='+username +'&password=' + password, '_blank', 'height=650,width=780, toolbar=yes, menubar=no, location=yes, resizable = yes, scrollbars = yes');
}

function ShowHideTerms() {
    if (document.getElementById('termstext').style.display == "none") {
        $('#termstext').show("normal").animate({ width: "555px", opacity: 1 }, 200);
    }
    else
    {
        $('#termstext').hide("normal").animate({ width: "555px", opacity: 1 }, 200);
    }
}

$(function () {
    $('#gallery a').lightBox();
});

function OpenPlaytechMyAccount() {
            window.open('/Account/PlaytechMyAccount', '_blank', 'height=650,width=780, toolbar=no, menubar=no, location=no, resizable = yes, scrollbars = yes');
        }

function areCookiesAccepted() {
    var ca = capabilities.cookies_accepted();
    /*if (ca) {
        alert('Flash Cookies Accepted!');
    } else {
        alert('Flash Cookies Not Accepted!');
    }*/
}

function setCookie(name, value) {
    var my_cookie = capabilities.set_cookie(name, value);
    /*if (my_cookie) {
        alert('Cookie Name: ' + name + "\nCookie Value: " + value);
    } else {
        alert('Oops! No cookie set.');
    }*/
}

function getCookie(name) {
    var my_cookie = capabilities.get_cookie(name);
    /*if (my_cookie) {
        alert(my_cookie);
    } else {
        alert('Oops! No cookie fetched.');
    }*/
}

function unsetCookie(name) {
    var my_cookie = capabilities.unset_cookie(name);
    /*if (my_cookie) {
        alert('Cookie deleted!');
    } else {
        alert('Oops! No cookie deleted.');
    }*/
} 
