// JavaScript Document

// these are for the Epoch calendar
var occur_cal, install_cal;      

// array to auto-populate part descriptions from part numbers.
/* var descrip_array = new Array();
descrip_array[0] = "";
descrip_array[1] = "CYLINDER ASSY";
descrip_array[2] = "POWER ASSY";
descrip_array[3] = "CYLINDER ASSY";
descrip_array[4] = "POWER ASSY";
descrip_array[5] = "POWER ASSY";
descrip_array[6] = "CYLINDER ASSY";
descrip_array[7] = "CYLINDER ASSY";
descrip_array[8] = "POWER ASSY";
descrip_array[9] = "POWER ASSY";
descrip_array[10] = "POWER ASSY";
descrip_array[11] = "POWER ASSY";
descrip_array[12] = "POWER ASSY";  
descrip_array[13] = "CYLINDER ASSY";
descrip_array[14] = "POWER ASSY";
descrip_array[15] = "POWER ASSY";
descrip_array[16] = "CYLINDER ASSY";
descrip_array[17] = "POWER ASSY";
descrip_array[18] = "CYLINDER ASSY";
descrip_array[19] = "POWER ASSY";
descrip_array[20] = "POWER ASSY";
descrip_array[21] = "CYLINDER ASSY";
descrip_array[22] = "POWER ASSY";
descrip_array[23] = "POWER ASSY";
descrip_array[24] = "POWER ASSY";
descrip_array[25] = "CYLINDER ASSY";
descrip_array[26] = "POWER ASSY";
descrip_array[27] = "POWER ASSY";
descrip_array[28] = "CYLINDER ASSY";
descrip_array[29] = "POWER ASSY";
descrip_array[30] = "POWER ASSY";
*/
     
window.onload = function () {
	occur_cal  = new Epoch('epoch_popup','popup',document.getElementById('occur_date'));
	install_cal  = new Epoch('epoch_popup','popup',document.getElementById('install_date'));
};

// These are the widths that the new dynamic columns take on...
//var part_num_width = 10;
//var quantity_width = 7;
var serial_width = 20
var desc_width = 35;
//var cost_width = 4;

// The starting rowcount, will be incremented each time user adds a row.
//var table_rows = 2;
//var table_rows = 7
var table_rows = asp_table_rows;

// Helper function to strip row numbers from object names.
function getNum(myString){
  var pattern=/[0-9]+$/;
  logs = pattern.exec(myString);
  return logs;
}

function removeDollarSign(myString) {
  var pattern=/[0-9.]+$/;
  logs = pattern.exec(myString);
  return logs;
}

// Helper function for currency values
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 labor_changed() {
	hours = document.getElementById("labor_hrs").value;
	cpr = document.getElementById("labor_cph").value;
	document.getElementById("labor_total").innerHTML = formatCurrency(hours * cpr);
	recalc_total();
}

function ri_changed() {
	hours = document.getElementById("ri_hrs").value;
	cpr = document.getElementById("ri_cph").value;
	document.getElementById("ri_total").innerHTML = formatCurrency(hours * cpr);
	recalc_total();
}

function recalc_total() {
    parts = removeDollarSign(document.getElementById("ptotal").innerHTML);
	labor = removeDollarSign(document.getElementById("labor_total").innerHTML);
	ri_labor = removeDollarSign(document.getElementById("ri_total").innerHTML);
	outside_repairs = removeDollarSign(document.getElementById("outside_repairs").value);
	freight_cost = removeDollarSign(document.getElementById("freight_cost").value);
	//tax = removeDollarSign(document.getElementById("tax").value);
	
	total = (parts * 1) + (labor * 1) + (ri_labor * 1) + (outside_repairs * 1) + (freight_cost * 1); // + (tax * 1);
	document.getElementById("grand_total").innerHTML = formatCurrency(total);
}

function failed_part_change(obj) {
  c_part = document.eng_warranty.failed_part_num;
  document.getElementById("part1").innerHTML = c_part.value;
  document.getElementById("desc1").innerHTML = descrip_array[c_part.selectedIndex];
  //document.eng_warranty.part1.selectedIndex = document.eng_warranty.failed_part_num.selectedIndex;
  //update_part_description(document.eng_warranty.part1);
}

function update_part_description(obj) {
  rownum = getNum(obj.name);
  idx = obj.selectedIndex;
  document.getElementById("desc" + rownum).innerHTML = descrip_array[idx];
  //alert("row:" + rownum + " idx:" + idx);
}

// fired when the data is changed in any of the relevant text inputs.
function data_changed(obj) {
  // obj is a text input.  Given this, we need to
  // get objects for the OTHER text input affecting this,
  // and the last cell so we can change the sum.
  
  // trim off the row number
  rownum = getNum(obj.name);
  
  //quantity_control = document.getElementById("quant" + rownum);
  cost_control = document.getElementById("cost" + rownum);
  sum_cell = document.getElementById("sum" + rownum);
  
  //sum_cell.innerHTML = formatCurrency(quantity_control.value * cost_control.value);
  sum_cell.innerHTML = formatCurrency(cost_control.value);
  
  // Deal with the parts subtotal  
  var pretotal = 0;
  for(n=1 ; n<table_rows ; n++) {   
    pretotal = (1 * pretotal) + (1 * removeDollarSign(document.getElementById("sum" + n).innerHTML)); 
  }
  
  document.getElementById("ptotal").innerHTML = formatCurrency(pretotal);
  
  recalc_total();
  
  return false;
}

function copy_serial(obj) {
  document.getElementById("serial1").innerHTML = document.getElementById("comp_serial_num").value;	
}

function click_button() {
  var the_table;
  
  the_table = document.getElementById('the_table');
  the_row = the_table.insertRow(table_rows+1);
  cell1 = the_row.insertCell(0);
  cell2 = the_row.insertCell(1);
  cell3 = the_row.insertCell(2);
  //cell4 = the_row.insertCell(3);
  //cell5 = the_row.insertCell(4);
  
  part_select = "<select name=\"part" + table_rows + "\" onchange=\"update_part_description(this)\" />"
  for (x=0; x < part_array.length; x++) {
    part_select = part_select + "<option value=\"" + part_array[x] + "\">" + part_array[x] + "</option>\r\n";
  }
  part_select = part_select + "</select>";
  
  cell1.innerHTML = part_select;
     
  cell2.innerHTML = "<input type=\"text\" id=\"serial" + table_rows + "\" name=\"serial" + table_rows + "\" size=\"" + serial_width + "\">";
  cell3.id = "desc" + table_rows;
  //cell4.innerHTML = "$ <input type=\"text\" id=\"cost" + table_rows + "\" name=\"cost" + table_rows + "\" size=\"" + cost_width + "\" onChange=\"data_changed(this);\">";
  //cell5.id = "sum" + table_rows;
  //cell5.style.textAlign = "right";
  //cell5.innerHTML = "$0.00";
  //cell5.outerHTML = "<td class=\"currency\" id=\"sum" + table_rows + "\">$0.00</td>";
  table_rows++;
  
  return false;
}





