hot_water = true //To stop it running the minimum_saving() in lookup_town.js
$(function(){

	//Hide the initial hidden bits
	$('#rspv_step_2, #rspv_step_3, #location_tr, .price_per_kwh_tr, #no_package_selected').hide()
	
	//Do the postcode lookup
	$('#home_postcode').keyup(function(){
		postcode_c()
	})
	
	//validate step 1 and enter step 2
	$('#select_package').click(function(){
		if(validate_p("#rspv_step_1")){
			
			system_size = $('#system').val()
			
			//do the ajax call
			$("#packages").load("/ajax/get_business_packages/"+system_size+"_kw", function(){
				$('#rspv_step_1').hide()
				$('#rspv_step_2').show()
				$('#system_size_header').text(system_size)
			})
			
			$('#calculator_progress_spv_business').addClass("step_2")
			$('#calculator_progress_spv_business.step_1').removeClass("step_1")
			
			
			
			//get the terms
			$(".terms").html("")
			//Make the call to AJAX
			$(".terms").load("/calculators/get_terms/business/"+system_size+"_kw")
			
		}
		
		return false;
	})
	
	//Go back to step 1
	$("#calculate_basic_savings").click(function(){
		$('#rspv_step_1').show()
		$('#rspv_step_2').hide()
		
		$('#calculator_progress_spv_business').addClass("step_1")
		$('#calculator_progress_spv_business.step_2').removeClass("step_2")
		
		return false;
	})
	
	
	//validate step 2 and enter step 3
	$("#btn_see_payback_period").click(function(){
		//Validate this step
		if($("input[name=package]:checked").val()){
			$('#no_package_selected').hide()			
			system_price = parseInt($("input[name=package]:checked").val())
			$("#grand_total").val(system_price);
			
			//Show the next screen and hide this
			$('#rspv_step_2').hide()
			$('#rspv_step_3').show()
			
			//Load the graph
			form_content = $('#rspv_form').serialize()
			
			$.ajax({
				type: "POST",
			  	url: '/calculators/business_graph',
				data: form_content,
			  success: function(data) {
			    $('#rspv_step_3 #graph').html(data);
			    
			    $('#calculator_progress_spv_business').addClass("step_3")
			    $('#calculator_progress_spv_business.step_2').removeClass("step_2")
			    
			  }
			});
			
		}
		else{
			$('#no_package_selected').show()
		}
		
		return false;
		
	});
	
	//Go back to step 2
	$("#select_package_b").click(function(){
		$('#rspv_step_3').hide()
		$('#rspv_step_2').show()
		
		$('#calculator_progress_spv_business').addClass("step_2")
		$('#calculator_progress_spv_business.step_3').removeClass("step_3")
		
		return false;
		
	});
	
	
	//Init
	postcode_c()
	
})

//Postcode check
function postcode_c(){
	if($("#home_postcode").val().length == 4){
		postcode = $("#home_postcode").val()
		find_town(postcode)
			// Returns:
				// kwh_per_day
				// town
				// selected_state
		get_state_info(selected_state)
			// Returns:
				// gross_nett
				// e_price
				// tariff
				// fit_rate
				// max_system_size
		//Wait for a second for the ajax to do it's stuff
		setTimeout(function(){
			if($("#per_per_kwh").val() != ""){
				$('#e_price').val($("#per_per_kwh").val())
			}
			else{
				$('#e_price').val(e_price)
			}
			$('#e_price').val(e_price)
			$("#gross_nett").val(gross_nett);
			$("#kwh_per_day").val(kwh_per_day);
			$("#tariff").val(tariff);
			$("#fit_rate").val(tariff);
			$("#max_system_size").val(max_system_size);
			$("#state").val(selected_state);
			
			if(gross_nett == "Nett"){
				$('.price_per_kwh_tr').show()
				$("#per_per_kwh").addClass("required")
			}
			else{
				$("#per_per_kwh").val("");
				$('.price_per_kwh_tr').hide()
				$("#per_per_kwh").removeClass("required")
			}
			
		}, 2500)
	}
}

function validate_p(ele){
	
	$(ele + " .required").each(function(i){
		
		if($(this).val().length == 0){
			$(this).addClass("error")
			$('#error_label_'+$(this).attr("id")).remove()
			$(this).parent().append('<label id="error_label_'+$(this).attr("id")+'"class="error">This field is required.</label>')
		}
		else{
			$(this).removeClass("error")
			$('#error_label_'+$(this).attr("id")).remove()
		}
		
		//Dropdown Fields
		$(this).change(function(){
			if($(this).val().length != 0){
				$(this).removeClass("error")
				$('#error_label_'+$(this).attr("id")).remove()
			}
			else{
				$(this).addClass("error")
				$('#error_label_'+$(this).attr("id")).remove()
				$(this).parent().append('<label id="error_label_'+$(this).attr("id")+'"class="error">This field is required.</label>')
			}
		})
		
		//Textboxes
		$(this).keyup(function(){
			if($(this).val().length != 0){
				$(this).removeClass("error")
				$('#error_label_'+$(this).attr("id")).remove()
			}
			else{
				$(this).addClass("error")
				$('#error_label_'+$(this).attr("id")).remove()
				$(this).parent().append('<label id="error_label_'+$(this).attr("id")+'"class="error">This field is required.</label>')
			}
		})
		
	})
	
	if($('.error').length){
		return false;
	}
	else{
		return true;
	}
	
}
