/**
 *  New England Nurses, Inc.
 *  Copyright (c) 2011 All rights reserved
 *  js/forms.js
 */

$(document).ready(function(){
    
    // Submit the Employment Inquiry form
    $("#formEmployment").submit(function() {
        // Validate the various fields
        foundError = false;
        FORMVALIDATION.textinput("ef_fullName");
        FORMVALIDATION.textinput("ef_homeAddress");
        FORMVALIDATION.textinput("ef_city");
        FORMVALIDATION.postalcode("ef_zipCode");
        FORMVALIDATION.phonenumberrequired("ef_phoneHome");
        FORMVALIDATION.phonenumber("ef_phoneMobile");
        FORMVALIDATION.emailaddress("ef_emailAddress");
        FORMVALIDATION.nospecial("ef_hearAbout");
        FORMVALIDATION.recaptcha("recaptcha_response_field");
        // Run or end the script depending on the validation
        if (foundError) {
            // Errors found, return false, nothing else gets done
            $("html, body").animate({scrollTop:0});
            return false;
        } else {
            // No errors were found so run the ajax submit
            var formData = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/scripts/send-employmentform.php",
                data: formData,
                dataType: "json",
                success: function(returnMSG){
                    $("#formEmployment").ajaxComplete(function(event, request, settings){
                        if(returnMSG.STATUS == "success") {
                            // The email sent correctly
                            $(".formReturnMessage").html("<strong>Thank You:</strong> Your information has been sent to the New England Nurses staff.  We will contact you as soon as we can.");
                            $(".formReturnMessage").removeClass("sendFail");
                            $(".formReturnMessage").addClass("sendSuccess");
                        } else {
                            // There was an error
                            $(".formReturnMessage").html("<strong>Error:</strong> " + returnMSG.MESSAGE);
                            $(".formReturnMessage").addClass("sendFail");
                        }
                        // Display the results
                        $(".formReturnMessage").css({"display" : "block"});
                        $("html, body").animate({scrollTop:0});
                    });
                }
            });
            return false;
        }
    });
    
    // Submit the Free Assessment form
    $("#formAssessment").submit(function() {
        // Validate the various fields
        foundError = false;
        FORMVALIDATION.textinput("af_fullName");
        FORMVALIDATION.textinput("af_homeAddress");
        FORMVALIDATION.textinput("af_city");
        FORMVALIDATION.emailaddress("af_emailAddress");
        FORMVALIDATION.phonenumberrequired("af_phoneHome");
        FORMVALIDATION.phonenumber("af_phoneMobile");
        FORMVALIDATION.recaptcha("recaptcha_response_field");
        // Run or end the script depending on the validation
        if (foundError) {
            // Errors found, return false, nothing else gets done
            $("html, body").animate({scrollTop:0});
            return false;
        } else {
            // No errors were found so run the ajax submit
            var formData = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/scripts/send-assessmentform.php",
                data: formData,
                dataType: "json",
                success: function(returnMSG){
                    $("#formAssessment").ajaxComplete(function(event, request, settings){
                        if(returnMSG.STATUS == "success") {
                            // The email sent correctly
                            $(".formReturnMessage").html("<strong>Thank You:</strong> Your information has been sent to the New England Nurses staff.  We will contact you as soon as we can.");
                            $(".formReturnMessage").removeClass("sendFail");
                            $(".formReturnMessage").addClass("sendSuccess");
                        } else {
                            // There was an error
                            $(".formReturnMessage").html("<strong>Error:</strong> " + returnMSG.MESSAGE);
                            $(".formReturnMessage").addClass("sendFail");
                        }
                        // Display the results
                        $(".formReturnMessage").css({"display" : "block"});
                        $("html, body").animate({scrollTop:0});
                    });
                }
            });
            return false;
        }
    });
    
    // Submit the Free Assessment form
    $("#formFluClinic").submit(function() {
        // Validate the various fields
        foundError = false;
        FORMVALIDATION.textinput("fcf_companyName");
        FORMVALIDATION.nospecial("fcf_location");
        FORMVALIDATION.textinput("fcf_contactName");
        FORMVALIDATION.nospecial("fcf_clinicLocation");
        FORMVALIDATION.textinput("fcf_homeAddress");
        FORMVALIDATION.textinput("fcf_city");
        FORMVALIDATION.numbersonly("fcf_zipCode");
        FORMVALIDATION.phonenumberrequired("fcf_phoneNumber");
        FORMVALIDATION.numbersonly("fcf_phoneExtension");
        FORMVALIDATION.emailaddress("fcf_emailAddress");
        FORMVALIDATION.numbersonly("fcf_participants");
        FORMVALIDATION.recaptcha("recaptcha_response_field");
        // Run or end the script depending on the validation
        if (foundError) {
            // Errors found, return false, nothing else gets done
            $("html, body").animate({scrollTop:0});
            return false;
        } else {
            // No errors were found so run the ajax submit
            var formData = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/scripts/send-fluclinicform.php",
                data: formData,
                dataType: "json",
                success: function(returnMSG){
                    $("#formFluClinic").ajaxComplete(function(event, request, settings){
                        if(returnMSG.STATUS == "success") {
                            // The email sent correctly
                            $(".formReturnMessage").html("<strong>Thank You:</strong> Your information has been sent to the New England Nurses staff.  We will contact you as soon as we can.");
                            $(".formReturnMessage").removeClass("sendFail");
                            $(".formReturnMessage").addClass("sendSuccess");
                        } else {
                            // There was an error
                            $(".formReturnMessage").html("<strong>Error:</strong> " + returnMSG.MESSAGE);
                            $(".formReturnMessage").addClass("sendFail");
                        }
                        // Display the results
                        $(".formReturnMessage").css({"display" : "block"});
                        $("html, body").animate({scrollTop:0});
                    });
                }
            });
            return false;
        }
    });
    
    // Submit the Login form
    $("#formLogin").submit(function() {
        // Validate the various fields
        foundError = false;
        FORMVALIDATION.emailaddress("lf_userName");
        FORMVALIDATION.textinput("lf_passWord");
        // Run or end the script depending on the validation
        if (foundError) {
            // Errors found, return false, nothing else gets done
            return false;
        } else {
            // No errors were found so run the ajax submit
            var formData = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/scripts/process-login.php",
                data: formData,
                dataType: "json",
                success: function(returnMSG){
                    $("#formLogin").ajaxComplete(function(event, request, settings){
                        if(returnMSG.STATUS == "success") {
                            // They are logged in, so go to the proper URL
                            window.location = returnMSG.MESSAGE;
                        } else {
                            // There was an error
                            $(".formReturnMessage").html("Error: " + returnMSG.MESSAGE);
                            // Display the results
                            $(".formReturnMessage").css({"display" : "block"});
                        }
                    });
                }
            });
            return false;
        }
    });
    
    // Submit the Login form
    $("#formApplicants").submit(function() {
        // Validate the various fields
        foundError = false;
        FORMVALIDATION.textinput("af_passWord");
        // Run or end the script depending on the validation
        if (foundError) {
            // Errors found, return false, nothing else gets done
            return false;
        } else {
            // No errors were found so run the ajax submit
            var formData = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "/scripts/process-applicants.php",
                data: formData,
                dataType: "json",
                success: function(returnMSG){
                    $("#formApplicants").ajaxComplete(function(event, request, settings){
                        if(returnMSG.STATUS == "success") {
                            // The password was correct so show the files
                            $.get(returnMSG.MESSAGE, function(data){
                                // The data was recieved
                                $("#container_applicantFiles").html(data);
                            });
                        } else {
                            // There was an error
                            $(".formReturnMessage").html("Error: " + returnMSG.MESSAGE);
                            // Display the results
                            $(".formReturnMessage").css({"display" : "block"});
                        }
                    });
                }
            });
            return false;
        }
    });
    
});
