var langId = GetParam("lang=id", 0);
var did = GetParam("dist=id", 1);
var primeDid = GetParam("pdist=id", 1);
var loc = GetParam("location=url", "http://www.bethedealer.com/");
var langImg = GetParam("img=url", "General/");
var dlUrl = GetParam("dl=url", "http://www.bethedealer.com/dl/did" + did + "/installcasino.exe");
var strGeneralMessage = "" //"Welcome to BeTheDealer CASINO";

var activeLink, preloadFlag = false;
loc += "IE/";
/***********************************************************************/
function ChangeOff2Hover(obj) {
    obj.src = obj.src.replace('Off', 'Hover');
}
function ChangeHover2Off(obj) {
    obj.src = obj.src.replace('Hover', 'Off');
}
function ChangeDisabled2Hover(obj) {
    obj.src = obj.src.replace('Disabled', 'Hover');
}
function ChangeHover2Disabled(obj) {
    obj.src = obj.src.replace('Hover', 'Disabled');
}
/***********************************************************************/
function flashFix(flashURL, WidthAndHeight, bgcolor, transparent, flashObjId, ctrl) {
    var sFlash = '<object id="' + flashObjId + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=5,0,0,0" ' + WidthAndHeight + ' id="ShockwaveFlash1">';
    sFlash += '<param name="movie" value="' + flashURL + '" />';
    sFlash += '<param name="quality" value="high" />';
    sFlash += '<param name="wmode" value="transparent" />';
    sFlash += '<param name="quality" value="high" />';
    sFlash += '<param name="menu" value="false" />';
    sFlash += '<param name="scale" value="noScale" />';

    sFlash += '<embed name="' + flashObjId + '" id="' + flashObjId + '" swLiveConnect="true" src="' + flashURL + '" quality="high" wmode="transparent" ' + WidthAndHeight + ' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'
    sFlash += '</object>';

    //alert(sFlash);

    if (document.getElementById(ctrl) != null)
        document.getElementById(ctrl).innerHTML = sFlash;
}
/***********************************************************************/
function TrackDownload(dlURL, trackingURL, TrackingID) {
    location.replace(dlURL);
    return false;
}
/***********************************************************************/
function CheckValidFrm(Frm) {
    var i;
    var currName, currFld, pattern;

    for (i = 0; i < Frm.length; i++) {
        currFld = Frm.elements[i];

        if (!currFld.disabled && currFld.id && currFld.id.indexOf("~") != -1 && Number(currFld.id.split("~")[1]) == 1) {
            currName = currFld.id.split("~")[0];

            switch (currFld.type) {
                case "text":
                    pattern = /\w+\W*/
                    if (currFld.name == "Email")
                        pattern = /^(\w+((-\w+)|(\.\w+))*){2,30}\@((\w(\.?))+(-\w+)*){2,20}\.[a-zA-Z]{2,7}((\.[a-zA-Z]{2,10})?)$/
                    else if (currFld.name == "Username")
                        pattern = /^[a-zA-Z0-9_]{5,10}$/

                    if (!pattern.test(currFld.value))
                        return DisableAction(currFld, "Valid " + currName + " is required");
                    break;

                case "password":
                    pattern = /^\w{6,10}$/
                    if (!pattern.test(currFld.value))
                        return DisableAction(currFld, "Valid " + currName + " is required");

                    else if (currFld.value.toLowerCase() != Frm.ConfPassword.value.toLowerCase())
                        return DisableAction(Frm.ConfPassword, "Password is not confirmed correctly");
                    break;

                case "textarea":
                    pattern = /\w*\W*/
                    if (currFld.name == "SiteDesc")
                        pattern = /\w+\W*/

                    if (!pattern.test(currFld.value))
                        return DisableAction(currFld, currName + " is required");

                    if (currFld.value.length > 250)
                        return DisableAction(currFld, currName + " must not exceed 250 characters");
                    break;

                case "select-one":
                    if (currFld.name != "State" && currFld.selectedIndex <= 0)
                        return DisableAction(currFld, "Valid " + currName + " is required");

                    if (currFld.name == "Country" && (currFld.value == "US" || currFld.value == "CA") && Frm.State.selectedIndex == 0)
                        return DisableAction(Frm.State, "Valid State is required");


                    break;
            }

        }
    }

    if (Frm.Country) {
        Frm.Country.options[Frm.Country.selectedIndex].value = Frm.Country.options[Frm.Country.selectedIndex].text;
        Frm.State.options[Frm.State.selectedIndex].value = Frm.State.options[Frm.State.selectedIndex].text;
    }
}
/***********************************************************************/
function ToggleState(Ctrl) {
    var Frm = Ctrl.form
    if (Frm.State)
        Frm.State.selectedIndex = 0;

    if (Ctrl) {

        var sList = Frm.AllStates
        var cs = Frm.State
        cs.length = 1
        cs.disabled = true
        if (Ctrl.value == "US" || Ctrl.value == "CA") {
            cs.disabled = false;
            for (i = sList.length - 1; i >= 0; i--) {
                if (sList.options[i].value.indexOf("~" + Ctrl.value) != -1) {
                    sListArr = sList.options[i].value.split("~");
                    stateId = sListArr[0];


                    cState = new Option(sList.options[i].text, stateId);
                    cs.add(cState, 1);

                    if (cState.value == Frm.cState.value)
                        cState.selected = true;
                }
            }

            cs.options[0].value = "N/A";

            //document.all.StateRow.style.display = "block";
        }
        else {
            cs.selectedIndex = 0;
            //document.all.StateRow.style.display = "none";
        }
    }
}
/***********************************************************************/
function DisableAction(Ctrl, Msg) {
    alert(Msg);
    if (Ctrl.type.indexOf("select") == -1)
        Ctrl.select();
    Ctrl.focus();
    return false;
}
/***********************************************************************/
function GetParam(strCk, retVal) {
    var str = document.cookie
    var start = str.indexOf(strCk)
    if (start == -1)
        return retVal;
    else {
        start += (strCk.length + 1);
        var end = str.indexOf(";", start)
        if (end == -1)
            end = str.length;

        var val = unescape(str.substring(start, end))
        return ((val == "" || val == "undefined") ? retVal : val);
    }
}
/***********************************************************************/
function GetUrlParam(win, param, def) {
    var paramArr, val = def;

    if (win.location.href.toLowerCase().indexOf(param + "=") != -1) {

        paramArr = win.location.href.split("&")
        if (paramArr.length > 1 && paramArr.length != "undefined") {
            for (var i = 0; i < paramArr.length; i++) {
                if (paramArr[i].toLowerCase().indexOf(param + "=") != -1)
                    val = paramArr[i].split("=")[1];
            }
        }
        else
            val = win.location.href.toLowerCase().split(param + "=")[1];

    }

    if (val == "")
        val = def;
    return val;
}
/***********************************************************************/
function findObj(n, d) { //v3.0
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n, d.layers[i].document); return x;
}

function swapImgRestore() { //v3.0
    var i, x, a = document.sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function swapImage() { //v3.0
    var i, j = 0, x, a = swapImage.arguments; document.sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = findObj(a[i])) != null) { document.sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
/***********************************************************************/
function CenterPopUp(Url, WindowWidth, WindowHeight) {
    var WindowTop = (screen.availHeight / 2) - (WindowHeight / 2);
    var WindowLeft = (screen.availWidth / 2) - (WindowWidth / 2);
    var WindowProps = 'toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + WindowWidth + ',height=' + WindowHeight + ',left=' + WindowLeft + ',top=' + WindowTop + ',screenx=' + WindowLeft + ',screeny=' + WindowTop;
    PopUpWindow = window.open(Url, "", WindowProps);
}
/***********************************************************************/
function CenterPopUpNoScrollBars(Url, WindowWidth, WindowHeight) {
    var WindowTop = (screen.availHeight / 2) - (WindowHeight / 2);
    var WindowLeft = (screen.availWidth / 2) - (WindowWidth / 2);
    var WindowProps = 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + WindowWidth + ',height=' + WindowHeight + ',left=' + WindowLeft + ',top=' + WindowTop + ',screenx=' + WindowLeft + ',screeny=' + WindowTop;
    PopUpWindow = window.open(Url, "", WindowProps);
}
/***********************************************************************/
function resizeWin(WindowWidth, WindowHeight) {
    var WindowTop = (screen.Height / 2) - (WindowHeight / 2);
    var WindowLeft = (screen.Width / 2) - (WindowWidth / 2);

    window.resizeTo(WindowWidth, WindowHeight);
    window.moveTo(WindowLeft, WindowTop);
}
/***********************************************************************/
function PreviewPopUp(WhatImage) {
    var Url = 'Preview_Window.html?WhatImage=' + WhatImage;
    var WindowWidth = 405;
    var WindowHeight = 393;
    var WindowTop = (screen.availHeight / 2) - (WindowHeight / 2);
    var WindowLeft = (screen.availWidth / 2) - (WindowWidth / 2);
    var WindowProps = 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + WindowWidth + ',height=' + WindowHeight + ',left=' + WindowLeft + ',top=' + WindowTop + ',screenx=' + WindowLeft + ',screeny=' + WindowTop;
    PreviewWindow = window.open(Url, "PreviewWindow", WindowProps);
    if (PreviewWindow.opener == null) PreviewWindow.opener = self;
    return;
}
/***********************************************************************/
function WindowOpenSelect(PageToOpen) {
    var dest = PageToOpen.options[PageToOpen.selectedIndex].value;
    if (dest == "0") {
        PageToOpen.selectedIndex = 0;
        return;
    }
    window.location.href = dest;
    PageToOpen.selectedIndex = 0;
}
/***********************************************************************/
function ToggleHiddenField(FieldId) {
    if (document.getElementById(FieldId) != null) {
        if (document.getElementById(FieldId).style.display == 'none') {
            document.getElementById(FieldId).style.display = '';
        }
        else {
            document.getElementById(FieldId).style.display = 'none';
        }
    }
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Functions to Hide/Show Hidden Areas using Smooth Scrolling.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var smooth_timer;

function servOC(i, nColor) {
    var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
    var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
    var ifObj = (document.getElementById) ? document.getElementById('ihif' + i) : eval("document.all['ihif" + i + "']");
    if (trObj != null) {
        if (trObj.style.display == "none") {
            ifObj.style.height = "0px";
            trObj.style.display = "";
            nameObj.style.background = "#FFFFFF";
            smoothHeight('ihif' + i, 0, 40, 10, 'o');
        }
        else {
            nameObj.style.background = nColor;
            smoothHeight('ihif' + i, 40, 0, 10, 'ihtr' + i);
        }
    }
}
/***********************************************************************/
function smoothHeight(id, curH, targetH, stepH, mode) {
    diff = targetH - curH;
    if (diff != 0) {
        newH = (diff > 0) ? curH + stepH : curH - stepH;
        ((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
        if (smooth_timer) window.clearTimeout(smooth_timer);
        smooth_timer = window.setTimeout("smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 16);
    }
    else if (mode != "o")
        ((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display = "none";
}
/***********************************************************************/
function rowOver(i, nColor) {
    if (!nColor) nColor = "#FFFFFF";
    var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
    if (nameObj != null) nameObj.style.background = nColor;
}
/***********************************************************************/
function rowOut(i, nColor) {
    var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
    var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
    if (trObj == null || trObj.style.display == "none") nameObj.style.background = nColor;
}
/***********************************************************************/
/***********************************************
* Pausing up-down scroller- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function pausescroller(content, divId, divClass, delay) {
    this.content = content //message array content
    this.tickerid = divId //ID of ticker div to display information
    this.delay = delay //Delay between msg change, in miliseconds.
    this.mouseoverBol = 0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
    this.hiddendivpointer = 1 //index of message array for hidden div
    document.write('<div id="' + divId + '" class="' + divClass + '" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="' + divId + '1">' + content[0] + '</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="' + divId + '2">' + content[1] + '</div></div>')
    var scrollerinstance = this
    if (window.addEventListener) //run onload in DOM2 browsers
        window.addEventListener("load", function() { scrollerinstance.initialize() }, false)
    else if (window.attachEvent) //run onload in IE5.5+
        window.attachEvent("onload", function() { scrollerinstance.initialize() })
    else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
        setTimeout(function() { scrollerinstance.initialize() }, 500)
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

pausescroller.prototype.initialize = function() {
    this.tickerdiv = document.getElementById(this.tickerid)
    this.visiblediv = document.getElementById(this.tickerid + "1")
    this.hiddendiv = this.hiddendiv = document.getElementById(this.tickerid + "2")
    this.visibledivtop = parseInt(pausescroller.getCSSpadding(this.tickerdiv))
    //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
    this.visiblediv.style.width = this.hiddendiv.style.width = this.tickerdiv.offsetWidth - (this.visibledivtop * 2) + "px"
    this.getinline(this.visiblediv, this.hiddendiv)
    this.hiddendiv.style.visibility = "visible"
    var scrollerinstance = this
    document.getElementById(this.tickerid).onmouseover = function() { scrollerinstance.mouseoverBol = 1 }
    document.getElementById(this.tickerid).onmouseout = function() { scrollerinstance.mouseoverBol = 0 }
    if (window.attachEvent) //Clean up loose references in IE
        window.attachEvent("onunload", function() { scrollerinstance.tickerdiv.onmouseover = scrollerinstance.tickerdiv.onmouseout = null })
    setTimeout(function() { scrollerinstance.animateup() }, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup = function() {
    var scrollerinstance = this
    if (parseInt(this.hiddendiv.style.top) > (this.visibledivtop + 5)) {
        this.visiblediv.style.top = parseInt(this.visiblediv.style.top) - 5 + "px"
        this.hiddendiv.style.top = parseInt(this.hiddendiv.style.top) - 5 + "px"
        setTimeout(function() { scrollerinstance.animateup() }, 50)
    }
    else {
        this.getinline(this.hiddendiv, this.visiblediv)
        this.swapdivs()
        setTimeout(function() { scrollerinstance.setmessage() }, this.delay)
    }
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivs = function() {
    var tempcontainer = this.visiblediv
    this.visiblediv = this.hiddendiv
    this.hiddendiv = tempcontainer
}

pausescroller.prototype.getinline = function(div1, div2) {
    div1.style.top = this.visibledivtop + "px"
    div2.style.top = Math.max(div1.parentNode.offsetHeight, div1.offsetHeight) + "px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage = function() {
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        setTimeout(function() { scrollerinstance.setmessage() }, 100)
    else {
        var i = this.hiddendivpointer
        var ceiling = this.content.length
        this.hiddendivpointer = (i + 1 > ceiling - 1) ? 0 : i + 1
        this.hiddendiv.innerHTML = this.content[this.hiddendivpointer]
        this.animateup()
    }
}

pausescroller.getCSSpadding = function(tickerobj) { //get CSS padding value, if any
    if (tickerobj.currentStyle)
        return tickerobj.currentStyle["paddingTop"]
    else if (window.getComputedStyle) //if DOM2
        return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
    else
        return 0
}
/***********************************************************************/
function validateSMSform(isSubmit) {
    var pattern, currFld, fldsToValidate, i;

    //Fields will be validated in this order
    fldsToValidate = Array("username", "password", "prefix", "mobphone", "MaxMessages");

    for (i = 0; i < fldsToValidate.length; i++) {
        currFld = document.getElementById(fldsToValidate[i]);

        if (document.getElementById(fldsToValidate[i] + "Err") != null) {
            document.getElementById(fldsToValidate[i] + "Err").style.display = 'none';
        }

        switch (currFld.id) {
            case "username":
                pattern = /^\w{5,10}$/
                break;
            case "password":
                pattern = /^\w{6,10}$/
                break;
            case "prefix":
                if (currFld.selectedIndex == 0)
                    return ShowValidationError(currFld);
                break;
            case "mobphone":
                pattern = /^\d{8,16}$/;
                break;
            case "MaxMessages":
                pattern = /^\d*$/;
                if (currFld.value < 1 || currFld.value > 31)
                    return ShowValidationError(currFld);
                break;
        }

        if (currFld.type == "text" || currFld.type == "password") {
            if (!pattern.test(currFld.value))
                return ShowValidationError(currFld);
        }
    }

    //Get ALL Input type fields
    var InputFields = document.getElementsByTagName('input');
    var CBSel = false;

    //Test if at least ONE checkbox has been selected
    for (i = 0; i < InputFields.length; i++) {
        if (InputFields[i].type == "checkbox" && InputFields[i].checked) {
            CBSel = true;
            break;
        }
    }

    if (!CBSel) {
        document.getElementById('servicesErr').style.display = '';
        return false;
    }
    else {
        document.getElementById('servicesErr').style.display = 'none';
    }

    if (isSubmit) {
        window.setTimeout("ShowWaitingImage();", 0);
        location.href = '#Waiting';
        CreateMD5str(document.getElementById('username').value, document.getElementById('password').value);
    }

    document.getElementById('validationErr').style.display = 'none';
    return true;
}
/***********************************************************************/
function ShowWaitingImage() {
    if (document.getElementById('Waiting') != null) {
        document.getElementById('Waiting').src = ImagePath + 'PleaseWait.gif';
    }
}
/***********************************************************************/
function ShowValidationError(Ctrl) {
    if (Ctrl == null) {
        if (document.getElementById('validationErr') != null) {
            document.getElementById('validationErr').style.display = '';
            location.href = '#validationErr';
        }
    }
    else {
        if (document.getElementById(Ctrl.id + "Err") != null) {
            document.getElementById(Ctrl.id + "Err").style.display = '';
        }
    }
    return false;
}
/***********************************************************************/
function updC(val) {
    val = document.getElementById('prefix').options[document.getElementById('prefix').selectedIndex].text
    if (document.getElementById('cntry') != null) {
        document.getElementById('cntry').value = val;
    }
}
/***********************************************************************/
function CreateMD5str(usr, pwd) {
    document.getElementById('enc').value = calcMD5(usr.toLowerCase() + pwd);
}
/***********************************************************************/
var hex_chr = "0123456789ABCDEF";
function rhex(num) {
    str = "";
    for (j = 0; j <= 3; j++)
        str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr.charAt((num >> (j * 8)) & 0x0F);
    return str;
}
function str2blks_MD5(str) {
    nblk = ((str.length + 8) >> 6) + 1;
    blks = new Array(nblk * 16);
    for (i = 0; i < nblk * 16; i++) blks[i] = 0;
    for (i = 0; i < str.length; i++)
        blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
    blks[i >> 2] |= 0x80 << ((i % 4) * 8);
    blks[nblk * 16 - 2] = str.length * 8;
    return blks;
}
function add(x, y) {
    var lsw = (x & 0xFFFF) + (y & 0xFFFF);
    var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    return (msw << 16) | (lsw & 0xFFFF);
}
function rol(num, cnt) {
    return (num << cnt) | (num >>> (32 - cnt));
}
function cmn(q, a, b, x, s, t) {
    return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t) {
    return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
    return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
    return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
    return cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function calcMD5(str) {
    x = str2blks_MD5(str);
    a = 1732584193;
    b = -271733879;
    c = -1732584194;
    d = 271733878;

    for (i = 0; i < x.length; i += 16) {
        olda = a;
        oldb = b;
        oldc = c;
        oldd = d;

        a = ff(a, b, c, d, x[i + 0], 7, -680876936);
        d = ff(d, a, b, c, x[i + 1], 12, -389564586);
        c = ff(c, d, a, b, x[i + 2], 17, 606105819);
        b = ff(b, c, d, a, x[i + 3], 22, -1044525330);
        a = ff(a, b, c, d, x[i + 4], 7, -176418897);
        d = ff(d, a, b, c, x[i + 5], 12, 1200080426);
        c = ff(c, d, a, b, x[i + 6], 17, -1473231341);
        b = ff(b, c, d, a, x[i + 7], 22, -45705983);
        a = ff(a, b, c, d, x[i + 8], 7, 1770035416);
        d = ff(d, a, b, c, x[i + 9], 12, -1958414417);
        c = ff(c, d, a, b, x[i + 10], 17, -42063);
        b = ff(b, c, d, a, x[i + 11], 22, -1990404162);
        a = ff(a, b, c, d, x[i + 12], 7, 1804603682);
        d = ff(d, a, b, c, x[i + 13], 12, -40341101);
        c = ff(c, d, a, b, x[i + 14], 17, -1502002290);
        b = ff(b, c, d, a, x[i + 15], 22, 1236535329);

        a = gg(a, b, c, d, x[i + 1], 5, -165796510);
        d = gg(d, a, b, c, x[i + 6], 9, -1069501632);
        c = gg(c, d, a, b, x[i + 11], 14, 643717713);
        b = gg(b, c, d, a, x[i + 0], 20, -373897302);
        a = gg(a, b, c, d, x[i + 5], 5, -701558691);
        d = gg(d, a, b, c, x[i + 10], 9, 38016083);
        c = gg(c, d, a, b, x[i + 15], 14, -660478335);
        b = gg(b, c, d, a, x[i + 4], 20, -405537848);
        a = gg(a, b, c, d, x[i + 9], 5, 568446438);
        d = gg(d, a, b, c, x[i + 14], 9, -1019803690);
        c = gg(c, d, a, b, x[i + 3], 14, -187363961);
        b = gg(b, c, d, a, x[i + 8], 20, 1163531501);
        a = gg(a, b, c, d, x[i + 13], 5, -1444681467);
        d = gg(d, a, b, c, x[i + 2], 9, -51403784);
        c = gg(c, d, a, b, x[i + 7], 14, 1735328473);
        b = gg(b, c, d, a, x[i + 12], 20, -1926607734);

        a = hh(a, b, c, d, x[i + 5], 4, -378558);
        d = hh(d, a, b, c, x[i + 8], 11, -2022574463);
        c = hh(c, d, a, b, x[i + 11], 16, 1839030562);
        b = hh(b, c, d, a, x[i + 14], 23, -35309556);
        a = hh(a, b, c, d, x[i + 1], 4, -1530992060);
        d = hh(d, a, b, c, x[i + 4], 11, 1272893353);
        c = hh(c, d, a, b, x[i + 7], 16, -155497632);
        b = hh(b, c, d, a, x[i + 10], 23, -1094730640);
        a = hh(a, b, c, d, x[i + 13], 4, 681279174);
        d = hh(d, a, b, c, x[i + 0], 11, -358537222);
        c = hh(c, d, a, b, x[i + 3], 16, -722521979);
        b = hh(b, c, d, a, x[i + 6], 23, 76029189);
        a = hh(a, b, c, d, x[i + 9], 4, -640364487);
        d = hh(d, a, b, c, x[i + 12], 11, -421815835);
        c = hh(c, d, a, b, x[i + 15], 16, 530742520);
        b = hh(b, c, d, a, x[i + 2], 23, -995338651);

        a = ii(a, b, c, d, x[i + 0], 6, -198630844);
        d = ii(d, a, b, c, x[i + 7], 10, 1126891415);
        c = ii(c, d, a, b, x[i + 14], 15, -1416354905);
        b = ii(b, c, d, a, x[i + 5], 21, -57434055);
        a = ii(a, b, c, d, x[i + 12], 6, 1700485571);
        d = ii(d, a, b, c, x[i + 3], 10, -1894986606);
        c = ii(c, d, a, b, x[i + 10], 15, -1051523);
        b = ii(b, c, d, a, x[i + 1], 21, -2054922799);
        a = ii(a, b, c, d, x[i + 8], 6, 1873313359);
        d = ii(d, a, b, c, x[i + 15], 10, -30611744);
        c = ii(c, d, a, b, x[i + 6], 15, -1560198380);
        b = ii(b, c, d, a, x[i + 13], 21, 1309151649);
        a = ii(a, b, c, d, x[i + 4], 6, -145523070);
        d = ii(d, a, b, c, x[i + 11], 10, -1120210379);
        c = ii(c, d, a, b, x[i + 2], 15, 718787259);
        b = ii(b, c, d, a, x[i + 9], 21, -343485551);

        a = add(a, olda);
        b = add(b, oldb);
        c = add(c, oldc);
        d = add(d, oldd);
    }
    return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}

if (typeof (Number) != "undefined" && typeof (Number.prototype) != "undefined" && typeof (Number.prototype.toFixed) == "undefined") {
    function Number_toFixed(d) {
        var n = this;
        d = d || 2;
        var f = Math.pow(10, d);
        n = Math.round(n * f) / f;
        n += Math.pow(10, -(d + 1));
        n += '';
        return (d == 0 ? n.substring(0, n.indexOf('.')) : n.substring(0, n.indexOf('.') + d + 1));
    }
    Number.prototype.toFixed = Number_toFixed;
}