var form_loadTimer = new Array();
var form_successHandlers = new Array();
var form_changeHandlers = new Array();
var form_errorHandlers = new Array();

/**
 * Shows the error that is attached to fieldName
 *  the name of the error field is error_fieldName
 */
function showError(fieldName,message) {
    if (fieldName == undefined) return;
    if ((message == undefined)||(message == '')) hideError(fieldName);
    $('[name='+fieldName+']').removeClass('formField_ok');
    errorField = $('[name=error_'+fieldName+']').show();
    errorField.find('.formError_message').html(message);
}

/**
 * Hides the errorField attached to fieldName
 */
function hideError(fieldName) {
    if (fieldName == undefined) return;
    errorField = $('[name=error_'+fieldName+']').hide();
    errorField.find('.formError_message').empty();
}

/**
 * Starts the loading timer for fieldName
 *  <p> It will show the loadingMessage in error_fieldName if the loading <br/>
 *  continues after timeOut Miliseconds
 *  </p>
 */
function startLoading(fieldName,loadingMessage,timeOut) {
    form_loadTimer[fieldName] = setTimeout('showLoading(\''+fieldName+'\',\''+loadingMessage+'\');',timeOut);
}

/**
 * Stops the loading timer and hides the loading Message
 */
function stopLoading(fieldName) {
    if (form_loadTimer[fieldName] == undefined) return;
    clearTimeout(form_loadTimer[fieldName]);
    delete(form_loadTimer[fieldName]);
    hideLoading(fieldName);
}

/**
 * Shows the loading Message and stops the timer
 */
function showLoading(fieldName, loadingMessage) {
    //if (loadTimer[fieldName] == undefined) return;
    stopLoading(fieldName);
    showError(fieldName,loadingMessage);
}

/**
 * Hides the loading message
 */
function hideLoading(fieldName) {
    hideError(fieldName);
}

function checkMandatoryFieldsInForm(form) {
        var ok = true;
        // check empty fields
        $(':input', $(form)).each(function(index,element) {
            if ($(element).hasClass('formField_mandatory')) {
                if ($(element).val() == '') {
                    //alert($(element).attr('name'));
                    var name = $(element).attr('name');
                    showError(name, form_message_error_empty);
                    ok = false;
                }
            }
        });
        return ok;
}

/**
 * Functions for handling standarized formField input types
 */
(function($) {
    // Binds to the .live event handler so future elements get triggered as well
    // 
    // check Mandatory Fields
    //Remove the old event handler before adding a new one
    $('.formField_mandatory').die('blur');
    $('.formField_mandatory').live('blur',function () {
        var value = $(this).val();
        if (value == '') {
            $(this).removeClass('formField_ok');
            showError($(this).attr('name'),form_message_error_empty);
        } else {
            $(this).addClass('formField_ok');
        }
    });
    //Remove the old event handler before adding a new one
    $('.formField_mandatory').die('focus');
    $('.formField_mandatory').live('focus',function () {
        hideError($(this).attr('name'));
    });

    // check Default Fields
    //Remove the old event handler before adding a new one
    $('.formField_settodefault').die('blur');
    $('.formField_settodefault').live('blur',function () {
        if (this.value == '') {
            this.value = this.defaultValue;
        }
    });
    //Remove the old event handler before adding a new one
    $('.formField_settodefault').die('focus');
    $('.formField_settodefault').live('focus',function () {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    });
})(jQuery);
