/**
 * Format a number as US currency
 *
 * Accepts a number or string and formats it like U.S. currency. Adds the
 * dollar sign, rounds to two places past the decimal, adds place holding
 * zeros, and commas where appropriate.
 *
 * @author  Cyanide_7 (leo7278@hotmail.com)
 * Web Site:  http://www7.ewebcity.com/cyanide7
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num/100).toString();
    if(cents < 10) {
        cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
        num = num.substring(0, num.length-(4*i+3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}



/**
 * Get the value of a specified browser cookie
 */
function getCookie(name) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

/**
 * Set the value of a specified browser cookie
 */
function setCookie(name, value, expires, path, domain, secure) {
    if (expires) {
        var today = new Date();
        today.setTime( today.getTime() );
                expires = expires * 1000 * 60 * 60 * 24;    
        var expires_date = new Date( today.getTime() + (expires) );
    }
    document.cookie = name + '=' + escape(value) +
        ( (expires) ? ';expires=' + expires_date.toGMTString() : '' ) +
        ( (path) ? ';path=' + path : '' ) +
        ( (domain) ? ';domain=' + domain : '' ) +
        ( (secure) ? ';secure' : '' );
}


/**
 * Remove a specified browser cookie
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + '=' +
            ( ( path ) ? ';path=' + path : '') +
            ( ( domain ) ? ';domain=' + domain : '' ) +
            ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    }
}

/**
 *  Preloads images for those that will be swapped via JavaScript
 */
function preloadImage(imageName) { 
    var image = new Image();
    image.src = imageName;
    return image;
}

/**
 * Pre-fetches an image file from the server before it needs to be displayed
 * thus getting it into the cache
 */
function fetchImage(url) {
    var image = new Image(); 
    image.src = url;
    return image;
}

function checkForClass(elem, className ) {
    return $(elem.id).hasClassName();
}

/**
 *  Simple function to add or remove classes to elements
 */
function changeClass(action, elementId, class1, class2)
{
    element = document.getElementById(elementId);

    switch (action) {
        case 'add':
            // Only add it if it's not already there
            if(!changeClass('check', elementId, class1)) {
                element.className += (element.className ? ' ' + class1 : class1);
            }
            break;
        case 'remove':
            var replacement = (element.className.match(' ' + class1) ? ' ' + class1 : class1);
            element.className = element.className.replace(replacement, '');
            break;
        case 'check':
            return new RegExp('\\b' + class1 + '\\b').test(element.className);
            break;
    }
}



/**
 * Checks a given class attribute for the presence of a given class
 */
function checkForClass(elementClass, value) {
    return new RegExp('\\b' + value + '\\b').test(elementClass);
}



/**
 * Gets the first child node of the given node with the specified name (tag name)
 */
function getChildByName(currentNode, elementName) {
    var children = currentNode.childNodes;
    elementName = elementName.toLowerCase();

    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType == 1 && children[i].nodeName.toLowerCase() == elementName) {
            return children[i];
        }
    }
    return false;
}


/**
 * Gets the first child node of the given node with the specified class
 */
function getChildByClass(currentNode, elementClass) {
    var children = currentNode.childNodes;

    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType == 1 && checkForClass(children[i].className, elementClass) ) {
            return children[i];
        }
    }
    return false;
}


/**
 * Gets the next sibling node with the specified element name (tag name)
 */
function getNextByName(currentNode, elementName) {
    var nextNode = currentNode.nextSibling;
    elementName = elementName.toLowerCase();

    while(nextNode.nodeType != 1 && nextNode.nodeName.toLowerCase() != elementName) {
        nextNode = nextNode.nextSibling;
    }

    return nextNode;
}



/**
 * Gets the first child text node within a specified node
 */
function getChildTextNode(currentNode) {
    var children = currentNode.childNodes;

    for (var i = 0; i < children.length; i++) {
        if (children[i].nodeType == 3) {
            return children[i];
        }
    }

    return false;
}




/**
 * Gets the first text node to appear after the specified node
 */
function getNextTextNode(currentNode) {
    var nextNode = currentNode.nextSibling;

    while(nextNode.nodeType != 3) {
        nextNode = nextNode.nextSibling;
    }

    return nextNode;
}


