$(document).ready(function(){ 
        $( ".form-datepicker" ).datepicker({
			showOn: "button",
			buttonImage: "/web_resources/widgets/form/images/calendar.gif",
			dateFormat: "d M, yy",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
		    onClose: function(dateText, inst) {
					$(this).trigger('focusout');     
											}


		});
      
        $('form').each(function(){
		
		    var myRules = {};
			$(this).find('input[type=text], textarea').each(function(){
				if($(this).attr('fieldmaxlength') > 0)
				{
					if(typeof(myRules[$(this).attr('name')]) === "undefined")
						myRules[$(this).attr('name')] = {};
					
					myRules[$(this).attr('name')]['maxlength'] = $(this).attr('fieldmaxlength');
				}
			});
			
            $(this).validate({
			    errorClass:"form-error-msg"  ,
				errorElement: "span",
				errorPlacement: function(error, element) {
				                   if(element.attr('type') == 'checkbox'){
								       error.appendTo( element.parent().parent());    
                                    }
                                    else{									
                                       error.appendTo( element.parent());
									}   
                                },
				rules:myRules				
								
			});
        });		
});
