function rewriteURL(peopleId)
{
  var links = document.links;
  for (var i = 0; i < links.length; i++)
  {
    var link = links[i].href;
    if (link.indexOf("mailto:") < 0 && link.indexOf("javascript:") < 0 && link.indexOf("&homePeople=") < 0 && link.indexOf("homeController.php") < 0 && link.id.value != 'home')
      document.links[i].href = link + "&homePeople=" + peopleId;
  } 
}


function color(elem) {
  elem.style.background='#f1f1f1';
}


function uncolor(elem) {
  elem.style.background='#f9f9f9';
}
function addOption(theSel, theText, theValue)
{
  //alert(theText + ' ' + theValue);
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{
  var selLength = theSel.length;
  if(selLength>0)
    {
      theSel.options[theIndex] = null;
    }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
    {
      if(theSelFrom.options[i].selected)
        {
          selectedText[selectedCount] = theSelFrom.options[i].text;
          selectedValues[selectedCount] = theSelFrom.options[i].value;
          deleteOption(theSelFrom, i);
          selectedCount++;
        }
    }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
    {
      addOption(theSelTo, selectedText[i], selectedValues[i]);
    }
}

function selectAll(theSel)
{
  var selectVar = document.getElementById(theSel);
  for (j = 0; j < selectVar.length; j++)
  {
    selectVar.options[j].selected = true;
  }
}

function addInList(idText, idList)
{
  var text = document.getElementById(idText);
  var list = document.getElementById(idList);
  
  addOption(list, text.value, text.value);
  
  text.value="";
}

function delFromList(idList)
{
  var list = document.getElementById(idList);
  for(i=list.length-1; i>=0; i--)
    if(list.options[i].selected)
      list.options[i] = null;
}

function goTo(page)
{
    if (page != "" ) {
        document.location.href = page;
    }
    return false;
}

function autoFillUserName(form)
{
	var name;
	var surname;
	var username;

	name = form.name.value.toLowerCase();
	surname = form.surname.value.toLowerCase();

    surname = surname.replace(/[^a-zA-Z0-9]/g,"");

	if( surname == '' ) {
		return false;
	}

	username = name.substr( 0,1 ) + surname;
	form.username.value = username;
}

function showSupervisor(form) {
    if((form.role.value == 50))
    {
      form.supervisorId.disabled=false;
      document.getElementById('showSupervisorDiv').style.visibility = "visible";
    } else {
      form.supervisorId.disabled=true;
      document.getElementById('showSupervisorDiv').style.visibility = "hidden";
    }
}
function showDate(form) {
  /* 
    I ruoli a cui non bisogna inserire lo startdate e l'enddate sono:
        10 - Full Professore
        20 - Associate professor
        30 - Assistente professor

    I rouli a cui bisgona attivare sempre lo startdate e l'enddate sono:
        40 - Post Doc 
        50 - Ph.D
        60 - Administrative Assistant
        70 - Contract researcher
        80 - Assistant
        90 - System Administrator
        100 - Laurea students
        110 - Partners
  */

  if(form.role.value >= 40)
  {
    form.startDate.disabled=false;
    form.endDate.disabled=false;
    document.getElementById('showStartDateDiv').style.visibility = "visible";
    document.getElementById('showEndDateDiv').style.visibility = "visible";
    document.getElementById('showDate').style.visibility = "visible";
  } else {
    form.startDate.disabled=true;
    form.endDate.disabled=true;
    document.getElementById('showStartDateDiv').style.visibility = "hidden";
    document.getElementById('showEndDateDiv').style.visibility = "hidden";
    document.getElementById('showDate').display = "none";
  }
}

function showAcl(form)
{
  if (form.type.value=="1")
  {
    document.getElementById('acl').style.visibility = "hidden";
    document.getElementById('acl').display = "none";
  }
  else
  {
    document.getElementById('acl').style.visibility = "visible";
  }

}

function getNextYear(curYearId, nextYearId)
{
    if (!isNaN(curYearId.value)) {
        nextYearId.value = parseInt(curYearId.value, 10) + 1;
    }
}

function delTableRow(elem)
{
    var row=elem.parentNode.parentNode.rowIndex;
    elem.parentNode.parentNode.parentNode.deleteRow(row);
}

function addTableRow(tableId, cell0, cell1, cell2, cell3, cell4)
{
    if(cell0.value == '') {
      alert('Field ' + cell0.name + ' can\'t be empty. ');
    } else {
      var theTable = document.getElementById(tableId);
      var insertRow=theTable.insertRow(theTable.rows.length);
      insertRow.insertCell(0).innerHTML=cell0.value;
      insertRow.insertCell(1).innerHTML=cell1.value;
      insertRow.insertCell(2).innerHTML=cell2.value;

      if(cell3 != null && cell4 == null) {
        insertRow.insertCell(3).innerHTML=cell3.value;
        insertRow.insertCell(4).innerHTML="<input type=button value=\"Del\" name=\"delMaintenanceButton\" onClick=\"javascript:delTableRow(this)\">";
      } else if(cell3 != null && cell4 != null) {
        insertRow.insertCell(3).innerHTML=cell3.value;
        insertRow.insertCell(4).innerHTML=cell4.value;
        insertRow.insertCell(5).innerHTML='<input type=button value="Del" name="delMaintenanceButton" onClick="javascript:delTableRow(this);">';
      } else {
        insertRow.insertCell(3).innerHTML="<input type=button value=\"Del\" name=\"delMaintenanceButton\" onClick=\"javascript:delTableRow(this)\">";
      }
    }
}

function readTable(form, tableId, firstRow, cellsNumber)
{
    var theTable = document.getElementById(tableId);
    for(i=firstRow; i < theTable.rows.length; i++) {
        for(j=0; j < cellsNumber; j++) {
            var input=document.createElement('INPUT');
            input.name=tableId + "_" + j + "[]";
            input.type="hidden";
            input.value=theTable.rows[i].cells[j].innerHTML;
            form.appendChild(input);
        }
    }
}
