﻿function loadBanking(strLanguage) {
    try {
        window.open("/" + strLanguage + "/launch/banking/", "Ecash", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    } catch (e) {

    }
}

function loadCashCheck(strLanguage) {
    window.open("/" + strLanguage + "/launch/cashcheck/", "CashCheck", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function loadPlayCheck(strLanguage) {
    window.open("/" + strLanguage + "/launch/playcheck/", "PlayCheck", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function loadLoyalty(strLanguage) {
    window.open("/" + strLanguage + "/launch/loyalty/", "Loyalty", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function LoadFreeSpins(strLanguage) {
    window.open("/" + strLanguage + "/launch/freespins/", "FreeSpins", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function loadChangePassword(strLanguage) {
    window.open("/" + strLanguage + "/launch/changepassword/", "ChangePassword", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function loadMyAccount(strLanguage) {
    window.open("/" + strLanguage + "/launch/myaccount/", "MyAccount", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

function loadRecoverPassword(strLanguage) {
    window.open("/" + strLanguage + "/launch/recoverpassword/", "RecoverPassword", "width=800,height=600,toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes");
    return false;
}

//##############################
// setting of culture at runtime
//##############################
var cultureCode;
function setCulturePref(cult) {
    try {
        if ((cult == undefined) || (cult == null) || (cult == "")) cult = "en-GB";
        if (cult == "en-EN") cult = "en-GB";
        cultureCode = cult;
        $.preferCulture(cultureCode);
    } catch (err) { }
}

function setCultureFormat() {
    try {
        //format currencies
        $(".iscurrency").each(
            function () {
                var strval = String($(this).html());
                var cval = ""
                for (var i = 0; i < strval.length; i++) {
                    if ((isNaN(strval.substr(i, 1)) == false) || (strval.substr(i, 1) == ".")) cval += strval.substr(i, 1);
                }
                $(this).html($.format(parseFloat(cval), "c"));
            }
        );
    } catch (err) { }

    try {
        //format numbers
        $(".isnumber").each(
            function () {
                var strval = String($(this).html());
                var cval = ""
                for (var i = 0; i < strval.length; i++) {
                    if ((isNaN(strval.substr(i, 1)) == false) || (strval.substr(i, 1) == ".")) cval += strval.substr(i, 1);
                }
                $(this).html($.format(parseFloat(cval), "n2"));
            }
        );
    } catch (err) { }
}

//##############################
// Secure submission ov HTTPS
//##############################

// Function to use in link.onclick event
function secureSubmission(sender, secureUrl) {
    if (sender.form) {
        sender.form.action = secureUrl;
        return true;
    }

    else if (sender.action) {
        sender.action = secureUrl;
        return true;
    }

    return false;
}

//##############################
// flash external interface
//##############################
function getJCurrency(number, culture) {
    if (culture != null) {
        return $.format(parseFloat(number), "c", culture);
    } else {
        return $.format(parseFloat(number), "c");
    }
}

function getJNmber(number, culture) {
    if (culture != null) {
        return $.format(parseFloat(number), "n2", culture);
    } else {
        return $.format(parseFloat(number), "n2");
    }
}

//#################################################################################
// polling register and login windows when opened and performing function on close
//#################################################################################
var win;
var gameToPlay;
var timer;

function refreshPage() {
    post_to_url(window.location.href, { gameToLoad: '', otherAction: '' });
}

function refreshMasterPage(myForm) {
    myForm.submit();
}

// Encountered problems when adding rubyfortune.com to my trusted sites???
// Polls window to see when it closes
function checkWin(url, action, game) {
    clearTimeout(timer);

    if (!win || win.closed) {
        switch (action) {
            case 'register':
                showLoadingWindow();
                relaunchGame(location.href, '', 'register');
                break;
            case 'login':
                showLoadingWindow();
                relaunchGame(location.href, '', 'login');
                break;
            case 'play':
                relaunchGame(url, game, 'play');
                break;
            default:
                location.reload(true);
                break;
        }
    }
    else {
        var strFuncToCall = "checkWin('" + url + "', '" + action + "', '" + game + "');";
        timer = setTimeout(strFuncToCall, 500);
    }
}

function relaunchGame(url, game, action) {
    post_to_url(url, { gameToLoad: game, otherAction: action });  //post_to_url(url, { category: strCategory, subcategory: strSubCategory, page: strPage });
}

//#################################################################################
// post to url with request variables
//#################################################################################

// Usage : post_to_url(url, { category: strCategory, subcategory: strSubCategory, page: strPage });
function post_to_url(path, params, method) {
    method = method || "post";

    var form = document.createElement("form");

    //move the submit function to another variable
    //so that it doesn't get over written
    form._submit_function_ = form.submit;

    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form._submit_function_(); //call the renamed function
}

function setTitle() {
    try {
        var newtitle = document.title
        if (newtitle != null && typeof newtitle != "undefined" && newtitle.indexOf('#') != -1) {
            newtitle = newtitle.substring(0, newtitle.indexOf('#'));
            document.title = newtitle;
        }
    } catch (e) {

    }
}

//#################################################################################
// javascript debug logging
//#################################################################################

function jsDebug(newEntry) {
    try {
        writeToDebuglog(newEntry);
    } catch (e) {

    }
}

function GetViewStateSize() {
    var buf = document.forms[0]["__VIEWSTATE"].value;
    jsDebug("View state is " + buf.length + " bytes");
}

//#################################################################################
// skype plugin killer
//#################################################################################
var skypeKillAttempts = 0;
function killSkype() {
    try {
        $('.skype_pnh_print_container').attr("class", "");
        $('.skype_pnh_container').remove();
    } catch (err) { }
    skypeKillAttempts++;
    if (skypeKillAttempts < 11) {
        setTimeout("killSkype();", 500);
    }
}

//#################################################################################
//function to detect if flash plugin is installed
//#################################################################################
function detectFlash() {
    var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
    var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

    //if ((navigator.userAgent.indexOf("x64") != -1) && (isIE)) return false;               // <-- removed because x64 is supported by flash now
    if ((navigator.plugins != null && navigator.plugins.length > 0) && (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"])) {
        return true;
    } else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) {
        return true;
    } else if (isIE && isWin && !isOpera) {
        try {
            axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            return true;
        } catch (e) { return false; }
    }
    return false;
}

try { var hasFlash = detectFlash(); } catch (err) { hasFlash = false; }

function closeNoFlashBar() {
    $('#noFlashBar').fadeOut(300);
}

//#################################################################################
//doccument is ready
//#################################################################################
try {
    $(document).ready(function () {
        //kill skype:
        killSkype();

        try {
            $("object").each(function () {
                try {
                    if (!hasFlash) $(this).remove(); else $(this).css("display", "block");
                } catch (err) { }
            });
        } catch (err) { }

        if (!hasFlash) $('#noFlashBar').animate( { opacity:1.0, top: '0' }, 800, 'swing');
    });
} catch (err) { }
