// 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 nombre = document.getElementById('nombre');
	if (nombre.value.length == 0)
	{
		nombre.style.backgroundColor = "red";
		nombre.style.color = "white";
		flag = false;
	}
	else
	{
		nombre.style.backgroundColor = "white";
		nombre.style.color = "black";
	}
	var identificacion = document.getElementById('identificacion');
	if (identificacion.value.length == 0)
	{
		identificacion.style.backgroundColor = "red";
		identificacion.style.color = "white";
		flag = false;
	}
	else
	{
		identificacion.style.backgroundColor = "white";	
		identificacion.style.color = "black";
	}
	var nacionalidad = document.getElementById('nacionalidad');
	if (nacionalidad.value.length == 0)
	{
		nacionalidad.style.backgroundColor = "red";
		nacionalidad.style.color = "white";
		flag = false;
	}
	else
	{
		nacionalidad.style.backgroundColor = "white";	
		nacionalidad.style.color = "black";
	}
	var residencia = document.getElementById('residencia');
	if (residencia.value.length == 0)
	{
		residencia.style.backgroundColor = "red";
		residencia.style.color = "white";
		flag = false;
	}
	else
	{
		residencia.style.backgroundColor = "white";	
		residencia.style.color = "black";
	}
	var edad = document.getElementById('edad');
	if (edad.value.length == 0)
	{
		edad.style.backgroundColor = "red";
		edad.style.color = "white";
		flag = false;
	}
	else
	{
		edad.style.backgroundColor = "white";	
		edad.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 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 profesion = document.getElementById('profesion');
	if (profesion.value.length == 0)
	{
		profesion.style.backgroundColor = "red";
		profesion.style.color = "white";
		flag = false;
	}
	else
	{
		profesion.style.backgroundColor = "white";	
		profesion.style.color = "black";
	}
	var area = document.getElementById('area');
	if (area.value.length == 0)
	{
		area.style.backgroundColor = "red";
		area.style.color = "white";
		flag = false;
	}
	else
	{
		area.style.backgroundColor = "white";	
		area.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 snombre = document.getElementById('nombre').value;
	var sidentificacion = document.getElementById('identificacion').value;
	var snacionalidad = document.getElementById('nacionalidad').value;
	var sresidencia = document.getElementById('residencia').value;
	var sedad = document.getElementById('edad').value;
	var stelefono = document.getElementById('telefono').value;
	var smail = document.getElementById('mail').value;
	var sprofesion = document.getElementById('profesion').value;
	var sarea = document.getElementById('area').value;
	var sotros = document.getElementById('otros').value;
	ajax.open("GET","enviar.jsp?flag=ok&nombre="+snombre+"&identificacion="+sidentificacion+"&nacionalidad="+snacionalidad+"&residencia="+sresidencia+"&edad="+sedad+"&telefono="+stelefono+"&mail="+smail+"&profesion="+sprofesion+"&area="+sarea+"&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');
		}
	}
}
