function cleanFields() 
{
	with (document.frm1) 
	{
		for (var i=0; i < elements.length; i++) 
		{
			if ((elements[i].type == 'text') || (elements[i].type == 'textarea') || (elements[i].type == 'select-one') || (elements[i].type == 'checkbox')) 
			{
				elements[i].value = '';
			}
		}
	}
}

function modifyField(obj) 
{
	var _pos = _find(obj.name,sel_itens);
	if(_pos == -1) 
	{
		if( !obj.disabled )
			sel_itens[sel_itens.length] = obj.name;
	}

	document.frm1.altered_vals.value  = sel_itens.sort();
}   

function goMenu()
{
	if(sel_itens.length > 0) 
	{
		if(confirm('Deseja abandonar as alterações realizadas?')) 
		{
			window.location = 'hb_menu.php'; 
		}
	} 
	else
	{ 
		window.location = 'hb_menu.php'; 
	}
}

function newAtend() 
{
	if(sel_itens.length > 0) 
	{
		if(confirm( translate('_DESEJAABANDONAR') )) 
		{
			cleanFields();
			document.frm1.action      = 'hb_novo_atendimento.php';
			document.frm1.submit();
		}
	} 
	else 
	{ 
		cleanFields();
		document.frm1.action      = 'hb_novo_atendimento.php';
		document.frm1.submit();
	}

}

function setEdit() 
{
	if(document.frm1.bnt_inc)
		document.frm1.bnt_inc.disabled = true;

	if(document.frm1.bnt_env)
		document.frm1.bnt_env.disabled = false;

	if(document.frm1.bnt_attach)
		document.frm1.bnt_attach.disabled = false;

	enableFields(false);
}

var num_atd;
function setAuxAtend(n_atd) 
{
	if(n_atd.value.length <= 0) 
	{
		n_atd.value = num_atd;
	}
}

function processaRetorno( v_result )
{
	var ok = false;
	
	if( v_result && v_result != '' && v_result.length < 5 ) 
		return false;

	var arr1 = _split(v_result,';');
	var arr2, aux;
	var i=0;

	for (i=0; i < arr1.length; i++) 
	{
		aux = arr1[i];
		arr2 = _split(aux,'|');
		
		if(arr2.length == 2 ) 
		{ 
			setaValor( arr2[0], arr2[1] ); 
			ok = true; 
		}
	}

	return ok;
}

function procuraObjeto( nam )
{	
	if(document.frm1)
	{
		with (document.frm1) 
		{
			for (var i=0; i < elements.length; i++) 
			{
				if( elements[i].name == nam )
					return elements[i];
			}
		}
	}
	return false;
}

function setaValor( campo, valor )
{
	if(campo.length > 0)
	{
		if( campo == 'cod_atent')
			t_user_code_object.value = valor;
		else if( campo == 'nom_atent')
			t_user_name_object.value = valor;
		else
		{
			var obj = procuraObjeto(campo);
			if(obj)
			{ 
				obj.value = valor;
				modifyField(obj);
			}
		}
	}
}

function limparCampos( form ) 
{

	with( form ) 
	{
		for (var i=0; i < elements.length; i++) 
		{
			if ((elements[i].type == 'text') || (elements[i].type == 'textarea') || (elements[i].type == 'select-one') || (elements[i].type == 'checkbox')) 
			{
				elements[i].value = '';
			}
		}
	}
}

function somenteNumeros()
{
	if ((event.keyCode<48) || (event.keyCode>57))
		event.keyCode = 0;
}

function openWindow(theURL,winName,features) 
{ 
	  var _W=window.open(theURL,'',features);
	  _W.focus();
      //_W.moveTo(50,50); 
} 

function openWindowNew(theURL,winName) 
{ 
	  var height = window.screen.height - 160;
	  var width = window.screen.width;
	  var h = 250; //largura
	  var w = 600; //altura
	  var top = 100;
	  var left = 100;
	  var top = (height- h)/2;
	  var left = (width - w)/2;
	  
	  var _W=window.open(theURL,'',"top=" + top + ",left="+ left +",width="+w+",height="+h+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,scrolling=yes,resizable=no");
	  _W.focus();
      //_W.moveTo(50,50); 
} 

function isEnterKey( e )
{
	var key;
	if(window.event) key = window.event.keyCode;
	else if(e) key = e.which;
	else return true;
	
	if( key == 13 )
	    return true;

    return false;	
}

var alteredColumns = new Array();
function updCols(names) 
{
	tmp = new Array();
	tmp = _split(names,':');

	for (k = 0; k < tmp.length; k++)
	{
		var _pos = _find(tmp[k], alteredColumns);
		if(_pos == -1) 
		{
			alteredColumns[alteredColumns.length] = tmp[k];
		}
	}
	document.frm1.updateColumns.value  = alteredColumns.sort();
}   

var objectReturn = '';
function abreDialogo( tipo, url, tp_lov )
{
	var opt = '';
	
	if( isMinIE4 )
	{
		//"dialogWidth:150px;dialogHeight:80px;help:no;scroll:yes;status:no;center:yes;"
		objectReturn = '';
		objectReturn = window.showModalDialog(url,[window],"dialogWidth:260px;dialogHeight:110px;help:no;scroll:no;status:yes;center:yes;");	
		retornaLov( tp_lov );
	}
	else
	{
		var h = screen.height;
		var w = screen.width;

		if( tipo == 'lov' )
		{
			h=(h/2)-125;
			w=(w/2)-210;	
			opt = 'left='+w+',top='+h+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,width=420,height=250;';
		}
		else
		{
			h=(h/2)-25;
			w=(w/2)-25;	
			opt = 'left='+w+',top='+h+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no,width=50,height=50';
		}
		
		openWindow(url,[window], opt);
		objectReturn = '';
		return true;
	}
}

function abreListaValores( w_lov , parm )
{
	if( parm == undefined ) parm = '';
	var _param     =  '';
	var _emp       =  ''; //incluido por william em 04/08/2004
	var _prod	   =  ''; //incluido por william em 04/08/2004
	var _dsc_prod  =  ''; //incluido por william em 04/08/2004
	var _PHPSESSID =  ''; //incluido por william em 04/08/2004

	if (document.frm1.PHPSESSID)
	{
		if (document.frm1.PHPSESSID.value!='')
		{
			_PHPSESSID = document.frm1.PHPSESSID.value;
		}
	}
	
	if (document.frm1.cod_emp)
	{
		if (document.frm1.cod_emp.value!='')
		{
			_emp = document.frm1.cod_emp.value;
		}
	}

	if (document.frm1.cod_produt)
	{
		if (document.frm1.cod_produt.value!='')
		{
			_prod = document.frm1.cod_produt.value;
		}
	}

	if (document.frm1.dsc_produt)
	{
		if (document.frm1.dsc_produt.value!='')
		{
			_dsc_prod = document.frm1.dsc_produt.value;
		}
	}
	
	if( document.frm1.bnt_env && document.frm1.bnt_env.disabled )
	{
		window.status = translate('_CPREADONLY'); 
		return false; 
	}

/* william
	if( w_lov == 'causa' ) 
	{
		_param = document.frm1.cod_grp_produt.value;
		if(_param.length <=0)
		{
			window.status = translate('_NOLOV');
			return false;
		} 
	}
*/
	
	if( isMinIE4 )
	{
		abreDialogo('lov','hb_lov_win.php?PHPSESSID=' + _PHPSESSID + '&hb_lov=' + w_lov + '&hb_find=' + parm + '&hb_parm='+_param + '&cod_emp='+_emp + '&cod_produt='+_prod + '&desc_produt='+_dsc_prod, w_lov); //alterado por william em 04/08/2004
	}
	else
	{
		abreDialogo('lov','hb_new_lov.php?PHPSESSID =' + _PHPSESSID + '&hb_lov=' + w_lov + '&hb_find=' + parm + '&hb_parm='+_param + '&cod_emp='+_emp + '&cod_produt='+_prod + '&desc_produt='+_dsc_prod, w_lov); //alterado por william em 04/08/2004
	}
	
	return true;
}

function retornaLov( tipo )
{
	if( objectReturn.length > 0 ) 
	{
		if( objectReturn.substr(0,7) == 'abreLov' )
		{
			var tmp = new Array();
			tmp = _split(objectReturn,'|');
			abreListaValores(tipo, tmp[1]);
			return false;
		}
		
/* william
		if( tipo == 'cliente' ) 
		{			
			if(objectReturn.length > 5)
				limpaDadosCliente();
		}
*/			
		
		if( processaRetorno( objectReturn ) )
		{			
			if( tipo == 'produto' )
			{
				modifyField(document.frm1.cod_priori);
			}
		}
	}

	// setar foco do campo
	if( tipo == 'produto' )
		setFocus('qtd_produt');

/* william
	if( tipo == 'cliente' )
		setFocus('dsc_categ');
*/
	
	if (tipo == 'cliente' && objectReturn!="naoExiste")
	{
		if (document.frm1.existe_cli)
		{
			document.frm1.existe_cli.value="S";
		}
	}

	if(tipo == 'categoria')
		setFocus('ind_fisico_jurid');

	if(tipo == 'causa')
		setFocus('num_calibr');

}


function setFocus( nome )
{
	obj = procuraObjeto(nome);

	if( obj )
		obj.focus();
}

function limpaListaValores() {
	document.frm1.num_cpf_cgc.value = "";
	document.frm1.nom_client.value  = "";
	document.frm1.cod_client.value  = "";
	document.frm1.submit();
}

function abre_detalhes(quem)
{
	var height = window.screen.height - 160;
	var width = window.screen.width;
	var www = quem +'.php?marca='+ document.frm1.marca.value;
	var h = 320; //altura
	var w = 480; //largura
	var top = 100;
	var left = 100;
	var top = (height- h)/2 + 130;
	var left = (width - w)/2 + 180;
	window.open(www,"nova","top=" + top + ",left="+ left +",width="+w+",height="+h+",toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,scrolling=yes,resizable=no")
}

function menu_visivel()
{
	if (document.frm1.flg_altera && document.frm1.flg_altera.value=='S')
		document.frm2.mostra_menu.value='N';
}

function abre_web(www,h,w)
{
	var height = window.screen.height - 160;
	var width = window.screen.width;
	
	var top = 100;
	var left = 100;
	var top = (height- h)/2;
	var left = (width - w)/2;
	window.open(www,"teste","top=" + top + ",left="+ left +",width="+w+",height="+h+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,scrolling=no,resizable=no")
}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
		}
	
	}
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				
				if (document.frm1.tipo_sel)
				{
					if (document.frm1.tipo_sel.value == "asc")
						to.options[index] = new Option( o.text + "(+)", o.value, false, false);
					else
						to.options[index] = new Option( o.text + "(-)", o.value, false, false);
					}
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}	
	
	function moveSelectedOptions(from,to,who) {
		if(arguments.length>3)
		{
			var regex = arguments[3];
			if(regex != "")
			{	
				unSelectMatchingOptions(from,regex);
			}
		}
		if(!hasOptions(from))
		{
			return;
		}
		for(var i=0;i<from.options.length;i++)
		{
			var o = from.options[i];if(o.selected)
			{
				if(!hasOptions(to))
				{
					var index = 0;
				}
				else
				{
					var index=to.options.length;
				}
				
				if (who=='para' && window.opener.document.frm1.para)
				{
					window.opener.document.frm1.para.value += o.value + '; ';
				}

				if (who=='cc' && window.opener.document.frm1.copia)
				{
					window.opener.document.frm1.copia.value += o.value + '; ';
				}

				if (who=='cco' && window.opener.document.frm1.copia_oculta)
				{
					window.opener.document.frm1.copia_oculta.value += o.value + '; ';
				}

				to.options[index] = new Option( o.text, o.value, false, false);

			}
		}
		for(var i=(from.options.length-1);i>=0;i--)
		{
			var o = from.options[i];
			if(o.selected)
			{
				from.options[i] = null;
			}
		}
		if((arguments.length<3) ||(arguments[2]==true))
		{
			/*sortSelect(from);sortSelect(to);*/
		}
		from.selectedIndex = -1;
		to.selectedIndex = -1;
	}

	function remover(from)
	{
		for (var i=(from.options.length-1); i>=0; i--) 
		{ 
			from.options[i] = null; 
		}
		from.selectedIndex = -1; 
	}

/************************************************************
Funções para manipulação do menu
************************************************************/

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new Browser();

var botaoAtivo = null;

var divFrame = null;

function clicaBotao(event, menuId) {

  var botao;

  if (browser.isIE)
    botao = window.event.srcElement;
  else
    botao = event.currentTarget;

  if (botao.menu == null) {
    botao.menu = document.getElementById(menuId);
  }
  
  if (botaoAtivo != null)
    fechaMenu(botaoAtivo);

  if (botao != botaoAtivo) {
    abreMenu(botao);
    botaoAtivo = botao;
  } else {
    botaoAtivo = null;
  }
  
  return false;

}

function abreMenu(botao) {

  botao.menu.style.zIndex = 4

  if (browser.isIE) {

    x = getPageOffsetLeft(botao.menu);
    y = getPageOffsetTop(botao.menu);
    
    w = botao.menu.offsetWidth;
    h = botao.menu.offsetHeight;
  
    frameM = document.getElementById("iFrameMenu");
    
    frameM.style.left = x + "px";
    frameM.style.top  = y + "px";
    frameM.style.width = w + "px";
    frameM.style.height  = h + "px";
    
    frameM.style.zIndex = (botao.menu.style.zIndex-1);
    
    frameM.style.visibility = "visible";
  }
  

  botao.className += " menuButtonActive";

  botao.menu.style.visibility = "visible";
}

function fechaMenu(botao) {

  botao.className = "menuButton";

  if (botao.menu != null) {

    if (browser.isIE) {

        frameM = document.getElementById("iFrameMenu");
  
        frameM.style.visibility = "hidden";
      }

    botao.menu.style.visibility = "hidden";
  }
}


function getPageOffsetLeft(el) {

  var x;

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

/************************************************************
FIM Funções para manipulação do menu
************************************************************/

/*
 * Classe para tratar eventos de KEYPRESS
 * --
 * Este componente deve ser chamado pelo evento keypress
 * como uma variavel dentro da função de tratamento do evento,
 * como no exemplo abaixo:
 * Ex:
 *   function teste(evt) { // Deve conter este evt, pois ele
 *                         // tem que ser passado via parametro
 *     var tclas = new fnTeclaPress(evt);
 *   }
 *
 *  Propriedades:
 *    - KeyCode : (int)  Retorna o codigo Ascii da tecla
 *    - KeyData : (char) Retorna a letra da tecla
 *    - Alt     : (bool) Retorna se a tecla Alt estava pressionada
 *    - Ctrl    : (bool) Retorna se a tecla Ctrl estava pressionada
 *    - Shift   : (bool) Retorna se a tecla Shift estava pressionada
 *    - Numerico: (bool) Retorna se a tecla digitada é um numero
 *    - Virgula : (bool) Retorna se a tecla digitada é uma virgula
 *    - Ponto   : (bool) Retorna se a tecla digitada é um ponto
 *    - Divisao : (bool) Retorna se a tecla digitada é uma barra ou divisao (/)
 *
 */
function fnTeclaPress(evt) {
	this.KeyCode = 0;

/*
	if (window.Event) {
		//this.KeyCode = evt.which;
		this.KeyCode = window.event.keyCode;
	} else {
		this.KeyCode = window.event.keyCode;
	}
*/

	if (evt.which)
		this.KeyCode = evt.which;
	else
		this.KeyCode = window.event.keyCode;

	
	this.KeyData = String.fromCharCode(this.KeyCode);

	// Separa Modificadores (Shift / Control / Alt)
	this.Alt = false;
	this.Ctrl = false;
	this.Shift = false;
	if (evt.modifiers) {
		this.Alt   = (evt.modifiers & Event.ALT_MASK     ? true : false);
		this.Ctrl  = (evt.modifiers & Event.CONTROL_MASK ? true : false);
		this.Shift = (evt.modifiers & Event.SHIFT_MASK   ? true : false);
	} else {
		this.Alt   =  evt.altKey;
		this.Ctrl  =  evt.ctrlKey;
		this.Shift =  evt.shiftKey;
	}

	//Verifica se caracter digitado é numero (8 = backspace, 9 = tab)
	if ((this.KeyCode >= 48 && this.KeyCode <= 57) || this.KeyCode == 8 || this.KeyCode == 0) {
		this.Numerico = true;
	} else {
		this.Numerico = false;
	}

	//Verifica se caracter digitado é virgula
	if (this.KeyCode == 44) {
		this.Virgula = true;
	} else {
		this.Virgula = false;
	}

	//Verifica se caracter digitado é ponto
	if (this.KeyCode == 46) {
		this.Ponto = true;
	} else {
		this.Ponto = false;
	}
	
	//Verifica se caracter digitado é barra (Divisao)
	if (this.KeyCode == 47) {
		this.Divisao = true;
	} else {
		this.Divisao = false;
	}

	//Verifica se caracter digitado é backspace
	if (this.KeyCode == 8) {
		this.Backspace = true;
	} else {
		this.Backspace = false;
	}

	//Verifica se caracter digitado é enter
	if (this.KeyCode == 13) {
		this.Enter = true;
	} else {
		this.Enter = false;
	}

	//Verifica se caracter digitado é tab
	if (this.KeyCode == 9) {
		this.Tab = true;
	} else {
		this.Tab = false;
	}

	//Verifica se caracter digitado é '+' ou '-' para descontos
	if ((this.KeyCode >= 48 && this.KeyCode <= 57) || this.KeyCode == 0 || this.KeyCode == 43 || this.KeyCode == 45 || this.KeyCode == 8 || this.Ponto || this.Virgula) {
		this.Desconto = true;
	} else {
		this.Desconto = false;
	}

	//Verifica se caracter digitado é numero decimal
	if ((this.KeyCode >= 48 && this.KeyCode <= 57) || this.KeyCode == 8 || this.KeyCode == 0 || this.KeyCode == 46) {
		this.Decimal = true;
	} else {
		this.Decimal = false;
	}
}

function onlyNumber(evt) 
{ 
	var tclas = new fnTeclaPress(evt);

	if (tclas.Numerico)
		return true;
	else
		return false;
}

function onlyDecimal(evt) 
{ 
	var tclas = new fnTeclaPress(evt);

	if (tclas.Decimal)
		return true;
	else
		return false;
}

function onlyEnter(evt)
{ 
	var tclas = new fnTeclaPress(evt);

	if (tclas.Enter)
		return true;
	else
		return false;
}

function onlyTab(evt)
{ 
	var tclas = new fnTeclaPress(evt);

	if (tclas.Tab)
		return true;
	else
		return false;
}