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

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

if(isset($_POST['submit'])){
        $valid = validmess($_POST['name'], "Name"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['email'], "Email"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        if($_POST['email'] != ''){
        $valid = checkEmail($_POST['email'], "Email"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	}
        if($_POST['phone'] != ''){
        //$valid = checkPhone($_POST['phone'], "Phone"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	}
        if($_POST['fax'] != ''){
        //$valid = checkPhone($_POST['fax'], "Fax"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	}

	if($error == ''){
		if($_POST['submit'] == 'Add Contact Details'){
			$sqll = "INSERT INTO ".TBL_CONTACT." (name,address,addres1,city,state,zip,phone,phone1,mobile,mobile1,fax,email) VALUES ('".trim($_POST['name'])."','".trim($_POST['address'])."','".trim($_POST['addres1'])."','".trim($_POST['city'])."','".trim($_POST['state'])."','".trim($_POST['zip'])."','".trim($_POST['phone'])."','".trim($_POST['phone1'])."','".trim($_POST['mobile'])."','".trim($_POST['mobile1'])."','".trim($_POST['fax'])."','".trim($_POST['email'])."') ";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: contact.php?msg=add');
			}
		}
	
		if($_POST['submit'] == 'Edit Contact Details'){
			$sqll = "UPDATE ".TBL_CONTACT." SET name = '".$_POST['name']."', address = '".$_POST['address']."', city = '".$_POST['city']."', state = '".$_POST['state']."', zip = '".$_POST['zip']."', phone = '".$_POST['phone']."', addres1 = '".$_POST['addres1']."', phone1 = '".$_POST['phone1']."', mobile = '".$_POST['mobile']."', mobile1 = '".$_POST['mobile1']."', fax = '".$_POST['fax']."', email = '".$_POST['email']."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: contact.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="contact.php">Contact Details</a></td>
    <td align="center" class="submenu">Add New Contact</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 And 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%">Name: </td>
                    <td align="left" width="70%"><input type="text" name="name" id="name" value="<?php echo ($_POST['name'] == '') ? $row['name'] : $_POST['name']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">Address:</td>
                    <td><input type="text" name="address" id="address" value="<?php echo ($_POST['address'] == '') ? $row['address'] : $_POST['address']; ?>" /></td>
                    </tr>
					<tr>
                    <td align="right">Address2:</td>
                    <td><input type="text" name="addres1" id="addres1" value="<?php echo ($_POST['addres1'] == '') ? $row['addres1'] : $_POST['addres1']; ?>" /></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']; ?>" />
                    <?php /*?><select name="state">
			<option value="">Select</option>
<?php 
$obj1 = new DBClass();
$sql2 = "SELECT * FROM ".TBL_STATE." ";
$rs2 = $obj1->executeQry($sql2,$link);
while($row2 = $obj1->fetchrow()){ 
?>          
			<option value="<?php echo $row2['StateName'];?>" <?php if(($row['state'] == $row2['StateName']) || ($_POST['state'] == $row2['StateName'])) echo 'selected'; ?>><?php echo $row2['StateName'];?></option>
<?php } ?>
			</select><?php */?>
			</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%">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 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 align="right" width="30%">Alternate Phone:</td>
                    <td width="70%"><input type="text" name="phone1" id="email" value="<?php echo ($_POST['phone1'] == '') ? $row['phone1'] : $_POST['phone1']; ?>" /></td>
                  </tr>
				  <tr>
                    <td align="right" width="30%">Mobile:</td>
                    <td width="70%"><input type="text" name="mobile" id="mobile" value="<?php echo ($_POST['mobile'] == '') ? $row['mobile'] : $_POST['mobile']; ?>" /></td>
                  </tr>
                  <tr>
                    <td align="right" width="30%">Alternate Mobile: </td>
                    <td width="70%"><input type="text" name="mobile1" id="mobile1" value="<?php echo ($_POST['mobile1'] == '') ? $row['mobile1'] : $_POST['mobile1']; ?>" /></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 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); ?>