(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);
function duo()
{
	var ht=$("#tr_content_strom input[name='"+kwh_ht+"']").val();
	var nt=$("#tr_content_strom input[name='"+kwh_nt+"']").val();  
    
	if($("#tr_content_strom div.big_select select").val()=="D") {		
		
		if(ht==label_kwh) {
			$("#tr_content_strom input[name='"+kwh_ht+"']").val(label_kwh_ht);  
			}
		// wenn ein wert eingegeben wurde, dann...
		if(parseInt(ht) && ht!=label_kwh_ht) {
			// wenn privat, dann...
			if ($("#tr_content_strom div.radios #H").attr("class") == 'round radio_act'){
				calcHTNT($("#tr_content_strom input[name='"+kwh_ht+"']").val(), "H");
				}			
			// wenn geschäft, dann...
			else {
				calcHTNT($("#tr_content_strom input[name='"+kwh_ht+"']").val(), "G");
				}
			}
		$("#tr_content_strom input[name='"+kwh_nt+"']").slideDown("slow");			
		}
	else {
		if(parseInt(ht) && ht!=label_kwh_ht && parseInt(nt) && nt!=label_kwh_nt) {
			calcHT($("#tr_content_strom input[name='"+kwh_ht+"']").val(), $("#tr_content_strom input[name='"+kwh_nt+"']").val());		
			}
		$("#tr_content_strom input[name='"+kwh_nt+"']").slideUp("slow"); 
			
		var ht=$("#tr_content_strom input[name='"+kwh_ht+"']").val();  
	
		if(ht==label_kwh_ht) {
			$("#tr_content_strom input[name='"+kwh_ht+"']").val(label_kwh);  
			}
		$("#tr_content_strom input[name='"+kwh_nt+"']").val(label_kwh_nt);  	    	 
     
          }   
}

function getNTField(ob) {
	

	if($(ob).val()=='D'){
		
		// NT betrag aufteilen
		if ($("#ntinput").css('display') == 'none'){
			calcHTNT($("input[name='+kwh_ht+']").val());
			}
		
		$("#ntinput").slideDown("slow");
		$("#ntnz").fadeIn("slow");
		
		} else {
			if ($("#ntinput").css('display') == 'block'){
				calcHT($("input[name='+kwh_ht+']").val(), $("input[name='+kwh_nt+']").val());
			}
			
			if ($("input[name='+kwh_nt+']").val() != ""){
				$("input[name='+kwh_nt+']").val('');
				}

			$("#ntinput").slideUp("slow");
			$("#ntnz").fadeOut("slow");
			}
	}


var customSelects={"strom":false,"gas":false};
$(document).ready(function(){
	
	   $("input.trs_input").blurAndFocus();
       
       $("form[name='tarifrechner']").submit(function()
       {
          if($("#tr_content_strom div.big_select select").val()=="D")
	      {       
	      var nt_val=$("#tr_content_strom input[name='"+kwh_nt+"']").val();  
	           if(!parseInt(nt_val) || nt_val=='label_kwh_nt')
	           {
	               alert('Um Duo Tarif berechnen zu können müssen Sie das Feld „Verbrauch KWh Nacht“ ausfüllen');
	               return false;
               }
          }
           
       }
       );
  
	  
	  $("#tr_content_strom div.big_select select").change(duo);
	  duo();
	  //ie fix
	  if($("div.persons").css("display")=="none")
	  {
	     reHide=true; 
	     $("div.persons").show(); 
      }

	   if($("#tr_content_strom").css("display")=="none")
       {
  	        $('#tr_content_gas select.styled').customStyle();
            customSelects['gas']=true;

       }
       else
       {
     	    $('#tr_content_strom select.styled').customStyle();        
             customSelects['strom']=true;     
       }
       //ie fix
       if(typeof reHide!="undefined")
          $("div.persons").hide();

	   
	   
   
       $("#tr_main div.strom ").click(function()
       {
           
           $("#tr_main div.strom").attr("class","strom act");
           $("#tr_main div.gas").attr("class","gas");     
                
           $("#tr_content_strom").show();
           $("#tr_content_gas").hide();           
           if(!customSelects['strom'])
           {
  	  if($("div.persons").css("display")=="none")
	  {
	     reHide=true; 
	     $("div.persons").show(); 
      }
             
       	    $('#tr_content_strom select.styled').customStyle();        
             customSelects['strom']=true;  
       //ie fix
       if(typeof reHide!="undefined")
          $("div.persons").hide();                             
           }
               
       });        
       $("#tr_main div.gas").click(function()
       {

           $("#tr_main div.strom").attr("class","strom");
           $("#tr_main div.gas").attr("class","gas act");             
       
           $("#tr_content_strom").hide();
           $("#tr_content_gas").show();      
           if(!customSelects['gas'])
           {
	  if($("div.persons").css("display")=="none")
	  {
	     reHide=true; 
	     $("div.persons").show(); 
      }
               
       	    $('#tr_content_gas select.styled').customStyle();        
             customSelects['gas']=true;                  
       //ie fix
       if(typeof reHide!="undefined")
          $("div.persons").hide(); 
           }                    
       });    
       
       	   
      $radio=$("#tr_content_strom div.radios li.round, #tr_content_gas div.radios_gas li.round").click(function()
      {
          $(this).parent().children("li.round").attr("class","round radio");
          var v=$(this).attr("title");
		  //alert(v);
          $(this).parent().parent().children("input").val(v);
          $(this).attr("class","round radio_act");
         if(v=="H")
         {
            $form=$(this).parents("form");
            $form.find("div.persons").show();
            
             $form.attr("action",$form.attr("action").replace("gewerbekunden/","privatkunden/"));
			 
			 var ht=$("#tr_content_strom input[name='"+kwh_ht+"']").val();
			 var nt=$("#tr_content_strom input[name='"+kwh_nt+"']").val();
			 
			 if($("#tr_content_strom div.big_select select").val()=="D") {
				 if (parseInt(ht) && ht!=label_kwh_ht && parseInt(nt) && nt!=label_kwh_nt){
					 calcG2H();
					 }				 
				 }			 
			 
         }
         else
         {
            $form=$(this).parents("form");
            $form.find("div.persons").hide();
            
             $form.attr("action",$form.attr("action").replace("privatkunden/","gewerbekunden/"));
			 
			 var ht=$("#tr_content_strom input[name='"+kwh_ht+"']").val();
			 var nt=$("#tr_content_strom input[name='"+kwh_nt+"']").val();
			 
			 if($("#tr_content_strom div.big_select select").val()=="D") {
				 if (parseInt(ht) && ht!=label_kwh_ht && parseInt(nt) && nt!=label_kwh_nt){
					 calcH2G();
					 }				 
				 }	
			 
         }
                  
      });
      $('#tr_content_strom select[name="erwachsene"],#tr_content_strom select[name="kinder"]').change(updateKWH);
      $('#tr_content_gas select[name="erwachsene"],#tr_content_gas select[name="kinder"]').change(updateKWHGas);
        	   
 
});
function updateKWH() {	

	if($("#tr_content_strom div.big_select select").val()=="D") {
		var gesKwh = $('#tr_content_strom select[name="erwachsene"]').val()*kwhPerAdult+$('#tr_content_strom select[name="kinder"]').val()*kwhPerChild;
		calcHTNT(gesKwh, "H");		
	}
	else {
		$("#tr_content_strom input[name='"+kwh_ht+"']").val($('#tr_content_strom select[name="erwachsene"]').val()*kwhPerAdult+$('#tr_content_strom select[name="kinder"]').val()*kwhPerChild);
	}
} 
function updateKWHGas()
{
     $("#tr_content_gas input[name='"+kwh_ht_gas+"']").val($('#tr_content_gas select[name="erwachsene"]').val()*kwhPerAdultGas+$('#tr_content_gas select[name="kinder"]').val()*kwhPerChildGas);
} 

