(function($){
	$(function(){
		$("#confimationMessage").hide();
		$("#customerMessageForm").validate({
			rules:ele.validations.customerMessage.rules,
			messages:ele.validations.customerMessage.messages,
			errorElement:"div",
			errorPlacement:function(error,element){
				element.parent("li").append(error);
			}
		});
		$("#customerMessageForm").submit(function(){
			if ($("#customerLoggedOut").length <= 0){
				if($(this).find(".e_serverValidationError").length>0){return false;}	
				if($(this).valid()){		
					var loggedInEvent = $("#loggedInEvent").val(); 
					var loggedOutEvent = $("#loggedOutEvent").val();
					var form=$(this),customerData=form.serializeObject(),proxy=(ele.session.loggedIn())?ele.proxy.customerService[loggedInEvent]:ele.proxy.customerService[loggedOutEvent];
					proxy(customerData,function(bean){
						// There was an error, display validation messages
						if(bean.validationErrors.customer=="NOT Logged In"){
							$("#customerMessageForm").attr("target", "_self");
							$("#customerMessageForm").attr("action", "?event=customerMessageValidateLogin");
							$("#customerMessageForm").append("<input type='hidden' id='customerLoggedOut' name='customerLoggedOut' value='true' />");
							$("#customerMessageForm").submit();
						}else{
							if(bean.validationErrors.customeremail){bean.validationErrors.customeremail=bean.validationErrors.customeremail;}
							if(bean['customerid'].length<1){
								for(var x in bean.validationErrors){
									var msg=$('<div class="e_serverValidationError">'+bean.validationErrors[x]+'</div>');
									form.find(':input').filter(function(){
										return(typeof($(this).attr("name"))!='undefined'&&$(this).attr("name").toLowerCase()==x);
									}).parent().append(msg);						
								}			
							}else{
								$("#messageForm").fadeOut("fast");
								$("#confimationMessage").fadeIn("fast");
							}
						}	
					});		
				}
				// Submits by AJAX, prevent default action			
				return false;
			}
		}).find(':input').bind('change',function(){
			// Remove server validation errors when input change
			$(this).parent().find('.e_serverValidationError').remove();
		});
	});
})(jQuery);

