Licenses         = null;
Edition          = null;
Term             = null;
Implementation   = false;
  
$(function(){
  Edition = $('#edition :selected').val();
  Term = $('#term :selected').val();
  Licenses = $('#licenses :selected').val();
  $('#services input').each(function(){
    updateService(this);
  });
  triggerUpdate();
  $("#edition").change(function () {
    Edition = this.value;
    updateEdition(Edition);
  });
  $("#term").change(function () {
    Term = this.value;
    updateTerm(Term);
  });
  $("#licenses").change(function () {
    Licenses = this.value;
    updateLicense(Licenses);
  });
  $("#services input").change(function () {
    updateService(this);
  });
});

function updateEdition(Edition){
    var TermDef = '---';
    var LicDef = '---';
    switch(Edition)
    {
    case 'Standard':
      addSelNumbers('licenses',10);
      TermDef = 'Monthly';
      LicDef = '1';
      break;
    case 'Enterprise':
      addSelNumbers('licenses',10);
      TermDef = 'Annual';
      LicDef = '1';
      break;
    case 'Community':
      addSelNumbers('licenses',1);
      TermDef = 'Monthly';
      LicDef = '1';
      break;
    default:
      Edition = null;
    }
    termSelEdition(Edition);
    $('#term').val(TermDef).change();
    $('#licenses').val(LicDef).change();
    triggerUpdate();
}

function updateTerm(Term) {
  if (Term=='---') {Term=null;}
  triggerUpdate();
}

function updateLicense(Licenses) {
  if (Licenses=='---') {Licenses=null;}
  triggerUpdate();
}

function updateService(Services) {
  switch(Services.value)
  {
    case 'Managed Pilot':
      Implementation = Services.checked;
      break;
    default:
      break;
  }
  triggerUpdate();
}

function triggerUpdate() {
  SuccessMsg       = null;
  UpfrontPayment   = null;
  RecurringPayment = null;
  TooMany          = false;
  Price            = null;
  Discount         = 0;
  EditionChart     = null;
  Also             = null;

  $('#licenses').attr("disabled","");
  $('#term').attr("disabled","");
  $('#total_msg').hide();
  $('#edition_cost').hide();
  

//=====================================================================
//******************** BASIC RULES ************************************ 
//=====================================================================

//  if ((Edition == 'Standard') && (Licenses > 10)) {TooMany = true;}
  if ((Edition == 'Community') && (Licenses > 1)) {TooMany = true;}

//=====================================================================
//******************** GET RECURRING SOFTWARE PRICE *******************
//=====================================================================

  switch(Edition)
  {
  case 'Standard':
    Price = 10000;
    EditionChart = new Array(1000,1000,15,1,10,3,1000);
    break;
  case 'Enterprise':    
    Price = 20000;
    EditionChart = new Array(99999,99999,30,1,20,5,2000);
    break;
  case 'Community':
    Licenses = 1;
    Price = 3000;
    EditionChart = new Array(2500,null,25,1,5,5,250);
    break;
  default:
    $('#licenses').attr("disabled","disabled");
    $('#term').attr("disabled","disabled");
  }

  if (!TooMany) {
    switch(Term)
    {
    case 'Monthly':
      Price = EditionChart[6];
      break;
    case 'Annual':
      break;
    default:
      break;
    }

    if (Licenses > 4) {// 20% off at 5 Licenses
      Discount += .20;
    }
    if (Licenses > 9) {// 20% off at 10 Licenses
      Discount += .20;
    }

    Price = (Price - (Price * Discount)) * Licenses; // Adjust for Discount and License Count

    RecurringPayment = Price;       // Final Recurring Price

//=====================================================================
//******************** ADJUST FOR UPFRONT SERVICES ********************
//=====================================================================
    
    if (Implementation) {Price += 4000;}              // Adjust for Managed Pilot
    UpfrontPayment = Price                     // Initial Payment

//=====================================================================
//******************** TELL THEM WHAT THEY'VE WON! ********************
//=====================================================================

    if (($.isArray(EditionChart)) && (Licenses>0) && (Term!=null)) {
      if (EditionChart[0]==99999) {
        $('#CommunityMembers').text('Unlimited');
      } else {
        $('#CommunityMembers').text(formatNum(EditionChart[0]*Licenses));
      }
      if (EditionChart[2]==99999) {
        $('#TeamMembers').text('Unlimited');
      } else {
        $('#TeamMembers').text(formatNum(EditionChart[2]*Licenses));
      }
      if (EditionChart[3]==99999) {
        $('#ProjectManagers').text('Unlimited');
      } else {
        $('#ProjectManagers').text(formatNum(EditionChart[3]*Licenses));
      }
      $('#DiskSpace').text((EditionChart[4]*Licenses)+'GB');
      $('#ActiveProjects').text(EditionChart[5]*Licenses);
      $('#MonthlyPrice').text(formatCurrency(RecurringPayment,false));
      $('#NamePrice').text(Term+" Price");
      $('#edition_cost').show();
    }

    SuccessMsg = '';
    if (((Edition!=null) && (Licenses!=null) && (Term!=null)) && ((RecurringPayment>0) || (UpfrontPayment>0))) {
      SuccessMsg = "You will be paying <b>" + formatCurrency(UpfrontPayment,false) + "</b> initially, with recurring payments of <b>" + formatCurrency(RecurringPayment,false) + "</b> on a " + Term + " basis.<br>";
      if (Term=="Annual") {
        SuccessMsg += " You have received <b>2 Months Free</b> for committing the Annual Term.  Thank you for your confidence.<br>";
        Also = "n additional";
      } else {
        SuccessMsg += "You can cancel at any time.";
        Also = "";
      }
      if (Licenses > 9) {
       SuccessMsg += " You have received a" + Also + " <b>40% discount</b> for purchasing " + Licenses.toString() + " " + Edition + " Licenses.<br>"; Also = "n additional";
      } else if (Licenses > 4) {
       SuccessMsg += " You have received a" + Also + " <b>20% discount</b> for purchasing " + Licenses.toString() + " " + Edition + " Licenses.<br>"; Also = "n additional";
      }

      SuccessMsg += " We sincerely thank you for your business.";
      $('#total_msg').html(SuccessMsg);
      $('#total_msg').show();
    }
  }
}

function addSelNumbers(selBoxID,num) {
  var options = '';
  for (var i=1;i<=num;i++) {
    options += '<option value="' + i + '">' + i + '</option>';
  }
  $("select#"+selBoxID).html(options);
}

// Formats a Number into a Currency with $
function formatCurrency(num,isCents) {
  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;
  num = ((sign)?'':'-') + '$' + formatNum(num);
  if (isCents) {
    num = num + '.' + cents;
  }
  return (num);
}

function formatNum(num) {
  num = num.toString();
  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 num;
}

function termSelEdition(Edition) {
  $('#term option').remove();
  $('#term').append('<option value="---">---</option>');
  $('#term').append('<option value="Monthly">Monthly</option>');
  $('#term').append('<option value="Annual">Annual</option>');
}

