var FC_URL_ENVIA = "http://desenv.e4w.com.br:8080/faleconosco/EnviaMensagemAjax";
var FC_URL_CATEGORIAS = "http://desenv.e4w.com.br:8080/faleconosco/ListaCategoriasAjax";
var FC_URL_CATEGORIAS_ING = "http://desenv.e4w.com.br:8080/faleconosco/ListaCategoriasAjaxING";

/* 1 **********************************************************************************/
function FC_iniciaForm( idFormulario )
{
	f = document.getElementById(idFormulario);
	if( FC_verificaForm(f) )
	{
		FC_travaForm(f);
		FC_requestCategorias(f);
	}

/* 2 **********************************************************************************/
function FC_verificaForm(f)
{
	texto = "";
	
	if( f.txtNome == null || f.txtNome == undefined )
	{
		texto += " - txtNome\n";
	}
	if( f.txtEmail == null || f.txtEmail == undefined )
	{
		texto += " - txtEmail\n";
	}
	if( f.cmbCategoria == null || f.cmbCategoria == undefined )
	{
		texto += " - cmbCategoria\n";
	}
	if( f.txtAssunto == null || f.txtAssunto == undefined )
	{
		texto += " - txtAssunto\n";
	}
	if( f.txtMensagem == null || f.txtMensagem == undefined )
	{
		texto += " - txtMensagem\n";
	}
	
	if( texto != "" )
	{
		texto = "Erro! \nO formulário precisa ter os seguintes campos: \n" + texto;
		alert(texto);
		window.document.writeln("<h1>ESTE FORMULÁRIO CONTÉM ERROS <textarea>" + texto + "</textarea>");
		return false;
	}
	
	return true;
}

/* 3 **********************************************************************************/
function FC_travaForm(f)
{
	f.action = "";
	f.onsubmit = function() { return false; };
}

/* 4 **********************************************************************************/
function FC_requestCategorias(f)
{
	/***############################################## 
	*	FC_xmlRequest		- função definida abaixo (em 6)
	*	FC_URL_CATEGORIAS	- valor cte definido na primeira linha deste arquivo 
	*	f 					- informações do formulário
	*	FC_preenchCombo		- função definida abaixo (em 5)
	***############################################### */
	FC_xmlRequest(FC_URL_CATEGORIAS,f,FC_preencheCombo);
}

/* 5 **********************************************************************************/
function FC_preencheCombo(f,xml)
{
	erro = xml.getElementsByTagName("erro");
	
	if( erro.length > 0 )
	{
		alert( "ERRO: " + erro[0].childNodes[0].nodeValue );
		return;
	}
	
	categorias = xml.getElementsByTagName("categoria");

	texto = "";
	for( i=0; i<categorias.length; i++ )
	{
		nome = categorias[i].getElementsByTagName("nome")[0].childNodes[0].nodeValue;
		codi = categorias[i].getElementsByTagName("codigo")[0].childNodes[0].nodeValue;
		
		opt = window.document.createElement('OPTION');
		opt.value = codi;
		opt.text = nome;
		f.cmbCategoria.options.add(opt);
	}
	
	/***############################################## 
	*	FC_destravaForm		- função definida abaixo (em 7)
	*	f 					- informações do formulário
	***############################################### */
	FC_destravaForm(f);
}

/* 6 **********************************************************************************/
function FC_xmlRequest(url, obj, func)
{
	if (!url) 
	{
		return false;
	}
		
  	if (window.XMLHttpRequest)
  	{
    	var http = new XMLHttpRequest();
    }
	else if (window.ActiveXObject)
	{
    	try 
    	{ 
    		http = new ActiveXObject("Msxml2.XMLHTTP"); 
    	}
    	catch(e) 
    	{ 
    		http = new ActiveXObject("Microsoft.XMLHTTP"); 
    	}
    }

    if (!http)
    {
    	return false;
    }
    if (func)
    { 
		http.onreadystatechange = function()
		{
			if (http.readyState != 4)
			{ 
				//alert("nada");
				return;
			}
	        func(obj,http.responseXML);
		};
    }
    else
   	{
		http.onreadystatechange = function() { return; }
	}
	
    http.open('GET', url, true);
    http.send(null);
    
    if (func) 
    {
    } 
    else 
    {
    	return http.responseText;
    }
    
    return true;
}

/* 7 **********************************************************************************/
function FC_destravaForm(f)
{
	f.action = FC_URL_ENVIA;
	f.onsubmit = function() { return FC_enviaForm(this) };
}

/* 8 **********************************************************************************/
function FC_enviaForm(f)
{
	if( !FC_validaForm(f) )
	{
		return false;
	}
	/* Daniel - descomentar isso depois */
	// FC_travaForm(f);
	url  = FC_URL_ENVIA + "?txtNome=" + f.txtNome.value;
	url += "&txtEmail=" + f.txtEmail.value;
	url += "&cmbCategoria=" + f.cmbCategoria.value;
	url += "&txtAssunto=" + f.txtAssunto.value;
	url += "&txtMensagem=" + f.txtMensagem.value;
	
	/***############################################## 
	*	FC_xmlRequest		- função definida acima (em 6)
	*	url 				- String definido aqui
	*	f					- informação do formulario
	*	FC_mensagemEnviada	- função definida abaixo (em 9)
	***############################################### */
	FC_xmlRequest(url, f, FC_mensagemEnviada);
	
	return false;
}

/* 9 **********************************************************************************/
function FC_mensagemEnviada(f,xml)
{
	erro = xml.getElementsByTagName("erro");
	
	if( erro.length > 0 )
	{
		alert( "ERRO: " + erro[0].childNodes[0].nodeValue );
		return;
	}
	
	alert("Mensagem enviada com sucesso");
	/* Daniel - descomentar isso depois */
	// FC_destravaForm(f);
	FC_limpaForm(f);
}



/* 10 **********************************************************************************/
function FC_validaForm(f)
{
	if (f.txtNome.value == "")
	{
		alert("Preencha o campo 'Nome'");
		f.txtNome.focus();
		return false;
	}
	if (f.txtEmail.value == "")
	{
		alert("Preencha o campo 'E-mail'");
		f.txtEmail.focus();
		return false;
	}
	if (f.txtEmail.value.match("@") == null)
	{
		alert("Preencha o campo 'E-mail' corretamente");
		f.txtEmail.focus();
		return false;
	}
/*** daniel *********************************************/
	if (		(document.fale.txtEmail.value.match("ê") != null) || 
				(document.fale.txtEmail.value.match("é") != null) ||
				(document.fale.txtEmail.value.match("è") != null) ||
				(document.fale.txtEmail.value.match("ë") != null) ||
				(document.fale.txtEmail.value.match("á") != null) ||
				(document.fale.txtEmail.value.match("à") != null) ||
				(document.fale.txtEmail.value.match("â") != null) ||
				(document.fale.txtEmail.value.match("ä") != null) ||
				(document.fale.txtEmail.value.match("ã") != null) ||
				(document.fale.txtEmail.value.match("í") != null) ||
				(document.fale.txtEmail.value.match("ì") != null) ||
				(document.fale.txtEmail.value.match("î") != null) ||
				(document.fale.txtEmail.value.match("ï") != null) ||
				(document.fale.txtEmail.value.match("ó") != null) ||
				(document.fale.txtEmail.value.match("ò") != null) ||
				(document.fale.txtEmail.value.match("õ") != null) ||
				(document.fale.txtEmail.value.match("ô") != null) ||
				(document.fale.txtEmail.value.match("ö") != null) ||
				(document.fale.txtEmail.value.match("ú") != null) ||
				(document.fale.txtEmail.value.match("ù") != null) ||
				(document.fale.txtEmail.value.match("û") != null) ||
				(document.fale.txtEmail.value.match("ü") != null) ||
				(document.fale.txtEmail.value.match("ÿ") != null) ||
				(document.fale.txtEmail.value.match("ý") != null) ||
				(document.fale.txtEmail.value.match("ç") != null)   )
		{
			alert("Por favor remova qualquer acento ou 'ç' do seu e-mail");
			document.fale.txtEmail.focus();
			return false;
		}
		
		if (		(document.fale.txtEmail.value.match(",") != null) ||
					(document.fale.txtEmail.value.match(";") != null)   )
		{
			alert("Remova qualquer ',' ou ';' do campo e-mail (entre com somente um endereço)");
			document.fale.txtEmail.focus();
			return false;
		}
		
		maxlengthMensagem=500;
		if ( ( f.txtMensagem.value.length ) >= maxlengthMensagem)
		{
			alert("A mensagem contem " + 
			      document.fale.txtMensagem.value.length +
			      " caracteres. Reduza para " +
			      maxlengthMensagem);
			document.fale.txtMensagem.focus();
			return false;
		}
		
		maxlengthAssunto=100;
		if ( (f.txtAssunto.value.length) >= maxlengthAssunto)
		{
			alert("O assunto contem " + 
			      document.fale.txtAssunto.value.length +
			      " caracteres. Reduza para " +
			      maxlengthAssunto);
			document.fale.txtAssunto.focus();
			return false;
		}
/*** FIM - daniel *************************************/
	if (f.cmbCategoria.value == "0")
	{
		alert("Preencha o campo categoria");
		f.cmbCategoria.focus();
		return false;
	}
	if (f.txtAssunto.value == "")
	{
		alert("Preencha o campo 'Assunto'");
		f.txtAssunto.focus();
		return false;
	}
	if (f.txtMensagem.value == "")
	{
		alert("Preencha o campo 'Mensagem'");
		f.txtMensagem.focus();
		return false;
	}
	return true;
}
/* 11 **********************************************************************************/
function FC_limpaForm(f)
{
	f.txtNome.value = "";
	f.txtEmail.value = "";
	f.txtAssunto.value = "";
	f.txtMensagem.value = "";
}

/***  Daniel **************************/
//function FC_iniciaFormING( idFormulario )
//{
//	f = document.getElementById(idFormulario);
//	if( FC_verificaForm(f) )
//	{
//		FC_travaForm(f);
//		FC_requestCategoriasING(f);
//	}
//	
//function FC_requestCategorias(f)
//{
//	/***############################################## 
//	*	FC_xmlRequest		- função definida abaixo (em 6)
//	*	FC_URL_CATEGORIAS	- valor cte definido na primeira linha deste arquivo 
//	*	f 					- informações do formulário
//	*	FC_preenchCombo		- função definida abaixo (em 5)
//	***############################################### */
//	FC_xmlRequest(FC_URL_CATEGORIAS_ING,f,FC_preencheCombo);
//}
	
	
/***  FIM - Daniel *********************/
}
