
$(document).ready(function(){
	
	$("#mortgage_fund").select();
	$("#row0 td").addClass("selected_background");
						   
	if(window.location.hash === "#calculate") {
		$("#tabs").addClass("calculator_selected").removeClass("contact_us_selected");
		$("#calculator_container").addClass("display").removeClass("displayNone");
		$("#contact_container").removeClass("display").addClass("displayNone");
	}
	
	 if(window.location.hash === "#contactus") {
		$("#tabs").addClass("contact_us_selected").removeClass("calculator_selected");
		$("#contact_container").addClass("display").removeClass("displayNone");
		$("#calculator_container").removeClass("display").addClass("displayNone");
	}
		   
	/* 	__tabs
	-------------------------------------------*/
	$("#calc").bind("click",function(){
		if(window.location.hash !== "#calculate") {
			//$("#calculator_container").show("slide", {direction: 'left', duration: 1000, easing: "easeOutExpo"});
			$("#calculator_container").show();
			//$("#contact_container").hide("slide", {direction: 'right', duration: 800, easing: "easeOutExpo"});
			$("#contact_container").hide();
			$("#tabs").addClass("calculator_selected").removeClass("contact_us_selected");
		}
	});
	
	$("#cont, #booklet").bind("click",function(){
		if(window.location.hash !== "#contactus") {
			/*$("#calculator_container").hide("slide", {direction: 'left', duration: 1000, easing: "easeOutExpo"});
			$("#contact_container").show("slide", {direction: 'right', duration: 800, easing: "easeOutExpo"});*/
			$("#calculator_container").hide();
			$("#contact_container").show();
			$("#tabs").addClass("contact_us_selected").removeClass("calculator_selected");
		}
	});
	
	
	/* 	__accordion
	-------------------------------------------*/
	
	$("#calculator").accordion({
							   
							   
		autoHeight:false,
		header:'h3',
		active:0,
		navigation:true,
		change: function(event, ui) {
			$("#row0 td, #row1 td, #row2 td").removeClass("selected_background");
			$("h3").each(function(i) {
				if($(this).attr("tabindex") == 0) {
					$("#row" + i + " td").addClass("selected_background");
				}
			});
		}

		
	});
	
	
	/* 	__functions
	-------------------------------------------*/
	
	
	/* 	__addCommas
		adds commas to the number	*/
	function addCommas(nStr)
	{
		nStr += "";
		x = nStr.split(".");
		x1 = x[0];
		x2 = x.length > 1 ? "." + x[1] : "";
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, "$1" + "," + "$2");
		}
		return x1 + x2;
	}

	
	
	/*	__validateNum
		validates the number string	*/
	function validateNum(strValue) {
		var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
		return objRegExp.test(strValue);
	}
	
	
	/*	__updateSession
		update the session values in the form all this is an ajax 
		call to the file updateSession.php	*/
	function updateSession(_form, _nextPanel, _total, _inputField) {
		
		var formData = _form.split(',');
		var formValues = "";
		
		$(formData).each(function(i) {
			formValues += formData[i] + "=" + $("#" + formData[i]).val() + "&";					  
		});
		
		
		$.ajax({
			type: "POST",
			url: "updateSession.php?" + formValues + "total=" + _total,
			success: function() {
				
				//	change according to next panel if set
				if(_nextPanel != "") {
					$("#calculator").accordion( "activate" , _nextPanel );
				}
				
				$("#" + _inputField).select();
				
			},
			error: function(objAJAXRequest) {
							
							
					//alert('error');
					
			}
		});
		
		
	}
	
	/* 	__updateTotal
		update the overall total with the sessions 	
		all this is an ajax call to the file updateSession.php	*/
	function updateTotal(_section_total, _as, _total_location) {
		
			$.ajax({
			type: "POST",
			url: "updateSession.php?section_total=" + _section_total + "&as=" + _as + "&total_location=" + _total_location,
			success: function() {
				
				//alert(_section_total);
				
			},
			error: function(objAJAXRequest) {
							
							
					//alert('error');
					
			}
		});
	}
	
	
	/* 	__changeTotal
		change the total on the ui 	*/
	function changeTotal(_type) {
		
		//	we want only certain sections updated at times
		
		//	update amount_needed total only if both are not 0
		if(_type == "amount_needed") {
			
			if($("#current_debt_total_number").val() != 0 && $("#long-term_needs_total_number").val() != 0) {
				
				//	get amount needed from values on the form
				//	#long-term_needs_total_number
				//	#current_debt_total_number
				//	then set to comma seperated values
				amount_needed = parseInt($("#long-term_needs_total_number").val()) + parseInt($("#current_debt_total_number").val());
				amount_needed_comma_seperated = addCommas(amount_needed);
				
				//	set to session
				updateTotal(Array($("#current_debt_total_number").val(), $("#long-term_needs_total_number").val()), 'add', "amount_needed_total");
																
				//	set values to html and hidden field
				$("#amount_needed_total").html("$" + amount_needed_comma_seperated);
				$("#amount_needed_total_number").val(amount_needed);
			}
		
		} 
		
		//	similar to conditional above that we only want to update if
		//	both values are above 0
		else if(_type == "additional_total") {
			
			if($("#assets_total_number").val() != 0 && $("#amount_needed_total_number").val() != 0) {
			
				//	gather values in final_total
				//	
				var final_total = parseInt($("#amount_needed_total_number").val()) - parseInt($("#assets_total_number").val());
				final_total_comma_seperated = addCommas(final_total);
				
				$("#additional_total").html("$" + final_total_comma_seperated);
				$("#additional_total_number").val(final_total);
				
				if(final_total < 0 || final_total == 0) {
					$("#no_insurance_message").animate({"opacity": "show"}, "slow");
					$("#additional_total").html("$0");
				} else {
					$("#no_insurance_message").animate({"opacity": "hide"}, "slow");
				}
				
				updateTotal(Array($("#amount_needed_total_number").val(), $("#assets_total_number").val()), 'subtract', "additional_total");
			
			}
		}
			
	}

	
	
	/* 	__calculator
	-------------------------------------------*/
	
	var current_total = 0;
	var long_term_total = 0;
	var child_education_total = 0;
	var assets_total = 0;
	var amount_needed = 0;
	var final_total_new = 0;
	var list_of_fields = "#mortgage_fund, #final_expenses, #other_debts, #income_continuation, #retirement, #investments, #savings, #current_insurance, #childrens_education, #college_amount";
	
	
	/*	select value of input on blur or focus*/
	$(list_of_fields).bind("focus" , function() {
		if($(this).val() === "") {
			$(this).val(0).select();
		} else {
			$(this).select();	
		}
	});	
	
	/*	dont allow other values but numbers	*/
	$(list_of_fields).bind("keyup" , function() {
		if(!validateNum($(this).val()) && $(this).val() != "") {
			$(this).val(0).select();
		}
	});
	
	/* 	remove nulls	 */
	$(list_of_fields).bind("blur" , function() {
		if($(this).val() == "") {
			$(this).val(0).select();
		}
	});
	
	/*	current debt calulation
		
		we want the total from
		
		#mortgage_fund
		#final_expenses
		#other_debts
		
		these are added together
		
	*/
	$("#current_debt_continue").bind("click keyup", function(ev) {
		
		if($("#mortgage_fund").val() === "" || $("#final_expenses").val() === "" || $("#other_debts").val() === "") {
			$("input").each(function(i) {
				if($(this).val() === "") {
					$(this).val(0).select();
					return false;
				}
			});	
			
			return false;
		} else {
													   
			// 	calculate current total
			current_total = parseInt($("#mortgage_fund").val()) + parseInt($("#final_expenses").val()) + parseInt($("#other_debts").val());
			current_total_comma_seperated = addCommas(current_total);
			
			// 	add to total value
			$("#current_debt_total").html("$" + current_total_comma_seperated);
			$("#current_debt_total_number").val(current_total);
			
			//	update session total
			updateSession("mortgage_fund,final_expenses,other_debts", 1, "current_debt_total", "income_continuation");
			
			// update both totals amount needed and additional total
			changeTotal("amount_needed");
			changeTotal("additional_total");
			
			return false;
		}

	});
	
	
	/*	long term plan calulation 
		
		we want the total from
		
		#income_continuation
		#retirement
		#college_total_number
		
		these are added together
		
	*/
	$("#long_term_continue").bind("click keyup", function() {
													   
		
		if($("#income_continuation").val() === "" || $("#retirement").val() === "" || $("#childrens_education").val() === "" || $("#college_amount").val() === "") {
			$("input").each(function(i) {
				if($(this).val() === "") {
					$(this).val(0).select();
					return false;
				}
			});	
			
			return false;
		} else {
		
			// calculate long term total
			//child_education_total = parseInt($("#childrens_education").val()) * parseInt($("#college_amount").val());
			long_term_total = parseInt($("#income_continuation").val()) + parseInt($("#retirement").val()) + parseInt($("#college_total_number").val());
			long_term_total_comma_seperated = addCommas(long_term_total);
			
			// add to total value
			$("#long-term_needs_total").html("$" + long_term_total_comma_seperated);
			$("#long-term_needs_total_number").val(long_term_total);
			
			//	update session total
			updateSession("income_continuation,retirement,childrens_education,college_amount,college_total_number", 2, "long-term_needs_total", "investments");
			
			// update both totals amount needed and additional total
			changeTotal("amount_needed");
			changeTotal("additional_total");
			
			return false;
		}

	});
	
	
	/*	children"s education amount needed calulation
		
		we want the total from
		
		#childrens_education
		#college_amount
		
		these are added together
		
	*/
	$("#childrens_education, #college_amount").bind("blur keyup", function() {
														  
		if($(this).val() != "" && validateNum($(this).val())) {
			child_education_total = parseInt($("#childrens_education").val()) * parseInt($("#college_amount").val());
			child_education_total_comma_seperated = addCommas(child_education_total);
			
			if(child_education_total != 0 && $("#childrens_education").val() !== "" && $("#college_amount").val() !== "") {
				// add to total value
				$("#college_total").html("$" + child_education_total_comma_seperated);
				$("#college_total_number").val(child_education_total);
			} else {
				$("#college_total").html("$0");
				$("#college_total_number").val(0);
			}
		}
		
	});	
	
	
	/*	current assets amount calulation
		
		we want the total from
		
		#investments
		#savings
		#current_insurance
		
		these are added together
		
	*/
	$("#assets_continue").bind("click keyup", function() {
													   
		if($("#investments").val() === "" || $("#savings").val() === "" || $("#current_insurance").val() === "") {
			$("input").each(function(i) {
				if($(this).val() === "") {
					$(this).val(0).select();
					return false;
				}
			});	
			
			return false;
		} else {
													   
		
			// calculate asset total
			assets_total = parseInt($("#investments").val()) + parseInt($("#savings").val()) + parseInt($("#current_insurance").val());
			assets_total_comma_seperated = addCommas(assets_total);
			
			// add to total value
			$("#assets_total").html("$" + assets_total_comma_seperated);
			$("#assets_total_number").val(assets_total);
			
			//	update session total
			updateSession("investments,savings,current_insurance", '' , 'assets_total');
			
			//	update overall total
			changeTotal("additional_total");
			
			//	switch calculator to contact us
			$("#calculator_container").hide("slide", {direction: 'left', duration: 1000, easing: "easeOutExpo"});
			$("#contact_container").show("slide", {direction: 'right', duration: 800, easing: "easeOutExpo"});
			$("#tabs").addClass("contact_us_selected").removeClass("calculator_selected");
			window.location.hash = "#contactus";
			
			return false;
		}

	});
	
	
	/*	additional amount needed calulation
		
		we want the total from
		
		@assets_total
		@amount_needed
		
		these these two sections are subtracted to
		get the overall amount needed
		
		if there is a negative or 0 value then we will show $0
		and the message "Your life insurace needs have been met at this time."
		
	*/
	
		
	/*$("#assets_continue").bind("click", function() {
				
				// 	update the overall total
				changeTotal("additional_total");
			
				
		});*/

		$('h3#current_header a.question_mark').qtip({
			content: 'This could include paying off or down your mortgage so your spouse will have fewer living expenses, uncovered medical costs, estate settlement fees. Think about an amount set aside to other debts such as auto loans or credit cards.',
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
				target: 'bottomLeft',
				tooltip: 'topRight'
				}
			},
			style: { 
			  name: 'cream' // Inherit from preset style
		   	}

		});
		
		$('h3#long_term_header a.question_mark').qtip({
			content: 'Life insurance proceeds can be used for long-term needs such as income continuation so that your family can maintain thier current lifestyle. It can also be used for future plans such as assisting with retirement, and thinking ahead to meet your children\'s needs for college.',
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
				target: 'bottomLeft',
				tooltip: 'topRight'
				}
			},
			style: { 
			  name: 'cream' // Inherit from preset style
		   	}

		});
		
		$('h3#assets_header a.question_mark').qtip({
			content: 'Indicate you current savings and investments, as well as life insurance you already have (through your employer or purchased on your own.) Investments include stocks, bonds, 401(k) proceeds, annuities, etc.',
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
				target: 'bottomLeft',
				tooltip: 'topRight'
				}
			},
			style: { 
			  name: 'cream' // Inherit from preset style
		   	}

		});
	

});