
function ajax_form_normalizeFormName( formName )
{
    var splitFormName = formName.split("_");

    for( var i = 0; i < splitFormName.length; i++ )
        if( "form" == splitFormName[i] )
        {
            delete splitFormName[i];
        }

    if( "form" == splitFormName[splitFormName.length - 1] )
        splitFormName.pop();
    formName = splitFormName.join("_");

    while( -1 != formName.indexOf("__") )
        formName = formName.replace( /__/, "_" );
    formName = formName.replace( /_$/, "" );

    return formName;
}

function ajax_form_computeIds( formName, outStatus, outSubmit, outSpinner, outForm )
{
    formName = ajax_form_normalizeFormName( formName );

    if( null != outStatus )
        outStatus.value  = formName  + "_status";
    if( null != outSubmit )
        outSubmit.value  = "submit_" + formName;
    if( null != outSpinner )
        outSpinner.value = formName  + "_spinner";
    if( null != outForm )
        outForm.value    = formName  + "_form";
}

function ajax_form_computeDisplayName( formName )
{
    formName = formName.toLowerCase();
    return formName.split("_").join(" ").replace( /1234567890/, "").replace( "editor", "" ).replace( "form", "" ).replace( "add", "").replace( "upload", "" );
}

function ajax_form_computeDisplayActionPastTense( formName )
{
    formName = formName.toLowerCase();
    if( -1 != formName.indexOf("add") )
        return "added";
    if( -1 != formName.indexOf("upload") )
        return "uploaded";
    return -1 == formName.indexOf("email") ? "changed" : "sent";
}

function ajax_form_computeDisplayAction( formName )
{
    formName = formName.toLowerCase();
    if( -1 != formName.indexOf("add") )
        return "add";
    if( -1 != formName.indexOf("upload") )
        return "upload";
    return -1 == formName.toLowerCase().indexOf("email") ? "change" : "send";
}

function ajax_form_computeProcessingMessage( formName )
{
    formName = formName.toLowerCase();
    if( -1 != formName.indexOf( "upload") )
        return "uploading...";
    if( -1 != formName.indexOf( "search") )
        return "searching...";
    return "processing...";  
}

function ajax_form_computeSuccessMessage( formName )
{
    if( -1 == formName.toLowerCase().indexOf("filter") && -1 == formName.toLowerCase().indexOf("search") )
        return ajax_form_computeDisplayName(formName) + " successfully " + ajax_form_computeDisplayActionPastTense( formName );
    else
        return "search completed";
}

function ajax_form_computeFailureMessage( formName )
{
    if( -1 == formName.toLowerCase().indexOf("filter") && -1 == formName.toLowerCase().indexOf("search") )
        return "an error occurred while trying to " + ajax_form_computeDisplayAction(formName) + " " + ajax_form_computeDisplayName(formName)
    else
        return "an error during search search";
}

function ajax_form_initialize( formName )
{
    var spinner_id = {value:""};
    ajax_form_computeIds( formName, null, null, spinner_id, null);
    $( "#" + spinner_id.value ).hide();
}

function ajax_form_beforeSubmit( suppressStatusMessages, formName, optionsJSON )
{
    var optionsHash = eval('('+optionsJSON+')');

    var status_id  = {value:""}, submit_id = {value:""}, spinner_id = {value:""};
    ajax_form_computeIds( formName, status_id, submit_id, spinner_id, null);

    $('.checkbox_error').hide();

    if( !suppressStatusMessages )
        $( "#" + status_id.value ).show().html( ajax_form_computeProcessingMessage(formName) ).removeClass("success").addClass("processing");
    $( "#" + submit_id.value ).attr("disabled","disabled");
    $('#' + formName + ' #global_error' ).html('').removeClass( 'error_msg' ).show();
    $('#' + formName + ' .cancel' ).addClass('hidden');
    $( "#" + spinner_id.value).removeClass('hidden').removeAttr("style").hide().fadeIn(400);
}

function ajax_form_onFailure( suppressStatusMessages, formName, optionsJSON, jsonErrors )
{
    var optionsHash = eval('('+optionsJSON+')');

    var status_id  = {value:""}, form_id = {value:""};
    ajax_form_computeIds( formName, status_id, null, null, form_id);

    if( -1 != formName.indexOf("password") )
    {
        $( "#" + status_id.value ).html("");
    }
    else
    {
         $( "#" + status_id.value ).removeClass('processing');
         if( !suppressStatusMessages && !keyValueExistsAndIsTrue( optionsHash, "suppress_error_message" ) )
            $( "#" + status_id.value ).html( ajax_form_computeFailureMessage(formName) );
         else
            $( "#" + status_id.value ).html('');

    }

    try
    {
        applyJSONErrorsToForm( jsonErrors, formName, true, optionsHash["clearPreviousErrors"], optionsHash["addAsGlobalErrorWhenCannotFindAttribute"] );
    }
    catch(error)
    {
    }  
}

function keyValueExistsAndIsTrue( hash, key )
{
    return undefined !== hash[key] && hash[key];
}

function ajax_form_onSuccess( suppressStatusMessages, formName, optionsJSON )
{
    var optionsHash = eval('('+optionsJSON+')');

    var status_id  = {value:""}, submit_id = {value:""}, spinner_id = {value:""};
    ajax_form_computeIds( formName, status_id, submit_id, spinner_id, null);
     if( !suppressStatusMessages && !keyValueExistsAndIsTrue( optionsHash, "suppress_success_message" ) ) {
        if (keyValueExistsAndIsTrue( optionsHash, "success_message")) {
          $( "#" + status_id.value ).html( optionsHash['success_message'] ).addClass("success").hide().fadeIn(300);
        } else {
          $( "#" + status_id.value ).html( ajax_form_computeSuccessMessage(formName) ).addClass("success").hide().fadeIn(300);
        }
     } else {
        $( "#" + status_id.value ).html('').addClass("success").hide().fadeIn(300);
     }
}

function ajax_form_onComplete( suppressStatusMessages, formName, optionsJSON )
{
    var optionsHash = eval('('+optionsJSON+')');

    var status_id  = {value:""}, submit_id = {value:""}, spinner_id = {value:""};
    ajax_form_computeIds( formName, status_id, submit_id, spinner_id, null);

    $( "#" + submit_id.value ).removeAttr("disabled");
    $( "#" + spinner_id.value).fadeOut(500);
    $( "#" + status_id.value ).removeClass("processing");
    $('#' + formName + ' .cancel' ).removeClass('hidden');
}

function ajax_form_clear( formName )
{
    var status_id  = {value:""}, submit_id = {value:""}, spinner_id = {value:""};
    ajax_form_computeIds( formName, status_id, submit_id, spinner_id, null);
    $( "#" + submit_id.value ).removeAttr("disabled");
    $( "#" + spinner_id.value).hide();
    $( "#" + status_id.value ).removeClass().html('');
}
