/**
 * swap an image
 * @param      string      image id
 * @param      string      image to swap to
 */
function swapImage (sId, sImg) {
    var oObj = document.getElementById(sId);
    if (oObj) {
        oObj.src = sImg;
        // let's preload as it's called
        preloadImages(sImg);
    }
}

/**
 * Global rollover image preloader function
 * @param      string      image url string
 */
var oImgs    = new Object();
function preloadImages (sImg) {
    var sId = sImg.replace(/[^a-zA-z0-9]/g, '');
    if (document.images) {
        // if it's already in the object return
        if (sId in oImgs) {
            return;
        } else {
            oImgs[sId] = new Image();
            oImgs[sId].src = sImg;
        }
    }

}

/**
 * Renders a specific about us image and caption
 * @param int index of image to display
 * @param string caption to display under image
 */
function showAboutUs(iImg, sCaption) {
    var oRightImg = document.getElementById('join_us_right_img');

    for (var i = 1; i <= 28; i++) {
        var oImgThumb = document.getElementById('join_us_' + i);
        oImgThumb.src = '/images/join_us/' + i + '_thumb.jpg';
    }

    var oImg = document.getElementById('join_us_' + iImg);
    oImg.src = '/images/join_us/' + iImg + '_thumb_over.jpg';

    oRightImg.src = '/images/join_us/' + iImg + '.jpg';

    var oCaption = document.getElementById('join_us_caption_content');
    oCaption.innerHTML = sCaption;

}

/**
 * Check the length of a text area and limit it's input
 * @param      string       text area id
 * @param      int          count limit
 * @param      string       id of HTML element target for display remaining count
 */
function checkLength (sCheckId, nCount, sTargetId) {
    var oCheck        = document.getElementById(sCheckId);
    var oTarget       = document.getElementById(sTargetId);

    if (oCheck && oTarget) {
        var nCheckLength  = oCheck.value.length;
        var nRemaining    = nCount - oCheck.value.length;
        oTarget.innerHTML = nRemaining;
        if (nRemaining < 0) {
            oCheck.value = oCheck.value.substring(0, (nCount - 1));
            checkLength(sCheckId, nCount, sTargetId);
        }
    }
}

/**
 * Draw stuff to a HTML element with innerHTML
 * @param      string      id to draw too
 * @param      array       array of text/HTML to draw to the screen
 */
function drawStuff (sId, aH) {
    var oObj = document.getElementById(sId);
    if (oObj) {
        oObj.innerHTML = aH.join("\n");
    }
}

/**
 * Find out if a value is in an array
 * @param      string      needle, value you are looking for
 * @param      array       haystack, array you are looking for the value in
 * @return     boolean
 */
function inArray (sNeedle, aHaystack)
{
    var bReturn = false;
    var nLength = aHaystack.length;
    for (var i = 0; i < nLength; i++)
    {
        if (sNeedle == aHaystack[i])
        {
            bReturn = true;
            break;
        }
    }
    return bReturn;
}

/**
 * Print out a hash pretty style to the writeDebug function
 * @param       array       array to print out
 */
function prettyPrint (oObj)
{
    for (var key in oObj)
    {
        if (typeof oObj[key] == 'object')
        {
            prettyPrint(oObj[key]);
        }
        else
        {
            writeDebug(key + ' => ' + oObj[key]);
        }
    }
}

/**
 * Used to change the view and limit phones to certain carriers
 * @param      int      carrier
 */
function setCarrier (sValue) {
    location.href = '/products/TellmeByMobile/SupportedPhones/' + sValue;
}

/**
 * Set the focus to the supplied element by id
 * @param      string      element id
 */
function setFocus (sValue) {
    var oObj = document.getElementById(sValue);
    if (oObj) oObj.focus();
}

/**
 * Used to change the view and limit users shown to certain programs
 * @param      int      program id
 */
function setProgram (sValue) {
    if (sValue && sValue != 0) {
        location.href = 'admin_users.php?program_id=' + sValue;
    } else {
        location.href = 'admin_users.php';
    }
}

/**
 * Toggle the display of an element from block to none and visa versa
 * @param        string        id of element to toggle
 * @param        boolean       true='block' | false='none'
 */
function toggleDisplay (sId, bValue) {
    var oObj = document.getElementById(sId);
    if (oObj) oObj.style.display = (bValue) ? 'block' : 'none';
}

/**
 * Write out text to the screen to the debug element
 * @param       string        debug string
 */
function writeDebug (sDebug)
{
    //return;
    var obj = document.getElementById('debug');
    if (typeof(sDebug) == 'string') sDebug = sDebug.replace(/\n/g, '<br />');
    if (obj) obj.innerHTML += '<br />' + sDebug;
}

/**
 * Draw the phone type picture popup
 */
function showPhoneHelp(nPhoneId, sHelpType) {
    window.open(
        '/products/TellmeByMobile/help/' + sHelpType + '/' + nPhoneId, 'Help',
        'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=600, height=514, top=200, left=200'
    ).focus();
}

