if(!Member) {
	var Member = new Class({
		options: {
			checkUrl: ''
		},
		
		initialize: function(id, options) {
			this.setOptions(options);
			
			this.form = $('login' + id).addEvent('submit', this.check.bind(this, null, true));
		},
		
		check: function(ev) {
			ev.stop();
			TSF.post(this.options.checkUrl, this.form.toQueryString(), function(r){
				var result = eval('(' + r + ')'), time = new Date().getTime(),
					errorElm, phraseElm, captchaElm, captchaContainer;
				
				errorElm		= this.form.getElement('div.login-error');
				phraseElm		= this.form.getElement('input.login-captcha');
				captchaElm		= this.form.getElement('img.login-captcha');
				captchaContainer= this.form.getElement('div.captcha');
				
				if (result.status) {
					location = location.href;
					return true;
				}
				
				errorElm.setHTML(result.errorMessage);
				
				if (result.error == 4 || (result.error == 3 && result.captcha)) {
					captchaContainer.setStyle('display', 'block');
					captchaElm.src = result.captcha;
					phraseElm.value = result.captchaId;
					return false;
				} else {
					captchaContainer.setStyle('display', 'none');
				}
				
			}.bind(this))
		}
	});
	Member.implement(new Events, new Options);
}

var Member_profile = new Class({
	options: {
		checkUrl: '',
		action: '',
		errorElem : ''
	},

	initialize: function( options) {
		this.setOptions(options);
		this.form = $('member_profile').addEvent('submit', this.check.bind(this, null, true));
	},
	
	
	formValidate : function (FieldNames)
	{
		if( !TSF.FormValidate.checkAllFormFields( 'member_profile', FieldNames, this.options.errorElem, fill_required_fields ) ) return false;
		return true;
	},
	validateFieldValue : function ( FieldName, filter, message)
	{
		if( !TSF.FormValidate.validateFieldValue( 'member_profile', FieldName, filter, this.options.errorElem, message ) ) return false;
		return true;
	},
	
	passwordCheck : function (pass , rePass)
	{
		var password 	= $('member_profile').getElement(pass);
		var repassword 	= $('member_profile').getElement(rePass);
		
		if(password != '' && repassword.value != password.value)
		{
			TSF.FormValidate.showMessageError( this.options.errorElem, repassword_dont_match );
			TSF.FormValidate.errorFieldStyle(repassword);
			TSF.FormValidate.errorFieldStyle(password);
			return false;
		}
		
		return true;
	},
	
	register_validate : function()
	{
		var FieldNames = new Array();
		
		FieldNames.push('input[name=firstname]');
		FieldNames.push('input[name=lastname]');
		
		if( email_required  )	FieldNames.push('input[name=email]');
		if( usernameRequire )	FieldNames.push('input[name=username]');
		
		if(passwordRequire){
			FieldNames.push('input[name=password]');
			FieldNames.push('input[name=repassword]');
		}
		FieldNames.push('select[name=ostan]');
		FieldNames.push('select[name=city]');
	
		if( !this.formValidate( FieldNames ) ) return false;
		
		if( !this.validateFieldValue( 'input[name=email]', 'mail', invalid_email ) ) 				return false;
		if( !this.validateFieldValue( 'input[name=postalcode]', 'zipcode', invalid_postalcode ) ) 	return false;
		
		if(passwordRequire || special_register == 'active_with_account')
			if( !this.passwordCheck('input[name=password]','input[name=repassword]')) 				return false;
		
		return true;
	},
	
	edit_validate : function()
	{
		var FieldNames = new Array();
		
		FieldNames.push('input[name=firstname]');
		FieldNames.push('input[name=lastname]');
		
		if( email_required  )	FieldNames.push('input[name=email]');
		
		FieldNames.push('select[name=ostan]');
		FieldNames.push('select[name=city]');
		
		if( !this.formValidate( FieldNames ) ) return false;
		if( !this.validateFieldValue( 'input[name=email]', 'mail', invalid_email ) ) 				return false;
		if( !this.validateFieldValue( 'input[name=postalcode]', 'zipcode', invalid_postalcode ) ) 	return false;
		
		return true;
	},
	change_password_validate : function()
	{
		var FieldNames = new Array();
		
		FieldNames.push('input[name=current_password]');
		FieldNames.push('input[name=password]');
		FieldNames.push('input[name=repassword]');
		
		if( !this.formValidate( FieldNames ) ) return false;
		if( !this.passwordCheck('input[name=password]','input[name=repassword]')) 					return false;
		
		return true;
	},
	forget_pass_validate : function()
	{
		var FieldNames = new Array();
		
		FieldNames.push('input[name=member_username]');
		FieldNames.push('input[name=member_email]');
		
		if( !this.formValidate( FieldNames ) ) return false;
		if( !this.validateFieldValue( 'input[name=member_email]', 'mail', invalid_email ) ) 		return false;
		
		return true;
	},
	check: function(ev) 
	{
		switch ( this.options.action )
		{
			case 'edit' 			: validateStatus = this.edit_validate(); 			break
			case 'register' 		: validateStatus = this.register_validate(); 		break;
			case 'change_password' 	: validateStatus = this.change_password_validate(); break;
			case 'forget_pass' 		: validateStatus = this.forget_pass_validate(); 	break;
		}
		

		if(!validateStatus)
			return false;
			
		ev.stop();
		
		TSF.page(this.options.checkUrl, this.form.toQueryString(), 'post');
	}
});
Member_profile.implement(new Events, new Options);


var IncomeInviteFriends = new Class({
	options: {
		checkUrl: '',
		action: ''
	},

	initialize: function( options) {
		this.setOptions(options);
		this.form = $('income_invite_friends').addEvent('submit', this.check.bind(this, null, true));
	},
	
	check: function(ev) 
	{
		$('invalid_email').style.display = 'none';
		
		var comSp = new Array();
		comSp =  $('friends_email').value.split(/\r\n?|\n/);
		
		if(comSp.length < 1)
		{
			$('invalid_email').style.display = '';
			$('friends_email').focus();
			$('friends_email').addClass('require_field_focuse');
			return false;
			
		}
		for(var i=0; i< comSp.length; i++) 
		{
			email = comSp[i];
			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
			var email_val=emailfilter.test(email);
			
			if(!email_val)
			{
				$('invalid_email').style.display = '';
				$('friends_email').focus();
				$('friends_email').addClass('require_field_focuse');
				return false;
			}
		}
	
		ev.stop();
		TSF.post(this.options.checkUrl, this.form.toQueryString(), function(r){
			
			$('invite_friend_form').style.display = 'none';
			if(r == 'Error')
			{
				$('invite_friend_not_done').style.display = '';
			}else{
				$('invite_friend_done').style.display = '';
			}
		}.bind(this))
	}
});
IncomeInviteFriends.implement(new Events, new Options);

