// JavaScript Document
//PREPARAR DIALOGOS
$(function()
{
	$("#alerta").dialog
	({
 		autoOpen:false,
		bgiframe:true,
		modal:true,
		buttons:
		{
			Aceptar:function()
			{
				$(this).dialog('close');
			}
		}
	});
	$("#cargando").dialog
	({
 		autoOpen:false,
		bgiframe:true,
		modal:true
	});	
});

var ajax;

function CrearXHRObject()
{
	//OTROS NAVEGADORES
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	//INTERNET EXPLORER
	else
	if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	else
	{
		document.getElementById('alerta').innerHTML = "Al parecer su navegador no soporta XMLHttpRequest, por favor actual&iacute;celo o cambie de software";
		$("#alerta").dialog('open');
	}
}

function EvaluarCamposVacios()
{
	var flag = true;
	var empresa = document.getElementById('empresa');
	if (empresa.value.length == 0)
	{
		empresa.style.backgroundColor = "red";
		empresa.style.color = "white";
		flag = false;
	}
	else
	{
		empresa.style.backgroundColor = "white";
		empresa.style.color = "black";
	}
	var contacto = document.getElementById('contacto');
	if (contacto.value.length == 0)
	{
		contacto.style.backgroundColor = "red";
		contacto.style.color = "white";
		flag = false;
	}
	else
	{
		contacto.style.backgroundColor = "white";	
		contacto.style.color = "black";
	}
	var mail = document.getElementById('mail');
	if (mail.value.length == 0)
	{
		mail.style.backgroundColor = "red";
		mail.style.color = "white";
		flag = false;
	}
	else
	{
		mail.style.backgroundColor = "white";	
		mail.style.color = "black";
	}	
	var telefono = document.getElementById('telefono');
	if (telefono.value.length == 0)
	{
		telefono.style.backgroundColor = "red";
		telefono.style.color = "white";
		flag = false;
	}
	else
	{
		telefono.style.backgroundColor = "white";	
		telefono.style.color = "black";
	}
	var fax = document.getElementById('fax');
	if (fax.value.length == 0)
	{
		fax.style.backgroundColor = "red";
		fax.style.color = "white";
		flag = false;
	}
	else
	{
		fax.style.backgroundColor = "white";	
		fax.style.color = "black";
	}
	var pais = document.getElementById('pais');
	if (pais.value.length == 0)
	{
		pais.style.backgroundColor = "red";
		pais.style.color = "white";
		flag = false;
	}
	else
	{
		pais.style.backgroundColor = "white";	
		pais.style.color = "black";
	}
	var materia = document.getElementById('materia');
	if (materia.value.length == 0)
	{
		materia.style.backgroundColor = "red";
		materia.style.color = "white";
		flag = false;
	}
	else
	{
		materia.style.backgroundColor = "white";	
		materia.style.color = "black";
	}		
	if (flag == false)
		document.getElementById('status').innerHTML = "<b>ATENCI&Oacute;N: Existen campos vacios indicados en color rojo, completelos antes de enviar.</b>";
	if (flag == true)
		Enviar();
}

function Enviar()
{
	document.getElementById('cargando').innerHTML = "Procesando por favor espere... <img src=\"/img/procesando.gif\" width=\"25\" height=\"32\" align=\"baseline\" />";
	$("#cargando").dialog('open');
	CrearXHRObject();
	ajax.onreadystatechange = ConfirmacionEnvio;
	var sempresa = document.getElementById('empresa').value;
	var scontacto = document.getElementById('contacto').value;
	var smail = document.getElementById('mail').value;
	var stelefono = document.getElementById('telefono').value;
	var sfax = document.getElementById('fax').value;
	var spais = document.getElementById('pais').value;
	var smateria = document.getElementById('materia').value;
	var sotros = document.getElementById('otros').value;
	ajax.open("GET","enviar.jsp?flag=ok&empresa="+sempresa+"&contacto="+scontacto+"&mail="+smail+"&telefono="+stelefono+"&fax="+sfax+"&pais="+spais+"&materia="+smateria+"&otros="+sotros,true);
	ajax.send(null);
}

function ConfirmacionEnvio()
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			document.getElementById('content').innerHTML="<b>"+ajax.responseText+"</b>";
			$("#cargando").dialog('close');
		}
	}
}
