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 naoCtrl(event){
		iKey = KeyStroke( event );
		if (iKey == 9 || iKey == 97)
		    return false;
		else if (iKey == 17) 
		{
		    return false;
		}
		else 
		{
		    return true;
		}
}

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 soNumNaoSinal(iKeyPerm, event) {
    iKey = KeyStroke(event);
    if (!((iKey > 47 && iKey < 58) || (iKey == 0 || iKey == 8 || 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 = "";
	}
	ExecBusca()
}

function ExecBusca()
{
	if( window.document.getElementById('btnPesquisar') != null )
	{
		//Ao clicar F8
		if( KeyStroke(event) == 119 )
		{
			window.document.getElementById('btnPesquisar').click();
		}
	}
}

//
//Functions to clear error label
//
if( document.body != null )
{
	document.body.onkeydown		= ExecBusca;
	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, dec)
{
    var tecla = teclapres.keyCode;
    vr = Limpar(campo.value,"0123456789");
    tam = vr.length;
    dec=dec

    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 vdData(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 fnMascaraCNPJ(sCNPJ, event) {
    valor = '';
    valor = trim(fnlmpCNPJ(sCNPJ.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 >= 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);
        }
        sCNPJ.value = mask;
    }
}

function fnlmpCNPJ(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 fnMascaraCPF(sCPF, event) {
    valor = '';
    valor = trim(fnlmpCPF(sCPF.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 >= 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);
        }
        sCPF.value = mask;
    }
}

function fnlmpCPF(strCPF) {
    strResult = '';
    for (iCont = 0; iCont < strCPF.length; iCont++) {
        if (strCPF.charAt(iCont) != '.' && strCPF.charAt(iCont) != '-') {
            strResult += strCPF.charAt(iCont);
        }
    }
    return strResult;
}

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 fnlmpCNPJCPF( strCNPJCPF ){

	strResult = '';
	for( iCont = 0; iCont < strCNPJCPF.length; iCont++){
		if( strCNPJCPF.charAt( iCont ) != '.' && strCNPJCPF.charAt( iCont ) != '-' && strCNPJCPF.charAt( iCont ) != '/' ){
			strResult += strCNPJCPF.charAt( iCont );
		}
	}
	return strResult;
}
