<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add New User';
$buton = 'Add User';
$cupage = 'user';
$sql1 = "SELECT * FROM ".TBL_USER."";
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 

if(isset($_GET['action']) && $_GET['action'] == 'edit'){
	$headin = 'Edit User';
	$buton = 'Edit User';
	$obj2 = new DBClass();
	$sql = "SELECT * FROM ".TBL_USER." WHERE id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}

if(isset($_POST['submit'])){
/*	$valid = checkName($_POST['fname'], "First Name"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = checkName($_POST['lname'], "Last Name"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = checkEmail($_POST['email'], "Email"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = validmess($_POST['password'], "Password"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = validmess($_POST['address1'], "Address"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = validmess($_POST['city'], "City"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = validmess($_POST['state'], "State"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
    $valid = validmess($_POST['crn'], "CRN"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = validmess($_POST['zip'], "Zip"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
	$valid = checkPhone($_POST['phone'], "Phone"); if(isset($valid) && $valid != ''){ $mess[] = $valid;}
        if($mess != '') { $error[] = 'Please fill all required fields with correct values.';}*/
	if($error == ''){
		if($_POST['submit'] == 'Add User'){
		$sqlr = "SELECT * FROM ".TBL_USER." WHERE email = '".$_POST['email']."'";
		$rsr = $obj->executeQry($sqlr,$link);
		$rowr = $obj->fetchrow();	 

		if(($rowr['email'] == $_POST['email']) && ($_POST['email'] != '')){ $error[] = "This email is already registered.";}

			else {
				$sqll = "INSERT INTO ".TBL_USER." (email,password,company_name,crn,fname,lname,address1,country,city,state,zip,phone,fax,register_date,aprove) VALUES ('".trim($_POST['email'])."','".trim($_POST['password'])."','".trim($_POST['company_name'])."','".trim($_POST['crn'])."','".trim($_POST['fname'])."','".trim($_POST['lname'])."','".trim($_POST['address1'])."','".trim($_POST['country'])."','".trim($_POST['city'])."','".trim($_POST['state'])."','".trim($_POST['zip'])."','".trim($_POST['phone'])."','".trim($_POST['fax'])."','".$dat."','0')";
				$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: users.php?msg=add');
				}
			}
		}
	
		if($_POST['submit'] == 'Edit User'){
			$sqll = "UPDATE ".TBL_USER." SET email = '".trim($_POST['email'])."', password = '".trim($_POST['password'])."', company_name = '".$_POST['company_name']."', crn = '".$_POST['crn']."', fname = '".$_POST['fname']."', lname = '".$_POST['lname']."', address1 = '".$_POST['address1']."', country = '".$_POST['country']."', city = '".$_POST['city']."', state = '".$_POST['state']."', zip = '".$_POST['zip']."', phone = '".$_POST['phone']."', fax = '".$_POST['fax']."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: users.php?msg=edit');
			}
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="users.php">Manage Users</a></td>
    <td align="center" class="submenu">Add New User</td>
    <td align="center" class="submenu"><a href="users.php?stat=unaprove">Users Waiting Aproval</a></td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35"><?php echo $headin;?></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF" class="innermain"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
	<form name="pageform" id="pageform" method="post" action="">
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">Name & Address</td>
                </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">First Name : </td>
                    <td align="left" width="70%"><input type="text" name="fname" id="fname" value="<?php echo ($_POST['fname'] == '') ? $row['fname'] : $_POST['fname']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right" width="30%">Last Name : </td>
                    <td align="left" width="70%"><input type="text" name="lname" id="lname" value="<?php echo ($_POST['lname'] == '') ? $row['lname'] : $_POST['lname']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right" width="30%">Company Name : </td>
                    <td align="left" width="70%"><input type="text" name="company_name" id="company_name" value="<?php echo ($_POST['company_name'] == '') ? $row['company_name'] : $_POST['company_name']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right" width="30%">CRN : </td>
                    <td align="left" width="70%"><input type="text" name="crn" id="crn" value="<?php echo ($_POST['crn'] == '') ? $row['crn'] : $_POST['crn']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">Address:</td>
                    <td><input type="text" name="address1" id="address1" value="<?php echo ($_POST['address1'] == '') ? $row['address1'] : $_POST['address1']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">Country:</td>
                    <td><input type="text" name="country" id="country" value="<?php echo ($_POST['country'] == '') ? $row['country'] : $_POST['country']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">City:</td>
                    <td><input type="text" name="city" id="city" value="<?php echo ($_POST['city'] == '') ? $row['city'] : $_POST['city']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">State:</td>
                    <td><input type="text" name="state" id="state" value="<?php echo ($_POST['state'] == '') ? $row['state'] : $_POST['state']; ?>" /></td></td>
                    </tr>
                  <tr>
                    <td align="right">Zip:</td>
                    <td><input type="text" name="zip" id="zip" value="<?php echo ($_POST['zip'] == '') ? $row['zip'] : $_POST['zip']; ?>" /></td>
                    </tr>
                  </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">Contact Details</td>
              </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">Phone: </td>
                    <td width="70%"><input type="text" name="phone" id="phone" value="<?php echo ($_POST['phone'] == '') ? $row['phone'] : $_POST['phone']; ?>" /></td>
                  </tr>
                  <tr>
                    <td valign="top" align="right">Fax: </td>
                    <td><input type="text" name="fax" id="fax" value="<?php echo ($_POST['fax'] == '') ? $row['fax'] : $_POST['fax']; ?>" /></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">Login Details</td>
              </tr>
              <tr>
                <td align="left" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">Email:</td>
                    <td width="70%"><input type="text" name="email" id="email" value="<?php echo ($_POST['email'] == '') ? $row['email'] : $_POST['email']; ?>" /></td>
                  </tr>
                  <tr>
                    <td valign="top" align="right">Password: </td>
                    <td><input type="password" name="password" id="password" value="<?php echo ($_POST['password'] == '') ? $row['password'] : $_POST['password']; ?>" /></td>
                  </tr>
                  </table></td>
              </tr>
              <tr>
                <td align="right" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td align="center"><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $buton;?>" /></td>
                  </tr>
                  </table></td>
              </tr>
</form>
              </table></td>
          </tr>
          </table></td>
      </tr>
      </table></td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>