/*
 * Object that searches various form-related tags looking for the REL attribute.
 * If it finds the REL attribute, it converts the REL content to a popup hint div.
 *
 * Relevant tags include INPUT, SELECT, and TEXTAREA.
 *
 * Tags must have an ID attribute to be processed.
 *
 * The use of the REL attribute oin those tags may cause HTML validation errors (whoops).
 *
 * Uses JS function decodeURIComponent() to decode REL because hint may contain characters
 * break the attribute (e.g. "). So if those chacters are present, the hint should be encoded at the server.
 * The PHP function rawurlencode() works well for this.
 *
 * Note: if the TITLE attribute is also used on the tags, there may be visual conflift between the
 * popup and browser-created tool tip.
 */
var CRNA_formHints = {
    fhElements : ['input', 'select', 'textarea'],
    evtObj : Object,
    fhWrapperObj : Object,
    fhContent : Object,
    fhArray : {},

	init : function() {
        if (!document.getElementById || !document.createElement || !document.getElementsByTagName) {
            return;
        }

        var i, j;

        this.fhWrapperObj = document.createElement('div');
        this.fhWrapperObj.className = 'fhWrapper';
        this.fhWrapperObj.id = 'fhWrapper';

        var fh1 = document.createElement('div');
        fh1.className = 'fhOuter1';
        this.fhWrapperObj.appendChild(fh1);

        var fh2 = document.createElement('div');
        fh2.className = 'fhOuter2';
        fh1.appendChild(fh2);

        var fh3 = document.createElement('div');
        fh3.className = 'fhOuter3';
        fh2.appendChild(fh3);

        var fh4 = document.createElement('div');
        fh4.className = 'fhBox';
        fh3.appendChild(fh4);

        var fh5 = document.createElement('div');
        fh5.className = 'fhCaption';
        fh5.innerHTML = 'Hint';
        fh4.appendChild(fh5);

        this.fhContentObj = document.createElement('div');
        this.fhContentObj.id = 'fhContent';
        this.fhContentObj.className = 'fhContent';
        fh4.appendChild(this.fhContentObj);

        var fhLen = this.fhElements.length;

        for (i = 0; i < fhLen; i++) {
            var current = document.getElementsByTagName(this.fhElements[i]);
            var curLen = current.length;

            for (j = 0; j < curLen; j++) {
                if (current[j].getAttribute('id')) {
                    if (current[j].getAttribute('rel')) {
                        this.fhArray[current[j].getAttribute('id')] = decodeURIComponent(current[j].getAttribute('rel'));
                        current[j].removeAttribute('rel');

                        CRNA_addEvent(current[j], 'focus', this.hintFocus);
                        CRNA_addEvent(current[j],'blur', this.hintBlur);
                    }
                }
            }
        }
    },

    hintFocus : function(e) {
        CRNA_formHints.evtObj = this;
        CRNA_fh_timerId = window.setTimeout("CRNA_formHints.hintShow()", 250);
    },

    hintBlur: function() {
        if (window.CRNA_fh_timerId) {
            clearTimeout(CRNA_fh_timerId);
        }

        if (window.CRNA_fh_opacityId) {
            clearTimeout(CRNA_fh_opacityId);
        }

        try {
            this.parentNode.removeChild(CRNA_formHints.fhWrapperObj);
        }
        catch (e) {
        }
    },

    hintShow : function() {
        var hintText = this.fhArray[this.evtObj.getAttribute('id')];
        if (hintText) {
            this.fhContentObj.innerHTML = hintText;

            this.evtObj.parentNode.insertBefore(this.fhWrapperObj, this.evtObj);

            if (!CRNA_GTE_IE7) {
                this.fhWrapperObj.style.opacity = '.1';
                this.hintFade(10);
            }
        }
    },

    hintFade: function(opac) {
        var passed = parseInt(opac);
        var newOpac = parseInt(passed + 10);

        if (newOpac < 100) {
            this.fhWrapperObj.style.opacity = '.' + newOpac;
            this.fhWrapperObj.style.filter = 'alpha(opacity:' + newOpac + ')';
            CRNA_fh_opacityId = window.setTimeout("CRNA_formHints.hintFade('" + newOpac + "')", 20);
        }
        else {
            this.fhWrapperObj.style.opacity = '1';
            this.fhWrapperObj.style.filter = 'alpha(opacity:100)';
        }
    }
}

/*
 * Function initializes CRNA_formHints object
 */
function CRNA_formHintLoader() {
	CRNA_formHints.init();
}

// Add CRNA_formHintLoader() to window.onload event
CRNA_addEvent(window, 'load', CRNA_formHintLoader);
