// JavaScript Document
//captura o codigo da tecla pressionada 

// JavaScript Document
//captura o codigo da tecla pressionada 
var codTecla;

document.onkeyup=handleKeyboardAction;
function handleKeyboardAction(e){
	if (!e) var e = window.event; 
	codTecla = e.keyCode;
}

function PrintWindow(){ 
if (navigator.appName == "Microsoft Internet Explorer") { 
     var PrintCommand = '< O B J E C T ID="PrintCommandObject" WIDTH=0 HEIGHT=0 ';
PrintCommand += 'CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></O B J E C T>';
     document.body.insertAdjacentHTML('beforeEnd', PrintCommand); 
     PrintCommandObject.ExecWB(6, -1); PrintCommandObject.outerHTML = ""; } 
else { window.print();} }  

function verificaCodigo(){
	var retorno = false;
	if (codTecla==37 || codTecla==38 || codTecla==39 || codTecla==40 ){
		retorno = true;
	}
	return retorno;
}

function veriticaQtdValores(campo){
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	if((numero.length < 6) || (numero.length > 6)){
	    alert('Formato invalido!');
	}	
	return;
}

function veriticaQtdValoresDesc(campo){

	numero = campo.value;	
	if((numero.length > 349)){
	    alert('Numero maximo de caracteres igual a 350!');
	}	
	campo.value = numero.substr(0,349);
	return;
}

//o formato eh esse: 11/11/1111
function mascaraData(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";
	for(cont=0;cont<numero.length;cont++){
		switch(cont){			
			case 2:				
				if(numero.charAt(cont)!='/'){
					retorno += '/';
					retorno += numero.charAt(cont);					
				}
				else
					retorno += numero.charAt(cont);
			break;
			case 4:				
				if(numero.charAt(cont)!='/'){
					retorno += '/';
					retorno += numero.charAt(cont);					
				}
				else
					retorno += numero.charAt(cont);
			break;
			default:
				retorno += numero.charAt(cont);
			break;
		}
	}
	if(retorno.length > 10){
		campo.value = retorno.substr(0,10);
		return;
	}
	campo.value = retorno;
}

//o formato eh esse: 111.111.111-11
function mascaraCpf(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 3:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 6:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 9:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 14){
		campo.value = retorno.substr(0,11);
		return;
	}	
	campo.value=retorno;
}

//o formato eh esse: 11.111.111-11
function mascaraRg(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 5:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 8:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 13){
		campo.value = retorno.substr(0,13);
		return;
	}	
	campo.value=retorno;
}


//o formato eh esse: 55 71 9999-9999
function mascaraTelPaisDDD(campo){
   if(verificaCodigo())
		return; 
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;			
			case 4:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;
			case 8:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 15){
		campo.value = retorno.substr(0,15);
		return;
	}	
	campo.value=retorno;
}

//o formato eh esse: 55 71 9999-9999
function mascaraTelDDD(campo){
   if(verificaCodigo())
		return; 
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;
			case 6:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;			
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 12){
		campo.value = retorno.substr(0,12);
		return;
	}	
	campo.value=retorno;
}

function mascaraHora(campo){
   if(verificaCodigo())
		return; 
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += ':';
				retorno += numero.charAt(cont);	
			break;
			case 4:
				retorno += ':';
				retorno += numero.charAt(cont);	
			break;		
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 7){
		campo.value = retorno.substr(0,8);
		return;
	}	
	campo.value=retorno;
}

function casSql(valor){
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");
}

function mascaraCep(e,src,mask) {
    if(window.event){ 
        _TXT = e.keyCode; 
    } 
    else if(e.which) { 
        _TXT = e.which; 
    }
    if(_TXT > 47 && _TXT < 58){ 
         var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
         if (texto.substring(0,1) != saida) { 
            src.value += texto.substring(0,1); 
         } 
         return true; 
    } else { 
        if (_TXT != 8){ 
            return false; 
        } 
        else{ 
            return true; 
        }
    }
}

function Mascara(evt, objeto, mask) {
     
    var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
    var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var Numeros = '0123456789';
    var Fixos  = '().-:/ '; 
    var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

    evt = (evt) ? evt : (window.event) ? window.event : "";
    var value = objeto.value;
    if (evt) {
         var ntecla = (evt.which) ? evt.which : evt.keyCode;
         tecla = Charset.substr(ntecla - 32, 1);
         if (ntecla < 32) return true;

         var tamanho = value.length;
         if (tamanho >= mask.length) return false;

         var pos = mask.substr(tamanho,1); 
         while (Fixos.indexOf(pos) != -1) {
              value += pos;
              tamanho = value.length;
              if (tamanho >= mask.length) return false;
              pos = mask.substr(tamanho,1);
         }

         switch (pos) {
               case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
               case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
               case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
               case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
               case '*' : objeto.value = value; return true; break;
               default : return false; break;
         }
    }
    objeto.value = value; 
    return true;
}

var wd
function parseelement(thisone)
  {    
   wd="w"
   var tempnum=thisone.value
   for (i=0;i<tempnum.length;i++)
   {
    if (tempnum.charAt(i)==",")
    {
     wd="d"
     break
    }
   }
   if (wd=="w")
    thisone.value=tempnum+",00"
   else
   {
    if (tempnum.charAt(tempnum.length-2)==",")
    {
     thisone.value=tempnum+"0"
    }
    else if (tempnum.charAt(tempnum.length-1)==",")
    {
     thisone.value=tempnum+"00"
    }
   }
}

//Abre o popup de cadastro
function abrirjanela(url)
{
	var ret;	
	ret = window.open(url,"", "dialogHeight: 768px; dialogWidth:1024px; status=yes; location=yes; scrollbars=yes; resizable=yes");
}

function fecharjanela(id, nome, campo)
{
 	var elOptNew = window.opener.document.createElement('option');
	elOptNew.text = nome; 
	elOptNew.value = id;
	
	var elSel = window.opener.document.getElementById(campo);
	var elSel2 = window.opener.document.getElementById('teste');
    //var elSel = window.opener.document.getElementById('id_tarefa');
   
   	/*try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {*/
		elSel.add(elOptNew); // IE only
	//}
	elSel.value = id;
	elSel2.value = id;

	//parent.returnValue ='xx';
	window.close();
}
