/**
 * FILENAME: switching.js
 * AUTHOR: Ashley Barrett (ashley.barret@rgroup.co.uk)
 * CREATED: 09/02/2010
 * COMMENTS: A javascript file to add functionality to the switching pages
 * */
$(function(){

	if(($("#gasTariff").length > 0)||($(".switching").length > 0)){

		/* Hide both forms by default and let the customer decide what they wanrt to compare. */
		$("#gas, #elec").hide();
		
		
		var showForm = true;
		var elecUsage = 0;
		var gasUsage = 0;
		var gasFirst = true;
		var elecFirst = true;
		var prepayOnlySave = false;
				
		$.fn.getTariffs = function(id,element,type){
		
			$(this).resetFuturePayment();
					
			$("#"+element).attr('disabled','true');
			$("#"+type+"Payment").attr("disabled","true");
			
			if($("#"+element+" option").length > 0){
				$("#"+element).children().remove();
			}
			
			if($("#"+type+"Payment option").length > 0){
				$("#"+type+"Payment").children().remove();
			}
			
			$("#"+element).append("<option value=\"-1\">-- Loading Options --</option>");
			$("#"+type+"Payment").append("<option value=\"-1\">-- Loading Options --</option>");
			
			$.ajax({
				url : "/ajax/switching/getTariffs.php",
				type : "post",
				data : type+"="+id,
				cache : false,
				dataType : "json",
				success : function(data){
					if(data != 0){
						
						if($("#"+element+" option").length > 0){
							$("#"+element).children().remove();
						}
						
						$.each(data,function(i,val){
							$("#"+element).append("<option value=\""+i+"\">"+val+"</option>");
						});
						
						if($("input[name=defaultElecTariff]").val() != 0 && elecFirst){
							$("#elecTariff").val($("input[name=defaultElecTariff]").val());
						}
						
						if($("input[name=defaultGasTariff]").val() != 0 && gasFirst){
							$("#gasTariff").val($("input[name=defaultGasTariff]").val());
						}
						
						
						$("#"+element).removeAttr("disabled");
						
						$(this).getPayments($("select[name="+type+"Supplier]").val(),$("select[name="+type+"Tariff]").val(),type+"Payment",type);
					}
				}
			});
			
			return false;
		};
		
		$.fn.resetFuturePayment = function(){
			
			$("#method").children().remove();
			$("#method").append("<option value=\"127\">All Payment Methods</option>");
			$("#method").append("<option value=\"1\">Monthly Direct Debit</option>");
			$("#method").append("<option value=\"2\">Pay On Receipt Of Bill</option>");
			$("#method").append("<option value=\"3\">Prepayment Meter</option>");
			$("#method").append("<option value=\"4\">Quarterly Direct Debit</option>");
			$("#method").append("<option value=\"7\">Weekly</option>");
			$("#method").append("<option value=\"10\">Annual Advanced DD</option>");
			$("#method").append("<option value=\"14\">Monthly in Advance DD</option>");

		}
		
		$.fn.isPrePayFuture = function(){
		
			var onlyPrepay = false;
			
			var gasChosen = $("#chooseGas:checked").val();
			var elecChosen = $("#chooseElec:checked").val();
			
			//both gas and elec
			//if (($("#chooseElec").is(":checked")) && ($("#chooseGas").is(":checked")))			
			if (  (gasChosen == 1) && (elecChosen == 1)  )
			{
				//both prepay
				if (($("#elecPayment").val() == "PM3") && ($("#gasPayment").val() == "PM3"))
				{
					//alert('1');
					onlyPrepay = true;
				}		
			}
			
			//just elec
			//if (($("#chooseElec").is(":checked")) && ($("#chooseGas").not(":checked")))
			
			if (  (elecChosen == 1) && (gasChosen != 1)  )
			
			{
				if ($("#elecPayment").val() == "PM3")
				{
					//alert('2');
					onlyPrepay = true;
				}
			}
			
			//just gas
			//if (($("#chooseElec").not(":checked")) && ($("#chooseGas").is(":checked")))
			if (  (elecChosen != 1) && (gasChosen == 1)  )
			{
				if ($("#gasPayment").val() == "PM3")
				{
					//alert('3');
					onlyPrepay = true;
				}
			}
			
			if (onlyPrepay == true)
			{
				$("#method").children().remove();
				$("#method").append("<option value=\"3\">Prepayment Meter</option>");
				prepayOnlySave = true;
				//alert('prepay only');
			}
			else
			{
				//only reset if it is different
				if (prepayOnlySave == true){
					$(this).resetFuturePayment();
				}
				prepayOnlySave = false;
			}
		
		}
		
		$("#elecPayment").change(function(){
			$(this).isPrePayFuture();
		});
		$("#gasPayment").change(function(){
			$(this).isPrePayFuture();
		});
		
		$.fn.getPayments = function(supplier,tariff,element,type){
			
			$("#"+element).attr('disabled','true');
			
			if($("#"+element+" option").length > 0){
				$("#"+element).children().remove();
			}
			
			$("#"+type+"Payment").append("<option value=\"-1\">-- Loading Options --</option>");
			
			$.ajax({
				url : "/ajax/switching/getPayments.php",
				type : "post",
				data : type+"Supplier="+supplier+"&"+type+"Tariff="+tariff,
				cache : false,
				dataType : "json",
				success : function(data){
					if(data != 0){
						if($("#"+element+" option").length > 0){
							$("#"+element).children().remove();
						}
						
						$.each(data,function(i,val){
							$("#"+element).append("<option value=\""+i+"\">"+val+"</option>");
						});
						
						if($("input[name=defaultElecPayment]").val() != 0 && elecFirst){
							//console.log($("input[name=defaultElecPayment]").val());
							$("#elecPayment").val($("input[name=defaultElecPayment]").val());
						}
						
						if($("input[name=defaultGasPayment]").val() != 0 && gasFirst){
							$("#gasPayment").val($("input[name=defaultGasPayment]").val());
						}
						
						
						gasFirst = false;
						elecFirst = false;
						
						$("#"+element).removeAttr("disabled");
						
						$(this).isPrePayFuture();
						
					}
				}
			});
			
			return false;
		}	
	//}
	
	$(".tariffnormal").colorbox({width:"35%", height:"35%", inline:true, href:"#tariffnormal"});
	$(".tariffcapped").colorbox({width:"35%", height:"35%", inline:true, href:"#tariffcapped"});
	
	$(".moreInfo").colorbox({width:"50%", height:"50%", iframe:true});
	
	$(this).getTariffs($("select[name=gasSupplier]").val(),"gasTariff","gas");

	$(this).getTariffs($("select[name=elecSupplier]").val(),"elecTariff","elec");
	
	$("select[name=gasSupplier]").change(function(){
		$(this).getTariffs($(this).val(),"gasTariff","gas");
	});
	
	$("select[name=elecSupplier]").change(function(){
		$(this).getTariffs($(this).val(),"elecTariff","elec");
	});
	
	$("select[name=gasTariff]").change(function(){
		$(this).getPayments($("select[name=gasSupplier]").val(), $(this).val(),"gasPayment","gas");
	});
	
	
	$("select[name=elecTariff]").change(function(){
		$(this).getPayments($("select[name=elecSupplier]").val(), $(this).val(),"elecPayment","elec");
	});
	
	if($("input[name=gasComp]").is(":checked")){
		$("#gas").slideDown();
	}
	
	if($("input[name=elecComp]").is(":checked")){
		$("#elec").slideDown();
	}
	
	$("#chooseGas").click(function(){
		if($(this).is(":checked")){
			$("#gas").slideDown();
		}else{
			$("#gas").slideUp();
		}
		$(this).isPrePayFuture();
	});
	
	
	$(".showQuestion").click(function(){
		$("#" + $(this).attr("name")).toggle('slide');
		return false;
	});
	
	$("#chooseElec").click(function(){
	
		if($(this).is(":checked")){
			$("#elec").slideDown();
		}else{
			$("#elec").slideUp();
		}
		$(this).isPrePayFuture();
	});
	
	$("input[name=elecUsage], input[name=gasUsage]").click(function(){
		/* Check to see if we need to display modal estimator popup */	
		if($(this).is(":checked") && showForm){	
			
			$(this).colorbox({width:"50%", height:"50%", inline:true, href:"#estimate"});
			
		}else if($(this).is(":checked")){
			if($(this).attr("name") == "elecUsage"){
				$("input[name=elecSpend]").val("");
				$("input[name=eleckWh]").val(elecUsage);
			}else{
				$("input[name=gasSpend]").val("");
				$("input[name=gaskWh]").val(gasUsage);
			}
		}
	});
	
	$("#estimatorForm").submit(function(){
	
		var data = "";
		var error_msg = "";
		
		data += "&BuildingType="+$("select[name=BuildingType]").val();
		data += "&Bedrooms="+$("select[name=Bedrooms]").val();
		data += "&CookingSource="+$("select[name=CookingSource]").val();
		data += "&CookingFrequency="+$("select[name=CookingFrequency]").val();
		data += "&CentralHeating="+$("select[name=CentralHeating]").val();
		data += "&Occupants="+$("select[name=Occupants]").val();
		data += "&Insulation="+$("select[name=Insulation]").val();
		data += "&UsageLevel="+$("select[name=UsageLevel]").val();
				
		if ($("#chooseGas:checked").val() == 1)
		{
			//are there any gas options chosen
			
			if (
			($("select[name=CookingSource]").val() != "Gas") &&
			($("select[name=CentralHeating]").val() != "Gas")
			)
			{
				error_msg = "Sorry, the options you've chosen for the usage calculator indicate that you aren't using any gas.";
			}

		}
		
		if ($("#chooseElec:checked").val() == 1)
		{
			//are there any elec options chosen

			if (
			($("select[name=CookingSource]").val() == "Gas") &&
			($("select[name=CentralHeating]").val() == "Gas")
			)
			{
				error_msg = "Sorry, the options you've chosen for the usage calculator indicate that you aren't using any electricity.";
			}
		
		}
		
		if (error_msg == ""){
	
			$.ajax({
				url : "/ajax/switching/estimator.php",
				type : "post",
				dataType : "json",
				data : data,
				cache : false,
				success : function(msg){
					if(msg != 0){

						elecUsage = msg.elec;
						gasUsage = msg.gas;
						showForm = false;

						if($("#chooseElec").is(":checked")){
							$("input[name=elecSpend]").val("");
							$("input[name=eleckWh]").val(elecUsage);
						}
						if($("#chooseGas").is(":checked")){
							$("input[name=gasSpend]").val("");
							$("input[name=gaskWh]").val(gasUsage);
						}

						$("#estimate").colorbox.close()

					}
				}
			});
		}else{
			
			alert(error_msg);
			
		}
		
		return false;
	});
	
	}
});

