/**
 * Класс для отправки форм на сервер
 *  
 * @link http://www.symp.ru
 * @author Alexandr Simonov (Александр Симонов)
 * @copyright  2010+ Александр Симонов
 * @license http://www.symp.ru/symp/license.txt
 * 
 * @class
 */
function Send_Symp() {
	
	/**
     * Ссылка, куда отправлять
     * @type {string} 
     */
	this.sever_url = "";
	
	/**
     * jQuery контейнер, объект формы, который отправлять
     * @type {object} 
     */
	this.obj_form;
	
	 
	/**
	 * Отправка данных на сервер   
	 * @function
	 */
	this.send = function() {			
		var bg = "#FBFBFB";
		$("#result_sender", this.obj_form).html('<img src="/face/img/indicator.gif"/> Пожалуйста подождите');
		
		//Найдем все элементы формы
		$("input[type = 'text']", this.obj_form).css("background-color", bg);
		$("select", this.obj_form).css("background-color", bg);
		$("textarea", this.obj_form).css("background-color", bg);
		$("label[type='result_field']", this.obj_form).text("");
		this.clearDef();
		this.param = this.obj_form.serialize();
		$.post(this.sever_url, this.param, this.response);
	}


	/**
	 * Получаем результат   
	 * @function
	 * @param {string} xml Данные с сервера
	 */
	this.response = function(xml) {			
		var accept = $("accept", xml).text();
		var obj_parent = this.success.parent;
		obj_parent.restoreDef();
		if (accept == 0) {
			$("#result_sender", this.obj_form).html($("glob_message", xml).text());
			$("filed", xml).each(
				function() {
					$("#result_"+$(this).attr("name"), obj_parent.obj_form).text($(this).text());
					$("[name = '"+$(this).attr("name")+"']", obj_parent.obj_form).css("background-color", "#F9F7A2");
				}
			)
		} else if (accept == 1) {
			obj_parent.accept(xml);
		}
	}
	
	/**
     * Позволит обращаться к предку
     * @type {object} 
     */
	this.response.parent = this;


	/**
	 * Функция обработки результата в случае успешной отправки   
	 * @function
	 * @param {string} xml Данные с сервера
	 */
	this.accept = function(xml) {		
	}
	
	this.clearDef = function() {
		$(":text, textarea", this.obj_form).each(function(index) {
			if ($(this).attr('def') == $(this).val()) {
				$(this).val("");
			}
		});
	}
	
	this.restoreDef = function() {
		$(":text, textarea", this.obj_form).each(function(index) {
			if (!$(this).val()) {
				$(this).val($(this).attr('def'));
			}
		});
	}
	
}
