
var count1 = 0;
var count2 = 0;

function edit(type,id)
{
	window.open("/admin/editor.php?id=" + id + "&type=" + type,"editor","toobar=no, location=no, width=800, height=600,left=200,top=100");

}

function insertOptionBefore(num)
{
  var elSel = document.getElementById('lista');
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = 'Insert' + num;
    elOptNew.value = 'insert' + num;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function removeOptionSelected()
{
  var elSel = document.getElementById('lista');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function editOptionSelected()
{
  var elSel = document.getElementById('lista');
  var i;  
  var opSel;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
		opSel = elSel.options[i].value;
    }
  }
  if (opSel == null){
  	alert("Selecione um programa da lista");
	return;
  }
  id = opSel.substring(0,opSel.indexOf('='));
  titulo = 	opSel.substring(opSel.indexOf('=')+1);
  titulo = prompt("Confirme o título da programação",titulo);

  opSel = id + "=" + titulo;	
  
  window.open("/admin/editor.php?id=" + id + "&type=programacao","editor","toobar=no, location=no, width=840, height=600,left=200,top=100");
}

function editTitleOptionSelected()
{
	itemSelected = document.getElementById('lista');
	
	editTitle(itemSelected.options[itemSelected.selectedIndex]);

}

function editTitle(item)
{
  itemValue = item.value;

  id = itemValue.substring(0,itemValue.indexOf('='));
  titulo = 	itemValue.substring(itemValue.indexOf('=')+1);
  titulo = prompt("Título da programação",titulo);

  if (titulo != null){
  	item.value = id + "=" + titulo;
  	item.text = titulo;
  }

}

function appendOptionLast(num)
{
  var newEntry = document.getElementById('newEntry');  
  var eoptNew = document.createElement('option');  
  
  eoptNew.text = newEntry.value;
  eoptNew.value = nextId + "=" + newEntry.value;
  var elSel = document.getElementById('lista');

  try {
    elSel.add(eoptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(eoptNew); // IE only
  }
  newEntry.value = "";
  nextId++;
}

function removeOptionLast()
{
  var elSel = document.getElementById('lista');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}

function persistList()
{
  var elSel = document.getElementById('lista');
  for (i = elSel.length - 1; i>=0; i--) {
		opSel = elSel.options[i].selected = "true";
		elSel.options[i].value = escape(elSel.options[i].value);
  }
  document.programacao.submit();

}

function moveUp()
{
	var currList = document.getElementById('lista');	
	var curr = currList.selectedIndex;	
	
	if ( curr == 0 )
	{
		return;
	}
	
	auxText = currList.options[curr - 1].text;
	currList.options[curr - 1].text = currList.options[curr].text;
	currList.options[curr].text = auxText;

	auxText = currList.options[curr - 1].value;
	currList.options[curr - 1].value = currList.options[curr].value;
	currList.options[curr].value = auxText;
	
	currList.selectedIndex--;
}

function moveDown()
{
	var currList = document.getElementById('lista');	
	var curr = currList.selectedIndex;
	var qtt = currList.options.length;
	
	if ( curr >= qtt - 1 )
	{
		return;
	}

	
	auxText = currList.options[curr + 1].text;
	currList.options[curr + 1].text = currList.options[curr].text;
	currList.options[curr].text = auxText;

	auxText = currList.options[curr + 1].value;
	currList.options[curr + 1].value = currList.options[curr].value;
	currList.options[curr].value = auxText;
	
	currList.selectedIndex++;
}


