﻿$.validator.addMethod("alpha_numericRegex", function (value, element) {
    return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
});

//Jackpot Start
function getJackpotTotal() {
    var strAnn = 29451147;

    $("#progressivejackpot").html(strAnn);
    $('#progressivejackpot').countTo({
        format: 'money',
        from: strAnn,
        to: 999999999.99,
        speed: 100000000,
        refresh_interval: 0.0002,
        on_complete: function (value) {
            $(this).hide().fadeIn(1000);
        }
    });
}

(function ($) {
    $.fn.countTo = function (options) {
        options = $.extend({}, $.fn.countTo.defaults, options || {});
        var loops = Math.ceil(options.speed / options.refresh_interval),
            increment = (options.to - options.from) / loops;
        return $(this).each(function () {
            var _this = this,
                loop_count = 0,
                value = options.from,
                interval = setInterval(update_timer, options.refresh_interval);
            function update_timer() {
                value += increment;
                loop_count++;
                if (options.format == 'money') {
                    $(_this).html('¥ ' + number_with_commas(value.toFixed(options.decimals)));
                } else {
                    $(_this).html(value.toFixed(options.decimals));
                }
                if (typeof (options.on_update) == 'function') {
                    options.on_update.call(_this, value);
                }
                if (loop_count >= loops) {
                    clearInterval(interval);
                    value = options.to;
                    if (typeof (options.on_complete) == 'function') {
                        options.on_complete.call(_this, value);
                    }
                }
            }

        });
    };
    $.fn.countTo.defaults = {
        from: 0,
        to: 100,
        speed: 1000,
        refresh_interval: 100,
        decimals: 2,
        on_update: null,
        on_complete: null
    };
})(jQuery);

function number_with_commas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
//Jackpot End

function comingsoon() {
    alert("Coming Soon");
}

function showErrorMessage(errCode, errMsg) {
    if (errCode == '99' || errCode == '100012') {
        alert(errCode + " - " + errMsg);
    }
    else {
        alert(errCode + " - " + errMsg);
    }
}

function getAnnouncement() {
    $.ajax({
        type: 'POST',
        url: '/ajaxData/proc_getAnnouncement',
        cache: false,
        dataType: 'json',
        success: function (d) {
            var ann_content = "";

            if (d.data == null)
                return false;
            if (d.data.length <= 0)
                return false;

            $.each(d.data, function (i, item) {
                var content = item.a_content;
                var start_time = item.start_time_local;

                ann_content += content + "&nbsp;&nbsp;";
            });
            $("#web_announcement").html(ann_content);

            //var rate = 0.25;
            //var charNum = $("#web_announcement").text().length;
            //var duration = charNum * rate;

            //$("#web_announcement").css("animation", "marquee " + duration + "s linear infinite");
        }
    });
}

function refreshVCode(s_name, ele_id) {
    $('#' + ele_id).attr('src', '/ajaxData/proc_captchaCode?s=' + s_name + '&v=' + Math.floor(Math.random() * 100));
}

function refreshVCodeWithClassName(s_name, ele_class) {
    $('.' + ele_class).attr('src', '/ajaxData/proc_captchaCode?s=' + s_name + '&v=' + Math.floor(Math.random() * 100));
}

function clearVCodeWithClassName(c_name) {
    $.ajax({
        type: 'POST',
        url: '/ajaxData/proc_captchaCode?c=' + c_name,
        success: function (d) {
            console.log("successfully clear " + c_name + " vcode.");
        }
    });
}

function generateQRCode(elementid, size, x) {
    $("#" + elementid).css("width", size);
    $("#" + elementid).css("height", size);
    $("#" + elementid).css("background-color", "white");
    var options = {
        render: 'image',
        text: x,
        size: size,
        background:"#FFF",
        ecLevel: 'M',
        color: '#000',
        minVersion: 1,
        quiet: 1,
        mode: 0
    };
    $("#" + elementid).qrcode(options);
}

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

function checkAgent() {
    var ag_code = getUrlParameter("up");
    if (ag_code != null && ag_code != "") {
        //*** obsolete ***
        //clearCookies("ag");
        //setCookies("ag", ag_code, 1);
        // **************
        Cookies.remove('ag');
        Cookies.set('ag', ag_code, { expires: 1 }); // expiry value is day
    }
}

//COOKIES START
function setCookies(cookiesname, cookiesvalue, cookieshour) {
    var date = new Date();
    date.setTime(date.getTime() + (cookieshour * 60 * 60 * 1000));
    var expires = ";path=/; expires=" + date.toGMTString();
    document.cookie = cookiesname + "=" + cookiesvalue + expires;
}

function clearCookies(cookiesname) {
    setCookies(cookiesname, "", -1);
}


function getCookie(cookiesname) {
    var name = cookiesname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return null;
}
//COOKIES END

function getDateTime() {
    setInterval('generateDateTime()', 1000);
}

function generateDateTime() {
    var dNow = new Date();

    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var m = dNow.getMinutes();
    var s = dNow.getSeconds();

    if (m < 10) {
        m = "0" + m;
    }
    if (s < 10) {
        s = "0" + s;
    }
    var localDate = "  " + weekday[dNow.getDay()] + ", " + dNow.getDate() + '/' + (dNow.getMonth() + 1) + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + m + ':' + s;
    $('#currentDate').text(localDate);
}

function clearCookie() {
    //createCookie('<%= common.cookiesname.WEBSITE_LANGUAGE %>', -1); //remove lang
    createCookie('view', -1); //remove desktop view
    createAgent('agentID', -1); //remove agent
    alert("cookies cleared.");
    location.reload();
}


function localeCurrency(value, defCul, ignoreDecimal) {
    var localeValue;
    var fractionDigits = "2";
    var defCulture = defCul;
    if (defCulture == null || defCulture == undefined || defCulture == '' || defCulture == 'ja-JP')
        defCulture = 'en-US';   

    if (ignoreDecimal) {
        if (isNaN(value))
            value = 0;
        fractionDigits = "0";
    }

    if (value && $.isNumeric(value)) {
        var val;
        if (typeof (value) == "string")
            val = parseFloat(value);
        else
            val = value;

        localeValue = val.toLocaleString(defCulture, {
            style: 'decimal',
            minimumFractionDigits: fractionDigits,
            maximumFractionDigits: fractionDigits
        });
    }
    else {
        var val = 0;
        localeValue = val.toLocaleString(defCulture, {
            style: 'decimal',
            minimumFractionDigits: fractionDigits,
            maximumFractionDigits: fractionDigits
        });;
    }

    return localeValue;
}

function replaceHTTPS(content) {
    var protocol = document.location.protocol;
    if (protocol == "https:") {
        if (content) {
            var newContent = content;
            var n = content.indexOf("http://file");

            if (n >= 0) {
                newContent = content.replace(/http:/g, "https:");
                return newContent;
            }
            return content;
        }
    }
    return content;
}

function Encrypt(str) {
    if (!str) str = "";
    str = (str == "undefined" || str == "null") ? "" : str;
    try {
        var key = 146;
        var pos = 0;
        ostr = '';
        while (pos < str.length) {
            ostr = ostr + String.fromCharCode(str.charCodeAt(pos) ^ key);
            pos += 1;
        }

        return ostr;
    } catch (ex) {
        return '';
    }
}

function Decrypt(str) {
    if (!str) str = "";
    str = (str == "undefined" || str == "null") ? "" : str;
    try {
        var key = 146;
        var pos = 0;
        ostr = '';
        while (pos < str.length) {
            ostr = ostr + String.fromCharCode(key ^ str.charCodeAt(pos));
            pos += 1;
        }

        return ostr;
    } catch (ex) {
        return '';
    }
}
function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes == 0) return '0 Byte';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    return (Math.round(bytes / Math.pow(1024, i) * 100) / 100) + ' ' + sizes[i];
};

String.prototype.toDecimalPoint = function (n) {
    var amt = parseFloat(this);
    var pow = parseFloat(n);
    if (!isNaN(amt) && !isNaN(pow)) { // if is number
        pow = Math.pow(10, n);
        return (Math.floor(amt * pow) / pow).toFixed(n);
    }
    else {
        amt = 0;
        return amt.toFixed(n);
    }
};

Number.prototype.toDecimalPoint = function (n) {
    var amt = parseFloat(this);
    var pow = parseFloat(n);
    if (!isNaN(amt) && !isNaN(pow)) { // if is number
        pow = Math.pow(10, n);
        return (Math.floor(amt * pow) / pow).toFixed(n);
    }
    else {
        amt = 0;
        return amt.toFixed(n);
    }
};