// JavaScript Document
$(document).ready(function() {
							   
function writeMailForm () {
	$.ajax({
  				url: "http://www.advanceie.org/embeds/mailingListForm/",
  				success: function(data) {
    			$('#mailingFormWrapper').html(data);		
 				},
				error: function() {
				$('#mailingFormWrapper').html('An error has occurred.');
				}
			});
			return false;
};
	
 // set up ajax handling of response
 
 		var options = { 
		
		target:"#hiddenDiv",
        
		beforeSubmit:function()  //before sending submit, give user notification...
  {
     $("#ajaxMsg").html("Sending...");     
  },
		
		
		success:function(rtn) 
   { 
    if(rtn=="success")
    {
      $("#mailingFormWrapper").html("You have been added to our mailing list.");
	  setTimeout(writeMailForm,5000);
     }else{
        //there was an erro, so grab the UL in the content ID, 
        //inside the hidden DIV, and put it into the Message Notification
      //$("#ajaxMsg").html("There was an error");
	  console.log("Found an error");
     }
   } 
  }
		
 
  //form validation code
		
		$("#mailingList").validate({
			
		//sets validation rules	
			rules: { 
			
     		name: {				
			required: true,
			minlength: 2
			},
     					
			email: {
				email: true,
      			required: true       			
     		}		
			
   		},
		
		//prevents validation on the fly as entered
		
		onkeyup: false,
		
		//sets custome messages for wrong entries
		
		messages: {
			
			name: "Enter your Name&nbsp;",
			
    		email: "Enter your Email&nbsp;"			
			
   		},
		
		//specifies class for error messages
		
		errorClass: "error",
		
		//specifies where to put error messages
		
		errorPlacement: function(error, element) {
     		error.appendTo(element.parent("label"));			
   		},
		
		errorElement: "span",
		
		submitHandler: function(form) {
   			//$("#ajaxMsg").html("");
			$(form).ajaxSubmit(options);
   		}

										   
		})
	});
