// JavaScript Document
$(document).ready(function(){

	
	$('.clearField').clearField({
		blurClass: 'myBlurredClass',
		activeClass: 'myActiveClass'
	});	
	$('#pane').jScrollPane({scrollbarWidth:20, scrollbarMargin:10});
	
	$("#menu li a").wrapInner( '<span class="out"></span>' ).append( '<span class="bg"></span>' );
	$("#menu li a").each(function() {
		$( '<span class="over"><img src="' +  $(this).find("img").attr("src") + '"/></span>' ).appendTo( this );
		
		
		if($(this).attr("id") == 'active'){
			$(".out",	this).stop().animate({'top':	'37px'},	250); // move down - hide
			$(".over",	this).stop().animate({'top':	'37px'},		250); // move down - show
			$(".bg",	this).stop().animate({'top':	'0px'},		120); // move down - show
		}
	});
	$("#menu li a").hover(function() {
		$(".out",	this).stop().animate({'top':	'37px'},	250); // move down - hide
		$(".over",	this).stop().animate({'top':	'37px'},		250); // move down - show
		$(".bg",	this).stop().animate({'top':	'0px'},		120); // move down - show
	}, function() {
		if($(this).attr("id") != 'active'){
			$(".out",	this).stop().animate({'top':	'37px'},		250); // move up - show
			$(".over",	this).stop().animate({'top':	'-71px'},	250); // move up - hide
			$(".bg",	this).stop().animate({'top':	'-71px'},	120); // move up - hide
		}
	});
	
	$("#tipo_cliente,#tipo_consulta,#lst_distrito_o,#lst_distrito_f,#distrito, #tipo_servicio, .dorigen, .ddestino, .radio").uniform();

	$("#calcular_tarifa").click(function(){
		if($(".dorigen:checked").val() == undefined){
			alert("Seleccione un origen de las opciones");	
			return false;
		}else if($(".ddestino:checked").val() == undefined){
			alert("Seleccione un destino de las opciones");	
			return false;
		}else if($(".dorigen:checked").val() == $(".ddestino:checked").val()){
			alert("El origen y el destino no pueden ser los mismos.");	
			return false;
		}
		$.get("procesa.php",{tarifa:'ok', origen:$(".dorigen:checked").val(), destino:$(".ddestino:checked").val()}, function(data){
			if(data == 1){ 
				alert("No sen encontraron tarifas entre el origen y destino");
				$(".load").html("Seleccionar Origen y Destino");
			}else{
				$(".load").html(data);	
			}
		});
	});
	
	$("#fecha_reserva").datepicker({ 
		dateFormat: 'dd/mm/yy',
		minDate: 0,
		maxDate: "+3M",
		showOn: "button",
		buttonImage: "aplication/webroot/imgs/piker.png"
	});
	
	 $.mask.definitions['H']='[01]';
     $.mask.definitions['N']='[012345]';
	 $.mask.definitions['n']='[0123456789]';

	$('#hini').mask("Hn:Nn");
	$('#hfin').mask("Hn:Nn"); 
	
	$("#tipo_servicio").change(function(){
		if(document.getElementById("tipo_servicio").selectedIndex == 4){
			$(".horaf").removeClass("hide");
		}else{
			$(".horaf").addClass("hide");
			$("#hfin").val("");
			$("#simfin1, #simfin2").removeAttr("checked").parent("span").removeClass("checked");
		}
	});

});

function Enter(e, url){
	tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla == 13) {search_tarifas(url);return true;}
}

function search_tarifas(url){
	if($("#origen").val() != "" &&  $("#destino").val() != ""){
		location.replace(url+"?qi="+$("#origen").val()+"&qf="+$("#destino").val())
	}else{
		alert("Ingrese un origen y destino.");
	}
}

function busqueda(url,texto){
	
	document.fbuscar.action = url+'&q=' + texto.value;
	document.fbuscar.submit();
}

function checkTheKey(keyCode){
	if(event.keyCode==13){	
		valida();
		return true ;
	}
	return false ;
}

var testresults
function checkemail(value){
	var str = value
	var filter=/^.+@.+\..{2,3}$/
		if (filter.test(str))
		testresults=true
	else{
		alert("Por favor ingrese un e-mail valido...");
		testresults=false
	}
	return (testresults)
}

function validateLogin(){
	var f1 = eval("document.login");
	if(f1.usuario.value == ""){
		alert("Por favor ingrese su email.");
		f1.usuario.focus();
		return false;
	}else if(checkemail(f1.usuario.value) == false){
		f1.usuario.focus();
		return false;
	}else if(f1.password.value == ""){
		alert("Por favor ingrese su contraseña.");
		f1.password.focus();
		return false;
	}
}


function validateSend(){
	var f1 = eval("document.login");
	if(f1.email.value == ""){
		alert("Por favor ingrese su email.");
		f1.email.focus();
		return false;
	}else if(checkemail(f1.email.value) == false){
		f1.email.focus();
		return false;
	}
}

function valida_reserva(f1){
	
	if(document.getElementById("tipo_servicio").value == ""){
		alert("Seleccione un servicio");
		document.getElementById("tipo_servicio").focus();
		return false;
	}else if(f1.dir_origen.value == ""){
		alert("Complete la direccion exacta del origen");
		f1.dir_origen.focus();
		return false;	
	}else if(f1.dir_destino.value == ""){
		alert("Complete la direccion exacta del destino");
		f1.dir_destino.focus();
		return false;	
	}else if(document.getElementById("hini").value == ""){
		alert("Ingrese la Hora");
		document.getElementById("hini").focus();
		return false;
	}else if(f1.simini[0].checked==false && f1.simini[1].checked==false){
		alert("Seleccione una opcion AM/PM");
		return false;	
	}else if(document.getElementById("hfin").value == "" && document.getElementById("tipo_servicio").value == 'Servicio  por Horas'){
		alert("Ingrese la Hora");
		document.getElementById("hfin").focus();
		return false;
	}else if(f1.simfin[0].checked==false  && f1.simfin[1].checked==false && document.getElementById("tipo_servicio").value == 'Servicio  por Horas'){
		alert("Seleccione una opcion AM/PM");
		return false;	
	}
	
	if(confirm("Esta seguro de realizar la reserva?")){
		return true;
	}
	return false;
}
