
$(window).load(function() {
	
	$('#js_links tr').click(function() {
		id = $(this).attr('id').substr(3);
		document.location.href = $('#lin' + id).attr('href');
	});
	
	$('#link_tips a').mouseover(loadTip);

	$('#busca_prod_btn').click(function() {
		$('#busca_prod').show();
		$('#busca_med').hide();
		return false;
	});

	$('#busca_med_btn').click(function() {
		$('#busca_prod').hide();
		$('#busca_med').show();
		return false;
	});

	$("#search").autocomplete("dir-ac.php", {
		width: 290,
		height: 300,
		max: 5,
		minChars: 2,
		delay: 600,
		matchContains: false,
		selectFirst: false,
		scrollHeight: 300,
		formatItem: function(data, i, n, value) {
			value = value.split("%@%");
			return "<img src='img/directorio/th/" + value[2] + "' align='left' width='60' height='60' /> " + value[0] + '<br />' + value[1];
		},
		formatResult: function(data, value) {
			return value.split("%@%")[0];
		}
	}).result(function(event, value) {
		value = ' ' + value;
		location.href = 'directorio/' + value.split("%@%")[3];
	});

	$("#search_prod").autocomplete("prod-ac.php", {
		width: 290,
		height: 300,
		max: 5,
		minChars: 2,
		delay: 600,
		matchContains: false,
		selectFirst: false,
		scrollHeight: 300,
		formatMatch: function(data, i, n, value) {
		 return value.split("%@%")[0];
		},
		formatItem: function(data, i, n, value) {
			value = value.split("%@%");
			return "<img src='img/productos/th/" + value[1] + "' align='left' width='60' height='60' /> " + value[0] +
						". Marca: " + value[3];
		},
		formatResult: function(data, value) {
			return value.split("%@%")[0];
		}
	}).result(function(event, value) {
		value = ' ' + value;
		location.href = 'productos/' + value.split("%@%")[2];
	});
	
	$('#busqueda').submit(function() 
	{
		base = $('base').attr('href');
		
		categoria = $('#categoria').val();
		search = $('#search').val().replace(' ','+');
		
		if(search != "")
		{
			if(categoria == 0)
			{
				categoria = 'todos';
			}
			
			location.href = base + 'directorio/buscar/' + search + '/' + categoria;
		}
		else if(categoria != 0)
		{
			location.href = base + 'directorio/categoria/' + categoria;
		}
		else
		{
			alert("Debe seleccionar una especialidad o escribir un texto de busqueda");
		}
		
		return false;
	});

	$('#busqueda_prod').submit(function()
	{
		base = $('base').attr('href');

		categoria = $('#categoria_prod').val();
		search = $('#search_prod').val().replace(' ','+');

		if(search != "")
		{
			if(categoria == 0)
			{
				categoria = 'todos';
			}

			location.href = base + 'productos/buscar/' + search + '/' + categoria;
		}
		else if(categoria != 0)
		{
			location.href = base + 'productos/categoria/' + categoria;
		}
		else
		{
			alert("Debe seleccionar una categoria o escribir un texto de busqueda");
		}

		return false;
	});
	
	$('.eliminar').click(function() {

		if(confirm('Esta seguro de que desea eliminar el registro seleccionado?')) {
			
			alert($(this).attr('href'));
			
			$('#del_item').val($(this).attr('href'));
			$('#del_form').submit();
		}
		return false;
	});
	
	$('#pedido_msg').hide();
	
	$('#agregar_a_pedido').submit(function() {
		
		id_prod = $('#id_prod').val();
		cant_prod = $('#cant_prod').val();
		
		if(cant_prod.search('[^0-9]') != -1)
		{
			alert("La cantidad solo debe contener numeros");
			return false;
		}
		
		$.post('pedidos/agregar', {id_prod: id_prod, cant_prod: cant_prod}, function(resul) {
			
			if(resul == 1) {
				$('#pedido_msg').fadeIn(1000, function() {
					setTimeout('fadeMsg()', 5000);
				});
			} else {
				alert('Datos no enviados, por favor intente de nuevo.');
			}
		
		});
		
		return false;
		
	});
	
	$('#contactar_vendedor').submit(function() {
		
		nombre = $('#nombre').val();
		email = $('#email').val();
		telefono = $('#telefono').val();
		comentarios = $('#comentarios').val();
		id_articulo = $('#id_articulo').val();
		
		if(nombre == "") alert('Debe completar el campo "nombre"');
		if(email == "") alert('Debe completar el campo "email"');
		if(comentarios == "") alert('Debe completar el campo "comentarios"');
		
		$.post('clasificados/contactar', 
			  {nombre: nombre, email: email, telefono: telefono, comentarios: comentarios, id_articulo: id_articulo},
			  function (data) {
				  
				if(data == 1) {
					$('#pedido_msg').fadeIn(1000, function() {
						
						$('#nombre').val("");
						$('#email').val("");
						$('#telefono').val("");
						$('#comentarios').val("");
						
						setTimeout('fadeMsg()', 5000);
					});
				} else {
					alert('Mensaje no enviado, por favor intente de nuevo.');
				}
				  
			  });
		
		return false;
		
	});

});

var back = true;

function volver()
{
	if(back)
	{
		back = false;
		history.back();
	}
	return false;
}

function fadeMsg() {
	$('#pedido_msg').fadeOut(1000);
}

function loadTip()
{
	$('.purplebox div span').html('<p>Cargando...</p>');

	$.post('home/tip/' + $(this).text(), null, function(html) {
		$('.purplebox div span').hide();
		$('.purplebox div span').html(html).slideDown(200);
	});
	
	return false;
}


function validarUsuario() {
	usuario = document.getElementById('usuario');
	contrasena = document.getElementById('contrasena');
	
	if((usuario.value == "") || (contrasena.value == "")) {
		alert("Debe completar los campos usuario y contrasena");
		return false;
	}

}

function isRadioChecked(name) {
	result = false;
	radios = document.getElementsByName(name);
	nombre = i;
	n = radios.length;
	for(var i = 0; i < n; i++) {
		if(radios[i].checked) {
	  	result = radios[i].value;
	    break;
	  }
	}
	return result;
}
