
function SomenteNumero(e)
{
	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}

	if(key > 47 && key < 58 || key == 8 || key == 0)
	{
		return;
	}
	else
	{
		if(window.event)
		{
			window.event.returnValue = null;
		}
		else
		{
			e.preventDefault();
		}
	}
}

function SomenteNumero_Mascara(e, src, mask)
{
	SomenteNumero(e);
	
	var key;

	if (window.event) 
	{
		key = event.keyCode;
	}
	else
	{ 
		key = e.which;
	}
	
	if (key != 8)
	{
		Mascara(src, mask);
	}
}

function AbrirPopUP(URL,nmPagina)
{
    try
    {
        void(window.open(URL, nmPagina, 'status = 1, height = 700, width = 600, resizable = 1,scrollbars=1'));
    }
    catch(err)
    {
        alert(err.description);
    }
}

function FormCabecalhoSubmit(action)
{
    var formCabecalho;
    var hidSid;
    var hidNome;
    
    formCabecalho = document.createElement("form");
    formCabecalho.name = "formCabecalho";
    formCabecalho.method = "post";
    formCabecalho.action = action;
    
    hidSid = document.createElement("input");
    hidSid.type = "hidden";
    hidSid.name = "sid";
    hidSid.value = getCookie("SID");
    
    hidNome = document.createElement("input");
    hidNome.type = "hidden";
    hidNome.name = "nome";
    hidNome.value = getCookie("NomeCliente");
    
    formCabecalho.appendChild(hidSid);
    formCabecalho.appendChild(hidNome);	    
    
    document.getElementById('divPost').appendChild(formCabecalho);
    formCabecalho.submit();
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return "";
	}
	if ( start == -1 ) return "";
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	var valuePlus = document.cookie.substring( len, end );
	while(valuePlus.indexOf('+') > -1)
	{
		valuePlus = valuePlus.replace('+', ' ');
	}
	return decodeURIComponent(valuePlus);
}

function _isinteger(test_string)
{
	var i;
	var non_nums = 0;

	for (i = 1; i < test_string.length; i++)
	{
		if ((test_string.substring(i-1,i) < '0') || (test_string.substring(i-1,i) > '9'))
		{
			non_nums++;
		}
	}

	if (non_nums == 0)
		return true;
	else
		return false;
}

function SomenteNumero2(e, src, mask)
{
	SomenteNumero(e);

	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}


	if(	key != 8)
	{
		Mascara(src,mask);
	}
}

function Mascara(src, mask)
{
	 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);
	 }
}

function ValidaCPF(cpf)
{
    var retorno = true;    	

    if(cpf == "")
    {
	    retorno = false;
    }
    else
    {
	    cpf = cpf.replace(".", "");
	    cpf = cpf.replace(".", "");
	    cpf = cpf.replace(".", "");
	    cpf = cpf.replace(",", "");
	    cpf = cpf.replace("-", "");
	    cpf = cpf.replace(" ", "");

	    erro = new String;
	    

	    if (cpf.length < 11) 
	        erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
	    
	    var nonNumbers = /\D/;
	    
	    if (nonNumbers.test(cpf)) 
	        erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 		
	        
	    if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
		    erro += "Numero de CPF invalido! \n\n";
		    
	    var a = [];
	    var b = new Number;
	    var c = 11;
		
	    for (i=0; i<11; i++)
	    {
		    a[i] = cpf.charAt(i);
		    if (i < 9) b += (a[i] * --c);
	    }			    
	    
        if ((x = b % 11) < 2) 
        { 
            a[9] = 0; 
        } 
        else 
        { 
            a[9] = 11-x;
        }		    
            
            
	    b = 0;
	    c = 11;
		
	    for (y=0; y<10; y++) 
	    {
	        b += (a[y] * c--); 	            
	    }
	    
        if ((x = b % 11) < 2) 
        { 
            a[10] = 0; 
        } 
        else 
        { 
            a[10] = 11-x; 
        }	
        
	    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
		    erro +="Digito verificador com problema!";
		
	    if (erro.length > 0)
		    retorno = false;      	    
    }
    
    return retorno;
}
    
function ValidaData(dia, mes, ano)
{
    retorno = true;
    
    dia = Number(dia);
    mes = Number(mes);
    ano = Number(ano);    
	
    if(ano < 1900)
    {
	    return false;
    }
	
	
    switch (mes) 
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        {
            if  (dia > 31) 
            {
                retorno = false;
            }
            break
        }
        
        case 4:
        case 6:
        case 9:
        case 11:
        
        {
            if  (dia > 30) 
            {
                retorno = false;
            }
            break
        }
        case 2:
        {
            /* Validando ano Bissexto / fevereiro / dia */
            if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
            { 
                bissexto = 1; 
            } 
            if ((bissexto == 1) && (dia > 29)) 
            { 
                retorno = false;;                 
            } 
            if ((bissexto != 1) && (dia > 28)) 
            { 
                retorno = false; 
            }            
            break                        
        }
    }
    
    return retorno;
}

