function toggleSection(checkbox, divID) {
	if (checkbox.checked)
	   document.getElementById(divID).className = "sectionShow";
	else
		document.getElementById(divID).className = "sectionHide";
}

// Remove a row from the specified <tbody> id
function removeRow(tbodyID, row)
{
  var tblBody = document.getElementById(tbodyID);
  var tblRows = tblBody.getElementsByTagName("tr");

  
  if(tblRows.length !=1) {
	 
	 var el = row;
	 while(el != null && el.tagName.toUpperCase() != 'TR') {
		 el = el.parentNode;
	 }
	 if (el != null)
        tblBody.removeChild(el);
  }
  else {
	  alert("Table must contain at least one row.");	  
  }
}


// Add method 1: Adds a row by cloning an existing row and appending to end of the <tbody> that is passed in
// Note -- does not work with radio boxes due to an issue with IE and cloneNode
function addRowClone(tbodyID)
{
  var tblBody = document.getElementById(tbodyID);
  var newNode = tblBody.rows[0].cloneNode(true);
  tblBody.appendChild(newNode);
  
  // Initialize cloned input boxes to blank
  var inputBoxes = newNode.getElementsByTagName("input");
  var selectBoxes = newNode.getElementsByTagName("select");
  for (var ii = 0; ii < inputBoxes.length; ii++) {
	 if (inputBoxes[ii].type=="checkbox") {
		inputBoxes[ii].checked = false;
	 }
	 else {
		inputBoxes[ii].value = "";
	 }
  }
  for (var jj = 0; jj < selectBoxes.length; jj++) {
     selectBoxes[jj].selectedIndex = 0;
  }  
  
}

// Add method 2: Add a row manually by creating all of the new row elements by hand and appending new row to the <tbody>
function addLicensor(tbodyID) {
	
	  // Create new <tr> and <td> elements to be added to the table
	  var tr = document.createElement('tr');
	  var td1 = document.createElement('td');
	  var td2 = document.createElement('td');
	  var td3 = document.createElement('td');

	  // Set the class name for the <TD> elements so that proper style is applied to each new cell
      td1.className = "c darker";
      td2.className = "c darker";
      td3.className = "c darker";
	  
      // Create the delete link
	  var deleteLink = document.createElement('a');
	  deleteLink.onclick = function() { removeRow(tbodyID, this); }
	  deleteLink.setAttribute('href', "javascript:void(0);");
	  deleteLink.appendChild(document.createTextNode("Delete"));
	  
	  // Create the <input> box and append to the first <TD> cell
	  td1.appendChild(inputBox("ownedProperty[]", ""));

	  // Create the <select> tag for "category"
	  var categorySelect = document.createElement('select');
	  var optionDefault = document.createElement('option');
	  categorySelect.name="licensorCategory[]";
	  optionDefault.appendChild(document.createTextNode("Select a Property Type"));
	  optionDefault.value="";
      categorySelect.appendChild(optionDefault);
	  
      // Append <select> to 2nd <TD> cell
      td2.appendChild(categorySelect);
	  
	  // Append delete link to last <TD> cell
	  td3.appendChild(deleteLink);

	  // Append <TD>'s to the new table row
	  tr.appendChild(td1);
	  tr.appendChild(td2);
	  tr.appendChild(td3);

	  // Append new table row to the <tbody> that was passed in by id
	  document.getElementById(tbodyID).appendChild(tr);

}


function inputBox(name, value, hidden) {
	  var inputBox = document.createElement("input");
	  inputBox.name = name;
	  inputBox.value = value;

	  if (hidden) {
	     inputBox.type="hidden";
	  }

	  return inputBox;
}

function checkboxesToHidden(formId, checkboxArrayName) {
	var cboxes = document.getElementsByName(checkboxArrayName);
	for (var ii = 0; ii < cboxes.length; ii++) {
       var cbox = document.createElement('input');
       cbox.type="hidden";
       if (cboxes[ii].checked)
           cbox.value = cboxes[ii].value;
       else cbox.value = "null";
       cbox.name = "_" + checkboxArrayName;
       document.getElementById(formId).appendChild(cbox);
	}
}

function exportTable(tableId, excelFileName)
{
  var htmlForm = document.createElement('form');
  htmlForm.method="POST";
  htmlForm.action="admin/export.php";
  htmlForm.id="exportForm";

  var hiddenInput = document.createElement('input');
  hiddenInput.type="hidden";
  hiddenInput.name="content";
  hiddenInput.value='<table>'+document.getElementById(tableId).innerHTML+'</table>';

  var hiddenInput2 = document.createElement('input');
  hiddenInput2.type="hidden";
  hiddenInput2.name="fname";
  hiddenInput2.value=excelFileName;
  
  htmlForm.appendChild(hiddenInput);
  htmlForm.appendChild(hiddenInput2);
  
  document.getElementsByTagName("body")[0].appendChild(htmlForm);
  
  document.getElementById("exportForm").submit();
}

function exportExcel()
{ 
   document.getElementById("exportParameters").submit();
}

function exportList(commaList, excelFileName)
{
  var htmlForm = document.createElement('form');
  htmlForm.method="POST";
  htmlForm.action="admin/export-comma-list.php";
  htmlForm.id="exportListForm";

  var hiddenInput = document.createElement('input');
  hiddenInput.type="hidden";
  hiddenInput.name="list";
  hiddenInput.value=commaList;

  var hiddenInput2 = document.createElement('input');
  hiddenInput2.type="hidden";
  hiddenInput2.name="fname";
  hiddenInput2.value=excelFileName;
  
  htmlForm.appendChild(hiddenInput);
  htmlForm.appendChild(hiddenInput2);
  
  document.getElementsByTagName("body")[0].appendChild(htmlForm);
  
  document.getElementById("exportListForm").submit();
}
