var validator = null;
$(document).ready(function(){
	validator = $("#ajax_pianoSerial_search").validate({
		submitHandler:function(form){
			document.getElementById("search_result").style.display = "none";
			$.blockUI({ message: "正在查询……" ,css: { backgroundColor: '#f00', color: '#fff'}});
			$(form).ajaxSubmit({
				dataType: 'json',
				success: response
			});
		},
		errorPlacement:function(error, element){
			error.appendTo(element.next("em"));
		},
		rules:{
			number:{
				required:true,
				number:true,
				minlength:4,
				maxlength:8
			}
		},
		messages:{
			number:{
				required:"请输入序列号",
				number:"必须是数字",
				minlength:$.format("不能少于 {0} 个字符"),
				maxlength:$.format("不能超过 {0} 个字符")
			}
		}
	});
});
function response(data){
	if(data.actionErrors && data.actionErrors.length > 0){
		alert(data.actionErrors);
	}
	else if(data.fieldErrors){
		showFieldErrors(data.fieldErrors);
	}
	else{
		var serial = document.getElementById("prefix").value + document.getElementById("number").value;
		var brand  = document.getElementById("brandId").options[document.getElementById("brandId").selectedIndex].text;
		$("#serial").html(serial);
		$("#brand").html(brand);
		$("#message").html(data.actionMessages[0]);
		document.getElementById("search_result").style.display = "block";
	}
	$.unblockUI();
	$("#no").click($.unblockUI); 
}
function showFieldErrors(fieldErrors){
	var plainErrors = {};
	for(field in fieldErrors){
		plainErrors[field] = fieldErrors[field].join(" ");
	}
	validator.showErrors(plainErrors);
}

