/*********
* form.js
*
* purpose: 
*   - Automatically hide default values when an element is clicked
*   - If the values in the fields on submit are the same as default, it will clear them out
*
* notes:
*   - If Ext is loaded, you only have to include this file to run.
*       - Otherwise just call "Form.hideFieldsOnclick();" in your body onload
*/


//So it will load automatically with Ext
if (typeof Ext == 'object')
{
    Ext.onReady(function(){Form.hideFieldsOnclick();});
}

//field types that will break
var skip_types = new Array(
                    'checkbox',
                    'submit',
                    'hidden',
                    'button',
                    'radio');

var Form = {

    //This is where everything gets started
    hideFieldsOnclick : function()
    {
        var forms = new Object();
        var fields = new Object();

        //Get all the forms on the page
        //  using this method prevents bad return crap
        forms = this.getObjectsByTagName('form');

        //We want to set all the inputs and textareas that are in the forms
        for (key in forms)
        {
            form = forms[key];
           
            //this relies on the forms class setting a cookie
            if(null == document.cookie.match(form.name+'=submitted'))
            {
                inputs = this.getObjectsByTagName('input',form);
                textareas = this.getObjectsByTagName('textarea',form);

                this.setFieldAttributes(inputs);
                this.setFieldAttributes(textareas);
            }
        }
        return false;
    },

    //Used to return actual elements, not iterations, or crap
    getObjectsByTagName : function(tag, p)
    {
        if (p == null)
            p = document;

        var objects = new Object();
        
        elements = p.getElementsByTagName(tag);

        for (key in elements)
        {
            element = elements[key];
            if(typeof element == 'object')
                objects[key] = element;
        }
        
        return objects;
    },

    //This method is used to set the events
    setFieldAttributes : function(fields)
    {
        for (key in fields)
        {
            field = fields[key];
            //if(field.type != 'submit' && field.type != 'radio')
            if(!this.in_array(field.type,skip_types))
            {
                //Because IE sucks, we have to use a place holder for our orginal value
                //  or we could use an eval later, but this seems to be cleaner
                field.setAttribute('org',field.value);

                //Declared like this because IE doesnt accept setting onblur ect. as setAttribute
                field.onblur = function ()
                {
                    if(this.value == '')
                        this.value = this.attributes.org.nodeValue;
                }
                field.onfocus = function ()
                {
                    if(this.value == this.attributes.org.nodeValue)
                        this.value = '';
                }
            }
            else
            {
                //This sets the onclick for the submit button so it will 
                //  clear the fields if they are the same as the orginals
                if(field.type == 'submit')
                {
                    field.onclick= function () {Form.clearDefaultValues(this)};
                }
            }   
        }
    
        return false;
    },

    //Simplistic PHP like version of in_array
    //  cause I'm lazy ;)
    in_array : function (str,arr)
    {
        for (key in arr)
        {
            test = arr[key];
            if (test == str)
            {
                return true;
            }
        }

        return false;
    },

    //This method and the ones after are used by the submit button
    //  and not called in the initialization part of the script
    clearDefaultValues : function(submit)
    {
        //You'll notice that this is very similar to hideFieldsOnclick
        // but basically is the reverse of it
        
        //Since we only have the submit button we need to get the parent form element to do anything with it 
        form = this.getParentForm(submit);

        //Gets the fields again
        inputs = this.getObjectsByTagName('input',form);
        textareas = this.getObjectsByTagName('textarea',form);
        
        //resets them if they are the same as they were on page load
        this.resetValues(inputs);
        this.resetValues(textareas);

        return false;
    },

    //Checks to see if the elements are the same as they were on page load
    //  if so then it clears them
    resetValues : function (fields)
    {
        for (key in fields)
        {
            field = fields[key];
            //if(field.type != 'submit' && field.type != 'hidden' && field.type != 'radio')
            if(!this.in_array(field.type,skip_types))
            {
                if (field.value == field.attributes.org.nodeValue)
                {
                    field.value = '';
                }
            }
        }
        
        return false;
    },

    //Cool reverse traversing method used to find the parent form given any child element of it
    getParentForm : function(element)
    {
        par = element.parentNode;
        if(par.tagName == 'FORM')
        {
            return par;
        }
        else
        {
            //returned so that the calling methods will have the response
            return this.getParentForm(par);
        }
    }
};
