<?php
$zstyle='height:1.9em;';
$zstyle1='';
// $zstyle3='';

// $zmaintag='datalist';
// $zmaintag='result';

if(empty($zmaintag)){$zmaintag='select';}

$zlong=strlen($zcaption);
$zdefa=trim($zdefa);
$zvalue=trim($zvalue);

if($zbool==1){$zoptions='1,SI;2,NO';}
if($ztriv==1){$zoptions='0,Todo;1,SI;2,NO';}
if($ztetrav==1){$zoptions='0,Todo;1,Positivo;2,Negativo;3,En Cero;';}
if($zheptav==1){$zoptions='0,Todo;6,< 0;8,= 0;9,> 0;68,<= 0;69,<> 0;98,>= 0;';}
if($zgamacolor==1){$zoptions='0,Todo;Amarillo;Azul;Rojo;Verde;Morado;Naranja;Blanco;Negro;';}
if($zmes==1){$zoptions='1,Enero;2,Febrero;3,Marzo;4,Abril;5,Mayo;6,Junio;7,Julio;8,Agosto;9,Septiembre;10,Octubre;11,Noviembre;12,Diciembre;';}
if($zdia==2){$zoptions='Sábado;7,Domingo;';}
if($zdia==5){$zoptions='1,Lunes;2,Martes;3,Miércoles;4,Jueves;5,Viernes;';}
if($zdia==7){$zoptions='1,Lunes;2,Martes;3,Miércoles;4,Jueves;5,Viernes;6,Sábado;7,Domingo;';}
if($zanio==1){$xyear = date("Y");for ($idx = $xyear ; $idx > 1945 ; $idx--){$zoptions.= $idx.";";}}
if($zdestino>=1)
{
     $zoptions='';
     $zoptions.='0,Pantalla;';
     $zoptions.='1,Impresora;';
//      $zoptions.='3,Excel';
}
if($zoptn1>0 and $zoptn2>$zoptn1)
{
     $zoptions='';
     if($ztodo==1){$zoptions.=''.','.'- TODO - '.';';}
     for($coptn=$zoptn1; $coptn<=$zoptn2; $coptn++)
     {$zoptions.=$coptn.','.$coptn.';';	}
}
if(!empty($zoptions))
{if($ztodo==1){$zoptions=' '.','.' - TODO - '.';'.$zoptions;}}

if(empty($zoptions))
{	
	if(empty($zdocu))
	{
		if(!empty($zdefa))
		{
			$sql_defa ='select * from '.$ztabla.'0 where '.$ztabla.'_codigo="'.$zdefa.'"';
			$res_defa=msql_query($sql_defa,$link_data);
			if(msql_num_rows($res_defa)!=0){$zdefad=msql_result($res_defa,0,$ztabla.'_descri');}
		}
          include('col2list.php');
	}
	else
	{
		if(!empty($zdefa))
		{
			$sql_defa ='select * from '.$ztabla.'2 ';
			$sql_defa.='where docu like "'.$zdocu.'%" ';
			$sql_defa.='and '.$zcolu.'="'.$zdefa.'" ';
			$sql_defa.='and status not like ("O","X") ';
			$res_defa=msql_query($sql_defa,$link_data);
			if(msql_num_rows($res_defa)!=0){$zdefad=msql_result($res_defa,0,$zcolu2);}
		}

		$sql_sele ='select * from '.$ztabla.'2 ';
		$sql_sele.='where docu like "'.$zdocu.'%" ';
		$sql_sele.='and status not in ("O","X") ';
		if(!empty($zwhere))
		{$sql_sele.='and ('.$zwhere.') ';}
		$sql_sele.='group by '.$zcolu2.' ';
		$sql_sele.='order by '.$zcolu2.' ';

		$res_sele=msql_query($sql_sele,$link_data);
		$zoptions.=$zdefa.','.$zdefad.';';
		if(msql_num_rows($res_sele)>0)
		{
			while ($aso_sele=msql_fetch_assoc($res_sele))
			{$zoptions.=$aso_sele[$zcolu].','.$aso_sele[$zcolu2].';';}
		}
	}
}

$zoptions=explode(';',$zoptions);
if(!empty($zsort)){sort($zoptions);}

for ($copt=0; $copt<count($zoptions); $copt++)
{ 
     $optval=explode(',',$zoptions[$copt]); 
     $seloptv1= $optval[0]; 
     $seloptv2= $optval[1]; 
     $xlong=strlen($seloptv2);
     if($xlong>$zlong){$zlong=$xlong;}
} 

if(empty($zobj)){$zobj=3;}
if(empty($zsize)){$zsize=1;}
if(empty($zalign)){$zalign='left';}
if(empty($zmaxlen)){$zmaxlen=0;}
if(empty($zid)){$zid=$ztabla;}
if(empty($zid)){$zid=$zlabel;}
if(empty($zlabel)){$zlabel=$zid;}
if(empty($zid)){$zid=microtime()*1000000;}
if(empty($zvalue) and !empty($zdefa) and empty($ztodo)){$zvalue=$zdefa;}
if(empty($zcolu2)){$zcolu;}

if(empty($zvalue)){$zreado=0;}


if(@mb_stristr('lsub.zsub.csub.asub',$ztabla)){$zoptgroup=1;}
if(getPlatform()=='Android'){$android=1;}
// echo '<br>**'.$oscreen;

// if(($zobj==2 or $zobj==3 or $zobj==6) and $ztcell<=1 and getPlatform()=='Android'){$zobj=4;}
if(($zobj==2 or $zobj==3 or $zobj==6) and $ztcell<=1 and $android==1){$zobj=4;}

if(!empty($ztabla))
{
   $sql_input='describe '.$ztabla.'0';
   $res_input=msql_query($sql_input,$link_data);
   
   if(!msql_error($link_data))	
   {
      $zmaxlen=msql_result($res_input,0,1);
      $zmaxlen=str_replace(')','',$zmaxlen);
      $zmaxlen=str_replace('varchar(','',$zmaxlen);
      $zmaxlen=str_replace('char(','',$zmaxlen);
      $zmaxlen=str_replace('int(','',$zmaxlen);
      $ztype='search';
      $zwidth=0;
   }else{$ztabla='';}
}

if(empty($zwidth) and !empty($zmaxlen)){$zwidth=(int)$zmaxlen*(.7);}
if(!empty($zsize) and $zsize!=1){$zstyle.='font-size:'.($zsize).'em;';$zstyle.='height:1.7em;';}
if(!empty($zsize) and $zsize!=1){$zstyle1.='font-size:'.($zsize).'em;';}
if(!empty($zmaxwidth)){$zstyle.='max-width:'.($zmaxwidth).'em;';}
// if(!empty($zsize) and $zsize!=1){$zstyle3.='font-size:'.($zsize).'em;';$zstyle3.='height:'.($zsize).'em;';}

$zstyle1.='vertical-align:top;';
if($ztcell>=2){$zstyle1.='width:100%;';}

$domd_s = new DOMDocument();
     $domd_s->encoding = 'utf-8';
     $domd_s->xmlVersion = '1.0';
     $domd_s->formatOutput = true;
$elem_s=$domd_s;

if(!empty($zdom)){$domd_s=$zdom;}
if(!empty($zele)){$elem_s=$zele;}

// if($zobj>1)
{
     
if($ztcell>=2 and $zobj>=2 and $zobj<=3)
{
     $element=$elem_s->appendChild($domd_s->createElement("td"));
     $element->setAttribute("valign","top");
}
else
{
     $element=$elem_s->appendChild($domd_s->createElement("span"));
     $xstyle0="float:".$zalign.";margin-left:".$zsize*(0.5)."em;margin-right:".$zsize*(0.5)."em;margin-bottom:".$zsize*(0)."em;";
     if($zobj==4)
     {$xstyle0.="margin-top:10px;";}
     
     if(empty($zdom))
     {$element->setAttribute("style",$xstyle0);}
     
     if(mb_stristr($zwidth,'%'))
     {$xstyle0='width:'.$zwidth.';';}
     if($zobj==4)
     {$xstyle0.="margin-top:10px;";}
     
//      {$element->setAttribute("style","width:".$zwidth."");}
     {$element->setAttribute("style",$xstyle0);}
     
}
if(!empty($zhide)){$element->setAttribute("hide","");}
if(!empty($znoprint)){$element->setAttribute("noprint","");}

  // windows
   if($zobj==2 or $zobj==3)
   {
      $zclass1='';
      $label=$element->appendChild($domd_s->createElement("button",$zlabel));   
      if(!empty($zid))
      {
        $label->setAttribute("id",$zid.'_l');
        $label->setAttribute("type","button");
        $label->setAttribute("tabindex","-1");
      }
      if(!empty($zlabel)){$label->setAttribute("value",$zlabel);}
      if(!empty($zstyle1)){$label->setAttribute("style",$zstyle1);}
      if(!empty($zclass1)){$label->setAttribute("class",$zclass1);}
      if(!empty($zdefa)){$label->setAttribute("defa",$zdefa);}
      if(!empty($zonclick)){$label->setAttribute("onclick",$zonclick);}
   }

}


// if(!empty($zsave)){$zonblur.="{save_data(event,'".$zid."');}";}
if(!empty($zsave)){$zonchange.="{save_data(event,'".$zid."');}";}


if($ztcell>=2)
{
  $element=$elem_s->appendChild($domd_s->createElement("td"));
  $element->setAttribute("colspan",$ztcell);
  $element->setAttribute("valign","top");
}

    //android
   if($zobj==4)
   {
      $zclass1='input-label';
      $label=$element->appendChild($domd_s->createElement("label",$zlabel));   
      if(!empty($zid)){$label->setAttribute("id",$zid.'_l');}
      if(!empty($zid)){$label->setAttribute("for",$zid);}
      if(!empty($zstyle1)){$label->setAttribute("style",$zstyle1);}
      if(!empty($zclass1)){$label->setAttribute("class",$zclass1);}
   }

     $div=$element->appendChild($domd_s->createElement("span"));
     if(mb_stristr($zwidth,'%'))
     {$div->setAttribute("style","width:".$zwidth."");}

// echo '=='.$zonblur;

     $input=$div->appendChild($domd_s->createElement($zmaintag," "));
          if(!empty($zid)){$input->setAttribute("id",$zid);}
          if(!empty($zname)){$input->setAttribute("name",$zname);}
          if(!empty($zclass)){$input->setAttribute("class",$zclass);}
          if(!empty($zonchange)){$input->setAttribute("onchange",$zonchange);}
          if(!empty($zonblur)){$input->setAttribute("onblur",$zonblur);}
          if(!empty($zvalue)){$input->setAttribute("value",$zvalue);}
          if(!empty($zstyle)){$input->setAttribute("style",$zstyle);}
          if(!empty($zreado)){$input->setAttribute("disabled","");}
          if(!empty($zreado)){$input->setAttribute("bold","");}

          if(!empty($zsave))
          {
               $input->setAttribute("savet",$zsavet);
               if(!empty($zsavec))
               {$input->setAttribute("savec",$zsavec);}
               else
               {
               $input->setAttribute("savei",$zsavei);
               if(!empty($zsaved)){$input->setAttribute("saved",$zsaved);}
               }
               $input->setAttribute("savef",$zsavef);
          }


          if(!empty($zdefa))
          {
//                $zoptions=$zdefa.';'.$zoptions;
          }


          $xlastg='';
          $xoptgroup=false;
          for ($copt=0; $copt<count($zoptions); $copt++)
          { 
               $optval=explode(',',$zoptions[$copt]); 
               $seloptv1= ($optval[0]); 
               $seloptv2= ($optval[1]); 
               
//                if(!empty($ztabla) and !empty(trim($seloptv1)) and !empty($user_codigo) and empty($zreado))
//                {$seloptv2.=' ('.$seloptv1.')';}
               if(@mb_stristr('docu.cair',$ztabla) and !empty(trim($seloptv1)) and !empty($user_codigo) and empty($zreado))
               {$seloptv2.=' ('.$seloptv1.')';}
               
               if($seloptv2==""){$seloptv2=$seloptv1;}
               
               if(($seloptv1)!="")
               {
                    if(!empty($ztabla))
                    {
                         if(mb_stristr('ctas.item.enti',$ztabla)){$seloptv2=$seloptv1.' - '.$seloptv2;}
                         
                         if($zoptgroup==1)
                         {
//                               if(substr(trim($seloptv1),-1)=='-')
//                               if(substr(trim($seloptv1),-1)=='-' and $android!=1)
//                               if((substr(trim($seloptv1),-1)=='-' or substr(trim($seloptv1),-4)=='0000' )and $android!=1)
                              if((substr(trim($seloptv1),0,4)=='****' or substr(trim($seloptv1),-1)=='-' or substr(trim($seloptv1),-4)=='0000' )and $android!=1)
                              {
                                   $seloptv1=str_replace('****','',$seloptv1);
                                   $xoptgroup=true;
                                   $optiong=$input->appendChild($domd_s->createElement('optgroup'));
                                   $optiong->setAttribute('label',''.htmlspecialchars($seloptv2));
                              }
                         }
                         if($ztabla=='ctas' and $zcoddet==1)
                         {if(!ctas_deta($seloptv1)){$xoptgroup=true;}}
                    }
                    
                    $seloptv2=abrevia($seloptv2);
                    
                    $eleoption='option';                    
                    if($xoptgroup)
                    {$optionv=$optiong->appendChild($domd_s->createElement($eleoption,''.htmlspecialchars($seloptv2)));}
                    else
                    {$optionv=$input->appendChild($domd_s->createElement($eleoption,''.htmlspecialchars($seloptv2)));}
                         
//                     if($xoptgroup){$optionv->setAttribute("label",$seloptv2);}
//                     else{$optionv->setAttribute("value",$seloptv1);}
                    {$optionv->setAttribute("value",$seloptv1);}
                    
                    if(!empty($zvalue))
                    {
                         if(strtolower($seloptv1)==strtolower($zvalue)){$optionv->setAttribute("selected","selected");}
                         elseif(mb_stristr(strtolower($seloptv2),strtolower($zvalue))){$optionv->setAttribute("selected","selected");}
                    }
                    
                    if($ztabla=='clor')
                    {
                         $optionv->setAttribute("clor","");
                         $bcolor='white';
                         $optionv->setAttribute("style","background-color:".$bcolor.";");
                    }

               } 
          } 


     $result=$element->appendChild($domd_s->createElement("result",""));   
          $result->setAttribute("id",$zid.'_r');
          $result->setAttribute("style","display:none;");

if(empty($zdom))
{
     if($zxml==1){$zxml=$domd_s->saveXML();}
     else{echo $domd_s->saveXML();}
}     

include ('ele_reset.php');
?>
