function criarCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	//document.cookie = name+"="+value+expires+"; path=/";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

function lerCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function apagarCookie(name) {
	createCookie(name,"",-1);
}


function FormataValor(objeto,teclapres,tammax,decimais) 
{
	var tecla			= teclapres.keyCode;
	var tamanhoObjeto	= objeto.value.length;
	if ((tecla == 8) && (tamanhoObjeto == tammax))
	{
		tamanhoObjeto = tamanhoObjeto - 1 ;
	}
    if (( tecla == 8 && tecla != 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) && ((tamanhoObjeto+1) <= tammax))
	{
		vr	= objeto.value;
		vr	= vr.replace( "/", "" );
		vr	= vr.replace( "/", "" );
		vr	= vr.replace( ",", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		vr	= vr.replace( ".", "" );
		tam	= vr.length;
		if (tam < tammax && tecla != 8)
		{
			tam = vr.length + 1 ;
		}
		if ((tecla == 8) && (tam > 1))
		{
			tam = tam - 1 ;
			vr = objeto.value;
			vr = vr.replace( "/", "" );
			vr = vr.replace( "/", "" );
			vr = vr.replace( ",", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
			vr = vr.replace( ".", "" );
		}
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{
			if (decimais > 0)
			{
				if ( (tam <= decimais) )
				{ 
					objeto.value = ("0," + vr) ;
				}
				if( (tam == (decimais + 1)) && (tecla == 8))
				{
					objeto.value = vr.substr( 0, (tam - decimais)) + ',' + vr.substr( tam - (decimais), tam ) ;	
				}
				if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) == "0"))
				{
					objeto.value = vr.substr( 1, (tam - (decimais+1))) + ',' + vr.substr( tam - (decimais), tam ) ;
				}
				if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) != "0"))
				{
				    objeto.value = vr.substr( 0, tam - decimais ) + ',' + vr.substr( tam - decimais, tam ) ; 
				}
				if ( (tam >= (decimais + 4)) && (tam <= (decimais + 6)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 3) ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
			 	if ( (tam >= (decimais + 7)) && (tam <= (decimais + 9)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 6) ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
				if ( (tam >= (decimais + 10)) && (tam <= (decimais + 12)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 9) ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
				if ( (tam >= (decimais + 13)) && (tam <= (decimais + 15)) )
				{
			 		objeto.value = vr.substr( 0, tam - (decimais + 12) ) + '.' + vr.substr( tam - (decimais + 12), 3 ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
				}
			}
			else if(decimais == 0)
			{
				if ( tam <= 3 )
				{ 
			 		objeto.value = vr ;
				}
				if ( (tam >= 4) && (tam <= 6) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr(0, tam - 3) + '.' + vr.substr( tam - 3, 3 ); 
				}
				if ( (tam >= 7) && (tam <= 9) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
				}
				if ( (tam >= 10) && (tam <= 12) )
				{
			 		if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
				}

				if ( (tam >= 13) && (tam <= 15) )
				{
					if(tecla == 8)
					{
						objeto.value = vr.substr(0, tam);
						window.event.cancelBubble = true;
						window.event.returnValue = false;
					}
					objeto.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ) ;
				}			
			}
		}
	}
	else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46))
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
}

function Mascara(formato,keypress,objeto){
	campo = eval (objeto);
	
	// cep
	if (formato=='cep'){
		separador = '-';
		conjunto1 = 5;
	
		if (campo.value.length == conjunto1)
		{		
			//campo.value = campo.value + separador;
			campo.value += separador;
		}
	}
	
	// cpf
	if (formato=='cpf'){
		separador1 = '.'; 
		separador2 = '-'; 
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		if (campo.value.length == conjunto1)
		  {
		  campo.value = campo.value + separador1;
		  }
		if (campo.value.length == conjunto2)
		  {
		  campo.value = campo.value + separador1;
		  }
		if (campo.value.length == conjunto3)
		  {
		  campo.value = campo.value + separador2;
		  }
	}
	
	// nascimento
	if (formato=='nascimento'){
		separador = '/'; 
		conjunto1 = 2;
		conjunto2 = 5;
		if (campo.value.length == conjunto1)
		  {
		  campo.value = campo.value + separador;
		  }
		if (campo.value.length == conjunto2)
		  {
		  campo.value = campo.value + separador;
		  }
	}
	
	// telefone
	if (formato=='telefone'){
		separador1 = '(';
		separador2 = ')';
		separador3 = '-';
		conjunto1 = 0;
		conjunto2 = 3;
		conjunto3 = 8;
		if (campo.value.length == conjunto1){
		campo.value = campo.value + separador1;
		}
		if (campo.value.length == conjunto2){
		campo.value = campo.value + separador2;
		}
		if (campo.value.length == conjunto3){
		campo.value = campo.value + separador3;
		}
	}	
}

function soNumeros(teclaPrescionada, objeto)
{
	var tecla = teclaPrescionada.keyCode;	
	if ( tecla == 8 && tecla != 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
 		return true;
	}
	else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46))
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
}

function AbreJanela(arq,nome)	{
	window.open(arq,nome,'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,top=0,left=0');	
} 
/*
    Formatação de campos.
    Diego Nepomuceno - WebVenda - 26/09/09
*/
/*
    Formato esperado: Somente números
    valor: String
    e: Event
    return: String
*/
function formata_Numero(valor, e){
	v= valor;
	v=v.replace(/\D/g,"");
	return v;
}
/*
    Formato esperado: __.___-___
    valor: String
    e: Event
    return: String
*/
function formata_CEP(valor, e){
	v= valor;
	v = v.substring(0,10);
	v=formata_Numero(v, e);
	v=v.replace(/^(\d{2})(\d)/,"$1.$2");
	v=v.replace(/(\d{3})(\d)/,"$1-$2");
	return v;
}
/*
    Formato esperado: (__)___.___
    valor: String
    e: Event
    return: String
*/
function formata_Telefone(valor, e){
	v= valor;
	v = v.substring(0,14);
	v=formata_Numero(v, e);
	v=v.replace(/^(\d{2})(\d)/,"($1) $2");
	v=v.replace(/(\d{4})(\d)/,"$1-$2");
	return v;
}
/*
    Formato esperado: ___.___.___-__
    valor: String
    e: Event
    return: String
*/
function formata_CPF(valor, e){
	v= valor;
	v = v.substring(0,14);
	v=formata_Numero(v, e);
	v=v.replace(/^(\d{3})(\d)/,"$1.$2");
	v=v.replace(/(\d{3})(\d)/,"$1.$2");
	v=v.replace(/(\d{3})(\d)/,"$1-$2");
	return v;
}
/*
    Formato esperado: __.___.___/____-__
    valor: String
    e: Event
    return: String
*/
function formata_CNPJ(valor, e){
	v= valor;
	v = v.substring(0,18);
	v=formata_Numero(v, e);
	v=v.replace(/^(\d{2})(\d)/,"$1.$2");
	v=v.replace(/(\d{3})(\d)/,"$1.$2");
	v=v.replace(/(\d{3})(\d)/,"$1/$2");
	v=v.replace(/(\d{4})(\d)/,"$1-$2");
	return v;
}


