
var gbIva = 0.16;

var arrNoEntrarUsuario1 = new Array("#litabRegistroDiario","#litabSignosVitales");
var arrNoEntrarUsuario2 = new Array("#litabCatMedicamentos","#litabCatDiagnosticos","#litabCatHospitales","#litabCatUsuarios", "#litabReportes", "#litabReporteActividad","#litabConsultas");
var arrNoEntrarUsuario4 = new Array("#litabAgenda","#litabRegistroDiario","litabMonitorConsultas","#litabCatalogos","#litabCatClientes","#litabCatMedicamentos","#litabCatDiagnosticos","#litabCatHospitales","#litabCatUsuarios", "#litabReportes", "#litabReporteActividad","#litabConsultas", "#litabMonitorConsultas");
var objDiaSel = new Object();
var objClienteSel = null;
var intWidth = 0, intWidthGrids = 0, intHeight = 0, intHeightGrids = 0;

$(document).ready(function(){
	
	if(objUsuario.tipo===1){ // Medico
		for(var n=0; n<arrNoEntrarUsuario1.length;n++){
			$(arrNoEntrarUsuario1[n]).css({"display":"none", "visibility":"hidden"});
		}
	}else if(objUsuario.tipo===2){ // Asistente
		for(var n=0; n<arrNoEntrarUsuario2.length;n++){
			$(arrNoEntrarUsuario2[n]).css({"display":"none", "visibility":"hidden"});
		}
	}else if(objUsuario.tipo===3){ // Monitor de consultas TV
		document.location.href = "monitorconsultas.php";
	}else if(objUsuario.tipo===4){ // Tomador de signos vitales
        for(var n=0; n<arrNoEntrarUsuario4.length;n++){
			$(arrNoEntrarUsuario4[n]).css({"display":"none", "visibility":"hidden"});
		}
	}		
	
	intWidth = $(window).width();
    intWidthGrids = intWidth-240;

	intHeight = $(window).height();
    intHeightGrids = intHeight - 300;


	/*$( window ).on( "orientationchange", function( event ) {
	  //location.reload();
	  setTimeout(function(){ getDataTABCATCLI(); }, 1000);
	});*/
	 
	// You can also manually force this event to fire.
	//$( window ).orientationchange();

	setTimeout(function(){ showPage('tabDashboard') }, 1000);
	//setTimeout(function(){ showPage('tabAgenda') }, 1000);
	//setTimeout(function(){ showPage('tabRegistroDiario') }, 1000);
	
	setInterval(function(){ $.get("checksession.php", function(data){  if(data==""){ document.location.href='logout.php'; } } ); }, 300000);

	//mainMenu();

	$( "#dialog-cita" ).dialog({
	  dialogClass :"clsJustBtnClose",
	  resizable: false,
	  autoOpen: false,
	  position:"center top",
	  width: 550,
	  height: 200,
	  modal: true,
	  close: function() {
			//limpiarTextboxesNuevaCita();
			objClienteSel = null;

			$("#dialog-cita").find("#intPaciente").val();
			$("#dialog-cita").find("#divDetalleCita").html();
	  }
	});

	$( "#dialog-buscar-cliente" ).dialog({
	  dialogClass :"clsJustBtnClose",
	  resizable: false,
	  autoOpen: false,
	  position:"center top",
	  width: 550,
	  height: 600,
	  modal: true,
	  close: function() {
			//limpiarTextboxesNuevaCita();
			//objClienteSel = null;

			$("#dialog-buscar-cliente").find("#txtPaciente").val('');
			$("#dialog-buscar-cliente").find("#divGridPacientes").html('');
	  }
	});
	
});
 

//MENU
var contador = 1;

function showMenu () {
    console.log("showMenu: " + contador );
    
    $("#navMenu").css({"display":"block"});
	if (contador == 1) {
		$('#navMenu').animate({
			left: '0'
		});
		contador = 0;
	} else {
		contador = 1;
		$('#navMenu').animate({
			left: '-100%'
		});
	}
}

function mouseEvent(){
	var cssLeft = $('#navMenu').css("left").replace("px","");
	//console.log("css left: " + cssLeft );
	if(parseInt(cssLeft)===0){
		$('#navMenu').animate({
			left: '-100%'
		});
		contador = 1;
	}
}

function limpiarTabs(){
	//$("#divAgenda").css("height", heightDocument);
	$("#divDashboard").css("display", "none");
	$("#divRegistroDiario").css("display", "none");
	$("#divMonitorConsultas").css("display", "none");
	$("#divSignosVitales").css("display", "none");
	$("#divAgenda").css("display", "none");
	$("#divCita").css("display", "none");
	$("#divExpediente").css("display", "none");
	$("#divCatClientes").css("display", "none");
	$("#divCatUsuarios").css("display", "none");
	$("#divCatMedicamentos").css("display", "none");
	$("#divCatDiagnosticos").css("display", "none");
	$("#divCatHospitales").css("display", "none");
	$("#divRepActividad").css("display", "none");
	$("#divConsultas").css("display", "none");

	$("#divDashboard").empty();
	$("#divRegistroDiario").empty();
	$("#divMonitorConsultas").empty();
	$("#divSignosVitales").empty();
	
	$("#divAgenda").empty();
	$("#divCita").empty();
	$("#divExpediente").empty();
	
	$("#divCatClientes").empty();
	$("#divCatUsuarios").empty();
	$("#divCatMedicamentos").empty();
	$("#divCatDiagnosticos").empty();
	$("#divCatHospitales").empty();
	
	$("#divRepActividad").empty();
	$("#divConsultas").empty();

}

function showPage(strPage, parametro){
	console.log("showPage / strPage: "+ strPage +" / parametro: "+ parametro);

	limpiarTabs();

	mouseEvent();
	
	if(strPage == "tabDashboard"){
    	strIdConsultaGral = "";

		$("#divDashboard").css("display", "block");
		//$("#litabAgenda").css("background-color","#CEF45B");

		TABDASH_Constructor();
	}
	if(strPage == "tabAgenda"){
    	strIdConsultaGral = "";

		$("#divAgenda").css("display", "block");
		//$("#litabAgenda").css("background-color","#CEF45B");

		TABAGE_Constructor();
	}
	if(strPage == "tabRegistroDiario"){
	    strIdConsultaGral = "";

		$("#divRegistroDiario").css("display", "block");
		//$("#litabRegistroDiario").css("background-color","#CEF45B");

		TABREGDIA_Constructor();
	}
	if(strPage == "tabMonitorConsultas"){
    	strIdConsultaGral = "";

		$("#divMonitorConsultas").css("display", "block");
		//$("#litabMonitorConsultas").css("background-color","#CEF45B");

		TABMONVIS_Constructor();
	}
	if(strPage == "tabSignosVitales"){
		$("#divSignosVitales").css("display", "block");
		//$("#litabSignosVitales").css("background-color","#CEF45B");

		TABSIGVIT_Constructor();
	}
	if(strPage == "tabExp"){
		$("#divExpediente").css("display", "block");

		TABEXP_Constructor(parametro);
		//$("#litabAgenda").css("background-color","#CEF45B");
	}
	if(strPage == "tabCatClientes"){
		$("#divCatClientes").css("display", "block");
		//$("#litabCatClientes").css("background-color","#CEF45B");

		TABCATCLI_Constructor();
	}
	if(strPage == "tabCatUsuarios"){
		$("#divCatUsuarios").css("display", "block");
		//$("#litabCatUsuarios").css("background-color","#CEF45B");

		TABCATUSU_Constructor();
	}
	if(strPage == "tabCatMedicamentos"){
		$("#divCatMedicamentos").css("display", "block");
		//$("#litabCatMedicamentos").css("background-color","#CEF45B");

		TABCATMED_Constructor();
	}
	if(strPage == "tabCatDiagnosticos"){
		$("#divCatDiagnosticos").css("display", "block");
		//$("#litabCatDiagnosticos").css("background-color","#CEF45B");

		TABCATDIA_Constructor();
	}
	if(strPage == "tabConsultas"){
		$("#divConsultas").css("display", "contents");
		//$("#litabCatHospitales").css("background-color","#CEF45B");

		TABCONSULTAS_Constructor();
	}
	if(strPage == "tabReporteActividad"){
		$("#divRepActividad").css("display", "block");
		//$("#litabReporteActividad").css("background-color","#CEF45B");

		TABREPACT_Constructor();
	}
}

function mostrarCita(intCase){
	console.log("\n\n---------- mostrarCita");
	//$( "#divCitas" ).find("#divAgregarCita").css({"visibility":"hidden","display":"none"});
	var fecha = objDiaSel.fecha;
	
	if(fecha!=null){
		
		//alert("Se agregara cita para el " + fecha);
		
		/*if(intCase==0){ // nueva cita
		
			$( "#divNuevaCita" ).find("#txtFecha").val( '' );
			$( "#divNuevaCita" ).css({"visibility":"hidden","display":"none"});
			$( "#divCalendarioContainer" ).css({"visibility":"visible","display":"inline-block"});
			
		}else */
		if(intCase==1){ // nueva cita
		
			$( "#divNuevaCita" ).find("#txtFecha").val( fecha );
			setAutocomplete( "clientes", $( "#divNuevaCita" ).find("#txtPaciente") );

			$( "#divNuevaCita" ).css({"visibility":"visible","display":"inline-block"});
			$( "#divCalendarioContainer" ).css({"visibility":"hidden","display":"none"});
			
		}else if(intCase==2){ // ver cita
			
		}
		
		objDiaSel = new Object();

		//showPage('tabExp');
		
	}else{
		var vista = $('#divCalendario').fullCalendar( 'getView' );
		if(vista.name=="agendaWeek"){
			alert("Favor de seleccionar d&iacute;a y hora.");
		}else if(vista.name=="agendaDay"){
			alert("Favor de seleccionar hora.");
		}
	}
}

function setAutocomplete(strCaso, objControl, objControlTxtA, intCase){
	//console.log("setAutocomplete / strCaso: "+ strCaso +" / objControl: "+ objControl +" / intCase: "+ intCase, objControl);
	//console.log("strCaso: "+ strCaso);

	var strUrl = "data/comboboxes.php?method="+ strCaso +"&intEmpresa="+ objUsuario.empresa +"&rnd="+ Math.random();
	//console.log(strUrl);
	
	objControl.autocomplete({
	  source: function( request, response ) {
		$.ajax({
		  url: strUrl,
		  dataType: "json",
		  data: {
			q: request.term
		  },
		  success: function( data ) {
			//console.log(data);
			response( data.Items );
		  }
		});
	  },
	  minLength: 3,
	  select: function( event, ui ) {

		$("#divNuevaCita").find("#hidIntPaciente" ).val(ui.item.id);

		var id = ui.item.id;
		var lbl = ui.item.label;
		console.log( ui.item ? "Selected: " + lbl + " / id = " + id : "Nothing selected, input was " + this.value);
	  
	  	if(strCaso=="medicamentos"){
		  	if(intCase==null){

				var txtAreaMedicamentos = objControlTxtA.val();
				if(txtAreaMedicamentos!=""){
					var retornosCarro = txtAreaMedicamentos.split("\n");
					//console.log("retornosCarro.length: "+ retornosCarro.length);
					var iNumber = retornosCarro.length + 1;
					objControlTxtA.val(txtAreaMedicamentos +"\n"+ iNumber +".- "+ lbl);
				}else{
					objControlTxtA.val("1.- "+ lbl);
				}

			}else{

				var txtAreaMedicamentos = objControlTxtA.html();
				if(txtAreaMedicamentos!=""){
					var intChildren = objControlTxtA.children(".clsItem").length;
					var iNumber = intChildren + 1;
				}else{
					var iNumber = 1;
				}
				var arrLbl = lbl.split(" - ");
				var strDiv = "<div class='clsItem' style='float:left; width:100%; height: 38px; border-bottom:1px solid #ccc;'>";
				  //strDiv += "<div style='margin: 5px;'>"+ iNumber +".- "+ lbl +"</div>";
				  strDiv += "<div style=' margin: 5px; float:left; width:25%; padding-top: 6px;'>- "+ arrLbl[1] +"</div>";
				  strDiv += "<div style=' margin: 5px; float:left; width:35%; padding-top: 6px;'>"+ arrLbl[2] +"</div>";
				  strDiv += "<div style=' margin: 5px; float:left; width:12%; '><input id='txtDosis' type='text' placeholder='Dosis' style='width:100px; border:1px solid #ccc; border-radius:4px; background:#e8e6e6; padding:5px;'/></div>";
				  strDiv += "<div style=' margin: 5px; float:left; width:12%; '>Cada <input id='txtHoras' type='text' style='width:50px; border:1px solid #ccc; border-radius:4px; background:#e8e6e6; padding:5px;'/> hrs</div>";
				strDiv += "</div>";
				objControlTxtA.append(strDiv);

			}
	
			setTimeout(function(){
				objControl.val("");
				console.log("Se manda limpiar");
			},100);
		}

	
	  },
	  open: function() {
		$("#divNuevaCita").find("#hidIntPaciente" ).val('');
		$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	  },
	  close: function() {
		$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	  }
	});
}

function salir(){
	document.location.href = "logout.php";
}
