<?php
session_start();

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



include('contract/config.inc.php');
//include('contract/Functions_Sales.php');
include("contract/FunctionsLicense.inc.php");

$LicenseError  = $_GET['LicenseError'];
if ($LicenseError != "") {
	$msg="<div style='width:600px;'>".$LicenseError."</div>";
}
	

if(isset($_POST['submit1'])){ 

	if ($_POST['user_password'] != "") {
		
		$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
		mysql_select_db($config['dbName'],$conn) or die("Could not find DB");
		//$LicenseError = CheckSoftwareLicense($conn,$EncryptKey);
		
		$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 ($LicenseError != "") {
			$msg="<div style='width:600px;'>".$LicenseError."</div>";
					
			$key="";
		} else {
		
			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 User_Name, User_account, DefaultComp, DefaultYear from users where User_password='$pass'";
			$resultusers=mysql_query($queryusers, $connection) or die(mysql_error());

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

				
				$_SESSION['username']    = $User_Name;
				$_SESSION['useraccount'] = $User_account;
				$_SESSION['defaultdb']   = $DefaultComp."_".$DefaultYear;
				$_SESSION['dbName']      = $config['dbName'];
				$_SESSION['dbLabel']     = $config['dbName_log'];
				
				$dbName  = $config['dbName'];
				$dbLabel = $config['dbName_log'];
				

				if($DefaultComp=='' || $DefaultYear==''){
					$msg="<div style='width:200px;'>No default Company and Year specified for this user. !</div>";
					$Done = 0;
				}else{

					$query="select ifNull(EDataCom, 0) As EDataCom from login where User_account='$User_account' and Login_Schema='$dbName' ";
					$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:300px;'>User Has No Access To EDatacom !</div>";
						$Done = 0;
						
					}
					else
					{
						
						$Done = 1;
					}

				}

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


?>
<!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>
<script type="text/javascript" language="javascript">

function LoadForm()
{
	x=document.form1;
	var winMain;
	var vDone = x.Done.value;
	
	x.user_password.focus();
	
	if (vDone == 1) {

		/*winMain = window.open('./contract/Form_Retail_CashUsers.php', 'Form_Retail_CashUsers', 'toolbar=no,location=no,status=yes,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=350,top=100,left=200');		
		winMain.creator = self;
		winMain.focus();	
		
		window.open("","_self");
		window.opener=top;
		window.close();*/
		
		open('./contract/Form_Retail_CashUsers.php', '_self').close();
				
	}	
}

function FillInput(PValue){
	document.form1.user_password.value = document.form1.user_password.value + PValue;
	
}
function EraseInput(){
	document.form1.user_password.value='';
	document.form1.user_password.focus();
}


function SaveChanges() {

	var formvalidate = validateForm();
	
	if (formvalidate) { 
		document.license_form.action="contract/LicenseAction.php?actionmode=activate";
		document.license_form.submit();
	}

}

function validateForm() {
    var x=document.license_form;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
    if (x.companyname.value == "") {
        alert("Company name should be filled.");
		x.companyname.focus();
        return false;
    }
	else if (x.licenseno.value == "") {
        alert("License no should be filled.");
		x.licenseno.focus();
        return false;
    }
	else if (x.email.value == "") {
        alert("Email address should be filled.");
		x.email.focus();
        return false;
    }
	else if (x.telephoneno.value == "" && x.mobile.value == "") {
        alert("Telephone or Mobile should be filled.");
		x.telephoneno.focus();
        return false;
    }
	else if (x.country.value == "") {
        alert("Country should be filled.");
		x.country.focus();
        return false;
    }
	else if (!filter.test(x.email.value)) {
		alert('Please provide a valid email address.');
		x.email.focus();
		return false;
	}   

	return true;
    	
}

</script>

<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;
}
.style11{
	color: #FFFFFF;
	font-size:13px;
	font-family: Arial, Geneva, sans-serif;
	
}
.style22{
	color: #FFFFFF;
	font-weight: bold;
	font-size:16px;
	font-family: Arial, Geneva, sans-serif;
	
}
-->
.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;
    }

.calculatorbutton {
	width:55px;
	height:55px;
	font-weight: bold;
	font-size: 12;
	text-align: center;
	cursor: pointer;
	word-wrap: break-word;
	font-family:arial,sans serif;
	background:#f4f4f4;
	color:#000000;
	outline:0px;
	-moz-border-radius: 0px 5px 5px 5px;
	-webkit-border-radius: 0px 5px 5px 5px;
	border-radius: 0px 10px 5px 10px;
}	
</style>
</head>

<body bgcolor='#0d404f' onload="LoadForm();">

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

<?php if ($LicenseError != "") { ?>
	<div id="rightDiv" style='position:relative; float:right; width:55%; border:0px solid black;' >
<?php } else { ?>
	<div id="rightDiv" style='position:relative; float:left; width:100%; border:0px solid black;' >
<?php } ?>

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

  <tr height='70px'>
	<td colspan="2" align="center" height="20"><span class="style2"><?php echo $msg; ?></span></td>
  </tr>
  <tr height='170px'>
	<td colspan="2" 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">&nbsp;<input type='password' style='font-size:14px;border:0;background-color: transparent;width:200px; border-color:transparent; outline:none;outline-offset: 0;' id='user_password' name='user_password' maxlength='15' value="<?php echo $user_password; ?>"></td>
  </tr>
  
 
  <tr height='130px'>
	<td align="center" valign="top" style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;</td>
    <td >
		<table cellspacing=0 cellpadding=0 border='0' style='padding-top:2px;'>
		<tr>
			<td><input type='button' value='1' class='calculatorbutton' onclick='FillInput(1);' id='button1' onkeydown='return false;'></td>
			<td><input type='button' value='2' class='calculatorbutton' onclick='FillInput(2);' id='button2' onkeydown='return false;'></td>
			<td><input type='button' value='3' class='calculatorbutton' onclick='FillInput(3);' id='button3' onkeydown='return false;'></td>
			<td><input type='button' value='4' class='calculatorbutton' onclick='FillInput(4);' id='button4' onkeydown='return false;'></td>
		</tr>
		<tr>
			<td><input type='button' value='5' class='calculatorbutton' onclick='FillInput(5);' id='button5' onkeydown='return false;'></td>
			<td><input type='button' value='6' class='calculatorbutton' onclick='FillInput(6);' id='button6' onkeydown='return false;'></td>
			<td><input type='button' value='7' class='calculatorbutton' onclick='FillInput(7);' id='button7' onkeydown='return false;'></td>
			<td><input type='button' value='8' class='calculatorbutton' onclick='FillInput(8);' id='button8' onkeydown='return false;'></td>
		</tr>
		<tr>
			<td><input type='button' value='CLR' class='calculatorbutton' onclick='EraseInput(9);' id='button10' onkeydown='return false;'></td>
			<td><input type='button' value='9' class='calculatorbutton' onclick='FillInput(9);' id='button9' onkeydown='return false;'></td>
			<td><input type='button' value='0' class='calculatorbutton' onclick='FillInput(0);' id='button0' onkeydown='return false;'></td>
			<td><input name="submit1" type="submit" id="submit1"  title="Login"  class='calculatorbutton' value="GO" /></td>
		</tr>
		</table>
	</td>
  </tr>
  
  <tr >
	<td colspan="2" align="center" valign="top" style="color: #FF0000; font-size:9px; font-family:Verdana, Arial, Helvetica, sans-serif;">&nbsp;</td>
  </tr>

	
  </table></div>
</form>

<form  enctype="multipart/form-data"  id='license_form' name='license_form' method='post' >

<?php if ($LicenseError != "") { ?>
	<div id="leftDiv" style='position:relative; float:left; width:43%; border:0px solid black;'  >
<?php } else { ?>
	<div id="leftDiv" style='position:relative; float:left; width:43%; border:0px solid black; display:none;'  >
<?php } ?>
<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed;'>
	<tr height='40px'> 
		<td valign='center' align='left' colspan='2' style='border-bottom:1px #CCC solid;'><span class='style22'>License Activation</span></td>
	</tr>
	<tr height='30px'> 
		<td valign='center' align='right' width='100px' style='padding-top:10px;'><span class='style11'>Company Name&nbsp;</span></td>
		<td align='left' width='300px' style='padding-top:10px;'><input type='text' name='companyname' id='companyname' value='<?php echo $_POST['companyname']; ?>' maxlength='100' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>License No&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='licenseno' id='licenseno' value='<?php echo $_POST['licenseno']; ?>' maxlength='16' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>First Name&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='firstname' id='firstname' value='<?php echo $_POST['firstname']; ?>' maxlength='60' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Father Name&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='fathername' id='fathername' value='<?php echo $_POST['fathername']; ?>' maxlength='60' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Family Name&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='familyname' id='familyname' value='<?php echo $_POST['familyname']; ?>' maxlength='60' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Email&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='email' id='email' value='<?php echo $_POST['email']; ?>' maxlength='60' style='font-size:14px; width:200px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Telephone&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='telephoneno' id='telephoneno' value='<?php echo $_POST['telephoneno']; ?>' maxlength='20' style='font-size:14px; width:130px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Mobile&nbsp;</span></td>
		<td align='left' width='300px'><input type='text' name='mobile' id='mobile' value='<?php echo $_POST['mobile']; ?>' maxlength='20' style='font-size:14px; width:130px; height:18px;' /></td>
	</tr>
	<tr height='30px'>
		<td valign='center' align='right' width='100px'><span class='style11'>Country&nbsp;</span></td>
		<td align='left' width='300px'>
			<select name='country' id='country' style='font-size:14px; width:200px; height:23px;'>
				<option value='' selected='selected'>select one</option>
			<?php	
			foreach($config['CountryList'] as $list=>$list_value)
			{
				echo "<option value='" . $list . "'>" . $list_value . "</option>";
			}	
			?>	
			</select>
		</td>
	</tr>
	<tr>
		<td valign='center' align='right' width='100px'><span class='style11'>Address&nbsp;</span></td>
		<td align='left' width='300px'><textarea rows='4' cols='50' name='address' id='address' style='font-size:14px;' ><?php echo $_POST['address']; ?></textarea></td>
	</tr>
	<tr height='35px'><td valign='center' align='right' colspan='2'>&nbsp;</td></tr>
	<tr>
		<td colspan='2' align='right' >
			<button type='button' id='btnPost' name='btnPost' onclick='SaveChanges(); return false;' title='Submit Data'><span><span>Activate</span></span></button>
			<button type='reset' id='btnReset' name='btnReset'><span><span>Reset</span></span></button>
		</td>
	</tr>
</table>
</div>

</form>

</body>
</html>