$(document).ready(function(){
// Set external links - XHTML Compatibility reasons
    $("a[rel='external']").attr("target","_blank");


// Form validation 
    $(".required").after('<strong class="astrix">*</strong>');

    $("#contactForm").submit(function(e) {

        var trigger = "0";
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".validation").remove();
        $("input").blur();
        $("#message").html("");
        
        $(".required",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            
            if($(this).attr("value") == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Required Field</div>');
                trigger = "1";
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

        $(".required" + ".email",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");

            if((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid Email</div>');
                trigger = "1";
            }
        });        
        
        if(trigger == "1") {
            $(".validation").fadeIn();
            e.preventDefault();
        }
        else {
            e.preventDefault();
            var inputs = [];
          
            $(':input',this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            })
          
            jQuery.ajax({
                data: inputs.join('&'),
                url: this.action,
                timeout: 2000,
                error: function() {
              $('#message').append( "There was a problem sending your message, please try again." ).fadeIn();
              $('#contactForm').get(0).reset();
                },
            success: function(r) { 
              $('#message').append( "Thank You. Your information has been sent." ).fadeIn();
              $('#contactForm').get(0).reset();
            }
          })        
        
        }

        $(".invalid > .required:first").focus();
    });	
    });
   
   // CLEAR FORM

      $.fn.clearForm = function() {
        return this.each(function() {
          var type = this.type, tag = this.tagName.toLowerCase();
          if (tag == 'form')
            return $(':input',this).clearForm();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
          else if (tag == 'select')
            this.selectedIndex = -1;
        });
      };


