$jq = jQuery.noConflict();

$jq(document).ready(function(){
  
  $jq('#accordion .head').click(function() {
    if($jq(this).next('.aContent').is(':visible')){
		  $jq('.btn',this).attr("src","http://www.peaklogix.com/estimator/images/btn_plus.jpg");
	  }
	  else{
	    $jq('.btn',this).attr("src","http://www.peaklogix.com/estimator/images/btn_minus.jpg");
	  }
		$jq(this).next('.aContent').toggle('fast');
		return false;
	}).next().hide();

  $jq("#contactSubmit").click(function(){
    
    /*
    //$jq("#contact").fadeOut('slow', function(){$jq("#summary").show();});
    valid = false;
    $jq("#errMsg").hide();
    $jq("#contactForm input").each(function(){
      if($jq(this).hasClass('required')){
        $jq(this).parent().css('color','red');
        if($jq(this).val()==""){
          valid = false;
          $jq("#errMsg").show();
        }
        else{
          valid = true;
        }
      }
    })
    
    if(valid){


      $.ajax({
        type: "POST",
        url: "http://www.peaklogix.com/estimator/send.php", 
        data: {name: $jq("#name").val(), company: $jq("#company").val(), email: $jq("#email").val()},
        timeout: 10000,
        beforeSend: function(){
          $jq("#contactForm").hide();
          $jq("#contact").append("<h1 align='center'>Loading...</h1>");
        },
        error: function(request,textStatus,error){
          console.log(request.statusText+" "+request.status+" "+error);
          $jq("#contact").fadeOut('slow', function(){$jq("#summary").show();});
        },
        success: function(data){
          $jq("#contact").fadeOut('slow', function(){$jq("#summary").show();});
          console.log(data);
        }
        
      });
    
      

    }
    else{
      $jq("#errMsg").show();
    }
    
    */

  })

});

var totalLow = 0;
var totalHigh = 0;

function calculateBudget(){
  totalLow = 0;
  totalHigh = 0;
  count = 1;
  tablecontent = '';
  
  $jq('body').animate({scrollTop:0}, 'fast'); 
  
  $jq("#accordion .aContent").each(function(){
    if($jq(this).is(':visible')){
      $jq(this).toggle('fast');		  
	  }
	  $jq('.btn').attr("src","http://www.peaklogix.com/estimator/images/btn_plus.jpg");
  })
  
  $jq("#popup").fadeIn('fast');
  
  $jq("#summary").html("<div align='right' style='float:right;margin-top:10px;'><button onclick='printEstimate();'>Print</button></div><h1 align='center'>Your Budget</h1>");
  tablecontent+="<table id='estimatecontents' cellspacing='3' width='100%'>";
  $jq(".costInput").each(function(){
    
    if($jq(this).hasClass('costRadio')){
      if($jq(this).is(":checked")&&$jq(this).val()!='on'){
        var description = $jq(this).next(".costLabel").html();
        multiplyer=1;
        switch($jq(this).attr('id')){
          case "p004":
            multiplyer = $jq("#p002").val();
          break;
        }
        range = $jq(this).val().split('-');
        thisLow = parseFloat(range[0]*multiplyer);
        totalLow = parseFloat(totalLow + thisLow);
        thisHigh = parseFloat(range[1]*multiplyer);
        totalHigh = parseFloat(totalHigh + thisHigh);
        tablecontent+="<tr><td align='left'>"+count+") "+ description + " </td><td align='right'>" + formatCurrency(thisLow) + " - " + formatCurrency(thisHigh) + "</td></tr>";
        count++;
      }
      
    }
    else if($jq(this).hasClass('costSelect')){
      
      if($jq(this).val()!="select"){
        var description = $jq(this).next(".costLabel").html();
        range = $jq(this).val().split('-');
        thisLow = parseFloat(range[0]);
        totalLow = parseFloat(totalLow + thisLow);
        thisHigh = parseFloat(range[1]);
        totalHigh = parseFloat(totalHigh + thisHigh);
        tablecontent+="<tr><td align='left'>"+count+") "+ description + " </td><td align='right'>" + formatCurrency(thisLow) + " - " + formatCurrency(thisHigh) + "</td></tr>";
        count++
      }
      
    }
    else{
      if($jq(this).val()!="0"){
        
        if($jq(this).attr('id')=="p110"){
            var description = $jq(this).next().html();
            range = $jq(this).attr('name').split('-');
            
            if($jq(this).val()>"1"){
              low1 = parseFloat(84000*(parseFloat($jq(this).val()-1)));
              thisLow = parseFloat((range[0]*1)+low1);
              
              high1 = parseFloat(96000*(parseFloat($jq(this).val()-1)));
              thisHigh = parseFloat((range[1]*1)+high1);
            }
            else{
              thisLow = parseFloat(range[0]*$jq(this).val());
              thisHigh = parseFloat(range[1]*$jq(this).val());
            }
            
            
            totalLow = parseFloat(totalLow + thisLow);
            totalHigh = parseFloat(totalHigh + thisHigh);
            tablecontent+="<tr><td align='left'>"+count+") "+ description + ": x" +$jq(this).val() + " </td><td align='right'>" + formatCurrency(thisLow) + " - " + formatCurrency(thisHigh) + "</td></tr>";
            count++ ;
        }
        else {					
					var description = $jq(this).next(".costLabel").html();
          range = $jq(this).attr('name').split('-');
          thisLow = parseFloat(range[0]*$jq(this).val());
          totalLow = parseFloat(totalLow + thisLow);
          thisHigh = parseFloat(range[1]*$jq(this).val());
          totalHigh = parseFloat(totalHigh + thisHigh);
          tablecontent+="<tr><td align='left'>"+count+") "+ description + ": x" +$jq(this).val() + " </td><td align='right'>" + formatCurrency(thisLow) + " - " + formatCurrency(thisHigh) + "</td></tr>";
          count++ ;
        }
      }
    }
    
    
  })
  tablecontent+="</table>";
  summaryButtons = '<div align="center"><button onclick="$jq(\'#popup\').hide();">Close</button></div>';
  $jq("#summary").append(tablecontent+"<div id='estimatetotal' align='center' style='font-size:16px;'><b>Your Estimated Cost: " + formatCurrency(totalLow) + " - " + formatCurrency(totalHigh) + "</b></div><br>" + summaryButtons);
}

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num); // + '.' + cents);
}

function clearForm(){
  if(confirm("Are you sure you wish to clear the Estimator? This is not undoable.")){
    $jq("input.costInput").val('0');
  }
}

var EstimateContent;

function printEstimate(){
  EstimateContent = "<table cellpadding='3'>"+$jq("#estimatecontents").html()+"</table><br><br><div align='left'>"+$jq("#estimatetotal").html()+"</div>";
  $jq("#ph_container").hide();
  $jq("#ph_container").before("<div id='printscreen'></div>");
  $jq("#printscreen").load("/estimator/print.php", function(){
    $jq("#estimateSummary").html(EstimateContent);
    window.print();
  });
  
}

function closePrint(){
  $jq("#printscreen").remove();
  $jq("#ph_container").show();
}
