
/*
 * XMLComponents - Rotinas para validação de formulários
 * Componentes: Kimochi
 * http://www.webconsult.com.br
 *
 * Version: 1.0.2.0
 * Date:    2002/01/12
 * 
 * 2002/03/29 - Implementado recurso de ajuda, rotinas de fechar, aplicacao de CSS em tempo real
 * 2002/01/12 - Primeira versão
 */
 
// ** Rotina ExibeAjuda ***********************************************
// Abre uma janela pre-definida, passando os parametros necessarios
// para a ajuda.
// Utiliza-se de variaveis padronizadas:
// TipoJanela: pode ser normal de navegador, em um frame especifico, ou usando modeless
// URL: Qual URL deve chamar para chamar a Ajuda
// Parametro: QUal parametro deve ser passado para essa rotina
// Janela: Parametros da janela ou  no caso de Frame o objeto Frame desejado.

var Ajuda_TipoJanela = 'Normal';
var Ajuda_URL = 'Ajuda_Topicos';
var Ajuda_Parametro = 'Topico';
var Ajuda_Janela = 'status=false,Width=600px,dialogWidth=600px,Height=400px,dialogHeight=400px,top=200,left=200, resizable=yes,scrollbars=yes';
var defaultWindow = 'status:false;Width:400px;dialogWidth:500px;Height:300px;dialogHeight:400px;top:200;left:200';
var lockTimer = 0;

function ExibeAjuda(topico){
 	var aURL = Ajuda_URL + '?' + Ajuda_Parametro + '=' + topico;
 	if (Ajuda_TipoJanela == 'Normal') {
 		wAjuda = window.open(aURL, 'Ajuda', Ajuda_Janela);
 	} else if (Ajuda_TipoJanela == 'Frame') {
		wAjuda = Ajuda_Janela;
		wAjuda.location = aURL;
 	} else if (Ajuda_TipoJanela == 'Modal') {
 		wAjuda = showModelessDialog(aURL,'Ajuda',Ajuda_Janela);
 	}
}

      function currencyFormat(fld, milSep, decSep, e) {
                  var sep = 0;
                  var key = '';
                  var i = j = 0;
                  var len = len2 = 0;
                  var strCheck = '0123456789';
                  var aux = aux2 = '';
                  var whichCode = (window.Event) ? e.which : e.keyCode;
                  if (whichCode == 13) return true;  // Enter
                  key = String.fromCharCode(whichCode);  // Get key value from key code
                  if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
                  len = fld.value.length;
                  for(i = 0; i < len; i++)
                  if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	                aux = '';
                  for(; i < len; i++)
                  if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
                  aux += key;
                  len = aux.length;

                  if (len == 0) fld.value = '';
                  if (len == 1) fld.value = '0'+ decSep + '0' + aux;
                  if (len == 2) fld.value = '0'+ decSep + aux;
                  if (len > 2) {
                  aux2 = '';
                  for (j = 0, i = len - 3; i >= 0; i--) {
                  if (j == 3) {
                  aux2 += milSep;
                  j = 0;
                  }
                  aux2 += aux.charAt(i);
                  j++;
                  }
                  fld.value = '';
                  len2 = aux2.length;
                  for (i = len2 - 1; i >= 0; i--)
                  fld.value += aux2.charAt(i);
                  fld.value += decSep + aux.substr(len - 2, len);
                  }
                  return false;
      }


// ** Rotina ExibeEditorHTML ***********************************************
// Exibe uma janela com o Editor HTML avancado permitindo a entrada de 
// texto de forma rapida. É interessante notar que esse editor requer IE4.x ou
// superior

function ExibeEditorHTML(campo){
	var fCampo = campo;
	janela = window.showModalDialog('EditorHTML',fCampo.value,'dialogHeight:300px;dialogWidth:700px');
	if (janela) {
		fCampo.value = janela;
		if (document.all[fCampo.form.name + "." + fCampo.name]){
			document.all[fCampo.form.name + "." + fCampo.name].innerHTML = janela;
		}
	}
}

function menu_pI() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=menu_pI.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function menu_sIR() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function menu_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=menu_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function menu_sI() { //v3.0
  var i,j=0,x,a=menu_sI.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=menu_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function menu_sHL() { //v6.0
  var i,p,v,vi,obj,args=menu_sHL.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=menu_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; vi=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=vi;
	 if (document.all) {
	    var sl = document.all.tags('select');
		 for (o=0; o<sl.length; o++)
		 {
   	 	if (sl[o].style) { slo=sl[o].style; vi=(v=='show')?'hidden':(v=='hide')?'visible':v; }
			slo.visibility=vi;
		 }
	 }
  }
}

function getRealPos(i,which) {
	iPos = 0
	while (i!=null) {
	 	iPos += i["offset" + which]
		i = i.offsetParent
	}
	return iPos
}

function menu_ajustPos(sDest,itop,ileft) {
	if (document.all) {
		var i = event.srcElement
		dest = document.all[sDest]
		if (dest) {
			if (ileft) 
				dest.style.pixelLeft = ileft
			else
				dest.style.pixelLeft = getRealPos(i,"Left")
			if (itop)
				dest.style.pixelTop = itop
			else
			   	dest.style.pixelTop = getRealPos(i,"Top")+ i.offsetHeight
			dest.style.display = "block"
		}
	}
}

// ** Rotina showActiveBody ***********************************************
// Exibe/oculta o conteúdo de uma linha de grid Ativa
// Para ser definido como ativa eh preciso ter uma tag com ID="ActiveBody" e
// uma tag superior que sirva para definir o selecionado no momento

function showActiveBody(o){
   var ActiveBody = o.all['ActiveBody'];
   o.activeBody = 1;
   if (!ActiveBody.length) {
   	ActiveBody.style.display = 'block';
   } else {
  		ActiveBody.item(0).style.display = 'block';
   }
}

// ** Rotina hideActiveBody ***********************************************
// Exibe/oculta o conteúdo de uma linha de grid Ativa
// Para ser definido como ativa eh preciso ter uma tag com ID="ActiveBody" e
// uma tag superior que sirva para definir o selecionado no momento

function hideActiveBody(o){
   var ActiveBody = o.all['ActiveBody'];
   o.activeBody = 0;
   if (!ActiveBody.length) {
   	ActiveBody.style.display = 'none';
   } else {
  		ActiveBody.item(0).style.display = 'none';
   }
}

function defineActiveBody(o){
	if (o.activeBody == 1) {
	  hideActiveBody(o); 
	} else {
	  showActiveBody(o);
	}
}

function showhideActiveBody(o){
   var obj = event.srcElement;
   if (obj.id=='ClickBody') {
    	if ( o.activeBody == 1) {
	     obj.classDefault = obj.classOpened;
    	  hideActiveBody(o); 
    	} else {
	     obj.classOpened = obj.classDefault;
   	  obj.classDefault = obj.className;
    	  showActiveBody(o);
    	}
    	event.cancelBubble = true;
	}
}


function showhideOptionMenu(o){
	var Options = o.all['OptionsMenu'];
  	if ( o.optionMenu == 1) {
  	  hidemenu(Options); 
  	  o.optionMenu = 0;
  	} else {
	  if (document.body.activeMenu != null) {
	  		bodyhideOptionMenu();
	  }
  	  showmenu(Options, o); 
  	  o.optionMenu = 1;
  	  document.body.activeMenu = o;
  	  document.body.onclick = bodyhideOptionMenu;
  	}
  	event.cancelBubble = true;
}

function bodyhideOptionMenu(){
  var o = document.body.activeMenu;
  var Options = o.all['OptionsMenu'];
  hidemenu(Options); 
  o.optionMenu = 0;
}

function mousehideOptionMenu(o){
  var Options = o.all['OptionsMenu'];
  hidemenu(Options); 
  o.optionMenu = 0;
}

function mouseshowOptionMenu(o){
  var Options = o.all['OptionsMenu'];
  showmenu(Options,o); 
  o.optionMenu = 1;
}

function showmenu(menu,ref) {
  var rightside = 0;
  var bottomedge = 0;
  if (ref != null) {
	 rightside = getRealPos(ref,"Left")+ menu.offsetWidth;
    bottomedge = document.body.scrollHeight-getRealPos(ref,"Top")+ref.offsetHeight;
  } else {
    rightside = event.clientX + menu.offsetWidth;
    bottomedge = document.body.clientHeight-event.clientY;
  }
  if (rightside > document.body.clientWidth)
    menu.style.left = document.body.clientWidth - menu.offsetWidth;
  else
    menu.style.left = getRealPos(ref,"Left");

  if (bottomedge < menu.offsetHeight)
    menu.style.top = getRealPos(ref,"Top") +ref.offsetHeight - menu.offsetHeight;
  else
    menu.style.top = getRealPos(ref,"Top") +ref.offsetHeight ;
  menu.style.visibility = 'visible';
  return false;
}

function hidemenu(menu){
  menu.style.visibility = 'hidden';
}


function ShowHideObj(objname){
	var obj = document.getElementById(objname);
	if (obj.style.display == 'none') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
}

function floatWindow(URL, features,debug){
	if (debug==1){
		value = window.open(URL,"",features)
	} else {
	   value = window.showModalDialog(URL,"",features)
	}
	return value;
}


function fireAHref(url){
	location = url;
}

function abrejanela(mValor,mNome,mWidth,mHeight)
	{
	window.open (mValor,mNome,"marginwidth=0,marginheight=0,status=0,scrollbars=1,resizable=yes,width="+mWidth+",height="+mHeight+",left=0,top=0");
	}


function keyformatDate(obj){
	var key = event.keyCode;	
	if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		if (obj.value.length == 2){
			obj.value += '/';
		}
		if (obj.value.length == 5){
			obj.value += '/';
		}
	}
}

function keyformatPhone(obj){										
	var key = event.keyCode;	
	if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		if (obj.value.length == 0){
			obj.value = "(";
		}
		if (obj.value.length == 3){
			obj.value += ") ";
		}
		if (obj.value.length == 9){
			obj.value +="-";
		}
	}
}

function keyformatCep(obj){						
	var key = event.keyCode;	
	if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		if (obj.value.length == 5){
			obj.value +="-";
		}	
	}
}

function keyformatCpf(obj){						
	var key = event.keyCode;	
	if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		if (obj.value.length == 3){
			obj.value +=".";
		}
		if (obj.value.length == 7){
			obj.value +=".";
		}
		if (obj.value.length == 11){
			obj.value +="-";
		}	
	}
}

function keyformatCnpj(obj){						
	var key = event.keyCode;	
	if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		if (obj.value.length == 2){
			obj.value +=".";
		}
		if (obj.value.length == 6){
			obj.value +=".";
		}
		if (obj.value.length == 10){
			obj.value +="/";
		}
		if (obj.value.length == 15){
			obj.value +="-";
		}	
	}
}

function CallBackResourceHTML(Resource, Conteudo){
	var HTMLResource = parent.document.all[Resource];
	HTMLResource.innerHTML = Conteudo;
}


/* FUNÇÃO DATAEXTENSO
	Retorna a qualquer a data escrita da seguinte forma (Dia da Semana, Dia, Mes, Ano)
	Parametro
		-mData = '0' Retorna a data atual por extenso
				 = '20/01/2003'  Retorna a Data passada por extenso
*/
function DataExtenso(mData,Formato)
	{
	if (mData == '0')
		var agora = new Date()
	else	
		var agora = new Date(mData);
	var anoN = agora.getYear()
	var mesN = agora.getMonth() + 1;
	var diaN = agora.getDay() + 1;
	var diaNr = ((agora.getDate()<10) ? "0" : "")+ agora.getDate();
	if(diaN==1) dia = "Domingo";
	if(diaN==2) dia = "Segunda";
	if(diaN==3) dia = "Terça";
	if(diaN==4) dia = "Quarta";
	if(diaN==5) dia = "Quinta";
	if(diaN==6) dia = "Sexta";
	if(diaN==7) dia = "Sábado";
	if(mesN==1) mes="Janeiro";
	if(mesN==2) mes="Fevereiro";
	if(mesN==3) mes="Março";
	if(mesN==4) mes="Abril";
	if(mesN==5) mes="Maio";
	if(mesN==6) mes="Junho";
	if(mesN==7) mes="Julho";
	if(mesN==8) mes="Agosto";
	if(mesN==9) mes="Setembro";
	if(mesN==10) mes="Outubro";
	if(mesN==11) mes="Novembro";
	if(mesN==12) mes="Dezembro";
	var DiaHoje =(" "+dia+", "+diaNr+" de "+mes+" de "+anoN);
	return(DiaHoje);
	}
// ** Rotina Valor por extenso ***********************************************
// Escreve os valores em formato moeda(real) por extenso
// c: Parametro = valor a ser escrito


function valorporExtenso(c) 
 { 
 if(c == "" || c < 0 || c >=10000000) 
  { 
  return(-1); 
  } 
 else 
  { 
  if(c == 0) 
   { 
   return(" ZERO "); 
   } 
  else 
   { 
   aUnid = new Array(); 
   aDezena = new Array(); 
   aCentena = new Array(); 
   aGrupo = new Array(); 
   aTexto = new Array(); 
  
   aUnid[1] = "UM "; 
   aUnid[2] = "DOIS "; 
   aUnid[3] = "TRÊS "; 
   aUnid[4] = "QUATRO "; 
   aUnid[5] = "CINCO "; 
   aUnid[6] = "SEIS "; 
   aUnid[7] = "SETE "; 
   aUnid[8] = "OITO "; 
   aUnid[9] = "NOVE "; 
   aUnid[10] = "DEZ "; 
   aUnid[11] = "ONZE "; 
   aUnid[12] = "DOZE "; 
   aUnid[13] = "TREZE "; 
   aUnid[14] = "QUATORZE "; 
   aUnid[15] = "QUINZE "; 
   aUnid[16] = "DEZESSEIS "; 
   aUnid[17] = "DEZESETE "; 
   aUnid[18] = "DEZOITO "; 
   aUnid[19] = "DEZENOVE "; 

   aDezena[1] = "DEZ "; 
   aDezena[2] = "VINTE "; 
   aDezena[3] = "TRINTA "; 
   aDezena[4] = "QUARENTA "; 
   aDezena[5] = "CINQÜENTA "; 
   aDezena[6] = "SESSENTA "; 
   aDezena[7] = "SETENTA "; 
   aDezena[8] = "OITENTA "; 
   aDezena[9] = "NOVENTA "; 

   aCentena[1] = "CENTO "; 
   aCentena[2] = "DUZENTOS "; 
   aCentena[3] = "TREZENTOS "; 
   aCentena[4] = "QUATROCENTOS "; 
   aCentena[5] = "QUINHENTOS "; 
   aCentena[6] = "SEISCENTOS "; 
   aCentena[7] = "SETECENTOS "; 
   aCentena[8] = "OITOCENTOS "; 
   aCentena[9] = "NOVECENTOS "; 
   
   if(c.indexOf(".") != -1) 
    { 
    aGrupo[4] = c.substring(c.indexOf(".")+1,c.length); 
    aGrupo[4] = aGrupo[4].substring(0,2); 
    ct = c.substring(0,c.indexOf(".")); 
    } 
   else 
    { 
    if(c.indexOf(",") != -1) 
     { 
     aGrupo[4] = c.substring(c.indexOf(",")+1,c.length); 
     aGrupo[4] = aGrupo[4].substring(0,2); 
     ct = c.substring(0,c.indexOf(",")); 
     } 
    else 
     { 
     aGrupo[4] = "00"; 
     ct = c; 
     } 
    tt = ""; 
    for(f=0;f<(10-ct.length);f++) 
     { 
     tt += "0"; 
     } 
    tt += ct; 
    } 
   aGrupo[1] = tt.substring(1,4); 
   aGrupo[2] = tt.substring(4,7); 
   aGrupo[3] = tt.substring(7,10); 
   aGrupo[4] = "0"+aGrupo[4]; 

   for(f=1;f<5;f++) 
    { 
    cParte = aGrupo[f]; 
    if(parseFloat(cParte) < 10) 
     { 
     nTamanho = 1; 
     } 
    else 
     { 
     if(parseFloat(cParte) < 100) 
       { 
      nTamanho = 2; 
      } 
     else 
      { 
      if(parseFloat(cParte) < 1000) 
       { 
       nTamanho = 3; 
       } 
      else 
       { 
       nTamanho = 0; 
       } 
      } 
     } 
    aTexto[f] = ""; 
    if(nTamanho == 3) 
     { 
     if(cParte.substring(1,3) != "00") 
      { 
      aTexto[f] += aCentena[cParte.substring(0,1)] + "E "; 
      nTamanho = 2; 
      } 
     else 
      { 
      if(cParte.substring(0,1) == "1") 
       { 
       aTexto[f] += "CEM "; 
       } 
      else 
       { 
       aTexto[f] += aCentena[cParte.substring(0,1)]; 
       } 
      } 
     } 
    if(nTamanho == 2) 
     { 
     if(parseFloat(cParte.substring(1,3)) < 10) 
      { 
      aTexto[f] += aUnid[cParte.substring(2,3)]; 
      } 
     else 
      { 
      if(parseFloat(cParte.substring(1,3)) < 20) 
       { 
       aTexto[f] += aUnid[cParte.substring(1,3)]; 
       } 
      else 
       { 
       aTexto[f] += aDezena[cParte.substring(1,2)]; 
       if(cParte.substring(2,3) != "0") 
        { 
        aTexto[f] += "E "; 
        nTamanho = 1; 
        } 
       } 
      } 
     } 
    if(nTamanho == 1) 
     { 
     aTexto[f] += aUnid[cParte.substring(2,3)]; 
     } 
    } 
   if(parseFloat(aGrupo[1] + aGrupo[2] + aGrupo[3]) == 0 && parseFloat(aGrupo[4]) != 0) 
    { 
    cFinal = aTexto[4]; 
    if(parseFloat(aGrupo[4]) == 1) 
     { 
     cFinal +=  "CENTAVO"; 
     } 
    else 
     { 
     cFinal +=  "CENTAVOS"; 
     } 
    } 
   else 
    { 
    if(parseFloat(aGrupo[1]) != 0) 
     { 
     cFinal = aTexto[1]; 
     if(parseFloat(aGrupo[1]) > 1) 
      { 
      cFinal += "MILHÕES "; 
      } 
     else 
      { 
      cFinal += "MILHÃO "; 
      } 
     if(parseFloat(aGrupo[2] + aGrupo[3]) == 0) 
      { 
      cFinal += "DE "; 
      } 
     else 
      { 
      cFinal += "E "; 
      } 
     } 
    else 
     { 
     cFinal = ""; 
     } 
    if(parseFloat(aGrupo[2]) != 0) 
     { 
     cFinal += aTexto[2] + "MIL "; 
     if(parseFloat(aGrupo[3]) != 0) 
      { 
      cFinal += "E "; 
      } 
     } 
    if(parseFloat(aGrupo[3]) != 0) 
     { 
     cFinal += aTexto[3]; 
     } 
    if(parseFloat(aGrupo[1] + aGrupo[2] + aGrupo[3]) == 1) 
     { 
     cFinal += "REAL"; 
     } 
    else 
     { 
     cFinal += "REAIS"; 
     } 
    if(parseFloat(aGrupo[4]) != 0) 
     { 
     cFinal += " E "+aTexto[4]; 
     if(parseFloat(aGrupo[4])==1) 
      { 
      cFinal += "CENTAVO"; 
      } 
     else 
      { 
      cFinal += "CENTAVOS"; 
      } 
     } 
    } 
   return(cFinal); 
   } 
  } 
 } 

// ** Rotina confirma ***********************************************
// Abre um ModalDialog e retorna true ou false, funciona como um confirm() do javascript
// Recebe uma string que é o texto exibido na ModalDialog
// Nao e interessante utilizar esta funcao na internet, somente em intranets

function confirma(str, height, width){
	if(width == '' || height == '')
		var vModal = window.showModalDialog("Mensagem?Tipo=1", str, "dialogHeight:200px; dialogWidth:350px; center:yes; help:no; resizable:no; status:no;");
	else
		var vModal = window.showModalDialog("Mensagem?Tipo=1", str, "dialogHeight:" + height + "px; dialogWidth:" + width + "px; center:yes; help:no; resizable:no; status:no;");
	return vModal;	
}

// ** Rotina alerta ***********************************************
// Abre um ModalDialog e exibe uma mensagem, funciona como um alert() do javascript
// Recebe uma string que é o texto exibido na ModalDialog
// Nao e interessante utilizar esta funcao na internet, somente em intranets

function alerta(str, height, width){
	if(width == '' || height == '')
		var vModal = window.showModalDialog("Mensagem?Tipo=2", str, "dialogHeight:200px; dialogWidth:350px; center:yes; help:no; resizable:no; status:no;");
	else
		var vModal = window.showModalDialog("Mensagem?Tipo=2", str, "dialogHeight:" + height + "px; dialogWidth:" + width + "px; center:yes; help:no; resizable:no; status:no;");
	return vModal;	
}
