<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/

session_name('portal_absa');
session_start();

require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();

$optionsCol = explode(";;",$_POST['optionsForm']);
$select="";
for($i = 0; $i < count($optionsCol); $i++){
	$optionCol = explode("|",$optionsCol[$i]);
	$optNameCol[$i]["name"] = trim($optionCol[0]);
	$optNameCol[$i]["key"] = trim($optionCol[1]);
	$optNameCol[$i]["label"] = trim($optionCol[2]);
	$optNameCol[$i]["options"] = trim($optionCol[3]);
	$optNameCol[$i]["input"] = trim($optionCol[4]);
	
	if($select == "")
		$select=$optionCol[0];
	else
		$select.=",".$optionCol[0];
}

//--------------------------Genero query para seleccionar Registro----------
if($_POST['oper'] == 'edit'){
	$valkeys = explode("|",$_POST['valKeys']);
	$keys = explode("|",$_POST['keys']);
	$sql="select $select from ".$_POST['tabla'];
	$where="";
	for($i = 0; $i < count($keys); $i++){
		if($where == ""){
			$where=" where ".$keys[$i]."='".$valkeys[$i]."'";
			$whereValida=$keys[$i].":".$valkeys[$i];
		}else{
			$where.=" and ".$keys[$i]."='".$valkeys[$i]."'";
			$whereValida.=",".$keys[$i].":".$valkeys[$i];
		}
	}
	
	$rquery = mysqli_query($conexion->CONEXION,$sql.$where);
	
	if(mysqli_num_rows($rquery) > 0){
		$datos = mysqli_fetch_array($rquery,MYSQLI_ASSOC);
	}
}

//--------------------------Fin Seleccion Registro-------------------------

//--------------------------Seleccion de atributos de fila-----------------

$_SESSION['query'] = str_replace("\\","",$_SESSION['query']);
$result = mysqli_query($conexion->CONEXION,$_SESSION['query']);

for ($i = 0; $i < mysqli_num_fields($result); $i++)
 {
		if(mysql_field_type($result,$i)=="string"){
			$t = (mysql_field_len($result, $i)/3);
		}else{
			$t = mysql_field_len($result, $i);
		}
		if($t < 50){
			$size=$t;
		}else{
			$size=50;
		}
		$Fields[$i]['name']=mysql_field_name($result, $i);
		$Fields[$i]['maxLenght']=$t;
		$Fields[$i]['size']=$size;
 }
//--------------------------Fin Seleccion de atributos de fila------------- 
?>
<script language="javascript" src="libs/Vanadium/vanadium.js"></script>
<script>
$j_3(document).ready(function() {
	$j_3('#loadingForm').hide();
    $j_3().ajaxStart(function() {
        $j_3('#loadingForm').show();
        $j_3('#resultForm').hide();
    }).ajaxStop(function() {
        $j_3('#loadingForm').hide();
        $j_3('#resultForm').fadeIn('slow');
    });
	
	/*$j_3('#form, #fat, #formDatos').submit(function() {
        $j_3.ajax({
            type: 'POST',
            url: $j_3(this).attr('action'),
            data: $j_3(this).serialize(),
            success: function(data) {
					$j_3('#resultForm').html(data);
            }
        })
        return false;
    });*/
	
	var opciones= {
                               //beforeSubmit: validaAdjuntos, //funcion que se ejecuta antes de enviar el form
                               success: mostrarRespuesta //funcion que se ejecuta una vez enviado el formulario
            };
		
		$j_3('#formDatos').ajaxForm(opciones);
		
		function mostrarRespuesta (data){
			$j_3('#resultForm').html(data);
		};
});
</script>
<table width="100%">
	<tr>
		<td  width="95%" align="right"><div id="resultForm"></div></td>
		<td><div id="loadingForm"><img src="Imagenes/loading.gif" width="28" height="25"></div></td>
	</tr>
</table>
<FORM ACTION="grids/guardaDatos.php" method="POST" id="formDatos" name="formDatos">
  <FIELDSET>
    <LEGEND><?=$_POST['captionForm']?></LEGEND>
	<INPUT TYPE="hidden" NAME="campos" id="campos" value="<?=str_replace("\\","",str_replace("\"","'",$_POST['optionsForm']))?>">
	<INPUT TYPE="hidden" NAME="oper" id="oper" value="<?=$_POST['oper']?>">
	<INPUT TYPE="hidden" NAME="tabla" id="tabla" value="<?=$_POST['tabla']?>">
	<INPUT TYPE="hidden" NAME="valkeys" id="valkeys" value="<?=$_POST['valKeys']?>">
	<INPUT TYPE="hidden" NAME="keys" id="keys" value="<?=$_POST['keys']?>">
	<INPUT TYPE="hidden" NAME="reloadGridAfterSave" id="reloadGridAfterSave" value="<?=$_POST['reloadGridAfterSave']?>">
	<INPUT TYPE="hidden" NAME="valLiga" id="valLiga" value="<?=$_GET['valLiga']?>">
    <TABLE>
		<?
			for($i = 0; $i < count($Fields); $i++){
				$label="";
				$options="";
				$ban=false;
				for($j = 0; $j < count($optionsCol); $j++){
					if($optNameCol[$j]["name"] == trim($Fields[$i]['name'])){
						$label = $optNameCol[$j]["label"];
						$options = $optNameCol[$j]["options"];
						$options = str_replace("[VALENBD]",$datos[$Fields[$i]["name"]],str_replace("[WHERE-FORM]",$whereValida,$options));
						$input = $optNameCol[$j]["input"];
						$ban=true;
						break;
					}
				}
				if($ban){
				$busca = preg_quote('hidden');
				if(!eregi($busca,$options)){
					$inputType=explode("{",$input);
			?>
				  <TR>
					<TD align="left">
					<?if(strtoupper(trim($inputType[0])) == "PASSWORD" && $_POST['oper'] == 'edit'){ $label=str_replace("*","",$label);}?>
					  <LABEL FOR="name"><?if($label != ""){echo $label;}else{echo $Fields[$i]["name"];}?>:</LABEL>
					</TD>
					<TD align="left">
					<?
					if($input != ""){
						if(strtoupper(trim($inputType[0])) == "SELECT"){
							$inputType[1] = str_replace("}","",$inputType[1]);
							$buscaQuery = preg_quote("QUERY:");
							if(eregi($buscaQuery,strtoupper($inputType[1]))){
								$inputTypeDiv = explode(":",$inputType[1]);
								$sql=$inputTypeDiv[1];
								
								$rquery = mysqli_query($conexion->CONEXION,$sql);
								
								for($t = 0; $t < mysqli_num_fields($rquery); $t++){
									$val[$t]['name'] = mysql_field_name($rquery,$t);
								}
								?>	<Select <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>">
								<?
								if(mysqli_num_rows($rquery) > 0){
									while($tmp = mysqli_fetch_array($rquery)){
									?>
										<option value="<?=$tmp[$val[0]['name']]?>" <?if($opt[0] == $datos[$Fields[$i]["name"]]){echo "selected";}?>><?=$tmp[$val[1]['name']]?></option>
									<?
									}
								}
								?>
									</select>
								<?
							}else{
								?>
								<Select <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>">
								<?
								$inputTypeDiv = explode(",",$inputType[1]);
								for($t = 0; $t < count($inputTypeDiv); $t++){
									$opt = explode(":",$inputTypeDiv[$t]);
							?>		
									<option value="<?=$opt[0]?>" <?if($opt[0] == $datos[$Fields[$i]["name"]]){echo "selected";}?>><?=$opt[1]?></option>
							<?
								}
							?>	
								</select>
							<?
							}
						}else if(strtoupper(trim($inputType[0])) == "TEXTAREA"){
						?>
							<textarea <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>"><?=$datos[$Fields[$i]["name"]]?></textarea>
						<?
						}else if(strtoupper(trim($inputType[0])) == "CHECKBOX"){
							$inputType[1] = str_replace("}","",$inputType[1]);
							$inputTypeDiv = explode(",",$inputType[1]);
							$valCheck="";
							for($t = 0; $t < count($inputTypeDiv); $t++){
								$opt = explode(":",$inputTypeDiv[$t]);
								$opt[0] = trim($opt[0]);
								$opt[1] = trim($opt[1]);
								if(strtoupper($opt[0]) == "TRUE" && $datos[$Fields[$i]["name"]] == strtoupper($opt[1])){
									$check="checked";
								}
								if(strtoupper($opt[0]) == "DEFAULT" && strtoupper($opt[1]) == "TRUE" && $_POST['oper'] == 'add'){
									$check="checked";
								}
								if(strtoupper($opt[0]) == "TRUE"){
										$valCheck=$opt[1];
								}
							}
						?>
							<input type="checkbox" value="<?=$valCheck?>" <?=$check?> <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>" />
						<?
						}else if(strtoupper(trim($inputType[0])) == "FILE"){
							if(trim($datos[$Fields[$i]["name"]]) != ""){
						?>
							<input type="text" value="<?=$datos[$Fields[$i]["name"]]?>" readOnly="true" name="upload_<?=$Fields[$i]["name"]?>" id="upload_<?=$Fields[$i]["name"]?>"/><input type="file" <?=str_replace(":","",str_replace("\\","",str_replace("\"","",$options)))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>" />
						<?						
							}else{
						?>
							
							<input type="file" <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>" />
						<?
							}
						}else if(strtoupper(trim($inputType[0])) == "PASSWORD"){
							if($_POST['oper'] == 'edit'){
								$options=str_replace("required","",$options);
							}
						?>
							<input type="hidden" value="<?=$datos[$Fields[$i]["name"]]?>" name="password_<?=$Fields[$i]["name"]?>" id="password_<?=$Fields[$i]["name"]?>"/><input type="password" <?=str_replace("\\","",str_replace("\"","",$options))?> name="<?=$Fields[$i]["name"]?>" id="<?=$Fields[$i]["name"]?>" />
						<?
						}
					}else{
						$buscaValue=preg_quote("value");
						if(eregi($buscaValue,$options)){
					   ?>
					   <INPUT <?=str_replace("\\","",str_replace("\"","",$options))?> TYPE="text" SIZE="<?=$Fields[$i]["size"]?>" MAXLENGTH="<?=$Fields[$i]["maxLenght"]?>" NAME="<?=$Fields[$i]["name"]?>" ID="<?=$Fields[$i]["name"]?>">
						<?}else{?>
					  <INPUT VALUE="<?=$datos[$Fields[$i]["name"]]?>" <?=str_replace("\\","",str_replace("\"","",$options))?> TYPE="text" SIZE="<?=$Fields[$i]["size"]?>" MAXLENGTH="<?=$Fields[$i]["maxLenght"]?>" NAME="<?=$Fields[$i]["name"]?>" ID="<?=$Fields[$i]["name"]?>">
					<?}
					}?>
					</TD>
				  </TR>
				<?}else{
					$buscaValue=preg_quote("value");
					if(eregi($buscaValue,$options)){
						$attr=str_replace("\\","",str_replace("\"","",$options));
					}else{
					$attr=str_replace("\\","",str_replace("\"","",$options))." VALUE=\"".$datos[$Fields[$i]["name"]]."\"";
					}
				?>
					<TR>
					<TD><INPUT <?=$attr?> NAME="<?=$Fields[$i]["name"]?>" ID="<?=$Fields[$i]["name"]?>"></TD>
					</TR>
				<?}
			}//ban
			}//for?>
    </TABLE>
  </FIELDSET>
  <P>
    <INPUT TYPE="submit" VALUE="GUARDAR" class="estilo_btn" />
  </P>
</FORM>