/*

 * File: Validate Form

 *

 * Description: This js file is used to validate forms before being sent to

 * Myemma using the jQuery library.

 */

function validateForm(){

    var submit = true;
    $('p#form-error').empty();


    $(':input.required').each(function(){
        if($(this).val()==""){
            $(this).addClass("error");
            submit = false;
        } else {
            $(this).removeClass("error");
        }
    });

     $(':input.required-alert').each(function(){
         
         if($(this).attr('checked')=="" || $(this).val() == ""){
            submit = false;
            alert($(this).attr('alert'));
            $(this).focus();
         }
     });



    // check that at leats one product on interest is checked

    if($('#product_interest').length){



          if ($('#product_interest .checkbox:checked').val() == null) {

            $('#product_interest').addClass("error");

            submit = false;

          } else {

              $('#product_interest').removeClass("error");

          }

    }





    if(!submit){

        $('p#form-error').append('Missing required fields. Please check that all hilighted fields filled out and resubmit.');

    }

    else {

        //validate email

        if($("input[name='email']").hasClass('required')){

            if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("input[name='email']").val())){



                 $('p#form-error').append('Invalid field. Your email address is invalid.');

                 $("input[name='email']").addClass("error");


                 submit = false;

            }

            else {

                $("input[name='email']").removeClass("error");

            }
            
        } else {
            
            if($("input[name='email']").val()==""){
                // myemma requires an email address so we make a fake one
                $("input[name='email']").val('noemail@email.com');
            }
        }

    }





    return submit;

}




