<?php use_helper('UrlFunctions'); ?>
<script>
findControl = function(){
  
  concatValueStr();
  
}

exportFindControl = function(action){
  strFormSerialize = returnQueryStringForm();
  executeFind(strFormSerialize, action);
}

executeFind = function(str, action){
  if(typeof(action) == 'undefined'){
	  url = "<?=buildUrl('asigcob/grid', "block=".$block)?>";
	}else{
	  switch (action){
	    case 'excel': parent.objMainTab.reqBody('<?=buildUrl('asigcob/excel')?>','001&'+str); break;
	    case 'pdf': parent.objMainTab.reqBody('<?=buildUrl('asigcob/pdf1')?>','001&'+str); break;	    
	  }
	  
	  return;
	}
  //str = str + '&block=001';
  url = "<?=buildUrl('asigcob/grid', "block=".$block)?>";
	var myAjax  = new Ajax.Updater(
                                	"resultGrtid",
                                	url,
                                	{
                                		asynchronous:	true,
                                		method			: 'get',
                                		parameters	: str,
                                		evalScripts	: true,
                                		onLoading   : cargandoImagenSerch(),
                                		onComplete	: function(){
                                			//objMainTab.showMenu();
                                		}
                                	});
}

concatValueStr = function(){
  strFormSerialize = returnQueryStringForm();
  if(arguments[0]){
    strFormSerialize += '&'+arguments[0];
  }
  executeFind(strFormSerialize);
}

function returnQueryStringForm(){
  cadenaFormulario = $('form1').serialize();
             var posicion = new Array();
             var a_pars = new Array();
             var miArray = new Array();
             posicion=[];
             a_pars=[];
             miArray=[];
             miArray = cadenaFormulario.split('&');
             x=0;
             for (i = 0; i < miArray.length; i++){
            	   posicion[i] = miArray[i].indexOf('=');
            	   if(typeof(miArray[i-1])!='undefined'){
            				if(miArray[i].substring(posicion[i],0)== miArray[i-1].substring(posicion[i-1],0) ){
           					 porcion = miArray[i].substring((1+posicion[i]));
           					 a_pars[x-1]+='@'+porcion;
           				}else{
          					 a_pars[x]= miArray[i];
         					 x++;
          				}
          		}else{
          			 a_pars[x]= miArray[i];
          			  x++;
          		}
           }
  cadenaFormulario = a_pars.join('&');
  cadenaFormulario += '&id_form=frmFindFedu';
  return(cadenaFormulario);
}

function cargandoImagenSerch(){
	//disabledWin.setPathImage('/images/common/progress.gif');
	disabledWin.show(true,true,'resultGrtid');
}

</script>
<script>
findControl = function(){
  
  var pars = $('form1').serialize();
  
	disabledWin.setPathImage('/images/common/progress.gif');
	disabledWin.show(true,true,'resultGrtid');
  
	url = "<?=buildUrl('asigcob/grid', "block=".$block)?>";
	var myAjax  = new Ajax.Updater(
	"resultGrtid",
	url,
	{
		asynchronous:	true,
		method			: 'get',
		parameters	: pars,
		evalScripts	: true,
		onLoading		: function(){
			//$('contentMenu').innerHTML = $('messageLoadMenu').innerHTML;
		},
		onComplete	: function(){
			//objMainTab.showMenu();
		}
	});
  
}

deleteRegistro = function(fecha,idmercado,idcolaborador){
  //buildUrl('rep/delete/id/'block=001'
  if(confirm("Esta seguro de eliminar la asignacion de prestamos: "+fecha+" "+idmercado+" ?")){
      //objMainTab.reqBody('<? //=buildUrl('rep/add')?>','block=001&delete=1&id='+id);   
      parent.objMainTab.reqBody('<?=buildUrl('asigcob/delete')?>','&delete=1&fecha='+fecha+'&idmercado='+idmercado+'&idcolaborador='+idcolaborador);
  }
  
}

acutalizarEspecialidad = function(destino,elm){
  $(destino).options.length = 1;
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=getEspecialidad&nombreElm='+destino+'&value='+elm.value;
  
	var myAjax = new Ajax.Request(
                            		url, 
                            		{
                            			method: 'get', 
                            			parameters: pars, 
                            			onComplete: actualizaJSON
                            		});
}

</script>
<script>
actualizaJSON = function(request, json){
  var numeroElementos = json.length;
  for (var i = 0; i < numeroElementos; i++){
    if($(json[i][0])){
  	  switch ($(json[i][0]).tagName){
  	    case 'TD'      : Element.update(json[i][0], json[i][1]); break;
  	    case 'INPUT'   : $(json[i][0]).value=json[i][1]; break;
  	    case 'SELECT'  : 
  	      var objDDL = $(json[i][0]);
  	      if(typeof(cont) == 'undefined'){
  	        var cont = 0;
  	        objDDL.options.length = 1;
  	      }
  	      var a_contentValues = json[i][1].split("_;_");
      		var value	= a_contentValues[0];
      		var text	= a_contentValues[1];
  	      var option	= new Option(text,value);
      		try{
      			objDDL.add(option,null);
      		}catch (e){
      			objDDL.add(option,-1);
      		}
  	      cont++;
  	    break;
  	  }
    }else{
      if(json[i][0].indexOf("function") > -1){
        eval("scriptEval = "+json[i][0]);
        scriptEval();
      }
    }
  }
}
</script>

<?php


//Cargar al Colaborador
 $o_ColaboradorBean = new ColaboradorBean();
 $o_ColaboradorBean->setIdPadre(myUser::getUser()->getIdColaborador());	
 $o_ColaboradorBean->setIdRol('sys.sup');
 
 $o_ColaboradorBean->sortCampos = "Apellido;Nombre";
 $o_ColaboradorBean->sortType = "1;1";

 //$o_ColaboradorBean->setEstado('A');

 $o_AdelantoSystemClass = new AdelantoSystemClass($o_ColaboradorBean);
 
 $a_Colaborador_1 = $o_AdelantoSystemClass->getColaborador();
 
 $a_Colaborador = $a_Colaborador_1;

if(myUser::getUser()->getIdPadre() == "A"){ //Todos los Padres Rol actualmente sys.gen y sys.view
	//SIN FILTRO
}else{
	$idAdministrador = myUser::getUser()->getIdColaborador();
}

if($idAdministrador){
	$o_SucursalBean = new SucursalBean();
	$o_SucursalBean->setIdAdministrador($idAdministrador);
	$o_SucursalBL = new SucursalBL();
	$a_o_sucursal = $o_SucursalBL->busquedaSucursal($o_SucursalBean);
	//----------------------------------------------------
	
	$IdSucursal = $a_o_sucursal[0]->getIdSucursal();

}

$o_BalanceDia = new BalanceDiaBean();
$o_BalanceDia->setIdSucursal($IdSucursal);
$o_BalanceDia->setIdAdministrador($idAdministrador);

$o_BalanceDiaDP = new BalanceDiaDP($o_BalanceDia);
$fecha = $o_BalanceDiaDP->ObtenerDiaAbierto();

////////////////
$IdAdministrador	  		= myUser::getUser()->getIdColaborador();
$o_ObjectBean 				= new SucursalBean();
$o_ObjectBean->setIdAdministrador($IdAdministrador);
		
$o_class 					= new SucursalBL();
$a_ObjectBean 				= $o_class->busquedaSucursal($o_ObjectBean);

if(count($a_ObjectBean) > 0){
	foreach ($a_ObjectBean as $iKey => $o_objBean){
		$a_user['idsucursal'] = $o_objBean->getIdSucursal();
		$a_user['sucursal'] = $o_objBean->getDescripcion();
		$o_SucursalMercadoBean = new SucursalMercadoBean();
		$o_SucursalMercadoBean->setIdSucursal($o_objBean->getIdSucursal());
		$o_SucursalMercadoBL = new SucursalMercadoBL();
		$a_o_sucursalmercado = $o_SucursalMercadoBL->busquedaSucursalMercadoDetalle($o_SucursalMercadoBean);
		if(is_array($a_o_sucursalmercado)){
			foreach ($a_o_sucursalmercado as $key => $o_sucursalmercado){
				$a_IdMercados[] = $o_sucursalmercado->getIdMercado();
			}
		}
	}
	$o_MercadoBean = new MercadoBean();
	$o_MercadoBean->sortCampos = "Descripcion";
	$o_MercadoBean->sortType = "1";
	if(!is_array($a_IdMercados)) $a_IdMercados[] = 'x'; 
	$o_MercadoBean->setIdMercados($a_IdMercados);
	$o_MercadoDP = new MercadoDP($o_MercadoBean);
	$a_o_Mercados = $o_MercadoDP->select();
}


?>


<img src="/images/common/spacer.gif" width="10">
<table class="cuerpo" style="background: #E3E9E2" width="99%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top">
      <form name="form1" id="form1" method="POST">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody"><li>&nbsp;&nbsp;Asignacion de Cobranza</li></td>
          <td align="right" valign="baseline">
            <img name="add" src="/images/common/buttons/btnAdd.gif" title="Nuevo Ingreso" onClick="parent.objMainTab.modalDialog('<?=buildUrl('asigcob/add')?>','001')" style="cursor:pointer">
              <!-- <img src="/images/common/buttons/btnExcel.gif" title="Exportar a Excel" onClick="exportFindControl('excel');" style="cursor:pointer"/>
            <img src="/images/common/buttons/btnPdf.gif" title="Exportar a PDF" onClick="exportFindControl('pdf');" style="cursor:pointer"/> -->
          </td>
        </tr>
      </table>
      <table class="marco" width="100%">
        <tr>
          <td align="center" valign="top">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="100%" valign="top">
                  <table class="SubCabecera" width="98%" border="0" cellpadding="0" cellspacing="2">
                    <tr>
                      <td width="15%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtDescripcion">Supervisor</label></td>
                      <td nowrap="nowrap"><select class="combo validate-selection " name="cboColaborador" id="cboColaborador" onchange="acutalizarEspecialidad('cboEspecialidad',this)" >
                        <option value="" > - Seleccionar - </option>
                          <?php foreach ($a_Colaborador as $iKey => $o_ColaboradorBean){ ?>
                           <option value="<?=$o_ColaboradorBean->getIdColaborador()?>">
                           <?=$o_ColaboradorBean->getNombreCompleto()?>
                           </option>
                          <?php } ?>
                        </select>
                    </td>
                    <td width="15%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtDescripcion">Mercado</label></td>
                      <td nowrap="nowrap"><select class="combo validate-selection " name="cboMercado" id="cboMercado" >
                        <option value="" > - Seleccionar - </option>
                          <?php foreach ($a_o_Mercados as $iKey => $o_Mercado){ ?>
                           <option value="<?=$o_Mercado->getIdMercado()?>">
                           <?=$o_Mercado->getDescripcion()?>
                           </option>
                          <?php } ?>
                        </select>
                    </td>
                    <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td> 
                    <td width="8%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtNombre">F.&nbsp;Inicial</label></td>
                    <td nowrap="nowrap"><input class="text" type="text" name="txtFechaInicial" ondblclick="this.value=''" id="txtFechaInicial" size="7" readonly value="<?=$fecha?>"/>&nbsp;<button type="button" onclick="return false" id="btnFechaInicial">...</button></td>
                      <script type="text/javascript">
                      		/*var dateStart=new Date() // Today
							dateStart.setHours(0,0,0,0)
							var dateEnd=new Date() // Today
							dateEnd.setDate(dateStart.getDate()+10);	// Allow 30 days
							dateEnd.setHours(0,0,0,0);
                            document.getElementById("txtFechaInicial").disabled = false;*/
                            Calendar.setup({
                              inputField : "txtFechaInicial",
                              ifFormat : "%Y-%m-%d",
                              daFormat : "%Y-%m-%d",
                             
                              //the function to call, true to disable, false to enable
							 //disableFunc  : dateOutOfRange, 
                              button   : "btnFechaInicial"
                            });
                            
                            
							
							/* 
							* Can't choose days after the
							* end date if it is choosen, hilights start and end dates with one style and dates between them with another
							*/
							function dateOutOfRange(date) {
								var dateSelected=new Date(date)
								dateSelected.setHours(0,0,0,0)
								//alert(dateStart.getTime());		//1530507600000
								//alert(dateEnd.getTime()); 		//1531371600000
								//alert(dateSelected.getTime());	//1527829200000
								if (dateSelected.getTime() < dateStart.getTime() || dateSelected.getTime() > dateEnd.getTime())
									// out of range, not allowed
									return true;
				
								// see if this day of week is allowed
								if (bDow) 
								{
									// If DOW clicked see if this DOW is allowed, 
									// if allowed then return false, not disabled
									return !arrDOW[date.getDay()]
								}
				
				
								if (bTrace)
									trace(
										'dateStart.....' + dateStart.print("%a, %B %d %Y") + '\n' +
										'dateSelect....' + dateSelected.print("%a, %B %d %Y") +  '\n' +
										'dateEnd.......' + dateEnd.print("%a, %B %d %Y"))
				
								return false
							}

                        </script>
                      <td width="8%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtNombre">F.&nbsp;Final</label></td>
                      
                      <td nowrap="nowrap"><input class="text" type="text" ondblclick="this.value=''" name="txtFechaFinal" id="txtFechaFinal" size="7" readonly value="<?=$fecha?>"/>&nbsp;<button type="button" onclick="return false" id="btnFechaFinal">...</button></td>
                      <script type="text/javascript">
                            document.getElementById("txtFechaFinal").disabled = false;
                            Calendar.setup({
                              inputField : "txtFechaFinal",
                              ifFormat : "%Y-%m-%d",
                              daFormat : "%Y-%m-%d",
                              button   : "btnFechaFinal"
                            });
                        </script>
                    <td width="15%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtDescripcion">Estado</label></td>

					           <tr>
			    		         <td align="right" style="vertical-align:top" colspan="5"><input class="button" type="button" value="Buscar" onclick="findControl();"/></td>
			 		         </tr>
                  </table>
                </td>
            </table>
          </td>
        </tr>
      </table>
      </form>
      <div id="resultGrtid">
      </div>
    </td>
  </tr>
</table>


<?php
if(isset($insert)){
  ?>
  <script>
    elementLoad.show(undefined,"Se inserto la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdAdelanto').value = "<?=$id?>";
    $('cboAdministrador').value = "<?=$cboAdministrador?>";
    $('FechaEntrega').value = "<?=$FechaEntrega?>";
    $('cboEstado').value = "<?=$cboEstado?>";
    findControl();
  </script>
  <?php
}
if(isset($update)){
  ?>
  <script>
    elementLoad.show(undefined,"Se actualizo la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdAdelanto').value = "<?=$id?>";
    $('cboColaborador').value = "<?=$cboColaborador?>";
    $('FechaEntrega').value = "<?=$FechaEntrega?>";
    $('cboEstado').value = "<?=$cboEstado?>";
    findControl();
  </script>
  <?php
}
if(isset($delete)){
  ?>
  <script>
    elementLoad.show(undefined,"Se elimino la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdAdelanto').value = "<?=$id?>";
    $('cboAdministrador').value = "<?=$cboAdministrador?>";
    $('FechaEntrega').value = "<?=$FechaEntrega?>";
    $('cboEstado').value = "<?=$cboEstado?>";
    findControl();
  </script>
  <?php 
  
}
?>