<?php
include("../includes/site_config.php");
//include("../lib/class.Main.php");
/*if ($_REQUEST['speeddating'] == 1) {

    include ('speeddating/error.php');

} else {

    include ('../pre_error.php');

}*/
# clean up the input
/*
if ($_COOKIE['WUsername_c']) {

	$WUsername=trim($_COOKIE['WUsername_c']);

	$WPassword=trim($_COOKIE['WPassword_c']);

}

if ($_POST['WUsername']){

	$WUsername=trim($_POST['WUsername']);

	$WPassword=trim($_POST['WPassword']);

}

# gives basic validation if the javascript fails to catch

if (empty($WUsername) || strlen($WUsername) < 6) {

	$error_message=AFF_INDEX_ERROR1;

	error_page($error_message,GENERAL_USER_ERROR);

}

if (empty($WPassword) || strlen($WPassword) < 4) {

	$error_message=AFF_INDEX_ERROR2;

	error_page($error_message,GENERAL_USER_ERROR);

}
*/
$query="SELECT * FROM members WHERE mem_username = '$WUsername' AND mem_password = '$WPassword'";
echo $query;

//$query="SELECT * FROM members WHERE mem_username = '$WUsername'";

$retval=mysql_query($query,$link) or die(mysql_error());

$result=mysql_num_rows($retval);

# if no user is found then display error else set the cookie

if ($result < 1) {

	$error_message=LOGIN_ERROR;

	error_page($error_message,GENERAL_USER_ERROR);

} else {

	$arr_row = mysql_fetch_object($retval);



	if ($arr_row->mem_confirm==0) {

		$error_message=sprintf(LOGIN_ERROR2,$CONST_URL);

		error_page($error_message,GENERAL_USER_ERROR);

	}



    if ($arr_row->mem_suspend=="Y") {

        $error_message=sprintf(LOGIN_ERROR4,$CONST_URL);

        error_page($error_message,GENERAL_USER_ERROR);

    }





	if ($_POST[save]){

		setcookie ("WUsername_c", $WUsername,time()+3600*24*356);

		setcookie ("WPassword_c", $WPassword,time()+3600*24*356);

	}

	else{

		setcookie ("WUsername_c", $WUsername,time()-3600);

		setcookie ("WPassword_c", $WUsername,time()-3600);

		$_COOKIE[WUsername_c] = '';

		$_COOKIE[txtPassowrd_c] = '';

	}

	session_start();



echo	$_SESSION['Sess_UserType']=$arr_row->mem_type;

echo	$_SESSION['Sess_UserName']=$arr_row->mem_username;

echo	$_SESSION['Sess_UserId']=$arr_row->mem_userid;

echo	$_SESSION['Sess_LastVisit']=$arr_row->mem_lastvisit;

	session_unregister('Sess_JustRegistered');

	$testdate=date("Y-m-d");

	$sess_id="PHPSESSID=".session_id();

	if ($arr_row->mem_expiredate < $testdate) {

		$_SESSION['Sess_Userlevel']="silver";

	} else {

		$_SESSION['Sess_Userlevel']="gold";

		# write in the instant message code

		#$template->tpl_header=str_replace("<body","<body onLoad=\"javascript:MDM_openWindow('$CONST_LINK_ROOT/messenger/instantmessage.php?$sess_id&action=new','InstantMessaging','width=550,height=350')\"",$template->tpl_header);

		#$query="UPDATE members SET mem_online='Y' WHERE mem_username = '$WUsername' AND mem_password = $WPassword";

		#$retval=mysql_query($query,$link) or die(mysql_error());

	}

	# update the last visit date

	$lastvisit=date('Y/m/d');

	$query="UPDATE members SET mem_lastvisit='$lastvisit' WHERE mem_username = '$WUsername' AND mem_password = '$WPassword'";

	$retval=mysql_query($query,$link) or die(mysql_error());

	# special code for prgcomeback.php

	if (isset ($comeback)) {

		$expiredate=mktime (0,0,0,date("m") ,date("d")+3,date("Y"));

		$expiredate=date('Y/m/d',$expiredate);

		$query="UPDATE members SET mem_expiredate='$expiredate' WHERE mem_username = '$WUsername' AND mem_password = '$WPassword'";

		$retval=mysql_query($query,$link) or die(mysql_error());

	}

}



if($_SESSION['HISTORY_PAGE'][0])

{

	// Redirect to the previous page.

	save_request();

	header('Location: '.get_prev_page_url());

}

elseif ($_REQUEST['speeddating']==1)

{

    header("Location: $CONST_LINK_ROOT/speeddating/home.php?$sess_id");

}

else

{

	$sql_result = mysql_query('SELECT * FROM adverts WHERE adv_approved != 2 AND adv_userid = '.$_SESSION['Sess_UserId'], $link);

	if(mysql_num_rows($sql_result) > 0){

		header("Location: $CONST_LINK_ROOT/home.php?$sess_id");

	} else {

		header("Location: $CONST_LINK_ROOT/prgamendad.php?$sess_id");

	}

}



?>