function keyPress(e)
{
	var x = '';
	if (document.all)
	{
		var evnt = window.event;
		x = evnt.keyCode;
	}
	else
	{
		x = e.keyCode;
	}
	return x;
}

function numberToFone(event, campo)
{
	var tecla = keyPress(event);

	if(tecla != 8)
	{
		if (tecla != 109)
		{
			if (campo.value.length == 1)
			{
				campo.value = '(' + campo.value;
			}
			else if (campo.value.length == 3)
			{
				campo.value += ')';
			}
			else if (campo.value.length == 8)
			{
				campo.value += '-';
			}
		}
	}
}

function numberToMoney(event, campo)
{
	var tecla = keyPress(event);

	if(tecla != 8)
	{
		if (tecla != 109)
		{
			if (campo.value.length == 1)
			{
				// 5
				campo.value = ',' + campo.value;
				// ,52
			}
			else if (campo.value.length == 3)
			{
				// ,52
				campo.value = campo.value.substr( 1, 1 ) + ',' + campo.value.substr( 2, 2 );
				// 5,20
			}
			else if (campo.value.length == 4)
			{
				// 5,20
				campo.value = campo.value.substr( 0, 1 ) + campo.value.substr( 2, 1 ) + ',' + campo.value.substr( 3, 2 );
				// 52,00
			}
			else if (campo.value.length == 5)
			{
				// 52,00
				campo.value = campo.value.substr( 0, 2 ) + campo.value.substr( 3, 1 ) + ',' + campo.value.substr( 4, 2 );
				// 520,05
			}
			else if (campo.value.length == 6)
			{
				// 520,05
				campo.value = campo.value.substr( 0, 1 ) + '.' + campo.value.substr( 1, 2 ) + campo.value.substr( 4, 1 ) + ',' + campo.value.substr( 5, 2 );
				// 5.200,51
			}
			else if (campo.value.length == 8)
			{
				// 5.200,51
				campo.value = campo.value.substr( 0, 1 ) + campo.value.substr( 2, 1 ) + '.' + campo.value.substr( 3, 2 ) + campo.value.substr( 6, 1 ) + ',' + campo.value.substr( 7, 2 );
				// 52.005,13
			}
			else if (campo.value.length == 9)
			{
				// 52.005,13
				campo.value = campo.value.substr( 0, 2 ) + campo.value.substr( 3, 1 ) + '.' + campo.value.substr( 4, 2 ) + campo.value.substr( 7, 1 ) + ',' + campo.value.substr( 8, 2 );
				// 520.051,30
			}
		}
	}
}

function stringToUrl(event, campo)
{
	var tecla = keyPress(event);

	if(tecla != 8)
	{
		if (tecla != 109)
		{
			if (campo.value.length == 0)
			{
				campo.value = 'http://' + campo.value;
			}
		}
	}
}

function carregaValorNormal( pProdutoId )
{
	new Ajax.Updater
	(
	    "valor_normal",
	    "/promocao/ajaxValorNormal/produto_id/"+pProdutoId,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$('valor_normal').innerHTML = 'Carregando...';
	      }
	    }
  	)
}

function enviarMalaDireta( moduloNome, moduloId )
{
	new Ajax.Updater
	(
	    "enviar_"+ moduloNome,
	    "/"+ moduloNome +"/ajaxMalaDireta/"+ moduloNome +"_id/"+ moduloId,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$("enviar_"+ moduloNome).innerHTML = 'Enviando...';
	      }
	    }
  	)
}

function enviarMailling( titulo, descricao, min, max )
{
	var lst_id = '';
	var separador = '';

	for( var i=min; i <= max; i++ )
	{
		if( document.getElementById('mailling_' + i) )
		{
			if( document.getElementById('mailling_' + i).checked == true )
			{
				lst_id += separador + i;

				separador = '_';
			}
		}
	}

	new Ajax.Updater
	(
	    "enviar_promocao",
	    "/mailling/ajaxMailling/titulo/"+ titulo +"/descricao/"+ descricao +"/lst_id/"+ lst_id,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$("enviar_promocao").innerHTML = 'Enviando...';
	      }
	    }
  	)
}

function carregaListaElementos( elementoNome, moduloNome, elementoPaiNome, elementoPaiId, elementoNomeAux, file_index )
{
    // Converte a primeira letra do nome do elemento para maiuscula.
    var primeiraLetra = elementoNome.substring(0, 1);
    var elementoScript = primeiraLetra.toUpperCase() + elementoNome.substring(1);

    if( elementoNomeAux )
    {
    	// Converte a primeira letra do nome do elemento auxiliar para maiuscula, para formar o nome do script.
	    primeiraLetra = elementoNomeAux.substring(0, 1);
	    elementoScript += primeiraLetra.toUpperCase() + elementoNomeAux.substring(1);
		// Adiciona '_' para formar o id do campo.
	    elementoNomeAux = '_'+ elementoNomeAux;
    }
    else
    {
    	elementoNomeAux = '';
    }

	new Ajax.Updater
	(
	    "select_"+ elementoNome + elementoNomeAux,
	    file_index+"/"+ moduloNome +"/ajax"+ elementoScript +"/"+ elementoPaiNome +"_id/"+ elementoPaiId,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$("select_"+ elementoNome + elementoNomeAux).innerHTML = 'Carregando...';
	      }
	    }
  	)
}

function exibeBotaoAdicionar( moduloNome, elementoNome, elementoId, elementoPaiNome, elementoPaiId )
{
    if( elementoId == '' )
    {
        elementoId = 0;
    }

    var elementoPai = '';
    if( elementoPaiId )
    {
        elementoPai = "/"+ elementoPaiNome +"_id/"+ elementoPaiId;
    }

    // Converte a primeira letra do nome do elemento para maiuscula.
    var primeiraLetra = elementoNome.substring(0, 1);
    var elementoScript = primeiraLetra.toUpperCase() + elementoNome.substring(1);

	new Ajax.Updater
	(
	    "select_"+ elementoNome,
	    "/"+ moduloNome +"/ajaxBotao"+ elementoScript +"/"+ elementoNome +"_id/"+ elementoId + elementoPai,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$("select_"+ elementoNome).innerHTML = 'Carregando...';
	      }
	    }
  	)
}

function adicionaElemento( elementoNome, elementoId, moduloNome )
{
    // Converte a primeira letra do nome do elemento para maiuscula.
    var primeiraLetra = elementoNome.substring(0, 1);
    var elementoScript = primeiraLetra.toUpperCase() + elementoNome.substring(1);

	new Ajax.Updater
	(
	    elementoNome + "_id_"+ elementoId,
	    "/"+ moduloNome +"/ajaxAdiciona"+ elementoScript +"/"+ elementoNome +"_id/"+ elementoId,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$(elementoNome + "_id_"+ elementoId).innerHTML = 'Adicionando...';
	      }
	    }
  	)
}

function removeElemento( elementoNome, elementoId )
{
    	$(elementoNome +'_id_'+ elementoId).innerHTML = '';
}

function adicionaImagem( imagemNome, moduloNome, moduloId )
{
	new Ajax.Updater
	(
	    'upload_'+ moduloNome,
	    "/"+ moduloNome +"/ajaxAdicionaImagem/"+ moduloNome  +"_id/"+ moduloId +"/imagem/"+ imagemNome,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$('upload_'+ moduloNome).innerHTML = 'Adicionando...';
	      }
	    }
  	)
}

function excluiImagem( moduloId, moduloNome, imagemId, imagemLocal )
{
	new Ajax.Updater
	(
	    "upload_"+ moduloNome +"_"+ imagemLocal,
	    "/"+ moduloNome +"/ajaxImagem/"+ moduloNome +"_id/"+ moduloId +"/imagem_id/"+ imagemId + "/imagem_local/"+ imagemLocal,
	    {
	      method: 'get',
	      onLoading: function()
	      {
	      	$("upload_"+ moduloNome +"_"+ imagemLocal).innerHTML = 'Carregando...';
	      }
	    }
  	)
}

/** Adiciona o site nos favoritos do navegador. **/
function adicionarFavoritos()
{
	var url = "http://www.cerebra.com.br";
	var title = "Cérebra";

	if( window.sidebar )
	{
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.opera && window.print )
	{
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}
	else if( document.all )
	{
		window.external.AddFavorite(url, title);
	}
}

/** Validacao do formulario de cadastro. **/
function validaCadastro()
{
	if(document.getElementById('nome').value == '')
	{
		alert('O preenchimento do campo \"Nome\" é obrigatório!');
		document.getElementById('nome').focus();
		return false;
	}

	if(document.getElementById('email').value == '')
	{
		alert('O preenchimento do campo \"E-mail\" é obrigatório!');
		document.getElementById('email').focus();
		return false;
	}
	else if( !validaEmail(document.getElementById('email').value) )
	{
		alert('Por favor, preencha o campo \"E-mail\" com um endereço válido!');
		document.getElementById('email').focus();
		return false;
	}

	return true;
}


/** Validacao do formulario de contato. **/
function validaContato()
{
	if(document.getElementById('nome_').value == '')
	{
		alert('O preenchimento do campo \"Nome\" é obrigatório!');
		document.getElementById('nome_').focus();
		return false;
	}

	if(document.getElementById('email_').value == '')
	{
		alert('O preenchimento do campo \"E-mail\" é obrigatório!');
		document.getElementById('email_').focus();
		return false;
	}
	else if( !validaEmail(document.getElementById('email_').value) )
	{
		alert('Por favor, preencha o campo \"E-mail\" com um endereço válido!');
		document.getElementById('email_').focus();
		return false;
	}

	if(document.getElementById('motivo').value == '')
	{
		alert('A seleção de uma opção válida do campo \"Motivo\" é obrigatório!');
		document.getElementById('motivo').focus();
		return false;
	}

	if(document.getElementById('descricao').value == '')
	{
		alert('O preenchimento do campo \"Mensagem\" é obrigatório!');
		document.getElementById('motivo').focus();
		return false;
	}

	return true;

}

/** Validacao do endereco do email. **/
function validaEmail(email)
{
   var reg = new RegExp('^[a-z0-9]+([\._-][a-z0-9]+)*@[a-z0-9_-]+(\.[a-z0-9]+){0,4}\.[a-z0-9]{2,4}$');
   if( reg.test(email) == false )
   {
      return false;
   }
   return true;
}

/** Ao colocar o foco no campo, todo o texto do mesmo eh selecionado. **/
function selecionaTexto( campo )
{
	document.getElementById( campo ).select()
}

/** Ao selecionar uma imagem via upload, o seu nome eh definido no campo falso. **/
function imagemSelecionada( campo, local )
{
	// Coloca o nome da imagem para vizualizacao na label falsa.
	document.getElementById('file_falso_'+ local).value = campo.value;

	// Exibe o botao de adicionar imagem.
	/*if( !document.getElementById("arquivo").style.display )
	{
		document.getElementById("arquivo").style.display = 'block';
	}*/
}
