// JavaScript Document


//ONLOAD
$(document).ready(function() {
	
	$('input.in').focus(function() {
								 $(this).addClass("inFocus");
								 });  
	$('input.in').blur(function() {
								 $(this).removeClass("inFocus");
								 }); 
});
//FIN ONLOAD

//ELIMINAR PRODUCTO

function eliminarProducto(id)
{
	if(confirm("Esta seguro que desea Eliminar este Producto?"))
	{
		window.document.location.href="eliminar_producto.php?id=" + id;
	}
}

function eliminarPregunta(id)
{
	if(confirm("Esta seguro que desea Eliminar esta Pregunta?"))
	{
		window.document.location.href="eliminar_pregunta.php?id=" + id;
	}
}
function eliminarRespuesta(id)
{
	if(confirm("Esta seguro que desea Eliminar esta Respuesta?"))
	{
		window.document.location.href="eliminar_respuesta.php?id=" + id;
	}
}
function eliminarUsuario(id)
{
	if(confirm("Esta seguro que desea Eliminar este Usuario?"))
	{
		window.document.location.href="eliminar_usuario.php?id=" + id;
	}
}
//CONTACTO

function enviarContacto()
{
	var error = 0;
	
	var nombre = $("#nombre").val();
	var fono = $("#fono").val();
	var email = $("#email").val();
	var mensaje = $("#mensaje").val();
	
	//$("#resp").slideUp("slow");
	
	if( requerido(nombre) )
	{
		$("#contacto_res").html("<span class='error'>Ingrese su Nombre</span>");
		$("#contacto_res").slideDown("slow");
		$("#nombre").focus();
		error++;
		return false;
	}
	if( requerido(fono) )
	{
		$("#contacto_res").html("<span class='error'>Ingrese su Fono</span>");
		$("#contacto_res").slideDown("slow");
		$("#fono").focus();
		error++;
		return false;
	}
	if( requerido(email) )
	{
		$("#contacto_res").html("<span class='error'>Ingrese su Correo Electrónico</span>");
		$("#contacto_res").slideDown("slow");
		$("#email").focus();
		error++;
		return false;
	}else{
		if( emailValido(email) )
		{
			$("#contacto_res").html("<span class='error'>Ingrese un Correo Electrónico Válido</span>");
			$("#contacto_res").slideDown("slow");
			$("#email").focus();
			error++;
			return false;
		}
	}
	if( requerido(mensaje) )
	{
		$("#contacto_res").html("<span class='error'>Ingrese su Mensaje</span>");
		$("#contacto_res").slideDown("slow");
		$("#mensaje").focus();
		error++;
		return false;
	}
	
	
	if(error==0)
	{
		$("#contacto_res").slideUp();
		$("#bot_upload").hide();
		$("#loading").show();
		
		$.ajax({
            type	: 'POST',
            url		: $("#form").attr('action'),
            data	: $("#form").serialize(),
            // Mostramos un mensaje con la respuesta de PHP
            success: function(data) {
                $('#loading').html(data);
            }
        })        
        return false;
		
	}
}
//=============================================================================================

//LOGIN

function validarLogin(f)
{
	var error = 0;
	
	var usuario = $("#usuario").val();
	var clave = $("#contrasena").val();
	
	//$("#resp").slideUp("slow");
	
	if( requerido(usuario) )
	{
		$("#login_res").html("<span class='error'>Ingrese su Usuario</span>");
		$("#login_res").slideDown("slow");
		$("#usuario").focus();
		error++;
		return false;
	}
	if( requerido(clave) )
	{
		$("#login_res").html("<span class='error'>Ingrese su Clave</span>");
		$("#login_res").slideDown("slow");
		$("#contrasena").focus();
		error++;
		return false;
	}
	
	
	if(error==0)
	{
		$("#login_res").slideUp();
		//$("#bot_upload").hide();
		//$("#loading").show();
		/*
		$.ajax({
            type	: 'POST',
            url		: $("#form").attr('action'),
            data	: $("#form").serialize(),
            // Mostramos un mensaje con la respuesta de PHP
            success: function(data) {
                $('#loading').html(data);
            }
        })
		*/
        return true;
		
	}
}
//=============================================================================================



//REGISTRO

function validarRegistro(f)
{
	var error = 0;
	
	var nombre = $("#nombre").val();
	var apellido = $("#apellido").val();
	var email = $("#email").val();
	var clave = $("#clave").val();
	var clave2 = $("#clave2").val();
	//$("#resp").slideUp("slow");
	
	if( requerido(nombre) )
	{
		$("#registro_res").html("<span class='error'>Ingrese su Nombre</span>");
		$("#registro_res").slideDown("slow");
		$("#nombre").focus();
		error++;
		return false;
	}
	if( requerido(apellido) )
	{
		$("#registro_res").html("<span class='error'>Ingrese su Apellido</span>");
		$("#registro_res").slideDown("slow");
		$("#apellido").focus();
		error++;
		return false;
	}
	
	if( requerido(email) )
	{
		$("#registro_res").html("<span class='error'>Ingrese su Correo Electrónico</span>");
		$("#registro_res").slideDown("slow");
		$("#email").focus();
		error++;
		return false;
	}else{
		if( emailValido(email) )
		{
			$("#registro_res").html("<span class='error'>Ingrese un Correo Electrónico Válido</span>");
			$("#registro_res").slideDown("slow");
			$("#email").focus();
			error++;
			return false;
		}
	}
	
	if( requerido(clave) )
	{
		$("#registro_res").html("<span class='error'>Ingrese su Clave</span>");
		$("#registro_res").slideDown("slow");
		$("#clave").focus();
		error++;
		return false;
	}
	if( clave.length < 4)
	{
		$("#registro_res").html("<span class='error'>La Clave debe contener más de 4 caracteres</span>");
		$("#regitro_res").slideDown("slow");
		$("#clave").focus();
		error++;
		return false;
	}
	if( clave!=clave2 )
	{
		$("#registro_res").html("<span class='error'>Las Claves no concuerdan</span>");
		$("#registro_res").slideDown("slow");
		$("#clave2").focus();
		error++;
		return false;
	}
	
	
	if(error==0)
	{
		$("#registro_res").slideUp();
		//$("#bot_upload").hide();
		//$("#loading").show();
		/*
		$.ajax({
            type	: 'POST',
            url		: $("#form").attr('action'),
            data	: $("#form").serialize(),
            // Mostramos un mensaje con la respuesta de PHP
            success: function(data) {
                $('#loading').html(data);
            }
        })
		*/
        return true;
		
	}
}
//=============================================================================================


//CUENTA

function validarCuenta(f)
{
	var error = 0;
	
	var nombre = $("#nombre").val();
	var apellido = $("#apellido").val();
	var email = $("#email").val();
	var clave = $("#clave").val();
	var clave1 = $("#clave1").val();
	var clave2 = $("#clave2").val();
	//$("#resp").slideUp("slow");
	
	if( requerido(nombre) )
	{
		$("#cuenta_res").html("<span class='error'>Ingrese su Nombre</span>");
		$("#cuenta_res").slideDown("slow");
		$("#nombre").focus();
		error++;
		return false;
	}
	if( requerido(apellido) )
	{
		$("#cuenta_res").html("<span class='error'>Ingrese su Apellido</span>");
		$("#cuenta_res").slideDown("slow");
		$("#apellido").focus();
		error++;
		return false;
	}
	
	if( requerido(email) )
	{
		$("#cuenta_res").html("<span class='error'>Ingrese su Correo Electrónico</span>");
		$("#cuenta_res").slideDown("slow");
		$("#email").focus();
		error++;
		return false;
	}else{
		if( emailValido(email) )
		{
			$("#cuenta_res").html("<span class='error'>Ingrese un Correo Electrónico Válido</span>");
			$("#cuenta_res").slideDown("slow");
			$("#email").focus();
			error++;
			return false;
		}
	}
	
	if( clave != "")
	{
		
			if( requerido(clave1) )
			{
				$("#cuenta_res").html("<span class='error'>Ingrese la Nueva Clave</span>");
				$("#cuenta_res").slideDown("slow");
				$("#clave").focus();
				error++;
				return false;
			}
			if( clave1.length < 4)
			{
				$("#cuenta_res").html("<span class='error'>La Nueva Clave debe contener más de 4 caracteres</span>");
				$("#cuenta_res").slideDown("slow");
				$("#clave").focus();
				error++;
				return false;
			}
			if( clave1!=clave2 )
			{
				$("#cuenta_res").html("<span class='error'>Las Claves no concuerdan</span>");
				$("#cuenta_res").slideDown("slow");
				$("#clave2").focus();
				error++;
				return false;
			}
			
	}
	
	if(error==0)
	{
		$("#cuenta_res").slideUp();
		//$("#bot_upload").hide();
		//$("#loading").show();
		/*
		$.ajax({
            type	: 'POST',
            url		: $("#form").attr('action'),
            data	: $("#form").serialize(),
            // Mostramos un mensaje con la respuesta de PHP
            success: function(data) {
                $('#loading').html(data);
            }
        })
		*/
        return true;
		
	}
}
//=============================================================================================


//PUBLICACION

function validarPublicacion(f)
{
	var error = 0;
	
	var titulo = $("#titulo").val();
	var precio = $("#precio").val();
	
	//$("#resp").slideUp("slow");
	
	if( requerido(titulo) )
	{
		$("#pub_res").html("<span class='error'>Ingrese el Título</span>");
		$("#pub_res").slideDown("slow");
		$("#titulo").focus();
		error++;
		return false;
	}
	if( requerido(precio) )
	{
		$("#pub_res").html("<span class='error'>Ingrese el Precio</span>");
		$("#pub_res").slideDown("slow");
		$("#precio").focus();
		error++;
		return false;
	}
	
	
	if(error==0)
	{
		$("#bot_upload").hide();
		$("#loading").show();
		
		$("#pub_res").slideUp();
		
		
        return true;
		
	}
}
//=============================================================================================


//SALIR
function salir(){
	if( confirm("¿ Está seguro que desea Salir del Sistema ?") ){
		parent.location.href="logout.php";
	}
}
//FIN SALIR

//=============================================================================================

//LOADING FORM
function loadingForm()
{
	//TODO OK
		$("#bot_upload").hide();
		$("#loading").show();
}

//=============================================================================================

//FUNCION ENVIAR FILTRO
function enviarFiltro(e,frm)
{
	tecla=(document.all) ? e.keyCode : e.which;
	if(tecla==13) frm.submit();
}
//FIN ENVIAR FILTRO

//=============================================================================================

//FUNCION VALIDAR AGREGAR IMAGENES
function agregarImagenes()
{
	$("#form_editar").hide();
	$("#form_upload").slideDown();
}
//FIN AGREGAR IMAGENES

//=============================================================================================

//FUNCION SELECCIONAR TODOS
function seleccionarTodos(sel)
{
	if(sel.checked == 1)
	{
		for (i=0;i<document.form.elements.length;i++)
		{
			if(document.form.elements[i].type == "checkbox" && document.form.elements[i].id == "centro[]")
			{
				document.form.elements[i].checked=1;
				document.getElementById('centros').value++;
			}
		}
	}else{
		for (i=0;i<document.form.elements.length;i++)
		{
			if(document.form.elements[i].type == "checkbox" && document.form.elements[i].id == "centro[]")
			{
				document.form.elements[i].checked = 0;
				document.getElementById('centros').value = 0;
			}
		}
	}

} 
//FIN SELECCIONAR TODOS

//=============================================================================================

//FUNCION VER CAMBIAR CLAVE
function cambiar_clave()
{
	$("#div_clave").hide();
	$("#div_clave_form").slideDown();
}
//FIN VER CAMBIAR CLAVE

//=============================================================================================

//FUNCION VALIDAR CONFIGURACION
function validarConfig(f)
{
	var error = 0;
	var mensaje = "";
	
	if( requerido(f.nombre.value) )
	{
		mensaje = "* Debe ingresar el Nombre\n";
		error++;
	}
	
	if( requerido(f.email.value) )
	{
		mensaje += "* Debe ingresar el Email\n";
		error++;
	}else{
	
		if( emailValido(f.email.value) )
		{
			mensaje += "* Debe ingresar un Email Válido\n";
			error++;
		}
	}
	
	if(error>0)
	{
		alert(mensaje);
		return false;
	}else{	
		//TODO OK
		return true;
	}
}
//FIN VALIDAR CONFIGURACION

//=============================================================================================

//FUNCION INPUT REQUERIDO
function requerido(text)
{
	if(text=="")
	{
		return true;
	}else{
		return false;
	}
}
//FIN REQUERIDO

//=============================================================================================

//FUNCION VALIDAR EMAIL
function emailValido(email)
{
	var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/  
	
	if(!(b.test(email))){
		return true;
	}else{
		return false;
	}
}
//FIN VALIDAR EMAIL

//=============================================================================================

//FUNCION INPUT SOLO NUMEROS
function soloNumeros(event) { 
    //tecla = (document.all) ? e.keyCode : e.which; 
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	//alert(tecla);
    
	if (tecla == 8) return true; 
	//if (tecla == 46) return true; 
    if (tecla == 9) return true;
	//patron =/[a-z0-9-]/;
	patron =/[0-9]/;
   
	te = String.fromCharCode(tecla);
    
	return patron.test(te); 
}
//FIN SOLO NUMERO

//=============================================================================================

//FUNCION VALIDAR NICK
function nick(event) { 
    //tecla = (document.all) ? e.keyCode : e.which; 
	var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	//alert(tecla);
    
	if (tecla == 8) return true; 
    
	patron =/[a-z0-9-]/;
	//patron =/[0-9]/;
   
	te = String.fromCharCode(tecla);
    
	return patron.test(te); 
}
//FIN VALIDAR NICK


//=============================================================================================

//FUNCION CONVERSION
function conversion()
{
	var valor = $("#de_in").val();
	var de = $("#de").val();
	var a = $("#a").val();

	$("#resultado").load("conversion.php?valor=" + valor + "&de=" + de + "&a=" + a);
}
//FIN CONVERSION