var allFields = null;
var activeForm = null;

function initForms(fields)
{
    allFields = fields;
    
    for(var field in allFields)
    {
        $("#id_" + field).focus(function() {
            if( allFields[$(this).attr("name")] == $(this).val() )
            {
                $(this).val("");
            }
            $(this).removeClass("error");
            $(activeForm).children("div.formHolder").children("div.formError").stop().fadeOut(300);
        }).blur(function() {
            if( $(this).val() == "" )
            {
                $(this).val( allFields[$(this).attr("name")] );
            }
        }).blur();
    }

    $("input.popupSubmit").click(function() {
        var error = false;
        var formValues = {};
        
        $(activeForm).children("div.formHolder").children("input, textarea").each(function() {
            
            formValues[ $(this).attr("name") ] = $(this).val(); // store field value for ajax post
            if( $(this).attr("type") == "hidden" || $(this).attr("type") == "submit" ) return true; // skip hidden and submit fields
            
            if( allFields[ $(this).attr("name") ] != undefined )
            {
                $(this).blur();
                if( $(this).val() == allFields[ $(this).attr("name") ] )
                {
                    $(this).addClass("error");
                    error = true;
                }
                else
                {
                    if( $(this).attr("name").indexOf("mail") > -1 )
                    {
                        // validate e-mail
                        var email_regex = /(^[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$/gi;
                        if( $(this).val().search(email_regex) == -1 )
                        {
                            $(this).addClass("error");
                            error = true;
                        }
                        else
                        {
                            $(this).removeClass("error");
                        }
                    }
                    else
                    {
                        $(this).removeClass("error");
                    }
                }
            }
        });
        
        if(!error)
        {
            $.post( $(activeForm).attr("action"), formValues, response, "text");
            $(activeForm).children("div.formHolder").children("div.formError").stop().fadeOut(300);
        }
        else
        {
            $(activeForm).children("div.formHolder").children("div.formError").html("Complete todos los campos.").stop().fadeIn(300);
        }
        
        return false;
    });
}

function response(data, textStatus)
{
    if(activeForm)
    {
        if(data == '1')
        {
            $(activeForm).children("div.formHolder").fadeOut(300);
            $(activeForm).children("div.formSent").delay(350).fadeIn(300);
        }
        else
        {
            $(activeForm).children("div.formHolder").children("div.formError").html("Error al enviar el formulario.").stop().fadeIn(300);
        }
    }
}

function resetActiveForm()
{
    if(activeForm)
    {
        $(activeForm).children("div.formHolder").show();
        $(activeForm).children("div.formHolder").children("div.formError").hide();
        $(activeForm).children("div.formSent").hide();
        
        $(activeForm).children("div.formHolder").children("input, textarea").each(function() {
            if( allFields[ $(this).attr("name") ] != undefined )
            {
                if( $(this).attr("type") == "hidden" || $(this).attr("type") == "submit" ) return true; // skip hidden and submit fields
                $(this).val( allFields[ $(this).attr("name") ] );
                $(this).removeClass("error");
            }
        });
    }
}
