$(function(){
	/* Общие функции */
	function ShowError(ErrorText){
		ClearErrors();
		AppendError(ErrorText);
	}

	function ClearErrors(){
		$('#errors').empty();
	}

	function AppendError(ErrorText){
		$('#errors').append($('<div class="Error">' + ErrorText + '</div>'));
	}
	
	function AppendMessage(MessageText){
		$('#messages').append($('<div class="Message">' + MessageText + '</div>'));
	}

	function ShowMessage(MessageText){
		$('#messages').empty();
		$('#errors').empty();
		AppendMessage(MessageText);
	}

	$.ajaxSetup({
		type: 'POST',
		cache: false,
		url: '../script/ajax.php',
		//dataType: 'JSON',
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ShowError('Ошибка: ' + textStatus + ' - ' + errorThrown);
		}
	});
	
	$("#central_form").validate({
			submitHandler: function() {
					//Прячем ошибки
					$("div.error").hide();
					// готовим запрос
					var options = {
						type: 'POST',
						cache: false,
						url: '../script/ajax.php',
						//dataType: 'JSON',
						error: function(XMLHttpRequest, textStatus, errorThrown){
							ShowError('Ошибка: ' + textStatus + ' - ' + errorThrown);
						},
						success: function(json){   
							$('#final_count input').val(json);
						}  
					};
				// передаем опции в  ajaxSubmit
				$("#central_form").ajaxSubmit(options);
				return false;
			},
			errorContainer: "span.error",
			errorLabelContainer: "span.error",
			wrapper: "span",
			onkeyup: false,
			rules: {
				cover_paper:{
					required:true
					
				},
				kol_exemp:{
					required:true,
					number:true
				},
				kol_str_v_exemp:{
					required:true,
					number:true
				}
			},
			messages:{
				cover_paper:{
					required: "Выберете бумагу на обложку",
				},
				kol_exemp:{
					required: "Укажите количество экземпляров",
					number:"Количество экземпляров указывается в цифрах"
				},
				kol_str_v_exemp:{
					required: "Укажите количество страниц в экземпляре",
					number:"Количество страниц в экземпляре должно быть указано в цифрах"
				}
		}
	})
})
