<?php
session_start();

$user_name     = $_POST['user_name'];
$user_password = $_POST['user_password'];
$selcompany	   = $_POST['selcompany'];
$selyear	   = $_POST['selyear'];
$key		   = $_POST['key'];

include('contract/config.inc.php');


$connection = mysql_connect($config['dbServer'],$config['dbUser_log'],$config['dbPass_log']) or die("Could not connect to DB");
mysql_select_db($config['dbName_log'],$connection) or die("Could not find DB");
	
	
if(isset($_POST['submit1'])){ 

	if ($_POST['user_name'] != "" && $_POST['user_password'] != "") {
	
		include('contract/Functions_Sales.php');
		
		if ($config['EncryptPassMod'] == 1)
			$querypass="select encryptpassword('$user_password') as pass";
		else
			$querypass="select '$user_password' as pass";
		
		
		$resultpass=mysql_query($querypass, $connection) or die(mysql_error());
		while($rows=mysql_fetch_array($resultpass)){
			extract($rows);
		}
		$queryusers="select * from users where User_account='$user_name' and User_password='$pass'";
		$resultusers=mysql_query($queryusers, $connection) or die(mysql_error());

		if(mysql_num_rows($resultusers)<1){
			$msg="<div style='width:600px;'>Invalid Username/Password !</div>";
				
			$key="";
		}else{
			while($rows=mysql_fetch_array($resultusers)){
				extract($rows);
			}

			
			$_SESSION['username']   =$User_Name;
			$_SESSION['useraccount']=$User_account;
			$_SESSION['defaultdb']  =$DefaultComp."_".$DefaultYear;
			/*session_register("username");
			$username = $User_Name;
			session_register("useraccount");
			$useraccount = $User_account;
			session_register("defaultdb");
			$defaultdb = $DefaultComp."_".$DefaultYear;*/

			$key="1";

		}
	} else {
		$msg="<div style='width:600px;'>Please enter a valid Username/Password !</div>";
				
		$key="";
	}
	
}


if(isset($_POST['submit2'])){
	//$_SESSION['dbName']=$_POST['company']."_".$_POST['year'];
	//$_SESSION['dbLabel']="accesslabels".$_POST['company'];
	$_SESSION['dbName']=$selcompany."_".$selyear;
	$_SESSION['dbLabel']=$config['dbName_log'];
	
	//session_register("dbName");
	$dbName = $selcompany."_".$selyear;
	//session_register("dbLabel");
	$dbLabel = $config['dbName_log'];
	
	
	include('contract/Functions_Sales.php');
	
	$query="select ifNull(EDataCom, 0) As EDataCom from login where User_account='".$_SESSION['useraccount']."' and Login_Schema='$dbName' ";
	//echo $query;exit;
	$result=mysql_query($query, $connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	} 
	if(mysql_num_rows($result)<1 || $EDataCom == 0){ 
		$msg="<div style='width:600px;'>User Has No Access To EDatacom !</div>";
		$key="";
		
	}
	else
	{
		$query="Update Users Set DefaultComp = '".$selcompany."', DefaultYear = '".$selyear."' Where User_account='".$_SESSION['useraccount']."' ";
		$result=mysql_query($query, $connection) or die(mysql_error());
		
		header("location:contract/menu.php");
	}

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $config['HomePageTitle']; ?>- Home Page</title>

<style type="text/css">

<!--

#apDiv1 {
	position:relative;
	width:800px;
	height:400px;
	z-index:1;
	margin: auto;
}

#apDiv5 {
	position:absolute;
	left:200px;
	top:0px;
	width:400px;
	height:107px;
	z-index:2;
	border-top-width: 1px;
	border-bottom-width: 1px;
	border-bottom-style: solid;
	border-bottom-color: #CCC;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #000;
}

-->
</style>

<style type="text/css">
<!--
.style1 {
	font-family: Arial;
	font-size: 13px;
	color: #4069A7;
	font-weight: bold;
}
.style2 {
	color: #FFFFFF;
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {
	font-size: 14px;
	font-weight: bold;
}
-->
.submit
{
         width: 101px;
         height: 27px;
	background-image: url(contract/img/loginbutton.png);
	background-repeat: repeat-x;
	border: 0px solid #4069A7;
	display: block;
	cursor: pointer;
	
}


.pictureInput {
      background-image: url(contract/img/samsung.jpg);
      background-position: 0 1px;
      background-repeat: no-repeat;
    }
</style>
</head>

<body bgcolor='#0d404f'>

  <p align="center">

<form action="" method="post" name="form1" id="form1">
<input type="hidden" name="key" id="key" value="<?php echo $key; ?>">

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" background='contract/img/loginpage.png' width='800' height='520' />

  <tr height='50px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td>
  </tr>
  <tr>
       <td colspan="4" align="center" height="20"><span class="style2"><?php echo $msg; ?></span></td>
     </tr> 
	   <tr height='170px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td>
  </tr>
  <tr height='48px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;" width='350px'>&nbsp;
  </td><Td  valign="center" colspan=2>&nbsp;
	<input type='text' style='font-size:14px;border:0;background-color: transparent;width:200px; border-color:transparent; outline:none;outline-offset: 0;' id='user_name' name='user_name' maxlength='15' value="<?php echo $user_name; ?>">
	</td>
  </tr>
  <tr height='8px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;" width='350px' colspan=3>&nbsp;
  </td>
  </tr>
   <tr height='55px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td><Td  valign="center" colspan=2>&nbsp;<input type='password' style='font-size:14px;border:0;background-color: transparent;width:200px' id='user_password' name='user_password' maxlength='15' value="<?php echo $user_password; ?>"></td>
  </tr>
  
   <?php if($key==""){  ?>
  <tr height='65px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td>
  </tr>
   <?php  } ?>
  
   <?php if($key=="1"){  ?>
   <?php $defaultschema=explode('_',$_SESSION['defaultdb']); ?>
    <tr height='45px'><td>&nbsp;</td>
      <td  align="left"  class="style1" valign='bottom' width="70px">Company&nbsp;&nbsp;</td><td valign='bottom' width="160px">
    <?php 
         $querylogin="SELECT DISTINCT CompanyName FROM listuserdir where Usercode='".$user_name."'  order by CompanyName";
		// echo $querylogin;?>
		 
	  <select name="selcompany" id="selcompany" onchange="document.form1.submit();" style='width:135px; color: #003366; font-family: Arial; font-weight: none; font-size: 12;  '>
	  <?php 
        // $querylogin="SELECT DISTINCT CompanyName FROM directories order by CompanyName";
		//exit;
		
		$resultlogin=mysql_query($querylogin, $connection) or die(mysql_error());
		while($rows=mysql_fetch_array($resultlogin)){
			extract($rows);
			//$schema=explode('_',$Login_Schema);

	  ?>
      <option value="<?php echo "$CompanyName";?>" <?php if ((!isset($_POST['selcompany']) && $CompanyName == $defaultschema[0]) || (isset($_POST['selcompany']) && $CompanyName == $_POST['selcompany'])) echo" selected "; ?> ><?php echo "$CompanyName";?></option>
      <?php  } ?>
	  
      </select></td><td>&nbsp;</td>
    </tr>
	
    <tr  height='45px'><td></td>
      <td align="left"  class="style1">Year&nbsp;&nbsp;</td><td>
      <label>
        <select name="selyear" id="selyear" style='width:135px; color: #003366; font-family: Arial; font-weight: none; font-size: 12;  '>  <?php $schema1=explode('_',$_SESSION['db1']); ?>
        <?php 
          $querylogin="SELECT DISTINCT Diryear FROM listuserdir where Usercode='".$user_name."'";
		  if ($selcompany != "")
			$querylogin .= "and Companyname = '".$selcompany."' order by Diryear ";
		  else
			$querylogin .= "and Companyname = '".$defaultschema[0]."' order by Diryear ";
			
		  $resultlogin=mysql_query($querylogin, $connection) or die(mysql_error());
		  while($rows=mysql_fetch_array($resultlogin)){
		  extract($rows);
		  //$schema=explode('_',$Login_Schema);

		?>
        <option value="<?php echo $Diryear;?>" <?php if ($Diryear == $defaultschema[1]) echo" selected "; ?> ><?php echo $Diryear;?></option>
          <?php } ?>
        </select>
      </label></td><td>&nbsp;</td>
    </tr>
<?php }  ?>
 <tr height='30px'>
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td><td width="40px"></td><td ><?php if($key==""){ ?>
	      
		  <input name="submit1" type="submit" id="submit1" class="submit"  title="Login"  value="" />

		<?php } ?>  
		<?php if($key=="1"){ ?>
    	  <input name="submit2" type="submit" id="submit2" class="submit"  title="Login"  value="" />
		<?php } ?>  
  </tr>
  
  <tr >
  <td align="center" valign="top"  style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;
  </td>
  </tr>

	
  </table>
</form>
</p>

<p align='center'><font face='Verdana' color='#ffffff' size='1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Designed and Developed by <b><a href="http://www.compusoft-ng.com"target=_blank style="color: #ffffff">CompuSoft Nigeria Ltd.</a></b> </b></font></p>
<p align='center'><font face='Verdana' color='#ffffff' size='1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Click <a href="mailto:Support@compusoft-ng.com?subject=Support Request">Here</a> for any support</b> </b></font></p>
<p align='center'><font face='Verdana' color='#ffffff' size='1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&copy; Copyright 2010-
<script language="JavaScript" type="text/javascript">
    now = new Date
    theYear=now.getYear()
    if (theYear < 1900)
    theYear=theYear+1900
    document.write(theYear)
</script>
 All rights reserved. DataCom Web Version </p>


</body>
</html>