<?
  include("config/connect.php");
  include("functions.php");
  $ref_url = $_POST["ref_url"];
  
  if($_GET["auc_key"]!="")
  {
//  	$uid = base64_decode($_GET["auc_key"]);
//  	$username = base64_decode($_GET["un"]);
//	$passs = base64_decode($_GET["pn"]);
	$auckey = $_GET["auc_key"];
	$confirmemail = 1;
	
	$qryuser = "select * from registration where account_status='0' and verify_code='$auckey' ";
	$resuser = mysql_query($qryuser);
	$totaluser = mysql_num_rows($resuser);
	
	if($totaluser>0)
	{
		$ress = mysql_fetch_object($resuser);
		$uid = $ress->id;		
		
		$qryupd = "update registration set account_status='1',final_bids=final_bids+5 where id='$uid'";
		mysql_query($qryupd) or die(mysql_error());  
  	
		$qryvoucher = "select * from vouchers where newuser_flag='1'";
		$resvoucher = mysql_query($qryvoucher);
		$totalvoucher = mysql_num_rows($resvoucher);
		if($totalvoucher>0)
		{
			$objvoucher = mysql_fetch_object($resvoucher);
			$voucherdesc = $objvoucher->voucher_title;
			if($objvoucher->validity>0)
			{			
				$voucherdesc .= " (ÓÐÐ§ÆÚ ".$objvoucher->validity." Ìì)";
				$expirydate = AcceptDateFunctionStatus_Voucher(date("Y-m-d H:i:s",time()),$objvoucher->validity);
				$qryinsv = "Insert into user_vouchers  (user_id,voucherid,issuedate,expirydate,voucher_status,voucher_desc) values('".$uid."','".$objvoucher->id."',NOW(),'".$expirydate."','0','".$voucherdesc."');";
				mysql_query($qryinsv) or die(mysql_error());						
			}		
		}	
	 	$query="select * from registration where id='$uid'";
	}
 }
  
 elseif($_GET["ud"]!="" && $_GET["pd"]!="" && $_GET["key"]!="")
 {
  	$username = base64_decode($_GET["ud"]);
	$pass = base64_decode($_GET["pd"]);
	  $query="select * from registration where username='$username' and password='$pass' and account_status='1'";
  }
  
  else
  {
  $username = $_POST["username"];
  $pass = $_POST["password"];

  $query="select * from registration where username='$username' and password='$pass' and account_status='1'";
  }
  
  $result=mysql_query($query);
  $total = mysql_num_rows($result);
  $ress = mysql_fetch_object($result);
 if($total>0)
  {
  	if($ress->member_status=="0" && $ress->user_delete_flag!='d')
	{
	  $_SESSION["username"]=$ress->username;
	  $_SESSION["userid"]=$ress->id;
	  $_SESSION['url'] = $SITE_URL;
	  $_SESSION["sessionid"] = session_id();
	  
			if($_SESSION["ipid"]!="")
			{
				$qryipupd = "update login_logout set user_id='".$_SESSION["userid"]."',login_time=NOW() where load_time='".$_SESSION["ipid"]."'";
				mysql_query($qryipupd) or die(mysql_error());
			}
			else
			{
				$_SESSION["ipaddress"] = $_SERVER['REMOTE_ADDR'];
				$qryinsip = "insert into login_logout (user_id,ipaddress,login_time,load_time) values('".$_SESSION["userid"]."','".$_SESSION["ipaddress"]."',NOW(),NOW())";
				mysql_query($qryinsip) or die(mysql_error());
				$_SESSION["ipid"]=date("Y-m-d H:i:s",time());
				session_unregister("login_logout");
			}
			
	  
	  if($_GET["key"]!="")
	  {
	  echo "<script>parent.location.href='editpassword.html'</script>";
	  }
	  elseif($_POST["feedback_hidden"]!="")
	  {
	  echo "<script>parent.location.href='feedback.html'</script>";
	  }
	  else
	  {
	  	if($confirmemail==1)
		{
	  echo "<script>parent.location.href='emailconfirmsuccess.html'</script>";
			exit;
		}
		else
		{
			if($_POST["index"]!="")
			{
	  echo "<script>parent.location.href='index.html'</script>";
				exit;
			}
			if($_POST["wonauctions"]!="")
			{
	  echo "<script>parent.location.href='wonauctions.html'</script>";
				exit;
			}
			else
			{	
				if($ref_url!=""){
	  		echo "<script>parent.location.href='myaccount.html?ref_url=".$ref_url."'</script>";
				}else{
	  echo "<script>parent.location.href='myaccount.html'</script>";
				}
			  exit;
			}
		}
	  }
	}
	else
	{
		if($ress->member_status=="1")
		{
			header("location:login_dialog.php?err=2");
			exit;
		}
		elseif($ress->user_delete_flag=='d')
		{
			header("location:login_dialog.php?err=3");
			exit;
		}
	} 
  }
   else
  {
	  if($username=="" && $pass=="")
	  {
	  	header("location:login_dialog.php?err=4");
	  }
	  elseif($username!="" && $pass=="")
	  {
	  	header("location:login_dialog.php?err=5");
	  }
	  else
	  {
	  	header("location:login_dialog.php?err=1");
	  }
  }
  
?>