$(document).ready(function() {
    //Initilize inital form values
    var textValues = new Array();
    var currentId;
    $('.txtinpt').each(function(){
        currentId = $(this).attr('id');
        textValues[currentId] = $(this).attr('value');
    });
    //Remove text onfocus and store inital value and set check for non-entry
    $('.txtinpt').focusin(function(){
        currentId = $(this).attr('id');
        currentFormId = $(this).parents("div:first").attr('id');
        //Resets color of form elements back to default (Used when they are turned red from an error)
        switch (currentFormId){
            case 'blogForm':
                    $(this).css('color','#666');
            break;
            case 'form_one':
            case 'fm1':
                    $(this).css('background','orange');
            break;
            case 'form_two':
                    $(this).css('background','#ffffff');
            break;
            case 'form_three':
            case 'fm3':
                    $(this).css('background','#dddddd');
            break;
            case 'form_four':
                    $(this).css('background','#ffffff');
            break;
        }
        if ($(this).attr('value') == textValues[currentId]) {
            $(this).attr('value','');
        }
        $(this).one('focusout', function(){
            textClearCheck(currentId);
        });
    });
    $('.submitButton').one('click',submitButtonFunction); //Bind button functions
    //Check form input
    function submitButtonFunction(){
        var currentFormName = $(this).parents("div:first").attr('id');
        var formInputValues = 'currentForm='+escape(currentFormName)+'&';
        var errors = new Array();
        var errorNo = 0;
        $("#"+currentFormName+" input:text").each(function(){
            currentId = $(this).attr('id');
            fieldName = $(this).attr('name');
            currentFormInputType = textValues[currentId];
            var testString = $(this).val();
            switch (currentFormInputType) {
                case 'Email Address':
                case 'Enter Your Email Address Here':
                    testString = testString.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)
                    if (testString)
                        formInputValues = formInputValues+ fieldName+'='+escape(testString)+'&';
                    else {
                        inputError = 1;
                        errors[errorNo] = currentId;
                        errorNo++;
                    }
                    break;
                case 'Zip Code':
                    testString = testString.match(/\d{5}(?:.?\d{4})?/);
                    if (testString)
                        formInputValues = formInputValues+ fieldName+'='+escape(testString)+'&';
                    else {
                        inputError = 1;
                        errors[errorNo] = currentId;
                        errorNo++;
                    }
                    break;
                case 'Phone Number':
                    testString = testString.match(/^\+?(\d[ -]?)?\(?\d{3}\)?[ -]?\d{3}[ -]?\d{4}$/)
                    if (testString)
                        formInputValues = formInputValues+ fieldName+'='+escape(testString)+'&';
                    else {
                        inputError = 1;
                        errors[errorNo] = currentId;
                        errorNo++;
                    }
                    break;
                default:
                    if (testString != currentFormInputType)
                        formInputValues = formInputValues+ fieldName+'='+escape(testString)+'&';
                    else {
                        inputError = 1;
                        errors[errorNo] = currentId;
                        errorNo++;
                    }
            }
        });
        if(!errorNo) {
            $('#'+currentFormName).find('.fdeOut').fadeOut(200);
            $('#'+currentFormName).find('.error').fadeOut(200);
            $('#'+currentFormName).find('input').fadeOut(200, function(){
                $('#'+currentFormName).find('.sending').fadeIn(200);
            });
            //alert(formInputValues);
            //iterate through all names and store the id + = + value in array + & in string to pass with ajax
            //alert(formInputValues.slice(1,-1))
            $.ajax({
                url: AjaxFormUrl,
                cache: false,
                type: 'POST',
                data: formInputValues.slice(0, -1),
                success: function(){
                    $('#'+currentFormName).find('.sending').fadeOut(200, function(){
                            $('#'+currentFormName).find('.submitted').fadeIn(200);
                    });
                },
                error: function(){
                    alert('There was an error processing your request, please try again or give us a call at (877) 773-3785\n');
                }
            });
        } else {
            switch(currentFormName) {
                case 'blogForm':
                    for (x in errors)
                        $('#'+errors[x]).css('color','#c00');
                    break;
                default:
                    for (x in errors)
                        $('#'+errors[x]).css('background','red');
            }
            $('#'+currentFormName).find('.error').fadeIn(200);
            $(this).one('click',submitButtonFunction);
        }
    }
    //Input inital value if no value is in the form element
    function textClearCheck(currentId) {
        var currentTextValue = jQuery.trim($('#'+currentId).attr('value'));
        if (currentTextValue.length == 0)
            $('#'+currentId).attr('value',textValues[currentId]);
    }

});

