/**
* Friends mailer
* @version 0.2
*
* Changelog:
*	0.2	23.05.10/goshi	new version
*/

// constructor
function FMailer(objid, add_id, ajx_href){

	this._init(objid, add_id, ajx_href); 
	
	
}

// prototype
FMailer.prototype = {

	_elem: '',
	_container: null,
	_add_id: '',
	_ajx_href: '',	
	_prefix : 'ch_elem',
	_callback : null,
	
	_init: function(params){
		
		this.addParams(params);
		
	},
	
	addParams : function (params){
		
		if (typeof params != 'undefined' && params != null){
		
			for (var i in params){
				
				if (typeof this['_'+i] != 'undefined'){
					
					this['_'+i] = params[i];
				}			
			}		
		}	
	},
	
	_addEvents : function(){
	
		if ($_('frmFMailer'+this._add_id)){
		
			//var _always_submit = false;
			var athis = this;
			
			$_('frmFMailer'+this._add_id).setAttribute('action', '');
			portal.events.attach($_('frmFMailer'+this._add_id), "submit", function (e){
					
				e.returnValue = false;
				e.preventDefault ? e.preventDefault() : '';
				
				athis.Save();	
				/*if (_always_submit){
					return e.returnValue;
				} else {
					//e.returnValue = athis._checkForm(e, athis);
						
					//if (!e.returnValue){
					//	e.preventDefault ? e.preventDefault() : '';
					//} else {
					_always_submit = true;
					if (typeof callback == 'function'){
			 			portal.events.remove($_('frmFMailer'+this._add_id), "submit");
			 			callback(container);
			 				portal.ajax.load(null, result, container.insert);
			 			container.clear(e);
			 		
			 		}
					//}
						
				}*/
				
				
				return e.returnValue;
			}, false);
			
		}
	
	},

	Load: function(){
			
		var athis = this;
		
		var req = new JsHttpRequest();
		req.onreadystatechange = function (){
			if (req.readyState == 4){
				// calling callback function with closures
				if (typeof athis._callback == 'function')
					athis._callback(null, req.responseJS.response, null);
				else
					$_(athis._elem).innerHTML = req.responseJS.response;

				portal.hash.set("oFMailer_"+athis._add_id);
				
				athis._addEvents();
			}
		};
		req.open(null, athis._ajx_href, true);	
		req.send({'objid': athis._add_id});
	
	},
	
	_CheckFields: function(){
	
		var athis = this;
		
		var obj = $_('your_email');
		if (obj.value != '' && email_reg.test(obj.value) == false){
			obj.focus();
			return portal.mess.errors.email_address_not_valid;
		}
		
		obj = $_('friend_email');
		if (obj.value != '' && email_reg.test(obj.value) == false){
			obj.focus();
			return portal.mess.errors.email_address_not_valid;
		}
		
		obj = $_('title');
		if (obj.value == ''){
			obj.focus();
			return portal.mess.errors.data_not_valid;
		}

		return false;	
	
	},


	Save: function(){
	
		var athis = this;		
		
		// checking fields
		var error = '';//athis._CheckFields();
		
		// checking for input data
		if (error == ''){
			var req = new JsHttpRequest();
			req.onreadystatechange = function (){
	
				if (req.readyState == 4){
					if (typeof athis._callback == 'function')
						athis._callback(null, req.responseJS.response, null);
					else
						$_(athis._elem).innerHTML = req.responseJS.response;
					portal.hash.set("oFMailer_"+athis._add_id);

					athis._addEvents();
							
				}
				
			};
		
			req.open('POST', athis._ajx_href, true);
			req.send({
				'ch_elem[your_email]': $_('your_email').value,
				'ch_elem[friend_email]': $_('friend_email').value,
				'ch_elem[title]': $_('title').value,
				'ch_elem[comments]': $_('comments').value,
				'hh': $_('hh').value,
				'ch_elem[tuning]': $_('tuning').value,
				'add_elem': 'yes', 
				'objid': athis._add_id});
		} else {

			alert(error);
		
		}
	},
	
	Close : function(){
		if (this._container != null){
			this._container.destroy(this._container);		
		} else if (this._elem != null)
			this._elem.style.display = 'none';
	}

}
