<?php
	$list = [
		"register_username","register_password","register_password_confirm","register_email","register_delete","g-recaptcha-response"
	];
	$continue = true;
	for ($i = 0; $i < sizeof($list); $i++){
		if (empty($_POST[$list[$i]])){
			$continue = false;
			break;
		}
	}
	if ($continue){
		if (!REG_ACTIVE){
			echo '<script>SendNotification("error", "El registro está desactivado.", "check");</script>';
			return;
		}
		$user = GetConexion()->real_escape_string($_POST["register_username"]);
		$email = GetConexion()->real_escape_string($_POST["register_email"]);
		$password = GetConexion()->real_escape_string($_POST["register_password"]);
		$password_confirm = GetConexion()->real_escape_string($_POST["register_password_confirm"]);
		$delete_code = GetConexion()->real_escape_string($_POST["register_delete"]);
		if (strlen($user) < 3 || strlen($user) > 16){
			echo '<script>SendNotification("'.$lang["register1"].'", "warning", "check");</script>';
			return;
		}
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
			echo '<script>SendNotification("'.$lang["register2"].'", "warning", "check");</script>';
			return;
		}
		if ($password != $password_confirm){
			echo '<script>SendNotification("'.$lang["register3"].'", "warning", "check");</script>';
			return;
		}
		$status = "OK";
		$coins = 0;
		$query = "INSERT INTO account.account (login,password,email,social_id,create_time,status,coins)
				  VALUES ('".$user."',PASSWORD('".$password."'),'".$email."','".$delete_code."',NOW(),'".$status."','".$coins."')";

		if (mysqli_query(GetConexion(), $query)){
			echo '<script>SendNotification("'.$lang["register4"].'", "success", "check");</script>';
		}
		else{
			echo '<script>SendNotification("'.$lang["register5"].'", "error","check");</script>';
		}
	}
?>
<script>
	function ValidateCheck(input){
		var x = document.getElementById("register_"+input.toString()).checked;
		document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/ajax_small.gif'/>";
		if (x){
			document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/accept.png'/>";
		}
		else{
			document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/exclamation.png'/>";
		}
	}
    function Validate(input){
        var text = document.getElementById("register_"+input.toString()).value;
        document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/ajax_small.gif'/>";
        $.post("scripts/validate.php", {type:input,text:text}, function(data){
			$("#"+input.toString()+"_error").html(data);
		});
    }
    function ValidatePwd(input){
        var text = document.getElementById("register_"+input.toString()).value;
        if (input == "delete"){
            if (text.length == 7){
                document.getElementById("register_delete_error").innerHTML = "<img src='style/imgs/accept.png'>";
            }
            else{
                document.getElementById("register_delete_error").innerHTML = "<img src='style/imgs/exclamation.png'>";
            }
        }
        else{
            var pwd1 = document.getElementById("register_password").value;
            var pwd2 = document.getElementById("register_password_confirm").value;
            document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/ajax_small.gif'/>";
            if (text.length <= 5){
                document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/exclamation.png'>";
            }
            else{
                document.getElementById(input.toString()+"_error").innerHTML = "<img src='style/imgs/accept.png'>";
            }
            if (pwd1 != pwd2){
                document.getElementById("password_confirm_error").innerHTML = "<img src='style/imgs/exclamation.png'>";
            }
            else{
                document.getElementById("password_confirm_error").innerHTML = "<img src='style/imgs/accept.png'>";
            }
        }
	}
</script>
<div class="news">
	<div class="article">
		<h2><?php echo $lang["register13"]; ?></h2>
		<center>
		<form action="register/" method="post">
			<table style="width:100%">
				<tr>
					<th style="width:30%"><?php echo $lang["register6"]; ?></th>
					<th style="width:60%"><input class="form_input" type="text" id="register_username" name="register_username" placeholder="<?php echo $lang["register6"]; ?>" onchange="Validate('username');" required></th>
					<th><span id="username_error"><img src='style/imgs/exclamation.png'></span></th>
				</tr>
				<tr>
					<th><?php echo $lang["register7"]; ?></th>
					<th><input class="form_input" type="password" id="register_password" name="register_password" placeholder="<?php echo $lang["register7"]; ?>" onchange="ValidatePwd('password');" required></th>
					<th><span id="password_error"><img src='style/imgs/exclamation.png'></span></th>
				</tr>
				<tr>
					<th><?php echo $lang["register8"]; ?></th>
					<th><input class="form_input" type="password" id="register_password_confirm" name="register_password_confirm" onchange="ValidatePwd('password_confirm');" placeholder="<?php echo $lang["register7"]; ?>" required></th>
					<th><span id="password_confirm_error"><img src='style/imgs/exclamation.png'></span></th>
				</tr>
				<tr>
					<th><?php echo $lang["register9"]; ?></th>
					<th><input class="form_input" type="email" id="register_email" name="register_email" placeholder="<?php echo $lang["register9"]; ?>" onchange="Validate('email');" required></th>
					<th><span id="email_error"><img src='style/imgs/exclamation.png'></span></th>
				</tr>
				<tr>
					<th><?php echo $lang["register10"]; ?></th>
					<th><input class="form_input" type="number" id="register_delete" name="register_delete" maxlength="7" placeholder="<?php echo $lang["register10"]; ?>" value="1234567" onchange="ValidatePwd('delete');" required></th>
					<th><span id="register_delete_error"><img src='style/imgs/accept.png'></span></th>
				</tr>
				<tr>
					<th><?php echo $lang["register11"]; ?></th>
					<th><input type="checkbox" name="register_checkbox" id="register_checkbox" value="second_checkbox" onchange="ValidateCheck('checkbox');" required><?php echo $lang["register12"]; ?> <a href="#">t&c</a></th>
					<th><span id="checkbox_error"><img src='style/imgs/exclamation.png'></span></th>
				</tr>
				<tr>
					<th>Captcha</th>
					<th><div class="g-recaptcha" data-theme="dark" data-sitekey="<?php echo RECAPTCHA; ?>"></div></th>
					<th></th>
				</tr>
			</table>
			<br>
			<input class="button" type="submit" value="<?php echo $lang["register13"]; ?>">
		</form>
		</center>
	</div>
</div>