<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com  2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

/*---------Multicart 2.0 Upgradation starts----------*/

$username_flag = 1;
$password_flag = 0;

/*---------Multicart 2.0 Upgradation ends----------*/

$chkexpusers =  getCheckoutExpiredUsers();
$chkexpusers  = explode(",",$chkexpusers );
for($i=0;$i<count($chkexpusers);$i++){
	adjustStock($chkexpusers[$i],"+" );
}
//getting values for redirecting to productdetails.php after successful login
if (isset($_GET["productid"]) and $_GET["productid"] != "") {
    $productid = $_GET["productid"];
}
else if (isset($_POST["productid"]) and $_POST["productid"] != "") {
   $productid = $_POST["productid"];
}

if (isset($_GET["artistid"]) && $_GET["artistid"] !="") {
    $artistid = $_GET["artistid"];
}
else if (isset($_POST["artistid"]) and $_POST["artistid"] != "") {
   $artistid = $_POST["artistid"];
}

if (isset($_GET["action"]) and $_GET["action"] != "") {
    $action = $_GET["action"];
}else if (isset($_POST["action"]) and $_POST["action"] != "") {
    $action = $_POST["action"];
}

/*
if($action != ""){
	$params = "action=".$action."&productid=".$productid;
}else if (isset($_POST["params"]) and $_POST["params"] != "") {
	$params = $_POST["params"];
}*/


$txtUserName = $_POST["txtUserName"];
$txtPassword = $_POST["txtPassword"];
if(isset($_POST["submit"]))
{
	/*$sql=" SELECT user_id,user_name,password FROM ".$tableprefix."users WHERE user_name='".addslashes($txtUserName)."' AND password = '".addslashes(md5($txtPassword))."' AND deleted = 'N'";
	$result=mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($result) > 0 ){
			$row = mysql_fetch_array($result);
			$message="Success!";
			$_SESSION["sess_username"] = $row["user_name"];
			$_SESSION["sess_userid"] = $row["user_id"];*/
			
/*----------Multicart 2.0 Upgradation starts-----------*/

	
/*---------Checking the password----------------*/	
			
	$select_password = "SELECT password FROM ".$tableprefix."users WHERE LOWER(user_name) = ".strtolower(GetSQLValueString($txtUserName,"text")).
						" AND deleted = 'N'";
	$result_password = mysql_query($select_password) or die(mysql_error());			
	if(mysql_num_rows($result_password) > 0)
	{
	
		$password_row = mysql_fetch_assoc($result_password);
						
		if(GetSQLValueString($password_row['password'],"text") != GetSQLValueString(md5($txtPassword),"text"))
		{
		
			$password_flag = 1;
			
		}
	
	
	}
	
/*---------Checking the username----------------*/		
	
		$select_username = "SELECT user_name FROM ".$tableprefix."users WHERE password = ".GetSQLValueString(md5($txtPassword),"text").
							" AND deleted = 'N'";
		$result_username = mysql_query($select_username) or die(mysql_error());			
		if(mysql_num_rows($result_username) > 0)
		{
		
			while($username_row = mysql_fetch_array($result_username))
			{
				if(strtolower(GetSQLValueString($username_row['user_name'],"text")) == strtolower(GetSQLValueString($txtUserName,"text")))
				{
				
					/*$message = "Sorry, the username you submitted is not present in our database !!";
					$username_flag = 1;*/
					$username_flag = 0;
				
				}
				/*else
				{
				
					$username_flag = 0;
				
				}*/
			}	
		
		
		}
						
		if($username_flag == 1 && $password_flag == 1)
		{
			$message = INVALID_USER_PASSWORD_TEXT;
		}
		if($username_flag == 1 && $password_flag == 0)
		{
			$message = INVALID_USER_PASSWORD_TEXT;
		}
				
		if($username_flag != 1 && $password_flag != 1)
		{				
			$sql=" SELECT user_id,user_name,password FROM ".$tableprefix."users WHERE user_name='".addslashes($txtUserName)."' AND password = '".addslashes(md5($txtPassword))."' AND deleted = 'N'";
			$result=mysql_query($sql) or die(mysql_error());
			if(mysql_num_rows($result) > 0 )
			{
				$row = mysql_fetch_array($result);
				$message="Success!";
				$_SESSION["sess_username"] = $row["user_name"];
				$_SESSION["sess_userid"] = $row["user_id"];
		// Ending Session for Sellers, Affiliates		
			if(isset($_SESSION["sess_artistid"]) and $_SESSION["sess_artistid"]!= "")
			{ // Logging Out User  from same window
			if(function_exists('session_unregister')) {
				session_unregister($_SESSION["sess_artistid"]);
                        }
                        else{
                            unset($_SESSION["sess_artistid"]);
                        }
					
			}
			if(isset($_SESSION["sess_affiliateid"]) and $_SESSION["sess_affiliateid"]!= "")
			{ // Logging OutSeller  from same window
			if(function_exists('session_unregister')){
				unset($_SESSION["sess_artistid"]);
                        }
					
			}
	// End check 		
				
/*---------------Multicart 2.0 Upgradation------------------*/				
				
/*-------function call to manage the different sessions--------*/

				manageSession(3);
				
/*---------------Multicart 2.0 Upgradation------------------*/				
				
				if(!isVisitorCartEmpty(session_id()))
				{
					mergeVisitorCartToUserCart(session_id(),$row["user_id"]);
				}
				
				
				// Check Gift Products Exists in Cart  // Then delete NON gift Products 
				if(checkGiftProductsExistsinCart($row["user_id"]))
				{
					
					cartClearDigital($row["user_id"]);
				}
				
				
				
				
				if($action!= "")
				{
				
					switch($action)
					{
					
							case "checkout":
							{
		
								header("Location:viewcart.php?action=checkout");
								exit;
								break;
							}
							
							case "postartistfeedback":
							{
		
								 header("Location:postartistfeedback.php?artistid=$artistid&productid=$productid");
								 exit();
								break;
							}
							
							case "artistfeedbacks":
							{
		
								header("Location:artistfeedbacks.php?artistid=$artistid&productid=$productid");
									exit();
								break;
							}
							
							default : 
							{
								 header("Location:index.php");
							 
								 exit;
							}
				}			
					
					
				}
				else
				{
			
					 header("Location:index.php");
					 
					 exit;
					
				}
				
/*--------Multicart 2.0 ends-----------*/				
				
				
			/*if($action == "postartistfeedback")
			{ 
			  header("Location:postartistfeedback.php?artistid=$artistid&productid=$productid");
			  exit();
			   
			}
			else if($action == "artistfeedbacks")
			{
			   header("Location:artistfeedbacks.php?artistid=$artistid&productid=$productid");
			   exit();
			}*/
			
			}
			else
			{
					
				$message = INVALID_USER_PASSWORD_TEXT;
		
			}
	
		}		
		
}
/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";
/* Top File Name */

include_once ("includes/".$active_template[0]);

?>
<script src="./js/javascript_check_userside.js"></script>
<script>
	function checkLoginFormNew()
	{
	
	  var form_name = new Array('txtUserName','txtPassword');		
	  var display_name = new Array('<?php echo USER_NAME_TEXT;?>','<?php echo PASSWORD_TEXT;?>');
	  var count = 2;	
	
	  if(check_login('frmLoginNew',form_name,display_name,count))	
	  {
	  
		
			   
	  }
	  else
	  {
	  
			return false;
	  
	  }
	   
	}
	
	
	function clearLogin()
	{
		document.frmLoginNew.txtUserName.value="";
		document.frmLoginNew.txtPassword.value="";
		return true;
	}
	
</script>
			<div align="left" class="main_body">
			<!--mainbody area start -->
			
			<!--center content area start-->		
	
	<form name="frmLoginNew" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" onSubmit="return checkLoginFormNew();">
	<input type="hidden" value="<?php echo $params;?>" name="params">
	<input type="hidden" value="<?php echo $action;?>" name="action">
	
	
	<div align="left" id="item_details">
	
	
	<!--items display area start -->

	<div align="left" id="items_top_area">
			<div align="left" id="items_top_area_ttl"><?php echo MEMBER_LOGIN_TEXT;?></div>
	</div>
	
	<div class="text_information" align="center"><?php echo $message?></div>


	<div id="login_area">
	<table width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><h5><?php echo RETURN_CUSTOMER_TEXT;?></h5></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><table width="400" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="133"><div class="logform_flheads2"><?php echo USER_NAME_TEXT1;?></div></td>
        </tr>
      <tr>
        <td><input name="txtUserName" class="textfield" type="text" size="65"  tabindex="1" value="<?php echo $_POST['txtUserName'];?>"/></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><table width="400" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="133"><div class="logform_flheads2"><?php echo PWD_TEXT;?></div></td>
      </tr>
      <tr>
        <td><input name="txtPassword" type="password" class="textfield" size="65"  tabindex="2" value="<?php echo $_POST['txtPassword'];?>"/></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="6"></td>
  </tr>
  <tr>
    <td><div class="bttn_cnt">
		    <input type="hidden" name="artistid" value="<?php echo $artistid;?>">
			<input type="hidden" name="action" value="<?php echo $action;?>">
			<input type="hidden" name="productid" value="<?php echo $productid;?>">	
			<input type="reset" class="button" value=" <?php echo RESET_BUTTON;?>" tabindex="4" onClick="return clearLogin();"> 
			&nbsp;<input name="submit" type="submit" class="button" value=" <?php echo LOGIN_BUTTON;?>" tabindex="3" />
						
	</div></td>
  </tr>
  <tr>
    <td><div align="left" class="login_btm_lnks"> <a href="forgotpassword.php" class="login_btm_lnks"><?php echo FORGOTPASSWORD_LINK_TEXT;?> </a></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
	<div class="clear"></div>																						
	</div>
	
	<div class="new_usersection">
	<h5><?php echo NEW_CUSTOMER_TEXT;?></h5>
	
	<p><?php echo SHOP_GUEST_TEXT;?></p>
	<?php 
	
		if(isset($_GET['action']) && $_GET['action']=='checkout')
		{
			$registerUrl=SITE_URL."/register.php?action=checkout";
		
		}
		else
		{
			$registerUrl=SITE_URL."/register.php";
		}
	?>
	<a href="<?php echo $registerUrl?>">
	<input type="button" class="button" value=" <?php echo CREATE_ACCOUNT_BUTTON;?>" tabindex="5"  onClick="Javascript:window.location='<?php echo $registerUrl;?>'" > 
	</a>
	<br>
	<br>
	<a href="<?php echo SITE_URL?>/checkout.php?guest_login=1">
	<input name="btn_guest" type="button" class="button" value="<?php echo CHECKOUT_GUEST_BUTTON;?>" tabindex="6"  onClick="Javascript:window.location='<?php echo SITE_URL?>/checkout.php?guest_login=1'"/>
	</a>
	
	<div class="clear"></div>																						

	</div>
			
	<div align="left" class="float_clear1"></div>
				
				
	 <!--items display area end -->
	  </div>
	
		</form>
	<!--center content area end-->	


	<!--mainbody area end -->
	</div>




				

<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																									