// JavaScript Document


//validad que en el formulario de acceso rellena los compos
function validar_accesoUser()
{
	var Ok = false;
	if(document.getElementById("txtAccederUsuario").value == "" || document.getElementById("txtAccederUsuario").value == "direcci&oacute;n de email" || document.getElementById("txtAccederPass").value == "")
	{
		MensajeShadow("Debe indicar Usuario y Clave", "");
		return false;
	}
	else
	{
		if(document.getElementById("txtAccederUsuario").value != '') Ok = esMail(document.getElementById("txtAccederUsuario"));
		return Ok;
	}
}

function validar_olvidar_pass()
{
	var Ok = false;
	if(document.getElementById("txtEmail").value == "" ||  document.getElementById("txtCaptcha").value=="")
	{
		MensajeShadow("Debe rellenar todos los campos", "a0cfff");
		return false;
	}
	else
	{
		if(document.getElementById("txtEmail").value != '') Ok = esMail(document.getElementById("txtEmail"));
		return Ok;
	}
}

function ocul_most_datos_envio(capa)//oculta o muestra una capa
{
	if(capa.style.display == "block")
	{
		document.getElementById("rdDatosEnvio").value = "no";
		capa.style.display = "none";
	}
	else
	{
		document.getElementById("rdDatosEnvio").value = "si";
		capa.style.display = "block";
	}
}

function nueva_dir_envio()
{
	document.getElementById("txtTituloEnvio").innerHTML = "";
	if(document.getElementById('registro_datos_envio').style.display == 'block')
	{
		if(document.getElementById('envio_nombre').value == '' ||  document.getElementById("envio_apellidos").value=="" || 
				document.getElementById("envio_direccion").value=="" || document.getElementById("envio_localidad").value=="" || 
				document.getElementById("envio_provincia").value=="0" || document.getElementById("envio_cp").value=="")
		{
			ocul_most_datos_envio(document.getElementById('registro_datos_envio'));
			document.getElementById("registro_datos_envio_listado").style.display = "block";
		}else{
			valores_vacio_user_envio();
			document.getElementById("idEnvio").value = "0";
			document.getElementById("txtCadena").value = "";
			document.getElementById("registro_datos_envio_listado").style.display = "none";
		}
	}else
	{
		ocul_most_datos_envio(document.getElementById('registro_datos_envio'));
		valores_vacio_user_envio();
		document.getElementById("registro_datos_envio_listado").style.display = "none";
	}
}

function editar_dir_envio(cadena)
{
	var aCampos = new Array();
	aCampos = cadena.split("&$");
	document.getElementById("txtTituloEnvio").innerHTML = "Editar direcci\u00f3n de env\u00edo";
	document.getElementById('envio_nombre').value = aCampos[0];
	document.getElementById('envio_apellidos').value = aCampos[1];
	document.getElementById('envio_direccion').value = aCampos[2];
	document.getElementById('envio_localidad').value = aCampos[3];
	document.getElementById('envio_provincia').value = aCampos[4];
	document.getElementById('envio_cp').value = aCampos[5];
	document.getElementById('envio_pais').value = aCampos[6];
	var indice = (aCampos[7] == 1) ? true : false;
	document.getElementById('chkPredeterminada').checked = indice;
}

//cargar los datos para editar una direccion de envo de un usuario registrado:
function editar_registro_envio(id,tipo,ruta)
{
	var ajax;
	//alert("id "+id+" ruta"+ruta);
	ajax=objetoAjax();	//instanciamos el objetoAjax
	//usamos el medoto POST
	//archivo que realizar la operacion
	ajax.open("POST",ruta,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar resultados en esta capa
			if(tipo == "edit")
			{
				document.getElementById('txtCadena').value = unescape(ajax.responseText);
				if(document.getElementById('txtCadena').value != "")
				{
					editar_dir_envio(document.getElementById('txtCadena').value);
					if(document.getElementById('registro_datos_envio').style.display == 'none')
						ocul_most_datos_envio(document.getElementById('registro_datos_envio'));
					//document.getElementById("registro_datos_envio_listado").style.display = "none";
				}
			}
			else
			{
				unescape(ajax.responseText);
				window.location.reload();
			}
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send("id="+id+"&tipo="+tipo);
}

//comprueba que exista el usuario
function ConsultarUsuario(login,password,recordar,ruta,desdeShadow)
{
	var ajax;
	ajax=objetoAjax();	//instanciamos el objetoAjax
	//usamos el medoto POST
	//archivo que realizar la operacion
	ajax.open("POST",ruta,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar resultados en esta capa
			document.getElementById('userEncontrado').value = unescape(ajax.responseText);
			if(document.getElementById('userEncontrado').value != 0)
			{
				if(document.getElementById('userEncontrado').value == 2) MensajeShadow('Su CUENTA est\u00e1 INACTIVA, revise su correo o pongase en contacto con nosotros a trav\u00e9s del formulario de contacta.', "a0cfff");
				else
				{
					if(desdeShadow == true)
					{
						parent.window.location.reload();
						Cerrar_Shadow();
					}
					else window.location.reload();
				}
			}
			else olvideShadow('EMAIL o CLAVE incorrectos', "a0cfff");
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send("login="+login+"&password="+password+"&recordar=true");
}

//cierra la sesin del usuario
function CerrarUsuario(ruta)
{
	var ajax;
	ajax=objetoAjax();	//instanciamos el objetoAjax
	//usamos el medoto POST
	//archivo que realizar la operacion
	ajax.open("POST",ruta,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			unescape(ajax.responseText);
			window.location.reload();
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send(null);
}
