<?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.                                                       |
// +----------------------------------------------------------------------+
?>
<!-- Login box-->
<?php

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

$username_flag = 0;
$password_flag = 0;

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

if(isset($_POST['loginbox'])){
   $txtUserName = $_POST["txtUserName"];
   $txtPassword = $_POST["txtPassword"];
	/*$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 user_name = ".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"))
		{
		
			$messagelog = "Sorry, the password you submitted is not present in our database !!";
			$password_flag = 1;
			
		}
	
	
	}
	//else
	//{
	
/*---------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(GetSQLValueString($username_row['user_name'],"text") != GetSQLValueString($txtUserName,"text"))
				{
				
					$messagelog = "Sorry, the username you submitted is not present in our database !!";
					$username_flag = 1;
				
				}
				else
				{
				
					$username_flag = 0;
				
				}
			}	
		
		
		}
	//}	
	
	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"];
				
				
			if(!isVisitorCartEmpty(session_id()))
			{
				mergeVisitorCartToUserCart(session_id(),$row["user_id"]);
			}
			if($action!= "")
			{
				if($action == "checkout")
				{
					echo "<script>location.href='viewcart.php';</script>";
					//header("Location:viewcart.php");
					exit;
				}
			}
			else
			{
			
/*--------Multicart 2.0 starts-----------*/			
			
			    /*echo "<script>location.href='usermain.php';</script>";*/
				
				echo "<script>location.href='viewcart.php';</script>";
				
/*--------Multicart 2.0 ends-----------*/				
				
				//header("Location:usermain.php");
				exit;
			}

	}	
	else
	{
		$messagelog = "Sorry, the username and password you submitted are not present in our database !!";

	}


}	

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

}else if(isset($_POST['newuser'])){
       echo "<script>location.href='register.php';</script>";
	  exit;
}
?>
<script src="./js/javascript_check_userside.js"></script>
<script>
	function templateChange()
	{
		document.frmTemplate.submit();
		//document.frmTop.submit();
	}	
	function checkLoginForm()
	{
	
	  var form_name = new Array('txtUserName','txtPassword');		
	  var display_name = new Array('User Name','Password');
	  var count = 2;	
	
	  if(check_fieldsUser('frmLogin',form_name,display_name,count))	
	  {
	  
		
		 	   
	  }
	  else
	  {
	  
			return false;
	  
	  }
	   
	}
</script>
<?php


if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= ""){ ; }else if(strcasecmp(basename($_SERVER['SCRIPT_FILENAME']),"login.php")!=0){?>

<form name="frmLogin" action="" method="POST" onSubmit="return checkLoginForm();">
 <table width="100%"  border="0" cellpadding="0" cellspacing="0">
					    <tr>
						 <td colspan=2 align=left class="tableheading"><font color=red><?php echo $messagelog;	?></font></td>
						</tr>
                        <tr>
                          <td align="left" class="tableheading">Username
                              <input name="txtUserName" type="text" class="textbox"></td>
                        </tr>
                        <tr>
                          <td align="left" class="tableheading">Password
                              <input name="txtPassword" type="password" class="textbox"></td>
                        </tr>
                        <tr>
                          <td width="88%" height="31" align="left" valign="middle" class="tableheading">
						  	<input  type="submit" class="submit" name="newuser" value="New User Signup"  style="width:90px">
						    <input  type="submit" class="submit" name="loginbox" value="Login" style="width:75px">
                          </td>
                        </tr>
                      </table>
                      <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="linksCopy3">
                            <tr>
                              <td><span class=steps><a class="links" href="forgotpassword.php">Forgot Password?</a></span></td>
                            </tr>
                      </table>

					  </form>
  <?php } ?>




  <!-- End of login-->
  
  
  
  <?php
  
  /*currency section starts*/

		$currency_selected = "";
		 if (function_exists('session_register'))
                        session_register('SESS_currency_type');
								
		if(isset($_POST['currency_type']))
		{
		
			//activateCurrencyType($_POST['currency_type']);
			$currency_type = $_POST['currency_type'];
			
			$_SESSION['SESS_currency_type'] = $currency_type;
					
			echo "<script type='text/javascript'> window.location='".$_SERVER['HTTP_REFERER']."';</script>";
	
		}
		else
		{
		
			if (!isset($_SESSION['SESS_currency_type']) OR $_SESSION['SESS_currency_type'] == "")
			{
				$_SESSION['SESS_currency_type']	= "1";
				$currency_type					= "1";
				
			}
		
		
			/*$select_currency_id = "SELECT ncurrency_id FROM ".$tableprefix."currency_master WHERE vactive = 'Y'";
			$result_currency_id = mysql_query($select_currency_id) or die(mysql_error());
			$currency_rowid = mysql_fetch_assoc($result_currency_id);
		
			$currency_type = $currency_rowid['ncurrency_id'];*/
		}
		
		/*currency section ends*/
		
		
		/*template section starts*/
		
		$template_selected = "";
		 if (function_exists('session_register'))
                        session_register('SESS_template');
		
		if(isset($_POST['template']))
		{
		
			//activateTemplate($_POST['template']);
			$template_type = $_POST['template'];
			
			$_SESSION['SESS_template'] = $template_type;
					
			echo "<script type='text/javascript'> window.location='".$_SERVER['HTTP_REFERER']."';</script>";
		}
		else
		{
		
			if (!isset($_SESSION['SESS_template']) OR $_SESSION['SESS_template'] == "") {
				$_SESSION['SESS_template']	= "1";
				$template_type				= "1";
				
			}
				
			/*$select_template_id = "SELECT ntemplate_id FROM ".$tableprefix."template_master WHERE vactive_status = 'Y'";
			$result_template_id = mysql_query($select_template_id) or die(mysql_error());
			$template_rowid = mysql_fetch_assoc($result_template_id);
		
			$template_type = $template_rowid['ntemplate_id'];
			
			$_SESSION['SESS_template'] = $template_type;*/
		}

				

		/*template section ends*/

?>
  
  
  
  

  <!-- shopping cart-->
  <?php
	if($shopingcartdisplay=="Y"){
  ?>
  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                           <tr>
                               <td width="9%" align="left"><img src="images/icon1.jpg" width="15" height="15"></td>
                               <td width="91%" class="headng" align="left"> &nbsp;&nbsp;&nbsp;Cart </td>
                             </tr>
							 <tr><td  colspan=2><img src="images/spacerr.gif" HEIGHT="4"></td></tr>
                             <tr>
                               <td colspan="2" class="sidetopbar"><img src="images/spacerr.gif" width="171" height="25"></td>
                             </tr>
                          </table>
                          <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="sidecoloumn">
                            <tr>
                              <td >

							    <table width="100%"  border="0" cellspacing="0" cellpadding="3">
								       <?
							$total = 0;
							$cartitems = "";
							if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= ""){
								$sql = "SELECT c.*, p.product_name,p.product_price,p.discount FROM ".$tableprefix."cart c INNER JOIN ".$tableprefix."products p ON c.product_id = p.product_id WHERE  c.user_id = '".addslashes($_SESSION["sess_userid"])."' ";
							}else{
								$sql = "SELECT c.*, p.product_name,p.product_price,p.discount FROM ".$tableprefix."cart c INNER JOIN ".$tableprefix."products p ON c.product_id = p.product_id WHERE  c.visitor_cart_id = '".addslashes(session_id())."' ";
							}

							$res = mysql_query($sql);
								if(mysql_num_rows($res) > 0 ){
								while($rw = mysql_fetch_array($res)){
										$productname = $rw["product_name"];
										$rate = $rw["product_price"];
										$qty = $rw["quantity"];
										$price = $qty * $rate;
										$discountstr="";
										if($rw["discount"] !="0"){
										$price=$price- ($price*$rw["discount"])/100;
										$discountstr="<br>discount(".$rw["discount"]."%)";

										}
										$total += $price;
										$link="<a class=\"links\" href=\"viewcart.php\" style=\"text-decoration:none;\">";
										echo "<tr><td width=\"8%\" align=\"right\"><img src=\"images/dot.jpg\" width=\"9\" height=\"9\"></td><td width=\"10%\">$link".$rw["quantity"]."&nbsp;x&nbsp;</a></td><td align=left style='word-break:break-all;'>$link".htmlentities($productname)."</a></td></tr>";
										$cartitems .= "," . $rw["cart_id"];
									}
									$cartitems = substr($cartitems,1 );



								?>

								  <tr>
								    <td COLSPAN="3" align=right valign="top"><img src="images/pixel_black.gif" border="0" alt="" width="50" height="1"><br>
									<?php
									
									$currency_attributes = displayCurrencyType();
									echo $currency_attributes[0];
									echo "&nbsp;";	
									
									echo number_format(($currency_attributes[1] * $total),2 ,".","" ); ?><br><img src="images/pixel_black.gif" border="0" alt="" width="50" height="1"><br><img src="images/pixel_black11.gif" border="0" alt="" width="50" height="0"><br><img src="images/pixel_black.gif" border="0" alt="" width="50" height="1"></td>
								  </tr>

								  <?php } else{ ?>

								  <tr>
								    <td COLSPAN="3" align=center><a class="links" HREF="categorydetail.php"> Cart is Empty!</a></td>
								  </tr>

								  <?php } ?>

                                  </table>




                                  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td class="sidecloumnbottmbottm"><img src="images/spacerr.gif" width="171" height="25" class="sidecloumnbottm"></td>
                                    </tr>
                                </table></td>
                            </tr>
                        </table>

  <?php } ?>

  <!-- Shopping cart-->





<!-- categories-->



                         <table width="100%"  border="0" cellspacing="0" cellpadding="0">
						     <tr><td  colspan=2><img src="images/spacerr.gif" HEIGHT="7"></td></tr>
                            <tr>
                                  <td width="9%" align="left"><img src="images/bag1.jpg" width="15" height="15"></td>
                                  <td width="91%" class="headng" align="left"> &nbsp;&nbsp;&nbsp;Categories </td>
                                </tr>
								 <tr><td  colspan=2><img src="images/spacerr.gif" HEIGHT="4"></td></tr>
                                <tr>
                                  <td colspan="2" class="sidetopbar1"><img src="images/spacerr.gif" width="171" height="25"></td>
                                </tr>
                          </table>
                          <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="sidecoloumn1">
                            <tr>
                              <td >

							     <table width="100%"  border="0" cellspacing="0" cellpadding="3">


																	<?php
																		
																		$sql=" SELECT category_id,category_name,count FROM ".$tableprefix."categories WHERE parent_id='0' ORDER BY category_name ";																				
																				
																		$resultcategories=mysql_query($sql) or die(mysql_error());
																		if(mysql_num_rows($resultcategories) > 0 )
																		{
																			while($rowcategories = mysql_fetch_array($resultcategories))
																			{
																			
																				$select_category_product = "SELECT product_id FROM ".$tableprefix."products WHERE product_category = ".$rowcategories['category_id'];
																				$result_category_product = mysql_query($select_category_product) or die(mysql_error());
																				
																				if($rowcategories['count'] > 0)
																				{
																																							
																					if(strlen($rowcategories["category_name"])>15)
																					  $category_name=substr($rowcategories["category_name"],0,15)."...";
																					else
																					   $category_name =$rowcategories["category_name"];
																					   
																				   

																	?>
																		<tr>
																		     <td width="8%" align="right" VALIGN="middle"><img src="images/dot.jpg" width="9" height="9"></td>
                                      										<td width="92%" VALIGN="top" align="left" class="links" style='word-break:break-all;'><a class="links" href="categorydetail.php?catid=<?php echo htmlentities($rowcategories["category_id"]); ?>"><?php echo htmlentities($category_name);?></a>&nbsp;(<?php echo htmlentities($rowcategories["count"]);?>)</td>
																		</tr>
																<?php 		
																				}
																
																			}
																		
																		}

																?>

                                  </table>

                                  <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="sidecloumnbottm1">
                                  <tr>
                                    <td><img src="images/spacerr.gif" width="171" height="25"></td>
                                  </tr>
                                </table>




                                </td>
                            </tr>
							<tr><td class=whitebg ><img src="images/spacerr.gif" HEIGHT="4"></td></tr>
                        </table>
<!-- categories ends here-->

<!---Currency Type changing starts-->



						<form name="frmCurrencyType" method="post">
						<table cellpadding="0" cellspacing="0" border="0" width="100%">
							<tr>
								<td class="headng">Currencies</td>
							</tr>
							<tr>
								<td>
									<select name="currency_type" class="textbox" onChange="Javascript:document.frmCurrencyType.submit();">
									<option value="">--Select--</option>
<?php

	
	$select_currency = "SELECT ncurrency_id,vcurrency_name FROM ".$tableprefix."currency_master WHERE vcurrency_delete_status != 'Y'";
	$result_currency = mysql_query($select_currency) or die(mysql_error());
	
	while($currency_row = mysql_fetch_array($result_currency))
	{
	
		if($_SESSION['SESS_currency_type'] == $currency_row['ncurrency_id'])
		{
		
			$currency_selected = "selected";
		
		}
		else
		{
				
			$currency_selected = "";
		
		}
				

?>									
									<option value="<?php echo $currency_row['ncurrency_id']?>" <?php echo $currency_selected?>><?php echo $currency_row['vcurrency_name']?></option>	
									
<?php

	}
	
?>										
									</select>
								
								</td>
							</tr>
						</table>
						</form>
						
<!---Currency Type changing ends-->



<!---Template selection starts-->

<!--


						<br>
						<form name="frmTemplate" method="post">
						<table cellpadding="0" cellspacing="0" border="0" width="100%">
							<tr>
								<td class="headng">Templates</td>
							</tr>
							<tr>
								<td>
									<select name="template" class="textbox" onChange="templateChange();">
									<option value="">--Select--</option>
									
-->
									
<?php

	
	/*$select_template = "SELECT * FROM ".$tableprefix."template_master WHERE 1";
	$result_template = mysql_query($select_template) or die(mysql_error());
	
	while($template_row = mysql_fetch_array($result_template))
	{
	
		if($_SESSION['SESS_template'] == $template_row['ntemplate_id'])
		{
		
			$template_selected = "selected";
		
		}
		else
		{
				
			$template_selected = "";
		
		}
		
		
				

?>									
									<option value="<?php echo $template_row['ntemplate_id']?>" <?php echo $template_selected?>><?php echo $template_row['vtemplate_name']?></option>	
									
<?php

	}
	
	*/
?>										

<!--

									</select>
								
								</td>
							</tr>
						</table>
						</form>
						
-->						
						
<!---Template selection ends-->