/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/**
 * @author 		Justin Ferrara
 * @copyright 	December 2008
 * @section		Back End
 * @content		Link Side Scripts
**/
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

function is_email(str_email)
{
	if(str_email == "") 
	{
		return false;
	}
	else
	{
		var obj_regex = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
		return obj_regex.test(str_email);
	}
}

function setError( fid )
{
	var field = $( '#' + fid );
	var error = $( '#err_form' );

	field.addClass( 'required' );
	error.show( 200 );
}

function clearError( fid )
{
	var field = $( '#' + fid );
	var error = $( '#err_form' );

	field.removeClass( 'required' );
	error.hide( 200 );
}

function validateContactSide()
{
	/* Form attributes */
	var myForm	= $( '#form_contact'	);
	var hidden	= $( '#save_contact'	);
	var button	= $( '#btn_submit'		);
	var action	= myForm.attr( 'action' );
	var error 	= $( '#err_form' );

	var txtMessage	= $( '#txt_message' );
	var txtPhone	= $( '#txt_phone'	);
	var txtEmail	= $( '#txt_email'	);
	var txtSess 	= $( '#session_id'	);

	if( txtSess.val().length < 1 )
	{
		self.parent.Shadowbox.close();
	}

	var blnFlag = true;

	error.hide();

	if( txtPhone.val().length < 1 )
	{
		error.html( 'Field(s) in red are required' );
		setError( 'txt_phone' );
		blnFlag = false;
	}
	else
	{
		clearError( 'txt_phone' );
	}

	if( txtEmail.val().length < 1 )
	{
		error.html( 'Field(s) in red are required' );
		setError( 'txt_email' );
		blnFlag = false;
	}
	else
	{
		if( !is_email( txtEmail.val() ) )
		{
			error.html( 'Field(s) in red are required<br/>Email must be in proper format. i.e. somebody@mail.com' );
			setError( 'txt_email' );
			blnFlag = false;
		}
		else
		{
			clearError( 'txt_email' );
			if( txtPhone.val().length < 1 ) setError( 'txt_phone' );		
		}
	}

	if( blnFlag )
	{
		hidden.val( '1' );
		return true;
	}
	else
	{
		hidden.val( '0' );
		return false;
	}
}

$( document ).ready(
	function()
	{

		/* Form attributes */
		var myForm	= $( '#form_contact'	);
		var hidden	= $( '#save_contact' 	);
		var button	= $( '#btn_save'		);
		var action	= myForm.attr( 'action' );

		/* Form elements */
		var txtMessage 	= $( '#txt_message' );
		var txtPhone	= $( '#txt_phone'	);
		var txtEmail	= $( '#txt_email'	);
		var txtSess 	= $( '#session_id'	);

		/* Ajax Modifier Divs */
		var hideDiv		= $( '#form_contact');
		var showDiv		= $( '#ajax-load'	);
		var error		= $( '#err_form'	);
		var divMessage 	= self.parent.$( '#contact_box_message' );
		var divPhone 	= self.parent.$( '#contact_box_phone'	);
		var divEmail 	= self.parent.$( '#contact_box_email'	);

		if( txtSess.val().length < 1 )
		{
			self.parent.Shadowbox.close();
		}

		error.hide();

		myForm.submit(
					function(e)
					{
						e.preventDefault();

						if( validateContactSide() )
						{
							button.attr( 'disabled', 'disabled' );
							hideDiv.fadeOut( 
											1000, 
											function()
											{
												showDiv.html( '<img src="/lib/images/ajax-loader.gif" alt="Loading..." title="Loading..." width="31" height="31" />' );
												showDiv.fadeIn( 1200 );
											});
							$.post(
									action,
									{ txt_message: txtMessage.val(), txt_phone: txtPhone.val(), txt_email: txtEmail.val(), txt_session: txtSess.val(), save_contact: hidden.val() }, //Enter Post Variables to send to Form Process
  									function( data )
									{
										if( data )
										{
											divMessage.text(data.message);
											divPhone.text( 	data.phone	);
											divEmail.text( 	data.email	);
	
											self.parent.Shadowbox.close();
										}
									},
									"json");
						}					

						return false;
					});
	}
);
