/*
 * TODO: complete conversion to jquery-
 * bottom portion already started by converting getElementById to $() & .value= to .val()
*/
  function addValuePak(){
      document.getElementById("gofees").value=D+"0.00";
    var vp = 149;
    var tp5 = parseFloat(document.getElementById("almPrice").value.replace(D,""));
    var tp4 = parseFloat(document.getElementById("eePrice").value.replace(D,""));
    var total = vp+tp5+tp4;  
    document.getElementById("monthly").value=D+total+".00";
  }
// ############################################################################################
  function addUser(){
    var num=parseFloat(document.getElementById("users").value);
    
    if(num>5){
      document.getElementById("proUserPrice").value=D+num*55+".00";
    }else{
      document.getElementById("proUserPrice").value=D+"275.00";    
    }    
    
    if(num<=3){
      document.getElementById("userPrice").value=D+num*p1+".00";
      document.getElementById("gofees").value=D+"0.00";
    }else{
      calcGo();
      document.getElementById("userPrice").value="N/A";
      document.getElementById("qbiPrice").value="N/A";
      document.getElementById("capPrice").value="N/A";
      document.getElementById("oePrice").value="N/A";
      document.getElementById("eePrice").value="N/A";
      document.getElementById("almPrice").value="N/A";
      document.getElementById("gofees").value="N/A";
      document.getElementById("monthly").value="N/A";
      return false;
    }

    addPackage('oe','n'); 
    addPackage('cap','n'); 
    addPackage('qbi','n');

    var eenum=document.getElementById("ee").value;
    addUserPackage(eenum,"ee",'y');

    var almnum=document.getElementById("alm").value;
    addUserPackage(almnum,"alm",'y');

   calcGo();
  }
// ############################################################################################
  function addPackage(pre,calc){
    if(document.getElementById(pre).value=="Yes"){
      if(pre=="oe"){
          var proprice="0.00";
          var price=p3+".00";
      }else if(pre=="cap"){
          var proprice=100+".00";
          var price=p4+".00";
      }else if(pre=="qbi"){
          var proprice=695+".00";
          var price=p5+".00";
      }
      document.getElementById("pro"+pre+"Price").value=D+proprice;
      if(parseFloat(document.getElementById("users").value)<=3){document.getElementById(pre+"Price").value=D+price;}
    }else{
      document.getElementById("pro"+pre+"Price").value=D+"0.00";
      if(parseFloat(document.getElementById("users").value)<=3){ document.getElementById(pre+"Price").value=D+"0.00";}
    }
    if(calc!="n"){
    calcGo();
    }
    
  }
// ############################################################################################ 
  function addUserPackage(num,pre,calc){
    var max=parseFloat(document.getElementById("users").value);
    if(num>max){
      document.getElementById(pre).value=max;
      if(parseFloat(document.getElementById("users").value)<=3){document.getElementById(pre+"Price").value=D+max*p6+".00";}
      document.getElementById("pro"+pre+"Price").value=D+max*p6+".00";
    }else{
      if(parseFloat(document.getElementById("users").value)<=3){document.getElementById(pre+"Price").value=D+num*p6+".00";}
      document.getElementById("pro"+pre+"Price").value=D+num*p6+".00";
    }
    if(calc!="n"){
    calcGo();
    }
  }
// ############################################################################################
  function calcGo(){
  
    var cusers = document.getElementById("users").value;
    var coe = document.getElementById("oe").value;
    var ccap = document.getElementById("cap").value;
    var cqbi = document.getElementById("qbi").value;
  
    var UP=document.getElementById("userPrice").value.replace(D,"");
    var vp=p2+".00";
    if(UP!=vp){
      var tp6 = parseFloat(document.getElementById("qbiPrice").value.replace(D,""));
      var tp3 = parseFloat(document.getElementById("capPrice").value.replace(D,""));
      var tp2 = parseFloat(document.getElementById("oePrice").value.replace(D,""));
    }else{
      var tp6 = 0;
      var tp3 = 0;
      var tp2 = 0;
    }
    var tp5 = parseFloat(document.getElementById("almPrice").value.replace(D,""));
    var tp4 = parseFloat(document.getElementById("eePrice").value.replace(D,""));
    var tp1 = parseFloat(document.getElementById("userPrice").value.replace(D,""));
    var total=tp1+tp2+tp3+tp4+tp5+tp6;
    if(parseFloat(document.getElementById("users").value)<=3){
   
      if(cusers=="3"&&coe=="Yes"&&ccap=="Yes"&&cqbi=="Yes"){
        addValuePak();
      }else{   
        document.getElementById("monthly").value=D+total+".00";
      }
   
    }

   var pro6 = parseFloat(document.getElementById("proqbiPrice").value.replace(D,""));
   var pro5 = parseFloat(document.getElementById("proalmPrice").value.replace(D,""));
   var pro4 = parseFloat(document.getElementById("proeePrice").value.replace(D,""));
   var pro3 = parseFloat(document.getElementById("procapPrice").value.replace(D,""));
   var pro2 = parseFloat(document.getElementById("prooePrice").value.replace(D,""));
   var pro1 = parseFloat(document.getElementById("proUserPrice").value.replace(D,""));
   var prototal=pro1+pro2+pro3+pro4+pro5;
   var fees = pro6 + 495.00 + (parseFloat(document.getElementById("users").value) * 99);
   
   document.getElementById("profees").value=D+fees+".00";
   document.getElementById("proMonthly").value=D+prototal+".00";
   ROI();
  }
// ############################################################################################

  function ROI(){
//  alert("ROI calc");
    var nbe =   parseFloat($("#nbe").val().replace(D,""));
    var abr =   parseFloat($("#abr").val().replace(D,""));
    var abhpm = parseFloat($("#abhpm").val().replace(D,""));

    var pf = parseFloat($("#profees").val().replace(D,""));
    var pm = parseFloat($("#proMonthly").val().replace(D,""));

    var gf = parseFloat($("#gofees").val().replace(D,""));
    var gm = parseFloat($("#monthly").val().replace(D,""));

    var pct = 0.15
// ---------------------
    var proBE = (pf/24 + pm)/(nbe*abr);
    
    var goBE = (gf/24 + gm)/(nbe*abr);    
 
 
    var AHC = abhpm*pct;
    var TMG = (nbe*abr*abhpm)*pct;

    var proA = (TMG*12)-((pf/2)+(pm*3))
    var goA = (TMG*12)-((gf/2)+(gm*3))
// ---------------------
    $("#proBreakEven").val(proBE.toFixed(1));
    $("#proAdditional").val(AHC.toFixed(1));
    $("#proGain").val(D+TMG.toFixed(2));
    $("#proAnnual").val(D+proA.toFixed(2));
    
    if(parseFloat($("#users").val())>3){
      $("#goBreakEven").val("N/A");
      $("#goAdditional").val("N/A");
      $("#goGain").val("N/A");
      $("#goAnnual").val("N/A");
    }else{
      $("#goBreakEven").val(goBE.toFixed(1));
      $("#goAdditional").val(AHC.toFixed(1));
      $("#goGain").val(D+TMG.toFixed(2));
      $("#goAnnual").val(D+goA.toFixed(2));
    } 
  } 

   
$(document).ready(function(){
  
  $("#results").click(function() {
    if ($(this).html()=="View Results") {
      $(this).html("Recalculate");
      $("#calculator").slideUp("slow");
      $("#roi").slideDown("slow");
      if($("#print").hasClass("hide")){
        $("#print").removeClass("hide");
      }
    }else{
      $(this).html("View Results");
      $("#calculator").slideDown("slow");
      $("#roi").slideUp("slow");
    }
  });

  $("#print").click(function() {
 
    var PU = $("#users").val();
    
    var PQ = $("#qbi").val();
    if(PQ=="Select"){PQ="No"}
    var PO = $("#oe").val();
    if(PO=="Select"){PO="No"}
    var PC = $("#cap").val();
    if(PC=="Select"){PC="No"}
    var PE = $("#ee").val();
    var PA = $("#alm").val();          
    var PN = $("#nbe").val();
    var PR = $("#abr").val();
    var PH = $("#abhpm").val();
    
    var PPF = $("#profees").val();
    var PGF = $("#gofees").val();
    var PPM = $("#proMonthly").val();
    var PGM = $("#monthly").val();
    var PPBE = $("#proBreakEven").val();
    var PGBE = $("#goBreakEven").val();
    var PPAd = $("#proAdditional").val();
    var PGAd = $("#goAdditional").val();
    var PPG = $("#proGain").val();
    var PGG = $("#goGain").val();
    var PPA = $("#proAnnual").val();
    var PGA = $("#goAnnual").val();
    
    pageCode="<html><head><title>IT management software | IT services software | PSA software - Autotask Corporation</title><link rel=\"shortcut icon\" href=\"/autotask.ico\" type=\"image/x-icon\" /><link rel=\"icon\" href=\"/autotask.ico\" type=\"image/x-icon\"><link rel=\"stylesheet\" href=\"/css/print.css\" type=\"text/css\" media=\"print\" /><link rel=\"stylesheet\" href=\"/css/roi.css\" type=\"text/css\" media=\"screen\" /></head><body>"
    // logo
    pageCode+="<a href=\"#\" class=\"smlButton floatRight\" onclick=\"window.print();return false;\">PRINT</a><a href=\"http://www.autotask.com\"><img src=\"/images/homepage/autotask.gif\" alt=\"Autotask\"></a>"
    // table 1 start
    pageCode+="<table cellspacing=0 cellpadding=0 margin=0><tbody><tr><th colspan=2>Autotask Questions</th><th rowspan=7 style=\"width:2px;\"></th><th colspan=4>Productivity Questions</th></tr>"
    // row 1
    pageCode+="<tr><td>Number of Users?</td><td width=70 align=center>"+ PU +"</td><td>Number of billable employees?</td><td colspan=3 align=center width=70 >"+ PN +"</td></tr>"
    // row 2
    pageCode+="<tr><td>Quickbooks&reg; Extension</td><td align=center>"+ PQ +"</td><td>Average bill rate</td><td colspan=3 align=center>"+ PR +"</td></tr>"
    // row 3
    pageCode+="<tr><td>MS Outlook Extension</td><td align=center>"+ PO +"</td><td>Average billable hrs/person/mo</td><td colspan=3 align=center>"+ PH +"</td></tr>"
    // row 4
    pageCode+="<tr><td>Client Access Portal with Taskfire&trade;</td><td align=center>"+ PC +"</td><td colspan=4 rowspan=3>&nbsp;</td></tr>"
    // row 5
    pageCode+="<tr><td>MS Exchange Extension</td><td align=center>"+ PE +"</td></tr>"
    // row 6
    pageCode+="<tr><td>Autotask LiveMobile</td><td align=center>"+ PA +"</td></tr></tbody></table>"
    // table 2 start
    pageCode+="<table cellspacing=0 cellpadding=0 margin=0><tbody><tr><th>ROI Analysis</th><th width=170>Autotask Pro</th><th width=170>Autotask Go!</th></tr>"
    // row 1
    pageCode+="<tr ><td>Total Getting Started Fees (1X)</td><td align=center>"+ PPF +"</td><td align=center>"+ PGF +"</td></tr>"
    // row 1
    pageCode+="<tr ><td>Ongoing Monthly Investment</td><td align=center>"+ PPM +"</td><td align=center>"+ PGM +"</td></tr>"
    // row 1
    pageCode+="<tr><td colspan=3 class=\"nb\"><hr /></td></tr>"
    // row 2
    pageCode+="<tr><td>BREAK-EVEN HRS. / billable employee)</td><td align=center>"+ PPBE +"</td><td align=center>"+ PGBE +"</td></tr>"
    // row 3
    pageCode+="<tr><td>Add'l Hrs. Captured / billable employee</td><td align=center>"+ PPAd +"</td><td align=center>"+ PGAd +"</td></tr>"
    // row 4
    pageCode+="<tr><td>Total Monthly Gain In Revenue</td><td align=center>"+ PPG +"</td><td align=center>"+ PGG +"</td></tr>"
    // row 5
    pageCode+="<tr><td>Annual Net ROI*</td><td align=center>"+ PPA +"</td><td align=center>"+ PGA +"</td></tr></tbody></table>"
    // body end
    pageCode+="</body></html>"
    
    var newWin = window.open("", "", "menubar=1,location=0,toolbar=0,width=800,height=600,resizable,scrollbars=yes");
    
      newWin.document.write(pageCode); 
      newWin.document.close(); 
  
  });
  
});