var numRows = 0;
var maxRows = 10;
var totalRows = 0;

var rows = new Array();

function trim(str) {
    str = str.replace(/^[\s]+/g,"");
    str = str.replace(/[\s]+$/g,"");

    return str;
}

function isEmpty(id) {
    if (trim(id.value).length == 0)
        return true;
    else
        return false;
}

function addList(elem) {
    if (isEmpty(elem)) {
        alert("Cannot be empty");
	      setFocus;
    }
    else if (numRows == maxRows) {
        alert ("Calculator doesn't allow more than " + maxRows + " lists");
	      setFocus;
    }
    else {
        addRow();
	      setFocus;
    }
}

function formReset() {
    deleteLists();
    s.setValue(0);

    document.frmSubscrib.reset();	
	  document.getElementById("subscribers").value = s.getValue();

    setFocus();
}

function setFocus() {
  document.getElementById("txtListName").focus();
}

function getTotalMembers() {
  
  var totalMembers = 0;
  
  for (counter = 0; counter < totalRows; counter++) {
	  var attributes = rows["row-" + counter];
	  
	  if (attributes["active"]) {
	    totalMembers = totalMembers + attributes['members'];
	  }
	}

  return totalMembers;
}

function getTotalArchives() {
  
  var totalArchives = 0;
  
  for (counter = 0; counter < totalRows; counter++) {
	  var attributes = rows["row-" + counter];
	  
	  if (attributes["active"]) {
	    if (attributes['archive']) {
        totalArchives++;
      }
	  }
	}
  
  return totalArchives;
}

function getBracket() {
  
  var bracket = 0;
  var totalMembers = getTotalMembers();
  
  if (totalMembers > 0) {
    bracket = Math.ceil(totalMembers/50);
  }
  
  
  
  return bracket;
}

function getMonthly() {
	
  var totalCost = 0;

  totalCost = (getBracket() * 10) + (getTotalArchives() * 5);
  
  if (totalCost < 20 && numRows > 0) {
    totalCost = 20;
  }
  
  return totalCost;
}

function getFees() {
	
 var customTotal = 0;

 for (counter = 0; counter < totalRows; counter++) {
   var attributes = rows["row-" + counter];

   if (attributes["active"]) {
     customTotal += attributes['setupFee'];
   }
 }

 return customTotal;
}

function render() {
	
  var attributes = rows["row-" + totalRows];

  var row = document.getElementById('tblList').insertRow(numRows+1);
  var listName = row.insertCell(0);
  var members = row.insertCell(1);
  var archive = row.insertCell(2);
  var setupFee = row.insertCell(3);
  var recurringFee = row.insertCell(4);
  var deleteRow = row.insertCell(5);

  listName.innerHTML = attributes['listName'];
  setupFee.innerHTML = "$" + attributes['setupFee'];
  setupFee.setAttribute('id', 'setupFee-' + totalRows);
  if (attributes['archive']) {
    archive.innerHTML = "$5";	  
  }
  else {
    archive.innerHTML = "$0";
  }

  members.innerHTML = attributes['members'];

  deleteRow.innerHTML = "<input type='image' src='/images/buttons/button_remlist_x.gif' value='Delete' onclick='deleteRow(this.parentNode.parentNode.rowIndex," + totalRows + ")' />";

  numRows++;
  totalRows++;

  printTotals();
}

function printTotals() {

  document.getElementById('lblCustomTotal').innerHTML = "One-time Setup Fee: $" + getFees();
  document.getElementById('lblGrandTotal').innerHTML = "Recurring Monthly Fee: $" + getMonthly();
}

function deleteRow(i, index) {
	
	var attributes = rows['row-' + index];
	
	attributes['active'] = false;
	
	if (!attributes['overage']) {
	  for (counter = 0; counter < totalRows; counter++) {	
	    attributes = rows['row-' + counter];
		  if (attributes['active'] && attributes['overage']) {
		    attributes['overage'] = false;
		    attributes['setupFee'] -= 5;
		    document.getElementById('setupFee-' + counter).innerHTML = '$' + attributes['setupFee'];
		    break;
		  }
	  }
	}  

  document.getElementById('tblList').deleteRow(i);

	numRows--;
  printTotals();
}

function addRow() {

	var fees = 0;
	var overage = false;
	
	if (numRows > 4) {
	  fees += 5;
	  overage = true;	
	}
			
<!--  if (document.getElementById("chkCustom").checked) {
<!--    fees += 50;
<!--  }

  var attributes = new Object;

  attributes['active'] = true;
  attributes['listName'] = totalRows + 1;
  attributes['members'] = parseInt(document.getElementById("subscribers").value);
  if (document.getElementById("chkArchive").checked)
  	attributes['archive'] = true;
	else
	  attributes['archive'] = false;

  attributes['setupFee'] = fees;
  attributes['overage'] = overage;

  rows["row-" + totalRows] = attributes;

	render();
}

function deleteLists() {

  var attributes;

  for (i = 0; i < totalRows; i++) {	
	attributes = rows["row-" + i];
	attributes["active"] = false;
  }

  for (i = 1; i <= numRows; i++) {
	document.getElementById('tblList').deleteRow(1);
  }
    
  numRows=0;
  printTotals();
}
