﻿


String.prototype.reverse = function()
{
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
}

function MascararDataHora(evento, objeto)
{
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '00/00/0000 00:00:00')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao1 = '/';
	separacao2 = ' ';
	separacao3 = ':';
	conjunto1 = 2;
	conjunto2 = 5;
	conjunto3 = 10;
	conjunto4 = 13;
	conjunto5 = 16;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (19))
	{
		if (campo.value.length == conjunto1 )
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto2)
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto3)
		campo.value = campo.value + separacao2;
		else if (campo.value.length == conjunto4)
		campo.value = campo.value + separacao3;
		else if (campo.value.length == conjunto5)
		campo.value = campo.value + separacao3;
	}
	else
		event.returnValue = false;
}


function MascararCEP(Campo, teclapres)
			{
				var tecla = teclapres.keyCode;
				var vr = new String(Campo.value);
				vr = vr.replace("-", "");
				tam = vr.length + 1;
				if (tecla != 8)
				{
					if (tam == 6)
						Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, 5);
				}
			}


function AbrirPopUp(strUrl,strTitulo,strLargura, strAltura) {
    //window.open(strUrl ,strTitulo, "width=" + strLargura + ",height=" + strAltura + ",scrollbars= "+ strScrollbars + "'")
    //window.open("Sample.htm",null,"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
    
    var str = "height=[height],width=[width],status=yes,toolbar=true,menubar=true,location=no,resizable=yes'";
    str = str.replace("[width]", strLargura)
    str = str.replace("[height]", strAltura)
    
    //alert(str);
    window.open(strUrl,'_blank',str);
}



function SomenteNumero()
{ 
    //alert(event.keyCode);
    //virgula e menos
    //if(event.keyCode>=43 || event.keyCode<=45)return true
    //não numeros
    if (event.keyCode<48 || event.keyCode>57) return false; 
}

//captura eventos tanto para ie qto firefox
function allEve(e){
	var ev= (window.event)? window.event: e;
	if(!ev || !ev.type) return false;
	var ME= ev;
	
	if(ME.type.indexOf('key')!= -1){
		if(iz('ie') || ME.type.indexOf('keypress')!= -1){
			ME.key= (ev.keyCode)? ev.keyCode: ((ev.charCode)? ev.charCode: ev.which);
		}
		else ME.key= ev.charCode;
		if(ME.key) ME.letter= String.fromCharCode(ME.key);
	}
	return ME;
}



//function BloquearEntrada()
//{ 

//    if(document.all)
//    {
//        if(event.keyCode !=  9)
//        return false;
//    }
//    else
//        return false;

//}

function BloquearEntrada(e)
{ 
    var unicode=e.keyCode? e.keyCode : e.charCode
    
    if(unicode !=  9)
        return false;
}

function Habilitar(txt,blnValor)
{
   txt.disabled = !blnValor;
}

function AlterarFundo(txt,strCor)
{
   txt.style.backgroundColor = strCor; 
}

function CalcularSomaPai_old(txt)
{
   //quem é o pai dele?
   var txtPai; 
   
   //Se forem campos do mes 1, olho no array 1, se nao olho no array do mes2
   for (i=0; i<=arrPaiFilho.length-1; i++)
   {
        //alert(arrPaiFilho[i][1]);alert(txt.id);
        if(arrPaiFilho[i][1] == txt.id)
        {
            txtPai = document.getElementById(arrPaiFilho[i][0]);
            //alert(arrPaiFilho[i][0]);
            break;
        }
   }
  
   if(txtPai == null) return;
  
   var i;
   var fltSomatorio = 0.00;
   
   //filhos do pai
   for (i=0; i<=arrPaiFilho.length-1; i++)
   {
        //filhos dele
        if(arrPaiFilho[i][0] == txtPai.id)
        {
            var valorFilho = 0.0;
            
            if(isNaN(parseFloat(FormatarValorPadraoInternacional(document.getElementById(arrPaiFilho[i][1]).value))) == false) 
            {
                valorFilho = parseFloat(FormatarValorPadraoInternacional(document.getElementById(arrPaiFilho[i][1]).value));
            }
            
            //alert(valorFilho);
            fltSomatorio = fltSomatorio + valorFilho;
        }    
   }
   
   txtPai.value = FormatarValorPadraoBrasileiro(parseFloat(fltSomatorio.toString()));
         
   CalcularSomaPai(txtPai);
}


//txt = textbox
//arrPaiFilho = é o array que contem a árvore de pais e filhos
function CalcularSomaPai(txt,arrPaiFilho)
{
   //quem é o pai dele?
   var txtPai; 
   
   for (i=0; i<=arrPaiFilho.length-1; i++)
   {
        //alert(arrPaiFilho[i][1]);alert(txt.id);
        if(arrPaiFilho[i][1] == txt.id)
        {
            txtPai = document.getElementById(arrPaiFilho[i][0]);
            //alert(arrPaiFilho[i][0]);
            break;
        }
   }
  
   if(txtPai == null) return;
  
   var i;
   var fltSomatorio = 0.00;
   
   //filhos do pai
   for (i=0; i<=arrPaiFilho.length-1; i++)
   {
        //filhos dele
        if(arrPaiFilho[i][0] == txtPai.id)
        {
            var valorFilho = 0.00;
            //alert(FormatarValorPadraoInternacional(document.getElementById(arrPaiFilho[i][1]).value)); 
            
            if(isNaN(parseFloat(FormatarValorPadraoInternacional(document.getElementById(arrPaiFilho[i][1]).value))) == false) 
            {
                valorFilho = parseFloat(FormatarValorPadraoInternacional(document.getElementById(arrPaiFilho[i][1]).value));
            }
            
            fltSomatorio = fltSomatorio + valorFilho;
        }    
   }
   
   txtPai.value = FormatarValorPadraoBrasileiro(fltSomatorio.toString());
   
   CalcularSomaPai(txtPai,arrPaiFilho);
   FormatarCamposSomatorio();
}

function setCursorPos( x ) {

    var txtRange = x.createTextRange();
    txtRange.moveStart( "character", x.value.length);
    txtRange.moveEnd( "character", 0 );
    txtRange.select();
    }









function FormatarValorPadraoInternacional(strValor)
{
    //return  strValor.toString().replace('.', '').replace(',', '.');
    //return  strValor.toString().replace('.', '')
    
    strValor = strValor.toString().replace(',', '_');
   
    for (i=0;i<=strValor.toString().length;i++) 
    {
      strValor =  strValor.toString().replace('.', '');    
    }
    
    strValor = strValor.toString().replace('_', '.');

    return strValor;
}

function FormatarValorPadraoBrasileiro(strValor)
{
    var numero = 0.0;
    numero = parseFloat(strValor);
    numero = numero.toFixed(2);    

    return numero.toString().replace('.', ',');
}

function MarcararValor(valor) 
{
    var tam=tam = valor.length;
    var vr=valor;
    var newval;
    dec=2;

    if ( tam <= dec ){ newval = vr ; }
    if ( (tam > dec) && (tam <= 5) ) { newval = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
    if ( (tam >= 6) && (tam <= 8) )  { newval = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
    if ( (tam >= 9) && (tam <= 11) ) { newval = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
    if ( (tam >= 12) && (tam <= 14) ){ newval = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
    if ( (tam >= 15) && (tam <= 17) ){ newval = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}

    return newval;
}

addEvent = 
   function(o, e, f, s)
   {
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
                                                    try{
                                                        (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
                                                        e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
                                                        e.target || (e.target = e.srcElement || null);
                                                        e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
                                                    }catch(f){}
                                                    for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
                                                    return e = null, !!d;
                                                }
    };

    removeEvent = function(o, e, f, s)
    {
        for(var i = (e = o["_on" + e] || []).length; i;)
            if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
                return delete e[i];
        return false;
    };

   function formatCurrency(o, n, dig, dec)
   {
       o=document.getElementById(o);
       
       if (o!=null)
       {
            o.c = !isNaN(n) ? Math.abs(n) : 2;
	        o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
	        addEvent(o,"keypress", function(e)
	                                        {
		                                        if(e.key > 47 && e.key < 58)
		                                        {
		                                            var neg;
                                                    neg="";
                                                    if (this.value.substr(0,1)=='-')
                                                    {
                                                        neg='-';
                                                        this.value=this.value.substr(1);
                                                    }        
                                    			    
			                                        var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
			                                        if(o.maxLength + 1 && l >= o.maxLength) return false;
			                                        l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
			                                        for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
			                                            n && n < l && (s[l - ++n] += o.dec);
			                                        o.value = s.join("");	            
                                                    o.value=neg+o.value;
		                                        }
                                		    
		                                        if(e.key!=8 && e.key!=107 && e.key!=109)
		                                        {
		                                            if (e.key!=9){ultipost=0;}
		                                            e.key > 30 && e.preventDefault();
		                                        }
	                                        
	                                        
	                                        }
	                );
	
	addEvent(o, "keyup", function(e){
		if (e.key== 109) {
            if (this.value.substr(0,1)!='-'){
            this.value='-' + this.value;
            return false;
            }
            return false;
        }
        else if (e.key==107){
            if (this.value.substr(0,1)=='-'){
            this.value=this.value.substr(1);
            return false;
            }
            return false;
        }
		else if(e.key ==8){
		var neg;
            neg="";
            if (this.value.substr(0,1)=='-'){
            neg='-';
            this.value=this.value.substr(1);
            }        
			var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
			if(o.maxLength + 1 && l >= o.maxLength) return false;
			l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
			for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
			n && n < l && (s[l - ++n] += o.dec);
			o.value = s.join("");
			o.value=neg+o.value;
		}
		//tab
		else if (e.key!=9){
		    ultipost=0;
		}
		
		if(this.value == "") 
		    this.value = "0,00";
		    
		    
	});
 }
}

function Confirmar(str)
{
    if(confirm(str) == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function SomarCampos(strCampos)
{
    alert(strCampos);
    /*
    var arrFilho = strCampos.split(";");
    
    
    var i=0;
    var fltSomatorio=0.0;
    
    for(i=1;i<arrFilho.length-1;i++)
    {
        fltSomatorio += parseFloat(FormatarValorPadraoInternacional(document.getElementById(arrFilho[i]).value));
    }
    
    return FormatarValorPadraoBrasileiro(parseFloat(fltSomatorio.toString()));
    */
}


function FormatarCamposSomatorio()
{
    var element;  
    for (var i = 0; i < document.forms[0].elements.length; i++)  
    {
        element = document.forms[0].elements[i];

	    switch (element.type)
	    {
            case 'text':
            if( 
                (element.id.toString().substring(0,37) == "ctl00_ContentPlaceHolder1_txt_NR_SOMA")
                || 
                (element.id.toString().substring(0,36) == "ctl00_ContentPlaceHolder1_txt_NR_SUM")
             )
            {
                element.value = FormatarMascaraSomatorio(element.value);
            }
            break;
        }
    }
}

function FormatarMascaraSomatorio(strValor)
{
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");
    strValor = strValor.toString().replace('.',"");

    var arrValor = new Array();
    arrValor = strValor.split(',');

    strValor = arrValor[0].toString();
    strValor = strValor.reverse();
    
    var strValorFormatado = ""; 
    var intPonto = 0;    
    
    for (i=0; i< arrValor[0].toString().length; i++) 
    {
        intPonto = intPonto +1; 
                
        if(intPonto == 3)
        {
            strValorFormatado =  strValorFormatado + strValor.substring(i,i+1) + "."; 
            intPonto =0;
        }
        else
        {
            strValorFormatado =  strValorFormatado + strValor.substring(i,i+1); 
        }
    }

    var strRetorno = strValorFormatado.reverse() + "," + arrValor[1].toString();    
    
    if(strRetorno.substring(0,1) == ".")//ponto na frente
        strRetorno = strRetorno.substring(1);
    else if(strRetorno.substring(0,2) == "-.") //negativo
        strRetorno = strRetorno.toString().replace("-.","-");     
    
    return strRetorno;
}


function BloaquearBackSpace(e) 
{
    if (window.event){   //IE
        tecla = e.keyCode;
    } else if (e.which){ //FF
        tecla = e.which;
    }
    
    alert(tecla);
       if (tecla == 8) 
       {
          return false;
       }
}



