<?php
require_once("../class/class.conectar.php");
require_once("../class/class.usudep.php");
require_once("../class/class.tipopersonal.php");
require_once("../class/class.tipocontrato.php");
require_once("../class/class.especialidad.php");
require_once("../class/class.cargo.php");
require_once("../class/class.personal.php");

$Objcon = new Conectar();
$conex = $Objcon->con();

$idj = $_GET['idj'];
$ids = $_GET['ids'];

if(isset($_GET['ugid'])){
$ugid=$_GET['ugid'];
$Objp = new Personal();
$res_p = array();
$res_p = $Objp->buscar_datos_per_campo($conex,$ugid);
$sumap=0;
$apepat=$res_p[0]['apepat'];
$apemat=$res_p[0]['apemat'];
$nom=$res_p[0]['nom'];
$idesp=$res_p[0]['idesp'];
$idcont=$res_p[0]['idcont'];
$idc=$res_p[0]['idc'];
$idtp=$res_p[0]['idtp'];
$fechaing=$res_p[0]['fechaing'];
}

if(isset($_GET['tmov'])){
//echo $_POST['depa'];
$ugid=$_GET['ugid'];

	if($ugid!=''){
	$apepat = $_GET['apepat'];
	$apemat = $_GET['apemat'];
	$nom = $_GET['nom'];
	$tipocontrato = $_GET['tipocont'];
	$especialidad = $_GET['especialidad'];
	$idc = $_GET['cargo'];
	$idtp = $_GET['tipopersonal'];
	$fecha = $_GET['fecha'];
	$uno=substr($fecha, 0, 2);
	$dos=substr($fecha, 3, 2);
	$tres=substr($fecha, 6, 4);
	$fecha = ($tres."-".$dos."-".$uno);

	$Objp=new UsuarioDependencia();
	$res_ut=array();
	$res_ut=$Objp->listar_det_trab_per($conex,$ugid);          
	$udid=$res_ut[0]['id'];
	$Objp->modificar_datos_dep_per($conex,$ugid,$udid,$idtp,$tipocontrato,$especialidad,$fecha,$idc);
	
	$Objpp = new Personal();
	$Objpp->modifcar_datos_per($conex,$ugid,$apepat,$apemat,$nom);
	echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
	}
}else{
//$error=0;
}


//seleccionando al combo tipo de personal
$Objtp = new TipoPersonal();
$res_tp = array();
$res_tp = $Objtp->buscar_tipoper_dep($conex,$idj,$ids);
$cadenatp="<select id='tipopersonal' name='tipopersonal'>";
$cadenatp=$cadenatp."<option value='0'>Seleccione</option>";
for ($i=0;$i<count($res_tp);$i++){
$cadenatp=$cadenatp."<option value='".$res_tp[$i]['id']."'";
if($idtp==$res_tp[$i]['id']){
$cadenatp=$cadenatp." selected ";
}
$cadenatp=$cadenatp." >".$res_tp[$i]['nom']."</option>";
}
$cadenatp=$cadenatp."</select>";


//seleccionando al comnbo especialidad
$Obje = new Especialidad();
$res_e = array();
$res_e = $Obje->listar_especialidad($conex,$idj,$ids);
$cadenaesp="<select id='especialidad' name='especialidad'>";
$cadenaesp=$cadenaesp."<option value='0'>Seleccione</option>";
for ($i=0;$i<count($res_e);$i++){
$cadenaesp=$cadenaesp."<option value='".$res_e[$i]['cod']."'";
if($idesp==$res_e[$i]['cod']){
$cadenaesp=$cadenaesp." selected ";
}
$cadenaesp=$cadenaesp.">".$res_e[$i]['descripcion']."</option>";
}
$cadenaesp=$cadenaesp."</select>";

//seleccionando al comnbo tipo de contrato
$Objtc = new TipoContrato();
$res_tc = array();
$res_tc = $Objtc->listar_tp_cont($conex);
$cadenatc="<select id='tipocont' name='tipocont'>";
$cadenatc=$cadenatc."<option value='0'>Seleccione</option>";
for ($i=0;$i<count($res_tc);$i++){
$cadenatc=$cadenatc."<option value='".$res_tc[$i]['cod']."'";
if($idcont==$res_tc[$i]['cod']){
$cadenatc=$cadenatc." selected ";
}
$cadenatc=$cadenatc.">".$res_tc[$i]['descripcion']."</option>";
}
$cadenatc=$cadenatc."</select>";

//seleccionar el tipo de cargo
$Objc = new Cargo();
$res_c = array();
$res_c = $Objc->listar_cargo($conex);
$cadenac="<select id='cargo' name='cargo'>";
$cadenac=$cadenac."<option value='0'>Seleccione</option>";
for ($i=0;$i<count($res_c);$i++){
$cadenac=$cadenac."<option value='".$res_c[$i]['id']."'";
if($idc==$res_c[$i]['id']){
$cadenac=$cadenac." selected ";
}
$cadenac=$cadenac.">".$res_c[$i]['nom']."</option>";
}
$cadenac=$cadenac."</select>";
?>
<html>
<head>
<title>MODIFICAR DATOS DEL PERSONAL ASISTENCIAL</title>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<link type="text/css" rel="stylesheet" href="src/css/jscal2.css" />
    <link type="text/css" rel="stylesheet" href="src/css/border-radius.css" />
    <!-- <link type="text/css" rel="stylesheet" href="src/css/reduce-spacing.css" /> -->

    <link id="skin-win2k" title="Win 2K" type="text/css" rel="alternate stylesheet" href="src/css/win2k/win2k.css" />
    <link id="skin-steel" title="Steel" type="text/css" rel="alternate stylesheet" href="src/css/steel/steel.css" />
    <link id="skin-gold" title="Gold" type="text/css" rel="alternate stylesheet" href="src/css/gold/gold.css" />
    <link id="skin-matrix" title="Matrix" type="text/css" rel="alternate stylesheet" href="src/css/matrix/matrix.css" />

    <link id="skinhelper-compact" type="text/css" rel="alternate stylesheet" href="src/css/reduce-spacing.css" />

    <script src="src/js/jscal2.js"></script>
	<script src="src/js/lang/en.js"></script>
    <!-- you actually only need to load one of these; we put them all here for demo purposes -->
     <script src="src/js/lang/es.js"></script>
    
    <link type="text/css" rel="stylesheet" href="demopage.css" />

<script type="text/javascript"> 

function ventanaClose(){ 
   //alert("hola mundo");
    window.close();
} 

function validaFechaDDMMAAAA(fecha){    
var dtCh= "/";    
var minYear=1900;
var maxYear=2100;
        function isInteger(s){
        var i;     
		for (i = 0; i < s.length; i++){ 
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
        }
        return true;
		}
		function stripCharsInBag(s, bag){
        var i;
        var returnString = ""; 
		for (i = 0; i < s.length; i++){ 
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;        }
        return returnString;    }
		function daysInFebruary (year){
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );    }
		function DaysArray(n) {
        for (var i = 1; i <= n; i++) { 
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}        
		} 
		return this
		}    
		function isDate(dtStr){
        var daysInMonth = DaysArray(12) 
		var pos1=dtStr.indexOf(dtCh) 
		var pos2=dtStr.indexOf(dtCh,pos1+1) 
		var strDay=dtStr.substring(0,pos1)
        var strMonth=dtStr.substring(pos1+1,pos2)
        var strYear=dtStr.substring(pos2+1)
        strYr=strYear
        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)        
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)        
		for (var i = 1; i <= 3; i++) {            
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
        }        
		month=parseInt(strMonth)        
		day=parseInt(strDay)        
		year=parseInt(strYr)        
		if (pos1==-1 || pos2==-1){            
		return false        }        
		if (strMonth.length<1 || month<1 || month>12){            
		return false        }       
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{            return false        }        
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){ 
		return false        }        
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		{            return false        }        
		return true    }    
		if(isDate(fecha))
		{        return true;    }
		else{        return false;    }
}

function ModificarPer(){
	var idj = document.getElementById("depa").value;
	var ids = document.getElementById("serv").value;
	var ugid = document.getElementById("ugid").value;
	
	var apepat = document.getElementById("apepat").value;
	if(apepat == ''){
		alert("Apellido Paterno no Ingresado");
		return false;
	}
	var apemat = document.getElementById("apemat").value;
	if(apemat == ''){
		alert("Apellido Materno no Ingresado");
		return false;
	}
	
	var nom = document.getElementById("nom").value;
	if(nom == ''){
		alert("Nombre no Ingresado");
		return false;
	}
	
	var fecha = document.getElementById("fecha").value;
	if(fecha == ''){
		alert("Fecha no Ingresado");
		return false;
	}
	
	if(validaFechaDDMMAAAA(fecha) == false){
		alert("La Fecha No Esta Escrita Correctamente");
		return false;
	}
	
	var tipocont = document.getElementById("tipocont").value;
	if(tipocont == 0){
		alert("Selecciones Tipo de Contrato");
		return false;
	}
	
	var especialidad = document.getElementById("especialidad").value;
	if(especialidad == 0){
		alert("Selecciones Especialiadad");
		return false;
	}
	
	var tipopersonal = document.getElementById("tipopersonal").value;
	if(tipopersonal == 0){
		alert("Selecciones Tipo de Personal");
		return false;
	}
	
	var cargo = document.getElementById("cargo").value;
	if(cargo == 0){
		alert("Selecciones Cargo");
		return false;
	}
	
	document.location= "modificarpersonal.php?idj="+idj+"&ids="+ids+"&ugid="+ugid+"&apepat="+apepat+"&apemat="+apemat+"&nom="+nom+"&fecha="+fecha+"&tipocont="+tipocont+"&especialidad="+especialidad+"&tipopersonal="+tipopersonal+"&cargo="+cargo+"&tmov=1";
}

</script> 
</head>
<body>
<table align='center'>
<tr><td>
<div id='editpersonal'>
<?php
echo "<input type='hidden' id='depa' name='depa' value='".$idj."'>";
echo "<input type='hidden' id='serv' name='serv' value='".$ids."'>";
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
?>
<fieldset>
	<legend align='left'><font color='blue' size='15'>Modificar Datos del Personal Asistencial </font></legend>
	<br>
		<table align='center' class='FacetFormTABLE' width='350'>
		<tr><td class='FacetFieldCaptionTDT'colspan='4'><center>MODIFICAR DATOS DEL PERSONAL</center></td></tr>
		<tr><td colspan='4'><center><?php if(isset($error)){ if($error=='0'){ echo "REVISE LOS DATOS, PERSONAL NO REGISTRADO";}elseif($error=='1'){ echo "PERSONAL REGISTRADO";}}?></center></td></tr>
		<tr><td align='right' colspan='1' class='FacetFieldCaptionTD'>APE. PATERNO</td><td colspan='2'><input type='text' id='apepat' name='apepat' size='40' value='<?php if(isset($_GET['ugid'])){ echo $apepat;}?>'></td><td rowspan='3'>
		
		</td></tr>
		<tr><td align='right' colspan='1' class='FacetFieldCaptionTD'>APE. MATERNO</td><td  colspan='2'><input type='text' id='apemat' name='apemat' size='40' value='<?php if(isset($_GET['ugid'])){ echo $apemat;}?>'></td></tr>
		<tr><td align='right' colspan='1' class='FacetFieldCaptionTD'>NOMBRES</td><td colspan='2'><input type='text' id='nom' name='nom' size='40' value='<?php if(isset($_GET['ugid'])){ echo $nom;}?>'></td></tr>
		<tr><td align='center'>
		<tr><td align='right' class='FacetFieldCaptionTD'>FECHA DE INGRESO</td><td align='left' colspan='4'>
		<input type='text' id='fecha' name='fecha' size='15' value='<?php if(isset($_GET['ugid'])){ echo $fechaing;}?>'>
		<button id="f_rangeStart_trigger"><img src="../img/b_calendar.png" ></button>
                <button id="f_clearRangeStart" onclick="clearRangeStart()">clear</button>
                <script type="text/javascript">
				  var LEFT_CAL = Calendar.setup({
                    //cont: "cont",
                    //weekNumbers: true,
                    //selectionType: Calendar.SEL_MULTIPLE,
                    //showTime: 12
                    // titleFormat: "%B %Y"
					})
                  new Calendar({
                          inputField: "fecha",
                          dateFormat: "%d/%m/%Y",
                          trigger: "f_rangeStart_trigger",
                          bottomBar: false,
                          onSelect: function() {
                                  var date = Calendar.intToDate(this.selection.get());
								  LEFT_CAL.setLanguage('es');
                                  LEFT_CAL.args.min = date;
                                  LEFT_CAL.redraw();
                                  this.hide();
                          }
                  });
                  function clearRangeStart() {
                          document.getElementById("fecha").value = "";
                          LEFT_CAL.args.min = null;
                          LEFT_CAL.redraw();
                  };
                </script>
		</td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>TIPO PERSONAL</td>	
		<td align='left'><?php echo $cadenatp;  ?></td>
		<td align='right' class='FacetFieldCaptionTD'>ESPECIALIDAD</td>
		<td align='left'><?php echo $cadenaesp;?></td></tr>
		<tr><td align='right' class='FacetFieldCaptionTD'>CARGO</td>
		<td align='left'><?php echo $cadenac; ?></td>
		<td align='right' class='FacetFieldCaptionTD'>TIPO CONTRATO</td>
		<td align='left'><?php echo $cadenatc; ?></td></tr>
		<tr><td align='center' colspan='2'><input type='button' id='pprog' style='width:150px' class='FacetButton'  value='Guardar Cambios' onClick='ModificarPer()'>
		</td>
		<td align='center' colspan='2'><button type='button' id='pprog' style='width:150px' class='FacetButton' onClick='ventanaClose();' >Cancelar</button>
		</td></tr></table>
	  <br>
	</fieldset>
</div>
</td><tr>
</table>
</body>
</html>