function Inicio () {
	document.getElementById('orden').value = '';
	document.getElementById('es_listado').value = '1';
	document.getElementById('texto_buscar').value = '';
	document.getElementById('id_tipo_filtro').value = '';
	document.getElementById('select_filtro').selectedIndex = 0;
	document.getElementById('opcion').value = '';
	document.getElementById('pagina').value = '0';
	Listado();
}

function RecomendarSitio () {
	document.getElementById('div_paginado').style.display = 'none';
	ParseaAjax('post','/mascotas/php/recomendar_sitio.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
	return false;
}

function SubmiteRecomendarSitio () {
	if (document.getElementById('btn_recomendarsitio').disabled == true) {
		return false;
	}
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (document.getElementById('recomendar_nombre').value == '') {
		alert("Por favor ingresa tu nombre");
		return false;
	}

	if (document.getElementById('recomendar_emails').value == '') {
		alert("Ingresa al menos un e-mail");
		return false;
	}

	var emails = document.getElementById('recomendar_emails').value.split(",");
	var numero_campo = 0;
	var valores = '';
	if (emails.length > 7) {
		alert("Se puede recomendar a 6 destinatarios por vez.");
		return false;
	}
	while (numero_campo < emails.length) {
		if (!filter.test(emails[numero_campo])) {
			alert('La direccion ' + emails[numero_campo] + ' no es valida.');
			return false;
		}
		numero_campo += 1;
	}
	
	if (document.getElementById('recomendar_captcha').value == '') {
		alert("Por favor ingrese el texto del recuadro");
		return false;
	}
	
	//document.getElementById('nombre_recomendar').value = nombre;
	document.getElementById('btn_recomendarsitio').disabled = true;
	ParseaAjax('post','/mascotas/php/recomendar_sitio_submit.php','div_recomendar_info','div_recomendar_info','cargando_flor_chico','recomendar_nombre recomendar_emails recomendar_captcha');
	return false;
}

function Enlaces () {
	document.getElementById('div_paginado').style.display = 'none';
	ParseaAjax('post','/mascotas/php/enlaces.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
	return false;
}

function Contacto () {
	document.getElementById('div_paginado').style.display = 'none';
	ParseaAjax('post','/mascotas/php/contacto.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
	return false;
}

function SubmiteContacto () {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (document.getElementById('contacto_nombre').value == '') {
		alert("Por favor diganos su nombre");
		return false;
	}

	if (!filter.test(document.getElementById('contacto_email').value)) {
		alert("Por favor diganos un e-mail valido");
		return false;
	}
	
	if (document.getElementById('contacto_comentario').value == '') {
		alert("Por favor diganos un comentario");
		return false;
	}
	
	if (document.getElementById('contacto_captcha').value == '') {
		alert("Por favor ingrese el texto del recuadro");
		return false;
	}
	if (document.getElementById('btn_contacto').disabled == false) {
		document.getElementById('es_contacto').value = '1';
		ParseaAjax('post','/mascotas/php/contacto_submit.php','div_contacto_info','div_contacto_info','cargando_flor_chico','es_contacto contacto_nombre contacto_email contacto_comentario contacto_captcha contacto_motivo');
		return false;
	}
}

function Oferta (id_oferta) {
	window.open('/track.php?id_oferta='+ id_oferta);
	return false;
}

function BuscaGoogle (nombre) {
	window.open('http://www.google.com.ar/search?hl=es&q=mascota+' + nombre);
	return false;
}

function TuMascota () {
	document.getElementById('div_paginado').style.display = 'none';
	ParseaAjax('post','/mascotas/php/enviar_foto.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
	return false;
}

// Si entra con 1 modifica los datos por mas que no sea primera vez...
function MisMascotas (modifica_datos) {
	document.getElementById('div_paginado').style.display = 'none';
	document.getElementById('modifica_datos').value = modifica_datos;
	ParseaAjax('post','/mascotas/php/home_usuario.php','div_listado_cambia','div_listado_cambia','cargando_barra','modifica_datos');
	return false;
}

function Buscar () {
	if (document.getElementById('texto_busqueda').value == "") {
		alert("Por favor ingresa el nombre a buscar");
		return false;
	}
	document.getElementById('texto_buscar').value = document.getElementById('texto_busqueda').value;
	ParseaAjax('post','/mascotas/php/buscar.php','div_listado_cambia','div_listado_cambia','cargando_barra','texto_buscar');
	return false;
}

function RotaPublicidad () {
	if (document.getElementById('div_publicidad_superior') && document.getElementById('div_publicidad_inferior')) {
		document.getElementById('seteo_intervalo').value = '0';
		ParseaAjax('post','/mascotas/php/publicidad.php','div_publicidad_superior','div_publicidad_superior','cargando_publicidad','');
		ParseaAjax('post','/mascotas/php/publicidad.php','div_publicidad_inferior','div_publicidad_inferior','cargando_publicidad','');
	} else {
		document.getElementById('seteo_intervalo').value = '1';
		if (typeof(intervalo) != "undefined") {
			window.clearInterval(intervalo);
		}
	}
	return false;
}

function Ordenar (orden) {
	document.getElementById('orden').value = orden;
	document.getElementById('pagina').value = 0;
	if (document.getElementById('es_listado').value == 1) {
		if (orden == '3') {
			document.getElementById('div_paginado').style.display = 'none';
			document.getElementById('id_tipo_filtro').value = '';
			document.getElementById('texto_buscar').value = '';
		} else {
			document.getElementById('div_paginado').style.display = 'block';
		}
		Cambiar();
	} else {
		Cambiar("next");
	}
	return false;
}

function Mascota (id_mascota) {
	document.getElementById('id_mascota').value = id_mascota;
	document.getElementById('es_listado').value = '0';
	document.getElementById('div_paginado').style.display = 'block';
	ParseaAjax('post','/mascotas/php/mascotas.php','div_listado_cambia','div_listado_cambia','cargando_barra','id_mascota');
	return false;
}

function Votar () {
	document.getElementById('valor_voto').value = document.getElementById('valor_voto_select').value;
	ParseaAjax('post','/mascotas/php/votar.php','div_avisos_cambia','div_avisos_cambia','cargando_flor_chico','id_mascota valor_voto');
	return false;
}

function RecomendarMascota () {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (document.getElementById('emails_recomendar').value == '') {
		alert("Ingresa al menos un e-mail");
		return false;
	}

	var emails = document.getElementById('emails_recomendar').value.split(",");
	var numero_campo = 0;
	var valores = '';
	if (emails.length > 7) {
		alert("Se puede recomendar a 6 destinatarios por vez.");
		return false;
	}
	while (numero_campo < emails.length) {
		if (!filter.test(emails[numero_campo])) {
			alert('La direccion ' + emails[numero_campo] + ' no es valida.');
			return false;
		}
		numero_campo += 1;
	}
	
	//document.getElementById('nombre_recomendar').value = nombre;
	document.getElementById('btn_recomendarmascota').disabled = true;
	ParseaAjax('post','/mascotas/php/recomendar_mascota.php','div_avisos_cambia','div_avisos_cambia','cargando_flor_chico','id_mascota emails_recomendar nombre_mascota_recomendar');
	return false;
}

function Listado () {
	document.getElementById('div_paginado').style.display = 'block';
	document.getElementById('es_listado').value = '1';
	document.getElementById('id_mascota').value = '';
	if ((document.getElementById('pagina').value - 1) < 0) {
		document.getElementById('pagina').value = 0;
	} else {
		document.getElementById('pagina').value = document.getElementById('pagina').value - 1;
	}
	ParseaAjax('post','/mascotas/php/listado.php','div_listado_cambia','div_listado_cambia','cargando_barra','opcion id_tipo_filtro orden pagina texto_buscar');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
}

function Cambiar (opcion) {
	if (document.getElementById('pausar').value == '0') {
		document.getElementById('pausar').value = 1;
	} else {
		alert("Aguarda a que cargue todo el contenido");
		return false;
	}
	document.getElementById('opcion').value = opcion;
	if (document.getElementById('es_listado').value == 1) {
		ParseaAjax('post','/mascotas/php/listado.php','div_listado_cambia','div_listado_cambia','cargando_barra','opcion id_tipo_filtro orden pagina texto_buscar');
	} else {
		ParseaAjax('post','/mascotas/php/mascotas.php','div_listado_cambia','div_listado_cambia','cargando_barra','id_mascota opcion id_tipo_filtro orden texto_buscar');
	}
	return false;
}

function CambiarComm (opcion) {
	document.getElementById('opcion_comm').value = opcion;
	ParseaAjax('post','/mascotas/php/comentarios.php','div_listado_cambia','div_listado_cambia','cargando_barra','opcion_comm pagina_comm id_mascota');
	return false;
}

function Comentarios () {
	document.getElementById('div_paginado').style.display = 'none';
	document.getElementById('pagina_comm').value = '0';
	ParseaAjax('post','/mascotas/php/comentarios.php','div_listado_cambia','div_listado_cambia','cargando_barra','id_mascota');
	return false;
}

function Comentar () {
	ParseaAjax('post','/mascotas/php/comentar.php','div_comentarios_cambia','div_comentarios_cambia','cargando_comentarios','id_mascota');
	return false;
}

function AgregarComentario () {
	document.getElementById('comentario').value = document.getElementById('comentario_falso').value;
	if (document.getElementById('comentario').value.length > 280) {
		alert("El comentario no debe superar los 280 caracteres");
		return false;
	}
	ParseaAjax('post','/mascotas/php/agregar_comentario.php','div_comentarios_cambia','div_comentarios_cambia','cargando_comentarios','id_mascota comentario');
	return false;
}

function setFiltro (id_tipo_filtro) {
	document.getElementById('id_tipo_filtro').value = id_tipo_filtro;
	document.getElementById('pagina').value = '0';
	if (document.getElementById('es_listado').value == 1) {
		Cambiar();
	} else {
		Cambiar("next");
	}
	return false;
}

function PonerOlvido () {
	document.getElementById('pongo_olvido').value = 1;
	document.getElementById('es_olvido').value = 0;
	ParseaAjax('post','/mascotas/php/olvido.php','div_info_cambia','div_info_cambia','cargando_2','pongo_olvido');
	return false;
}

function Olvido () {
	if (document.getElementById('es_olvido').value == 1) {
		alert("Te hemos enviado instrucciones a tu correo -> " + document.getElementById('email_olvido').value);
		return false;
	}
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(document.getElementById('email_olvido').value)) {
		alert("Por favor introduzca un e-mail valido");
		return false;
	}
	
	if (document.getElementById('captcha_olvido').value == "") {
		alert("Por favor ingresa el texto de la imagen");
		return false;
	}

	document.getElementById('pongo_olvido').value = 0;
	ParseaAjax('post','/mascotas/php/olvido.php','div_logueo_cambia','div_logueo_cambia','cargando_login','pongo_olvido email_olvido captcha_olvido');
	return false;
}

function ModificaDatos () {
	if (document.getElementById('nombre_usuario_falso').value == "") {
		alert("Por favor ingresa tu nombre");
		return false;
	}
	document.getElementById('nombre_usuario').value = document.getElementById('nombre_usuario_falso').value;
	document.getElementById('apellido_usuario').value = document.getElementById('apellido_usuario_falso').value;
	document.getElementById('check_novedades').value = "";
	document.getElementById('check_ofertas').value = "";
	if (document.getElementById('check_novedades_falso').checked) {
		document.getElementById('check_novedades').value = "on";
	}
	if (document.getElementById('check_ofertas_falso').checked) {
		document.getElementById('check_ofertas').value = "on";
	}
	ParseaAjax('post','/mascotas/php/modificar_datos.php','div_datos_cambia','div_datos_cambia','cargando_comentarios','nombre_usuario apellido_usuario check_novedades check_ofertas');
	return false;
}

function CambiaPassword () {
	document.getElementById('div_paginado').style.display = 'none';
		ParseaAjax('post','/mascotas/php/cambiar_password.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	ParseaAjax('post','/mascotas/php/menu_derecho.php','div_info_cambia','div_info_cambia','cargando_2','id_mascota');
	return false;
}

function SubmiteCambiaPassword () {
	if (document.getElementById('pass_actual').value == '') {
		alert("Debes ingresar tu password actual");
		return false;
	}
	if (document.getElementById('pass_nuevaa').value == '') {
		alert("Debes ingresar tu nueva password");
		return false;
	}
	if (document.getElementById('pass_nuevab').value == '') {
		alert("Debes repetir tu nueva password");
		return false;
	}
	if (document.getElementById('pass_nuevaa').value != document.getElementById('pass_nuevab').value) {
		alert("Las nuevas password no coinciden");
		return false;
	}
	ParseaAjax('post','/mascotas/php/cambiar_password_submit.php','div_cambiapass_info','div_cambiapass_info','cargando_flor_chico','pass_actual pass_nuevaa pass_nuevab');
	return false;
}

function SubmiteRegistro() {
	if (document.getElementById('es_registro').value == 1) {
		alert("Te hemos enviado instrucciones a tu correo -> " + document.getElementById('registro_email').value);
		return false;
	}

	if (document.getElementById('registro_nombre').value == '') {
		alert("Por favor ingresa tu nombre");
		return false;
	}

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(document.getElementById('registro_email').value)) {
		alert("Por favor diganos un e-mail valido");
		return false;
	}

	if (document.getElementById('registro_password').value == '') {
		alert("Por favor ingrese una contraseņa");
		return false;
	}
	
	ParseaAjax('post','/mascotas/php/registro_submit.php','div_logueo_cambia','div_logueo_cambia','cargando_login','registro_nombre registro_email registro_password');
	return false;
}

function PonerRegistro() {
	document.getElementById('pongo_registro').value = 1;
	ParseaAjax('post','/mascotas/php/registro.php','div_info_cambia','div_info_cambia','cargando_2','pongo_registro');
}

function Logueo() {
	document.getElementById('setear_cookie').value = "";
	if (document.getElementById('setear_cookie_falso').checked) {
		document.getElementById('setear_cookie').value = "on";
	}
	ParseaAjax('post','/mascotas/php/login.php','div_logueo_cambia','div_logueo_cambia','cargando_login','usuario password id_mascota setear_cookie');
	return false;
}

function PonerLogueo(accion) {
	document.getElementById('pongo_logueo').value = 1;
	document.getElementById('accion').value = accion;
	//document.getElementById('setear_cookie').value = "";
	//if (document.getElementById('setear_cookie_falso').checked) {
	//	document.getElementById('setear_cookie').value = "on";
	//}
	ParseaAjax('post','/mascotas/php/login.php','div_info_cambia','div_info_cambia','cargando_2','pongo_logueo accion id_mascota setear_cookie');
}

function Desloguear () {
	ParseaAjax('post','/mascotas/php/desloguear.php','div_encabezado_cambia','div_encabezado_cambia','cargando_encabezado','');
	return false;
}

function CambiarPagina (destino) {
	window.location.href = destino;
}

function SubmitirEnter(e,accion) {
var keycode;
if (window.event)
	keycode = window.event.keyCode;
else if (e)
	keycode = e.which;
else
	return true;

	if (keycode == 13) {
		if (accion == "login") {
			Logueo();
		} else if (accion == "buscar") {
			Buscar();
		} else if (accion == "contacto") {
			SubmiteContacto();
		} else if (accion == "registro") {
			SubmiteRegistro();
		} else if (accion == "olvido") {
			Olvido();
		} else if (accion == "recomendar_sitio") {
			SubmiteRecomendarSitio();
		} else if (accion == "cambiar_password") {
			SubmiteCambiaPassword();
		}
		return false;
	} else {
		return true;
	}
}


function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function EstaLogueado () {
	id_usuario	= document.getElementById('cookie_id_usuario').value;
	password	= document.getElementById('cookie_password').value;
	if (getCookie("cookie_id_usuario") == id_usuario && getCookie("cookie_password") == password)
		return true;
	else
		return false;
}

function CuentaChars(comentarios, contador) {
	var txtComentarios = document.getElementById(comentarios);
	var txtContador = document.getElementById(contador);
	if (txtComentarios.value.length > 280) {
		alert("El comentario debe tener maximo 280 caracteres");
		txtComentarios.value = txtComentarios.value.substring(0, 280);
		return false;
	} else {
		txtContador.value = 280 - txtComentarios.value.length;
		return false;
	}
}

function Famosas () {
	ParseaAjax('post','/mascotas/php/famosas.php','div_listado_cambia','div_listado_cambia','cargando_barra','');
	return false;
}

function DetalleFamosa (id_mascota) {
	document.getElementById('id_mascota').value = id_mascota;
	ParseaAjax('post','/mascotas/php/famosas.php','div_listado_cambia','div_listado_cambia','cargando_barra','id_mascota');
	return false;
}
