function OnLoadMain(pastas,userName,selectedTab,lang)
{
    var swf = SwfObject('pastas',
			'pastas.swf?pastas='+pastas+'&userName='+userName+'&pageId='+selectedTab,
			110,840,'rt');
    WriteLayer(swf,'pastas');

    switch(selectedTab)
    {
    case 'a0':
	swf = SwfObject('home', 'home_'+lang+'.swf',580,200,null,'#ffffff');
	WriteLayer(swf,'home');
	break;
    }
}

function OpenWin(path,name,winW,winH) 
{
    var iMyWidth;
    var iMyHeight;
    iMyWidth = (window.screen.width/2) - ((winW/2)+ 10); 
    iMyHeight = (window.screen.height/2) - ((winH/2) + 50); 
    var win2 = window.open(path,name,"status,toolbar=0,height="+winH+",width="+
			   winW+",resizable=yes,left=" + iMyWidth + ",top=" +
			   iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + 
			   iMyHeight +
			   ",scrollbars=yes,location=no,directories=no,status=no,menubar=no");
    win2.opener=window;
    win2.focus();
}

function ChangePassword()
{
    var entityId = document.getElementById('entityId').value;
    OpenWin('index.php?pageId=changePasswordForm&entityId='+entityId,
	    'changePasswordForm', 350,200);
}

function RemoveClient()
{
    if(confirm("Deseja realmente remover este cliente?"))
    {
	var docId = document.getElementById('entityId').value;
	var path = document.getElementById('path').value;
	var href = "index.php?pageId=delClient&docId=" + docId;
	href += "&path=" + path;
	urlLoader.location.href = href;
    }
}

function RemoveProcess()
{
    if(confirm("Deseja realmente remover este processo?"))
    {
	var docId = document.getElementById('processId').value;
	var href = "index.php?pageId=delProcess&docId=" + docId;
	urlLoader.location.href = href;
    }
}

function ShiftProcessClientAttrIndex(startIndex,shift)
{
    for(i=startIndex;i <= document.getElementById('numOfClients').value;i++)
    {
	var field = document.getElementById('processClients_'+i+'__entityId');
	field.setAttribute('id','processClients_'+(i+shift)+'__entityId');
	field.setAttribute('name','processClients_'+(i+shift)+'__entityId');
	field = document.getElementById('processClients_'+i+'__header');
	field.setAttribute('id','processClients_'+(i+shift)+'__header');
	field.setAttribute('name','processClients_'+(i+shift)+'__header');
	field = document.getElementById('processClients_'+i+'__excluded');
	field.setAttribute('id','processClients_'+(i+shift)+'__excluded');
	field.setAttribute('name','processClients_'+(i+shift)+'__excluded');
	field.setAttribute('onchange','OnChangeExcluded(this,'+(i+shift)+')');
	field = document.getElementById('processClients_'+i+'__reason');
	field.setAttribute('id','processClients_'+(i+shift)+'__reason');
	field.setAttribute('name','processClients_'+(i+shift)+'__reason');
    }
}

function RemoveProcessClient(entityId)
{
    if(confirm("Deseja realmente remover este cliente?"))
    {
	// recupera index da linha a ser removida
	var index=document.getElementById('processClients_'+entityId).rowIndex;

	// remove linha da tabela de clientes
	document.getElementById('processClients').deleteRow(index);

	// reajusta atributos das demais linhas
	ShiftProcessClientAttrIndex(index+1,-1);

	// reduz numOfClients
	document.getElementById('numOfClients').value -= 1;
    }
}

function InsertProcessClient(entityId, label)
{
    var processClients = document.getElementById('processClients');
    if(processClients)
    {
	if(document.getElementById('processClients_'+entityId))
	{
	    alert('Este cliente já está neste processo');
	    return false;
	}

	// reajusta atributos das demais linhas
	ShiftProcessClientAttrIndex(1,1);
	
	// incrementa numOfClients
	var numOfClients = document.getElementById('numOfClients').value;
	numOfClients = parseInt(numOfClients)+1;
	document.getElementById('numOfClients').value = numOfClients;

	var row = processClients.insertRow(1);
	row.setAttribute('id','processClients_'+entityId);
	var name = row.insertCell(0);
	var header = row.insertCell(1);
	var excluded = row.insertCell(2);
	var reason = row.insertCell(3);
	var removeBtn = row.insertCell(4);
	name.innerHTML="<input type='hidden' id='processClients_1__entityId' name='processClients_1__entityId' value='"+entityId+"' />"+label;
	header.innerHTML= "<input type='checkbox' id='processClients_1__header' name='processClients_1__header' value='1' />";
	excluded.innerHTML="<input type='checkbox' id='processClients_1__excluded' name='processClients_1__excluded' value='1' onchange='OnChangeExcluded(this,1)' />";
	reason.innerHTML="<input type='text' id='processClients_1__reason' name='processClients_1__reason' disabled='disabled' />";
	removeBtn.innerHTML="<img class='b' onclick='RemoveProcessClient("+entityId+")' alt='Remover' src='images/b-remover.gif' />";
    }
    return true;
}

function OnChangeExcluded(checkbox, index)
{
    var textField=document.getElementById('processClients_'+index+'__reason');
    if(checkbox.checked)
	textField.disabled = false;
    else
    {
	textField.value="";
	textField.disabled = true;
    }
}

function RemoveProcessNumber(index)
{
    if(confirm("Deseja realmente remover este número?"))
    {
	// remove linha da tabela de numeros
	document.getElementById('processNumbers').deleteRow(index);

	// reajusta atributos das demais linhas
	for(i=index+1;i<=document.getElementById('numOfNumbers').value;i++)
	{
	    var field = document.getElementById('processNumbers_'+i);
	    field.setAttribute('id','processNumbers_'+(i-1));
	    field = document.getElementById('processNumbers_'+i+'__number');
	    field.setAttribute('id','processNumbers_'+(i-1)+'__number');
	    field.setAttribute('name','processNumbers_'+(i-1)+'__number');
	    field = document.getElementById('processNumbers_'+i+'__removeBtn');
	    field.setAttribute('id','processNumbers_'+(i-1)+'__removeBtn');
	    field.setAttribute('onclick','RemoveProcessNumber('+(i-1)+')');
	}

	// reduz numOfNumbers
	document.getElementById('numOfNumbers').value -= 1;
    }
}

function InsertProcessNumber()
{
    var numberInput = document.getElementById('processNumbers_input');
    if(numberInput.value == "")
	return;
    var processNumbers = document.getElementById('processNumbers');
    if(!processNumbers)
	return;

    // verifica se numero ainda nao foi inserido
    for(i=1;i<=document.getElementById('numOfNumbers').value;i++)
	if(document.getElementById('processNumbers_'+i+'__number').value==
	   numberInput.value)
	{
	    alert('Este número já está neste processo');
	    return;
	}

    // incrementa numOfNumbers
    var numOfNumbers = document.getElementById('numOfNumbers').value;
    numOfNumbers = parseInt(numOfNumbers)+1;
    var row = processNumbers.insertRow(numOfNumbers);
    document.getElementById('numOfNumbers').value = numOfNumbers;
    
    row.setAttribute('id','processNumbers_'+numOfNumbers);
    var number = row.insertCell(0);
    number.innerHTML="<input type='hidden' id='processNumbers_"+numOfNumbers+"__number' name='processNumbers_"+numOfNumbers+"__number' />"+numberInput.value;
    document.getElementById('processNumbers_'+numOfNumbers+'__number').value = 
	numberInput.value;
    var removeBtn = row.insertCell(1);
    removeBtn.innerHTML="<img class='b' id='processNumbers_"+numOfNumbers+"__removeBtn' onclick='RemoveProcessNumber("+numOfNumbers+")' alt='Remover' src='images/b-remover.gif' />";
    numberInput.value = "";
}

function DisableLocals()
{
    for(var i=1; i <= document.getElementById('numOfLocal').value; i++)
	document.getElementById("processLocal_"+i+"__localId").disabled=true;
}

function EnableLocals()
{
    for(var i=1; i <= document.getElementById('numOfLocal').value; i++)
	document.getElementById("processLocal_"+i+"__localId").disabled=false;
}

function OnChangeLocalCheckbox(field, index)
{
    if(field.checked)
    {
	// carrega campo somente se não foi carregado anteriormente
	if(document.getElementById('processLocal_'+index+'__needLoad').value
	   =='1')
	{
	    DisableLocals();
	    var loc="index.php?pageId="+loadFieldPageId;
	    loc += "&fieldName=processLocal_"+index;
	    urlLoader.location = loc;
	}
	else
	    document.getElementById('processLocal_'+index).style.display=
		'block';
    }
    else
    {
	document.getElementById('processLocal_'+index).style.display='none';
    }
}

function selectUpdate(selectAux)
{
  eval(selectAux);  
}


function returnValueToParent(label,id)
{
    var sMult = document.processForm.processClientOptions;
    var opt = new Option(label, id, false, false);
    var tam =  sMult.length;

    if(tam==0)
    {
      sMult.options[tam] = opt;
    }
    else
    {
      for(i=0; i < tam; i++)
      {
        if(id == sMult.options[i].value)
        {
          alert('ERRO: Esse cliente ja foi inserido');
	  return;
	}
      } 
      sMult.options[tam] = opt; 
    }
}

function removeItem(val, sMult)
{
  var tam =  sMult.length;
  if(tam==0)
  {
    alert('ERRO: Não há dados para serem removidos');
  }
  else
  {
    for(i=0; i < tam; i++)
    {
      if(val == sMult.options[i].value)
      {
        for (j=i; j < tam-1; j++)
          sMult.options[j] = sMult.options[j+1];  
	  
        sMult.length = tam-1;
      }
    } 
  }
}

auxLocal = 0;

function changeAction(form)
{
  var j = 0;
  var newAction = "";

  for (var i=1; i <= form.auxLocal2.value ; i++)
  {
    if (eval("form.ch"+i+".checked"))
    {
      j++;
      if(eval("form.cityFlag_"+i+".value") == '1')
      {
        localList = eval("form.localId_"+i+".value");
        stateList = eval("form.stateId_"+i+".value");
	cityList = eval("form.cityId_"+i+".value");
	roomList = eval("form.roomId_"+i+".value");
	if(i==1)
	  newAction = newAction+"local_"+j+"__localId="+localList+"&local_"+j+"__stateId="+stateList+"&local_"+j+"__cityId="+cityList+"&local_"+j+"__room="+roomList;
	else
	  newAction = newAction+"&local_"+j+"__localId="+localList+"&local_"+j+"__stateId="+stateList+"&local_"+j+"__cityId="+cityList+"&local_"+j+"__room="+roomList;
      }
      else
      {
        localList = eval("form.localId_"+i+".value");
        roomList = eval("form.roomId_"+i+".value");
	if(i==1)
	  newAction = newAction+"local_"+j+"__localId="+localList+"&local_"+j+"__room="+roomList;
	else
	  newAction = newAction+"&local_"+j+"__localId="+localList+"&local_"+j+"__room="+roomList;
      }
    }
  }
  newAction = newAction + "&numOfLocal=" + j;
  form.action = "index.php?" +  newAction;
}

function CheckForm(form)
{
    for(var i=0; i<form.length; i++)
    {
	var field = form.elements[i];
	var fieldLabel;
	if((field.tagName=='INPUT' && 
	    (field.type=='text' || field.type=='password') || 
	    field.tagName=='TEXTAREA' || field.tagName=="SELECT") &&
	   (fieldLabel = document.getElementById(field.name+'Label')) &&
	   fieldLabel.className=="required" && 
	   field.value.replace(/^\s+|\s+$/g,"")=="")
	{
 	    fieldLabel = fieldLabel.firstChild;
 	    if(fieldLabel) fieldLabel = fieldLabel.nodeValue;
 	    alert('Campo requirido ('+ fieldLabel+')');
 	    field.focus();
 	    return false;
 	}
    }

    return true;
}

