var si = false
hot_water = false;

$(function(){
	
	//State information
	
	//Timeout length
	timeout = 2000
	state = ""
	gross_nett = ""
	
	
	system_size = 0
	extra_pitched_roof = 0
	extra_roof_material = 0
	extra_cathedral_ceiling = 0
	extra_number_of_stories = 0
	//extra_state = 0
	
	system_price = 0
	average_kwh = 0
	
	
	require_inspection = new Array()
	
	$('.calculate_figure').html("")
	
	
	//Hide the second and third stages
	$('#rspv_step_2, #rspv_step_3, #rspv_step_4, .disclaimer_tr, #no_package_selected, .state_based_charge, .state_based_charge .man_col div, #disclaimer, #show_hash').hide()
	
	$("#location_tr").hide()
	 
	
	
	$('#help_kwh').change(function(){
		$('#average_kwh').val($(this).val())
		average_kwh = $(this).val()
		minimum_savings()
	})
	$('#average_kwh').keyup(function(){
		average_kwh = $(this).val()
		minimum_savings()
	})
	
	
	$('#location').change(function(){		
		minimum_savings()
	})
	
	
	//Entering step 2
	$('#select_package').click(function(){
		if(!validate_p('#rspv_step_1')){
			return false;
		}
		else{
			$('#rspv_step_1').hide()
			$('#rspv_step_2').show()
			
			$('#calculator_progress_spv.step_1').removeClass("step_1")
			$('#calculator_progress_spv').addClass("step_2")
			
			get_system_size()
			$('#system_size_header').text(system_size)
			
			load_package()
			
			return false;
		}
	})
	
	
	//Re-entering step 1
	$('#calculate_basic_savings').click(function(){
	
		delete si;

	
		$('#calculator_progress_spv.step_2').removeClass("step_2")
		$('#calculator_progress_spv').addClass("step_1")
		$('#rspv_step_2').hide()
		$('#rspv_step_1').show()
		return false;
	})

	//Entering Step 3
	$('#refine_price').click(function(){
		valid = false
		
		//Find State
		state = find_state($('#home_postcode').val())
		
		if(state == "SA"){
			$("#sa_pitch_tr").show()		
		}
		else {
			$("#sa_pitch_tr").hide()		
		}
		
		//Show Meter Type if QLD
		if(state == "QLD"){
			$("#meter_type_tr").show()
			$('#meter_type_td').html("+ $150")
			extra_meter_type = 150
		}
		else {
			$("#meter_type_tr").hide()
			extra_meter_type = 0
		}
		
		//clear the state based charges
		$('.state_based_charge').remove()
		//extra_state = 0
		
		if($("input[name=package]:checked").val()){
			$('#no_package_selected').hide()
			
			system_price = parseInt($("input[name=package]:checked").val())
			
			valid = true
		}
		
		if(!valid){
			$('#no_package_selected').show()
			return false;
		}
		else{
			$('#rspv_step_2').hide()
			$('#rspv_step_3').show()
			
			$('#calculator_progress_spv.step_2').removeClass("step_2")
			$('#calculator_progress_spv').addClass("step_3")
			
			calculate_running_total()
			
			//Add any extra charges
			state = find_state($('#home_postcode').val())
			
			//****** EDITS ********
			add_state_based_charges()
			
			return false;
		}
	})


	
	
	
	$('#btn_enquire_now').click(function(){ 
		if(!validate_p('#rspv_step_3')){
			return false;
		}
	})
	
	$('#btn_payback_period').click(function(){
		/*if(!validate_p('#rspv_step_3')){
			
			return false;
		}*/
		$('#rspv_step_2').hide()
		$('#rspv_step_3').show()
		
		calculate_running_total()
		
		$('#indicates').show()
		
		
		return false;
	})
	

	$('#select_package_b').click(function(){
		$('#calculator_progress_spv.step_3').removeClass("step_3")
		$('#calculator_progress_spv').addClass("step_2")
		$('#rspv_step_3').hide()
		$('#rspv_step_2').show()
		return false;
	})
	
	
	
	
	
	/* SYSTEM SIZE
	***************************/
	$('#system').change(function(){
		get_system_size()
		minimum_savings()
	})
	
	/* Step 3
	***************************/
	$('#roof_direction').change(function(){
		c_roof_direction($(this))		
	})
	
	$('#pitched_roof').change(function(){
		c_pitched_roof($(this))	
		calculate_running_total()
	})
	
	$('#roof_material').change(function(){
		c_roof_material($(this))	
		calculate_running_total()
	})
		
	$('#accessible_roof').change(function(){
		c_accessible_roof($(this))
	})
	
	$('#cathedral_ceiling').change(function(){
		c_cathedral_ceiling($(this))
		calculate_running_total()
		
	})
	
	$('#number_of_stories').change(function(){
		c_number_of_stories($(this))
		calculate_running_total()
	})
	
	$('#accessible_roof_cavity').change(function(){
		c_accessible_roof_cavity($(this))
		calculate_running_total()
	})
	
	$("input[name='meter_type']").click(function(){
		meter_type_value = $("input[name='meter_type']:checked").val();

		if(meter_type_value == 1){
			$('#meter_type_td').html("+ $150")	
			extra_meter_type = 150
		}
		else if(meter_type_value == 2){
			$('#meter_type_td').html("+ $200")
			extra_meter_type = 200
		}
		else if(meter_type_value == 3){
			$('#meter_type_td').html("+ $250")
			extra_meter_type = 250
		}
		calculate_running_total()
	})
	
	/* Request a quote button */
	$("#btn_request_quote_l").click(function(){
	
		if(!validate_p('#rspv_step_3')){
			return false;
		}
		else{
			//The form is complete
			form_content = $('#rspv_form').serialize()
			
			$.ajax({
				type: "POST",
			  	url: '/calculators/insert_home_spv',
				data: form_content,
			  success: function(data) {
			    	window.location = "/quote"	    
			  }
			});
			
			return false;
		}
	
	});
	
	
	/* Validate Step 3
	***************************/
	$('#btn_see_payback_period').click(function(){
		if(!validate_p('#rspv_step_3')){
			return false;
		}
		else{
		
			$('#rspv_step_3').hide()
			$('#rspv_step_4').show()
			//The form is complete
			form_content = $('#rspv_form').serialize()
			
			$.ajax({
				type: "POST",
			  	url: '/calculators/insert_home_spv',
				data: form_content,
			  success: function(data) {
			    $('#rspv_step_4 #graph').html(data);
			    
			    $('#calculator_progress_spv.step_3').removeClass("step_3")
			    $('#calculator_progress_spv').addClass("step_4")
			    
			  }
			});
			
			$('#indicates').hide()
			
			return false;
		}
	})
	
	/* Return to Step 3
	***************************/
	$('#refine_price_b').click(function(){
		$('#rspv_step_4').hide()
		$('#rspv_step_3').show()
		
		$('#calculator_progress_spv.step_4').removeClass("step_4")
		$('#calculator_progress_spv').addClass("step_3")
		
		return false;
	})
	
	minimum_savings()
	
	
})


/* STATE BASED CHARGES REMOVED 20100921
******************************************
function add_state_based_charges(){

	get_state_info(state)


	//Delete the state rows if there
 	extra_state = 0
	html = ""
	$('.state_based_charge').remove()
	

	

	setTimeout(function(){
		//Adds a second of delay
		
		
		if(has_state_charges){
			l = state_charges.length
		}
		else{
			l = 0
		}
		
		if(l == 0){
			calculate_running_total()
			return false;
		}
		else{

			
			for(i=0; i<l; i++){
			
				html += '<tr class="state_based_charge"><th class="bottom_border">'+state_charges[i]["description"]+'</th><td class="bottom_border">&nbsp;</td>'
				
				if(system_size == "1.5"){
					ss = "1_5"
					extra_state += state_charges[i]["1_5"]
				}
				else if(system_size == "2"){
					ss = "2"
					extra_state += state_charges[i]["2"]
				}
				else if(system_size == "3"){
					ss = "3"
					extra_state += state_charges[i]["3"]
				}
				html +='<td class="man_col bottom_border">+ $'+state_charges[i][ss]+'</td></tr>'	
		
			}
		}
	
	
		
		$('#system_price_tr').before(html)
		
		calculate_running_total()
	
	}, 2000)
	
	
}
**************************************************************/

function load_package(){
	//Get the system size
	system_size = $('#system').val()
	
	if(system_size == "1.5"){
		ss = "1_5"
	}
	else if(system_size == "2"){
		ss = "2"
	}
	else if(system_size == "3"){
		ss = "3"
	}
	
	//Look up the postcode and return the zone to a "zone" variable
	zone = postcode_zone($('#home_postcode').val());
	// Look up the state
	s = find_state($('#home_postcode').val())
	
	$("#packages").html("")
	//Make the call to AJAX
	$("#packages").load("/ajax/package/"+ss+"_kw/zone_"+zone+"/"+s)
	
	
	//get the terms
	$(".terms").html("")
	//Make the call to AJAX
	$(".terms").load("/calculators/get_terms/home/"+ss+"_kw")
	
}


function calculate_running_total(){
	
	get_system_size()
	
	s = find_state($('#home_postcode').val())
	
	
	$('.state_based_charge .man_col div').hide()
	$('.selected_value').removeClass("selected_value")
	
	
	//Show the value based on the system size
	if(system_size == "1.5"){
		$('.ss_1_5').show().addClass("selected_value")
	}
	else if(system_size == "2"){
		$('.ss_2').show().addClass("selected_value")
	}
	else if(system_size == "3"){
		$('.ss_3').show().addClass("selected_value")
	}
	


	
	c_roof_direction($('#roof_direction'))
	c_pitched_roof($('#pitched_roof'))
	c_roof_material($('#roof_material'))
	c_accessible_roof($('#accessible_roof'))
	c_cathedral_ceiling($('#cathedral_ceiling'))
	c_number_of_stories($('#number_of_stories'))
	c_accessible_roof_cavity($('#accessible_roof_cavity'))
    

	
	
	//Add the initial total
	$('.system_price_placeholder').text(system_price)
	/*
	$('#sub_total_tr .extras_placeholder').html(extra_pitched_roof + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type +extra_state)
	$('.total .placeholder').html(system_price + extra_pitched_roof + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type +extra_state)

	$('#extras_total').val(extra_pitched_roof + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type +extra_state)
	$('#grand_total').val(system_price + extra_pitched_roof + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type +extra_state)
	*/
	
	$('#sub_total_tr .extras_placeholder').html(extra_pitched_roof + extra_sa_pitched + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type)
	$('.total .placeholder').html(system_price + extra_pitched_roof + extra_sa_pitched + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type)

	$('#extras_total').val(extra_pitched_roof + extra_sa_pitched + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type)
	$('#grand_total').val(system_price + extra_pitched_roof + extra_sa_pitched + extra_roof_material + extra_cathedral_ceiling +extra_number_of_stories +extra_accessible_roof_cavity +extra_meter_type)
	
	
	$('#gross_nett').val(gross_nett)
	$('#e_price').val(e_price)
	
	find_town($('#home_postcode').val())
	
	$('#kwh_per_day').val(kwh_per_day)
	$('#state').val(state)
	
	//Find State based charges
	si = get_state_info(state)

	
	
	return false

}


function get_system_size(){
	system_size = $('#system').val()
	minimum_savings()
}

/* Calculate Minimum savings
***************************/
function minimum_savings(){
	
	//if($('#system').val() != 0 && $('#location').val() != "" && $('#average_kwh').val() != 0){
	if($('#system').val() != 0 && $('#location').val() != ""){
		
		if(state == ""){
			state = find_state($('#home_postcode').val())
		}
		
		var size = Object.size(si);
		if(size == 0){
			si = get_state_info(state)
			setTimeout ("calculate_minimum_savings()", timeout );
		}
		else{
			calculate_minimum_savings()
		}

	}
}

function calculate_minimum_savings(){

	
	AEP = ($('#location').val() * $('#system').val()) * 365
	
	if(gross_nett == "Nett"){
		// 0.6 = Electricity Tariff
		FIT_Income = AEP * tariff * (3/7)
		min_sav = parseInt(si['state_elecricity_price'] * AEP * (4/7)  + FIT_Income);
	}
	else{
		FIT_Income = AEP * tariff;
		min_sav = parseInt(FIT_Income);
	}
	
	$('.calculate_figure').html("$"+min_sav)
	$('#min_saving').val(min_sav)
}

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;
	}
	
}

function c_roof_direction(e){
	
	
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "no"){	
		
		require_inspection["roof_direction"] = true;
		site_inspection_display()
	}
	else{
		delete require_inspection["roof_direction"];
		site_inspection_display()
	}
}
function c_pitched_roof(e){
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "no"){
		
		if(system_size == 1.5){
			$(e).parent().next().html("+ $415")	
			extra_pitched_roof = 415
		}
		else if(system_size == 2){
			$(e).parent().next().html("+ $550")	
			extra_pitched_roof = 550
		}
		else if(system_size == 3){
			$(e).parent().next().html("+ $825")
			extra_pitched_roof = 825
		}
		
		if(state == "SA"){
			$('#sa_pitch_td').html("+ $240")
			extra_sa_pitched = 240		
		}
		else {
			$('#sa_pitch_td').html("&nbsp;")
			extra_sa_pitched = 0		
		}
	}
	else{
		$(e).parent().next().html('&nbsp;')
		$('#sa_pitch_td').html("&nbsp;")
		extra_pitched_roof = 0
		extra_sa_pitched = 0
	}
}
function c_roof_material(e){
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "tile"){
		
		delete require_inspection["roof_material"];
		site_inspection_display()
		
		
		if(system_size == 1.5){
			$(e).parent().next().html("+ $198")	
			extra_roof_material = 198
		}
		else if(system_size == 2){
			$(e).parent().next().html("+ $264")	
			extra_roof_material = 264
		}
		else if(system_size == 3){
			$(e).parent().next().html("+ $396")
			extra_roof_material = 396	
		}
		else{
			$(e).parent().next().html("&nbsp;")
			extra_roof_material = 0
		}
	
	}
	else{
		
		delete require_inspection["roof_material"];
		site_inspection_display()
	
		if($(e).val() == "other"){
			require_inspection["roof_material"] = true;
			site_inspection_display()
			
		}
		$(e).parent().next().html("&nbsp;")
		extra_roof_material = 0
	}
}
function c_accessible_roof(e){
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "no"){
		
		require_inspection["accessible_roof"] = true;
		site_inspection_display()
	}
	else{
		delete require_inspection["accessible_roof"];
		site_inspection_display()
	}
}
function c_cathedral_ceiling(e){
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "yes"){
		if(system_size == 1.5){
			$(e).parent().next().html("+ $300")	
			extra_cathedral_ceiling = 300
		}
		else if(system_size == 2){
			$(e).parent().next().html("+ $300")	
			extra_cathedral_ceiling = 300
		}
		else if(system_size == 3){
			$(e).parent().next().html("+ $300")	
			extra_cathedral_ceiling = 300
		}
	
	}
	else{
		$(e).parent().next().html("&nbsp;")
		extra_cathedral_ceiling = 0
	}
}
function c_number_of_stories(e){
	
	if(system_size == 0){
		get_system_size()			
	}
	
	if($(e).val() == "double"){
		if(system_size == 1.5){
			$(e).parent().next().html("+ $200")	
			extra_number_of_stories = 200
		}
		else if(system_size == 2){
			$(e).parent().next().html("+ $250")	
			extra_number_of_stories = 250
		}
		else if(system_size == 3){
			$(e).parent().next().html("+ $300")	
			extra_number_of_stories = 300
		}
		
		delete require_inspection["apartment_unit"];
		site_inspection_display()

	}
	else if($(e).val() == "apartment_unit"){

		require_inspection["apartment_unit"] = true;
		
		site_inspection_display()
	}
	else{
		extra_number_of_stories = 0

		$(e).parent().next().html("&nbsp;")
		delete require_inspection["apartment_unit"];
		site_inspection_display()
	}
}

/* 20100909 - Addition */
function c_accessible_roof_cavity(e){
	if($(e).val() == "no"){
		$(e).parent().next().html("+ $300")	
		extra_accessible_roof_cavity = 300
	}
	else{
		$(e).parent().next().html('&nbsp;')
		extra_accessible_roof_cavity = 0
	}
}


Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

function site_inspection_display(){
	var size = Object.size(require_inspection);
	
	if(size){
		$('#disclaimer, #show_hash').show()
	}
	else{
		$('#disclaimer, #show_hash').hide()
	}
}

