var conAdd = 'Add';
var conRemove = 'Remove';
var delimiter = "|";
var objBrow;

//CONSTANTES UTILIZADAS
SIZE_DATE = 10;
SIZE_CEP = 8
SIZE_ACCOUNT = 7;
SIZE_AG_ACCOUNT = 11;
MAX_NUMERIC = Number.MAX_VALUE; 
SIZE_MONEY = 10; 
SIZE_FLOAT = 10; 
SIZE_CPF =11;
SIZE_CNPJ=14;
SIZE_PERCENT =6;
SIZE_BRANCH =4;
SIZE_MONTH_YEAR= 6;
SIZE_TIME= 5;
SIZE_PLACA= 7;
MAX_VALUE= 999999999.99

function MoveOption(f, From, To, action) {
	var eFrom = eval('f.' + From);
    var eTo = eval('f.' + To);
    var selection = eFrom.options.selectedIndex;
    var eAddContainer;
    var eRemoveContainer;


    if (action == conAdd) {
		eAddContainer = eval('f.htx' + conAdd + To);
        eRemoveContainer = eval('f.htx' + conRemove + To);
	}

    if (action == conRemove) {
    	eAddContainer = eval('f.htx' + conRemove + From);
    	eRemoveContainer = eval('f.htx' + conAdd + From);
    }



	if (selection == -1) {
		alert("Por favor, selecione uma ou mais opções.");
	} else {
		eval('f.cmd' + action + '.disabled = true;');

		for (i = 0; i < eFrom.options.length; i++) {
			if(eFrom.options[i].selected) {
				var name = eFrom.options[i].text;
                var ID = eFrom.options[i].value;
                eFrom.options[i] = null;
                eTo.options[eTo.options.length] = new Option (name,ID);
                i = i - 1;
                
                var rep = new String('\\' + delimiter + ID + '\\' + delimiter + '');
                var repExp = new RegExp(rep);

                if (eRemoveContainer.value.match(repExp) == null) {
					eAddContainer.value = eAddContainer.value + ID + delimiter;
				} else {
					eRemoveContainer.value = eRemoveContainer.value.replace(repExp,delimiter);
				}
			}
		}
	}
}


function onChange_msbUserPool(f) {
	f.cmdAdd.disabled = (f.cboLojas.selectedIndex == -1);
}


function onChange_msbAssignedUser(f) {
	f.cmdRemove.disabled = (f.cboLojasSEL.selectedIndex == -1);
}



function go(strURL){
	document.forms[0].method = 'post';
	document.forms[0].action = strURL;
	document.forms[0].submit();
}

function BuscaLojasDep( intDEP, objcmbLojas ) {	

	window.status = 'Aguarde...';
	
	if( intDEP == '' ) {
		objcmbLojas.length = 0;		
	}
	else
	{	
		var objDOMDoc		= xmlSource; // objeto DOMDocument
		var objDOMNodeLst	= null;		 // objeto DOM para os nós
		var objDOMNodeLoja	= null;		 // objeto DOM para cada item do nó das Lojas
		var strUrl          = "";		 // página utilizada para carregar o XML	
		var objTagErro		= "";		 // tag de retorno de erro do xml
		var intQteRegs		= 0			 // para contar os registros inseridos na combo
		var strNomeLojas	= ""		 // Nome da loja
		var intCodLojas		= ""		 // Código da loja

		strUrl = "./ListaLojas.asp";
		strUrl += "?DP=" + intDEP;

		var intNo = 0;
		
	    // limpa itens anteriores
		objcmbLojas.length = 0;		

		// Busca dados
		objDOMDoc.async = false;
		objDOMDoc.src = strUrl;
		
		objDOMNodeLst = objDOMDoc.selectNodes("//lojas/loja");

		while (intNo < objDOMNodeLst.length) {
			objDOMNodeLoja = objDOMNodeLst.item(intNo);
					
			intCodLoja  = objDOMNodeLoja.selectSingleNode("cd_loja").text;
					
			strNomeLoja  = objDOMNodeLoja.selectSingleNode("nm_loja").text;
				
								
			//Monta a combo Data Vencimento
			objcmbLojas.length = intQteRegs + 1;		
			objcmbLojas.options[intQteRegs] = new Option;
			objcmbLojas.options[intQteRegs].text = strNomeLoja;
			objcmbLojas.options[intQteRegs].value = intCodLoja;			
			intQteRegs++;	
			
			intNo++;
		}
	
	}
	window.status = '';
	
	return true;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function saltaCampo( evento,field,tipo,tamanho)
{
	var tecla;
	var maxSize;
	var fieldSize;
	if( isNNType(tipo,"float")) tipo= "float";
	if( isNNType(tipo,"money")) tipo= "money";

	switch(tipo)
	{
		case "date":		maxSize=SIZE_DATE;	break;
		case "time":		maxSize=SIZE_TIME;	break;
		case "cpf": 		maxSize=SIZE_CPF;		break;
		case "cnpj": 		maxSize=SIZE_CNPJ;	break;
		case "cpf_cnpj": 	maxSize=SIZE_CNPJ; 	break;
		case "cep": 		maxSize=SIZE_CEP; 	break;
		case "account": 	maxSize=SIZE_ACCOUNT; break;
		case "branch": 	maxSize=SIZE_BRANCH; break;
		case "percent": 	maxSize=SIZE_PERCENT; break;
		case "ag_account": 
		case "ag_savings": 
			maxSize=SIZE_AG_ACCOUNT; break;
		case "month_year":	maxSize=SIZE_MONTH_YEAR; break;
		case "float":
			if( saltaCampo.arguments.length > 3)  // Tamanho foi informado?
				maxSize= tamanho;
			else
				maxSize= SIZE_FLOAT;
			break;
		case "money":
			if( !verifyMaxValue(field.value))
			{ 
				if( saltaCampo.arguments.length > 3)  // Tamanho foi informado?
					maxSize= tamanho;
				else
					maxSize= SIZE_MONEY;
			}else{
				field.value= "0,00";
			}
			break;
		case "text":
		case "uppercase":
		case "interval":
		case "alfanumeric":
		case "default":
		case "numeric":
		case "email":
			maxSize= tamanho;
	}
	(getObjBrow().isNetscape())?tecla = evento.which:tecla = evento.keyCode;
	fieldSize=String(field.value).length;
	if ( fieldSize >= maxSize && tecla >= 48 )
	{
		autoSkip(field);
		return true;
	}else return false;
}

function focusCamp(campo)
{
	if( getObjBrow().isNetscape())
		campo.value= "";
	else
		campo.focus();
	ERRO= true;
}

function getObjBrow() 
{
	if( typeof objBrow != "object")
		objBrow= new Browser();
	return objBrow;
}

function autoSkip(field, orientation)
{
	var indCampo= -1;
	var form= field.form;
	// Obtendo posicao do campo no formulario
	for(i=0; i < form.elements.length; i++)
	{
		if (field == form.elements[i] ) 
		{
			indCampo= i; break;
		}
	}
	focusCampByPos(form, indCampo, orientation);
}

function DataHoje()
{
	var objData = new Date();
	var strDia = (objData.getDate()).toString();
	var strMes = (objData.getMonth() + 1).toString();
	var strAno = (objData.getFullYear()).toString();
	if (strDia.length == 1)
	{
		strDia = '0' + strDia;
	}
	if (strMes.length == 1)
	{
		strMes = '0' + strMes;
	}
	return strDia + '/' + strMes + '/' + strAno;
}

function TrocaCaracteresTelefone(strValor)
{
	var strAux;
										
	strAux = strValor;
									
	if (strValor == '')
	{
		strAux = '';
	}
	else
	{
		strAux = TrocaCaracteres(strAux);
		strAux = ReplaceString(strAux, '0xx', '');
		strAux = ReplaceString(strAux, '-', '');
	}
	return strAux;
}

function autoFocus(form)
{
	var formulario= arguments.length==0?document.forms[0]:form;
	focusCampByPos(formulario, -1);
}

function focusCampByPos(form, indCampo, orientation)
{	
	var orient=  (orientation?orientation:"down");
	var indNext= (orient == "down"?1:-1);

	// Caso seja o ultimo componente nao foca em ninguem.
   if( ( typeof form.elements[indCampo+indNext]) == "undefined")
   {
		if( indCampo!= -1) form.elements[indCampo].blur();
      return;
   }
	//Procurando proximo campo para setar foco.
   for(i=indCampo+indNext; i < form.elements.length; i=i+indNext)
   {
      tipo= form.elements[i].type;
      if((tipo=='text' || tipo=='password' || tipo=='select-one' || tipo=='textarea' || tipo=='checkbox' || tipo=='radio') && !form.elements[i].disabled)
      {
         form.elements[i].focus();
			return;
      }
   }
	// Caso nao consiga focar em ninguem, sai do campo...
	form.elements[indCampo].blur();
}

function isNNType(fieldType,type)
{
	return (fieldType.indexOf(type) == 0);
}

function TrocaCaracteresCep(strValor)
{
	var strAux;
										
	strAux = strValor;
									
	if (strValor == '')
	{
		strAux = '';
	}
	else
	{
		strAux = TrocaCaracteres(strAux);
		strAux = ReplaceString(strAux, '-', '');
	}
	return strAux;
}

function ReplaceString(str, strf, strs)
{
	var strr = str;
	while(strr.indexOf(strf) != -1) strr = strr.replace(strf, strs);
	return strr;
}

function TrocaCaracteres(strValor)
{
	var strAux;
										
	strAux = strValor;
									
	if (strValor == '')
	{
		strAux = '';
	}
	else
	{
		//strAux = ReplaceString(strAux, "'", '');
		strAux = ReplaceString(strAux, "'", '');
		strAux = ReplaceString(strAux, '"', '');
		strAux = ReplaceString(strAux, '\\', '');
		strAux = ReplaceString(strAux, ',', '');
		strAux = ReplaceString(strAux, '!', '');
		strAux = ReplaceString(strAux, '#', '');
		strAux = ReplaceString(strAux, '$', '');
		strAux = ReplaceString(strAux, '¨', '');
		strAux = ReplaceString(strAux, '&', '');
		strAux = ReplaceString(strAux, '*', '');
		strAux = ReplaceString(strAux, '(', '');
		strAux = ReplaceString(strAux, ')', '');
		strAux = ReplaceString(strAux, '[', '');
		strAux = ReplaceString(strAux, ']', '');
		strAux = ReplaceString(strAux, '{', '');
		strAux = ReplaceString(strAux, '}', '');
		strAux = ReplaceString(strAux, '~', '');
		strAux = ReplaceString(strAux, '^', '');
		strAux = ReplaceString(strAux, '?', '');
		strAux = ReplaceString(strAux, ';', '');
		strAux = ReplaceString(strAux, '|', '');
		strAux = ReplaceString(strAux, '=', '');
		strAux = ReplaceString(strAux, '+', '');
		strAux = ReplaceString(strAux, '%', '');
		strAux = ReplaceString(strAux, '`', '');
		strAux = ReplaceString(strAux, '´', '');
		strAux = ReplaceString(strAux, ':', '');
		strAux = ReplaceString(strAux, '/', '');
	}
	return strAux;
}

function ConverteData(strData)
{
	strData = strData.substr(6,4) + '-' + strData.substr(3,2) + '-' + strData.substr(0,2);
	return strData;
}

function ConverteMoney(strValor)
{
	strValor = (strValor.replace('.','')).replace(',','.');
	return strValor;
}

function fncAbreDialogo(oUrl, oWidth, oHeight, oArgument)
{
var oRetorno

	return window.showModalDialog(oUrl,oArgument,"dialogHeight: " + oHeight + "; dialogWidth: " + oWidth + "; center: Yes; resizable: No; scroll: No; status: No;");
}

function fncAbreJanela(oUrl, oWidth, oHeight)
{
	//Variáveis para centralizar a tela
	
	var oTop = window.screen.availHeight
	var oLeft = window.screen.availwidth

	oTop  = (oTop - oHeight) / 2
	oLeft = (oLeft - oWidth) / 2
	
	return window.open(oUrl,"NetCallCenter","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=" + oTop +" ,left=" + oLeft + ",width="+ oWidth +",height=" + oHeight);	
	
}

function fncAbreDialogo(oUrl, oWidth, oHeight, oArgument)
{
	return window.showModalDialog(oUrl,oArgument,"dialogHeight: " + oHeight + "; dialogWidth: " + oWidth + "; center: Yes; resizable: No; scroll: Auto; status: No;");
}

function TrocaCaracteres(strValor)
{
	var strAux;
										
	strAux = strValor;
									
	if (strValor == '')
	{
		strAux = '';
	}
	else
	{
		//strAux = ReplaceString(strAux, "'", '');
		strAux = ReplaceString(strAux, "'", '');
		strAux = ReplaceString(strAux, '"', '');
		strAux = ReplaceString(strAux, '\\', '');
		strAux = ReplaceString(strAux, ',', '');
		strAux = ReplaceString(strAux, '!', '');
		strAux = ReplaceString(strAux, '#', '');
		strAux = ReplaceString(strAux, '$', '');
		strAux = ReplaceString(strAux, '¨', '');
		strAux = ReplaceString(strAux, '&', '');
		strAux = ReplaceString(strAux, '*', '');
		strAux = ReplaceString(strAux, '(', '');
		strAux = ReplaceString(strAux, ')', '');
		strAux = ReplaceString(strAux, '[', '');
		strAux = ReplaceString(strAux, ']', '');
		strAux = ReplaceString(strAux, '{', '');
		strAux = ReplaceString(strAux, '}', '');
		strAux = ReplaceString(strAux, '~', '');
		strAux = ReplaceString(strAux, '^', '');
		strAux = ReplaceString(strAux, '?', '');
		strAux = ReplaceString(strAux, ';', '');
		strAux = ReplaceString(strAux, '|', '');
		strAux = ReplaceString(strAux, '=', '');
		strAux = ReplaceString(strAux, '+', '');
		strAux = ReplaceString(strAux, '%', '');
		strAux = ReplaceString(strAux, '`', '');
		strAux = ReplaceString(strAux, '´', '');
		strAux = ReplaceString(strAux, ':', '');
		strAux = ReplaceString(strAux, '/', '');
	}
	return strAux;
}