function iecompattest(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

/*******************************************************************************************
INCLUI JAVASCRIPT DINAMICAMENTE
*******************************************************************************************/

function include_jsfile(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0) ;
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
	html_doc.appendChild(js);
	//return false;
}

/*******************************************************************************************
ROLLOVER MENU
*******************************************************************************************/

function MM_swapImgRestore() { //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 MM_preloadImages() { //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=MM_preloadImages.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 MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*******************************************************************************************
CENTRALIZA A JANELA
*******************************************************************************************/

function CentralizaJanela() {

	var ie5=document.all&&document.getElementById
	var ns6=document.getElementById&&!document.all
	
	windowWidth = ns6? window.innerWidth : iecompattest().clientWidth;
	windowHeight = ns6? window.innerHeight : iecompattest().clientHeight;
	screenWidth = screen.width;
	screenHeight = screen.height;
	
	winY = Math.ceil((screenHeight - windowHeight) / 2);
	winX = Math.ceil((screenWidth - windowWidth) / 2);
	
	window.moveTo(winX,winY);
	
}

/*******************************************************************************************
ABRE JANELA POPUP (BROWSER)
*******************************************************************************************/

function MM_openBrWindow(theURL,winName,features) { //v2.0
  
  window.open(theURL,winName,features);
  
}

function MM_closeBrWindow() { //v2.0
  
	window.opener = self;
	self.close();
  
}

/*******************************************************************************************
HABILITA / DESABILITA O BOTÃO SEND DO FORMULÁRIO
*******************************************************************************************/

function transformaBotaoForm(texto,botao,acao) {
	
	if (acao == "desabilitar") {		
		document.getElementById(botao).disabled	= true;		
	} else if (acao == "habilitar") {		
		document.getElementById(botao).disabled	= false;		
	}
	
	document.getElementById(botao).value = texto;
	
}

/*******************************************************************************************
MONTA A RESPOSTA DE UMA VALIDAÇÃO DE FORM A PARTIR DE UM ARRAY
*******************************************************************************************/
function montaRespostaErro(arrError) {
	
	texto = "";
	marcador = 1;
	
	if (arrError.length > 0) {
		
		for (indice=0; indice<arrError.length; indice++) {			
			texto += marcador + ') ' + arrError[indice] + '\n';
			marcador++;
		}
		
	}
	
	return texto;
	
}

/*******************************************************************************************
LIMPA O CAMPO TEXTO (\n)
*******************************************************************************************/

function retiraQuebraLinha(obj) {	
	
	// recebe um espaço no final
	obj.value			+= " ";
	
	// variáveis do texto
	var textoNovo	= "";
	var texto 			= obj.value.split("\n");
	
	// monta novamente a string
	for (i=0; i<texto.length; i++) {
		
		if (texto[i] != "" && texto != "\n") {
			textoNovo += texto[i].substr(0,texto[i].length-1);	
		}
		
	}
	
	// insere o novo texto
	obj.value = textoNovo;
	
}

/*******************************************************************************************
PEGA UMA STRING E EXECUTA COMO CÓDIGO
*******************************************************************************************/

function executaCodigo(codigo) {
	eval(codigo);
}

/*******************************************************************************************
VALIDAÇÃO DE DATA
*******************************************************************************************/

function checkDate(v) {
	 if (v.length!=10) return false
	 var date=v.split('/')
	 var d=parseInt(date[0],10)
	 var m=parseInt(date[1],10)
	 var y=parseInt(date[2],10)
	 if(!checkDateDay(d))return false
	 else if(!checkDateMonth(m))return false
	 else if(!checkDateYear(y))return false
	 else if( (m==4||m==6||m==9||m==11) && (d==31)) return false
	 else if(m==2 && (d>29 || (d==29&&((y%4)!=0)))) return false
	 return true
}

function checkDateDay(v){if(v>=1&&v<=31)return true}
function checkDateMonth(v){if(v>=1&&v<=12)return true}
function checkDateYear(v){if(v>=1)return true}

/*******************************************************************************************
PULAR CAMPOS
*******************************************************************************************/

function jumpField() {
	
	if(window.event.srcElement.type == 'text') { 
		
		var ele = window.event.srcElement;
		var index = ele.sourceIndex;
		
		if(ele.value.length == ele.getAttribute('maxlength')){
        	document.all.item(index + 1).focus();  
    	}
		
	} 
	
}
   
document.onkeyup = jumpField;

/***********************************************************************************/
/* pré formatação de campo telefone */
/***********************************************************************************/

function formatTelefone(e1) {
   if ( e1.value != '' ) {
      if ( (e1.value.length < 9) && (e1.value.length > 0) ) {
         alert('Você deve preencher o telefone completo (com DDD)');
         e1.focus();
      } else {
         var ddd = e1.value.substring(0,2);
         var fone = e1.value.substring(2,e1.value.length);
         var indice;
         if (e1.value.length == 9)
            indice = 3;
         else
            indice = 4;
         var fone1 = fone.substring(0, indice);
         var fone2 = fone.substring(indice, fone.length);
         e1.value = '(' + ddd + ') ' + fone1 + '-' + fone2;
      }
   }
}

/***********************************************************************************/
/* pré formatação de campo cpf */
/***********************************************************************************/

function formatCPF(e1) {
   if ( e1.value != '' ) {
      if ( (e1.value.length < 11) && (e1.value.length > 0) ) {
         alert('CPF Inválido');
         e1.focus();
      } else {
         var campo1 = e1.value.substring(0,3);
         var campo2 = e1.value.substring(3,6);
         var campo3 = e1.value.substring(6,9);
         var verificador = e1.value.substring(9,11);
         e1.value = campo1 + '.' + campo2 + '.' + campo3 + '-' + verificador;
      }
   }
}

/***********************************************************************************/
/* remove caracteres */
/***********************************************************************************/

function removeCaracs(Campo) {

	wrkCampo = '';
	var type= "";
	var caracter= "", ok= false;

	vr = Campo.value;
	
	for ( i=0; i < vr.length; i++ ) {
		
		caracter = vr.substring(i,i+1);
		
		ok = ( caracter != '.' && caracter != '/' && caracter != '-' && caracter != ' ' && caracter != '%' && caracter != '(' && caracter != ')');

		if ( ok) {
			wrkCampo += caracter;
		}
		
	}
	
	Campo.value = wrkCampo;
	
}

/***********************************************************************************/
// Confere se o CPF dado esta' OK
/***********************************************************************************/

function isCPF(cpf) {

	var result = "";
	var OK = false;
	var temp = apenasNumeros(cpf);
	
	if ( apenasNumerosRepetidos(temp)) {
		return false;
	}

	if (temp.length>10) {

		var work=temp.substring(0,(temp.length)-2)
		var resto = Resto(work);
		OK = (resto==parseInt(temp.charAt((temp.length)-2)));
		
		if (OK) {
			
			work=work+resto;
			resto= Resto(work);
			OK = (resto==parseInt(temp.charAt((temp.length)-1)));
		
		}
	
	}
	
	return (OK);
	
}

/***********************************************************************************/
/* formata Moeda */
/***********************************************************************************/

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;
}

/**********************************************************************************
MOSTRA RELÓGIO
**********************************************************************************/

function getRelogioServidor() {
	
	var objecto = document.getElementById("DisplayData");
	
	executaAjax(__URL__ + '_loadfiles/geral/relogio_load.php','DisplayData','relogio');
	
}

//setInterval("getRelogioServidor()",1000);

/***********************************************************************************/
/* somente números podem ser digitados */
/***********************************************************************************/

function valNumero(obj,event) {
    var str = obj.value;
    var Tecla = event.which;
    if(Tecla == null)
        Tecla = event.keyCode;

    if ( Tecla < 48 || Tecla > 57 ){
        event.returnValue = false;
		//alert("Neste campo somente números podem ser digitados");
        return false;
    }
    event.returnValue = true;
    return true;
}

/***********************************************************************************/
/* somente números e , podem ser digitados */
/***********************************************************************************/

function valMoney(obj,event) {
    var str = obj.value;
    var Tecla = event.which;
    if(Tecla == null)
        Tecla = event.keyCode;

    if ( (Tecla != 44) &&  (Tecla < 48 || Tecla > 57) ){
        event.returnValue = false;
        return false;
    } else if (Tecla == 44) {
    	if (str.indexOf(',') != -1) {
    		event.returnValue = false;
       		return false;
    	}
    }
    event.returnValue = true;
    return true;
}

/***********************************************************************************/
/* gera um numero randomico */
/***********************************************************************************/

function GetNumeroRandomico(possibilidades) {	
	return Math.round( Math.random() * possibilidades);
}



/**********************************************************************************
PEGA UM OBJETO PELO ID
**********************************************************************************/

function getObject(id) {	
	return document.getElementById(id);	
}


/**********************************************************************************
PEGA O VALOR DE UM CAMPO TEXT
**********************************************************************************/

function getValorText(id) {
	
	var campo = document.getElementById(id);
	
	if (campo != null) {		
		return campo.value;		
	}
	
}

/**********************************************************************************
PEGA O VALOR DE UM CAMPO RADIOBOX
**********************************************************************************/

function getValorRadioButton(campo) {
	
	var valor = '';
	
	if (campo != null) {		
		
		var tamanho = campo.length;
		
		for (i=0; i<tamanho; i++) {
			
			if (campo[i].checked == true) {
				valor = campo[i].value;
				break;
			}
			
		}
		
	}
	
	return valor;
	
}


