function EnterPress(event, strBotao, bolPost)
{
    iKey = KeyStroke(event);

    if (iKey == 13) {
        if (bolPost == null)
        {
            bolPost = true
        }
        
        if (bolPost)
        {
            __doPostBack(strBotao, '');
            document.getElementById(strBotao).focus();
        }
        else
        {
            document.getElementById(strBotao).click();
            //document.getElementById(strBotao).focus();   // está comentado, porque no IE8 clica quando seta o focus e acaba executando 2 vezes a mesma rotina
        }
        return false;
    }
}

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 KeyStroke(event)
{ 
	ikey = 0;
	if(event.which == undefined)
	{
		ikey = window.event.keyCode;
	}
	else
	{
		ikey = event.which;
	}
	return 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 = "";
	}
	if (window.document.getElementById('lblAviso') != null) {
	    window.document.getElementById('lblAviso').innerHTML = "";
	    window.document.getElementById('lblAviso').innerText = "";
	}
}

//
//Functions to clear error label
//
if( document.body != null )
{
	document.body.onkeypress	= ErrorClear;
	document.body.onclick		= ErrorClear;
}

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 MascaraCNPJ_OnKeyPress(sCNPJ, event) {
    sCNPJ.maxLength = 18;
    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_OnKeyPress(sCPF, event) {
    sCPF.maxLength = 14;
    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 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);
}

function ShowMSG(strMSG) {
    strMSG = strMSG + "<br />";

    spanErro1 = window.document.getElementById("lblErroJS1");
    if (spanErro1)
        spanErro1.innerHTML = strMSG;

    spanErro2 = window.document.getElementById("lblErroJS2");
    if (spanErro2)
        spanErro2.innerHTML = strMSG;
}

