

/*

	Class used to manage quick form events

*/

function QuickForm(formElement)
{
	//properties
	this.parentForm = formElement;
	this.optionIndividual = null;
	this.optionJoint = null;
	this.optionCorporate = null;
	this.optionMT4 = null;
	this.optionSpeedTrader = null;
	this.optionMirrorTrader = null;

	//methods
	this.Validate = function()
	{

		var self = this;
		var frm = self.parentForm;
		var isValid = true;
		var errMessage = '';

		

		//ensure live or demo is selected

		if(frm.find("input[name='account_type']:checked").length < 1)

		{

			isValid = false;

		}

		if(!isValid) { ShowMessage("Please indicate whether this will be a live or demo account.", false, 'ERROR - MISSING DATA'); return false; }

		

		//required field validation

		frm.find('.required:input[type=text], select.required').each

		(

			function()

			{

				if($(this).css('display') == 'none') { return; }

				$(this).css('background-color','#ebd800');

				if($(this).val() == '' || $(this).val() == $(this).attr('default'))

				{

					$(this).css('background-color','#ebd800');

					isValid = false;

				}

			}

		);

		if(!isValid) { ShowMessage("One or more required fields are missing.", false, 'ERROR - MISSING DATA'); return false; }

		

		//email validation

		frm.find('.required.email').each

		(

			function()

			{

				if($(this).css('display') == 'none') { return; }

				$(this).css('background-color', '');

				//ensure valid email address is entered

				var filter = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;

				if(String($(this).val()).search (filter) == -1)

				{

					$(this).css('background-color', '#ebd800');

					ShowMessage('Invalid email address', false, 'ERROR - INVALID DATA');

					isValid = false;

				}

			}

		);

		if(!isValid) { ShowMessage('Invalid email address', false, 'ERROR - INVALID DATA'); return false; }

		

		//date validation

		frm.find('.date').each

		(

			function()

			{

				if($(this).css('display') == 'none') { return; }

				$(this).css('background-color', '');

				var val = $(this).val();

				val = new Date(val);

				if(isNaN(val.getDate()))

				{

					$(this).css('background-color', '#ebd800');

					isValid = false;

				}

			}

		);

		if(!isValid) { ShowMessage("One or more date fields contain an invalid entry.", false, 'ERROR - INVALID DATA'); return false; }

		

		var action = '#';

		if(frm.find("input[name='account_type']:checked").val() == 'LIVE ACCOUNT')

		{

			switch($('#account_type').val())

			{

			case 'Individual':

				action = 'https://thinkforex.com/live/ind/index.php';

				break;

			case 'Joint':

				action = 'https://thinkforex.com/live/joint/index.php';

				break;

			case 'Corporate':

				action = 'https://thinkforex.com/live/corporation/index.php';

				break;

			}

		}

		else
		{

			//action = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
			if($('#account_type').val() == 'Metatrader 4')
			{

				$('#demo_type').val('Demo_Acct_Ind');
				$('#platform_type').val('mt4');
				action = 'https://' + window.location.hostname + '/forex/open-forex-demo-account-step2/';
				//$('#retURL').val('http://www.thinkforex.com/opendemoaccount-step2');

			}
			else if($('#account_type').val() == 'Speed Trader')
			{

				$('#demo_type').val('Demo_Acct_Ind_Currenex');
				action = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
				$('#retURL').val('http://www.thinkforex.com/opendemoaccount-currenex');

			}
			else if($('#account_type').val() == 'Mirror Trader')
			{
				$('#platform_type').val('mirror trader');
				action = 'https://' + window.location.hostname + '/forex/open-forex-demo-account/actions/submit_account_info.php';
				//action = 'https://www.thinkforex.com/forex/open-mirrortrader-demo-account/';
			}

		}

		frm.attr('action', action);

		//$('#phone').val($('#country_code').val().toString() + ' ' + $('#phone').val().toString());

		

		return true;

	};

	

	this.AssignDefaults = function()

	{

		var self = this;

		var frm = self.parentForm;

		

		//default field values

		frm.find('input').each

		(

			function()

			{

				$(this).blur

				(

					function()

					{

						if($(this).val() == '')

						{

							$(this).val($(this).attr('title'));

						}

					}

				);

				$(this).focus

				(

					function()

					{

						if($(this).val() == $(this).attr('title'))

						{

							$(this).val('');

						}

					}

				);

			}

		);

		

		return;

	}

	

	this.SwitchFormType = function()

	{

		var self = this;

		var frm = self.parentForm;

		//changes the name attributes on form fields to accomodate demo vs. live account submission, as processing scripts are expecting different element names

		var type = frm.find("input[name='account_type']:checked").val();

		if(type == "DEMO ACCOUNT")

		{

			frm.find('#first_name').attr('name', 'first_name');

			frm.find('#last_name').attr('name', 'last_name');

			frm.find('#country').attr('name', 'country');

			frm.find('#phone').attr('name', 'phone');

			frm.find('#primary_dob').css('display', 'none');

			frm.find('#email').addClass('fullwidth');

			frm.find('#email').removeClass('halfwidth');

			if(self.optionMT4)
			{
				self.optionMT4.appendTo(frm.find('#account_type'));
				self.optionMT4 = null;
			}
			if(self.optionSpeedTrader)
			{
				self.optionSpeedTrader.appendTo(frm.find('#account_type'));
				self.optionSpeedTrader = null
			}
			if(self.optionMirrorTrader)
			{
				self.optionMirrorTrader.appendTo(frm.find('#account_type'));
				self.optionMirrorTrader = null
			}

			self.optionIndividual = frm.find('#account_type option[value="Individual"]').detach();
			self.optionJoint = frm.find('#account_type option[value="Joint"]').detach();
			self.optionCorporate = frm.find('#account_type option[value="Corporate"]').detach();			

		}

		else

		{

			frm.find('#first_name').attr('name', 'primary_first_name');

			frm.find('#last_name').attr('name', 'primary_last_name');

			frm.find('#country').attr('name', 'primary_citizenship');

			frm.find('#phone').attr('name', 'phone_number');

			frm.find('#primary_dob').css('display', 'inline');

			frm.find('#email').addClass('halfwidth');

			frm.find('#email').removeClass('fullwidth');

			

			if(self.optionIndividual)
			{
				self.optionIndividual.appendTo(frm.find('#account_type'));
				self.optionIndividual = null;
			}
			if(self.optionJoint)
			{
				self.optionJoint.appendTo(frm.find('#account_type'));
				self.optionJoint = null;
			}

			if(self.optionCorporate)
			{
				self.optionCorporate.appendTo(frm.find('#account_type'));
				self.optionCorporate = null;
			}

			self.optionMT4 = frm.find('#account_type option[value="Metatrader 4"]').detach();
			self.optionMirrorTrader = frm.find('#account_type option[value="Mirror Trader"]').detach();
			self.optionSpeedTrader = frm.find('#account_type option[value="Speed Trader"]').detach();

		}

		frm.find('#account_type option[value=""]').attr('selected', 'selected');

		return;

	}

	

	this.AddHandlers = function()

	{

		var self = this;

		var frm = self.parentForm;

		

		//form submission

		frm.submit

		(

			function()

			{

				return self.Validate();

			}

		);

		

		//country lookup

		frm.find('#country').change

		(

			function()

			{

				if($(this).val() == '') //if value is empty, clear country code

				{

					frm.find('#country_code').val('');

				}

				else //send request for country code

				{

					var page = '../openliveaccount/get_country_code.php';

					var country = $(this).val();

					$.post(page, { country: country }, function(data) { frm.find('#country_code').val(data); });

				}

			}

		);

		

		//account type change

		frm.find("input[name='account_type']").change

		(

			function()

			{

				self.SwitchFormType();

			}

		);

		

		self.SwitchFormType();

		self.AssignDefaults();

		return;

	}

	

	return true;

}


