
jQuery(document).ready(function($){

	function CheckForInsufficientData() {

		if ($('input[@name="property_type"]:checked').val() == "sfr") {
			$('.fields_sfr').show();
			$('.fields_condo').hide();
		} else {
			$('.fields_sfr').hide();
			$('.fields_condo').show();
		}
	
		if ($('input[@name="property_type"]:checked').val() == "condo" && ($('#city-id').val() == "belvedere" || $('#city-id').val() == "ross") ) {
			if ($('#calc_sections').css('display') != 'none')
				$('#calc_sections').fadeOut(function(){
					alert("Sorry, there isn't enough data to calculate these values.");
				});
			else
				alert("Sorry, there isn't enough market data to calculate these values.");
			return true;
		} else
			return false;
	}

	function UpdateCityTexts() {
		var city = $('#city-id').val();
		$('#explain1').html( $('#'+city+' .city_content1').html() );
		$('#explain2').html( $('#'+city+' .city_content2').html() );
		$('#explain3').html( $('#'+city+' .city_content3').html() );
	}
	
	// upper inputs
	$('#city-id').change(function(){
		if (CheckForInsufficientData())
			return false;
		if ($('#city-id').val()!='') {
			if ($('#calc_sections').css('display') != 'none') {
				$('#calc_sections').fadeOut(function(){
					UpdateCityTexts();
					$('#calc_sections').fadeIn();
				});
			} else
				UpdateCityTexts();
		} else if ($('#calc_sections').css('display') != 'none')
			$('#calc_sections').fadeOut();
	});
	$('input[@name="property_type"]').change(function(){
		if (CheckForInsufficientData())
			return false;
		if ($('#calc_sections').css('display') != 'none') {
			$('#calc_sections').fadeOut(function(){
				$('#calc_sections').fadeIn();
			});
		}
	});
	$('#continue').click(function(){
		if (CheckForInsufficientData())
			return false;
		if ($('#city-id').val()!='')
			$('#calc_sections').fadeIn();
		else
			alert("Please select a city before continuing.");
	});
	
	// lower calculations
	$('#calc1').click(function(event){
		if ( $('#sqft-id').val() == ""
			 || ( $('#characteristic-id').val() == "" && $('input[@name="property_type"]:checked').val() == "sfr" )
			 || ( $('#floorplan-id').val() == "" && $('input[@name="property_type"]:checked').val() == "sfr" )
			 || ( $('#condition-id').val() == "" && $('input[@name="property_type"]:checked').val() == "sfr" )
			 || ( $('#condition_condo-id').val() == "" && $('input[@name="property_type"]:checked').val() == "condo" )
			 || ( $('#layout-id').val() == "" && $('input[@name="property_type"]:checked').val() == "condo" ) )
			return alert('Please enter all fields for the Land / Structure Valuation section.');
		$.ajax({
			type: "POST",
			url: def_path+"old-estimator/calculate",
			data: "section=1&"+$('#estimator').serialize(),
			success: function(msg){
				var resp = msg.split('|');
				$('#calc1_result1').html(resp[0]);
				$('#calc1_result2').html(resp[1]);
				$('#calc1_result3').html(resp[2]);
				$('#calc1_result4').html(resp[3]);
			}
		});
	});
	$('#calc2').click(function(event){
		if ( $('#sqft-id').val() == ""
				|| ( $('#floorplan-id').val() == "" && $('input[@name="property_type"]:checked').val() == "sfr" )
				|| ( $('#layout-id').val() == "" && $('input[@name="property_type"]:checked').val() == "condo" ) )
			return alert('Please fill out fields from Step 1.');
		else if ( ($('#characteristic_sqft-id').val() == "" || $('#condition_sqft-id').val() == "") && $('input[@name="property_type"]:checked').val() == "sfr" )
			return alert('Please enter Lot Characteristic and Structure Condition.');
		$.ajax({
			type: "POST",
			url: def_path+"old-estimator/calculate",
			data: "section=2&"+$('#estimator').serialize(),
			success: function(msg){
				var resp = msg.split('|');
				if ($('input[@name="property_type"]:checked').val() == "sfr")
					$('#calc2_result1').html( $('#floorplan-id option:selected').text() );
				else
					$('#calc2_result1').html( $('#layout-id option:selected').text() );
				$('#calc2_result2').html( $('#sqft-id').val() );
				$('#calc2_result3').html(resp[0]);
				$('#calc2_result4').html(resp[1]);
			}
		});
	});
	$('#calc3').click(function(event){
		if ($('#sqft-id').val() == "" || $('#year-id').val() == "" || $('#price-id').val() == "")
			return alert('Please enter Square Feet, Year Purchased and Past Purchase Price.');
		$.ajax({
			type: "POST",
			url: def_path+"old-estimator/calculate",
			data: "section=3&"+$('#estimator').serialize(),
			success: function(msg){
				var resp = msg.split('|');
				$('#calc3_result1').html(resp[0]);
				$('#calc3_result2').html(resp[1]);
			}
		});
	});
	$('#reset3').click(function(event){
		$.ajax({
			type: "POST",
			url: def_path+"old-estimator/reset",
			data: "section=3",
			success: function(msg){
				$('#year-id').val('');
				$('#price-id').val('');
				$('#workneeded-id').val('');
				$('#calc3_result').html('');
			}
		});
	});
	$('#calc4').click(function(event){
		$.ajax({
			type: "POST",
			url: def_path+"old-estimator/calculate",
			data: "section=4&"+$('#estimator').serialize(),
			success: function(msg){
				var resp = msg.split('|');
				$('#calc4_result1').html(resp[0]);
				$('#calc4_result2').html(resp[1]);
				$('#calc4_result3').html(resp[2]);
				$('#calc4_result4').html(resp[3]);
			}
		});
	});
	$('#print').click(function(){
		var header = prompt("Please enter a title for this printout.");
		if (header)
			$('#printHeader').html(header);
		window.print();
	});

});
