<?php use_helper('UrlFunctions');

if($control){
	
	if(is_array($cboRepresentante)){
		$ok = 1;
		foreach ($cboRepresentante as $key => $value){
			if(!$value){
				$ok = 0;
				break;
			}
		}
		reset($cboRepresentante);
		if($ok){
			//Cambiar al cliente de Mercado
			$o_ClienteBean = new ClienteBean();
			$o_ClienteBean->setIdCliente($idCliente);
			$o_ClienteBean->setIdMercado($IdMercado);
			$o_ClienteDP = new ClienteDP($o_ClienteBean);
			$o_ClienteDP->update();
			
			foreach ($cboRepresentante as $key => $value){
			
				
				//Cambiar cada cuenta 
				foreach ($IdPrestamo as $keyP => $valueP){
					$o_PrestamoBean = new PrestamoBean();
					$o_PrestamoBean->setIdColaboradorPrestamo($cboRepresentante[$keyP]);
					$o_PrestamoBean->setIdPrestamo($valueP);
					$o_PrestamoDP = new PrestamoDP($o_PrestamoBean);
					$o_PrestamoDP->update();
				}
			}
		}
		$bResult = $ok;
		if($bResult){
		    ?>
		    <script language="JavaScript">
		    parent.objMainTab.reqBody('<?=buildUrl('cli/list')?>','&block=001&changeMercado=1&id=<?=$idCliente?>');
		    parent.objMainTab.windowsCloseAll();
		    </script>
		    <?
		  }
	}else{
		
	}
}


?>
<script>
actualizaJSON = function(request, json){
 
  elementos_hidden = $$('input.texto');
  
  var n_elementoh = elementos_hidden.length;
  
  var numeroElementos = json.length;
  
 for (var j = 0; j < n_elementoh; j++){
 	
 	for (var i = 0; i < numeroElementos; i++){
 	
 
    if($(json[i][0]+"_"+(j+1))){
      $(json[i][0]+"_"+(j+1)).length = 0;
  	  switch ($(json[i][0]+"_"+(j+1)).tagName){
  	    case 'TD'      : Element.update(json[i][0]+"_"+(j+1), json[i][1]); break;
  	    case 'INPUT'   : $(json[i][0]+"_"+(j+1)).value=json[i][1]; break;
  	    case 'SELECT'  : 
  	      var objDDL = $(json[i][0]+"_"+(j+1));
  	     
  	      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();
      }
    }
  }
 }
}

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

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);
}
</script>
<?php

$o_ClienteBean = new ClienteBean();
$o_ClienteBean->setIdCliente($id);
$o_class = new ClienteBL();
$a_ObjectBean = $o_class->busquedaCliente($o_ClienteBean);

//Mercados
$o_ClienteSystemClass = new ClienteSystemClass(new TipoNegocioBean());
$a_Mercado = $o_ClienteSystemClass->LoadMercadoXRol();

if(is_array($a_ObjectBean)){
  while(list($key,$o_ObjectBean) = each($a_ObjectBean)){
?>
<img src="/images/common/spacer.gif" width="10">
<form method="POST" >
<table class="cuerpo" width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="0">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody">
            <li>Cambio de Mercado</li>
            <span class="subtitle"><I>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:::&nbsp;Ficha de Cambio de Mercado</I><?php echo $id?></span></td>
          <td align="right" valign="baseline">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
  		<td>
			<table>
				<tr>
					<td width="12%" nowrap="nowrap" class="cabecera"><label class="label req" for="txtSueldo" >Cliente</label></td>
					<td nowrap="nowrap">
					<label class="label"><?php echo $o_ObjectBean->getNombreCompleto();?></label>
					</td>
				</tr>
				<tr>
					<td width="12%" nowrap="nowrap" class="cabecera"><label class="label req" for="txtSueldo" >Mercado</label></td>
					<td nowrap="nowrap">
					<select class="combo validate-selection " name="IdMercado" id="IdMercado" onchange="AsignarRepresentantes('cboRepresentantes',this)">
						<?php foreach ($a_Mercado as $iKey => $o_MercadoBean){ ?>
								 <option value="<?=$o_MercadoBean->getIdMercado()?>" <?php if($o_MercadoBean->getIdMercado() == $o_ObjectBean->getIdMercado() ) echo " SELECTED "?>>
							<?=$o_MercadoBean->getDescripcion()?>
						</option>
						<?php } ?>
					</select>
					</td>
				</tr>
			</table>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
	       		<tr>
		          <td class="titleBody">
		            <span class="subtitle"><I>Cuentas en estado Pendiente</I></span></td>
		          <td align="right" valign="baseline">&nbsp;</td>
		        </tr>
	        </table>
			<table class="grid" width="100%" border="0" cellpadding="0" cellspacing="0">
			      <tr height="23">
					<th width="4%" >Nro</th>
					<th width="9%" >F.Incio</th>
					<th width="9%">F.Venci.</th>
					<th width="6%" align="right">Monto</th>
					<th width="6%" align="right">Saldo</th>
					<th width="8%" id="col01" onMouseOver="sort_col(this);" align="center" >Usr.Crea.</th>
					<th width="8%" id="col01" onMouseOver="sort_col(this);" align="left"  >Fec.Crea.</th>
					<th width="8%" id="col01" onMouseOver="sort_col(this);" align="left" >Representante</th>
					<th width="8%" id="col01" onMouseOver="sort_col(this);" align="left" style="color:#0000FF">al Representante</th>
			      </tr>
			      
			      <?
			      if($id){
	
					$o_PrestamoBean = new PrestamoBean();
					$o_PrestamoBean->setIdCliente($id);
					$o_PrestamoBean->setEstado('P');
					$o_class = new PrestamoBL();
					$a_ObjectBeanP = $o_class->busquedaPrestamo($o_PrestamoBean);
					
					if(is_array($a_ObjectBeanP)){
						$i = 1;
  						while(list($key,$o_ObjectBean) = each($a_ObjectBeanP)){?>
  							 <tr height="23">
								<td width="4%" ><input type="hidden" name="IdPrestamo[]" value="<?=$o_ObjectBean->getIdPrestamo()?>"><?=$o_ObjectBean->getIdPrestamo()?></td>
								<td align="center"><? echo $o_ObjectBean->getFechaInicio();?></td>
								<td align="center"><? echo $o_ObjectBean->getFechaVencimiento();?></td>
								<td align="right"><? echo number_format($o_ObjectBean->getMontoTotal(),2);?></td>
								<td align="right"><? echo number_format($o_ObjectBean->getSaldoPrestamo(),2);?></td>
								<td width="10%" align="center"><? echo $o_ObjectBean->getUsuarioCreacion();?></td>
								<td width="15%" ><? echo $o_ObjectBean->getFechaCreacion();?></td>
								<td width="21%" ><? 
								
								$o_ColobaboradorBean = new ColaboradorBean();
								$o_ColobaboradorBean->setIdColaborador($o_ObjectBean->getIdColaborador());
								$o_ColobaboradorBL = new ColaboradorDP($o_ColobaboradorBean);
								$a_o_colaborador = $o_ColobaboradorBL->select();
								if(is_array($a_o_colaborador)){
									foreach ($a_o_colaborador as $key => $o_ColaboradorBean ){
										echo $cola = $o_ColaboradorBean->getApellidoPaterno()." ".$o_ColaboradorBean->getApellidoMaterno().", ".$o_ColaboradorBean->getNombres();
									}
								}
								?></td>
								<input type="hidden" name="Representante" class="texto" value="<? echo $o_ObjectBean->getIdColaborador();?>">
								<td width="20%" style="color:#FF0000">	
								<select id="cboRepresentantes_<?=$i?>" name="cboRepresentante[]">
									<option value=""><?=$cola?></option>
								</select></td>
						      </tr>	
  						<?
  						$i++;
						}
					}
				}
				?>
			      
			</table>
			
			
		</td>
	</tr>
</table>
<input type="hidden" name="control" value="1">
<input type="hidden" name="idCliente" value="<?=$id?>">
<br>
<div style="float:left">

<input type="submit" name="Enviar" value="Guardar" class="button">
</div>
<div style="text-align:right">

<input class="button" type="button" value="Cancelar" onclick="parent.objMainTab.windowsCloseAll();"/>
</div>
</form>
<?php } 
}?>