<?php
	//-------------------------
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	$StrMen=V($_POST['CboMen']);
	$StrNom=V($_POST['TxtNom']);
	$StrUrl=V($_POST['TxtUrl']);
	$StrOrd=V($_POST['TxtOrd']);
	$StrNl=V($_POST['TxtNl']);
	//ACTIVAR DESDE OTRA PAGINA
	if(strlen($StrId)==0){
		$StrId=V($_GET['Id']);
		if(is_numeric($StrId)==false){$StrId="";
		}else{
			$StrCmd="AR";$LStrPag=V($_GET['Pag']);if(!is_numeric($LStrPag)){$LStrPag="";
		}}}
	//FUNCIONES DE DESZPLAZAMIENTO
	if(strlen($LStrPag)==0)	$LStrPag="0";
	if($StrCmd=="MSig")$LStrPag=($LStrPag+1);
	if($StrCmd=="MAnt")	$LStrPag=($LStrPag-1);
	if($StrCmd=="MPri")	$LStrPag=0;
	if($StrCmd=="MUlt")	$LStrPag=(floor($LStrTR / $VP_RegXPag) );
	//FUNCION PARA ACTIVAR UN REGISTRO
	if(strlen($StrId)>0 && $StrCmd=="AR"){
		$Id=$StrId;
		$StrMen=gs("MN","vmenus","ID=$Id");		
		$StrNom=gs("SN","vmenus","ID=$Id");		
		$StrUrl=gs("SU","vmenus","ID=$Id");		
		$StrOrd=gs("SO","vmenus","ID=$Id");		
		$StrNl=gs("SL","vmenus","ID=$Id");		
		if($StrOrd==0){
			$StrOrd=gn("max(SO)","vmenus","MN='$StrMen'") +1;
		}
		$LStrFoc="document.getElementById('CboMen').focus();";		
		$StrGT="S";
	}	
	if($StrCmd=="G" || $StrCmd=="M"){
		$StrMen_=gn("id_menu","menu","nombre='$StrMen'");
	}
	//FUNCIONES DE ACTUALIZACION
	if($StrCmd=="G"){
		if(gn("count(*)","menus","sm='$StrNom'")>0){
			$LStrMsg="Nombre del submenu ya registrado";
			$LStrFoc="document.getElementById('TxtNom').focus();";			
			$StrGT="S";
		}
		if(strlen($StrOrd)==0) $StrOrd=0;
		if(strlen($StrNl)==0) $StrNl=0;
		
		if(is_numeric($StrOrd)==false) $StrOrd=0;
		if(is_numeric($StrNl)==false) $StrNl=0;

		if(strlen($StrGT)==0){
			$Id=GenId("id","menus");
			$s="INSERT INTO menus VALUES ".
			"($Id,$StrMen_,'$StrNom','$StrUrl',$StrOrd,$StrNl,0)";
			ejt($s);
			$LStrMsg="Datos registrados";
			$LStrFoc="document.getElementById('CboMen').focus();";
			$StrGT="S";
			$StrId="";
			$StrNom="";
			$StrUrl="";
			$StrOrd="";
			$StrNl="";
		}
	}
	if($StrCmd=="M"){
		if(gn("count(*)","menus","sm='$StrNom' AND id<>$StrId")>0){
			$LStrMsg="Nombre del submenu ya registrado";
			$LStrFoc="document.getElementById('TxtNom').focus();";			
			$StrGT="S";
		}
		if(strlen($StrOrd)==0) $StrOrd=0;
		if(strlen($StrNl)==0) $StrNl=0;
		
		if(is_numeric($StrOrd)==false) $StrOrd=0;
		if(is_numeric($StrNl)==false) $StrNl=0;
		
		if(strlen($StrGT)==0){
			$s="UPDATE menus SET id_menu=$StrMen_,sm='$StrNom',url='$StrUrl',orden=$StrOrd,
				nl=$StrNl WHERE id=$StrId";	
			ejt($s);
			$LStrMsg="Datos actualizados";
			$LStrFoc="document.getElementById('CboMen').focus();";			
			$StrGT="S";
			$StrId="";
			$StrNom="";
			$StrUrl="";
			$StrOrd="";
			$StrNl="";
		}
	}
	if($StrCmd=="E"){
		if(strlen($StrGT)==0){
			$s="DELETE FROM menus WHERE id=$StrId";	
			ejt($s);
			$LStrMsg="Registro Eliminado";
			$LStrFoc="document.getElementById('CboMen').focus();";
			$StrGT="S";
			$StrId="";
			$StrNom="";
			$StrUrl="";
			$StrOrd="";
			$StrNl="";
		}		
	}
	//////////////////////////////////
	if($StrGT=="S"){
		if(strlen($LStrMsg)>0){
			if(strlen($LStrFoc)==0){
				$LStrMsg="onload=\"alert('$LStrMsg');fVistaPrevia();\"";
			}else{
				$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;fVistaPrevia();\"";
			}
		}else{
			if(strlen($LStrFoc)>0){
				$LStrMsg="onload=\"$LStrFoc;fVistaPrevia();\"";
			}
		}
	}else{
		$LStrFoc="document.getElementById('CboMen').focus();";
		$LStrMsg="onload=\"$LStrFoc;fVistaPrevia();\"";
	}

?>