<?php use_helper('UrlFunctions'); ?>

<?php

if($control){//cuando se hizo el submit
 	
	if($idOrigen && $idDestino && $idOrigen != $idDestino){
		
		$o_PagoBean = new PagoBean();
		$o_PagoBean->setIdColaborador($idOrigen);
		$o_PagoBean->setIdColaboradorDestino($idDestino);
		$o_PagoDP = new PagoDP($o_PagoBean);
		$o_PagoDP->TrasladoPagos();
		
		$o_PrestamoBean = new PrestamoBean();
		$o_PrestamoBean->setIdColaborador($idOrigen);
		$o_PrestamoBean->setIdColaboradorDestino($idDestino);
		$o_PrestamoDP = new PrestamoDP($o_PrestamoBean);
		$o_PrestamoDP->TrasladoPrestamos();
		
		$bResult = 1;
	}
 
    if($bResult){
    ?>
      <script language="JavaScript">
     	 parent.objMainTab.reqBody('<?=buildUrl('asig/list')?>','&block=001&insert=1&cod=0');
      </script>
    <?
    }
       
  
  
  
}

if($id){
	
  $o_MercadoBean = new MercadoBean();
  $o_MercadoBean->setIdMercado($id);
  $o_MercadoBL = new MercadoBL();
  $o_MercadoBean = $o_MercadoBL->busquedaMercadoDetalles($o_MercadoBean);
  $a_MercadoRepresentante = $o_MercadoBean[0]->getArrayMerColaborador();
  
 foreach ($a_MercadoRepresentante as $iKey => $o_MColaboradorBean){
	$a_RepresentanteAux = $o_MColaboradorBean->getObjectColaborador();
	$a_Representante[] 	= $a_RepresentanteAux[0];
 }
  //$a_Representante = $a_MercadoRepresentante[0]->getObjectColaborador();
  
  
} 


?>

<script>
validateControls = function(){
  var errOutput               = { error:false, msg:"" };
  return errOutput;
}
</script>

<script type="text/javascript" src="<?php echo Constantes::ROOT_DIR_APP?>/web/js/objectForm.js"></script>

<script type="text/javascript">
  validatePass = function(){
  	//alert('*-*-*-*-');
  	if($('idOrigen').selectedIndex != $('idDestino').selectedIndex){
    	$('form1').submit();
  	}else{
  		alert('Debe ser diferente el representante Origen y Destino.');
  		return false;
  	}
  }
</script>



<script>
actualizarElementos = function(elmValue, elmDestino,type){
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=NombreColaborador&typeFind='+type+'&nombreElm='+elmDestino+'&value='+elmValue;
	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,'selected');
      		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>


<script>
insertOptionDefault = function(){
  
  var table = $('tableContent');
  var countRows = table.rows.length;
  
  var tr = table.rows[countRows-1];
  var td = tr.cells[1];
  var idSelect = td.firstChild.id;
  
  values = [[idSelect,'rep_;_Colaborador']];
  
  actualizaJSON('', values);
  
}
</script>

<script>
events = function(type){
  oReturn = validateControls();
  if(!oReturn.error){
    switch (type){
      case 'submit':
       $('form1').submit();break;
    }
  }else{
    alert(oReturn.msg);
    return false;
  }
}
</script>

<script>
buscaDetalle = function(e,elm){
  
  var charCode;
  if (navigator.appName == "Mozilla" || navigator.appName == "Netscape"){
    charCode = e.keyCode;
  }else{
    charCode = event.keyCode;
  }
  if (charCode == 114){
    
    var parentTr      = elm.parentNode.parentNode.id;
    var valueSelect   = $(parentTr + "_td_1_tipoColaborador[]").value;
    var url           = "";
    
    if(valueSelect != ""){
    	
      switch (valueSelect){
        case 'rep':
          url = '<?=buildUrlComplet('mnt.php/rep/listFind')?>iframe/contentResultxx/nameElm/'+elm.id+'/value/'+elm.value;
          tit = 'Representante';
        break;
        /*case 'alm':
          url = '<?=buildUrlComplet('mnt.php/alm/listFind')?>iframe/contentResultxx/nameElm/'+elm.id+'/value/'+elm.value;
          tit = 'Alumno';
        break;
        case 'ext':
          url = '<?=buildUrlComplet('mnt.php/ext/listFind')?>iframe/contentResultxx/nameElm/'+elm.id+'/value/'+elm.value;
          tit = 'Externo';
        break;*/
      }
      parent.objMainTab.modalDialog(url,{title:'Busqueda de '+ tit,width:600,height:420});
    }else{
      elementLoad.show(undefined,'&nbsp;&nbsp;&nbsp;Seleccione el tipo para el ListFind.'); setTimeout('elementLoad.hide()', 1000);
    }
  }
}
</script>

<script>
actionSelect = function(elm){
  
  var parentTr      = elm.parentNode.parentNode.id;
  
  $(parentTr + "_td_2_IdColaborador[]").value = "";
  $(parentTr + "_td_3").innerHTML = "&nbsp;";
 
}

actionCod = function(elm){
  //alert("Hola");
  elm       = $(elm);
  elm.value = elm.value.trim();
  var elmValue = elm.value;
  
  parentTr = elm.parentNode.parentNode.id;
  
  elmDestino = parentTr + "_td_3";
  
  if(elmValue != ""){
    
    if(!grid.validateByColumn(2, elm, 'INPUT')){
    
      var parentTr      = elm.parentNode.parentNode.id;
      var valueSelect   = $(parentTr + "_td_1_tipoColaborador[]").value;
      
      $(elmDestino).innerHTML = "&nbsp;";
      formItem.loadProcess(parentTr + "_td_3");
      actualizarElementos(elmValue, elmDestino, valueSelect);
      
    }else{
      
      elm.value = "";
      elementLoad.show(undefined,'&nbsp;&nbsp;&nbsp;El Empleado ya fue Ingresado'); setTimeout('elementLoad.hide()', 1000);
      $(elmDestino).innerHTML = "&nbsp;";
      
    }
    
  }
  
}
</script>


<table class="cuerpo" width="98%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="0">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody">
            <li>Asignación de Representante</li>
            <!--<span class="subtitle"><I>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:::&nbsp;Asignacion de Representante</I></span></td>-->
          <td align="right" valign="baseline">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td valign="top">
      <form id="form1" name="form1" method="POST"  >
      <input type="hidden" name="control" value="1">
      <input type="hidden" name="IdMercado" id="IdMercado">
       <!-- <input type="hidden" name="txtivnid" id="txtivnid"> -->
      <fieldset>
       <legend class="fielsettex"><I>Datos Generales</I></legend>
			
      <table   width="100%" class="marco" style="background: #E3E9E2">
        <tr>
          <td valign="top">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="50%" height="80" valign="top"><table height="80" border="0" cellpadding="0" cellspacing="2" class="SubCabecera">
                    
                     <tr>
  					   <td nowrap="nowrap" width="10%" class="cabecera"><label class="label req" for="txtDescripcion" >Mercado</label></td>
  					   <td nowrap="nowrap"><label class="label">&nbsp;:&nbsp;<?php echo $o_MercadoBean[0]->getDescripcion()?></label></td>
  						<td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
                  		<td nowrap="nowrap" width="10%" class="cabecera"><label class="label req" for="txtTelefono" >Teléfono</label></td>
                  		<td nowrap="nowrap"><label class="label">&nbsp;:&nbsp;<?php echo $o_MercadoBean[0]->getTelefono()?></label></td>
  						
				   </tr>
				   <tr>
				     <td nowrap="nowrap" width="10%" class="cabecera"><label class="label" for="txtDireccion">Direcci&oacute;n</label></td>
  					 <td nowrap="nowrap" ><label class="label" for="txtDireccion">&nbsp;:&nbsp;<?php echo $o_MercadoBean[0]->getDireccion()?></label></td>
  					 <td nowrap="nowrap" width="8%" class="cabecera">&nbsp;</td>
  					  <td nowrap="nowrap" width="10%" class="cabecera"><label class="label req" for="txtEstado" >Estado</label></td>
					  <td nowrap="nowrap" width="38%"><label class="label">&nbsp;:&nbsp;
					    	<?php  	if($o_MercadoBean[0]->getEstado() == 'A') echo "Activo";
						   			if($o_MercadoBean[0]->getEstado() == 'I') echo "Inactivo"
						   	?></label>
					 </td>
				   </tr>
                </table>
                </td>
              </tr>
            </table>
            <br>
            
            
                  <table width="100%" height="150" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td valign="top" id="contentDetail">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td>
                              <table border="0" cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                  <td class="tabSpace">&nbsp;</td>
                                  <td class="ssTab ssTabAct" id="ttab1" onclick=";" nowrap="nowrap">Traslado de Prestamos y Pagos</td>
                                  <td class="tabSpace">&nbsp;</td>
                                  <td width="100%" class="tabSpace">&nbsp;</td>
                                </tr>
                              </table>
                            </td>
                          </tr>
                          <tr>
                            <td class="marcoTab">
                              <div id="tab1">
                                <table class="SubDetalle" width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td valign="top">
                                      <table width="100%" border="0" cellpadding="5" cellspacing="0">
                                        <tr>
                                          <td align="left">
                                            <table class="marco" border="0" cellpadding="0" cellspacing="0" width="100%">
                                              <tr>
                                                <td>
                                                  <table class="grid" id="tableContent" width="100%" border="0" cellpadding="2" cellspacing="4">
                                                    <tr>
                                                    	<td width="16%"><label class="label">Representante Origen</label></td>
                                                    	<td>&nbsp;:&nbsp;
                                                    	<select id="idOrigen" name="idOrigen" class="combo required">
                                                    	<?php foreach ($a_Representante as $iKey => $o_ColaboradorBean){ ?>
								                           <option value="<?=$o_ColaboradorBean->getIdColaborador()?>">
								                           <?=$o_ColaboradorBean->getNombreCompleto()?>
								                           </option>
								                         <?php }?>
                                                    	</select> </td>
                                                    </tr>
                                                    <tr>
                                                    	<td><label class="label">Representante Destino</label></td>
                                                    	<td>&nbsp;:&nbsp;
                                                    	<select id="idDestino" name="idDestino" class="combo required">
                                                    	<?php foreach ($a_Representante as $iKey => $o_ColaboradorBean){ ?>
								                           <option value="<?=$o_ColaboradorBean->getIdColaborador()?>">
								                           <?=$o_ColaboradorBean->getNombreCompleto()?>
								                           </option>
								                         <?php }?>
                                                    	</select> </td>
                                                    </tr>
                                                  </table>
                                                </td>
                                              </tr>
                                            </table>
                                          </td>
                                        </tr>
                                      </table>
                                    </td>
                                  </tr>
                                </table>
                              </div>
                            </td>
                          </tr>
                        </table>
                      </td>
                    </tr>
                  </table>
                  <div align="right">
                  <input type="hidden" name="typeSubmit" id="typeSubmit" onkeyup="">
                  <input class="button" type="button" value="Grabar" onclick="$('typeSubmit').value='list'; validatePass();"/>
                  <input class="button" type="button" value="Cancelar" onclick="parent.objMainTab.reqBody('<?=buildUrl('asig/list')?>','001')" />
                  </div>
          </td>
        </tr>
       </table>
       </form>
       </fieldset>
       

    </td>
  </tr>
</table>


<script type="text/javascript">
	function formCallback(result, form) {
		window.status = "valiation callback for form '" + form.id + "': result = " + result;
	}
	var valid = new Validation('form1', {immediate : true});
</script>

