// JavaScript Document
function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp=false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp=new XMLHttpRequest();
	}
	return xmlhttp;
}


function lTrim(sStr){
     while (sStr.charAt(0) == " ")
      sStr = sStr.substr(1, sStr.length - 1);
     return sStr;
    }

    function rTrim(sStr){
     while (sStr.charAt(sStr.length - 1) == " ")
      sStr = sStr.substr(0, sStr.length - 1);
     return sStr;
    }
 
    function allTrim(sStr){
     return rTrim(lTrim(sStr));
    }



function comprobar_anuncio(){
	//g=document.getElementById('Iview').contentWindow.document.body.innerHTML;
	
	correo_registrado=document.anuncio_publicacion.email_registrado.value;
	if (correo_registrado.indexOf('@')==-1 || correo_registrado.indexOf('.')==-1 || correo_registrado.length<5){
		alert ("Introduzca una dirección de correo válida");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	clave_registrado=document.anuncio_publicacion.clave_registrado.value;
	if (clave_registrado==""){
		alert ("Introduce una clave");
		document.anuncio_publicacion.clave_registrado.focus();
		return (false);
	}
	
	registrado_o_no=document.getElementById('jota').innerHTML;
	if (registrado_o_no=="Usuario no registrado"){
		alert("Tus datos de registro no son correctos. Por favor, introdúzcalos de nuevo");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	categoria=document.anuncio_publicacion.categoria_b.value;
	if (categoria=="0"){
		alert("Debes escoger una categoría para tu anuncio");
		document.anuncio_publicacion.categoria_b.focus();
		return (false);
	}
	
	subcategoria=document.anuncio_publicacion.subcategoria_b.value;
	if (subcategoria=="0"){
		alert ("Debes escoger una subcategoría para tu anuncio");
		document.anuncio_publicacion.subcategoria_b.focus();
		return (false);
	}
	
	provincia=document.anuncio_publicacion.provincia_publi.value;
	if (provincia=="0"){
		alert ("Debes elegir una provincia");
		document.anuncio_publicacion.provincia_publi.focus();
		return (false);
	}
	
	localidad=document.anuncio_publicacion.localidad.value;
	local=allTrim(localidad);
	if (local==""){
		alert ("Debes introducir una localidad");
		document.anuncio_publicacion.localidad.focus();
		return (false);
	}
	
	titulo=document.anuncio_publicacion.titulo_mensaje.value;
	ti=allTrim(titulo);
	if (ti=="" || ti.lenght<5){
		alert ("Debes escribir un título (al menos 5 caracteres)");
		document.anuncio_publicacion.titulo_mensaje.focus();
		return (false);
	}
	
	preci=document.anuncio_publicacion.precio_mensaje.value;	
			
	var ok="1234567890 .";
	var num=preci;
	var valido=true;
	for (i=0;i<num.length;i++){
 		n=num.charAt(i);
		for (j=0;j<ok.length;j++)
			if (n==ok.charAt(j))
			break;
		if (j==ok.length){
		valido=false;
		break;
		}
	}
	

	if (!valido){
		alert ("Sólo números en el campo PRECIO (utilice el punto (.) como separador de decimales)");
		document.anuncio_publicacion.precio_mensaje.focus();
		return (false);
	}

	contacto=document.anuncio_publicacion.contacto.value;
	cont=allTrim(contacto);
	if (cont=="" || cont.length<4){
		alert ("Debes indicar una persona de contacto (mínimo 4 caracteres)");
		document.anuncio_publicacion.contacto.focus();
		return (false);
	}
	
	tipo=document.anuncio_publicacion.tip.value;
	if (tipo=="0"){
		alert ("Debes escoger un tipo de anuncio");
		document.anuncio_publicacion.tip.focus();
		return (false);
	}
	
	if (!document.anuncio_publicacion.condiciones.checked){
		alert ("Debes aceptar las condiciones");
		document.anuncio_publicacion.condiciones.focus();
		return (false);
	}
	setTimeout("descripcion()",100);
	return false;
}
function descripcion(){
	g=document.anuncio_publicacion.cuerpecillo.value;
	p=allTrim(g);
	if (p==""){
		alert("Debes escribir una descripción para tu anuncio");
		return false;
	}
	
	document['anuncio_publicacion'].submit();
}
		



function comprobar_registro(a,b){
var contenedor;
	contenedor=document.getElementById('regis');
	ajax=nuevoAjax();
	ajax.open("GET","../scripts/comprobar.php?c="+a+"&b="+b, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==3){
			contenedor.innerHTML='Cargando';
		} else if (ajax.readyState==4){
			contenedor.innerHTML=ajax.responseText;
		}
	}
	ajax.send(null)
	
}
function comprobar_imagen(d){
var contenedor;
	contenedor=document.getElementById('formato');
	ajax=nuevoAjax();
	ajax.open("GET","../scripts/comprobar_imagen.php?c="+d, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==3){
			contenedor.innerHTML='Cargando';
		} else if (ajax.readyState==4){
			contenedor.innerHTML=ajax.responseText;
		}
	}
	ajax.send(null)
	
}
function cuanto(d){
var contenedor;
	contenedor=document.getElementById('cuanto_cuesta');
	ajax=nuevoAjax();
	ajax.open("GET","../scripts/cuanto.php?x="+d, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==3){
			contenedor.innerHTML='Cargando';
		} else if (ajax.readyState==4){
			contenedor.innerHTML=ajax.responseText;
		}
	}
	ajax.send(null)
	
}




function textcounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
else
countfield.value = maxlimit - field.value.length;
}

function textcount(field, countfield, maxlimit) {
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
else
countfield.value = maxlimit - field.value.length;
}
function contare(){
	valor1=document.form_editar.titulo_anuncio_edicion.value;
	valor2=document.form_editar.caracteres_anuncio_edicion.value;
	document.form_editar.caracteres_anuncio_edicion.value=65-valor1.length;
}
	
function comp_search(){
	category=document.formulario_busqueda.categoria_b.value;
	if (category==0){
		alert ("Debes escoger al menos una categoría");
		document.formulario_busqueda.categoria_b.focus();
		return (false);
	}
	 return (true);
}
function comprobar_edicion(){
	titulo=document.form_editar.titulo_anuncio_edicion.value;
	if (titulo==""){
		alert ("Debes introducir un título en tu anuncio");
		document.form_editar.titulo_anuncio_edicion.focus();
		return (false);
	}
	cuerpo=document.form_editar.cuerpo_editar.value;
	if (cuerpo==""){
		alert ("Debes introducir una descripción en el cuerpo de tu anuncio");
		document.form_editar.cuerpo_editar.focus();
		return false;
	}
	
	setTimeout("descripcion2()",100);
	return (false);
}
function descripcion2(){
	p=document.form_editar.cuerpo_editar.value;
	if (p==""){
		alert("Debes escribir una descripción para tu anuncio");
		return false;
	}
	
	document['form_editar'].submit();
}	
function terminar(ques){
	location.href="mostrar-anuncio.php?di="+ques+"&iq=0";
}

function comprobar_contacto(){
	nombre=document.form_contacto.nombre_contacto.value;
	if (nombre==""){
		alert ("Debes introducir un nombre");
		document.form_contacto.nombre_contacto.focus();
		return (false);
	}
	
	telefono=document.form_contacto.telefono_contacto.value;
	correo=document.form_contacto.correo_contacto.value;
	if (correo=="" && telefono==""){
		alert ("Debes introducir una forma de contacto, o bien electrónico o bien teléfono");
		document.form_contacto.telefono_contacto.focus();
		return (false);
	}
	
	asunto=document.form_contacto.asunto_contacto.value;
	if (asunto==""){
		alert ("Debes introducir un asunto");
		document.form_contacto.asunto_contacto.focus();
		return (false);
	}
	
	mensaje=document.form_contacto.mensaje_contacto.value;
	if (mensaje==""){
		alert ("Debes introducir un mensaje");
		document.form_contacto.mensaje_contacto.focus();
		return (false);
	}
	return true;
}

function comprobar_enlace(){
	correo=document.form_enlaces.correo_link.value;
	if (correo==""){
		alert ("Debes introducir un correo electrónico");
		document.form_enlaces.correo_link.focus();
		return (false);
	}
	if (correo.indexOf("@")==-1){
		alert ("Debes introducir un correo electrónico válido");
		document.form_enlaces.correo_link.focus();
		return (false);
	}
	
	titulo=document.form_enlaces.titulo_link.value;
	if (titulo==""){
		alert ("Debes introducir un título para el enlace");
		document.form_enlaces.titulo_link.focus();
		return (false);
	}
	
	enlace=document.form_enlaces.url_link.value;
	if (enlace==""){
		alert ("Debes introducir una URL válida");
		document.form_enlaces.url_link.focus();
		return (false);
	}
	if (enlace.slice(0,7)!="http://"){
		alert ("El formato de la URL debe comenzar por 'http://'");
		document.form_enlaces.url_link.focus();
		return (false);
	}
	
	cat=document.form_enlaces.categoria_link.value;
	if (cat=="0"){
		alert ("Debe escoger una categoría donde publicar el enlace");
		document.form_enlaces.categoria_link.focus();
		return (false);
	}
	
	d=document.form_enlaces.descrip_link.value;
	if (d==""){
		alert ("Debe introducir una descripción");
		document.form_enlaces.descrip_link.focus();
		return (false);
	}
	return true;
}


function cerrando(){	
	document.getElementById('procesando').style.visibility='visible';	
	location.href="scripts/grabar_anuncio.php";
}
function comprobar_amigo(){
	nombre=document.form_amigo.tu_nombre.value;
	if (nombre==""){
		alert ("Debes introducir un nombre para que tu amigo sepa quién le envía el anuncio");
		document.form_amigo.tu_nombre.focus();
		return (false);
	}
	correo=document.form_amigo.correo_amigo.value;
	if (correo==""){
		alert ("Debes introducir un correo electrónico");
		document.form_amigo.correo_amigo.focus();
		return (false);
	}
	if (correo.indexOf('@')==-1 || correo.indexOf('.')==-1 || correo.length<5){
		alert ("Introduce una dirección de correo válida");
		document.form_amigo.correo_amigo.focus();
		return (false);
	}
	return true;
}



function comprobar_anunci(){
	//g=document.getElementById('Iview').contentWindow.document.body.innerHTML;
	
	correo_registrado=document.anuncio_publicacion.email_registrado.value;
	if (correo_registrado.indexOf('@')==-1 || correo_registrado.indexOf('.')==-1 || correo_registrado.length<5){
		alert ("Introduzca una dirección de correo válida");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	clave_registrado=document.anuncio_publicacion.clave_registrado.value;
	if (clave_registrado==""){
		alert ("Introduce una clave");
		document.anuncio_publicacion.clave_registrado.focus();
		return (false);
	}
	
	registrado_o_no=document.getElementById('jota').innerHTML;
	if (registrado_o_no=="Usuario no registrado"){
		alert("Tus datos de registro no son correctos. Por favor, introdúzcalos de nuevo");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	categoria=document.anuncio_publicacion.categoria_b.value;
	if (categoria=="0"){
		alert("Debes escoger una categoría para tu anuncio");
		document.anuncio_publicacion.categoria_b.focus();
		return (false);
	}
	
	subcategoria=document.anuncio_publicacion.subcategoria_b.value;
	if (subcategoria=="0"){
		alert ("Debes escoger una subcategoría para tu anuncio");
		document.anuncio_publicacion.subcategoria_b.focus();
		return (false);
	}
	
	provincia=document.anuncio_publicacion.provincia_publi.value;
	if (provincia=="0"){
		alert ("Debes elegir una provincia");
		document.anuncio_publicacion.provincia_publi.focus();
		return (false);
	}
	
	localidad=document.anuncio_publicacion.localidad.value;
	local=allTrim(localidad);
	if (local==""){
		alert ("Debes introducir una localidad");
		document.anuncio_publicacion.localidad.focus();
		return (false);
	}
	
	titulo=document.anuncio_publicacion.titulo_mensaje.value;
	
	
	ti=allTrim(titulo);
	if (ti=="" || ti.lenght<5){
		alert ("Debes escribir un título (al menos 5 caracteres)");
		document.anuncio_publicacion.titulo_mensaje.focus();
		return (false);
	}
	
	cuerpo=document.anuncio_publicacion.cuerpecillo.value;
	bo=allTrim(cuerpo);
	if (bo==""){
		alert ("Debes escribir algún contenido en el cuerpo del anuncio");
		document.anuncio_publicacion.cuerpecillo.focus();
		return (false);
	}
	
	preci=document.anuncio_publicacion.precio_mensaje.value;	
			
	var ok="1234567890 .";
	var num=preci;
	var valido=true;
	for (i=0;i<num.length;i++){
 		n=num.charAt(i);
		for (j=0;j<ok.length;j++)
			if (n==ok.charAt(j))
			break;
		if (j==ok.length){
		valido=false;
		break;
		}
	}
	

	if (!valido){
		alert ("Sólo números en el campo PRECIO (utilice el punto (.) como separador de decimales)");
		document.anuncio_publicacion.precio_mensaje.focus();
		return (false);
	}

	contacto=document.anuncio_publicacion.contacto.value;
	cont=allTrim(contacto);
	if (cont=="" || cont.length<4){
		alert ("Debes indicar una persona de contacto (mínimo 4 caracteres)");
		document.anuncio_publicacion.contacto.focus();
		return (false);
	}
	
	tipo=document.anuncio_publicacion.tip.value;
	if (tipo=="0"){
		alert ("Debes escoger un tipo de anuncio");
		document.anuncio_publicacion.tip.focus();
		return (false);
	}
	
	if (!document.anuncio_publicacion.condiciones.checked){
		alert ("Debes aceptar las condiciones");
		document.anuncio_publicacion.condiciones.focus();
		return (false);
	}
	return true;
}


function comprobar_anunc(){
	//g=document.getElementById('Iview').contentWindow.document.body.innerHTML;
	
	correo_registrado=document.anuncio_publicacion.email_registrado.value;
	if (correo_registrado.indexOf('@')==-1 || correo_registrado.indexOf('.')==-1 || correo_registrado.length<5){
		alert ("Introduzca una dirección de correo válida");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	clave_registrado=document.anuncio_publicacion.clave_registrado.value;
	if (clave_registrado==""){
		alert ("Introduce una clave");
		document.anuncio_publicacion.clave_registrado.focus();
		return (false);
	}
	
	registrado_o_no=document.getElementById('jota').innerHTML;
	if (registrado_o_no=="Usuario no registrado"){
		alert("Tus datos de registro no son correctos. Por favor, introdúzcalos de nuevo");
		document.anuncio_publicacion.email_registrado.focus();
		return (false);
	}
	
	categoria=document.anuncio_publicacion.categoria_b.value;
	if (categoria=="0"){
		alert("Debes escoger una categoría para tu anuncio");
		document.anuncio_publicacion.categoria_b.focus();
		return (false);
	}
	
	subcategoria=document.anuncio_publicacion.subcategoria_b.value;
	if (subcategoria=="0"){
		alert ("Debes escoger una subcategoría para tu anuncio");
		document.anuncio_publicacion.subcategoria_b.focus();
		return (false);
	}
	
	provincia=document.anuncio_publicacion.provincia_publi.value;
	if (provincia=="0"){
		alert ("Debes elegir una provincia");
		document.anuncio_publicacion.provincia_publi.focus();
		return (false);
	}
	
	localidad=document.anuncio_publicacion.localidad.value;
	local=allTrim(localidad);
	if (local==""){
		alert ("Debes introducir una localidad");
		document.anuncio_publicacion.localidad.focus();
		return (false);
	}
	
	titulo=document.anuncio_publicacion.titulo_mensaje.value;
	
	
	ti=allTrim(titulo);
	if (ti=="" || ti.lenght<5){
		alert ("Debes escribir un título (al menos 5 caracteres)");
		document.anuncio_publicacion.titulo_mensaje.focus();
		return (false);
	}
	var okey="0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZ abcdefghijklmnñopqrstuvwxyzÁÉÍÓÚáéíóú.,!¡?¿";
	var nu=titulo;
	var valid=true;
	for (i=0;i<nu.length;i++){
 		n=nu.charAt(i);
		for (j=0;j<okey.length;j++)
			if (n==okey.charAt(j))
			break;
		if (j==okey.length){
		valid=false;
		break;
		}
	}
	

	if (!valid){
		alert ("No puedes introducir caracteres especiales en el título del anuncio (permitidos: a-z,A-Z,0-9 letras acentuadas, comas, puntos y signos de interrogación y de exclamación)");
		document.anuncio_publicacion.titulo_mensaje.focus();
		return (false);
	}
	
	cuerpo=document.anuncio_publicacion.cuerpecillo.value;
	bo=allTrim(cuerpo);
	if (bo==""){
		alert ("Debes escribir algún contenido en el cuerpo del anuncio");
		document.anuncio_publicacion.cuerpecillo.focus();
		return (false);
	}
	
	preci=document.anuncio_publicacion.precio_mensaje.value;	
			
	var ok="1234567890 .";
	var num=preci;
	var valido=true;
	for (i=0;i<num.length;i++){
 		n=num.charAt(i);
		for (j=0;j<ok.length;j++)
			if (n==ok.charAt(j))
			break;
		if (j==ok.length){
		valido=false;
		break;
		}
	}
	

	if (!valido){
		alert ("Sólo números en el campo PRECIO (utilice el punto (.) como separador de decimales)");
		document.anuncio_publicacion.precio_mensaje.focus();
		return (false);
	}

	contacto=document.anuncio_publicacion.contacto.value;
	cont=allTrim(contacto);
	if (cont=="" || cont.length<4){
		alert ("Debes indicar una persona de contacto (mínimo 4 caracteres)");
		document.anuncio_publicacion.contacto.focus();
		return (false);
	}
	
	tipo=document.anuncio_publicacion.tip.value;
	if (tipo=="0"){
		alert ("Debes escoger un tipo de anuncio");
		document.anuncio_publicacion.tip.focus();
		return (false);
	}
	
	if (!document.anuncio_publicacion.condiciones.checked){
		alert ("Debes aceptar las condiciones");
		document.anuncio_publicacion.condiciones.focus();
		return (false);
	}
	
	bot=document.form_editar.antibot.value;
	
	if (bot.length<5 || bot==""){
		alert ("Debes introducir los caracteres de la imagen");
		document.form_editar.antibot.focus();
		return (false);
	}
	
	return true;
}



function comprobar_edicio(){
	titulo=document.form_editar.titulo_anuncio_edicion.value;
	ti=allTrim(titulo);
	if (ti==""){
		alert ("Debes introducir un título en tu anuncio");
		document.form_editar.titulo_anuncio_edicion.focus();
		return (false);
	}
	cuerpo=document.form_editar.cuerpo_editar.value;
	corp=allTrim(cuerpo);
	if (corp==""){
		alert ("Debes introducir una descripción en el cuerpo de tu anuncio");
		document.form_editar.cuerpo_editar.focus();
		return false;
	}
	
	
	
	return true;
}