jQuery(document).ready(function(){
  jQuery('select[name="GoMenu"]').bind('change', function(){
    var target = jQuery('select[name="GoMenu"] option:selected').val();
    if (target) {
      NewWin = window.open(target,"_self");
      window['NewWin'].focus();
    }
  });
  
  jQuery('#slider').bind('click', function(){
    window.location="/physicians/photo-gallery";
  });
  
  if (jQuery('#calculate-income').length > 0) {
    jQuery("#calculate-income").bind('click', function(){
      var cost_per_client = parseInt(jQuery("#treatment-income").val());
      if (cost_per_client > 0) {
        var output = outputIncomeTable(cost_per_client);
        jQuery(".income-table").html(output);
      } else {
        alert("Please enter your expected cost per client.");
      }
      return false;
    });
  }
  
});

function outputIncomeTable(cost_per_treatment) {
  var columns = [["1 (1 unit)", 1], ["2", 2], ["3", 3], ["4",4], ["5", 5],
                  ["6 (2 units)", 6], ["7", 7], ["8", 8], ["9",9], ["10",10],
                  ["11 (3 units)",11], ["12",12], ["13",13], ["14",14], ["15",15],
                  ["20 (4 units)", 20], ["25 (5 units)", 25]];
  var ret = "<table><tr><th>Customers</th><th>Per day</th><th>Per Week</th><th>Per Month</th><th>Income Per Year</th></tr>";
  for(var k in columns) {
    var tuple = columns[k];
    var label = tuple[0];
    var num_cust = tuple[1];
    ret += "<tr><td>" + label + "</td>" + makeIncomeElement(num_cust, cost_per_treatment, 1) +
      makeIncomeElement(num_cust, cost_per_treatment, 2) +
      makeIncomeElement(num_cust, cost_per_treatment, 3) +
      makeIncomeElement(num_cust, cost_per_treatment, 4) + "</tr>";
      
      if (num_cust % 5 == 0) {
        ret += "<tr class=\"spacer\"><td colspan=\"5\"></td><tr>";
      }
  }
  ret += "</table>";
  return ret;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function makeIncomeElement(num, cost, type) {
  var multiplier = 1;
  switch(type) {
    case 2:
      multiplier = 5;
      break;
    case 3:
      multiplier = 20;
      break;
    case 4:
      multiplier = 20*12;
      break;
    default:
      multiplier = 1;
  }
  return "<td>$" + addCommas("" + (num*cost*multiplier).toFixed(2)) + "</td>";
}

function OnGoMenuFormLink(GoList)
{
   var url = GoList.options[GoList.selectedIndex].value;
   var target = "_self";
   GoList.selectedIndex=0;
   GoList.blur();
   if (url)
   {
      NewWin=window.open(url,target);
      window['NewWin'].focus()
   }
}

