$(document).ready(function() {
    $("#desde").datepicker({dateFormat:'yy-mm-dd'});
    $("#hasta").datepicker({dateFormat:'yy-mm-dd'});
    $("#desdeTrainer").datepicker({dateFormat:'yy-mm-dd'});
    $("#hastaTrainer").datepicker({dateFormat:'yy-mm-dd'});
    $("#weightlosstrainerreport_fecha").datepicker({dateFormat:'yy-mm-dd'});
    $("#reportprogres_date").datepicker({dateFormat:'yy-mm-dd'});

    $('#formulario').submit(function(){
        if($('#system').val() == ""){
           alert("Debe ingresar un código de socio.");            
           return false;
        }    

    });

    $("#contactForm").submit(function(){    
        if ($("#sedesSelect").val() == ""){
           alert("Debe selecionar una Sede"); 
           return false;
          }
        if ($("#asuntoSelect").val() == "" ){
            alert("Debe selecionar un asunto"); 
           return false;
        }
     });


  $("#processReport").submit(function(){

    if (($("#desde").val() == "") || ($("#hasta").val() =="" )){

      alert ("Debe ingresar el rango de Fechas!");
      return false;    
    }
    else
		{
			if($("#desde").val().length == 10)
			{
				$("#desde").val($("#desde").val() + " 00:00:00");
			}
			if($("#hasta").val().length == 10)
			{
				$("#hasta").val($("#hasta").val() + " 23:59:59");
			}
		}
  });

   $("#processReportTrainer").submit(function(){
      
    if (($("#desdeTrainer").val() == "") || ($("#hastaTrainer").val() == "" )){
      alert ("Debe ingresar el rango de Fechas!");
      return false;    
    }          
    else
		{
			if($("#desdeTrainer").val().length == 10)
			{
				$("#desdeTrainer").val($("#desdeTrainer").val() + " 00:00:00");
			}
			if($("#hastaTrainer").val().length == 10)
			{
				$("#hastaTrainer").val($("#hastaTrainer").val() + " 23:59:59");
			}
		}
    });

	jQuery('input[title]').example(function() {
    	return $(this).attr('title');
    });	

	jQuery('#featured').cycle({
		fx: 'fade',
		speed: 300,
		timeout: 10000, 
		prev:   '#controlLeft', 
    	next:   '#controlRight',
    	pager:  '#navFeatured',
    	pagerAnchorBuilder: function(idx, slide) { 
    		return '<li><a href="#"><!--'+ idx +'--></a></li>';
    	}
	});
	
	jQuery('#ageSlider').cycle({
        fx: 'fade',
        speed: 300,
        timeout: 15000,
        pager: '#ageSlider_pager',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + jQuery(".imgMinage", slide).html() + '</a></li>';
        }
    });
	
	$('#statistics').jScrollPane();
	

	$("a.actividad",this).fancybox({
	    'overlayOpacity': 0.8,
	    'overlayColor' 	: '#000',
	    'padding'		: 0,
	    'titleShow'		: false,				
	    'transitionIn'		: 'none',
	    'transitionOut'		: 'none',
	    'autoScale'     	: false,
	    'scrolling'   		: 'no'		
	});

	$("a.modal",this).fancybox({
	    'overlayOpacity': 0.8,
	    'overlayColor' 	: '#000',
	    'padding'		: 0,
	    'titleShow'		: false,				
	    'transitionIn'		: 'none',
	    'transitionOut'		: 'none',
	    'autoScale'     	: false,
	    'scrolling'   		: 'no'		
	});
	
	
	$(".postGallery a, .mainGallery a, .mainGallerySingle a").fancybox({
		'overlayOpacity'	: 0.9,
		'overlayColor' 		: '#000',
		'padding'			: 0,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Imagen ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});

    
    $('a.expander',this).click(function() {
        var id = $(this).attr('id');
     $('.panelHide_' + id).slideToggle("fast");
     $(this).toggleClass("expander_current");
        //alert(id);
     ;
    // $.get("/socio/99008/mensajes", {id: "John", readed: "true" });
   //  window.location.refresh();
     return false;
     });
    
		$(".rut").Rut({format_on: 'keyup'});

		//console.log($("#system_id"));
		$("#system_id_fake").show();
		$("#system_id").hide();

		$("#system_id_fake").focus(function(){
			$("#system_id_fake").hide();
			$("#system_id").show();
			$("#system_id").focus();
		});
		$("#system_id").blur(function(){
			if($("#system_id").val() == "")
			{
				$("#system_id_fake").show();
				$("#system_id").hide();
			}
		});
});


function getsocio(id) {	
	var url = '?system_id='+id;

	var request =  new XMLHttpRequest();
	request.open('GET', '/getsocio'+url, true);

	request.onreadystatechange = function() {
		if (request.readyState < 4 ){

		} else {
		if (request.readyState == 4) {
				eval( "survey= (" + request.responseText + ")" );	

		
				if (survey.hasOwnProperty("user_exist")){
					if(survey.user_exist)
					{
						str = "El alumno no ha sido encuestado";
					}
					else
					{
						str = "El alumno no existe en el portal del Alumno, de igual manera puede ser encuestado";
					}
					document.getElementById("checkuser").innerHTML = str;
				} else {
					if(survey.survey_status == "pendiente")
					{
						// REDIRECT
						location.href = "/encuesta/1/make/" + id;
					}
					else
					{
						document.getElementById("checkuser").innerHTML = "Este alumno ya ha realizado la encuesta";	
					}
					
				}
									
				}
				}	
			}
		request.send(null);
		return false;
}



