<?php require "php/cabecera3.inc";
	
	if(isset($_SESSION['usuario'])){
		echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php?IDS=$IDS'>";
		exit();
	}
	$form="<br><CENTER><h1>Registro</h1><br><br><TABLE><form action='reg.php' method='post'>
	<TR><TD><span style='float:right'>* Email </span></td><td><input type='text' name='email' required='required' maxlength='30' value='$_POST[email]'></TD></TR>
	<TR><TD><span style='float:right'>* Repetir Email </span></td><td><input type='text' name='remail' required='required' maxlength='30' value='$_POST[remail]'></TD></TR>
	<TR><TD><span style='float:right'>* Contraseña </span></td><td><input type='password' name='passw' required='required' maxlength='15' value='$_POST[passw]'></TD></TR>
	<TR><TD><span style='float:right'>* Repetir Contraseña </span></td><td><input type='password' name='rpassw' required='required' maxlength='15' value='$_POST[rpassw]'></TD></TR>	
	<TR><TD><span style='float:right'>* Nombre </span></td><td><input type='text' name='nom' required='required' maxlength='100' value='$_POST[nom]'></TD></TR>	                                  
	<TR><TD><span style='float:right'>* Teléfono </span></td><td><input type='text' name='tel' required='required' maxlength='20' value='$_POST[tel]'></TD></TR>
	<TR><TD><span style='float:right'>* Calle </span></td><td><input type='text' name='calle' required='required' maxlength='50' value='$_POST[calle]'></TD></TR>
	<TR><TD><span style='float:right'>* Número exterior</span></td><td><input type='text' name='next' required='required' maxlength='10' value='$_POST[next]'></TD></TR>
	<TR><TD><span style='float:right'>* Colonia </span></td><td><input type='text' name='col' required='required' maxlength='50' value='$_POST[col]'></TD></TR>
	<TR><TD><span style='float:right'>* Código Postal</span></td><td><input type='text' name='cp' required='required' maxlength='10' value='$_POST[cp]'></TD></TR>
	<TR><TD><span style='float:right'>* Ciudad </span></td><td><input type='text' name='mun' required='required' maxlength='30' value='$_POST[mun]'></TD></TR>
	<TR><TD><span style='float:right'>* Estado</span></td><td><input type='text' name='edo' required='required' maxlength='20' value='$_POST[edo]'></TD></TR>
	<TR><TD><span style='float:right'>* País</span></td><td><input type='text' name='pais' required='required' maxlength='20' value='$_POST[pais]' ></TD></TR>
	<TR><TD><span style='float:right'>RFC </span></td><td><input type='text' name='rfc' maxlength='50' value='$_POST[rfc]'></TD></TR>
	<input type='hidden' name='IDS' value='$IDS'>
	<input type='hidden' name='respuesta' value='1'>
	<TR><TD colspan='2'><CENTER><input type='submit' class='btn' STYLE='float:center;' value='Ok'><CENTER></TD></TR>		
	</form></TABLE></CENTER>";
	
	if(isset($_POST['respuesta'])){
	
		$val=true;
		$msg = "";
		$usec = mysqli_query($conexion,"SELECT count(ID) FROM  ecomm_usuarios where EMAIL='$_POST[email]'");
		$exst=mysqli_fetch_array($usec);
		if($exst[0]>0){$val=false;$msg=$msg."El Email $_POST[email] ya esta registrado.\\n";}
		if((!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))){$val=false;$msg=$msg."Email inválido.\\n";}
		if($_POST['email']!=$_POST['remail']){$val=false;$msg=$msg."El Email de confirmación no coincide.\\n";}
		if((strlen($_POST['passw'])<6)){$val=false;$msg=$msg."La contraseña debe tener por lo menos 6 carácteres.\\n";}
		if($_POST['passw']!=$_POST['rpassw']){$val=false;$msg=$msg."La contraseña de confirmación no coincide.\\n";}
	
		if(!$val){echo "<script>alert('$msg')</script>";echo $form;}
		else{
			
			$ps=sha1($_POST['passw']);
			$in = mysqli_query($conexion,"INSERT INTO ecomm_usuarios VALUES(NULL,'$IDS','$_POST[email]','$ps','$_POST[nom]','-1','0')");
			$in = mysqli_query($conexion,"SELECT LAST_INSERT_ID();");
			$newid = mysqli_fetch_array($in);
			
			$iddest=mysqli_query($conexion,"SELECT MAX(ID) FROM tabla_destinosclientes");
			$m=mysqli_fetch_array($iddest);
			$dest = $m[0]+1;
			
			$usec = mysqli_query($conexion,"SELECT USUARIO_ALTAS FROM  wms_configura_ecom where CVE_CUENTA='$IDS'");
			$usu = mysqli_fetch_array($usec);
			
			$Hora=time();
			$HoraOK=date("H:i:s",$Hora);
			$Fecha=date("Y/m/d");
			
			$r = mysqli_query($conexion,"INSERT INTO tabla_destinosclientes VALUES('$dest','1','$IDS','$newid[0]','$_POST[nom]','$_POST[rfc]','$_POST[calle]','$_POST[next]','$_POST[col]','$_POST[cp]','$_POST[mun]','$_POST[edo]','$_POST[pais]','$_POST[tel]','$_POST[email]','$HoraOK','$Fecha','$usu[0]')");
			
			echo "<script>alert('registro realizado correctamente')</script>";
			$_SESSION['usuario']=$newid[0];
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php?IDS=$IDS'>";
			
			}
	}
	else{
		echo $form;
	}
	
	require "php/pie.inc";
?>