<?php
require_once("../class/class.conectar.php");

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


if(isset($_GET['save'])){
$ugid=$_GET['ugid'];
$dni=$_GET['dni'];
$pass=$_GET['pass'];
$apep=$_GET['apep'];
$apem=$_GET['apem'];
$nom=$_GET['nom'];
$direc=$_GET['direc'];
$iddistr=$_GET['dist'];
$fnac=$_GET['fnac'];
$ecivil=$_GET['ecivil'];
$mail=$_GET['mail'];
$telf1=$_GET['telf1'];
$telf2=$_GET['telf2'];
$cel1=$_GET['cel1'];
$cel2=$_GET['cel2'];
$celo=$_GET['celo'];
$nplan=$_GET['nplan'];
$ncol=$_GET['ncol'];
$nesp=$_GET['nesp'];

$uno=substr($fnac, 0, 2);
$dos=substr($fnac, 3, 2);
$tres=substr($fnac, 6, 4);
$fnac = ($tres."/".$dos."/".$uno);

$sqlupdate="update tb_usuasist set ua_dni='$dni', ua_password='$pass', ua_apepat=upper('$apep'), ua_apemat=upper('$apem'), ua_nom=upper('$nom'), ua_numplanilla='$nplan', ua_telef1='$telf1',";
$sqlupdate=$sqlupdate." ua_telf2='$telf2', ua_direc=upper('$direc'), d_id='$iddistr', ua_cel1='$cel1',ua_cel2='$cel2',ua_celotro='$celo', ua_mail='$mail', ua_fechanac='$fnac', ua_estcivil=upper('$ecivil'), ";
$sqlupdate=$sqlupdate." ua_numcoleg='$ncol', ua_numesp='$nesp' where ug_id='$ugid'";
//echo $sqlupdate;
$resulupdate = mysqli_query($conex,$sqlupdate);
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
echo "<script type='text/javascript'>window.opener.location.reload();window.close();</script>";
}

$ugid = $_GET['ugid'];

$sqlpersonal="select a.ug_id as coduni, a.ua_dni as dni, a.ua_password as psw, a.ua_apepat as apepat, a.ua_apemat as apemat, a.ua_nom as nom, a.ua_numplanilla as numpla, a.ua_codprog as codprog, a.ua_telef1 as telef1,"; 
$sqlpersonal=$sqlpersonal."a.ua_telf2 as telf2, a.ua_cel1 as cel1, a.ua_cel2 as cel2, a.ua_celotro as celo, a.ua_direc as direc, a.d_id, a.ua_mail as mail, date_format(a.ua_fechanac, '%d/%m/%Y') as fechanac, a.ua_estcivil as estcivil, a.ua_numcoleg as numcol, a.ua_numesp as numesp,";
$sqlpersonal=$sqlpersonal."b.ua_estado as estado, (select tc_abrev from tb_tipocont where tc_idcont=b.tc_idcont) as tcabrev, (select c_desc from tb_cargo where c_id=b.c_id) as cargo from tb_usuasist a inner join tb_usudep b on a.ug_id=b.ug_id where a.ug_id='$ugid'";
//$sqlpersonal=$sqlpersonal."inner join tb_cargo c on a.c_id=c.c_id where a.ua_dni='$dni1' and a.ua_password='$password'";
//echo $sqlpersonal;
$resultarea = mysqli_query($conex,$sqlpersonal);
while ($row= mysqli_fetch_array($resultarea)){
$coduni=$row['coduni'];
$dni=$row['dni'];
$pasw=$row['psw'];
$apepat=$row['apepat'];
$apemat=$row['apemat'];
$nom=$row['nom'];
$numpla=$row['numpla'];
$codprog=$row['codprog'];
$telef1=$row['telef1'];
$telef2=$row['telf2'];
$cel1=$row['cel1'];
$cel2=$row['cel2'];
$celo=$row['celo'];
$direc=$row['direc'];
$iddistr=$row['d_id'];
$mail=$row['mail'];
$fechanac=$row['fechanac'];
if($fechanac=="00/00/0000"){
$fechanac="dd/mm/aaaa";
}
$estcivil=$row['estcivil'];
$numcol=$row['numcol'];
$numesp=$row['numesp'];
$estado=$row['estado'];
$tcabrev=$row['tcabrev'];
$cargo=$row['cargo'];
}
?>
<html>
<title>EDITAR DATOS DEL PERSONAL</title>
<head>
<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 validarFNac(){ 
var fnac = document.getElementById('fnac').value;	
	if(fnac != "" )
	{
		if(validaFechaDDMMAAAA(fnac) == false){
		alert("La Fecha No Esta Escrita Correctamente");
		document.getElementById('fnac').value='dd/mm/aaaa';
		document.getElementById('fnac').focus();
		}
	}
}

function LimpiarFNac(){
document.getElementById('fnac').value="";
//document.getElementById('fnac').focus();
}

function validarEmail() {  
var mail = document.getElementById('mail').value;
	if(mail != "" )
	{
		var filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
		if(!filtro.test(mail)){
		alert("El correo No Esta Escrita Correctamente");
		document.getElementById('mail').focus();
		}else{
		mail = mail.toLowerCase(); 
	    document.getElementById('mail').value = mail;
		}
	}

}

function validarTelefono1(){
var telf1 = document.getElementById('telf1').value;
	if(telf1.length != ''){
		if(telf1.length != 7){
			alert("Numero Telefonico Incorrecto");
			document.getElementById('telf1').focus();
		}
	}
}

function validarTelefono2(){
var telf2 = document.getElementById('telf2').value;
	if(telf2.length != ''){
		if(telf2.length != 7){
			alert("Numero Telefonico Incorrecto");
			document.getElementById('telf2').focus();
		}
	}
}

function validarCelular1(){
var cel1 = document.getElementById('cel1').value;
	if(cel1.length != ''){
		if(cel1.length != 9){
			alert("Numero Celular Incorrecto");
			document.getElementById('cel1').focus();
		}
	}
}

function validarCelular2(){
var cel2 = document.getElementById('cel2').value;
	if(cel2.length != ''){
		if(cel2.length != 9){
			alert("Numero Celular Incorrecto");
			document.getElementById('cel2').focus();
		}
	}
}

function validardni(){
var dni = document.getElementById('dni').value;
	if(dni.length != ''){
		if(dni.length != 8){
			alert("DNI incorrrecto");
			document.getElementById('dni').focus();
		}
	}
}

function validarPassword(){
var pasw = document.getElementById('pasw').value;
	if(pasw.length != ''){
		if(pasw.length < 3){
			alert("Password tiene que tener mas de 3 caracteres");
			document.getElementById('pasw').focus();
		}
	}
}

function validarSiNumero(numero){
    if (!/^([0-9])*$/.test(numero))
      return false;
}

function mis_datos(){
var key=window.event.keyCode;
if (key < 48 || key > 57){
window.event.keyCode=0;
}}

</script>
<link rel="stylesheet" type="text/css" href="styles/Style.css">
<script language="JavaScript" type="text/javascript" src="ajax.js"></script>
<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" />
	
</head>
<body>
<form action='' >
<table align='center' class='FacetFormTABLE'>
<tr><td colspan='2'align='center' class='FacetFieldCaptionTD'><b><font color='blue'>DATOS DEL PERSONAL ASISTENCIAL</font></b></td></tr>
<?php
if(isset($_GET['error'])){
$error=$_GET['error'];
if($error =="1"){
echo "<tr><td colspan='2'  align='center'><font color='red'>LOS PASSWORDS NO SON IGUALES</font></td></tr>";
}
}
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";
setlocale(LC_TIME,"spanish"); 


//busca el distrito
$sqldistrito = "SELECT d_id, d_desc FROM tb_distrito order by d_desc asc";
$resultdist = mysqli_query($conex,$sqldistrito);
$cadestado = "<select class='FacetInput' id='dist' name='dist'>";
$cadestado = $cadestado."<option value='0'>Seleccione:</option>";
while ($rowd= mysqli_fetch_array($resultdist)){
$cadestado = $cadestado."<option value='".$rowd['d_id']."'";
if($iddistr==$rowd['d_id']){
$cadestado = $cadestado." selected ";
}
$cadestado = $cadestado." >".$rowd['d_desc']."</option>";
}
$cadestado = $cadestado."</select>";


?>
<br>
<tr><td colspan='2' align='center'>
<table align='center' class='FacetFormTABLE'>
<tr><td class='FacetFieldCaptionTD' width='110'>DNI</td><td width='250' ><input type='text' id='dni' class='FacetInput' maxlength='8' onKeypress='mis_datos()' value='<?php echo $dni;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>PASSWORD</td><td><input type='password' id='pasw' class='FacetInput' onBlur='validarPassword()' maxlength='18' size='30' value='<?php echo $pasw;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>REP PASSWORD</td><td><input type='password' id='rpasw' class='FacetInput' maxlength='18'size='30'></td></tr>
</table>
<br>
<table align='center' class='FacetFormTABLE'>
<tr><td class='FacetFieldCaptionTD'width='110'>APE. PATERNO</td><td width='250'><input type='text' id='apep' class='FacetInput' size='30' value='<?php echo $apepat;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>APE. MATERNO</td><td><input type='text' id='apem'class='FacetInput' size='30' value='<?php echo $apemat;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>NOMBRES</td><td><input type='text' id='nom' class='FacetInput' size='40' value='<?php echo $nom;?>'></td></tr>
</table>
<br>
<table align='center' class='FacetFormTABLE'>
<tr><td class='FacetFieldCaptionTD' width='110'>DIRECCION</td><td width='250'><input type='text' id='direc' size='55' maxlength='90' class='FacetInput' value='<?php echo $direc;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD' width='110'>DISTRITO</td><td width='250'><?php echo $cadestado;?></td></tr>
<tr><td class='FacetFieldCaptionTD'>FECHA DE NAC.</td><td><input type='text' id='fnac' name='fnac' size='20' onBlur='validarFNac()' class='FacetInput' value='<?php echo $fechanac;?>'>

</td></tr>
<tr><td class='FacetFieldCaptionTD'>ESTADO CIVIL</td><td><input type='text' id='ecivil' size='25' class='FacetInput' value='<?php echo $estcivil;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>CORREO</td><td><input type='text' id='mail' size='45' class='FacetInput' onBlur='validarEmail()' value='<?php echo $mail;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>TELEFONO 1</td><td><input type='text' id='telf1' size='20' onKeypress='mis_datos()' onBlur='validarTelefono1()' maxlength='7' class='FacetInput' value='<?php echo $telef1;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>TELEFONO 2</td><td><input type='text' id='telf2' size='20' onKeypress='mis_datos()' onBlur='validarTelefono2()' maxlength='7' class='FacetInput' value='<?php echo $telef2;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>CELULAR 1</td><td><input type='text' id='cel1' size='20' onKeypress='mis_datos()' onBlur='validarCelular1()' maxlength='9' class='FacetInput' value='<?php echo $cel1;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>CELULAR 2</td><td><input type='text' id='cel2' size='20' onKeypress='mis_datos()' onBlur='validarCelular2()' maxlength='9' class='FacetInput' value='<?php echo $cel2;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>OTRO CEL</td><td><input type='text' id='ocel' size='20' class='FacetInput' value='<?php echo $celo;?>'></td></tr>
</table><br>
<table align='center' class='FacetFormTABLE'>
<tr><td class='FacetFieldCaptionTD' width='110'>NUM PLANILLA</td><td width='250'><input type='text' id='nplan' size='20' class='FacetInput' maxlength='8' value='<?php echo $numpla;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>NUM COLEGIATURA</td><td><input type='text' id='ncol' size='20' class='FacetInput' maxlength='8' value='<?php echo $numcol;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>NUM ESPECIALIDAD</td><td><input type='text' id='nesp' size='20' class='FacetInput' maxlength='8' value='<?php echo $numesp;?>'></td></tr>
<tr><td class='FacetFieldCaptionTD'>TIPO DE CONTRATO</td><td><?php echo $tcabrev;?>'</td></tr>
</table>
</td></tr>
<tr><td align='center'><input type='button' value='Grabar'id='save' onCLick='GrabardatosPersonal()' class='FacetButton'></td><td align='center'><input type='button' value='Cancelar' onCLick='ventanaClose()' id='cancel' class='FacetButton'></td></tr>
</table>
</form>
</body>
</html>