/*
 * FORM VALIDATION.js
 * This is form validation code used throughout the site.  All forms try to validate, but if
 * no validation ruleset is given to the form, the validation yields silently and the form
 * has no JS validation.
 *
 * Define a ruleset using another ruleset as a template.
 */

$(function(){
    var emptyErrorDiv = $("<div>").addClass("error").get(0);
    
    // simplified input retreival
    var getInp = function(name,form){return $(form).find("[name="+name+"]:first").val();}
    // Form-Specific Validation Rulesets
    // Career Form Validation Ruleset
    $("form[name=career]").bind("validate",function(){
        var errors = [];
        if(getInp("fname",this) == "")      errors.push("Please enter your First Name");
        if(getInp("lname",this) == "")      errors.push("Please enter your Last Name");
        if(getInp("email",this) == "")      errors.push("Please enter your Email Address");
        if(getInp("interest",this) == "")   errors.push("Tell us what you are interested in");
        if(getInp("comments",this) == "")   errors.push("Tell us more about why you are interested in a career at K-Motion");
        $(this).data("errors",errors); // post errors genericly to form
    });
    // Schedule Webinar Validation Ruleset
    $("form[name=schedule_webinar]").bind("validate",function(){
        var errors = [];
        if(getInp("name",this) == "")       errors.push("Please Enter your Name");
        if(getInp("phone",this) == "")      errors.push("Please give us your phone number");
        if(getInp("email",this) == "")      errors.push("Please give us your email address");
        if(getInp("interest",this) == "")   errors.push("Please tell us what product you are intersted in");
        if(getInp("profession",this) == "") errors.push("Please tell us what kind of professional you are");
        $(this).data("errors",errors);
    });
    // KCert Registration Validation Ruleset
    $("form[name=kcert_register]").bind("validate",function(){
        var errors = [];
        if(getInp("fname",this) == "")      errors.push("First Name is Empty");
        if(getInp("lname",this) == "")      errors.push("Last Name is Empty");
        if(getInp("email",this) == "")      errors.push("Email is Empty");
        if(getInp("phone",this) == "")      errors.push("Phone # is Empty");
     //   if(getInp("pga",this) == "")        errors.push("PGA # is Empty");
        $(this).data("errors",errors);
    });
	// Submit Software Maintenance - support.php
    $("form[name=maintenance]").bind("validate",function(){
        var errors = [];
        if(getInp("fname",this) == "")      errors.push("First Name is Empty");
        if(getInp("lname",this) == "")      errors.push("Last Name is Empty");
        if(getInp("email",this) == "")      errors.push("Email is Empty");
        if(getInp("phone",this) == "")      errors.push("Phone # is Empty");
		//if(getInp("comments",this) == "")   errors.push("Please enter a comment");
        $(this).data("errors",errors);
    });	
	// Submit Software Maintenance - support_maintenance.php
    $("form[name=maintenance1]").bind("validate",function(){
        var errors = [];
        if(getInp("fname",this) == "")      errors.push("First Name is Empty");
        if(getInp("lname",this) == "")      errors.push("Last Name is Empty");
        if(getInp("email",this) == "")      errors.push("Email is Empty");
        if(getInp("phone",this) == "")      errors.push("Phone # is Empty");
		//if(getInp("comments",this) == "")   errors.push("Please enter a comment");
        $(this).data("errors",errors);
    });	



    // Behavior of all forms, triggers validation events, but does not need validation event to process
    $("form").submit(function(){
        $(this).find(".error").slideUp(function(){$(this).remove();}); //hide any error elements
        $(this).trigger("validate"); // trigger the validation code
        var errors = $(this).data('errors'); // gather validation errors
        if(typeof(errors) == "undefined") errors = []; // if there were no errors set, default to empty array
        if(errors.length > 0){ // if errors were detected
            var errorDiv = $(emptyErrorDiv).clone(); // create an error message container
            $(errorDiv).append(errors.join("<br />")) // post error messages to error message container
            $(errorDiv).prependTo(this).hide().slideDown(); // display errors smoothly
            return false; // stop form from submitting
            // yes I am feeling comment crazy ~Jeff
        }
    });
});
