$(function() {
    
	var numStepId = getCurrentStepId();
	
	var chrFormId = $('.boxOuter').attr('id');
	
	//back/next logic
	
	if(chrFormId == 'step22a'){
		$('#nextBtn').hide();
	}
	
	if ((numStepId == 1 || numStepId == '21' || numStepId == '22') && chrFormId != 'step22a') {
		$('#backBtn').hide();
	}
	else { 
		$('#backBtn').click(function(event) {
			event.preventDefault();
			var numStepId = getStepId(chrFormId);
			var policy_numRiders = getNumRiders();
			var proposer_conviction_numConvictions = getProposerNumConvictions(chrFormId);
			var proposer_claim_numClaims = getProposerNumClaims(chrFormId);
			var drivers_conviction_numConvictions = getDriversNumConvictions(chrFormId);
			var drivers_claim_numClaims = getDriversNumClaims(chrFormId);
			var total_conviction_numConvictions = proposer_conviction_numConvictions + drivers_conviction_numConvictions;
			var total_claim_numClaims = proposer_claim_numClaims + drivers_claim_numClaims;
			//moving backwards from last to first
			showBackModalTimer();
			if (numStepId == 2 || numStepId == 3) {
				//step 3 main
				if (chrFormId == 'step3') {
					if (total_claim_numClaims) {
						chrNextStepId = '2c';
					} else {
						if (total_conviction_numConvictions) {
							chrNextStepId = '2b';
						} else {
							if (policy_numRiders) {
								chrNextStepId = '2a&numRider=' + policy_numRiders;
							} else {
								chrNextStepId = '2';
							}
						}
					}
				}
				//step 2c
				if (chrFormId == 'step2c') {
					//additional logic for additional riders here
				
					if (total_conviction_numConvictions) {
						chrNextStepId = '2b';
					} else {
						if (policy_numRiders) {
							chrNextStepId = '2a&numRider=' + policy_numRiders;
						} else {
							chrNextStepId = '2';
						}
					}		
				}
				//step 2b
				//alert(chrFormId);
				if (chrFormId == 'step2b') {
					if (policy_numRiders) {
						chrNextStepId = '2a&numRider=' + policy_numRiders;
					} else {
						chrNextStepId = '2';
					}
				}
				//step 2a
				if (chrFormId == 'step2a') {
					var numRider = parseInt($('#numRider').val());
					if (policy_numRiders > 1 && numRider > 1) {
						var numNextRider = numRider - 1;
						chrNextStepId = '2a&numRider=' + numNextRider;
					} else {
						chrNextStepId = '2';
					}
				}
				//step 2
				
				if (chrFormId == 'step2') {
					chrNextStepId = '1';
				}
				window.location = 'index.cfm?go=quote.step' + chrNextStepId;	
			} else {
				//step 4 has been removed so step 5 steps straight back to step 3
				if (numStepId == 5) {
 					window.location =  $('#nonSecureDomain').text() + '/index.cfm?go=quote.step3';
				} else {
					window.location = 'index.cfm?go=quote.step' + --numStepId;
				}
				
			}
		
		    
		});
		
		
		
		if (numStepId == 6) {
			//initially disable the EU cover if UK value = N
			// as you must have UK cover to include European cover
			if($("input[@name='addon_breakdown_UK']:checked").val() == "N"){
				$("input[@name='addon_breakdown_EU']").attr('disabled', true);
			    $("input[@name='addon_breakdown_EU']:eq(1)").attr('checked', true);
		    	updateSession('addon_breakdown_EU','N');
			}
			
			
			//Add a click event on the UK radio buttons 
			$("input[@name='addon_breakdown_UK']").click(function(event) {
				if(this.value == "UK"){
					//enable the EU cover buttons if UK cover selected
					$("input[@name='addon_breakdown_EU']").attr('disabled', false);
				}
			    if(this.value == "N"){
			    	// disable the EU buttons and set the session value for EU to N
			    	$("input[@name='addon_breakdown_EU']:eq(1)").attr('checked', true);
			    	updateSession('addon_breakdown_EU','N');
					$("input[@name='addon_breakdown_EU']").attr('disabled', true);	
				}
			}) // end of click event on UK cover radio buttons
			
		}
	}
	
	
	
	if (numStepId == 5) {
		$('#nextBtn').hide();
		
		$('#AdjustCoverBtn').click(function(event) {
			event.preventDefault();
			$('#clickAdjust').hide();
			$('#adjust').hide();
			$('#adjusting').show();
			
			if (validateStep(numStepId)){
				// validated ok carry on
				showAdjustTimer(); 
				setTimeout (function() {window.location = 'index.cfm?go=quote.step' + numStepId;}, 1000 );
				
			}
			
		})
		
		if ( $('#proposer_terms2').length ) {
		  $('#proposer_terms2').click(function(event) {
			if($('#proposer_terms2:checked').val() == "Y"){
				//reload the page so that the results email is sent
				location.reload();
			}
		})
 		}
		
		
		//cover if changed add the preventClck attribute to buynow buttons
		$('#policy_cover').change(function(event) {
			//find the buy now butttons and disable them
			//$('input[type=image]').attr({preventClick: 'yes'});
			$('#adjust').hide();
			$('#clickAdjust').show();
		});
		
		//Voluntary Excess if changed add the preventClck attribute to buynow buttons
		$('#policy_volxs').change(function(event) {
			//find the buy now butttons and disable them
			//$('input[type=image]').attr({preventClick: 'yes'});
			$('#adjust').hide();
			$('#clickAdjust').show();
		});
		
		$('input[name=policy_protectedBonus]').click(function(event) {
			$('#adjust').hide();
			$('#clickAdjust').show();
		});
		
		//buynow buttons
		$('.buyNowButton').click(function(event) {
			// get the id of the quote the user has selected from the buynow image
			event.preventDefault();
			
			var quoteId = $(this).attr('id');
			// if the volxs or cover havent been amended then allow the user to buy
			// otherwise they must adjust the quote
			//if ($(this).attr('preventclick') != 'yes'){
			
			if ($('#clickAdjust').is(':visible')) {
				showModal("Adjust Cover","As you have adjusted your cover requirements, please click on the <span style='color:red'>Adjust Cover</span> button before you can proceed to payment.");
			} else {
				showModalTimer();
				window.location = 'index.cfm?go=quote.step' + 6 + "&step=6&quoteId=" + quoteId;
			}
			
		});
		

		
	} else {
		$('#nextBtn').click(function(event) {
			event.preventDefault();
			var numStepId = getStepId(chrFormId);
			if (validateStep(numStepId)) {
				var policy_numRiders = getNumRiders();
				var proposer_conviction_numConvictions = getProposerNumConvictions(chrFormId);
				var proposer_claim_numClaims = getProposerNumClaims(chrFormId);
				var drivers_conviction_numConvictions = getDriversNumConvictions(chrFormId);
				var drivers_claim_numClaims = getDriversNumClaims(chrFormId);
				var total_conviction_numConvictions = proposer_conviction_numConvictions + drivers_conviction_numConvictions;
				var total_claim_numClaims = proposer_claim_numClaims + drivers_claim_numClaims;
				//moving forwards from first to last
				if (numStepId == 2) {
					//step 2 main
					if (chrFormId == 'step2') {
						if (policy_numRiders) {
							chrNextStepId = '2a&numRider=1';
						} else {
							if (total_conviction_numConvictions) {
								chrNextStepId = '2b';
							} else {
								if (total_claim_numClaims) {
									chrNextStepId = '2c';
								} else {
									chrNextStepId = '3';
								}
							}
						}
					}
					//step 2a
					if (chrFormId == 'step2a') {
						//additional logic for additional riders here
						var numRider = parseInt($('#numRider').val());
						if (policy_numRiders > numRider) {
							var numNextRider = numRider + 1;
							chrNextStepId = '2a&numRider=' + numNextRider;
						} else {
							if (total_conviction_numConvictions) {
								chrNextStepId = '2b';
							} else {
								if (total_claim_numClaims) {
									chrNextStepId = '2c';
								} else {
									chrNextStepId = '3';
								}
							}	
						}	
					}
					//step 2b
					if (chrFormId == 'step2b') {
						if (total_claim_numClaims) {
							chrNextStepId = '2c';
						} else {
							chrNextStepId = '3';
						}
					}
					//setp 2c
					if (chrFormId == 'step2c') {
						chrNextStepId = '3';
					}
					chrNextStepLocation = 'index.cfm?go=quote.step' + chrNextStepId;
					//window.location = 'index.cfm?go=quote.step' + chrNextStepId;
				} else {
					chrNextStepLocation = 'index.cfm?go=quote.step' + + ++numStepId;
					//window.location = 'index.cfm?go=quote.step' + ++numStepId;
				}
				//step 4 has been removed so step 3 goes straight to step 5
				if (numStepId == '3' || numStepId == '4') {
					chrNextStepLocation = $('#secureDomain').text() + '/index.cfm?go=quote.step5';	
				}
				
				if (numStepId == '7' && $('input[name=addon_legal]:checked').val() == 'N' && $('#noLegalCoverWarningGiven').val() == 'N') {
					showModal("Legal Cover","Are you sure you don't want to add legal expenses cover to your policy for just \u00A326?<br><br><br><i>Please note this reminder only occurs once.</i>");
					 $('#noLegalCoverWarningGiven').val('Y');
					return false;
				}

				
				
			//	if (numStepId == '9' && $('#hidden_brokerBuyWarning').val() == 'N' ) {
			//		showModal("Buy Warning","By clicking \'Buy Now\' again, you are asking Ridersure to complete this policy and send your customers documents to you.  The premium will appear on your next statement<br><br><br><i>Please note this reminder only occurs once.</i>");
			//		 $('#hidden_brokerBuyWarning').val('Y');
			//		return false;
			//	}
				
				//window.location = chrNextStepLocation;
				showModalTimer();
				setTimeout (function() {updateFormSession(chrNextStepLocation)}, 1000 );
				
			// NEEDED FOR BIBIT!!
			if (numStepId == '10' ){
				chrNextStepLocation = 'index.cfm?go=quote.SPMregisterPaymentRequest';
	 		}
	 		
	 		// renewal proces
	 		if (numStepId == '21' ){
				chrNextStepLocation = 'index.cfm?go=quote.renewalStep22';
	 		}
				
			} else {
				//step did not validate
				return false;
			}
			
			
		});
	}
	
	// Step 7 Summary page add click function to "buy now" button
	
	if (numStepId == 7) {
		$('#buynowBtn').click(function(event) {
			event.preventDefault();
			if($('#proposer_terms1:checked').val() == "Y"){
				window.location = 'index.cfm?go=quote.step8';
			}
			else{
				showModal("Terms","Please confirm you have read our terms of business and assumptions");
				return false;
			}
		});
		
		$('#buynowBtn2').click(function(event) {
			event.preventDefault();
			
			if( $('#proposer_terms1').is(":visible") == false ){
   			   window.location = 'index.cfm?go=quote.step8';
			}
			else{
				if($('#proposer_terms1:checked').val() == "Y"){
					window.location = 'index.cfm?go=quote.step8';
				}
				else{
					showModal("Terms","Please confirm you have read our terms of business and assumptions");
					return false;
				}
			}
		});
	} 
	
	/*if (numStepId == 9){
		$('#nextBtn').click(function(event) {
			event.preventDefault();
			$('#nextBtn').hide();
		});
	}*/
	
});

function getStepId(chrFormId) {
 	return parseInt($.GetId(chrFormId));
}

function getNumRiders() {
 	return parseInt($('#hidden_policy_numRiders').val());
}

function getDriversNumConvictions(chrFormId) {
	if ($('#hidden_policy_numRiders').val() == 0) {
		return 0;
	} else {
		var numDriverConvictions = 0
		if (chrFormId == 'step2a') {
			//get number of convictions on the form
			var numRider = parseInt($('#numRider').val());
			var thisDriverConvictionsSel = '#' + 'drivers_' + numRider + '_conviction_numConvictions';
			numDriverConvictions = numDriverConvictions + parseInt($(thisDriverConvictionsSel).val());
		}
		//hidden values
		$('[id*=hidden_drivers]').filter('[id*=numConvictions]').each(function() {
			numDriverConvictions = numDriverConvictions + parseInt($(this).val());
		});
		return numDriverConvictions;
	}
}		

function getDriversNumClaims(chrFormId) {
	if ($('#hidden_policy_numRiders').val() == 0) {
		return 0;
	} else {
		var numDriverClaims = 0
		if (chrFormId == 'step2a') {
			//get number of claims on the form
			var numRider = parseInt($('#numRider').val());
			var thisDriverClaimsSel = '#' + 'drivers_' + numRider + '_claim_numClaims';
			numDriverClaims = numDriverClaims + parseInt($(thisDriverClaimsSel).val());
		}
		//hidden values
		$('[id*=hidden_drivers]').filter('[id*=numClaims]').each(function() {
			numDriverClaims = numDriverClaims + parseInt($(this).val());
		});
		return numDriverClaims;
	}
}

function getProposerNumConvictions(chrFormId) {
	if (chrFormId == 'step2') {
		return parseInt($('#proposer_conviction_numConvictions').val());
	} else {
		return parseInt($('#hidden_proposer_conviction_numConvictions').val());
	}
}

function getProposerNumClaims(chrFormId) {
	if (chrFormId == 'step2') {
		return parseInt($('#proposer_claim_numClaims').val());
	} else {
		return parseInt($('#hidden_proposer_claim_numClaims').val());
	}
}

function getProposerAddressPostCode(chrFormId) {
		return $('#hidden_proposer_address_postCode').val();
}

	
