//<!-- 
function SeeYouLater(){
	window.close();
	}
/////////////////////////////////////////////////////////////////
function Alerta(texto)
{
    alert(texto);
}

function ValidaData(objeto) 
{ 

    var DataString    = objeto.value; 
    var DataArray    = DataString.split("/");   
    var Flag=true; 

    if (DataArray.length != 3) 
        Flag=false; 
    else 
        { 
            if (DataArray.length==3) 
            { 
                var dia = DataArray[0], mes = DataArray[1], ano = DataArray[2]; 

                if (((Flag) && (ano<1000) || ano.length>4)) 
                    Flag=false; 
                 
                if (Flag) 
                { 
                    verifica_mes = new Date(mes+"/"+dia+"/"+ano); 
                    if (verifica_mes.getMonth() != (mes - 1)) 
                        Flag=false; 
                } 
            } 
            else 
                Flag=false; 
        } 
return Flag; 
} 

/***********************************************************************/

function Mascara_Hora(campo){ 
var hora01 = ''; 
hora01 = hora01 + campo.value; 
if (hora01.length == 2){ 
hora01 = hora01 + ':'; 
campo.value = hora01; 
} 
if (hora01.length == 5){ 
Verifica_Hora(campo); 
} 
} 
           
function Verifica_Hora(campo){ 
hrs = (campo.value.substring(0,2)); 
min = (campo.value.substring(3,5)); 
               
estado = ""; 
if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
estado = "errada"; 
} 
               
if (campo.value == "") { 
estado = "errada"; 
} 

if (estado == "errada") { 
alert("Hora inválida!"); 
campo.focus(); 
} 
}




/**********************************************************************/ 
function FormataData(objeto,teclapress) 
{ 
    var tecla = teclapress.keyCode; 

    if(((window.event.keyCode == 13) || (window.event.keyCode == 9))&&objeto.value != "") 
    { 
        if(!(ValidaData(objeto))) 
            { 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
                alert("Data Inválida"); 
                objeto.value = ""; 
                objeto.focus(); 
            } 
    } 

    if (( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )&& objeto.value.length < (10)) 
    { 
        vr = objeto.value; 
        vr = vr.replace( "/", "" ); 
        vr = vr.replace( "/", "" ); 
        tam = vr.length; 

        if (tam < 8) 
            { 
                if (tecla != 8) {tam = vr.length + 1 ;} 
            } 
        else 
            { 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
            } 
         
        if ((tecla == 8) && (tam > 1)) 
            { 
                tam = tam - 1 ; 
                objeto.value = vr.substr(0,tam); 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
            } 
                if ( tam <= 4 && tecla != 8){ 
                     objeto.value = vr ; } 

                if ( (tam >= 4) && (tam <= 6) ){ 
                     objeto.value = vr.substr(0, tam - 4) + '/' + vr.substr( tam - 4, 4 ); } 

                if ( (tam >= 6) && (tam <= 8) ){ 
                    objeto.value = vr.substr(0, tam - 6 ) + '/' + vr.substr( tam - 6, 2 ) + '/' + vr.substr( tam - 4, 4 ); } 

                if ((tam == (8)) && tecla != 8) 
                    { 
                        if(tecla >=96 && tecla <=105) 
                            { 
                                tecla = tecla - 48; 
                            } 

                        objeto.value = objeto.value + (String.fromCharCode(tecla)); 
                        window.event.cancelBubble = true; 
                        window.event.returnValue = false; 

                        if (!(ValidaData(objeto))) 
                            { 
                                alert("Data Invalida"); 
                                objeto.value = ""; 
                                objeto.focus(); 
                            } 
                    } 
    } 
    else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46)) 
        { 
            event.returnValue = false; 
        }
        if (window.event.keyCode >= 48 && window.event.keyCode <= 57)
		return true
	else
		window.event.keyCode = 0; 
} 
/////////////////////////////////////////////////////////////////
function FormataCPF(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
		if (window.event.keyCode >= 48 && window.event.keyCode <= 57)
		return true
	else
		window.event.keyCode = 0;
}
/////////////////////////////////////////////////////////////////
function FormataCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}
		if (window.event.keyCode >= 48 && window.event.keyCode <= 57)
		return true
	else
		window.event.keyCode = 0;
}

//////////////////////////////////////////////////////////////////
function FormataCEP(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '-' + vr.substr(5,tam-5);
			}
		if (window.event.keyCode >= 48 && window.event.keyCode <= 57)
		return true
	else
		window.event.keyCode = 0;
}
//////////////////////////////////////////////////////////////////
function SomenteNumeros(){
	if ((window.event.keyCode >= 48 && window.event.keyCode <= 57) || window.event.keyCode == 13)
		return true
	else
		window.event.keyCode = 0;
}
///////////////////////////////////////////////////////////////////
function formatamoney(c) {
    var t = this; if(c == undefined) c = 2;		
    var p, d = (t=t.split("."))[1].substr(0, c);
    for(p = (t=t[0]).length; (p-=3) >= 1;) {
	        t = t.substr(0,p) + "." + t.substr(p);
    }
    return t+","+d+Array(c+1-d.length).join(0);
}

String.prototype.formatCurrency=formatamoney

function demaskvalue(valor, currency){
/*
* Se currency é false, retorna o valor sem apenas com os números. Se é true, os dois últimos caracteres são considerados as 
* casas decimais
*/
var val2 = '';
var strCheck = '0123456789';
var len = valor.length;
	if (len== 0){
		return 0.00;
	}

	if (currency ==true){	
		/* Elimina os zeros à esquerda 
		* a variável  <i> passa a ser a localização do primeiro caractere após os zeros e 
		* val2 contém os caracteres (descontando os zeros à esquerda)
		*/
		
		for(var i = 0; i < len; i++)
			if ((valor.charAt(i) != '0') && (valor.charAt(i) != ',')) break;
		
		for(; i < len; i++){
			if (strCheck.indexOf(valor.charAt(i))!=-1) val2+= valor.charAt(i);
		}

		if(val2.length==0) return "0.00";
		if (val2.length==1)return "0.0" + val2;
		if (val2.length==2)return "0." + val2;
		
		var parte1 = val2.substring(0,val2.length-2);
		var parte2 = val2.substring(val2.length-2);
		var returnvalue = parte1 + "." + parte2;
		return returnvalue;
		
	}
	else{
			/* currency é false: retornamos os valores COM os zeros à esquerda, 
			* sem considerar os últimos 2 algarismos como casas decimais 
			*/
			val3 ="";
			for(var k=0; k < len; k++){
				if (strCheck.indexOf(valor.charAt(k))!=-1) val3+= valor.charAt(k);
			}			
	return val3;
	}
}


/////////////////////////////////////////////////////////////////
function reais(obj,tam,event){
var whichCode = (window.Event) ? event.which : event.keyCode;
/*
Executa a formatação após o backspace nos navegadores !document.all
*/
if (obj.value.length < tam)
{
  if (whichCode == 8 && !documentall) {  
  /*
  Previne a ação padrão nos navegadores
  */
      if (event.preventDefault){ //standart browsers
                  event.preventDefault();
            }else{ // internet explorer
                  event.returnValue = false;
      }
      var valor = obj.value;
      var x = valor.substring(0,valor.length-1);
      obj.value= demaskvalue(x,true).formatCurrency();
      return false;
  }
}else{
  window.event.keyCode = 0;
  }
/*
Executa o Formata Reais e faz o format currency novamente após o backspace
*/
FormataReais(obj,'.',',',event);
} // end reais

/////////////////////////////////////////////////////////////////

function backspace(obj,event){
/*
Essa função basicamente altera o  backspace nos input com máscara reais para os navegadores IE e opera.
O IE não detecta o keycode 8 no evento keypress, por isso, tratamos no keydown.
Como o opera suporta o infame document.all, tratamos dele na mesma parte do código.
*/

var whichCode = (window.Event) ? event.which : event.keyCode;
if (whichCode == 8 && documentall) {	
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	var y = demaskvalue(x,true).formatCurrency();

	obj.value =""; //necessário para o opera
	obj.value += y;
	
	if (event.preventDefault){ //standart browsers
			event.preventDefault();
		}else{ // internet explorer
			event.returnValue = false;
	}
	return false;

	}// end if		
}// end backspace

function FormataReais(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 == 8 ) return true; //backspace - estamos tratando disso em outra função no keydown
if (whichCode == 0 ) return true;
if (whichCode == 9 ) return true; //tecla tab
if (whichCode == 13) return true; //tecla enter
if (whichCode == 16) return true; //shift internet explorer
if (whichCode == 17) return true; //control no internet explorer
if (whichCode == 27 ) return true; //tecla esc
if (whichCode == 34 ) return true; //tecla end
if (whichCode == 35 ) return true;//tecla end
if (whichCode == 36 ) return true; //tecla home

/*
O trecho abaixo previne a ação padrão nos navegadores. Não estamos inserindo o caractere normalmente, mas via script
*/

if (e.preventDefault){ //standart browsers
		e.preventDefault()
	}else{ // internet explorer
		e.returnValue = false
}

var key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida

/*
Concatenamos ao value o keycode de key, se esse for um número
*/
fld.value += key;

var len = fld.value.length;
var bodeaux = demaskvalue(fld.value,true).formatCurrency();
fld.value=bodeaux;

/*
Essa parte da função tão somente move o cursor para o final no opera. Atualmente não existe como movê-lo no konqueror.
*/
  if (fld.createTextRange) {
    var range = fld.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (fld.setSelectionRange) {
    fld.focus();
    var length = fld.value.length;
    fld.setSelectionRange(length, length);
  }
  return false;
}
/////////////////////////////////////////////////////////////////////////
function FormataTelefone(objeto){
	if (objeto.value.indexOf("-") == -1 && objeto.value.length > 6){ objeto.value = ""; }
	if (objeto.value.length == 4){
	objeto.value +="-";
	}
	if (window.event.keyCode >= 48 && window.event.keyCode <= 57)
		return true
	else
		window.event.keyCode = 0;
 }
//////////////////////////////////////////////////////////
function FaixaNumero(Faixa1, Faixa2, objeto) 
{
 //Esta função testa se um determinado número(inteiro ou decimal) está numa faixa válida
  var re = /,/g;
  var number = objeto.value;
  var output = '';
  number = number.replace(re, ".");
  if ((number < Faixa1) || (number > Faixa2))
  {
    output = "";
    objeto.value = output;
  }
  //else 
  //{
  //  number = number.replace(".", ",");
  //  output = number; 
  //}
  
}
/////////////////////////////////////////////////////////////////
function limitaTextBox(campo1,campo2,campo3)
{
   if ((window.event.button != 0)||(window.event.keyCode == 16)||(window.event.keyCode == 18)||(window.event.keyCode == 17)||(window.event.keyCode == 93))
      {
        alert('Operacao invalida!'); 
        this.value=""; 
        }  

   teste = campo1.value;
   agora = new Date() 
   hora = agora.getHours();
   minuto = agora.getMinutes(); 
   segundo = agora.getSeconds(); 
   miliseg = agora.getMilliseconds();
   if (hora < 10) 
   {
      hora = "0" + hora
   } 
   if (minuto < 10) 
   {
      minuto = "0" + minuto
   }
   if (segundo < 10) 
   {
     segundo = "0" + segundo
   }
   if (miliseg < 10 ) 
   {
     miliseg = "00" + miliseg
   } 
     else if (miliseg < 100 && miliseg > 10) 
     {
       miliseg = "0" + miliseg
     }
     if (teste.length == 1)
            document.all[campo2.id].value = hora + "" + minuto + "" + segundo + "" + miliseg 
     else 
        if (teste.length == 12) 
        {
          document.all[campo3.id].value = hora + "" +  minuto + "" + segundo + "" +  miliseg;
           if ((document.all[campo3.id].value - document.all[campo2.id].value) >3000) 
           {
             alert('Operacao invalida!');
             document.all[campo1.id].value="";
             document.all[campo1.id].focus();
           }
        }
}
function Alerta(texto)
{
    alert(texto);
}

function desabilitaEnter() {
    if (window.event.keyCode == 13) { window.event.keyCode = 9; };
}
/////////////////////////////////////////////////////////////////

//-->
