oRcsf.RegisterObject('Module_Contact_Tafform');
var RCSF_Project_Module_Contact_Tafform =
{
	 msExtends	: 'RCSF_Base_Form'
	 
	/**
	 * Handles things that should be done after the document has fully loaded
	 *
	 */
	,OnDomLoaded : function()
	{
		// Assign form handlers
		var o_form = this.moForm = $('btn_submit').up('form');
		$('btn_submit').observe('click', this.SubmitForm.bind(this, o_form));
		$('btn_previewemail').observe('click', this.PreviewEmail.bind(this, o_form));
		$('btn_showvoorwaarden').observe('click', function(){
			oRcsf.moSite.OpenVoorwaarden();
		});
		$('btn_close').observe('click', this.CloseVoorwaarden.bind(this));
		$('btn_close_preview').observe('click', this.ClosePreview.bind(this));
		$('a_main-s_personal_text').focus();
		//$('btn_resetform').observe('click',this.Reset.bind(this));
	}
	
	
	
	/**
	 * Submits the form
	 */
	,SubmitForm : function(oForm,event)
	{
		event.stop();
		
		var o_request_options = {
			s_mode			: 'data'
			,s_module		: 'contact'
			,s_controller	: 'taf'
			,s_action		: 'submitform'
			,o_scope		: this
			,s_callback		: 'OnFormSubmitted'
		};
		//Object.extend(o_request_options,this.moCallback);

		this.moCore.RequestServer(
			this.GetFormData(oForm)
			,o_request_options
		);
		
		return false;
	}
	
	
	,OnFormSubmitted : function(oData)
	{
		this.HideAllErrors();
		if(oData.s_status == 'error')
		{
			this.ShowFieldErrors(oData);
		}
		else
		{
			//printr(oData.a_result.a_main);
			var s_type = 'website';
			var i_id = 0;
			if(oData.a_result.a_main.i_article_id > 0)
			{
				var s_type = 'artikel - ' + oData.a_result.s_title;
			}
			if(oData.a_result.a_main.i_newsitem_id > 0)
			{
				var s_type = 'nieuwsbericht - ' + oData.a_result.s_title;;
			}
			
			// track the vertelverder form
			pageTracker._trackEvent('Formulier','Vertelverder',s_type);
			
			$('taf_confirmation_text').update(oData.s_confirmation);
			Effect.Fade('tafform_content',{duration: 0.5,afterFinish : function(){
				Effect.Appear('taf_confirmation',{duration: 0.5 });
			}});
		}
	}
	
	,Reset : function() {
		Effect.Fade('taf_confirmation',{duration: 0.5,afterFinish : function(){
			Effect.Appear('tafform_content',{duration: 0.5 });
			$('taf_confirmation_text').update('');
			$('taferror').update('');
			$('a_main-s_friend_email').clear();
			$('a_main-s_friend_firstname').clear();
			$('a_main-s_friend_firstname').focus();
		}});
	}
	
	,ShowFieldErrors : function(oData) 
	{
		var i_count = 0;
			
		$('taferror').update(oData.s_message);
	
		for (var s_field in oData.a_errors)
		{
			// Set reference to current field
			var o_cur_label = $('label-' + s_field);
			var o_cur_field = oData.a_errors[s_field];
			
			// Check if there is an error message for that field
			if(o_cur_label)
			{
				if(i_count < 1) $(s_field).focus(); i_count++;
				o_cur_label.addClassName('error');
				$(s_field).addClassName('error');
				i_count++;
			}
		}
	}
	
	/**
	 * Submits the form
	 */
	,PreviewEmail : function(oForm,event)
	{
		event.stop();
		
		var o_request_options = {
			s_mode			: 'data'
			,s_module		: 'contact'
			,s_controller	: 'taf'
			,s_action		: 'previewemail'
			,o_scope		: this
			,s_callback		: 'OnEmailPreview'
		};

		this.moCore.RequestServer(
			this.GetFormData(oForm)
			,o_request_options
		);
		
		return false;
	}
	
	/**
	 * 
	 */
	,OnEmailPreview : function(oData)
	{
		this.HideAllErrors(oData);
		
		if (oData.s_status == 'done')
		{
			$('email_preview_content').update(oData.s_html);
			new Dialog.Box('preview_text');
			$('preview_text').show();
		}
		else
		{
			this.ShowFieldErrors(oData);
		}
	}
	
	
	,HideAllErrors : function() 
	{
		// Swaps the old error fields to hidden
		$$('.field_info .error').each(function(oEl)
			{
				oEl.removeClassName('error');
			}
		);
	}

	,ClosePreview : function() 
	{
		$('preview_text').hide();
	}
}
	