// RMSchedulerAssetsController.j
// RMSchedulerAssetsController
// Created by Anthony Picado Ávila

@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import <MapKit/MKMapView.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPApplication.j>
@import <Foundation/CPURLRequest.j>
//@import "RMTabView.j"
@import "AppController.j"



@implementation RMSchedulerOrganizationsController : RMControllerObject {

	CPTableView counHoliTabl;
	CPArray counHoliArra;

	CPArray orgaArra;
	CPString orgaCond;
	CPPanel orgaPane;
	CPTableView orgaTabl;
	CPTextField orgaIdenText,orgaLawIdenText,orgaNameText,orgaNameLegaText,orgaPhonText,orgaAddrStreText,orgaAddrCityText,orgaAddrStatText;
	id orgaActiChec;
	id prevValuOrgaActiChec;
	
	CPTextField orgaEmaiText, orgaNoteText;

	CPTableView orgaEmplTabl;
	CPArray orgaEmplArra;
	CPTextField orgaEmplLawIdenText, orgaEmplNameLastText, orgaEmplNameFirsText, orgaEmplPosiText;
	CPTextField orgaEmplAddrStreText,orgaEmplAddrCityText,orgaEmplAddrStatText;
	CPTextField orgaEmplCellPhon1Text,orgaEmplCellPhon2Text,orgaEmplEmaiText,orgaEmplNoteText;
	id orgaEmplActiChec;
	id orgaEmplActiMondChec,orgaEmplActiTuesChec,orgaEmplActiWednChec,orgaEmplActiThurChec,orgaEmplActiFridChec,orgaEmplActiSatuChec,orgaEmplActiSundChec;

	CPTableView orgaLocaTabl;
	CPArray orgaLocaArra;
	CPTextField orgaLocaIdenText,orgaLocaNameText,orgaLocaBuilText,orgaLocaGateText
	CPTextField orgaLocaAddrStreText,orgaLocaAddrCityText,orgaLocaAddrStatText;
	CPTextField orgaLocaPhonText,orgaLocaEmaiText,orgaLocaRestText, orgaLocaNoteText;
	CPTextField orgaLocaLngdText,orgaLocaLatiText,orgaLocaColoText;
	id orgaLocaCont0Popu, orgaLocaCont1Popu, orgaLocaCont2Popu, orgaLocaCont3Popu;
	id orgaLocaCorpCont0Popu, orgaLocaCorpCont1Popu, orgaLocaCorpCont2Popu, orgaLocaCorpCont3Popu;
    id orgaLocaActiChec;
    id prevValuOrgaLocaActiChec
    
	id orgaLocaActiMondChec,orgaLocaActiTuesChec,orgaLocaActiWednChec,orgaLocaActiThurChec,orgaLocaActiFridChec,orgaLocaActiSatuChec,orgaLocaActiSundChec;
	CPTextField orgaLocaHourStarMondText,orgaLocaHourStarTuesText,orgaLocaHourStarWednText,orgaLocaHourStarThurText,orgaLocaHourStarFridText,orgaLocaHourStarSatuText,orgaLocaHourStarSundText;
	CPTextField orgaLocaHourFiniMondText,orgaLocaHourFiniTuesText,orgaLocaHourFiniWednText,orgaLocaHourFiniThurText,orgaLocaHourFiniFridText,orgaLocaHourFiniSatuText,orgaLocaHourFiniSundText;

    id menuItem;

     CPTableView orgaHoliTabl;
     CPArray orgaHoliArra;
     CPTextField orgaHoliDateStarText, orgaHoliDateFiniText, orgaHoliDescText;

     CPTableView orgaTraiTabl;
     CPArray orgaTraiArra;
     CPTextField orgaTraiAbbrText, orgaTraiDateLastText, orgaTraiDateExpiText, orgaTraiDescText;
     
     
     id prevOrgaIden;    
     id prevOrgaName;
     
     id prevLocaIden;     
     id prevLocaName;

     
}



- (void)  applicationQuit {
	if ( orgaPane ) {
		[orgaPane orderOut:0];
		[orgaPane release];
	}
}



- (void) countryHolidaysCopy:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 ) {
		[glblAlerPane showWithMessage:@"Atencion" info:"Seleccione una organizacion y pruebe de nuevo"];
		//alert("Seleccione una organización y pruebe de nuevo.");
		return;
	}
	var arra = [orgaArra[inde] valueList];
	var orgaIden = arra[0];
	var quer = [CPString stringWithFormat:@"delete from %@.organizationHolidays where organizationId='%@'",dataBase,orgaIden];
	[self query:quer];
	quer = [CPString stringWithFormat:@"insert into %@.organizationHolidays (organizationId,dateStart,dateFinish,description) select '%@',dateStart,dateFinish,description from %@.countryHolidays where countryId=506",dataBase,orgaIden,dataBase];
	[self query:quer];
	
	[orgaHoliTabl reloadData];
}



- (id) init:(id)paraAppCont {
	[super init];
	
	var ip = [CPString stringWithFormat:@"192.168.2.252"];
	orgaCond = @"";
	orgaPane = NULL;
	prevOrgaIden = "";     
    prevOrgaName = "";
    
    prevLocaIden = "";     
    prevLocaName = "";    
    
	//routRefrFunc = setInterval(refreshFunction,30000);
	appCont = paraAppCont;
	dataBase = [appCont getDataBase];
	prevValuOrgaLocaActiChec = "0";
	prevValuOrgaActiChec     = "0";
	//alert("db init= " + [appCont getDataBase]);
	
	return self;
}



- (void) organizationsPanel {
	return orgaPane;
}



- (void) organizationsPanel:(id)sender {
	
	var horiPos1 = 10;
	var horiPos2 = horiPos1 + 100;
	var horiPos3 = 390;
	var horiPos4 = horiPos3 + 100;

	var horiTextWidt = 200;
	var horiPosiOffs = -4;
	var horiPosi = 200;

	var vertPosi = 20;
	var vertPosiOffs = -4;
	var vertIncr = 26;
	var vertLineHeig = 30;

	var paneWidt = 1150;
	var paneHeig = 680;
	var tablWidt = paneWidt - 20;
	var tablHeig = 150;
	
	var x = 10 + Math.random()*40;
	var y = 60 + Math.random()*40;
	var fram = CGRectMake(x,y, paneWidt,paneHeig);

	orgaPane = [[CPPanel alloc] initWithContentRect:fram styleMask:CPHUDBackgroundWindowMask|CPClosableWindowMask|CPResizableWindowMask]; 
	[orgaPane setFloatingPanel:YES]; 
	[orgaPane orderFront:self];
	[orgaPane setTitle:@"Panel Organizaciones"];
	
	var paneContView = [orgaPane contentView];
	[paneContView setBackgroundColor:coloDefaLite];
	[self addBanners:paneContView heightBlue:1. heightOrange:4.];

	fram = CGRectMake(10,10,paneWidt-20,paneHeig-20);

	var tabView = [[CPTabView alloc] initWithFrame:fram];
	//[tabView setBackgroundColor:coloDefaBrig];
	//[tabView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable|CPViewMinXMargin];
	[tabView setDelegate:self];
	[paneContView addSubview:tabView]; 

	var labe;
	var tabViewItem;
	var view;


	// Organizations *************************************************

	tabViewItem = [[CPTabViewItem alloc] initWithIdentifier:@"organizations"];
	[tabViewItem setLabel:@"  Organizaciones  "];
				
	view = [[CPView alloc] initWithFrame: CGRectMake(50,50, 50,50)]; // donde se pone los controles --> cp view
	[view setBackgroundColor:coloDefaBrig];

	vertPosi = 20;

	var fram= CGRectMake(horiPos2,vertPosi, 1000,tablHeig+70);
	var scroView= [[CPScrollView alloc] initWithFrame:fram];

	orgaTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[orgaTabl setDataSource:self];
	[orgaTabl setTarget:self];
	[orgaTabl setAction:@selector(organizationTableSelection:)];
	[orgaTabl setAllowsColumnReordering:YES];
	[orgaTabl setUsesAlternatingRowBackgroundColors:YES];
	[orgaTabl setAllowsMultipleSelection:YES];
	[orgaTabl setDelegate:self];

	var colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:100];
	[orgaTabl addTableColumn:colu];

	var colu= [[CPTableColumn alloc] initWithIdentifier:@"IDLegal"];
	[[colu headerView] setStringValue:@"IDLegal"];
	[colu setWidth:100];
	[orgaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Nombre"];
	[[colu headerView] setStringValue:@"Nombre"];
	[colu setWidth:120];
	[orgaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"NombreLegal"];
	[[colu headerView] setStringValue:@"NombreLegal"];
	[colu setWidth:180];
	[orgaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Telefono1"];
	[[colu headerView] setStringValue:@"Telefono1"];
	[colu setWidth:140];
	[orgaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Correo"];
	[[colu headerView] setStringValue:@"Correo"];
	[colu setWidth:140];
	[orgaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Comentarios"];
	[[colu headerView] setStringValue:@"Comentarios"];
	[colu setWidth:240];
	[orgaTabl addTableColumn:colu];

	[scroView setDocumentView:orgaTabl];

	[view addSubview:scroView];


    vertPosi += tablHeig + 80;
    var vertPosiHere = vertPosi;
	
    labe = [self addTextField:@"ID :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaIdenText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];
	
    vertPosi += vertIncr;

    labe = [self addTextField:@"ID Legal :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLawIdenText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

	labe = [self addTextField:@"Nombre :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaNameText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Nombre Legal :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaNameLegaText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Direccion :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaAddrStreText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:3*vertLineHeig VView:view Editable:YES];
	[orgaAddrStreText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 3*vertLineHeig;

    labe = [self addTextField:@"Ciudad :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaAddrCityText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Estado :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaAddrStatText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Teléfono :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaPhonText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Correo :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmaiText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];
    
	var vertPosiBott = vertPosi;

    vertPosi = vertPosiHere;

    labe = [self addTextField:@"Comentarios:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaNoteText = [self addTextField:@"" TFPosX:horiPos4 TFPosY:vertPosi-4 TFWidth:240 TFHeight:3*vertLineHeig VView:view Editable:YES];
	[orgaNoteText setLineBreakMode:CPLineBreakByWordWrapping];


    vertPosi = vertPosiBott + 2*vertIncr;

    labe = [self addTextField:@"Activo:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
    [labe setTextColor:[CPColor whiteColor]];

    orgaActiChec = [CPCheckBox checkBoxWithTitle:@"Seleccione para activar/desactivar la organizacion"];
    [orgaActiChec setTextColor:[CPColor whiteColor]];
    [orgaActiChec setFrameOrigin:CPPointMake(horiPos2, vertPosi)];
    [orgaActiChec setTarget:self];
    [orgaActiChec setEnabled:YES];
    //[checDeshClie setAction:@selector(stateCheck4:)];
    [view addSubview:orgaActiChec];


    vertPosi += 1.5*vertIncr;

    var butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 80,24)];
	[butt setTitle:"Ingresar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationInsert:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+80,vertPosi, 80,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationUpdate:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+160,vertPosi, 80,24)];
	[butt setTitle:"Eliminar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationDelete:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+240,vertPosi, 80,24)];
	[butt setTitle:"Limpiar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationClearFields:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+320,vertPosi, 80,24)];
	[butt setTitle:"Buscar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationFind:)];                
	[view addSubview:butt];


	[tabViewItem setView:view];

	[tabView addTabViewItem: tabViewItem];


	// Locations *****************************************************

	tabViewItem = [[CPTabViewItem alloc] initWithIdentifier:@"locations"];
	[tabViewItem setLabel:@"  Locaciones  "];
				
	view = [[CPView alloc] initWithFrame: CGRectMake(50,50, 50,50)]; // donde se pone los controles --> cp view
	[view setBackgroundColor:coloDefaBrig];

	vertPosi = 20;

	var fram= CGRectMake(horiPos2,vertPosi, 1000,tablHeig);
	var scroView= [[CPScrollView alloc] initWithFrame:fram];

	orgaLocaTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[orgaLocaTabl setDataSource:self];
	[orgaLocaTabl setTarget:self];
	[orgaLocaTabl setAction:@selector(organizationLocationTableSelection:)];
	[orgaLocaTabl setAllowsColumnReordering:YES];
	[orgaLocaTabl setUsesAlternatingRowBackgroundColors:YES];
	[orgaLocaTabl setAllowsMultipleSelection:YES];
	[orgaLocaTabl setDelegate:self];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:50];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Nombre"];
	[[colu headerView] setStringValue:@"Nombre"];
	[colu setWidth:160];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Edificio"];
	[[colu headerView] setStringValue:@"Edificio"];
	[colu setWidth:80];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Porton"];
	[[colu headerView] setStringValue:@"Porton"];
	[colu setWidth:80];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Ciudad"];
	[[colu headerView] setStringValue:@"Ciudad"];
	[colu setWidth:70];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Estado"];
	[[colu headerView] setStringValue:@"Estado"];
	[colu setWidth:70];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Telefono"];
	[[colu headerView] setStringValue:@"Telefono"];
	[colu setWidth:120];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Correo"];
	[[colu headerView] setStringValue:@"Correo"];
	[colu setWidth:140];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Restriccion"];
	[[colu headerView] setStringValue:@"Restriccion"];
	[colu setWidth:100];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Longitud"];
	[[colu headerView] setStringValue:@"Longitud"];
	[colu setWidth:80];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Latitud"];
	[[colu headerView] setStringValue:@"Latitud"];
	[colu setWidth:80];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Color"];
	[[colu headerView] setStringValue:@"Color"];
	[colu setWidth:50];
	[orgaLocaTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Activo"];
	[[colu headerView] setStringValue:@"Activo"];
	[colu setWidth:50];
	[orgaLocaTabl addTableColumn:colu];

	[scroView setDocumentView:orgaLocaTabl];

	[view addSubview:scroView];


    vertPosi += tablHeig + 10;
    vertPosiHere = vertPosi;

	labe = [self addTextField:@"Nombre:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaNameText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Edificio:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaBuilText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Puerta:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaGateText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:100 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

	labe = [self addTextField:@"Direccion:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaAddrStreText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:2*vertLineHeig VView:view Editable:YES];
	[orgaLocaAddrStreText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 2*vertLineHeig;

	labe = [self addTextField:@"Ciudad:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaAddrCityText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Estado:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaAddrStatText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Telefono:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaPhonText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Correo:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaEmaiText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Restriccion:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaRestText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:vertLineHeig VView:view Editable:YES];
	[orgaLocaRestText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += vertLineHeig;

    labe = [self addTextField:@"Comentarios:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaNoteText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:2*vertLineHeig VView:view Editable:YES];
	[orgaLocaNoteText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 2*vertLineHeig;

	var vertPosiBott = vertPosi + vertIncr;


    vertPosi = vertPosiHere;

    labe = [self addTextField:@"Longitud:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaLngdText = [self addTextField:@"" TFPosX:horiPos4 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Latitud:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaLatiText = [self addTextField:@"" TFPosX:horiPos4 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Color:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaColoText = [self addTextField:@"" TFPosX:horiPos4 TFPosY:vertPosi-4 TFWidth:100 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += 1.5*vertIncr;

    labe = [self addTextField:@"Contactos:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaCont0Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCont0Popu setTarget:self];
	[orgaLocaCont0Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCont1Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCont1Popu setTarget:self];
	[orgaLocaCont1Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCont2Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCont2Popu setTarget:self];
	[orgaLocaCont2Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCont3Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCont3Popu setTarget:self];
	[orgaLocaCont3Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;

    labe = [self addTextField:@"Corporativos:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaLocaCorpCont0Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCorpCont0Popu setTarget:self];
	[orgaLocaCorpCont0Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCorpCont1Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCorpCont1Popu setTarget:self];
	[orgaLocaCorpCont1Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCorpCont2Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCorpCont2Popu setTarget:self];
	[orgaLocaCorpCont2Popu setAction:@selector(organizationLocationPopupSelection:)];

    vertPosi += vertIncr;
    
	orgaLocaCorpCont3Popu = [self addPopupButton:@"Contactos" TFPosX:horiPos4+4 TFPosY:vertPosi TFWidth:240 TFHeight:24  VView:view pullsDown:NO];
	[orgaLocaCorpCont3Popu setTarget:self];
	[orgaLocaCorpCont3Popu setAction:@selector(organizationLocationPopupSelection:)];


    vertPosi = vertPosiBott;

    labe = [self addTextField:@"Activo:" TFPosX:horiPos1 TFPosY:vertPosi+10 TFWidth:0 TFHeight:0 VView:view Editable:NO];
    [labe setTextColor:[CPColor whiteColor]];

    orgaLocaActiChec = [CPCheckBox checkBoxWithTitle:@"Seleccione para activar/desactivar la locacion"];
    [orgaLocaActiChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiChec setFrameOrigin:CPPointMake(horiPos2, vertPosi+10)];
    [orgaLocaActiChec setTarget:self];
    [orgaLocaActiChec setEnabled:YES];
    //[checDeshClie setAction:@selector(stateCheck4:)];
    [view addSubview:orgaLocaActiChec];


	vertPosi += 2*vertIncr;

    butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 80,24)];
	[butt setTitle:"Ingresar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationLocationInsert:)];                
	[view addSubview:butt];
    
	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+80,vertPosi, 80,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationLocationUpdate:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+160,vertPosi, 80,24)];
	[butt setTitle:"Eliminar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationLocationDelete:)];                
	[view addSubview:butt];
    
	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+240,vertPosi, 80,24)];
	[butt setTitle:"Limpiar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationLocationClearFields:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+320,vertPosi, 80,24)];
	[butt setTitle:"Mapa"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationLocationsInMap:)];                
	[view addSubview:butt];


	vertPosi = vertPosiHere;
	var checPos1 = 800;
	var checPos2 = checPos1 + 80;
	var hourWidt = 80;
	var checPos3 = checPos2 + hourWidt;

    labe = [self addTextField:@"Horas de Servicio:" TFPosX:checPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
    [labe setTextColor:[CPColor whiteColor]];


    vertPosi += vertIncr;

    orgaLocaActiSundChec = [CPCheckBox checkBoxWithTitle:@"Domingo"];
    [orgaLocaActiSundChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiSundChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiSundChec setTarget:self];
    [orgaLocaActiSundChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiSundChec setEnabled:YES];
    [view addSubview:orgaLocaActiSundChec];

    orgaLocaHourStarSundText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniSundText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiMondChec = [CPCheckBox checkBoxWithTitle:@"Lunes"];
    [orgaLocaActiMondChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiMondChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiMondChec setTarget:self];
    [orgaLocaActiMondChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiMondChec setEnabled:YES];
    [view addSubview:orgaLocaActiMondChec];

    orgaLocaHourStarMondText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniMondText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiTuesChec = [CPCheckBox checkBoxWithTitle:@"Martes"];
    [orgaLocaActiTuesChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiTuesChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiTuesChec setTarget:self];
    [orgaLocaActiTuesChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiTuesChec setEnabled:YES];
    [view addSubview:orgaLocaActiTuesChec];

    orgaLocaHourStarTuesText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniTuesText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiWednChec = [CPCheckBox checkBoxWithTitle:@"Miercoles"];
    [orgaLocaActiWednChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiWednChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiWednChec setTarget:self];
    [orgaLocaActiWednChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiWednChec setEnabled:YES];
    [view addSubview:orgaLocaActiWednChec];

    orgaLocaHourStarWednText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniWednText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiThurChec = [CPCheckBox checkBoxWithTitle:@"Jueves"];
    [orgaLocaActiThurChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiThurChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiThurChec setTarget:self];
    [orgaLocaActiThurChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiThurChec setEnabled:YES];
    [view addSubview:orgaLocaActiThurChec];

    orgaLocaHourStarThurText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniThurText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiFridChec = [CPCheckBox checkBoxWithTitle:@"Viernes"];
    [orgaLocaActiFridChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiFridChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiFridChec setTarget:self];
    [orgaLocaActiFridChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiFridChec setEnabled:YES];
    [view addSubview:orgaLocaActiFridChec];

    orgaLocaHourStarFridText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniFridText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

    orgaLocaActiSatuChec = [CPCheckBox checkBoxWithTitle:@"Sabado"];
    [orgaLocaActiSatuChec setTextColor:[CPColor whiteColor]];
    [orgaLocaActiSatuChec setFrameOrigin:CPPointMake(checPos1, vertPosi)];
    [orgaLocaActiSatuChec setTarget:self];
    [orgaLocaActiSatuChec setAction:@selector(organizationLocationCheck:)];
    [orgaLocaActiSatuChec setEnabled:YES];
    [view addSubview:orgaLocaActiSatuChec];

    orgaLocaHourStarSatuText = [self addTextField:@"" TFPosX:checPos2 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];
    orgaLocaHourFiniSatuText = [self addTextField:@"" TFPosX:checPos3 TFPosY:vertPosi TFWidth:hourWidt TFHeight:vertLineHeig VView:view Editable:YES];


	[tabViewItem setView:view];
				
	[tabView addTabViewItem: tabViewItem];


	// Holidays *****************************************************

	tabViewItem = [[CPTabViewItem alloc] initWithIdentifier:@"holidays"];
	[tabViewItem setLabel:@"  Feriados  "];
				
	view = [[CPView alloc] initWithFrame: CGRectMake(50,50, 50,50)]; // donde se pone los controles --> cp view
	[view setBackgroundColor:coloDefaBrig];

	vertPosi = 20;

	labe = [self addTextField:@"Pais:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	var fram= CGRectMake(horiPos2,vertPosi, 500,tablHeig);
	var scroView= [[CPScrollView alloc] initWithFrame:fram];

	counHoliTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[counHoliTabl setDataSource:self];
	[counHoliTabl setTarget:self];
	[counHoliTabl setAction:@selector(organizationHolidaysTableSelection:)];
	[counHoliTabl setAllowsColumnReordering:YES];
	[counHoliTabl setUsesAlternatingRowBackgroundColors:YES];
	[counHoliTabl setAllowsMultipleSelection:YES];
	[counHoliTabl setDelegate:self];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:50];
	[counHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"FechaInicio"];
	[[colu headerView] setStringValue:@"FechaInicio"];
	[colu setWidth:140];
	[counHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"FechaFin"];
	[[colu headerView] setStringValue:@"FechaFin"];
	[colu setWidth:140];
	[counHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Descripcion"];
	[[colu headerView] setStringValue:@"Descripcion"];
	[colu setWidth:160];
	[counHoliTabl addTableColumn:colu];

	[scroView setDocumentView:counHoliTabl];

	[view addSubview:scroView];

    vertPosi += tablHeig + 10;

    var butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 200,24)];
	[butt setTitle:"Copiar Feriados del Pais"];
	[butt setTarget:self];
	[butt setAction:@selector(countryHolidaysCopy:)];                
	[view addSubview:butt];

    vertPosi += vertIncr + 10;

	labe = [self addTextField:@"Organización:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	fram= CGRectMake(horiPos2,vertPosi, 500,tablHeig);
	scroView= [[CPScrollView alloc] initWithFrame:fram];

	orgaHoliTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[orgaHoliTabl setDataSource:self];
	[orgaHoliTabl setTarget:self];
	[orgaHoliTabl setAction:@selector(organizationHolidaysTableSelection:)];
	[orgaHoliTabl setAllowsColumnReordering:YES];
	[orgaHoliTabl setUsesAlternatingRowBackgroundColors:YES];
	[orgaHoliTabl setAllowsMultipleSelection:YES];
	[orgaHoliTabl setDelegate:self];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:50];
	[orgaHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"FechaInicio"];
	[[colu headerView] setStringValue:@"FechaInicio"];
	[colu setWidth:140];
	[orgaHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"FechaFin"];
	[[colu headerView] setStringValue:@"FechaFin"];
	[colu setWidth:140];
	[orgaHoliTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Descripcion"];
	[[colu headerView] setStringValue:@"Descripcion"];
	[colu setWidth:160];
	[orgaHoliTabl addTableColumn:colu];

	[scroView setDocumentView:orgaHoliTabl];

	[view addSubview:scroView];

    vertPosi += tablHeig + 10;

    vertPosiHere = vertPosi;

	labe = [self addTextField:@"FechaInicio:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaHoliDateStarText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"FechaFin:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaHoliDateFiniText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Descripcion:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaHoliDescText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:500 TFHeight:2*vertLineHeig VView:view Editable:YES];
	[orgaHoliDescText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 2*vertLineHeig;

    butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 80,24)];
	[butt setTitle:"Ingresar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationHolidaysInsert:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+80,vertPosi, 80,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationHolidaysUpdate:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+160,vertPosi, 80,24)];
	[butt setTitle:"Eliminar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationHolidaysDelete:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+240,vertPosi, 80,24)];
	[butt setTitle:"Limpiar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationHolidaysClearFields:)];                
	[view addSubview:butt];


	[tabViewItem setView:view];
				
	[tabView addTabViewItem: tabViewItem];

	[tabView selectFirstTabViewItem:0];


	// Employees *************************************************

	tabViewItem = [[CPTabViewItem alloc] initWithIdentifier:@"employees"];
	[tabViewItem setLabel:@"  Empleados  "];
				
	view = [[CPView alloc] initWithFrame: CGRectMake(50,50, 50,50)]; // donde se pone los controles --> cp view
	[view setBackgroundColor:coloDefaBrig];

	vertPosi = 20;

	var fram= CGRectMake(horiPos2,vertPosi, 1000,tablHeig);
	var scroView= [[CPScrollView alloc] initWithFrame:fram];

	orgaEmplTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[orgaEmplTabl setDataSource:self];
	[orgaEmplTabl setTarget:self];
	[orgaEmplTabl setAction:@selector(organizationEmployeeTableSelection:)];
	[orgaEmplTabl setAllowsColumnReordering:YES];
	[orgaEmplTabl setUsesAlternatingRowBackgroundColors:YES];
	[orgaEmplTabl setAllowsMultipleSelection:YES];
	[orgaEmplTabl setDelegate:self];

	var colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:50];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Apellidos"];
	[[colu headerView] setStringValue:@"Apellidos"];
	[colu setWidth:140];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Nombre"];
	[[colu headerView] setStringValue:@"Nombre"];
	[colu setWidth:120];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Posicion"];
	[[colu headerView] setStringValue:@"Posicion"];
	[colu setWidth:55];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Telefono1"];
	[[colu headerView] setStringValue:@"Telefono1"];
	[colu setWidth:100];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Telefono2"];
	[[colu headerView] setStringValue:@"Telefono2"];
	[colu setWidth:100];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Correo"];
	[[colu headerView] setStringValue:@"Correo"];
	[colu setWidth:140];
	[orgaEmplTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Comentarios"];
	[[colu headerView] setStringValue:@"Comentarios"];
	[colu setWidth:200];
	[orgaEmplTabl addTableColumn:colu];

	[scroView setDocumentView:orgaEmplTabl];

	[view addSubview:scroView];

    vertPosi += tablHeig + 10;
    var vertPosiHere = vertPosi;

	labe = [self addTextField:@"Cedula:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplLawIdenText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:120 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Apellidos :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplNameLastText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Nombre :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplNameFirsText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Posicion :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplPosiText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Direccion :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplAddrStreText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:240 TFHeight:2*vertLineHeig VView:view Editable:YES];
	[orgaEmplAddrStreText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 2*vertLineHeig;

	labe = [self addTextField:@"Ciudad :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplAddrCityText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Estado :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplAddrStatText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];


    vertPosi += vertIncr;

    labe = [self addTextField:@"Teléfono 1:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplCellPhon1Text = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Teléfono 2:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplCellPhon2Text = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];
    
    vertPosi += vertIncr;

    labe = [self addTextField:@"Correo :" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplEmaiText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:200 TFHeight:vertLineHeig VView:view Editable:YES];
    
	var vertPosiBott = vertPosi;


    vertPosi = vertPosiHere;

    labe = [self addTextField:@"Comentarios:" TFPosX:horiPos3 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaEmplNoteText = [self addTextField:@"" TFPosX:horiPos4 TFPosY:vertPosi-4 TFWidth:240 TFHeight:3*vertLineHeig VView:view Editable:YES];
	[orgaEmplNoteText setLineBreakMode:CPLineBreakByWordWrapping];


    vertPosi = vertPosiBott + 2*vertIncr;

    labe = [self addTextField:@"Activo:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
    [labe setTextColor:[CPColor whiteColor]];

    orgaEmplActiChec = [CPCheckBox checkBoxWithTitle:@"Seleccione para activar/desactivar el ejecutivo"];
    [orgaEmplActiChec setTextColor:[CPColor whiteColor]];
    [orgaEmplActiChec setFrameOrigin:CPPointMake(horiPos2, vertPosi)];
    [orgaEmplActiChec setTarget:self];
    [orgaEmplActiChec setEnabled:YES];
    //[checDeshClie setAction:@selector(stateCheck4:)];
    [view addSubview:orgaEmplActiChec];


    vertPosi += 2*vertIncr;

    butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 80,24)];
	[butt setTitle:"Ingresar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationEmployeeInsert:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+80,vertPosi, 80,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationEmployeeUpdate:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+160,vertPosi, 80,24)];
	[butt setTitle:"Eliminar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationEmployeeDelete:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+240,vertPosi, 80,24)];
	[butt setTitle:"Limpiar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationEmployeeClearFields:)];                
	[view addSubview:butt];


    vertPosi = vertPosiHere;
    var vacaPos1 = 750;
    

	[tabViewItem setView:view];

	[tabView addTabViewItem: tabViewItem];


	// Training *****************************************************

	tabViewItem = [[CPTabViewItem alloc] initWithIdentifier:@"training"];
	[tabViewItem setLabel:@"  Capacitaciones  "];
				
	view = [[CPView alloc] initWithFrame: CGRectMake(50,50, 50,50)]; // donde se pone los controles --> cp view
	[view setBackgroundColor:coloDefaBrig];

	vertPosi = 20;

	var fram= CGRectMake(horiPos2,vertPosi, 700,tablHeig);
	var scroView= [[CPScrollView alloc] initWithFrame:fram];

	orgaTraiTabl = [[CPTableView alloc] initWithFrame:[scroView bounds]];
	[orgaTraiTabl setDataSource:self];
	[orgaTraiTabl setTarget:self];
	[orgaTraiTabl setAction:@selector(organizationTrainingTableSelection:)];
	[orgaTraiTabl setAllowsColumnReordering:YES];
	[orgaTraiTabl setUsesAlternatingRowBackgroundColors:YES];
	[orgaTraiTabl setAllowsMultipleSelection:YES];
	[orgaTraiTabl setDelegate:self];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"ID"];
	[[colu headerView] setStringValue:@"ID"];
	[colu setWidth:50];
	[orgaTraiTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Abbreviation"];
	[[colu headerView] setStringValue:@"Siglas"];
	[colu setWidth:80];
	[orgaTraiTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"Description"];
	[[colu headerView] setStringValue:@"Descripción"];
	[colu setWidth:300];
	[orgaTraiTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"DateLast"];
	[[colu headerView] setStringValue:@"FechaDesde"];
	[colu setWidth:120];
	[orgaTraiTabl addTableColumn:colu];

	colu= [[CPTableColumn alloc] initWithIdentifier:@"DateExpiration"];
	[[colu headerView] setStringValue:@"FechaHasta"];
	[colu setWidth:120];
	[orgaTraiTabl addTableColumn:colu];

	[scroView setDocumentView:orgaTraiTabl];

	[view addSubview:scroView];


    vertPosi += tablHeig + 10;
    vertPosiHere = vertPosi;

	labe = [self addTextField:@"Siglas:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaTraiAbbrText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:100 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Descripcion:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaTraiDescText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:500 TFHeight:2*vertLineHeig VView:view Editable:YES];
	[orgaTraiDescText setLineBreakMode:CPLineBreakByWordWrapping];

    vertPosi += 2*vertLineHeig;

	labe = [self addTextField:@"Fecha Desde:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaTraiDateLastText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += vertIncr;

	labe = [self addTextField:@"Fecha Hasta:" TFPosX:horiPos1 TFPosY:vertPosi TFWidth:0 TFHeight:0 VView:view Editable:NO];
	[labe setTextColor:[CPColor whiteColor]];

	orgaTraiDateExpiText = [self addTextField:@"" TFPosX:horiPos2 TFPosY:vertPosi-4 TFWidth:140 TFHeight:vertLineHeig VView:view Editable:YES];

    vertPosi += 2*vertIncr;

    var butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2,vertPosi, 80,24)];
	[butt setTitle:"Ingresar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationTrainingInsert:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+80,vertPosi, 80,24)];
	[butt setTitle:"Actualizar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationTrainingUpdate:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+160,vertPosi, 80,24)];
	[butt setTitle:"Eliminar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationTrainingDelete:)];                
	[view addSubview:butt];

	butt = [[CPButton alloc] initWithFrame: CGRectMake(horiPos2+240,vertPosi, 80,24)];
	[butt setTitle:"Limpiar"];
	[butt setTarget:self];
	[butt setAction:@selector(organizationTrainingClearFields:)];                
	[view addSubview:butt];

	[tabViewItem setView:view];
				
	[tabView addTabViewItem: tabViewItem];

	[tabView selectFirstTabViewItem:0];

}



// Organizations

- (void)  organizationClearFields:(id)sender {
	[orgaIdenText setStringValue:@""];
	[orgaLawIdenText setStringValue:@""];
	[orgaNameText setStringValue:@""];
	[orgaNameLegaText setStringValue:@""];
	[orgaAddrStreText setStringValue:@""];
	[orgaAddrCityText setStringValue:@""];
	[orgaAddrStatText setStringValue:@""];
	[orgaPhonText setStringValue:@""];
	[orgaEmaiText setStringValue:@""];
	[orgaNoteText setStringValue:@""];
}



// Organizations 

- (void) organizationDelete:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione una organización de la tabla para actualizar."];
		return;
	}

	var iden = [[orgaArra[inde] valueList] objectAtIndex:11];
	var name = [[orgaArra[inde] valueList] objectAtIndex:2];

	var conf = confirm("Quiere eliminar la organizacion *"+name+"* con codigo "+iden+" ?");
	if ( ! conf )
		return;

	var conf = confirm("Por favor reconfirmar la eliminacion la organizacion *"+name+"* con codigo "+iden+". Esto va a eliminar toda la información relacionada a la organización.");
	if ( ! conf )
		return;

	var quer= [CPString stringWithFormat:@"update %@.organizations set deletion=1, Active=0 where idPrimary='%@'",dataBase,iden];
	[self query:quer];
	
	quer = [CPString stringWithFormat:@"DELETE FROM %@.automaticReportDefinitions  WHERE identifier = '%@' AND typeReportId = 1",dataBase,iden];
	[self query:quer];

	[orgaTabl reloadData];
	[orgaLocaTabl reloadData];
	[orgaEmplTabl reloadData];
	
	[self organizationClearFields:0];
}



- (void) organizationFind:(id)sender {

	orgaCond = @"";

	var orgaIden = [orgaIdenText  stringValue];
	if ( [orgaIden length] > 0 )
		orgaCond = [CPString stringWithFormat:@"%@ and id like '%%%@%%'",orgaCond,orgaIden];
	var orgaLawIden = [orgaLawIdenText  stringValue];
	if ( [orgaLawIden length] > 0 )
		orgaCond = [CPString stringWithFormat:@"%@ and lawId like '%%%@%%'",orgaCond,orgaLawIden];
	var orgaName = [orgaNameText  stringValue];
	if ( [orgaName length] > 0 )
		orgaCond = [CPString stringWithFormat:@"%@ and name like '%%%@%%'",orgaCond,orgaName];
	var orgaNameLega = [orgaNameLegaText  stringValue];
	if ( [orgaNameLega length] > 0 )
		orgaCond = [CPString stringWithFormat:@"%@ and nameLegal like '%%%@%%'",orgaCond,orgaNameLega];

	var orgaAddrStre = [orgaAddrStreText  stringValue];
	var orgaAddrCity = [orgaAddrCityText  stringValue];
	var orgaAddrStat = [orgaAddrStatText  stringValue];
	var orgaPhon = [orgaPhonText stringValue];
	var orgaEmai = [orgaEmaiText stringValue];
	var orgaNote = [orgaNoteText stringValue];
        var orgaActi = [orgaActiChec state];

  	if ( [orgaEmai length] > 0)
    		orgaCond = [CPString stringWithFormat:@"%@ and email like '%%%@%%'",orgaCond,orgaEmai];

	[orgaTabl reloadData];
	[orgaLocaTabl reloadData];
	[orgaEmplTabl reloadData];
}



- (void) organizationInsert:(id)sender {
	var orgaIden = [orgaIdenText  stringValue];
	if ( [orgaIden length] == 0 ) {
		[glblAlerPane showWithMessage:@"Atencion" info:"Por favor introduzca el código de la empresa con el formato: nombreEnMinusculaSinGuiones_inicialesPais. Ejemplo: 'routeminer_cr'."];
		//alert("Por favor introduzca el código de la empresa con el formato: nombreEnMinusculaSinGuiones_inicialesPais. Ejemplo: 'routeminer_cr'.");
		return;
	}
	
	var quer= [CPString stringWithFormat:@"select id from %@.organizations where id='%@'",dataBase,orgaIden];
	var arra = [self query:quer];
	var coun = [arra count];
	if ( coun > 0 ) {
		[glblAlerPane showWithMessage:@"El codigo ya existe" info:"Por favor corrobore la lista para encontrarlo o intente de nuevo e introduzca el codigo de la empresa con el formato: nombreEnMinusculaSinGuiones_inicialesPais. Ejemplo: 'routeminer_cr'."];
		//alert("El codigo ya existe. Por favor corrobore la lista para encontrarlo o intente de nuevo e introduzca el codigo de la empresa con el formato: nombreEnMinusculaSinGuiones_inicialesPais. Ejemplo: 'routeminer_cr'.");
		return;
	}

	var orgaLawIden = [orgaLawIdenText  stringValue];
	var orgaName = [orgaNameText  stringValue];
	var orgaNameLega = [orgaNameLegaText  stringValue];
	var orgaAddrStre = [orgaAddrStreText  stringValue];
	var orgaAddrCity = [orgaAddrCityText  stringValue];
	var orgaAddrStat = [orgaAddrStatText  stringValue];
	var orgaPhon = [orgaPhonText stringValue];
	var orgaEmai = [orgaEmaiText stringValue];
	var orgaNote = [orgaNoteText stringValue];
    var orgaActi = [orgaActiChec state];

	quer = [CPString stringWithFormat:@"insert into %@.organizations (id,lawId,name,nameLegal,addressStreet,addressCity,addressState,phone1,email,notes,active,deletion) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',%@,0)",
		dataBase,orgaIden,orgaLawIden,orgaName,orgaNameLega,orgaAddrStre,orgaAddrCity,orgaAddrStat,orgaPhon,orgaEmai,orgaNote,orgaActi];
	var resuO = [self query:quer];
    var coun = [resuO count];
	var orgaId = "";	
	for ( var i=0; i<coun; i++ ) {
		var valuList = [resuO[i] valueList];
		var idO = valuList[0];
		orgaId = idO;
	}
	
	
	quer = [CPString stringWithFormat:@"INSERT INTO %@.automaticReportDefinitions (typeReportId,sendFrequencyId,identifier,dayMonth,contentParameters) VALUES (1,1,'%@','28','order=text')",
								  dataBase,orgaId];
	[self query:quer];
	

	
	
	
	[orgaTabl reloadData];

	var coun = [orgaArra count];
	for ( var i=0; i<coun; i++ ) {
		var valuList = [orgaArra[i] valueList];
		if ( [valuList[0] isEqualToString:orgaIden] ) {
			[orgaTabl scrollRowToVisible:i];
			var indeSet = [[CPIndexSet alloc] init];
			[indeSet addIndex:i];
			[orgaTabl selectRowIndexes:indeSet byExtendingSelection:NO];
			break;
		}
	}

	[orgaLocaTabl reloadData];
	[orgaEmplTabl reloadData];
}



- (void) organizationUpdate:(id)sender {
	var inde = [orgaTabl selectedRow];
	//alert([orgaTabl selectedRow]);
	//alert(inde);
	//alert(inde < 0);
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione una organización de la tabla para actualizar."];
		return;
	}

	var indeArra = [orgaTabl selectedRowIndexes];
 	var coun     = [indeArra count];
 	if ( coun > 1 ) {
		[glblAlerPane showWithMessage:@"Atención" info:"La operación solo se aplica al registro seleccionado, al seleccionar varios, la actualización no tendra efecto."]; 	
		return;
 	}

	var valuList = [orgaArra[inde] valueList];
	var iden = [valuList objectAtIndex:11];
	var code = [valuList objectAtIndex:0];
	var name = [valuList objectAtIndex:2];

	var conf = confirm("Quiere actualizar la organizacion *"+name+"* con codigo "+code+" ?");
	if ( ! conf )
		return;

	var orgaIden = [orgaIdenText  stringValue];
	var orgaLawIden = [orgaLawIdenText  stringValue];
	var orgaName = [orgaNameText  stringValue];
	var orgaNameLega = [orgaNameLegaText  stringValue];
	var orgaAddrStre = [orgaAddrStreText  stringValue];
	var orgaAddrCity = [orgaAddrCityText  stringValue];
	var orgaAddrStat = [orgaAddrStatText  stringValue];
	var orgaPhon = [orgaPhonText stringValue];
	var orgaEmai = [orgaEmaiText stringValue];
	var orgaNote = [orgaNoteText stringValue];
    var orgaActi = [orgaActiChec state];


	var quer= [CPString stringWithFormat:@"update %@.organizations set lawId='%@',name='%@',nameLegal='%@',addressStreet='%@',addressCity='%@',addressState='%@',phone1='%@',email='%@',notes='%@',active=%@ where idPrimary='%@'",
		dataBase,orgaLawIden,orgaName,orgaNameLega,orgaAddrStre,orgaAddrCity,orgaAddrStat,orgaPhon,orgaEmai,orgaNote,orgaActi,iden];
	[self query:quer];
	
	///*
	if (  (prevOrgaIden != orgaIden ) || (prevOrgaName != orgaName )   ){
	   /*
		quer= [CPString stringWithFormat:@"UPDATE %@.eventsPermanent SET customer = '|%@:%@|' WHERE customer = '|%@:%@|'",
		       dataBase,orgaName,orgaIden,prevOrgaName,prevOrgaIden];
		[self query:quer];
		
		
		quer= [CPString stringWithFormat:@"UPDATE %@.automaticReportDefinitions SET identifier = '%@',  emailList = REPLACE(emailList,'iden=%@|type=1','iden=%@|type=1') WHERE typeReportId = 1 AND identifier = '%@' ",
		       dataBase,orgaIden,prevOrgaIden,orgaIden,prevOrgaIden];
		[self query:quer];
		
		//update oganizationLocations = oragaIden where orginazationId = prevOrgaIden
		//update oganizationPersons = oragaIden where orginazationId = prevOrgaIden
		
		
		*/
		//Temporarly, mean while fix the previous update
		[glblAlerPane showWithMessage:@"Atención" info:"De momento al cambiar identificador o nombre de la organización se perderá el enlace con las locaciones, empleados y eventos asociados.\nSe recomienda restablecer el identificador anterior."];
		prevOrgaIden = orgaIden;
		prevOrgaName = orgaName;
		//console.log("update " + quer);
	}
	//*/
	
	if  (  (prevValuOrgaActiChec == "1") && ([orgaActiChec state] == 0) ){
		quer = [CPString stringWithFormat:@"UPDATE %@.automaticReportDefinitions SET active = '0' WHERE typeReportId = 1 AND identifier = '%@' ",
		       dataBase,iden];
		[self query:quer];
		prevValuOrgaActiChec = "0";
	}
	
	
	[orgaTabl reloadData];
	[orgaTabl deselectAll];
	[orgaLocaTabl reloadData];
	[orgaEmplTabl reloadData];
	[self organizationClearFields:0];
}



- (void)  organizationTableSelection:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 )
		return;

	var valuList = [[orgaArra objectAtIndex:inde] valueList];
	[orgaIdenText setStringValue:valuList[0]];
	[orgaLawIdenText setStringValue:valuList[1]];
	[orgaNameText setStringValue:valuList[2]];
	[orgaNameLegaText setStringValue:valuList[3]];
	[orgaAddrStreText setStringValue:valuList[4]];
	[orgaAddrCityText setStringValue:valuList[5]];
	[orgaAddrStatText setStringValue:valuList[6]];
	[orgaPhonText setStringValue:valuList[7]];
	[orgaEmaiText setStringValue:valuList[8]];
	[orgaNoteText setStringValue:valuList[9]];
	[orgaActiChec setStringValue:valuList[10]];
	
	
	prevValuOrgaActiChec = "" + [orgaActiChec state] + "";
	prevOrgaIden = [orgaIdenText  stringValue];
	prevOrgaName = [orgaNameText  stringValue];
	
	//[orgaEmplTabl reloadData];
	//[orgaLocaTabl reloadData];
	//[orgaHoliTabl reloadData];
	//[orgaTraiTabl reloadData];
	
	[self organizationEmployeeClearFields:0];
	[orgaEmplTabl reloadData];

	if ( sender == 0 )
		return;

	[self organizationLocationClearFields:0];
	[self organizationHolidaysClearFields:0];
	[self organizationEmployeeClearFields:0];
	[self organizationTrainingClearFields:0];

}



// Organization Employees

- (void)  organizationEmployeeInsert:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 ) {
		[glblAlerPane showWithMessage:@"Atencion" info:"No se ha seleccionado ninguna locacion"];
		//alert("No se ha seleccionado ninguna organizacion");
		return;
	}

	var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:11];
	var orgaEmplLawIden = [orgaEmplLawIdenText stringValue];
	var orgaEmplNameLast = [orgaEmplNameLastText stringValue];
	var orgaEmplNameFirs = [orgaEmplNameFirsText stringValue];
	var orgaEmplPosi = [orgaEmplPosiText     stringValue];
	var orgaEmplAddrStre = [orgaEmplAddrStreText stringValue];
	var orgaEmplAddrCity = [orgaEmplAddrCityText stringValue];
	var orgaEmplAddrStat = [orgaEmplAddrStatText stringValue];
	var orgaEmplCellPhon1 = [orgaEmplCellPhon1Text stringValue];
	var orgaEmplCellPhon2 = [orgaEmplCellPhon2Text stringValue];
	var orgaEmplEmai = [orgaEmplEmaiText stringValue];
	var orgaEmplNote = [orgaEmplNoteText stringValue];
	var orgaEmplActi = [orgaEmplActiChec state];

	var quer = 0;
	var arra = 0;
	var coun = 0;
	if ( [orgaEmplLawIden length] > 0 ) {
		quer = [CPString stringWithFormat:@"select id,nameLast,nameFirst FROM %@.persons where lawId='%@'",dataBase,orgaEmplLawIden];
		arra = [self query:quer];
		coun = [arra count];
		if ( coun ) {
			var valuList = [arra[0] valueList];
			[glblAlerPane showWithMessage:@"Atencion" info:"Ya existe la persona *"+valuList[1]+", "+valuList[2]+"*  con IDLegal="+orgaEmplLawIden+"  e ID="+valuList[0]];
			//alert("Ya existe la persona *"+valuList[1]+", "+valuList[2]+"*  con IDLegal="+orgaEmplLawIden+"  e ID="+valuList[0]);
			return;
		}
	}

	quer = [CPString stringWithFormat:@"insert into %@.persons (lawId,nameLast,nameFirst,homeAddressStreet,homeAddressCity,homeAddressState,phone1,phone2,email,notes) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",
		dataBase,orgaEmplLawIden,orgaEmplNameLast,orgaEmplNameFirs,orgaEmplAddrStre,orgaEmplAddrCity,orgaEmplAddrStat,orgaEmplCellPhon1,orgaEmplCellPhon2,orgaEmplEmai,orgaEmplNote];
	[self query:quer];

   //quer = @"select max(id) from db.persons";//Original 
    quer =  [CPString stringWithFormat:@"select max(id) from %@.persons",dataBase];
	arra = [self query:quer];
	coun = [arra count];
	if ( coun ) {
		var valuList = [arra[0] valueList];
		quer = [CPString stringWithFormat:@"insert into %@.organizationPersons (position,active,eMail,personId,organizationId) values ('%@',%@,'%@',%@,'%@')",
			dataBase,orgaEmplPosi,orgaEmplActi,orgaEmplEmai,valuList[0],orgaIden];
		[self query:quer];
	}
	[orgaEmplTabl reloadData];

	if ( coun ) {
		var valuList = [arra[0] valueList];
		var iiii = [valuList[0] intValue];
		[orgaEmplTabl scrollRowToVisible:iiii];
		var indeSet = [[CPIndexSet alloc] init];
		[indeSet addIndex:iiii];
		[orgaEmplTabl selectRowIndexes:indeSet byExtendingSelection:NO];
	}
}



- (void)  organizationEmployeeClearFields:(id)sender {
	[orgaEmplLawIdenText setStringValue:@""];
	[orgaEmplNameLastText setStringValue:@""];
	[orgaEmplNameFirsText setStringValue:@""];
	[orgaEmplPosiText     setStringValue:@""];
	[orgaEmplAddrStreText setStringValue:@""];
	[orgaEmplAddrCityText setStringValue:@""];
	[orgaEmplAddrStatText setStringValue:@""];
	[orgaEmplCellPhon1Text setStringValue:@""];
	[orgaEmplCellPhon2Text setStringValue:@""];
	[orgaEmplEmaiText setStringValue:@""];
	[orgaEmplNoteText setStringValue:@""];
	[orgaEmplActiChec setState:0];
}



- (void)  organizationEmployeeDelete:(id)sender {
	var inde = [orgaEmplTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione el empleado de la tabla que desea eliminar."];
		return;
	}
	var emplIden = [[orgaEmplArra[inde] valueList] objectAtIndex:0];
	var orgaEmplNameLast = [[orgaEmplArra[inde] valueList] objectAtIndex:2];
	var orgaEmplNameFirs = [[orgaEmplArra[inde] valueList] objectAtIndex:3];
	
	var conf = confirm("Desea usted borrar a la persona * "+orgaEmplNameLast+", "+orgaEmplNameFirs+" * con id= "+emplIden);
	if ( ! conf )
		return;

	var quer = [CPString stringWithFormat:@"delete from %@.persons where id=%@",dataBase,emplIden];
	[self query:quer];

	quer = [CPString stringWithFormat:@"delete from %@.organizationPersons where personId=%@",dataBase,emplIden];
	[self query:quer];

	[orgaEmplTabl reloadData];

	[self organizationEmployeeClearFields:0];
}



- (void)  organizationEmployeeTableSelection:(id)sender {
	var inde = [orgaEmplTabl selectedRow];
	if (inde < 0){
		return;
	}
	var valuList = [[orgaEmplArra objectAtIndex:inde] valueList];
	[orgaEmplLawIdenText  setStringValue:valuList[1]];
	[orgaEmplNameLastText setStringValue:valuList[2]];
	[orgaEmplNameFirsText setStringValue:valuList[3]];
	[orgaEmplPosiText     setStringValue:valuList[4]];
	[orgaEmplAddrStreText setStringValue:valuList[5]];
	[orgaEmplAddrCityText setStringValue:valuList[6]];
	[orgaEmplAddrStatText setStringValue:valuList[7]];
	[orgaEmplCellPhon1Text setStringValue:valuList[8]];
	[orgaEmplCellPhon2Text setStringValue:valuList[9]];
	[orgaEmplEmaiText setStringValue:valuList[10]];
	[orgaEmplNoteText setStringValue:valuList[11]];
	[orgaEmplActiChec setState:[valuList[12] intValue]];
}



- (void)  organizationEmployeeUpdate:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione un empleado de la tabla para actualizar."];
		return;
	}

	var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:0];

	inde = [orgaEmplTabl selectedRow];
	if ( inde < 0 )
		return;
	var persIden = [[orgaEmplArra[inde] valueList] objectAtIndex:0];

	var orgaEmplNameLast = [[orgaEmplArra[inde] valueList] objectAtIndex:2];
	var orgaEmplNameFirs = [[orgaEmplArra[inde] valueList] objectAtIndex:3];
	
	var conf = confirm("Desea usted actualizar a la persona * "+orgaEmplNameLast+", "+orgaEmplNameFirs+" * con id= "+persIden);
	if ( ! conf )
		return;

	var orgaEmplLawIden = [orgaEmplLawIdenText stringValue];
	var orgaEmplNameLast = [orgaEmplNameLastText stringValue];
	var orgaEmplNameFirs = [orgaEmplNameFirsText stringValue];
	var orgaEmplPosi = [orgaEmplPosiText     stringValue];
	var orgaEmplAddrStre = [orgaEmplAddrStreText stringValue];
	var orgaEmplAddrCity = [orgaEmplAddrCityText stringValue];
	var orgaEmplAddrStat = [orgaEmplAddrStatText stringValue];
	var orgaEmplCellPhon1 = [orgaEmplCellPhon1Text stringValue];
	var orgaEmplCellPhon2 = [orgaEmplCellPhon2Text stringValue];
	var orgaEmplEmai = [orgaEmplEmaiText stringValue];
	var orgaEmplNote = [orgaEmplNoteText stringValue];
	var orgaEmplActi = [orgaEmplActiChec state];

	var quer = [CPString stringWithFormat:@"update %@.persons set lawId='%@',nameLast='%@',nameFirst='%@',homeAddressStreet='%@',homeAddressCity='%@',homeAddressState='%@',phone1='%@',phone2='%@',email='%@',notes='%@' where id=%@",
			dataBase,orgaEmplLawIden,orgaEmplNameLast,orgaEmplNameFirs,orgaEmplAddrStre,orgaEmplAddrCity,orgaEmplAddrStat,orgaEmplCellPhon1,orgaEmplCellPhon2,orgaEmplEmai,orgaEmplNote,persIden];
	[self query:quer];

	quer = [CPString stringWithFormat:@"update %@.organizationPersons set position='%@',active=%@,eMail='%@' where personId=%@ and organizationId='%@'",
			dataBase,orgaEmplPosi,orgaEmplActi,orgaEmplEmai,persIden,orgaIden];
	[self query:quer];

	[orgaEmplTabl reloadData];
}



// Organization Holidays

- (void) organizationHolidaysClearFields:(id)sender {
	[orgaHoliDateStarText setStringValue:@""];
	[orgaHoliDateFiniText setStringValue:@""];
	[orgaHoliDescText setStringValue:@""];
}



- (void) organizationHolidaysDelete:(id)sender {
	var inde = [orgaHoliTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione el feriado de la tabla que desea eliminar."];
		return;
	}

	var iden = [[orgaHoliArra[inde] valueList] objectAtIndex:0];
	var orgaIden = [[orgaHoliArra[inde] valueList] objectAtIndex:1];
	var desc = [[orgaHoliArra[inde] valueList] objectAtIndex:3];

	var conf = confirm("Quiere eliminar el feriado * "+desc+" * de la organizacion * "+orgaIden+" * con codigo "+iden+" ?");
	if ( ! conf )
		return;

	var quer= [CPString stringWithFormat:@"delete from %@.organizationHolidays where id=%@",dataBase,iden];
	[self query:quer];

	[orgaHoliTabl reloadData];
}



- (void) organizationHolidaysInsert:(id)sender {

	var holiDateStar = [orgaHoliDateStarText  stringValue];
	var holiDateFini = [orgaHoliDateFiniText  stringValue];
	var holiDesc = [orgaHoliDescText  stringValue];

	var inde = [orgaTabl selectedRow];
	if ( inde < 0 ) {
		[glblAlerPane showWithMessage:@"No procede." info:@"Debe seleccionar una organizacion antes de ejecutar cualquier accion"];
		return;
	}
	var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:11];

	var quer= [CPString stringWithFormat:@"insert into %@.organizationHolidays (organizationId,dateStart,dateFinish,description) values ('%@','%@','%@','%@')",
		dataBase,orgaIden,holiDateStar,holiDateFini,holiDesc];

	[self query:quer];

	[orgaHoliTabl reloadData];
}



- (void) organizationHolidaysTableSelection:(id)sender {
	var inde = [orgaHoliTabl selectedRow];
	if ( inde < 0 )
		return;

	var valuList = [orgaHoliArra[inde] valueList];
	var holiDateStar = [valuList objectAtIndex:1];
	var holiDateFini = [valuList objectAtIndex:2];
	var holiDesc = [valuList objectAtIndex:3];
	
	[orgaHoliDateStarText setStringValue:holiDateStar];
	[orgaHoliDateFiniText setStringValue:holiDateFini];
	[orgaHoliDescText setStringValue:holiDesc];
}



- (void) organizationHolidaysUpdate:(id)sender {
	var inde = [orgaHoliTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione un feriado de la tabla para actualizar."];
		return;
	}

	var iden = [[orgaHoliArra[inde] valueList] objectAtIndex:0];

	var holiDateStar = [orgaHoliDateStarText  stringValue];
	var holiDateFini = [orgaHoliDateFiniText  stringValue];
	var holiDesc = [orgaHoliDescText  stringValue];

	var quer= [CPString stringWithFormat:@"update %@.organizationHolidays set dateStart='%@',dateFinish='%@',description='%@' where id=%@",
		dataBase,holiDateStar,holiDateFini,holiDesc,iden];

	[self query:quer];

	[orgaHoliTabl reloadData];
}


// Organization Locations 

- (void) organizationLocationCheck:(id)sender {
	if ( sender == orgaLocaActiSundChec && [orgaLocaActiSundChec state] ) {
		[orgaLocaHourStarSundText setStringValue:@"00:00"];
		[orgaLocaHourFiniSundText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiSundChec && ![orgaLocaActiSundChec state] ) {
		[orgaLocaHourStarSundText setStringValue:@""];
		[orgaLocaHourFiniSundText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiMondChec && [orgaLocaActiMondChec state] ) {
		[orgaLocaHourStarMondText setStringValue:@"00:00"];
		[orgaLocaHourFiniMondText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiMondChec && ![orgaLocaActiMondChec state] ) {
		[orgaLocaHourStarMondText setStringValue:@""];
		[orgaLocaHourFiniMondText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiTuesChec && [orgaLocaActiTuesChec state] ) {
		[orgaLocaHourStarTuesText setStringValue:@"00:00"];
		[orgaLocaHourFiniTuesText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiTuesChec && ![orgaLocaActiTuesChec state] ) {
		[orgaLocaHourStarTuesText setStringValue:@""];
		[orgaLocaHourFiniTuesText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiWednChec && [orgaLocaActiWednChec state] ) {
		[orgaLocaHourStarWednText setStringValue:@"00:00"];
		[orgaLocaHourFiniWednText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiWednChec && ![orgaLocaActiWednChec state] ) {
		[orgaLocaHourStarWednText setStringValue:@""];
		[orgaLocaHourFiniWednText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiThurChec && [orgaLocaActiThurChec state] ) {
		[orgaLocaHourStarThurText setStringValue:@"00:00"];
		[orgaLocaHourFiniThurText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiThurChec && ![orgaLocaActiThurChec state] ) {
		[orgaLocaHourStarThurText setStringValue:@""];
		[orgaLocaHourFiniThurText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiFridChec && [orgaLocaActiFridChec state] ) {
		[orgaLocaHourStarFridText setStringValue:@"00:00"];
		[orgaLocaHourFiniFridText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiFridChec && ![orgaLocaActiFridChec state] ) {
		[orgaLocaHourStarFridText setStringValue:@""];
		[orgaLocaHourFiniFridText setStringValue:@""];
	}
	else if ( sender == orgaLocaActiSatuChec && [orgaLocaActiSatuChec state] ) {
		[orgaLocaHourStarSatuText setStringValue:@"00:00"];
		[orgaLocaHourFiniSatuText setStringValue:@"23:59"];
	}
	else if ( sender == orgaLocaActiSatuChec && ![orgaLocaActiSatuChec state] ) {
		[orgaLocaHourStarSatuText setStringValue:@""];
		[orgaLocaHourFiniSatuText setStringValue:@""];
	}
}



- (void) organizationLocationClearFields:(id)sender{
	[orgaLocaIdenText setStringValue:@""];
	[orgaLocaNameText setStringValue:@""];
	[orgaLocaBuilText setStringValue:@""];
	[orgaLocaGateText setStringValue:@""];
	[orgaLocaAddrStreText setStringValue:@""];
	[orgaLocaAddrCityText setStringValue:@""];
	[orgaLocaAddrStatText setStringValue:@""];
	[orgaLocaPhonText setStringValue:@""];
	[orgaLocaEmaiText setStringValue:@""];
	[orgaLocaRestText setStringValue:@""];
	[orgaLocaNoteText setStringValue:@""];
	[orgaLocaLngdText setStringValue:@""];
	[orgaLocaLatiText setStringValue:@""];
	[orgaLocaColoText setStringValue:@""];
	[orgaLocaCont0Popu selectItemAtIndex:0];
	[orgaLocaCont1Popu selectItemAtIndex:0];
	[orgaLocaCont2Popu selectItemAtIndex:0];
	[orgaLocaCont3Popu selectItemAtIndex:0];
	[orgaLocaCorpCont0Popu selectItemAtIndex:0];
	[orgaLocaCorpCont1Popu selectItemAtIndex:0];
	[orgaLocaCorpCont2Popu selectItemAtIndex:0];
	[orgaLocaCorpCont3Popu selectItemAtIndex:0];
    [orgaLocaActiChec setState:0];
    prevValuOrgaLocaActiChec = "0";
    //prevValuOrgaActiChec = "0";
    
	[orgaLocaActiSundChec setState:0];
	[orgaLocaActiMondChec setState:0];
	[orgaLocaActiTuesChec setState:0];
	[orgaLocaActiWednChec setState:0];
	[orgaLocaActiThurChec setState:0];
	[orgaLocaActiFridChec setState:0];
	[orgaLocaActiSatuChec setState:0];
	[orgaLocaHourStarSundText setStringValue:@""];
	[orgaLocaHourStarMondText setStringValue:@""];
	[orgaLocaHourStarTuesText setStringValue:@""];
	[orgaLocaHourStarWednText setStringValue:@""];
	[orgaLocaHourStarThurText setStringValue:@""];
	[orgaLocaHourStarFridText setStringValue:@""];
	[orgaLocaHourStarSatuText setStringValue:@""];
	[orgaLocaHourFiniSundText setStringValue:@""];
	[orgaLocaHourFiniMondText setStringValue:@""];
	[orgaLocaHourFiniTuesText setStringValue:@""];
	[orgaLocaHourFiniWednText setStringValue:@""];
	[orgaLocaHourFiniThurText setStringValue:@""];
	[orgaLocaHourFiniFridText setStringValue:@""];
	[orgaLocaHourFiniSatuText setStringValue:@""];
}



- (void) organizationLocationDelete:(id)sender {
	var inde = [orgaLocaTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione la locación de la tabla que desea eliminar."];
		return;
	}

	var iden = [[orgaLocaArra[inde] valueList] objectAtIndex:0];
	var name = [[orgaLocaArra[inde] valueList] objectAtIndex:1];

	var conf = confirm("Quiere eliminar la locacion * "+name+" * con codigo "+iden+" ?");
	if ( ! conf )
		return;

	var quer= [CPString stringWithFormat:@"delete from %@.organizationLocations where id='%@'",dataBase,iden];
	[self query:quer];
	
	
	var quer= [CPString stringWithFormat:@"DELETE FROM %@.automaticReportDefinitions WHERE identifier='%@' AND typeReportId = 2",dataBase,iden];
	[self query:quer];

	[orgaLocaTabl reloadData];
}



- (void) organizationLocationInsert:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 )
		return;

	var orgaLocaName = [orgaLocaNameText  stringValue];
	var orgaLocaBuil = [orgaLocaBuilText  stringValue];
	var orgaLocaGate = [orgaLocaGateText stringValue];
	var orgaLocaAddrStre = [orgaLocaAddrStreText stringValue];
	var orgaLocaAddrCity = [orgaLocaAddrCityText stringValue];
	var orgaLocaAddrStat = [orgaLocaAddrStatText stringValue];
	var orgaLocaPhon = [orgaLocaPhonText stringValue];
	var orgaLocaEmai = [orgaLocaEmaiText stringValue];
	var orgaLocaRest = [orgaLocaRestText stringValue];
	var orgaLocaNote = [orgaLocaNoteText stringValue];
	var orgaLocaLngd = [orgaLocaLngdText floatValue];
	var orgaLocaLati = [orgaLocaLatiText floatValue];
	var orgaLocaColo = [orgaLocaColoText stringValue];
	if ( [orgaLocaColo length] != 4 )
	    orgaLocaColo = [self getRandomColor];
	//alert(orgaLocaColo);
    var orgaLocaCont0 = [orgaLocaCont0Popu titleOfSelectedItem];
    var orgaLocaCont1 = [orgaLocaCont1Popu titleOfSelectedItem];
    var orgaLocaCont2 = [orgaLocaCont2Popu titleOfSelectedItem];
    var orgaLocaCont3 = [orgaLocaCont3Popu titleOfSelectedItem];
    var orgaLocaCorpCont0 = [orgaLocaCorpCont0Popu titleOfSelectedItem];
    var orgaLocaCorpCont1 = [orgaLocaCorpCont1Popu titleOfSelectedItem];
    var orgaLocaCorpCont2 = [orgaLocaCorpCont2Popu titleOfSelectedItem];
    var orgaLocaCorpCont3 = [orgaLocaCorpCont3Popu titleOfSelectedItem];
    if ( ![orgaLocaCont1 isEqualToString:@"Contactos"] && [orgaLocaCont1 isEqualToString:orgaLocaCont0] 
    	|| ![orgaLocaCont2 isEqualToString:@"Contactos"] && ([orgaLocaCont2 isEqualToString:orgaLocaCont0] || [orgaLocaCont2 isEqualToString:orgaLocaCont1]) 
    	|| ![orgaLocaCont3 isEqualToString:@"Contactos"] && ([orgaLocaCont3 isEqualToString:orgaLocaCont0] || [orgaLocaCont3 isEqualToString:orgaLocaCont1] || [orgaLocaCont3 isEqualToString:orgaLocaCont2]) 
    	|| ![orgaLocaCorpCont1 isEqualToString:@"Contactos"] && [orgaLocaCorpCont1 isEqualToString:orgaLocaCorpCont0] 
    	|| ![orgaLocaCorpCont2 isEqualToString:@"Contactos"] && ([orgaLocaCorpCont2 isEqualToString:orgaLocaCorpCont0] || [orgaLocaCorpCont2 isEqualToString:orgaLocaCorpCont1]) 
    	|| ![orgaLocaCorpCont3 isEqualToString:@"Contactos"] && ([orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont0] || [orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont1] || [orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont2]) 
    	) {
    	[glblAlerPane showWithMessage:@"Contacto repetido en el mismo bloque" info:"Por favor elimine la redundancia"];
    	//alert("Existe una repeticion de contacto dentro del mismo bloque. Por favor elimine redundancias.");
    	return;
    }

    var orgaLocaActi = [orgaLocaActiChec state];
    
        //add 2017-03-10
    if ( ! orgaLocaCont0 || orgaLocaCont0 && [orgaLocaCont0 isEqualToString:@"Contactos"] )
		orgaLocaCont0 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont0 componentsSeparatedByString:@":"];
		orgaLocaCont0 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont1 || orgaLocaCont1 && [orgaLocaCont1 isEqualToString:@"Contactos"] )
		orgaLocaCont1 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont1 componentsSeparatedByString:@":"];
		orgaLocaCont1 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont2 || orgaLocaCont2 && [orgaLocaCont2 isEqualToString:@"Contactos"] )
		orgaLocaCont2 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont2 componentsSeparatedByString:@":"];
		orgaLocaCont2 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont3 || orgaLocaCont3 && [orgaLocaCont3 isEqualToString:@"Contactos"] )
		orgaLocaCont3 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont3 componentsSeparatedByString:@":"];
		orgaLocaCont3 = orgaLocaContArra[1];
	}

	if ( ! orgaLocaCorpCont0 || orgaLocaCorpCont0 && [orgaLocaCorpCont0 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont0 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont0 componentsSeparatedByString:@":"];
		orgaLocaCorpCont0 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont1 || orgaLocaCorpCont1 && [orgaLocaCorpCont1 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont1 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont1 componentsSeparatedByString:@":"];
		orgaLocaCorpCont1 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont2 || orgaLocaCorpCont2 && [orgaLocaCorpCont2 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont2 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont2 componentsSeparatedByString:@":"];
		orgaLocaCorpCont2 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont3 || orgaLocaCorpCont3 && [orgaLocaCorpCont3 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont3 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont3 componentsSeparatedByString:@":"];
		orgaLocaCorpCont3 = orgaLocaCorpContArra[1];
	}
    
    var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:11];
	var quer= [CPString stringWithFormat:@"insert into %@.organizationLocations (organizationId,name,addressBuilding,addressGate,addressStreet,addressCity,addressState,phone,email,restrictions,notes,locationLongitude,locationLatitude,active,color,contact0Id,contact1Id,contact2Id,contact3Id,contactCorporate0Id,contactCorporate1Id,contactCorporate2Id,contactCorporate3Id) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',%@,%@,%@,'%@',%@,%@,%@,%@,%@,%@,%@,%@)",
	dataBase,orgaIden,orgaLocaName,orgaLocaBuil,orgaLocaGate,orgaLocaAddrStre,orgaLocaAddrCity,orgaLocaAddrStat,orgaLocaPhon,orgaLocaEmai,orgaLocaRest,orgaLocaNote,orgaLocaLngd,orgaLocaLati,orgaLocaActi,orgaLocaColo,orgaLocaCont0,orgaLocaCont1,orgaLocaCont2,orgaLocaCont3,orgaLocaCorpCont0,orgaLocaCorpCont1,orgaLocaCorpCont2,orgaLocaCorpCont3];
//end
	

/*
	var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:0];
	var quer= [CPString stringWithFormat:@"insert into %@.organizationLocations (name,addressBuilding,addressGate,addressStreet,addressCity,addressState,phone,email,restrictions,notes,locationLongitude,locationLatitude,active,color,contact0Id,contact1Id,contact2Id,contact3Id,contactCorporate0Id,contactCorporate1Id,contactCorporate2Id,contactCorporate3Id,organizationId) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',%@,%@,%@,'%@','%@','%@','%@','%@','%@')",
		dataBase,orgaLocaName,orgaLocaBuil,orgaLocaGate,orgaLocaAddrStre,orgaLocaAddrCity,orgaLocaAddrStat,orgaLocaPhon,orgaLocaEmai,orgaLocaRest,orgaLocaNote,orgaLocaLngd,orgaLocaLati,orgaLocaActi,orgaLocaColo,orgaLocaCont0,orgaLocaCont1,orgaLocaCont2,orgaLocaCont3,orgaLocaCorpCont0,orgaLocaCorpCont1,orgaLocaCorpCont2,orgaLocaCorpCont3,orgaIden];
*/
	//console.log(quer);
	[self query:quer];
	
	//quer = @"select max(id) from db.organizationLocations"; //ORIGINAL
	quer= [CPString stringWithFormat:@"select max(id) from %@.organizationLocations",dataBase];
	var arra = [self query:quer];

	[orgaLocaTabl reloadData];

	var orgaLocaIden = 0;
	
	var coun = [arra count];
	if ( coun ) {
		var valuList = [arra[0] valueList];
		orgaLocaIden = [valuList[0] intValue];
		quer = [CPString stringWithFormat:@"INSERT INTO %@.automaticReportDefinitions (typeReportId,sendFrequencyId,identifier,prevDays,nextGeneDate) VALUES (%@,%@,'%@',%@,curdate() + interval 20 hour + interval 30 minute)",
								  dataBase,2,4,orgaLocaIden,3];
		//console.log("query = " + quer);
		[self query:quer];
	}

	coun = [orgaLocaArra count];
	for ( var i=0; i<coun; i++ ) {
		var valuList = [arra[0] valueList];
		var orgaLocaTempIden = valuList[0];
		if ( [orgaLocaTempIden isEqualToString:orgaLocaIden] ) {
			[orgaLocaTabl scrollRowToVisible:i];
			var indeSet = [[CPIndexSet alloc] init];
			[indeSet addIndex:i];
			[orgaLocaTabl selectRowIndexes:indeSet byExtendingSelection:NO];
			break;
		}
	}
	//add 2017-03-03
	quer = @"insert into " + dataBase + ".locationSchedule (day,timeStart,timeFinish,organizationLocationId) values "
	var chan = NO;

	var orgaLocaActiSund = [orgaLocaActiSundChec state];
	var orgaLocaHourStarSund = [orgaLocaHourStarSundText stringValue];
	var orgaLocaHourFiniSund = [orgaLocaHourFiniSundText stringValue];
	if ( orgaLocaActiSund ) {
		quer = [CPString stringWithFormat:@"%@ (0,'%@','%@',%@)",quer,orgaLocaHourStarSund,orgaLocaHourFiniSund,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiMond = [orgaLocaActiMondChec state];
	var orgaLocaHourStarMond = [orgaLocaHourStarMondText stringValue];
	var orgaLocaHourFiniMond = [orgaLocaHourFiniMondText stringValue];
	if ( orgaLocaActiMond ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (1,'%@','%@',%@)",quer,orgaLocaHourStarMond,orgaLocaHourFiniMond,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (1,'%@','%@',%@)",quer,orgaLocaHourStarMond,orgaLocaHourFiniMond,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiTues = [orgaLocaActiTuesChec state];
	var orgaLocaHourStarTues = [orgaLocaHourStarTuesText stringValue];
	var orgaLocaHourFiniTues = [orgaLocaHourFiniTuesText stringValue];
	if ( orgaLocaActiTues ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (2,'%@','%@',%@)",quer,orgaLocaHourStarTues,orgaLocaHourFiniTues,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (2,'%@','%@',%@)",quer,orgaLocaHourStarTues,orgaLocaHourFiniTues,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiWedn = [orgaLocaActiWednChec state];
	var orgaLocaHourStarWedn = [orgaLocaHourStarWednText stringValue];
	var orgaLocaHourFiniWedn = [orgaLocaHourFiniWednText stringValue];
	if ( orgaLocaActiWedn ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (3,'%@','%@',%@)",quer,orgaLocaHourStarWedn,orgaLocaHourFiniWedn,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (3,'%@','%@',%@)",quer,orgaLocaHourStarWedn,orgaLocaHourFiniWedn,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiThur = [orgaLocaActiThurChec state];
	var orgaLocaHourStarThur = [orgaLocaHourStarThurText stringValue];
	var orgaLocaHourFiniThur = [orgaLocaHourFiniThurText stringValue];
	if ( orgaLocaActiThur ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (4,'%@','%@',%@)",quer,orgaLocaHourStarThur,orgaLocaHourFiniThur,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (4,'%@','%@',%@)",quer,orgaLocaHourStarThur,orgaLocaHourFiniThur,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiFrid = [orgaLocaActiFridChec state];
	var orgaLocaHourStarFrid = [orgaLocaHourStarFridText stringValue];
	var orgaLocaHourFiniFrid = [orgaLocaHourFiniFridText stringValue];
	if ( orgaLocaActiFrid ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (5,'%@','%@',%@)",quer,orgaLocaHourStarFrid,orgaLocaHourFiniFrid,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (5,'%@','%@',%@)",quer,orgaLocaHourStarFrid,orgaLocaHourFiniFrid,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiSatu = [orgaLocaActiSatuChec state];
	var orgaLocaHourStarSatu = [orgaLocaHourStarSatuText stringValue];
	var orgaLocaHourFiniSatu = [orgaLocaHourFiniSatuText stringValue];
	if ( orgaLocaActiSatu ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (6,'%@','%@',%@)",quer,orgaLocaHourStarSatu,orgaLocaHourFiniSatu,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (6,'%@','%@',%@)",quer,orgaLocaHourStarSatu,orgaLocaHourFiniSatu,orgaLocaIden];
		chan = YES;
	}

	if ( chan )
		[self query:quer];
		//console.log(quer);
}



- (void) organizationLocationPopupSelection:(id)sender {
	var titl = [sender titleOfSelectedItem];
}



- (void) organizationLocationUpdate:(id)sender {
	var inde = [orgaLocaTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione una locación de la tabla para actualizar."];
		return;
	}


	var orgaLocaIden = [[orgaLocaArra[inde] valueList] objectAtIndex:0];


	var name = [[orgaLocaArra[inde] valueList] objectAtIndex:1];

	var conf = confirm("Quiere actualizar la locacion * "+name+" * con codigo "+orgaLocaIden+" ?");
	if ( ! conf )
		return;

	var orgaLocaName = [orgaLocaNameText  stringValue];
	var orgaLocaBuil = [orgaLocaBuilText  stringValue];
	var orgaLocaGate = [orgaLocaGateText stringValue];
	var orgaLocaAddrStre = [orgaLocaAddrStreText stringValue];
	var orgaLocaAddrCity = [orgaLocaAddrCityText stringValue];
	var orgaLocaAddrStat = [orgaLocaAddrStatText stringValue];
	var orgaLocaPhon = [orgaLocaPhonText stringValue];
	var orgaLocaEmai = [orgaLocaEmaiText stringValue];
	var orgaLocaRest = [orgaLocaRestText stringValue];
	var orgaLocaNote = [orgaLocaNoteText stringValue];
	var orgaLocaLngd = [orgaLocaLngdText stringValue];
	var orgaLocaLati = [orgaLocaLatiText stringValue];
	var orgaLocaColo = [orgaLocaColoText stringValue];
	if ( [orgaLocaColo length] != 4 ) {
	    orgaLocaColo = [self getRandomColor];
    	//alert(orgaLocaColo);
    }
    var orgaLocaCont0 = [orgaLocaCont0Popu titleOfSelectedItem];
    var orgaLocaCont1 = [orgaLocaCont1Popu titleOfSelectedItem];
    var orgaLocaCont2 = [orgaLocaCont2Popu titleOfSelectedItem];
    var orgaLocaCont3 = [orgaLocaCont3Popu titleOfSelectedItem];
    var orgaLocaCorpCont0 = [orgaLocaCorpCont0Popu titleOfSelectedItem];
    var orgaLocaCorpCont1 = [orgaLocaCorpCont1Popu titleOfSelectedItem];
    var orgaLocaCorpCont2 = [orgaLocaCorpCont2Popu titleOfSelectedItem];
    var orgaLocaCorpCont3 = [orgaLocaCorpCont3Popu titleOfSelectedItem];
	if ( ![orgaLocaCont1 isEqualToString:@"Contactos"] && [orgaLocaCont1 isEqualToString:orgaLocaCont0] 
    	|| ![orgaLocaCont2 isEqualToString:@"Contactos"] && ([orgaLocaCont2 isEqualToString:orgaLocaCont0] || [orgaLocaCont2 isEqualToString:orgaLocaCont1]) 
    	|| ![orgaLocaCont3 isEqualToString:@"Contactos"] && ([orgaLocaCont3 isEqualToString:orgaLocaCont0] || [orgaLocaCont3 isEqualToString:orgaLocaCont1] || [orgaLocaCont3 isEqualToString:orgaLocaCont2]) 
    	|| ![orgaLocaCorpCont1 isEqualToString:@"Contactos"] && [orgaLocaCorpCont1 isEqualToString:orgaLocaCorpCont0] 
    	|| ![orgaLocaCorpCont2 isEqualToString:@"Contactos"] && ([orgaLocaCorpCont2 isEqualToString:orgaLocaCorpCont0] || [orgaLocaCorpCont2 isEqualToString:orgaLocaCorpCont1]) 
    	|| ![orgaLocaCorpCont3 isEqualToString:@"Contactos"] && ([orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont0] || [orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont1] || [orgaLocaCorpCont3 isEqualToString:orgaLocaCorpCont2]) 
    	) {
		[glblAlerPane showWithMessage:@"Contacto repetido en el mismo bloque" info:"Por favor elimine la redundancia"];
    	//alert("Existe una repeticion de contacto dentro del mismo bloque. Por favor elimine redundancias.");
    	return;
    }

	var orgaLocaActi = [orgaLocaActiChec state];
	

	if ( ! orgaLocaCont0 || orgaLocaCont0 && [orgaLocaCont0 isEqualToString:@"Contactos"] )
		orgaLocaCont0 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont0 componentsSeparatedByString:@":"];
		orgaLocaCont0 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont1 || orgaLocaCont1 && [orgaLocaCont1 isEqualToString:@"Contactos"] )
		orgaLocaCont1 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont1 componentsSeparatedByString:@":"];
		orgaLocaCont1 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont2 || orgaLocaCont2 && [orgaLocaCont2 isEqualToString:@"Contactos"] )
		orgaLocaCont2 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont2 componentsSeparatedByString:@":"];
		orgaLocaCont2 = orgaLocaContArra[1];
	}
	if ( ! orgaLocaCont3 || orgaLocaCont3 && [orgaLocaCont3 isEqualToString:@"Contactos"] )
		orgaLocaCont3 = 0;
	else {
		var orgaLocaContArra = [orgaLocaCont3 componentsSeparatedByString:@":"];
		orgaLocaCont3 = orgaLocaContArra[1];
	}

	if ( ! orgaLocaCorpCont0 || orgaLocaCorpCont0 && [orgaLocaCorpCont0 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont0 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont0 componentsSeparatedByString:@":"];
		orgaLocaCorpCont0 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont1 || orgaLocaCorpCont1 && [orgaLocaCorpCont1 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont1 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont1 componentsSeparatedByString:@":"];
		orgaLocaCorpCont1 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont2 || orgaLocaCorpCont2 && [orgaLocaCorpCont2 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont2 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont2 componentsSeparatedByString:@":"];
		orgaLocaCorpCont2 = orgaLocaCorpContArra[1];
	}
	if ( ! orgaLocaCorpCont3 || orgaLocaCorpCont3 && [orgaLocaCorpCont3 isEqualToString:@"Contactos"] )
		orgaLocaCorpCont3 = 0;
	else {
		var orgaLocaCorpContArra = [orgaLocaCorpCont3 componentsSeparatedByString:@":"];
		orgaLocaCorpCont3 = orgaLocaCorpContArra[1];
	}

	var quer= [CPString stringWithFormat:@"update %@.organizationLocations set name='%@',addressBuilding='%@',addressGate='%@',addressStreet='%@',addressCity='%@',addressState='%@',phone='%@',email='%@',restrictions='%@',notes='%@',locationLongitude=%@,locationLatitude=%@,color='%@',contact0Id=%@,contact1Id=%@,contact2Id=%@,contact3Id=%@,contactCorporate0Id=%@,contactCorporate1Id=%@,contactCorporate2Id=%@,contactCorporate3Id=%@,active=%@ where id='%@'",
		dataBase,orgaLocaName,orgaLocaBuil,orgaLocaGate,orgaLocaAddrStre,orgaLocaAddrCity,orgaLocaAddrStat,orgaLocaPhon,orgaLocaEmai,orgaLocaRest,orgaLocaNote,orgaLocaLngd,orgaLocaLati,orgaLocaColo,orgaLocaCont0,orgaLocaCont1,orgaLocaCont2,orgaLocaCont3,orgaLocaCorpCont0,orgaLocaCorpCont1,orgaLocaCorpCont2,orgaLocaCorpCont3,orgaLocaActi,orgaLocaIden];

	[self query:quer];
	
	var  arraEmai = [[CPArray alloc] init];
	
	for (var i = 0; i < [orgaEmplArra count]; i ++) {
		var valuList = [orgaEmplArra[i] valueList];
		var iden = valuList[0];
		var emai = valuList[10];
		
		switch ( iden ){
				case orgaLocaCont0:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCont1:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCont2:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCont3:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCorpCont0:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCorpCont1:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCorpCont2:
					[arraEmai addObject:@"" + emai ];	
					break;
				case orgaLocaCorpCont3:
					[arraEmai addObject:@"" + emai ];	
					break;					
				default:
				break;		
			}
	}
	
	
	 var striEmai = "";
	for(var kk = 0; kk < [arraEmai count]; kk ++ ){
		var aux = kk + 1;
		if (aux ==  [arraEmai count] ){
			striEmai += arraEmai[kk];
		}else{
			striEmai +=  arraEmai[kk] + ":sc:" ;
		}
		
	}

	if  (  (prevValuOrgaLocaActiChec == "1") && (orgaLocaActi == 0) ){
		quer = [CPString stringWithFormat:@"UPDATE %@.automaticReportDefinitions set active = 0, emailList = '%@' WHERE identifier = '%@' AND typeReportId = 2",
										  dataBase,striEmai,orgaLocaIden];	
	}else{
		quer = [CPString stringWithFormat:@"UPDATE %@.automaticReportDefinitions set emailList = '%@' WHERE identifier = '%@' AND typeReportId = 2",
										  dataBase,striEmai,orgaLocaIden];
	}
	//console.log("quer = " + quer);
	[self query: quer];
	
	/**
	if (  (prevLocaIden != orgaLocaIden ) || (prevLocaName != orgaLocaName )   ){
		quer= [CPString stringWithFormat:@"UPDATE %@.scheduleSeriesDefinitions SET location = '%@:%@' WHERE location = '%@:%@'",
		       dataBase,orgaLocaName,orgaLocaIden,prevLocaName,prevLocaIden];
		[self query:quer];
		//console.log("update " + quer);
		
		quer= [CPString stringWithFormat:@"UPDATE %@.eventsPermanent SET text = REPLACE(text, '%@', '%@'), location = '|%@:%@|' WHERE location = '|%@:%@|'",
		       dataBase,prevLocaName,orgaLocaName,orgaLocaName,orgaLocaIden,prevLocaName,prevLocaIden];
		[self query:quer];
		//console.log("update " + quer);
		
		prevLocaIden = orgaLocaIden;
		prevLocaName = orgaLocaName;		
	}
	**/
	

	[orgaLocaTabl reloadData];


	quer = [CPString stringWithFormat:@"delete from %@.locationSchedule where organizationLocationId=%@",dataBase,orgaLocaIden];

	[self query:quer];

	//modified 2017-03-03
	quer = @"insert into " + dataBase + ".locationSchedule (day,timeStart,timeFinish,organizationLocationId) values "
	var chan = NO;

	var orgaLocaActiSund = [orgaLocaActiSundChec state];
	var orgaLocaHourStarSund = [orgaLocaHourStarSundText stringValue];
	var orgaLocaHourFiniSund = [orgaLocaHourFiniSundText stringValue];
	if ( orgaLocaActiSund ) {
		quer = [CPString stringWithFormat:@"%@ (0,'%@','%@',%@)",quer,orgaLocaHourStarSund,orgaLocaHourFiniSund,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiMond = [orgaLocaActiMondChec state];
	var orgaLocaHourStarMond = [orgaLocaHourStarMondText stringValue];
	var orgaLocaHourFiniMond = [orgaLocaHourFiniMondText stringValue];
	if ( orgaLocaActiMond ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (1,'%@','%@',%@)",quer,orgaLocaHourStarMond,orgaLocaHourFiniMond,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (1,'%@','%@',%@)",quer,orgaLocaHourStarMond,orgaLocaHourFiniMond,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiTues = [orgaLocaActiTuesChec state];
	var orgaLocaHourStarTues = [orgaLocaHourStarTuesText stringValue];
	var orgaLocaHourFiniTues = [orgaLocaHourFiniTuesText stringValue];
	if ( orgaLocaActiTues ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (2,'%@','%@',%@)",quer,orgaLocaHourStarTues,orgaLocaHourFiniTues,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (2,'%@','%@',%@)",quer,orgaLocaHourStarTues,orgaLocaHourFiniTues,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiWedn = [orgaLocaActiWednChec state];
	var orgaLocaHourStarWedn = [orgaLocaHourStarWednText stringValue];
	var orgaLocaHourFiniWedn = [orgaLocaHourFiniWednText stringValue];
	if ( orgaLocaActiWedn ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (3,'%@','%@',%@)",quer,orgaLocaHourStarWedn,orgaLocaHourFiniWedn,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (3,'%@','%@',%@)",quer,orgaLocaHourStarWedn,orgaLocaHourFiniWedn,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiThur = [orgaLocaActiThurChec state];
	var orgaLocaHourStarThur = [orgaLocaHourStarThurText stringValue];
	var orgaLocaHourFiniThur = [orgaLocaHourFiniThurText stringValue];
	if ( orgaLocaActiThur ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (4,'%@','%@',%@)",quer,orgaLocaHourStarThur,orgaLocaHourFiniThur,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (4,'%@','%@',%@)",quer,orgaLocaHourStarThur,orgaLocaHourFiniThur,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiFrid = [orgaLocaActiFridChec state];
	var orgaLocaHourStarFrid = [orgaLocaHourStarFridText stringValue];
	var orgaLocaHourFiniFrid = [orgaLocaHourFiniFridText stringValue];
	if ( orgaLocaActiFrid ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (5,'%@','%@',%@)",quer,orgaLocaHourStarFrid,orgaLocaHourFiniFrid,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (5,'%@','%@',%@)",quer,orgaLocaHourStarFrid,orgaLocaHourFiniFrid,orgaLocaIden];
		chan = YES;
	}

	var orgaLocaActiSatu = [orgaLocaActiSatuChec state];
	var orgaLocaHourStarSatu = [orgaLocaHourStarSatuText stringValue];
	var orgaLocaHourFiniSatu = [orgaLocaHourFiniSatuText stringValue];
	if ( orgaLocaActiSatu ) {
		if ( chan )
			quer = [CPString stringWithFormat:@"%@, (6,'%@','%@',%@)",quer,orgaLocaHourStarSatu,orgaLocaHourFiniSatu,orgaLocaIden];
		else
			quer = [CPString stringWithFormat:@"%@ (6,'%@','%@',%@)",quer,orgaLocaHourStarSatu,orgaLocaHourFiniSatu,orgaLocaIden];
		chan = YES;
	}

	if ( chan )
		[self query:quer];
}



- (void) organizationLocationTableSelection:(id)sender {
	var inde = [orgaLocaTabl selectedRow];
	
	if (inde < 0){
		return;
	}

	[self organizationLocationClearFields:0];

	var valuList = [[orgaLocaArra objectAtIndex:inde] valueList];
	var locaIden = valuList[0];
	[orgaLocaIdenText setStringValue:locaIden];
	[orgaLocaNameText setStringValue:valuList[1]];
	[orgaLocaBuilText setStringValue:valuList[2]];
	[orgaLocaGateText setStringValue:valuList[3]];
	[orgaLocaAddrStreText setStringValue:valuList[4]];
	[orgaLocaAddrCityText setStringValue:valuList[5]];
	[orgaLocaAddrStatText setStringValue:valuList[6]];
	[orgaLocaPhonText setStringValue:valuList[7]];
	[orgaLocaEmaiText setStringValue:valuList[8]];
	[orgaLocaRestText setStringValue:valuList[9]];
	[orgaLocaNoteText setStringValue:valuList[10]];

	var lngd = [valuList[11] floatValue]
	var lati = [valuList[12] floatValue]
	[orgaLocaLngdText setFloatValue:lngd];
	[orgaLocaLatiText setFloatValue:lati];
	
	prevLocaIden = locaIden;
	prevLocaName = [orgaLocaNameText  stringValue];
	
	
	if ( mapCont ) {
		var colo = [CPColor colorWithHexString:@"00AA00"];
		[mapCont radiusCenteredAtPoint:lngd :lati erase:NO draw:YES radius:0.0004 color:colo];
	}

	[orgaLocaColoText setStringValue:valuList[13]];

	var orgaLocaCont0 = valuList[14];
	var orgaLocaCont1 = valuList[15];
	var orgaLocaCont2 = valuList[16];
	var orgaLocaCont3 = valuList[17];

	var orgaLocaCorpCont0 = valuList[18];
	var orgaLocaCorpCont1 = valuList[19];
	var orgaLocaCorpCont2 = valuList[20];
	var orgaLocaCorpCont3 = valuList[21];

    [orgaLocaActiChec setState:[valuList[22] intValue]];
	prevValuOrgaLocaActiChec = "" + [orgaLocaActiChec state] + "";
	var orgaIden = valuList[23];

	inde = [orgaTabl selectedRow];
	if ( inde < 0 ) {
		var orgaArraCoun = [orgaArra count];
		for ( var i=0; i<orgaArraCoun; i++ ) {
			var valuList = [orgaArra[i] valueList];
			if ( [valuList[0] isEqualToString:orgaIden] ) {
				[glblAlerPane showWithMessage:@"" info:"Nombre Legal = "+valuList[2]+"\nNombre Comercial = "+valuList[3]];
				//alert("Nombre Legal = "+valuList[2]+"\nNombre Comercial = "+valuList[3]);
				var indeSet = [[CPIndexSet alloc] init];
				[indeSet addIndex:i];
				[orgaTabl selectRowIndexes:indeSet byExtendingSelection:NO];
				[orgaTabl scrollRowToVisible:i];
				[self organizationTableSelection:0];
				break;
			}
		}
	}

	[orgaLocaCont0Popu removeAllItems];
	[orgaLocaCont1Popu removeAllItems];
	[orgaLocaCont2Popu removeAllItems];
	[orgaLocaCont3Popu removeAllItems];

	[orgaLocaCont0Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCont1Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCont2Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCont3Popu addItemWithTitle:@"Contactos"];

	[orgaLocaCorpCont0Popu removeAllItems];
	[orgaLocaCorpCont1Popu removeAllItems];
	[orgaLocaCorpCont2Popu removeAllItems];
	[orgaLocaCorpCont3Popu removeAllItems];

	[orgaLocaCorpCont0Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCorpCont1Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCorpCont2Popu addItemWithTitle:@"Contactos"];
	[orgaLocaCorpCont3Popu addItemWithTitle:@"Contactos"];

	var coun = [orgaEmplArra count];
	for ( var i=0; i<coun; i++ ) {
		var valuList = [orgaEmplArra[i] valueList];
		var iden = valuList[0];
		var nameLast = valuList[2];
		var nameFirs = valuList[3];
		var stri = [CPString stringWithFormat:@"%@, %@:%@",nameLast,nameFirs,iden];
		[orgaLocaCont0Popu addItemWithTitle:stri];
		[orgaLocaCont1Popu addItemWithTitle:stri];
		[orgaLocaCont2Popu addItemWithTitle:stri];
		[orgaLocaCont3Popu addItemWithTitle:stri];
		[orgaLocaCorpCont0Popu addItemWithTitle:stri];
		[orgaLocaCorpCont1Popu addItemWithTitle:stri];
		[orgaLocaCorpCont2Popu addItemWithTitle:stri];
		[orgaLocaCorpCont3Popu addItemWithTitle:stri];
	}
	
	if (orgaLocaCont0)  {
	
	}
	
	var quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCont0];
	
	var arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCont0];
		[orgaLocaCont0Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCont1];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCont1];
		[orgaLocaCont1Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCont2];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCont2];
		[orgaLocaCont2Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCont3];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCont3];
		[orgaLocaCont3Popu selectItemWithTitle:titl];
	}

	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCorpCont0];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCorpCont0];
		[orgaLocaCorpCont0Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCorpCont1];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCorpCont1];
		[orgaLocaCorpCont1Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCorpCont2];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCorpCont2];
		[orgaLocaCorpCont2Popu selectItemWithTitle:titl];
	}
	quer = [CPString stringWithFormat:@"select nameLast,nameFirst from %@.persons where id=%@",dataBase,orgaLocaCorpCont3];
	arra = [self query:quer];
	if ( [arra count] ) {
		var valuList = [arra[0] valueList];
		var titl = [CPString stringWithFormat:@"%@, %@:%@",valuList[0],valuList[1],orgaLocaCorpCont3];
		[orgaLocaCorpCont3Popu selectItemWithTitle:titl];
	}

	var quer = [CPString stringWithFormat:@"select day,timeStart,timeFinish from %@.locationSchedule where organizationLocationId=%@",dataBase,locaIden];
	var arra = [self query:quer];
	var coun = [arra count];

	for ( var i=0; i<coun; i++ ) {
		valuList = [arra[i] valueList];
		var day = [valuList[0] intValue];

		if ( day == 0 ) {
			[orgaLocaActiSundChec setState:1];
			[orgaLocaHourStarSundText setStringValue:valuList[1]];
			[orgaLocaHourFiniSundText setStringValue:valuList[2]];
		}
		else if ( day == 1 ) {
			[orgaLocaActiMondChec setState:1];
			[orgaLocaHourStarMondText setStringValue:valuList[1]];
			[orgaLocaHourFiniMondText setStringValue:valuList[2]];
		}
		else if ( day == 2 ) {
			[orgaLocaActiTuesChec setState:1];
			[orgaLocaHourStarTuesText setStringValue:valuList[1]];
			[orgaLocaHourFiniTuesText setStringValue:valuList[2]];
		}
		else if ( day == 3 ) {
			[orgaLocaActiWednChec setState:1];
			[orgaLocaHourStarWednText setStringValue:valuList[1]];
			[orgaLocaHourFiniWednText setStringValue:valuList[2]];
		}
		else if ( day == 4 ) {
			[orgaLocaActiThurChec setState:1];
			[orgaLocaHourStarThurText setStringValue:valuList[1]];
			[orgaLocaHourFiniThurText setStringValue:valuList[2]];
		}
		else if ( day == 5 ) {
			[orgaLocaActiFridChec setState:1];
			[orgaLocaHourStarFridText setStringValue:valuList[1]];
			[orgaLocaHourFiniFridText setStringValue:valuList[2]];
		}
		else if ( day == 6 ) {
			[orgaLocaActiSatuChec setState:1];
			[orgaLocaHourStarSatuText setStringValue:valuList[1]];
			[orgaLocaHourFiniSatuText setStringValue:valuList[2]];
		}
	}

}



- (void) organizationLocationsInMap:(id)sender {
	if ( !mapCont ) {
		[glblAlerPane showWithMessage:@"Atencion" info:"Por favor active el mapa"];
		//alert("Por favor active el mapa.");
		return;
	}

	var orgaIden = 0;
	var inde = [orgaTabl selectedRow];
	if ( inde >= 0 )
		orgaIden = [[orgaArra[inde] valueList] objectAtIndex:0];

	[mapCont radiusCenteredAtPoint:0 :0 erase:YES draw:NO radius:0.001 color:[CPColor greenColor]];

	var quer = 0;
	if ( orgaIden )
		quer = [CPString stringWithFormat:@"select id,name,locationLongitude,locationLatitude,color from %@.organizationLocations where organizationId='%@' and active=1 order by name",
			dataBase,orgaIden];
	else
	//modified 2017-03-03
		quer = @"select id,name,locationLongitude,locationLatitude,color from " + dataBase + ".organizationLocations where active=1 order by name";

	var orgaTempArra= [self query:quer];
	var coun = [orgaTempArra count];

	for ( var i=0; i<coun; i++ ) {
		var valuList = [orgaTempArra[i] valueList];
		var locaIden = valuList[0];
		var name = valuList[1];
		var stri = [CPString stringWithFormat:@"%@:%@",name,locaIden];
		var lngd = valuList[2];
		var lati = valuList[3];
		[mapCont radiusCenteredAtPoint:lngd :lati erase:NO draw:YES radius:0.0004 color:[CPColor blueColor]];
	}
}



// Organization Trainings

- (void) organizationTrainingClearFields:(id)sender {
	[orgaTraiAbbrText setStringValue:@""];
	[orgaTraiDescText setStringValue:@""];
	[orgaTraiDateLastText setStringValue:@""];
	[orgaTraiDateExpiText setStringValue:@""];
}



- (void) organizationTrainingDelete:(id)sender {
	var inde = [orgaTraiTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione la capacitación de la tabla que desea eliminar."];
		return;
	}
	var orgaTraiIden = [[orgaTraiArra[inde] valueList] objectAtIndex:0];
	var orgaTraSig= [[orgaTraiArra[inde] valueList] objectAtIndex:1];
	var orgaTraDes= [[orgaTraiArra[inde] valueList] objectAtIndex:2];

	var conf = confirm("Desea usted borrar a la capacitación * "+orgaTraSig+", "+orgaTraDes+" * con id= "+orgaTraiIden);
	if ( ! conf )
		return;

	

	var quer= [CPString stringWithFormat:@"delete from %@.organizationTraining where id=%@",dataBase,orgaTraiIden];
	[self query:quer];

	[orgaTraiTabl reloadData];
}



- (void) organizationTrainingInsert:(id)sender {
	var inde = [orgaTabl selectedRow];
	if ( inde < 0 )
		return;
	var orgaIden = [[orgaArra[inde] valueList] objectAtIndex:11];
	var orgaTraiAbbr = [orgaTraiAbbrText stringValue];
	var orgaTraiDesc = [orgaTraiDescText stringValue];
	var orgaTraiDateLast = [orgaTraiDateLastText stringValue];
	var orgaTraiDateExpi = [orgaTraiDateExpiText stringValue];

	var quer= [CPString stringWithFormat:@"insert into %@.organizationTraining (organizationId,abbreviation,description,dateLast,dateExpiration) values ('%@','%@','%@','%@','%@')",
		dataBase,orgaIden,orgaTraiAbbr,orgaTraiDesc,orgaTraiDateLast,orgaTraiDateExpi];

	[self query:quer];

	[orgaTraiTabl reloadData];
}



- (void) organizationTrainingTableSelection:(id)sender {
	var inde = [orgaTraiTabl selectedRow];
	if ( inde < 0 )
		return;

	var valuList = [orgaTraiArra[inde] valueList];
	var orgaTraiAbbr = valuList[1];
	var orgaTraiDesc = valuList[2];
	var orgaTraiDateLast = valuList[3];
	var orgaTraiDateExpi = valuList[4];
	
	[orgaTraiAbbrText setStringValue:orgaTraiAbbr];
	[orgaTraiDescText setStringValue:orgaTraiDesc];
	[orgaTraiDateLastText setStringValue:orgaTraiDateLast];
	[orgaTraiDateExpiText setStringValue:orgaTraiDateExpi];
}



- (void) organizationTrainingUpdate:(id)sender {
	var inde = [orgaTraiTabl selectedRow];
	if ( inde < 0 ){
		[glblAlerPane showWithMessage:@"Atención" info:"Seleccione una capacitación de la tabla para actualizar."];
		return;
	}

	var orgaTraiIden = [[orgaTraiArra[inde] valueList] objectAtIndex:0];

	var orgaTraiAbbr = [orgaTraiAbbrText stringValue];
	var orgaTraiDesc = [orgaTraiDescText stringValue];
	var orgaTraiDateLast = [orgaTraiDateLastText stringValue];
	var orgaTraiDateExpi = [orgaTraiDateExpiText stringValue];

	var quer= [CPString stringWithFormat:@"update %@.organizationTraining set abbreviation='%@',description='%@',dateLast='%@',dateExpiration='%@' where id=%@",
		dataBase,orgaTraiAbbr,orgaTraiDesc,orgaTraiDateLast,orgaTraiDateExpi,orgaTraiIden];

	[self query:quer];

	[orgaTraiTabl reloadData];
}


//		TAB SOURCE DELEGATE METHODS


- (void) tabView:(CPTabView)tabView willSelectTabViewItem:(CPTabViewItem)tabViewItem {
        var tabIden = [tabViewItem identifier];
        if ( [tabIden isEqualToString:@"organizations"])
        	[orgaTabl reloadData];
        else if ( [tabIden isEqualToString:@"employees"])
        	[orgaEmplTabl reloadData];
        else if ( [tabIden isEqualToString:@"locations"])
        	[orgaLocaTabl reloadData];
        else if ( [tabIden isEqualToString:@"holidays"])
        	[orgaHoliTabl reloadData];
        else if ( [tabIden isEqualToString:@"training"])
        	[orgaTraiTabl reloadData];
}



//		TABLE SOURCE DELEGATE METHODS

- (int) numberOfRowsInTableView:(CPTableView)tablView {
	var coun= 0;
	var quer;

	if ( tablView == counHoliTabl ) {
	//modified 2017-03-03
		quer = @"select id,dateStart,dateFinish,description from " + dataBase + ".countryHolidays where countryId=506 order by dateStart";
		counHoliArra = [self query:quer];
		coun = [counHoliArra count];
		orgaCond = @"";
	}
	else if ( tablView == orgaTabl ) {
		quer = [CPString stringWithFormat:@"select id,lawId,name,nameLegal,addressStreet,addressCity,addressState,phone1,email,notes,active,idPrimary from %@.organizations where id!='%@' and deletion=0 %@ order by name,lawId",dataBase,orgaCode,orgaCond];
		orgaArra = [self query:quer];
		coun = [orgaArra count];
		orgaCond = @"";
     //alert("tablView=orgaTabl : query="+quer);
	}
	else if ( tablView == orgaEmplTabl ) {
		var inde = [orgaTabl selectedRow];
		if ( inde >= 0 ) {
			var iden = [[orgaArra[inde] valueList] objectAtIndex:11];
			quer = [CPString stringWithFormat:@"select t1.id,t1.lawId,t1.nameLast,t1.nameFirst,t2.position,t1.homeAddressStreet,t1.homeAddressCity,t1.homeAddressState,t1.phone1,t1.phone2,t1.email,t1.notes,t2.active from %@.persons t1, %@.organizationPersons t2 where t2.organizationId='%@' and t1.id=t2.personId order by t1.nameLast,t1.nameFirst",dataBase,dataBase,iden];
			orgaEmplArra = [self query:quer];
			coun = [orgaEmplArra count];
		}
	}
	else if ( tablView == orgaHoliTabl ) {
		var inde = [orgaTabl selectedRow];
		if ( inde >= 0 ) {
			var iden = [[orgaArra[inde] valueList] objectAtIndex:11];
			quer = [CPString stringWithFormat:@"select id,dateStart,dateFinish,description from %@.organizationHolidays where organizationId='%@' order by dateStart",dataBase,iden];
			orgaHoliArra = [self query:quer];
			coun = [orgaHoliArra count];
		}
	}
	else if ( tablView == orgaLocaTabl ) {
		var inde = [orgaTabl selectedRow];
		if ( inde >= 0 ) {
			var iden = [[orgaArra[inde] valueList] objectAtIndex:11];
			quer = [CPString stringWithFormat:@"select id,name,addressBuilding,addressGate,addressStreet,addressCity,addressState,phone,email,restrictions,notes,locationLongitude,locationLatitude,color,IFNULL(contact0Id,0),IFNULL(contact1Id,0),IFNULL(contact2Id,0),IFNULL(contact3Id,0),IFNULL(contactCorporate0Id,0),IFNULL(contactCorporate1Id,0),IFNULL(contactCorporate2Id,0),IFNULL(contactCorporate3Id,0),active from %@.organizationLocations where organizationId='%@' order by name",dataBase,iden];
			orgaLocaArra = [self query:quer];
			coun = [orgaLocaArra count];
		}
		else {
			quer = @"select id,name,addressBuilding,addressGate,addressStreet,addressCity,addressState,phone,email,restrictions,notes,locationLongitude,locationLatitude,color,IFNULL(contact0Id,0),IFNULL(contact1Id,0),IFNULL(contact2Id,0),IFNULL(contact3Id,0),IFNULL(contactCorporate0Id,0),IFNULL(contactCorporate1Id,0),IFNULL(contactCorporate2Id,0),IFNULL(contactCorporate3Id,0),active,organizationId from " + dataBase + ".organizationLocations order by name";
			orgaLocaArra = [self query:quer];
			coun = [orgaLocaArra count];
			//alert(quer+"   count="+coun);
		}
	}
	else if ( tablView == orgaTraiTabl ) {
		var inde = [orgaTabl selectedRow];
		if ( inde >= 0 ) {
			var iden = [[orgaArra[inde] valueList] objectAtIndex:11];
			quer = [CPString stringWithFormat:@"select id,abbreviation,description,dateLast,dateExpiration from %@.organizationTraining where organizationId='%@' order by abbreviation",dataBase,iden];
			orgaTraiArra = [self query:quer];
			coun = [orgaTraiArra count];
		}
	}
	
	return coun;
}




- (id) tableView:(CPTableView)tablView objectValueForTableColumn:(CPTableColumn)tablColu row:(int)rowInde {
	var headStri= [[tablColu headerView] stringValue];
	var headIden= [tablColu identifier];
	var valu = @"N/A";

	if ( tablView == counHoliTabl ) {
		var arra = [[counHoliArra objectAtIndex:rowInde] valueList];
		if ( [headStri isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headStri isEqualToString:@"FechaInicio"] )
			valu = arra[1];
		else if ( [headStri isEqualToString:@"FechaFin"] )
			valu = arra[2];
		else if ( [headStri isEqualToString:@"Descripcion"] )
			valu = arra[3];
	}
	else if ( tablView == orgaTabl ) {
		var arra = [[orgaArra objectAtIndex:rowInde] valueList];
		if ( [headStri isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headStri isEqualToString:@"IDLegal"] )
			valu = arra[1];
		else if ( [headStri isEqualToString:@"Nombre"] )
			valu = arra[2];
		else if ( [headStri isEqualToString:@"NombreLegal"] )
			valu = arra[3];
		else if ( [headStri isEqualToString:@"Direccion"] )
			valu = arra[4];
		else if ( [headStri isEqualToString:@"Ciudad"] )
			valu = arra[5];
		else if ( [headStri isEqualToString:@"Estado"] )
			valu = arra[6];
		else if ( [headStri isEqualToString:@"Telefono1"] )
			valu = arra[7];
		else if ( [headStri isEqualToString:@"Correo"] )
			valu = arra[8];
		else if ( [headStri isEqualToString:@"Comentarios"] )
			valu = arra[9];
		else if ( [headStri isEqualToString:@"Activo"] )
			valu = arra[10];
	}
	else if ( tablView == orgaEmplTabl ) {
		var arra = [[orgaEmplArra objectAtIndex:rowInde] valueList];
		if ( [headStri isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headStri isEqualToString:@"Cedula"] )
			valu = arra[1];
		else if ( [headStri isEqualToString:@"Apellidos"] )
			valu = arra[2];
		else if ( [headStri isEqualToString:@"Nombre"] )
			valu = arra[3];
		else if ( [headStri isEqualToString:@"Posicion"] )
			valu = arra[4];
		else if ( [headStri isEqualToString:@"Direccion"] )
			valu = arra[5];
		else if ( [headStri isEqualToString:@"Ciudad"] )
			valu = arra[6];
		else if ( [headStri isEqualToString:@"EstadoProvincia"] )
			valu = arra[7];
		else if ( [headStri isEqualToString:@"Telefono1"] )
			valu = arra[8];
		else if ( [headStri isEqualToString:@"Telefono2"] )
			valu = arra[9];
		else if ( [headStri isEqualToString:@"Correo"] )
			valu = arra[10];
		else if ( [headStri isEqualToString:@"Comentarios"] )
			valu = arra[11];
	}
	else if ( tablView == orgaHoliTabl ) {
		var arra = [[orgaHoliArra objectAtIndex:rowInde] valueList];
		if ( [headStri isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headStri isEqualToString:@"FechaInicio"] )
			valu = arra[1];
		else if ( [headStri isEqualToString:@"FechaFin"] )
			valu = arra[2];
		else if ( [headStri isEqualToString:@"Descripcion"] )
			valu = arra[3];
	}
	else if ( tablView == orgaLocaTabl ) {
		var arra = [[orgaLocaArra objectAtIndex:rowInde] valueList];
		if ( [headStri isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headStri isEqualToString:@"Nombre"] )
			valu = arra[1];
		else if ( [headStri isEqualToString:@"Edificio"] )
			valu = arra[2];
		else if ( [headStri isEqualToString:@"Porton"] )
			valu = arra[3];
		else if ( [headStri isEqualToString:@"Direccion"] )
			valu = arra[4];
		else if ( [headStri isEqualToString:@"Ciudad"] )
			valu = arra[5];
		else if ( [headStri isEqualToString:@"Estado"] )
			valu = arra[6];
		else if ( [headStri isEqualToString:@"Telefono"] )
			valu = arra[7];
		else if ( [headStri isEqualToString:@"Correo"] )
			valu = arra[8];
		else if ( [headStri isEqualToString:@"Restriccion"] )
			valu = arra[9];
		else if ( [headStri isEqualToString:@"Comentarios"] )
			valu = arra[10];
		else if ( [headStri isEqualToString:@"Longitud"] )
			valu = arra[11];
		else if ( [headStri isEqualToString:@"Latitud"] )
			valu = arra[12];
		else if ( [headStri isEqualToString:@"Color"] )
			valu = arra[13];
		else if ( [headStri isEqualToString:@"Activo"] )
			valu = arra[18];
	}
	else if ( tablView == orgaTraiTabl ) {
		var arra = [[orgaTraiArra objectAtIndex:rowInde] valueList];
		if ( [headIden isEqualToString:@"ID"] )
			valu = arra[0];
		else if ( [headIden isEqualToString:@"Abbreviation"] )
			valu = arra[1];
		else if ( [headIden isEqualToString:@"Description"] )
			valu = arra[2];
		else if ( [headIden isEqualToString:@"DateLast"] )
			valu = arra[3];
		else if ( [headIden isEqualToString:@"DateExpiration"] )
			valu = arra[4];
	}
	return valu;
}



@end
