// JavaScript Document
(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.1') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // Same thing but for IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
	helloCalc();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
	helloCalc();
}

/******** Print Our Calc ********/
function helloCalc() { 
    jQuery(document).ready(function($) { 
	var loading_image = "<img style=\"margin-left:40%; margin-top: 20px; margin-bottom: 20px; \" src=\"http://mlsreversemortgage.com/widget/loading.gif\" />"; 
	$('#results').html(loading_image); 
	var creditlink = "<a href=\"http://mlsreversemortgage.com\">Calculation provided by MLS Reverse Mortgage</a>";
	$('#credit').html(creditlink); 
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "http://mlsreversemortgage.com/widget/calcStyle.css" 
        });
		css_link.appendTo('head'); 
		freshenUp();
		function freshenUp() { 
		var	final_quote_url = quote_url;
		$('#results').html(loading_image);
		if($("#credit").length == 0) { 
			$('#results').html("The credit link for this calculator is missing and will not work until it is replaced.");
		}
		else
		{		
			var jsonp_url = "http://mlsreversemortgage.com/widget/widget_initial.php?callback=?";
        $.getJSON(jsonp_url, function(data) {
          $('#results').html(data.html); 
			$("#refinance").click(function() {
       		if($(this).is(":checked")) { 
			$('#results').html(loading_image); 
			var theirurl = document.location.href;
			var theirurl = encodeURIComponent(theirurl);
			var jsonp_url = "http://mlsreversemortgage.com/widget/widget_refi_initial.php?callback=?&theirurl="+theirurl;
        	$.getJSON(jsonp_url, function(data) { 
          		$('#results').html(data.html);    
		  			jQuery("#calcbtn").click(function() {   
					var age = $("#age").val();
					var homevalue = $("#homevalue").val();
					var mortgage = $("#mortgage").val();
					var zip = $("#zip").val();
					final_quote_url = encodeURIComponent(final_quote_url);
        			var jsonp_url = "http://mlsreversemortgage.com/widget/widget_refi_results.php?callback=?&homevalue="+homevalue+"&age="+age+"&mortgage="+mortgage+"&zip="+zip+"&quote_url="+final_quote_url;
        			$('#results').html(loading_image);
					$.getJSON(jsonp_url, function(data) {
					var str = "<div class=\"quote_url\" ><a href=\""+quote_url+"\">Get A Complete Quote</a></div><div class=\"discl\" >Estimates are net after insurance and financed costs</div><div class=\"mls_credit\" ><a href=\"http://mlsreversemortgage.com\">Reverse Mortgage Calculator</a> by MLS Mortgage</div><br  />";
          			$('#results').html(data.html+str);
					var blank = "";
					$('#credit').html(blank); 
						jQuery("#resetbtn").click(function() {
						freshenUp(); 
						});
        			});
					});		  
			});
			}
			});
			$("#purchase").click(function() {
       		if($(this).is(":checked")) {
			$('#results').html(loading_image);
			var theirurl = document.location.href;
			var theirurl = encodeURIComponent(theirurl);
			var jsonp_url = "http://mlsreversemortgage.com/widget/widget_pur_initial1.php?callback=?&theirurl="+theirurl;
        	$.getJSON(jsonp_url, function(data) {
          		$('#results').html(data.html); 
				$("#downpayment").click(function() {
       				if($(this).is(":checked")) {
					$('#results').html(loading_image); 
					var jsonp_url = "http://mlsreversemortgage.com/widget/widget_pur_initial2.php?callback=?&type=downpayment";
        			$.getJSON(jsonp_url, function(data) {
          				$('#results').html(data.html);    
		  					jQuery("#calcbtn").click(function() {  
							var age = $("#age").val();
							var downpayment = $("#downpayment").val();
							var zip = $("#zip").val(); 
							final_quote_url = encodeURIComponent(final_quote_url);
        					var jsonp_url = "http://mlsreversemortgage.com/widget/widget_pur_results.php?callback=?&type=downpayment&downpayment="+downpayment+"&age="+age+"&zip="+zip+"&quote_url="+final_quote_url;
							$('#results').html(loading_image);
        					$.getJSON(jsonp_url, function(data) {
							var str = "<div class=\"quote_url\" ><a href=\""+quote_url+"\">Get A Complete Quote</a></div>"; 
          					$('#results').html(data.html1+str+data.html2); 
							var blank = "";
							$('#credit').html(blank); 
								jQuery("#resetbtn").click(function() {
								freshenUp(); 
								});
        					});
							});	
					});
					}
					})
				$("#price").click(function() {
       				if($(this).is(":checked")) {
					$('#results').html(loading_image);
					var jsonp_url = "http://mlsreversemortgage.com/widget/widget_pur_initial2.php?callback=?&type=price";
        			$.getJSON(jsonp_url, function(data) {
          				$('#results').html(data.html);    
		  					jQuery("#calcbtn").click(function() {  
							var age = $("#age").val();
							var price = $("#price").val();
							var zip = $("#zip").val(); 
							final_quote_url = encodeURIComponent(final_quote_url);        					
							var jsonp_url = "http://mlsreversemortgage.com/widget/widget_pur_results.php?callback=?&type=price&price="+price+"&age="+age+"&zip="+zip+"&quote_url="+final_quote_url;
							$('#results').html(loading_image);
        					$.getJSON(jsonp_url, function(data) { 
							var str = "<div class=\"quote_url\" ><a href=\""+quote_url+"\">Get A Complete Quote</a></div>"; 
          					$('#results').html(data.html1+str+data.html2); 
							var blank = "";
							$('#credit').html(blank); 
								jQuery("#resetbtn").click(function() {
								freshenUp(); 
								});
        					});
							});	
					});
					}
					})
			});
			}
			});
        });
		
		
		
		
		
		
		}
	}
    });
}
})(); 
