﻿// JScript File

    // ADS Application Busy response
    var rotate_delay = 250; // delay in milliseconds (5000 = 5 secs)
    var current = 3;
    var currentImage = "gear_ani_3.gif";
    var imageSource;
    function rotate() {
        current = (current == 3) ? 0 : current+1;
        switch (currentImage){
            case "gear_ani_1.gif":
                current = 2;
                currentImage = "gear_ani_2.gif";
                break;
            case "gear_ani_2.gif":
                current = 3;
                currentImage = "gear_ani_3.gif";
                break;
            case "gear_ani_3.gif":
                current = 0;
                currentImage = "gear_ani_1.gif";
                break;
        }
        imageSource = "https://www.sportsmansguide.com/net/checkout/images/" + currentImage;
        document.images.show.src = imageSource;
        document.getElementById("divBusyBox").style.visibility = "visible";
        window.setTimeout("rotate()", rotate_delay);
    }

    var rotate_color_delay = 250; // delay in milliseconds (5000 = 5 secs)
    var rotateCount = 0; // allow color to rotate a limited number of times
    var color = "";
    var currR = 255;
    var currG = 255;
    var currB = 0;

    // convert a single digit (0 - 16) into hex
    function enHex(aDigit)
    {
        return("0123456789ABCDEF".substring(aDigit, aDigit+1))
    }

    // Convert a 24bit number to hex
    function toHex(n)
    {
        return (enHex((0xf00000 & n) >> 20) +
                enHex((0x0f0000 & n) >> 16) +
                enHex((0x00f000 & n) >> 12) +
                enHex((0x000f00 & n) >>  8) +
                enHex((0x0000f0 & n) >>  4) +
                enHex((0x00000f & n) >>  0))
    }

    function rotateColor() {
        //if (currG > 256 || currG == 0)
        //{
        //    alert("currG=" + currG);
        //}
        currG = (currG < 200) ? 255 : currG-50;
        color = '#' + toHex(currR).substring(4,6) + toHex(currG).substring(4,6) + toHex(currB).substring(4,6);
        //alert("currG=" + currG + "; color=" + color);
        document.getElementById("divCongratulations").style.background = color;
        
        // limit rotate count
        rotateCount += 1;
        if (rotateCount < 18) // Stops on yellow highlight
        {
            window.setTimeout("rotateColor()", rotate_color_delay);
        }
    }

    function selectedIndexChangeRefocus(callingObject, callingObjectId, focusId) {
        if (document.all || document.getElementById) {
            // find respective button
            var callingObjectUniqueId = callingObject.id;
            var focusUniqueId = callingObjectUniqueId.replace(callingObjectId, focusId);
            
            // iterate page controls
            var focusObject;
            var focusObjectFound = true;
            focusObject = document.getElementById(focusUniqueId);

            if (focusObjectFound) {
                // focus on the next field
                focusObject.focus();
            }
        }
        
        return false;
    }

    function keyPressHandlerGeneric(callingObject, callingObjectId, buttonId, e) {
        // process only the Enter key; trigger respective submit button
        
        // fetch keycode pressed
        var keycode = "";
        if (e.keyCode) {
            keycode=e.keyCode;
        } else {
            keycode=e.which;
        }

        if (keycode == 13)
        {
            //alert("Enter key pressed!");
            // find respective submit button
            var callingObjectUniqueId = callingObject.id;
            var buttonUniqueId = callingObjectUniqueId.replace(callingObjectId, buttonId);
            var buttonObject;
            buttonObject = document.getElementById(buttonUniqueId);

            // submit the form by programmatically clicking the specified button
            buttonObject.click();
            
            // cancel the default submit
            return false;
        }
    }
