//ENUM
var Status=
{
     BEGIN:1
     ,SEARCHING:2
     ,NO_RESULTS:3
     ,SELECTED:4
     ,SEARCH_COMPLETED:5
     ,TOO_MUCH_RESULTS:5
}
    
controls.CtrlSeleccionarPersona=function(id,tiposPersona,nuevoVisible,parentElement)
{
//    //ATRIBUTOS
//    this.id=id;
//    this.textBox=null;
//    this.tablaResultados=null
//    this.attachTextBox(textBox);
//    this.resultados=new Array();
//    this.imageStatus=imageStatus;
//    this.selectedPersona=null;
//    this.hiddenPost=hiddenPost;
//    this.status=Status.BEGIN;
//    this.personaNueva=null;
//    this.tiposPersona=tiposPersona;
//    this.nuevoVisible=nuevoVisible;

    this.id=id;
    this.tablaPrincipal=document.getElementById(this.id+'_tablaPrincipal');
    if (this.tablaPrincipal!=null)
    {
        parentElement.removeChild(this.tablaPrincipal);
        this.tablaResultados=this.tablaPrincipal.tablaResultados;
        this.hideSearchResults();
    }
    this.tablaPrincipal=document.createElement('<table id="'+this.id+'_tablaPrincipal" cellpadding="1" cellspacing="0" style="border-right: gainsboro 1px solid;border-top: gainsboro 1px solid; border-left: gainsboro 1px solid; border-bottom: gainsboro 1px solid; background-color: white;">');
    parentElement.ctrlSeleccionarPersona=this;
    
    var row=this.tablaPrincipal.insertRow();
    var cell1=row.insertCell();
    var cell2=row.insertCell();
    this.imageStatus=document.createElement('<img src="/./img/lupa_contacto.gif">');
    cell1.appendChild(this.imageStatus);
    this.textBox=document.createElement('<input type="text" id="'+this.id+'_textBox">');
    cell2.appendChild(this.textBox);

    this.attachTextBox(this.textBox);
    
    parentElement.appendChild(this.tablaPrincipal);
    
    this.resultados=new Array();
    this.tablaResultados=document.getElementById(this.id+'_tablaResultados');
    if (this.tablaResultados!=null)
        this.hideSearchResults();
    this.selectedPersona=null;
    this.status=Status.BEGIN;
    this.personaNueva=null;
    this.tiposPersona=tiposPersona;
    this.hiddenPost=document.getElementById(this.id+'_hidden');
    this.nuevoVisible=nuevoVisible;
    this.idBusqueda=0;
 
    
    if (this.hiddenPost.value>0)
    {   
        var thisInstance=this;
        CtrlSeleccionarPersona2.BuscarPersona('ID:'+this.hiddenPost.value,this.tiposPersona,
            function(context)
            {
                thisInstance.setSelectedPersona(context.value[0]);
            }
        );
    }
}
controls.CtrlSeleccionarPersona.getCtrlSeleccionarPersona=function(id) 
{
    var tabla=document.getElementById(id);
    if (tabla!=null)
        return tabla.ctrlSeleccionarPersona;
    return null;
}

controls.CtrlSeleccionarPersona.prototype.attachTextBox=function(textBox)
{
    JScriptPage.attachJSEvent(this,textBox,'onkeyup',this.textBoxChanged);
    JScriptPage.attachJSEvent(this,textBox,'onkeydown',this.textBoxKeyDown);
    JScriptPage.attachJSEvent(this,textBox,'onfocus',this.textBoxFocus);
    
    JScriptPage.attachJSEvent(this,document,'onkeydown',
        function(e)
        {
            if (e.keyCode==27)
            {
                this.hideSearchResults();
            }
        }
    );
    //JScriptPage.attachJSEvent(this,textBox,'onblur',function(){alert('blur');});
    this.textBox=textBox;
    this.textBox.style.borderWidth='0px';
    this.textBox.style.borderSyle='none';
    Common.setInmoParentObject(this.textBox,this);
}
controls.CtrlSeleccionarPersona.prototype.createTablaResultados=function()
{
     if(this.tablaResultados==null){
        this.tablaResultados=document.createElement('<table id="'+this.id+'_tablaResultados">');
        this.tablaResultados.style.position='absolute';
        this.tablaResultados.style.zIndex='5';
        this.tablaResultados.style.backgroundColor='white';
        this.tablaResultados.style.borderColor='black';
        this.tablaResultados.style.borderStyle='solid';
        this.tablaResultados.style.borderWidth='1px';
        this.tablaResultados.style.fontFamily='arial';
        this.tablaResultados.style.fontSize='11px';
        document.body.insertBefore(this.tablaResultados,document.body.firstChild); 
        //JScriptPage.attachJSEvent(this,this.tablaResultados,'onmouseup',function(){alert('focus');});
        JScriptPage.attachJSEvent(this,window,'onresize',this.positionTablaResultados);
        
        var iFrame=document.createElement('iframe');
        var fnResize=function()
        {
            iFrame.style.width=this.tablaResultados.offsetWidth+'px';
            iFrame.style.height=this.tablaResultados.offsetHeight+'px';
            iFrame.style.left=this.tablaResultados.offsetLeft+'px';
            iFrame.style.top=this.tablaResultados.offsetTop+'px';
            iFrame.style.zIndex='4';
            iFrame.style.position='absolute';
            document.body.insertBefore(iFrame,this.tablaResultados); 
            this.tablaResultados.iFrame=iFrame;
        }
        JScriptPage.attachJSEvent(this,this.tablaResultados,'onresize',fnResize);
        
        this.tablaPrincipal.tablaResultados=this.tablaResultados;
    }
}
controls.CtrlSeleccionarPersona.prototype.positionTablaResultados=function()
{
    var tabla=this.imageStatus.offsetParent.offsetParent;
    if (this.tablaResultados!=null)
    {
        this.tablaResultados.style.left=Common.getAbsolutePosition( tabla).left+'px';
        this.tablaResultados.style.top=Common.getAbsolutePosition(tabla).top+tabla.offsetHeight+'px';
    }
}
controls.CtrlSeleccionarPersona.prototype.textBoxFocus=function(e)
{
    if (this.getStatus()==Status.SELECTED && this.getSelectedPersona()!=null)
        this.buscarPersona("ID:"+this.getSelectedPersona().ID);
}
controls.CtrlSeleccionarPersona.prototype.textBoxChanged=function(e)
{
    if (e.keyCode!=13 && e.keyCode!=9) //Verificamos que no se presiono ni TAB ni ENTER
    { 
        var texto=
            e.keyCode==40 && this.getStatus()==Status.SELECTED?"ID:"+this.getSelectedPersona().ID
            :this.textBox.value;
        if (!texto.startsWith("ID"))
            this.setSelectedPersona(null);
            

        if (this.textBox.value.length>3 
            && (this.textBox.value!=this.textBox.oldValue || (e.keyCode==40 && this.getStatus()==Status.SELECTED)))
        {              
    //            this.createTablaResultados();
    //            Common.deleteAllRows(this.tablaResultados);
    //            var cell=this.tablaResultados.insertRow().insertCell();
    //            cell.style.fontFamily='arial';
    //            cell.style.fontSize='11px';
    //            cell.innerText='Por favor espere...';
            
            
            thisTmp=this;
            
            if (!texto.startsWith("ID"))
                this.setStatus(Status.SEARCHING);
            
            this.buscarPersona(texto);
        }
        else if (this.textBox.value.length<=3 && this.tablaResultados!=null)
        {
            this.hideSearchResults();
        }
    }
    
}
controls.CtrlSeleccionarPersona.prototype.buscarPersona=function(texto)
{
    this.idBusqueda++;
    thisTmp=this;
    //AjaxPro.queue.abort();  
    CtrlSeleccionarPersona2.BuscarPersona(texto,this.tiposPersona,function(context){thisTmp.searchResult.call(thisTmp,context)}, this.idBusqueda);
}
controls.CtrlSeleccionarPersona.prototype.searchResult=function(context)
{
    if (this.idBusqueda==context.context)
    {
    //        if(this.tablaResultados!=null)
    //        {
    //             document.body.removeChild(this.tablaResultados);
    //             this.tablaResultados=null;
    //        }
        
        if (context.error==null)
        {
            this.drawSearchResults(context.value);
        }
        else
        {
            this.createTablaResultados();
            this.positionTablaResultados();
            Common.deleteAllRows(this.tablaResultados);
            
            var cell=this.tablaResultados.insertRow().insertCell();
            cell.innerHTML='Por favor especifique mejor su b&uacute;squeda,<br />hay muchas coincidencias con el texto introducido.';
            this.setStatus(Status.TOO_MUCH_RESULTS);
            this.drawCellNuevo();
        }
    }
    
}
controls.CtrlSeleccionarPersona.prototype.drawSearchResults=function(personas)
{
     this.createTablaResultados();
     this.positionTablaResultados();
     Common.deleteAllRows(this.tablaResultados);
         
    if (personas.length>0)
    {
       
        this.resultados.length=0;
        var tabla=document.createElement('table');
        var row=this.tablaResultados.insertRow();
        var cell=row.insertCell();
        cell.appendChild(tabla);
        cell=row.insertCell();
        this.drawCloseButton(cell);
        for (var i=0;i<personas.length;i++)
        {
            row=tabla.insertRow();
            cell=row.insertCell();
            //cell.innerText=personas[i].Nombre1y2Apellido1y2;
            this.resultados[i]=new SearchResult();
            this.resultados[i].cell=cell;
            this.resultados[i].persona=personas[i];
            this.resultados[i].selected=i==0;
            cell.resultado=this.resultados[i];
            
             this.drawPersonaCell(this.resultados[i]);
            
            JScriptPage.attachJSEvent(this,cell,'onmousemove',this.cellResultadoMouseOver,cell);
            JScriptPage.attachJSEvent(this,cell,'onclick',this.cellResultadoSelected,cell);
            
            
        }
       
        this.applySelected(this.resultados);
        if (this.getStatus()!=Status.SELECTED)
            this.setStatus(Status.SEARCH_COMPLETED);
    }
    else
    {
        var cell=this.tablaResultados.insertRow().insertCell();
        cell.innerHTML='No hay resultados para su b&uacute;squeda';
        this.setStatus(Status.NO_RESULTS);
    }
    this.drawCellNuevo();
    
}
controls.CtrlSeleccionarPersona.prototype.drawCellNuevo=function()
{
    if (this.nuevoVisible)
    {
        var cellNuevo=this.tablaResultados.insertRow().insertCell();
        cellNuevo.padding='2px';
        var linkNuevo=document.createElement('a');
        linkNuevo.innerText='Crear un nuevo contacto';
        linkNuevo.href='javascript:void(0);';
        linkNuevo.style.color='blue';
        cellNuevo.appendChild(linkNuevo);
        cellNuevo.align='center';
        JScriptPage.attachJSEvent(this,linkNuevo,'onclick',this.drawCellNuevaPersona);
    }
    
}
controls.CtrlSeleccionarPersona.prototype.applySelected=function(resultados)
{
    for (var i=0;i<resultados.length;i++)
    {
        var r=resultados[i];
        if (r.selected)
        {
            r.cell.style.backgroundColor='lightblue';
        }
        else
        {
            r.cell.style.backgroundColor='white';
        }
    }
}
controls.CtrlSeleccionarPersona.prototype.selectResultado=function(index)
{
    var resultados=this.resultados;
    if ((typeof index=="number" && index>0 && index<resultados.length-1)|| typeof index=="object")
    {
        for (var i=0;i<resultados.length;i++)
        {
            resultados[i].selected=(i==index || resultados[i]==index);
        }
    }
}
controls.CtrlSeleccionarPersona.prototype.getSelectedResultado=function()
{
    for (var i=0;i<this.resultados.length;i++)
    {
        if(this.resultados[i].selected)
        return this.resultados[i];
    }
    return null;
}
controls.CtrlSeleccionarPersona.prototype.getSelectedResultadoIndex=function()
{
    for (var i=0;i<this.resultados.length;i++)
    {
        if(this.resultados[i].selected)
        return i;
    }
    return -1;
}
controls.CtrlSeleccionarPersona.prototype.getResultadoByPersona=function(persona)
{
    for (var i=0;i<this.resultados.length;i++)
    {
        if(this.resultados[i].persona==persona)
        return this.resultados[i];
    }
    return null;
}
controls.CtrlSeleccionarPersona.prototype.setSelectedPersona=function(persona)
{
    this.selectedPersona=persona;
    if (persona!=null)
    {
        this.textBox.value=persona.Nombre1y2Apellido1y2;
        this.hideSearchResults();
        this.setStatus(Status.SELECTED);
        this.hiddenPost.value=persona.ID;
    }
    else
    {
        this.hiddenPost.value="-1";
        this.setStatus(Status.BEGIN);
    }
    
}
controls.CtrlSeleccionarPersona.prototype.getSelectedPersona=function()
{
    return this.selectedPersona;
}
controls.CtrlSeleccionarPersona.prototype.hideSearchResults=function()
{
    if (this.tablaResultados!=null)
    {
        if (this.tablaResultados.iFrame!=null && this.tablaResultados.iFrame.parentElement!=null)
            document.body.removeChild(this.tablaResultados.iFrame);
        if (this.tablaResultados.parentElement!=null)
            document.body.removeChild(this.tablaResultados);
        this.tablaResultados=null;
        if (this.getStatus()!=Status.SELECTED)
            this.setStatus(Status.BEGIN);
    }
}
controls.CtrlSeleccionarPersona.prototype.changeImageStatus=function(retard)
{
    var thisInstance=this;
    var fun=function()
    {
        
        var img;
        if (thisInstance.status==Status.BEGIN)
        {
            img='/./img/lupa_contacto.gif';
        }
        else if (thisInstance.status==Status.SEARCHING)
        {
            img='/./img/lupa_contacto_ani.gif';
        }
        else if(thisInstance.status==Status.NO_RESULTS)
        {
            img='/./img/lupa_contacto.gif';
        }
        else if(thisInstance.status==Status.SELECTED)
        {
            img='/./img/check_contacto.gif';
        }  
        else if(thisInstance.status==Status.TOO_MUCH_RESULTS)
        {
            img='/./img/lupa_contacto.gif';
        } 
        else if(thisInstance.status==Status.SEARCH_COMPLETED)
        {
            img='/./img/lupa_contacto.gif';
        } 
        if (thisInstance.imageStatus!=null && thisInstance.imageStatus.src.indexOf(img)==-1)
            thisInstance.imageStatus.src=img;
    };
    if (retard)
        setTimeout(fun,400);
    else
        fun();
}
controls.CtrlSeleccionarPersona.prototype.setStatus=function(status)
{
    this.status=status;
    this.changeImageStatus(status!=Status.SEARCHING && status!=Status.SELECTED);
}
controls.CtrlSeleccionarPersona.prototype.getStatus=function()
{
    return this.status;
}
controls.CtrlSeleccionarPersona.prototype.textBoxKeyDown=function(e)
{
    this.textBox.oldValue=this.textBox.value;
    var index=-1;
    for (var i=0;i<this.resultados.length;i++)
    {
        if(this.resultados[i].selected)
        {
            index=i;
            break;
        }
    }
    if (e.keyCode==38 && index>0)
    {
        this.resultados[index].selected=false;
        this.resultados[index-1].selected=true;
    }
    else if (e.keyCode==40 && index<this.resultados.length-1)
    {
        this.resultados[index].selected=false;
        this.resultados[index+1].selected=true;
    }
//    else if (e.keyCode==27)
//    {
//        this.hideSearchResults();
//        e.returnValue=false;
//    }
    else if (e.keyCode==13 || e.keyCode==9)
    {
        if (this.getSelectedResultado()!=null)
            this.setSelectedPersona(this.getSelectedResultado().persona);
        e.returnValue=false;
    }
    this.applySelected(this.resultados);         
}
controls.CtrlSeleccionarPersona.prototype.drawPersonaCell=function(resultado)
{       
    var tabla=document.createElement('table');
    tabla.style.width='100%';
    tabla.cellPadding=1;
    tabla.cellSpacing=0;
    
    var row=tabla.insertRow();
    var cellFoto=row.insertCell();
    var cellDatos=row.insertCell();
    var cellLinks=row.insertCell();
    
    var imgFoto=document.createElement('img');
    cellFoto.appendChild(imgFoto);
    imgFoto.src='/./img/transparent.gif';
    imgFoto.style.width='27px';
    imgFoto.style.borderStyle='solid';
    imgFoto.style.borderWidth='1px';
    imgFoto.style.borderColor='lightgrey';
    var func=function(context)
    {
        imgFoto.src=context.value;
    }
    Utilities.ImageFileName.GetShowNameOf(resultado.persona.IdArchivoImagen,Model.TamanoImagenEnum.PersonalMini,func);
    
    cellFoto.vAlign='top';
    
    cellDatos.style.width='100%';
    cellDatos.vAlign='top';
    cellDatos.style.fontSize='11px';
    cellDatos.innerHTML="<b>"+resultado.persona.Nombre1y2Apellido1y2+"</b>";
    cellDatos.innerHTML+="<br />"+Common.getTipoPersonaString(resultado.persona.TipoPersona);
    func=function(context)
    {
        var html="";
        var contactos=context.value;
        for (var i=0;i<contactos.length;i++)
        {
            var c=contactos[i];
            html+="<br />"+c.ToStringVal;
        }
        cellDatos.innerHTML+=html;
    }
    CtrlSeleccionarPersona2.CargarContactos(resultado.persona.ID,func)
    
    cellLinks.align='right';
    cellLinks.style.fontFamily='arial';
    cellLinks.style.fontSize='11px';
    cellLinks.style.fontColor='blue';
    cellLinks.vAlign='top';
    //cellLinks.innerHTML='<a >Detalles</a><br /><a>Modificar</a>';
    var linkDetalles=document.createElement('a');
    linkDetalles.innerHTML='Detalles<br />';
    linkDetalles.href='javascript:void(0);';
    linkDetalles.style.color='blue';
    Common.setInmoParentObject(linkDetalles,this);
    linkDetalles.persona=resultado.persona;
    JScriptPage.attachJSEvent(this,linkDetalles,'onclick',this.linkDetallesClicked,resultado.persona);
    cellLinks.appendChild(linkDetalles);
    
    var linkModificar=document.createElement('a');
    cellLinks.appendChild(linkModificar);
    linkModificar.innerHTML='Modificar';
    linkModificar.href='javascript:void(0);';
    linkModificar.style.color='blue';
    linkModificar.style.display='none';
    
    JScriptPage.attachJSEvent(this,linkModificar,'onclick',this.linkModificarClicked,resultado.persona);
    
    CtrlSeleccionarPersona2.PuedeModificar(resultado.persona.ID,
        function(value)
        {
            linkModificar.style.display=value.value?'':'none';
        }
    );

    Common.removeAllElements(resultado.cell);
    resultado.cell.appendChild(tabla);
}
controls.CtrlSeleccionarPersona.prototype.linkDetallesClicked=function(e,persona)
{
    e.cancelBubble=true;
    this.drawCellDetallePersona(persona);
}
controls.CtrlSeleccionarPersona.prototype.linkModificarClicked=function(e,persona)
{
    e.cancelBubble=true;
    this.drawCellModificarPersona(persona);
}
controls.CtrlSeleccionarPersona.prototype.drawCellDetallePersona=function(persona)
{
    var tabla=this.tablaResultados;
    this.tablaResultados.style.padding='2px';
    
    Common.hideAllRows(tabla);
    var row=tabla.insertRow();
    var cellBack=row.insertCell();
    cellBack.vAlign='top';
    
    var img=document.createElement('img');
    img.src='/./img/back.gif';
    cellBack.appendChild(img);
    img.style.cursor='pointer';
    JScriptPage.attachJSEvent(this,img,'onclick',this.hideCellDetallePersona);
    
    var cellFoto=row.insertCell();
    cellFoto.vAlign='top';  
    
    var imgFotoPersona=document.createElement('img');
    cellFoto.appendChild(imgFotoPersona);
    setTimeout(function(){imgFotoPersona.src=Utilities.ImageFileName.GetShowNameOf(persona.IdArchivoImagen,Model.TamanoImagenEnum.PersonalNormal).value;},5);

    
    var cellDatos=row.insertCell();
    var html="<b style='font-size:14px;'>"+persona.Nombre1y2Apellido1y2+"</b>";
    var contactos=CtrlSeleccionarPersona2.CargarContactos(persona.ID).value;
    for (var i=0;i<contactos.length;i++)
    {
        var c=contactos[i];
        html+="<br />"+c.ToStringVal;
    }
    if (persona.NumeroPersona!='')
        html+="<br /><b>C&eacute;dula</b><br />"+persona.NumeroPersona;
    if (persona.Sexo!='')
        html+="<br /><b>Sexo</b><br />"+(persona.Sexo=='M'?'Masculino':'Femenino');
    if (persona.Observaciones!='')
        html+="<br /><b>Observaciones</b><br />"+persona.Observaciones;
    
    cellDatos.innerHTML=html;
    
    var cellClose=row.insertCell();
    this.drawCloseButton(cellClose);
    
}
controls.CtrlSeleccionarPersona.prototype.hideCellDetallePersona=function()
{
    var tabla=this.tablaResultados;
    tabla.deleteRow(tabla.rows.length-1);
    Common.showAllRows(tabla);
     this.tablaResultados.style.padding='0px';
}
controls.CtrlSeleccionarPersona.prototype.hideCellNuevaPersona=function()
{
    this.hideCellDetallePersona();
}
controls.CtrlSeleccionarPersona.prototype.drawCloseButton=function(parentElement)
{
    var img=document.createElement('<img src="/./img/close_small.gif">');
    parentElement.appendChild(img);
    parentElement.vAlign='top';
    JScriptPage.attachJSEvent(this,img,'onclick',this.hideSearchResults);
    this.tablaResultados.cellSpacing=0;
    this.tablaResultados.cellPadding=0;
    parentElement.style.paddingRight='2px';
    parentElement.style.paddingTop='2px';
    img.style.cursor='pointer';
}
controls.CtrlSeleccionarPersona.prototype.drawCellModificarPersona=function(persona)
{
    
    this.drawCellNuevaPersona();
    this.personaNueva=persona;
    this.textNombre1.value=persona.Nombre1;
    this.textNombre2.value=persona.Nombre2;
    this.textApellido1.value=persona.Apellido1;
    this.textApellido2.value=persona.Apellido2;
    this.textNumeroPersona.value=persona.NumeroPersona;
    this.radioFemenino.checked=persona.Sexo=='F';
    this.radioMasculino.checked=(persona.Sexo=='M' || !this.radioFemenino.checked);
    this.dtPickerNacimiento.setValue(persona.FechaNacimiento);
    this.textObservaciones.value=persona.Observaciones;
    
    
    var contactos=CtrlSeleccionarPersona2.CargarContactos(persona.ID).value;
    var indexEmails=0,indexTelefonos=0;
    for (var i=0;i<contactos.length;i++)
    {
        if (contactos[i].TipoContacto==Model.TipoContacto.Telefono)
        {
            this.textExtensiones[indexTelefonos].value=contactos[i].Extension;
            this.textNumeros[indexTelefonos++].value=contactos[i].NumeroTelefono;
        }
        else
        {
            this.textEmails[indexEmails++].value=contactos[i].Email;
        }
    }
}
controls.CtrlSeleccionarPersona.prototype.drawCellNuevaPersona=function()
{
    Common.hideAllRows(this.tablaResultados);
    var rowOuter=this.tablaResultados.insertRow();
    var cellContenido=rowOuter.insertCell();
    var cellClose=rowOuter.insertCell();
    this.drawCloseButton(cellClose);
    var tabla=document.createElement('<table cellpadding=2 cellspacing=0>');
    cellContenido.appendChild(tabla);
    
    var row=tabla.insertRow();
    var cellNuevaPersona=row.insertCell();
    tabla=document.createElement('<table style="font-family:arial;font-size:11px">');
    cellNuevaPersona.appendChild(tabla);
    row=tabla.insertRow();
    var cellBack=row.insertCell();
    cellBack.vAlign='top';
    cellBack.colSpan=2;
    
    var img=document.createElement('img');
    img.src='/./img/back.gif';
    cellBack.appendChild(img);
    img.style.cursor='pointer';
    JScriptPage.attachJSEvent(this,img,'onclick',this.hideCellDetallePersona);
    
//        var cellFoto=row.insertCell();
//        cellFoto.vAlign='top';  
//        cellFoto.rowSpan;
//        var imgFotoPersona=document.createElement('img');
//        cellFoto.appendChild(imgFotoPersona);
//        setTimeout(function(){imgFotoPersona.src=Archivos.ImageFileName.GetShowNameOf(persona.IdArchivoImagen,Model.TamanoImagenEnum.PersonalNormal).value;};5);

    var cellLabel=null;
    var cellDatos=null;
    
    //Nombre1
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textNombre1=document.createElement('input');
    cellLabel.innerHTML='<b>Primer nombre:</b>';
    cellDatos.appendChild(textNombre1);
    this.textNombre1=textNombre1;
    
    //Nombre2
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textNombre2=document.createElement('input');
    cellLabel.innerHTML='<b>Segundo nombre:</b>';
    cellDatos.appendChild(textNombre2);
    this.textNombre2=textNombre2;
    
    //Apellido1
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textApellido1=document.createElement('input');
    cellLabel.innerHTML='<b>Primer Apellido:</b>';
    cellDatos.appendChild(textApellido1);
    this.textApellido1=textApellido1;
    
    //Apellido2
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textApellido2=document.createElement('input');
    cellLabel.innerHTML='<b>Segundo Apellido:</b>';
    cellDatos.appendChild(textApellido2);
    this.textApellido2=textApellido2;
    
    //Numero Persona
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textNumeroPersona=document.createElement('input');
    cellLabel.innerHTML='<b>C&eacute;dula:</b>';
    cellDatos.appendChild(textNumeroPersona);
    this.textNumeroPersona=textNumeroPersona;
    
    //Sexo
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();             
    var radioFemenino=document.createElement('<input type=\"radio\" name=\"sexo\" id=\"'+this.id+'_radioFemenino\" />');
    radioFemenino.value='femenino';
    var radioMasculino=document.createElement('<input type=\"radio\" name=\"sexo\" id=\"'+this.id+'_radioMasculino\" checked />');   
   // radioMasculino.checked=true;
    radioMasculino.value='masculino'
   
    cellLabel.innerHTML='<b>Sexo:</b>';
    cellDatos.appendChild(radioFemenino);
    var labelFemenino= document.createElement('<label for=\''+radioFemenino.id+'\'>');
    labelFemenino.innerText='Femenino';
    cellDatos.appendChild(labelFemenino);
    cellDatos.appendChild(radioMasculino);
    var labelMasculino= document.createElement('<label for=\''+radioMasculino.id+'\'>');
    labelMasculino.innerText='Masculino';
    cellDatos.appendChild(labelMasculino);
    this.radioFemenino=radioFemenino;
    this.radioMasculino=radioMasculino;
    radioMasculino.defaultChecked=true;
    radioFemenino.defaultChecked=false;
   
   //Fecha
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    cellLabel.innerHTML='<b>Fecha nacimiento:</b>';
    var dtPicker=new controls.DTPicker('dtPicker1',cellDatos);
    this.dtPickerNacimiento=dtPicker;
    
    //Telefono1
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    cellLabel.innerHTML='<b>Tel&eacute;fono 1:</b>';
    var textExtension1=document.createElement('<input  size=3 maxlength=4>');
    var textNumero1=document.createElement('<input  size=10>');
    var sep1=document.createElement('<span>');
    sep1.innerHTML='&nbsp;-&nbsp;';
    cellDatos.appendChild(textExtension1);
    cellDatos.appendChild(sep1);
    cellDatos.appendChild(textNumero1);
    JScriptPage.textBoxToTextBox(textExtension1,textNumero1);
    this.textExtension1=textExtension1;
    this.textNumero1=textNumero1;
    
    
    //Telefono2
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    cellLabel.innerHTML='<b>Tel&eacute;fono 2:</b>';
    var textExtension2=document.createElement('<input size=3 maxlength=4>');
    var textNumero2=document.createElement('<input size=10 maxlenght=10>');
    var sep2=document.createElement('<span>');
    sep2.innerHTML='&nbsp;-&nbsp;';
    cellDatos.appendChild(textExtension2);
    cellDatos.appendChild(sep2);
    cellDatos.appendChild(textNumero2);
    JScriptPage.textBoxToTextBox(textExtension2,textNumero2);
    this.textExtension2=textExtension2;
    this.textNumero2=textNumero2;
    
    //Telefono3
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    cellLabel.innerHTML='<b>Tel&eacute;fono 3:</b>';
    var textExtension3=document.createElement('<input size=3 maxlength=4>');
    var textNumero3=document.createElement('<input size=10>');
    var sep3=document.createElement('<span>');
    sep3.innerHTML='&nbsp;-&nbsp;';
    cellDatos.appendChild(textExtension3);
    cellDatos.appendChild(sep3);
    cellDatos.appendChild(textNumero3);
    JScriptPage.textBoxToTextBox(textExtension3,textNumero3);
    this.textExtension3=textExtension3;
    this.textNumero3=textNumero3;
    
    //E-Mail1
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textEmail1=document.createElement('input');
    cellLabel.innerHTML='<b>E-Mail 1:</b>';
    cellDatos.appendChild(textEmail1);
    this.textEmail1=textEmail1;
    
     //E-Mail2
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textEmail2=document.createElement('input');
    cellLabel.innerHTML='<b>E-Mail 2:</b>';
    cellDatos.appendChild(textEmail2);
    this.textEmail2=textEmail2;
    
    //Observaciones
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellDatos=row.insertCell();
    var textObservaciones=document.createElement('<textarea  rows=3 cols=20>');
    cellLabel.innerHTML='<b>Observaciones:</b>';
    cellLabel.vAlign='top';
    cellDatos.appendChild(textObservaciones);
    this.textObservaciones=textObservaciones;
    
    //Boton
    row=tabla.insertRow();
    cellLabel=row.insertCell();
    cellLabel.colSpan=2;
    var btnGuardar=document.createElement('<input type=\'button\' value=\'Guardar\'>');
    cellLabel.appendChild(btnGuardar);
    cellLabel.align='center';
    JScriptPage.attachJSEvent(this,btnGuardar,'onclick',this.guardarPersona);
    
    this.textExtensiones=new Array(this.textExtension1,this.textExtension2,this.textExtension3);
    this.textNumeros=new Array(this.textNumero1,this.textNumero2,this.textNumero3);
    this.textEmails=new Array(this.textEmail1,this.textEmail2);
    
    this.personaNueva=new Object();
}
controls.CtrlSeleccionarPersona.prototype.cellResultadoMouseOver=function(e,cell)
{
    this.selectResultado(cell.resultado);
    this.applySelected(this.resultados);
}
controls.CtrlSeleccionarPersona.prototype.cellResultadoSelected=function()
{
    var resultado=this.getSelectedResultado();
    this.setSelectedPersona(resultado.persona);
}
controls.CtrlSeleccionarPersona.prototype.guardarPersona=function()
{
    var p=this.personaNueva;
    p.Nombre1=this.textNombre1.value;
    p.Nombre2=this.textNombre2.value;
    p.Apellido1=this.textApellido1.value;
    p.Apellido2=this.textApellido2.value;
    p.NumeroPersona=this.textNumeroPersona.value;
    p.Sexo=this.radioMasculino.checked?'M':'F';
    p.FechaNacimiento=this.dtPickerNacimiento.getValue();
    p.Observaciones=this.textObservaciones.value;
    
    //Contactos
    var contactos=new Array();
    for (var i=0;i<this.textExtensiones.length;i++)
    {
        if (this.textExtensiones[i].value!='')
        {
            var c=
            {
                Extension:this.textExtensiones[i].value,
                NumeroTelefono:this.textNumeros[i].value,
                TipoContacto:Model.TipoContacto.Telefono
            };
            contactos[contactos.length]=c;
        }
    }
    for (var i=0;i<this.textEmails.length;i++)
    {
        if (this.textEmails[i].value!='')
        {
            var c=
            {
                Email:this.textEmails[i].value,
                TipoContacto:Model.TipoContacto.Email
            };
            contactos[contactos.length]=c;
        }
    }
   
    var r=CtrlSeleccionarPersona2.GuardarPersona(p,contactos);
    p=r.value;
    this.hideCellNuevaPersona();
    this.setSelectedPersona(p);
    this.drawSearchResults([p]);
    this.drawPersonaCell(this.getResultadoByPersona(p));
    
    this.drawCellDetallePersona(p);
}
    

//-------------
