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

$id = $_POST['id'];
$cName = $_POST['cName'];
$address = $_POST['address'];
$cNo = $_POST['cNo'];
$email = $_POST['email'];
$person = $_POST['person'];
$class = $_POST['class'];
$post = $_POST['post'];
$country = $_POST['country'];
$code = $_POST['code'];
$paidBy = $_POST['paidBy'];
$year = $_POST['year'];

//-----------------Take the rate out---------------------------//
$pQuery=mysql_query("select * from price order by id desc");
	$displayP=mysql_fetch_array($pQuery);
	$price=$displayP['price'];
	
	$price = $price * $year;

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

if($year==1){
$expire=date('Y-m-d', strtotime('365 days'));
}
if($year==2){
$expire=date('Y-m-d', strtotime('730 days'));
}
if($year==3){
$expire=date('Y-m-d', strtotime('1095 days'));
}
$pass = generatePassword(12,2);

$time = $expire." ".$ltime;

//-------------------------------------------------------------------------------------//

$day = substr($ldate,0,2);
$month = substr($ldate,3,2);
$years = substr($ldate,6,4);

//------------------------Auto increment id (License report)------------------------------//
$str = "SELECT Max(id) FROM licenseReport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "L0000001";
		}


  // echo "Directory has been created successfully...";
	mysql_query("INSERT INTO company (id,password,class,cName,address,postcode,country,countryCode,telNo,email,cPerson,registerDate,expireDate,agentID,sorttime)
			VALUES('$id','$pass','$class','$cName','$address','$post','$country','$code','$cNo','$email','$person','$ldate','$expire','$user','$time')"); 
	
	mysql_query("INSERT INTO licenseReport (id,companyID,day,month,year,updateDate,updateTime,oldExpire,newExpire,updateYear,payment,type,paymentType,agent)
	VALUES('$number','$id','$day','$month','$years','$ldate','$ltime','$ldate','$expire','$year','$price','new','$paidBy','$user')"); 
	
	mysql_query("INSERT INTO logincontrol (id)VALUES('$id')");
mysql_close($login);

//define the receiver of the email
$to = "$email";
//define the subject of the email
$subject = 'Register as easyShop business'; 
//define the message to be sent. Each line should be separated with \n
$message = "Hi! Welcome to be our easyShop member. 
\nYour login details will shown in below:
\nUser ID :$id\n
  Password :$pass\n
  Expire date :$expire\n
  Link to = http://www.wauhaha.com/smart/login.php
			"; 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: j_jwai2046@hotmail.com\r\nReply-To: j_jwai2046@hotmail.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";

echo "<script language=javascript>alert('Register successful!!');
		window.location='newRegister.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;
}
?>