/**
 * Functions common to all pages.
 */

/**
 * Adds a class name to all input items based on their type.
 */
function setInputClasses()
{
    var elements = document.getElementsByTagName('input');
    for ( i=0;i<elements.length;i++ )
    {
        if ( elements[i].getAttribute('type') )
        {
            if( elements[i].type != null )
                elements[i].className += ' ' + elements[i].type;
        }
    }
}

/**
 * Adds the clearfix class to the elements that have the class
 * name(s) provided.
 */
function addClearfix( classes )
{
    for( i=0; i < classes.length; i++ )
    {
        elements = document.getElementsByClassName( classes[i] );

        for( j=0; j < elements.length; j++ )
        {
            elements[j].className += ' clearfix';
        }
    }
}

/**
 * Makes it so that all submit buttons will be
 * disable after they are clicked.
 */
function addDisableSubmitFunctionality()
{
    inputs = document.getElementsByTagName('input');

    for( i=0; i < inputs.length; i++ )
    {
        if( inputs[i].type.toLowerCase() == 'submit' && inputs[i].onclick == undefined )
        {
            inputs[i].onclick=disableMe;
        }
    }
}

/**
 * Disabled the calling button.
 */
function disableMe( e )
{
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    if( targ.type.toLowerCase() != 'submit' ) return;

    if( !hasClassName( targ, 'nodisable' ) )
    {
        targ.disabled = true;
        targ.className += ' disabled';
        targ.form.submit(); // need this for IE
    }
}


function hasClassName(objElement, strClass)
{
    // if there is a class
    if ( objElement.className )
    {
        var arrList = objElement.className.split(' ');
        var strClassUpper = strClass.toUpperCase();

        for ( var i = 0; i < arrList.length; i++ )
        {
            if ( arrList[i].toUpperCase() == strClassUpper )
                return true;
        }
    }

    return false;
}
