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

/**
 * Selects the first text field on a page.
 */
function selectFirstText()
{
    var elements = document.getElementsByTagName('input');
    for ( i=0;i<elements.length;i++ )
    {
        if ( elements[i].getAttribute('type') && elements[i].type != null && elements[i].type == 'text' )
        {
            elements[i].activate();
            break;
        }
    }
}

/**
 * Selects the <select> option of element that matches the
 * value provided.
 */
function selectValue(element, value)
{
    for( i = 0; i < element.options.length; i++ )
    {
        if( element.options[i].value == value )
        {
            element.selectedIndex = i;
            return;
        }
    }
}

/**
 * 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') && !$(elements[i]).hasClassName('noautostyle') )
        {
            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' ) )
    {
        // create a hidden variable with the same name and value as the
        // submit button (so we can preserve the button's value)
        // NOTE:  uses Prototype library
        $(targ.form).insert(new Element('input', { 'type': 'hidden', 'name': targ.name, 'value': targ.value }));

        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;
}

function is_object( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   improved by: Michael White (http://getsprink.com)
    // *     example 1: is_object('23');
    // *     returns 1: false
    // *     example 2: is_object({foo: 'bar'});
    // *     returns 2: true
    // *     example 3: is_object(null);
    // *     returns 3: false
 
    if(mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}
