// JavaScript Document
var InsuranceFormUI = {
	sendUrl:'',
	
	isAllEmpty: function(selection)
	{
		var result = true;
		$(selection).each(
			function()
			{
				var val = $(this).val();
				if($.trim(val) != '')
				{
					result = false;
					return;
				}
			});
		
		return result;
	},
	
	sendRequest: function()
	{
		var vld = new FormValidator();
		
		var isCouple = $('#rdoCouple').attr('checked');
		
		var msgPrefix = '';
		var msgPrefix2 = 'Insured Person 2 : ';
		
		if(isCouple)
		{
			msgPrefix = 'Insured Person 1 : ';
		}
		
		vld.checkEmpty({'cboTitle' : msgPrefix + 'Title is required. Please select your title.',
					   'txtFirstName' : msgPrefix + 'First name is required. Please enter your first name.',
					   'txtLastName' : msgPrefix + 'Last name is required. Please enter your last name.',
					   'txtEmail' : msgPrefix + 'Email is required. Please enter an email address.'});
		
		vld.checkSelected({'optGender': msgPrefix + 'Gender is required. Please select a gender.'});
		
		vld.checkEmail({'txtEmail' : msgPrefix + 'Email is invalid. Please enter a valid email address.'});
		
		
		vld.checkEmpty({'txtBirthDate' : msgPrefix + 'Birth date is required. Please select your birth date.',
					   'cboMaritalStatus' : msgPrefix + 'Marital status is required. Please select your marital status.',
					   'txtOccupation' : msgPrefix + 'Occupation is required. Please enter your occupation.'});
		
		vld.checkSelected({'optSmoker': msgPrefix + 'Smoker status is required. Please select your status.'});		
		
		vld.checkEmpty({'txtAddress' :  msgPrefix + 'Birth date is required. Please select your birth date.',
					   'txtSuburb' :  msgPrefix + 'Marital status is required. Please select your marital status.',
					   'cboState' :  msgPrefix + 'Occupation is required. Please enter your occupation.',
					   'txtPostCode' :  msgPrefix + 'Post code is required. Please enter your postcode.'});
		
		if($('#txtPhoneM').val() == '' && $('#txtPhoneH').val() == '' && $('#txtPhoneW').val() == '')
		{
			vld.checkEmpty({'txtPhoneM':  msgPrefix + 'Contact number is required. Please enter atleast one contact number.'});
		}
		
		if(InsuranceFormUI.isAllEmpty('#txtTLCover, #txtTPDCover, #txtTraumaCover, #txtAnnualIncome'))
		{
			vld._addError('txtTLCover', msgPrefix + 'At least one cover information is required. Please enter value for at least one of them.');
		}
		
		if($('#txtAnnualIncome').val() != '')
		{
			vld.checkSelected({'optPolicyType':  msgPrefix + 'Policy type is required. Please select a policy type.'});
			vld.checkEmpty({'cboWaitingPeriod':  msgPrefix + 'Waiting period is required. Please select waiting period.',
					   'cboBenefitPeriod':  msgPrefix + 'Benefit period is required. Please select benefit period.'});
		}
		
		/*vld.checkEmpty({'txtTLCover' :  msgPrefix + 'Term life cover is required. Please enter an amount.',
					   'txtTPDCover' :  msgPrefix + 'Total permanent disability (TPD) cover is required. Please enter an amount.',
					   'txtTraumaCover' :  msgPrefix + 'Trauma cover is required. Please enter an amount.',
					   'txtAnnualIncome' :  msgPrefix + 'Annual income is required. Please enter an amount.',
					   'cboWaitingPeriod':  msgPrefix + 'Waiting period is required. Please select waiting period.',
					   'cboBenefitPeriod':  msgPrefix + 'Benefit period is required. Please select benefit period.'});*/
		
		
		
		vld.checkNumber({'txtTLCover' :  msgPrefix + 'Term life cover is invalid. Please enter a valid numeric value.',
					   'txtTPDCover' :  msgPrefix + 'Total permanent disability (TPD) cover is invalid. Please enter a valid numeric value.',
					   'txtTraumaCover' :  msgPrefix + 'Trauma cover is invalid. Please enter a valid numeric value.',
					   'txtAnnualIncome' :  msgPrefix + 'Annual income is invalid. Please enter a valid numeric value.'});
		
		
		if(isCouple)
		{
			vld.checkEmpty({'cboTitle2' : msgPrefix2 + 'Title is required. Please select your title.',
					   'txtFirstName2' : msgPrefix2 + 'First name is required. Please enter your first name.',
					   'txtLastName2' : msgPrefix2 + 'Last name is required. Please enter your last name.',
					   'txtEmail2' : msgPrefix2 + 'Email is required. Please enter an email address.'});
			
			vld.checkSelected({'optGender2': msgPrefix2 + 'Gender is required. Please select a gender.'});
			vld.checkEmail({'txtEmail2' : msgPrefix2 + 'Email is invalid. Please enter a valid email address.'});
			vld.checkEmpty({'txtBirthDate2' : msgPrefix2 + 'Birth date is required. Please select your birth date.',
					   'cboMaritalStatus2' : msgPrefix2 + 'Marital status is required. Please select your marital status.',
					   'txtOccupation2' : msgPrefix2 + 'Occupation is required. Please enter your occupation.'});
			vld.checkSelected({'optSmoker2': msgPrefix2 + 'Smoker status is required. Please select your status.'});
			
			vld.checkEmpty({'txtAddress2' :  msgPrefix2 + 'Birth date is required. Please select your birth date.',
					   'txtSuburb2' :  msgPrefix2 + 'Marital status is required. Please select your marital status.',
					   'cboState2' :  msgPrefix2 + 'Occupation is required. Please enter your occupation.',
					   'txtPostCode2' :  msgPrefix2 + 'Post code is required. Please enter your postcode.'});
			if($('#txtPhoneM2').val() == '' && $('#txtPhoneH2').val() == '' && $('#txtPhoneW2').val() == '')
			{
				vld.checkEmpty({'txtPhoneM2':  msgPrefix2 + 'Contact number is required. Please enter atleast one contact number.'});
			}
			
			if(InsuranceFormUI.isAllEmpty('#txtTLCover2, #txtTPDCover2, #txtTraumaCover2, #txtAnnualIncome2'))
			{
				vld._addError('txtTLCover2', msgPrefix2 + 'At least one cover information is required. Please enter value for at least one of them.');
			}
			
			if($('#txtAnnualIncome2').val() != '')
			{
				vld.checkSelected({'optPolicyType2':  msgPrefix2 + 'Policy type is required. Please select a policy type.'});
				vld.checkEmpty({'cboWaitingPeriod2':  msgPrefix2 + 'Waiting period is required. Please select waiting period.',
						   'cboBenefitPeriod2':  msgPrefix2 + 'Benefit period is required. Please select benefit period.'});
			}
			
			/*
			vld.checkEmpty({'txtTLCover2' :  msgPrefix2 + 'Term life cover is required. Please enter an amount.',
						   'txtTPDCover2' :  msgPrefix2 + 'Total permanent disability (TPD) cover is required. Please enter an amount.',
						   'txtTraumaCover2' :  msgPrefix2 + 'Trauma cover is required. Please enter an amount.',
						   'txtAnnualIncome2' :  msgPrefix2 + 'Annual income is required. Please enter an amount.',
						   'cboWaitingPeriod2':  msgPrefix2 + 'Waiting period is required. Please select waiting period.',
						   'cboBenefitPeriod2':  msgPrefix2 + 'Benefit period is required. Please select benefit period.'});
			
			vld.checkSelected({'optPolicyType2':  msgPrefix2 + 'Policy type is required. Please select a policy type.'});
			*/
			
			vld.checkNumber({'txtTLCover2' :  msgPrefix2 + 'Term life cover is invalid. Please enter a valid numeric value.',
					   'txtTPDCover2' :  msgPrefix2 + 'Total permanent disability (TPD) cover is invalid. Please enter a valid numeric value.',
					   'txtTraumaCover2' :  msgPrefix2 + 'Trauma cover is invalid. Please enter a valid numeric value.',
					   'txtAnnualIncome2' :  msgPrefix2 + 'Annual income is invalid. Please enter a valid numeric value.'});			
		}		
		
		if(!vld.isValid())
		{
			$('#result').html(vld.html()).attr('class','error').fadeIn('fast');
			scroll(0,0);
			return false;
		}
		
		$U.showProgress();
		$.post(InsuranceFormUI.sendUrl, $('#frmInsurance').fastSerialize(),
			   function(response)
			   {
				   if(response.succeed)
				   {
					   $('#record_entry').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;
	},
	
	typeChanged: function()
	{
		if($('#rdoIndividual').attr('checked'))
		{
			$('#form-content').show();
			$('#section-two').hide();
			$('h2.title-insurance-1').html('Personal Details');
		}
		else if($('#rdoCouple').attr('checked'))
		{
			$('#form-content').show();
			$('#section-two').show();
			$('h2.title-insurance-1').html('Insured Person 1');
			$('h2.title-insurance-2').html('Insured Person 2');
		}
		else
		{
			$('#form-content').hide();
			$('#section-two').hide();
		}
		
		$('#result').slideUp();
	},
	
	init: function()
	{
		$('#frmInsurance').bind('submit', InsuranceFormUI.sendRequest);
		
		$('#form-content').hide();
		$("input[name='rdoInsuranceType']").bind('click', InsuranceFormUI.typeChanged)
		
		var dt = new Date();
		var yearRange = '1910:' + dt.getFullYear();
		$('input.cal').calendar({'speed':'', yearRange: yearRange});
		
		jQuery("#txtOccupation").suggest(serviceUrl + "/searchOccupation",{});
	}
}