<?php
include ("../../session.php");
include("../../connect.php");
$user=$_SESSION['admin'];

$id = $_POST['id'];
$cName = $_POST['cName'];
$ic = $_POST['ic'];
$cNo = $_POST['cNo'];
$email = $_POST['email'];
$code = $_POST['codeCountry'];
$file = $_POST['file'];

date_default_timezone_set ("Singapore"); 
		$ldate=date("d-m-Y", time());
		$ltime=date("h:i:sa", time());

$expire=date('d-m-Y', strtotime('365 days'));
$pass = generatePassword(12,2);

//-----------------------------------------------------------------//
if ($_FILES["file"]["size"] < 20000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    //echo "Type: " . $_FILES["file"]["type"] . "<br />";
    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
$filename = basename ($_FILES["file"]["name"]);	  


//-----------------------------------------------------------------//
	
	mysql_query("INSERT INTO agent (agentID,name,countryCode,contactNo,ic,email,pic,password,joinDate,status)
			VALUES('$id','$cName','$code','$cNo','$ic','$email','$filename','$pass','$ldate','available')");
	
	mysql_query("INSERT INTO logincontrol(id)VALUES('$id')");
			
mysql_close($login);

echo "<script language=javascript>alert('Register successful!!');
		window.location='success.php?id=$id'</script>";
	

//password generator	
function generatePassword($length=9, $strength=0) {
	$vowels = 'aeuy';
	$consonants = 'bdghjmnpqrstvz';
	if ($strength & 1) {
		$consonants .= 'BDGHJLMNPQRSTVWXZ';
	}
	if ($strength & 2) {
		$vowels .= "AEUY";
	}
	if ($strength & 4) {
		$consonants .= '23456789';
	}
	if ($strength & 8) {
		$consonants .= '@#$%';
	}
 
	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i < $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}
?>