var HomeLoanFormUI = {
	sendUrl:'',
	
	onLoadTypeChange: function()
	{
		if($('#cboLoanType').val() == 'New Home Loan')
		{
			$('tr.rowOld').hide();
			$('tr.rowNew').show();
			
			$('#txtBalance, #txtBank, #cboLoanYear, #cboLoanMonth, #cboType, #cboExpireMonth, #cboExpireYear, #cboHowApply').val('');
		}
		else
		{
			$('tr.rowOld').show();
			$('tr.rowNew').hide();
			$('#txtNewLoanAmount').val('');
			HomeLoanFormUI.onTypeChange();
		}
	},
	
	onTypeChange: function()
	{
		var typeVal = $('#cboType').val();
		if(typeVal == 'Fixed' || typeVal == 'Both')
		{
			$('#rowIfFixed').show();
		}
		else
		{
			$('#rowIfFixed').hide();
			$('#cboExpireMonth, #cboExpireYear').val('');
		}
	},
	
	sendEnquiry: function()
	{
		var vld = new FormValidator();
		
		vld.checkEmpty({'txtName' : 'Name is required. Please enter your name.',
					   'txtEmail' : 'Email is required. Please enter an email address.'});
		vld.checkEmail({'txtEmail' : 'Email is invalid. Please enter a valid email address.'});
		vld.checkEmpty({'txtPhoneM' : 'Mobile phone is required. Please enter a phone number.',
					   'txtPhoneW' : 'Work phone is required. Please enter a phone number.',
					   'txtPhoneH' : 'Home phone is required. Please enter a phone number.',
					   'cboLoanType': 'Type of home loan is required. Please select a type.'});
		
		if($('#cboLoanType').val() == 'New Home Loan')
		{
			vld.checkEmpty({'txtNewLoanAmount': 'New loan amount is required. Please enter an amount.'});
			vld.checkNumber({'txtNewLoanAmount': 'New loan amount is invalid. Please enter a valid amount.'});
		}
		else
		{
			vld.checkEmpty({'txtBalance': 'Current home loan balance is required. Please enter an amount.',
						   	'txtBank' : 'Institution/Bank name is required. Please enter a name.',
						   	'cboLoanYear' : 'Please select how long have you had the loan for?',
							'cboType' : 'Please select the type of your home loan interest rate.',
							'cboHowApply' : 'Please enter originally how did you apply for your current home loan?'});
			vld.checkNumber({'txtBalance' : 'Current home loan balance is invalid. Please enter a valid numeric value.'});
			
			var typeVal = $('#cboType').val();
			if(typeVal == 'Fixed' || typeVal == 'Both')
			{
				vld.checkEmpty({'cboExpireMonth' : 'Select a month when your fixed rate will expire.',
							   'cboExpireYear' : 'Select an year when your fixed rate will expire.'});
			}
		}
				
		
		if(!vld.isValid())
		{
			$('#result').html(vld.html()).attr('class','error').slideDown('fast');
			vld.focus();
			return false;
		}
		
		$U.showProgress();
		
		$.post(HomeLoanFormUI.sendUrl, $('#frmHomeLoan').fastSerialize(),
				function(response)
				{
					if(response.succeed)
					{
						$('#enquiry').slideUp('fast',
											  function()
											  {
												  $('#result').html(response.message).attr('class','message').slideDown('fast');
											  });
					}
					else
					{
						$('#result').html(response.message).attr('class','error').slideDown('fast');
					}
					
					$U.hideProgress();
				},'json');
		return false;
	},
	
	init: function()
	{
		$('#frmHomeLoan').bind('submit', HomeLoanFormUI.sendEnquiry);
		$('tr.rowOld').hide();
		$('tr.rowNew').hide();
		
		$('#cboType').bind('change', HomeLoanFormUI.onTypeChange);
		
		
		$('#cboLoanType').bind('change', HomeLoanFormUI.onLoadTypeChange);
	}
}
