/* Eventos */

function iniciar(evento)
{
	_apellido = document.getElementById("apellido");
	_nombre = document.getElementById("nombre");
	_email = document.getElementById("email");
	_pais = document.getElementById("pais");
	_provincia = document.getElementById("provincia");
	_ciudad = document.getElementById("ciudad");
	_form = document.getElementById("formNewsletter");
	
	padreProvincia = _provincia.parentNode;
	http = getObjetoHTTP();
	
	setProvincia(18);
}

function errorReserva(elemento, mensajeError)
{
	alert(mensajeError);
	elemento.focus();
	return false;
}

function validar(evento)
{
	if (_nombre.value == "")
		return errorReserva(_nombre, "El nombre no puede estar vac\u00EDo.");
	else if (_apellido.value == "")
		return errorReserva(_apellido, "El apellido no puede estar vac\u00EDo.");
	else if (_provincia.value == "")
		return errorReserva(_provincia, "La provincia no puede estar vac\u00EDa.");
	else if (_ciudad.value == "")
		return errorReserva(_ciudad, "La ciudad no puede estar vac\u00EDa.");
	else if (_email.value == "")
		return errorReserva(_email, "El email no puede estar vac\u00EDo.");
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_email.value)))
		return errorReserva(_email, "El email no es v\u00E1lido.");
	else
		return true;
}

function actualizarProvincias(evento)
{
	setProvincia(_pais.value);
}


/* Funciones */
function remover(elemento)
{
	while (elemento.hasChildNodes())
		elemento.removeChild(elemento.firstChild);
	
	elemento.parentNode.removeChild(elemento);
}

function crearImputProvincia()
{
	var hijo = document.createElement("input");
	
	
	hijo.setAttribute("type", "text");
	hijo.setAttribute("id", "provincia");
	hijo.setAttribute("name", "provincia");
	hijo.setAttribute("maxlength", 200);
	hijo.setAttribute("size", 59);
	
	remover(_provincia);
	padreProvincia.appendChild(hijo);
	_provincia = hijo;
}

function crearSelectProvincia()
{
	var hijo = document.createElement("select");
	
	
	hijo.setAttribute("id", "provincia");
	hijo.setAttribute("name", "provincia");
	
	remover(_provincia);
	padreProvincia.appendChild(hijo);
	_provincia = hijo;
}


/* AJAX */

function getObjetoHTTP()
{
	var xmlhttp;
	
	
	if (window.ActiveXObject)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else
		if (window.XMLHttpRequest)
			xmlhttp = new XMLHttpRequest();
	
	return xmlhttp;
}

function getRespuestaHTTP()
{
	var provincias;
	
	
	if (http.readyState == 4)
	{
		if (http.responseXML)
		{
			try
			{
				provincias = http.responseXML.getElementsByTagName("provinces")[0];
				
				if (provincias.hasChildNodes())
				{
					crearSelectProvincia();
					for (var x = 0; x < provincias.childNodes.length; x++)
					{
						nuevoOption = document.createElement("option");
						nuevoOption.setAttribute("value", provincias.childNodes[x].getAttribute("id"));
						nuevoOption.appendChild(document.createTextNode(provincias.childNodes[x].firstChild.nodeValue));
						_provincia.appendChild(nuevoOption);
					}
				}
				else
					crearImputProvincia();
			}
			catch(err)
			{
				crearImputProvincia();
			}
		}
	}
}

function setProvincia(idPais)
{
	http.open("GET", "/landings/newsletter/infoProvincias.php?idPais=" + idPais, true);
	http.onreadystatechange = getRespuestaHTTP;
	http.send(null);
}