//***************************************************
//Padrao.js - Arquivo com os JavaScripts da Aplicação
//***************************************************

function setFocus(field,text)
{
	field.value = '';
	field.focus();
	var x = new String(text)
	for(var i=0;i<x.length;i++)
	{
		field.value=field.value + x.charAt(i);
	}
}

function limpaSelect( oLst )
{
	for( i=oLst.length-1; i >=0; i--)
	{
		oLst.remove(i);
	}
	oLst.lenght = 0;
}

function ltrim(str)
{
	l = new Number(str.length); 
	if(l >0){
		while (str.charAt(0) == " " || str.charAt(0) == "\t")
			str = str.substr(1,l-1);
	}
	return str;
} 

function rtrim(str)
{
	l = new Number(str.length); 
	if(l >0){
		while (str.charAt(l-1) == " " || str.charAt(l-1) == "\t")
		{
			str = str.substr(0,l-1);
			l = str.length;
		}
	}
	return str;
} 

function trim(str)
{
	str = rtrim(str);
	str = ltrim(str);
	return str;
}

function lmpStr( strVal, StrCar ){

	strResult = '';
	for( iCont = 0; iCont < strVal.length; iCont++){
		if( strVal.charAt( iCont ) != StrCar ){
			strResult += strVal.charAt( iCont );
		}
	}
	return strResult;
}

function replace( strRepl, strArg1, strArg2  )
{
	while( strRepl.indexOf( ) > -1 )
	{
		strRepl = strRepl.replace(strArg1,strArg2);
	}
	return strRepl;

}

function soNumero(event)
{
		iKey = KeyStroke( event );
		if (!((iKey > 47 && iKey < 58) || iKey == 0 || iKey == 8 || iKey == 13)) {
			return false;
		}
		return true;
}

function soNumSinal( iKeyPerm, event )
{
	iKey = KeyStroke( event );
	if (!((iKey > 47 && iKey < 58) || (iKey == 0 || iKey == 8 || iKey == 13 || iKey == 44 || iKey == iKeyPerm))) {
		return false;
	}
	else
	{
	    return true;
	}
}

function soNumSinalsemVirgula(iKeyPerm, event) {
    iKey = KeyStroke(event);
    if (!((iKey > 47 && iKey < 58) || (iKey == 0 || iKey == 8 || iKey == 13 || iKey == iKeyPerm))) {
        return false;
    }
    else {
        return true;
    }
}

function soNaoSinal(iKeyPerm, event)
{
	iKey = KeyStroke( event );
	if( iKey == iKeyPerm ){
	    return false;
	}
	else {
	    return true;
	}
}

function KeyStroke( event )
{ 
	ikey = 0;
	if( event.which == undefined )
	{
		ikey = window.event.keyCode;
	}
	else
	{
		ikey = event.which;
	}
	return ikey;
}

function KeyIns( event, iKey )
{
	if( event.which == undefined )
	{
		window.event.keyCode = iKey;
	}
	else
	{
		event.keyCode = iKey;
	}
}

function ErrorClear(strLblError)
{
	if( window.document.getElementById(strLblError) != null )
	{
		window.document.getElementById(strLblError).innerHTML = "";
		window.document.getElementById(strLblError).innerText = "";
	}
}

function ErrorClear()
{
	if( window.document.getElementById('lblErro') != null )
	{
		window.document.getElementById('lblErro').innerHTML = "";
		window.document.getElementById('lblErro').innerText = "";
	}
}

//
//Functions to clear error label
//
if( document.body != null )
{
	document.body.onkeypress	= ErrorClear;
	document.body.onclick		= ErrorClear;
}

function ValidConfirm(strMsg)
{
    ErrorClear('lblErro');
    Page_ValidationActive = true;
    Page_ClientValidate();
    if( Page_IsValid )
    {
        if( confirm(strMsg) )
        {
        return true;
        }
        else
        {
        return false;
        }
    }
    else
    {
    return false;
    }
}

function Limpar(valor, validos) {
	// retira caracteres inválidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo, tammax, teclapres, decimal)
{
    var tecla = teclapres.keyCode;
    vr = Limpar(campo.value,"0123456789");
    tam = vr.length;
    dec=decimal

    if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

    if (tecla == 8 )
    { tam = tam - 1 ; }

    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
    {
        if ( tam <= dec )
        { campo.value = vr ; }

        if ( (tam > dec) && (tam <= 5) ){
        campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
        if ( (tam >= 6) && (tam <= 8) ){
        campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
        }
        if ( (tam >= 9) && (tam <= 11) ){
        campo.value = 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) ){
        campo.value = 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) ){
        campo.value = 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 ) ;}
    }
}

function isDate(ObjText) 
{
	valor	= '';
	valor	= trim(ObjText.value);
	valor	= lmpStr( valor, '/' );
	iDia	= 0;
	iMes	= 0;
	iAno	= 0;
	mask	= '';
	var today = new Date();
	var today_year = today.getFullYear();
	var today_mes = today.getMonth();
	var today_dia = today.getDay();
	
	if( trim(valor).length != 0 )
	{
		if( valor.length >= 1)
		{
			iDia	= parseFloat(valor.substring(0,2));
	        if( iDia < 01 ){ iDia = 01 }
	        if( iDia > 31 ){ iDia = 31 }
		}
		else
		{
		    iDia = today_dia;
		}
		if(valor.length >= 3)
		{
			iMes = parseFloat(valor.substring(2,4));
	        if( iMes < 01 ){ iMes = 01 }
	        if( iMes > 12 ){ iMes = 12 }        
	        
		}
		else
		{
		    iMes = today_mes+1;
		}
		if( valor.length > 4 )
		{
			iAno = parseFloat(valor.substring(4,8));
	        if( iAno < 100 ){ iAno = iAno + 2000 }
		}
		else
		{
		    iAno = today_year;
		}
		if( iAno < 1500 ){ iAno = today_year }
		if( iAno > 2500 ){ iAno = today_year }
		
		ObjText.value = '';

		//
		//Validacao de dias por mes
		//
		if( iMes == 4  && iDia > 30 ){ iDia = 30 }
		if( iMes == 6  && iDia > 30 ){ iDia = 30 }
		if( iMes == 9  && iDia > 30 ){ iDia = 30 }
		if( iMes == 11 && iDia > 30 ){ iDia = 30 }
		if( iMes == 2  && iDia > 29 ){ iDia = 29 }
		
		//
		//Bisexto
		//
		if( iMes == 2 && iDia >= 28 )
		{
			iBiSexto = iAno / 4;
			if( iBiSexto.toString().length == 3 )
			{
				iDia = 29;
			}
			else
			{
				iDia = 28;
			}
		}
		
		//
		//Dia
		//
		if( iDia.toString().length < 2 )
		{
			ObjText.value = ObjText.value + '0' + iDia.toString();
		}
		else
		{
			ObjText.value = ObjText.value + iDia.toString();
		}
		ObjText.value = ObjText.value + '/';
		
		//
		//Mes
		//
		if( iMes.toString().length < 2 )
		{
			ObjText.value = ObjText.value + '0' + iMes.toString() ;
		}
		else
		{
			ObjText.value = ObjText.value + iMes.toString();
		}
		ObjText.value = ObjText.value + '/' + iAno.toString();
	}
}

function MascData(ObjText, event) {
    valor     = '';
    valor     = trim(ObjText.value);
    valor     = lmpStr( valor, '/' );
    iKey     = KeyStroke(event);
    strDia   = '';
    strMes  = '';

    if (valor.length > 0 && ((iKey >= 48 && iKey <= 57) || (iKey >= 96 && iKey <= 105)))
    {
        mask = '';
        if( valor.length > 0 && valor.length <= 2)
        {
           mask = valor;
           if(valor.length == 2)
           {
               if(parseFloat( valor.substring(0,2) ) > 31)
               {
                   mask = '31';      
               }
            }
        }
        if( valor.length > 2 && valor.length <= 4)
        {
           if(parseFloat( valor.substring(0,2) ) > 31)
           { strDia = '31' }

           else
           {  strDia = valor.substring(0,2) }

           if( valor.length == 4 )
           {
               if(parseFloat( valor.substring(2,4) ) > 12)
               { strMes = '12' }

               else
               { strMes = valor.substring(2,4) }
           }
           else
           {
               strMes = valor.substring(2,4);
           }
           mask = mask + strDia + '/' + strMes;
        }
        if( valor.length > 4 )
        {
           if(parseFloat( valor.substring(0,2) ) > 31)
           { strDia = '31' }

           else
           {  strDia = valor.substring(0,2) }

           if( valor.length > 4 )
           {
               if(parseFloat( valor.substring(2,4) ) > 12)
               { strMes = '12' }

               else
               { strMes = valor.substring(2,4) }
           }
           else
           {
               strMes = valor.substring(2,4);
           }
           mask = mask + strDia + '/' + strMes + '/' + valor.substring(4,8);
        }
        ObjText.value = mask;
    }
}

function vdHora(ObjText) {
    valor = '';
    valor = trim(ObjText.value);
    valor = lmpStr(valor, ':');
    iHora = 0;
    iMinuto = 0;
    mask = '';
    var today = new Date();
    var today_Hora = today.getHours();
    var today_Minuto = today.getMinutes();

    if (trim(valor).length != 0) {
        if (valor.length >= 1) {
            iHora = parseFloat(valor.substring(0, 2));
            if (iHora < 00) { iHora = 00 }
            if (iHora > 23) { iHora = 23 }
        }
        else {
            iHora = today_Hora;
        }
        if (valor.length >= 3) {
            iMinuto = parseFloat(valor.substring(2, 4));
            if (iMinuto < 00) { iMinuto = 00 }
            if (iMinuto > 59) { iMinuto = 59 }

        }
        else {
            iMinuto = today_Minuto;
        }

        ObjText.value = '';

        if (iHora.toString().length < 2) {
            ObjText.value = ObjText.value + '0' + iHora.toString();
        }
        else {
            ObjText.value = ObjText.value + iHora.toString();
        }
        ObjText.value = ObjText.value + ':';

        if (iMinuto.toString().length < 2) {
            ObjText.value = ObjText.value + '0' + iMinuto.toString();
        }
        else {
            ObjText.value = ObjText.value + iMinuto.toString();
        }
    }
}

function MascHora(ObjText, event) {
    valor = '';
    valor = trim(ObjText.value);
    valor = lmpStr(valor, ':');
    iKey = KeyStroke(event);
    strHora = '';
    strMinuto = '';

    if (valor.length > 0 && ((iKey >= 48 && iKey <= 57) || (iKey >= 96 && iKey <= 105))) {
        mask = '';
        if (valor.length > 0 && valor.length <= 2) {
            mask = valor;
            if (valor.length == 2) {
                if (parseFloat(valor.substring(0, 2)) > 23) {
                    mask = '23';
                }
            }
        }

        if (valor.length > 2 && valor.length <= 4) {
            if (parseFloat(valor.substring(0, 2)) > 23)
            { strHora = '23' }

            else
            { strHora = valor.substring(0, 2) }

            if (valor.length == 4) {
                if (parseFloat(valor.substring(2, 4)) > 59)
                { strMinuto = '59' }

                else
                { strMinuto = valor.substring(2, 4) }
            }
            else {
                strMinuto = valor.substring(2, 4);
            }
            mask = mask + strHora + ':' + strMinuto;
        }

        ObjText.value = mask;
    }
}

function MascaraCNPJ_OnBlur(sCNPJ) {
    if (isValidCPF_CNPJ(sCNPJ.value))
        MascaraCNPJ(sCNPJ);
    else
        sCNPJ.value = '';
}
function MascaraCNPJ_OnKeyPress(sCNPJ, event) {
    valor = '';
    valor = trim(limpaCPF_CNPJ(sCNPJ.value));
    Key = KeyStroke(event);
    if (valor.length > 0 && ((Key >= 48 && Key <= 57) || (Key >= 96 && Key <= 105)))
        sCNPJ.value = MascaraCNPJ(valor);
}
function MascaraCNPJ(valor) {
    valor = trim(limpaCPF_CNPJ(valor));
    mask = '';
    if (valor.length > 0) {
        mask = valor;
        if (valor.length >= 2)
            mask = valor.substring(0, 2) + '.' + valor.substring(2, 5);
        if (valor.length >= 5)
            mask = valor.substring(0, 2) + '.' + valor.substring(2, 5) + '.' + valor.substring(5, 8);
        if (valor.length >= 8)
            mask = valor.substring(0, 2) + '.' + valor.substring(2, 5) + '.' + valor.substring(5, 8) + '/' + valor.substring(8, 12);
        if (valor.length >= 12)
            mask = valor.substring(0, 2) + '.' + valor.substring(2, 5) + '.' + valor.substring(5, 8) + '/' + valor.substring(8, 12) + '-' + valor.substring(12, 14);
    }
    
    return  mask;
}

function limpaCPF_CNPJ(strCNPJ) {
    strResult = '';
    for (iCont = 0; iCont < strCNPJ.length; iCont++) {
        if (strCNPJ.charAt(iCont) != '.' && strCNPJ.charAt(iCont) != '-' && strCNPJ.charAt(iCont) != '/') {
            strResult += strCNPJ.charAt(iCont);
        }
    }
    return strResult;
}

function MascaraCPF_OnBlur(sCPF) {
    if (isValidCPF_CNPJ(sCPF.value))
        sCPF.value = MascaraCPF(sCPF.value);
    else
        sCPF.value = '';
}
function MascaraCPF_OnKeyPress(sCPF, event) {
    valor = '';
    valor = trim(limpaCPF_CNPJ(sCPF.value));
    Key = KeyStroke(event);
    if (valor.length > 0 && ((Key >= 48 && Key <= 57) || (Key >= 96 && Key <= 105)))
        sCPF.value = MascaraCPF(valor);
}
function MascaraCPF(valor) {
    valor = trim(limpaCPF_CNPJ(valor));
    mask = '';
    if (valor.length > 0) {
        mask = valor;
        if (valor.length >= 3)
            mask = valor.substring(0, 3) + '.' + valor.substring(3, 6);
        if (valor.length >= 6)
            mask = valor.substring(0, 3) + '.' + valor.substring(3, 6) + '.' + valor.substring(6, 9);
        if (valor.length >= 9)
            mask = valor.substring(0, 3) + '.' + valor.substring(3, 6) + '.' + valor.substring(6, 9) + '-' + valor.substring(9, 11);
    }
    return mask;
}

function fnMascaraCEP(sCEP, event) {
    valor = '';
    valor = trim(fnlmpCEP(sCEP.value));
    Key = KeyStroke(event);
    if (valor.length > 0 && ((Key >= 48 && Key <= 57) || (Key >= 96 && Key <= 105))) {
        mask = '';
        if (valor.length > 0) {
            mask = valor;
            if (valor.length >= 5)
                mask = valor.substring(0, 5) + '-' + valor.substring(5, 8);
        }
        sCEP.value = mask;
    }
}

function fnlmpCEP(strCEP) {
    strResult = '';
    for (iCont = 0; iCont < strCEP.length; iCont++) {
        if (strCEP.charAt(iCont) != '-') {
            strResult += strCEP.charAt(iCont);
        }
    }
    return strResult;
}

function ConfirmaExclusao(Nome) {
    if (confirm('Confirma a exclusão de: "' + Nome + '"?'))
        return true;
    else
        return false;
}

function Confirma(strMensagem, strNome) {
    if (confirm(strMensagem + ': "' + strNome + '" ?'))
        return true;
    else
        return false;
}


function ConfirmaMensagemDepois(strMensagem, strMensagemDepois, strNome) {
    if (confirm(strMensagem + ': "' + strNome + '" ' + strMensagemDepois + '"?'))
        return true;
    else
        return false;
}

function isValidCPF_CNPJ(theCPF) {

    theCPF = trim(limpaCPF_CNPJ(theCPF));

    if (theCPF == "") {
        return (false);
    }
    if (((theCPF.length == 11) && (theCPF == 11111111111) || (theCPF == 22222222222) || (theCPF == 33333333333) || (theCPF == 44444444444) || (theCPF == 55555555555) || (theCPF == 66666666666) || (theCPF == 77777777777) || (theCPF == 88888888888) || (theCPF == 99999999999) || (theCPF == 00000000000))) {
        return (false);
    }


    if (!((theCPF.length == 11) || (theCPF.length == 14))) {
        return (false);
    }

    var checkOK = "0123456789";
    var checkStr = theCPF;
    var allValid = true;
    var allNum = "";
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
        allNum += ch;
    }

    if (!allValid) {
        return (false);
    }

    var chkVal = allNum;
    var prsVal = parseFloat(allNum);
    if (chkVal != "" && !(prsVal > "0")) {
        return (false);
    }

    if (theCPF.length == 11) {
        var tot = 0;

        for (i = 2; i <= 10; i++)
            tot += i * parseFloat(checkStr.charAt(10 - i));

        if ((tot * 10 % 11 % 10) != parseFloat(checkStr.charAt(9))) {
            return (false);
        }

        tot = 0;

        for (i = 2; i <= 11; i++)
            tot += i * parseFloat(checkStr.charAt(11 - i));

        if ((tot * 10 % 11 % 10) != parseFloat(checkStr.charAt(10))) {
            return (false);
        }
    }
    else {
        var tot = 0;
        var peso = 2;

        for (i = 0; i <= 11; i++) {
            tot += peso * parseFloat(checkStr.charAt(11 - i));
            peso++;
            if (peso == 10) {
                peso = 2;
            }
        }

        if ((tot * 10 % 11 % 10) != parseFloat(checkStr.charAt(12))) {
            return (false);
        }

        tot = 0;
        peso = 2;

        for (i = 0; i <= 12; i++) {
            tot += peso * parseFloat(checkStr.charAt(12 - i));
            peso++;
            if (peso == 10) {
                peso = 2;
            }
        }

        if ((tot * 10 % 11 % 10) != parseFloat(checkStr.charAt(13))) {
            return (false);
        }
    }
    return (true);

}


Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};
String.prototype.replaceAll = function(OldValue, NewValue) {
    var returnValue = "";
    returnValue = this;
    while (returnValue.indexOf(OldValue) >= 0) {
        returnValue = returnValue.replace(OldValue, NewValue);
    }
    return returnValue.toString();
};

