﻿// required fields for validation
var aryRequiredFields = [   { "Name": "First Name", "ID": "FirstName" },
							{ "Name": "Last Name", "ID": "LastName" },
							{ "Name": "Email Address", "ID": "Email" },
							{ "Name": "Telephone", "ID": "TelephoneP" }
						];

$(document).ready(function()
{
    $("div.AlertMessage").click(function() { $(this).stop().fadeOut("slow"); });
    $("#btnSubmit").removeAttr("disabled").click(function() { if (Validate()) { Submit(); } else { return false } });
    $("#freeEvaluation").removeAttr("checked").click(function()
    {
        if ($(this).is(":checked"))
            $("#addtlQuestions").slideDown("slow");
        else
            $("#addtlQuestions").slideUp("slow");
    });

    if (document.location.href.indexOf("siteeval=1") >= 0)
    {
        $("#freeEvaluation").attr("checked", "checked");
        $("#addtlQuestions").slideDown("slow");
    }
});

function Validate()
{
    var formValid = true;
    var aryInvalidFields = new Array();

    for (itemIndex in aryRequiredFields)
        if ($.trim($("#" + aryRequiredFields[itemIndex].ID).val()).length == 0)
            aryInvalidFields.push("* " + aryRequiredFields[itemIndex].Name);

    if (aryInvalidFields.length > 0)
        $("div.AlertMessage").stop().html("The following fields are required:<br /><br />" + aryInvalidFields.join("<br />")).fadeIn("slow").fadeTo(6000, 1).fadeOut("slow");
    else
        $("div.AlertMessage").stop().fadeOut(300);

    return !(aryInvalidFields.length > 0);
}

function Submit() {
    $("#btnSubmit").attr({ "disabled": "disabled", "value": "Sending..." });
    $.post("/processforms.php", $("#ContactForm").serialize(),
		function(data){
			if(data == 'success'){
				if($("#FormType").val() == "Contact"){
					document.location.href="thankYou.html";
				} else {
					document.location.href="thankYouAssessment.html";				
				}
			} else {
				$("div.AlertMessage").html("There has been an error:  "+data+"").fadeIn("slow").fadeTo(6000, 1);			
			    $("#btnSubmit").removeAttr('disabled').attr({ "value": "Resend" });
			}
		}
    );
}

