﻿/*
-----------------------------------------------
Revision:   $Rev: 659 $
Updated:    $Date: 2009-10-05 02:09:14 -0400 (Mon, 05 Oct 2009) $
Author:     $Author: boyan $
-----------------------------------------------
*/
var applicationLogin = Class.create({
    // Define the name of the css class that the script will look for
    // and add enable/disable code for
    enableOnEntrySelector: '[class="enableOnEntry"],.enableOnEntry',
    setFocusClassName: 'setFocus',
    formElements: '',
    submitButton: '',
    initialize: function() {
        // For each element that has the 'enableOnEntrySelector' defined,
        // loop over the form elements and store any text and password fields
        // Also save the id of the submit button and disable it
        $$(this.enableOnEntrySelector).each((function(e) {
            // Get all the form inputs and itterate through them
            $(e).getInputs().each((function(formElement) {
                // If the element has a class name that matches the 'setFocusClassName'
                if ($(formElement).hasClassName(this.setFocusClassName)) {
                    // Activate the element
                    $(formElement).activate();
                }

                // If the input is a text type or password type
                if ($(formElement).readAttribute('type') == 'text' || $(formElement).readAttribute('type') == 'password') {
                    // Store the id of the element
                    this.formElements += $(formElement).readAttribute('id') + ' ';

                    // Add a keyup event handler to called the 'checkInput' function
                    $(formElement).observe('keyup', this.checkInput.bind(this));
                    $(formElement).observe('click', this.checkInput.bind(this));
                }
                // If the input is a submit type
                else if ($(formElement).readAttribute('type') == 'submit') {
                    // Store the id of the submit button
                    this.submitButton = $(formElement).id;
                    // Disable the submit button
                    $(this.submitButton).disabled = true;
                }
            }).bind(this));
        }).bind(this));
    },
    checkInput: function(event) {
        var emptyFeildsExist = false;

        // Itterate through the stored form elements
        $w(this.formElements).each((function(e) {
            // Check if the element value is empty
            if ($F(e).strip().empty()) {
                // It is empty, so break out of the loop
                emptyFeildsExist = true;
                $break;
            }
        }).bind(this));

        // If empty form fields do not exist, enable the submit button
        if (!emptyFeildsExist) $(this.submitButton).disabled = false;
        // Else, disable the submit button
        else $(this.submitButton).disabled = true;
    } 
});

// Create an instance of the object after the page has loaded
document.observe("dom:loaded", function() {
  applicationLoginInstance = new applicationLogin();
});
