jQuery(document).ready(function() {


jQuery(".calculate").keydown(function(event) {
		// Allow only backspace and delete
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 190 || event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
			// let it happen, don't do anything
		}
		else {
			// Ensure that it is a number and stop the keypress
			if ((event.keyCode < 48 || event.keyCode > 57)&(event.keyCode < 96 || event.keyCode > 105) ) {
				event.preventDefault();
			}	
		}
	});
   
	jQuery(".calculate").bind('change blur keyup keydown',function(){	
		if(jQuery("#loanTerm").val() == "" || jQuery("#annualRate").val() == "" || jQuery("#loanAmount").val() == "") {
			jQuery("#monthlyPayment").val("0.00");
		}
		else {
			calculatePayment(this.form);	
		}
	});
	
	jQuery("#webform-component--of-breeds, #webform-component-slide, #webform-component-above-ground").hide();
	jQuery("#edit-submitted-dogs-1").change(function() {
		if (jQuery(this).is(":checked")) {
			jQuery("#webform-component--of-breeds").fadeIn();
		} else {
				jQuery("#webform-component--of-breeds").fadeOut();
		}
	});
	
	jQuery("#edit-submitted-swimming-1").change(function() {
		if (jQuery(this).is(":checked")) {
			jQuery("#webform-component-slide, #webform-component-above-ground").fadeIn();
		} else {
				jQuery("#webform-component-slide, #webform-component-above-ground").fadeOut();
		}
	});
	
	jQuery("#webform-component-miles-one-way, #webform-component-miles-one-way-2, #webform-component-miles-one-way-3, #webform-component-miles-one-way-4").hide();
	jQuery("#webform-component-do-you-commute").change(function() {
		var miles = "#webform-component-miles-one-way"
			if (jQuery("#webform-component-do-you-commute option[value='yes']").attr('selected')) {
			jQuery(miles).fadeIn();
		} else {
			jQuery(miles).fadeOut();
		}
	});
	
	jQuery("#webform-component-do-you-commute-2").change(function() {
		var miles_2 = "#webform-component-miles-one-way-2"
			if (jQuery("#webform-component-do-you-commute-2 option[value='yes']").attr('selected')) {
			jQuery(miles_2).fadeIn();
		} else {
			jQuery(miles_2).fadeOut();
		}
	});
	
	jQuery("#webform-component-do-you-commute-3").change(function() {
		var miles_3 = "#webform-component-miles-one-way-3"
			if (jQuery("#webform-component-do-you-commute-3 option[value='yes']").attr('selected')) {
			jQuery(miles_3).fadeIn();
		} else {
			jQuery(miles_3).fadeOut();
		}
	});
	
	jQuery("#webform-component-do-you-commute-4").change(function() {
		var miles_4 = "#webform-component-miles-one-way-4"
			if (jQuery("#webform-component-do-you-commute-4 option[value='yes']").attr('selected')) {
			jQuery(miles_4).fadeIn();
		} else {
			jQuery(miles_4).fadeOut();
		}
	});
	
	jQuery("#webform-component-garaging-zip, #webform-component-out-of-state").hide();
	jQuery("#webform-component-operated-moored").change(function() {
		var hidden = "#webform-component-garaging-zip, #webform-component-out-of-state"
			if (jQuery("#webform-component-operated-moored option[value='yes']").attr('selected')) {
			jQuery(hidden).fadeIn();
		} else {
			jQuery(hidden).fadeOut();
		}
	});
	
	jQuery("#webform-component-marine-survey").hide();
	jQuery("#webform-component-boat-years-old").change(function() {
		var hidden = "#webform-component-marine-survey"
			if (jQuery("#webform-component-boat-years-old option[value='yes']").attr('selected')) {
			jQuery(hidden).fadeIn();
		} else {
			jQuery(hidden).fadeOut();
		}
	});
	
	
	//this is the add driver portion of the auto quote
	jQuery("#node-59 #webform-component-driver-2").nextUntil('#node-59 #webform-component-vehicle-1').andSelf().hide(); //hiding extra form components ofor Auto quote
	jQuery("#node-59 #webform-component-dl").after("<input type='button' class='add_driver' value='Add Another Driver'>");
	jQuery("#node-72 #webform-component-driver-2").nextUntil('#node-72 #webform-component-vehicle-1').andSelf().hide(); //hiding extra form components for RV quote
	jQuery("#node-72 #webform-component-dl").after("<input type='button' class='add_driver' value='Add Another Driver'>");
		var drvr_cnt = 0
		jQuery(".add_driver").live("click", function() { //live is used to buttons can be interacted with
		jQuery(this).next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn(); //fade in next 4 elements
		if(drvr_cnt < 2) { //if variable doesn't exceed 2 (meaning a maximum of 4 drivers)...
			jQuery(this).next().next().next().next().after("<input type='button' class='add_driver' value='Add Another Driver'>"); //add button after new elements
		drvr_cnt++ //add one to drvr_cnt when one of the add_driver button's recieve a click
		}
		jQuery(this).remove(); //remove old button
	})
	
	//add vehicle portion of auto quote
	
    jQuery("#node-59 #webform-component-vehicle-divider").nextUntil('#node-59 #webform-component-violations').andSelf().hide(); //hide all extra form components on auto
	jQuery("#node-59 #webform-component-lienholder").after("<input type='button' class='add_vehicle' value='Add Another Vehicle'>");
		var vhcl_cnt = 0
		jQuery("#node-59 .add_vehicle").live("click", function() { //live is used to buttons can be interacted with
		jQuery(this).next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn(); //fade in next several elements
		if(vhcl_cnt < 2) { //if variable doesn't exceed 2 (meaning a maximum of 4 drivers)...
			jQuery(this).next().next().next().next().next().next().next().next().next().next().next().next().next().next().next().after("<input type='button' class='add_vehicle' value='Add Another Vehicle'>"); //add button after new elements
		vhcl_cnt++ //add one to vhcl_cnt when one of the add_vehicle button's recieve a click
		}
		jQuery(this).remove(); //remove old button
	});
	
	jQuery("#node-72 #webform-component-vehicle-divider").nextUntil('#node-72 #webform-component-years-of-rv-experience').andSelf().hide(); //hide all extra form components on rv
	jQuery("#node-72 #webform-component-vehicle-use").after("<input type='button' class='add_vehicle' value='Add Another Vehicle'>");
		var vhcl_cnt = 0
		jQuery("#node-72 .add_vehicle").live("click", function() { //live is used to buttons can be interacted with
		jQuery(this).next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn().next().fadeIn(); //fade in next several elements
		if(vhcl_cnt < 2) { //if variable doesn't exceed 2 (meaning a maximum of 4 drivers)...
			jQuery(this).next().next().next().next().next().next().next().next().next().next().next().after("<input type='button' class='add_vehicle' value='Add Another Vehicle'>"); //add button after new elements
		vhcl_cnt++ //add one to vhcl_cnt when one of the add_vehicle button's recieve a click
		}
		jQuery(this).remove(); //remove old button
	});
	
	
});

	//calculator
	function calculatePayment(form) {
    	princ = form.loanAmount.value;
    	intRate = (form.annualRate.value/100) / 12;
    	months = form.loanTerm.value * 12;
    	form.monthlyPayment.value = (Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100).toFixed(2);
		
    };

