<?php session_start();  //Developed by ITechScripts
?>
<?
require 'include/configurations.php'; // ITECHBIDS Configuration File 
require '../include/itechbids.php'; // ITECHBIDS Configuration File 
$mode=$_GET['mode'];
$userid=$_GET['id'];
if($mode=='edit')
{
$gresult=mysql_query("select * from member_details where user_id=$userid");
$grow=mysql_fetch_array($gresult);
}
$name=$_POST['cboStyle'];
$cansave=$_POST['cansave']; 
$member_type=$_POST['member_type'];
$nowplan=member_tp($userid);
if($cansave==1)
{
$username=$_POST['txtUname'];
$pass=$_POST['txtPass'];
$confirm=$_POST['txtConfirm'];
$email=$_POST['txtEmail'];
$city=$_POST['txtCity'];
$country=$_POST['cboCountry'];
$state=$_POST['cboState'];
$date=date('Y-m-d');

 $firstname=$_POST['firstname'];
 $lastname=$_POST['lastname'];
 $address=$_POST['address'];
 $pincode=$_POST['pincode'];
 $homephone=$_POST['homephone'];
 $workphone=$_POST['workphone'];
 $password=$_POST['password'];
 $pvt_per=$_POST['permission'];
$mem_plans=$_POST[mem_plans];
//------------validatating variables--------------------------
if($firstname=="")
$fnflag=1;
if($lastname=="")
$lnflag=1;
if($address=="")
$addflag=1;
/* if($pincode=="")
$pinflag=1; */
if($homephone=="")
$homephoneflag=1;
if($workphone=="")
$workphoneflag==1;
//if($password=="")
//$passflag=1;



if($username=="")
$uflag=1;
$a=strcmp($pass,$confirm);
if($a!=0)
{
$pcflag=1;
$pmessage="Please re-enter your password correctly";
}
if($email=="")
$eflag=1;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
{
$evflag=1;
$emessage="Email invalid";
}
if($city=="")
$ctflag=1;

if($mode=='edit')
{
if($uflag!=1 && $eflag!=1 && $evflag!=1 && $ctflag!=1 &&  $fnflag!=1 && $lnflag!=1 && $addflag!=1 && $homephoneflag!=1 && $workphoneflag!=1 && $passflag!=1)
{  

$duration = site_plans($mem_plans, 'duration');
function addDay($date,$interval) 
  { 
  if (!isset($date)) 
  $date = date("Y-m-d"); 
  $elts = explode("-", $date); 
  $inter=$interval*24*3600; 
  $dcour=mktime(1,0,0,$elts[1],$elts[2],$elts[0]); 
  $dres=$dcour+$inter; 
  $date1=date("Y-m-d",$dres);
  $sec=date("G:i:s");
  $ret_date="$date1"." "."$sec";
  return $ret_date; 
}
if($duration!=0)
{
$start_date=date("y-m-d");
$expir_date= addDay($start_date,$dur);  
}
if($duration==0)
{
$expir_date = "";
}
$nowplan=member_tp($userid);
if(($nowplan==$mem_plans)&&($duration!=0))
{
$tuser_sql="select exp_date as mt from f_members where userid=$userid";
$tuser_res=mysql_query($tuser_sql);
$tuser_rec=mysql_fetch_array($tuser_res);
$exp_date=$tuser_rec['mt'];
$expir_date= addDay($exp_date,$duration); 
}








$up_sql="update member_details set user_name='$username', member_account='$member_type' ,email='$email',city='$city',country='$country',";
 $up_sql.="first_name='$firstname', last_name='$lastname', address='$address', pin_code='$pincode',";
 $up_sql.=" home_phone='$homephone' , work_phone='$workphone' , postpermission='$pvt_per' where user_id=$userid ";
  $up_exe=mysql_query($up_sql);
$query="update f_members set status='Active', start_date=now(), exp_date='$expir_date', fee_id=$mem_plans where user_id=$userid";
mysql_query($query);

$up_flag=1;

echo '<meta http-equiv="refresh" contents="0;url=user.php">';

}
else
$message="Please fill in the fields marked in red";
}
else
{
if($uflag!=1 && $pflag!=1 && $pcflag!=1 && $eflag!=1 && $evflag!=1 && $ctflag!=1 && $coflag!=1 && $stflag!=1)
{  
   	$sql="insert into member_details (user_name,password,email,city,state,country,joined,verify,status) values('$username','$pass','$email','$city',$state,$country,'$date','yes','active')";
	$result=mysql_query($sql);
	if($result)
	$message="user added sucessfully";
	else
	$message="Sorry, user registration failed";
}
else
$message="Please fill in the fields marked in red";
}

}
?>
>
<style type="text/css">
<!--
.style1 {
	color: #666666;
	font-weight: bold;
}
.style3 {color: #666666; font-size: 11px; font-family:Arial, Helvetica, sans-serif}
-->
</style>
  <? require 'include/header.php'; ?><table width="80%">
  <tr><td align="center" colspan="10"><font size=2 color=red><? if($up_flag==1) echo "Updated Successfully"; ?></font></td></tr>
  <tr><form name="frm" method="post" onSubmit="this.cansave.value=1;return true();">
    <td colspan="8">
	<table width="80%" align="center" cellpadding="5" cellspacing="2" class="ft">
	<tr><td colspan="2" class="ct" height="25">Edit User Information</td></tr>
	<tr><td colspan="2" align="center">
	<?php 
	echo $message;
	echo '<br>';
	if($pmessage!='' && $emessage!="")
	{
	?>
	<li style="list-style-type:disc"><?=$pmessage ?></li>
	<li style="list-style-type:disc"><?=$emessage ?></li>
	<?php
	}
	else
	{
		if($pmessage) echo $pmessage.'<br>'; 
		if($emessage) echo $emessage;
	}
	?>
	<?
	if($mode=='edit')
{
$gresult=mysql_query("select * from member_details where user_id=$userid");
$grow=mysql_fetch_array($gresult);
}
?>
	</td></tr>
	<tr><td><?php if($uflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Username</font></td>
	<td><input type="text" class="bluetext"name="txtUname" value="<?php if($mode=='edit') echo $grow['user_name']; else echo $username ?>" style="<?=$text ?>"></td></tr>
		<tr><td><?php if($fnflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">First Name</font></td>
	<td><input type="text" class="bluetext"name="firstname" value="<?php if($mode=='edit') echo $grow['first_name']; else echo $firstname ?>" style="<?=$text ?>"></td></tr>
	
	
	<tr><td><?php if($lnflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Last Name</font></td>
	<td><input type="text" class="bluetext"name="lastname" value="<?php if($mode=='edit') echo $grow['last_name']; else echo $lasstname ?>" style="<?=$text ?>"></td></tr>
	
	
	<tr><td><?php if($addflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Address</font></td>
	<td><input type="text" class="bluetext"name="address" value="<?php if($mode=='edit') echo $grow['address']; else echo $address ?>" style="<?=$text ?>"></td></tr>
	
	<tr><td><?php if($pinflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Pincode</font></td>
	<td><input type="text" class="bluetext"name="pincode" value="<?php if($mode=='edit') echo $grow['pin_code']; else echo $pincode ?>" style="<?=$text ?>"></td></tr>

	<tr><td><?php if($homephoneflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Home Phone</font></td>
	<td><input type="text" class="bluetext"name="homephone" value="<?php if($mode=='edit') echo $grow['home_phone']; else echo $homephone ?>" style="<?=$text ?>"></td></tr>

	<tr><td><?php if($workphoneflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Work Phone</font></td>
	<td><input type="text" class="bluetext"name="workphone" value="<?php if($mode=='edit') echo $grow['work_phone']; else echo $workphone ?>" style="<?=$text ?>"></td></tr>
	

	
	<tr><td><?php if($pflag==1 || $pcflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<? if($mode!='edit')
	{
	?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Password</font></td>
	<td><input type="password" name="txtPass" style="<?=$text ?>"></td></tr>
	<tr><td><?php if($cflag==1 || $pcflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Confirm password</font></td>
	<td><input type="password" name="txtConfirm" style="<?=$text ?>"></td></tr>
	<?
	}
	?>
	
	<tr><td><?php if($eflag==1 || $evflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Email</font></td><td><input type="text" class="bluetext"name="txtEmail" value="<?php if($mode=='edit') echo $grow['email']; else echo $email ?>" style="<?=$text ?>"></td></tr>
	<tr><td><?php if($ctflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">City</font></td><td><input type="text" class="bluetext"name="txtCity" value="<?php if($mode=='edit') echo $grow['city']; else echo $city ?>" style="<?=$text ?>"></td></tr>
	<tr><td><?php if($coflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Country</font></td>
	<td><select name="cboCountry" onChange="Choosestate()" style="font-family:<?=$font?>; font-size:12 ">
	<option value="">Country</option>
	<?php
	$cres=mysql_query("select *from site_countries");
	while($crow=mysql_fetch_array($cres)){
	if($mode=='edit' && $crow['country_id']==$grow['country'])
	echo '<option value='.$crow['country_id'] .' selected>'.$crow['country'].'</option>';
	else
	if($crow['country_id']==$country)
	echo '<option value='.$crow['country_id'] .' selected>'.$crow['country'].'</option>';
	else echo '	<option value='.$crow['country_id'].'>'.$crow['country'].'</option>';
	}
	?>
	</select></td></tr>
	<tr><td><?php if($stflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?><font face="<?=$font; ?>" size="<?=$size; ?>">State</font></td>
	<td><select name="cboState" style="font-family:<?=$font?>;font-size:12">
	<option value="">State</option>
	<?php
	$sres=mysql_query("select *from site_states");
	while($srow=mysql_fetch_array($sres)){
	if($mode=='edit' && $srow['state_id']==$grow['state'])
	echo '<option value='.$srow['state_id'] .' selected>'.$srow['state'].'</option>';
	if($srow['state_id']==$state)
	echo '<option value='.$srow['state_id'] .' selected>'.$srow['state'].'</option>';
	else echo '	<option value='.$srow['state_id'].'>'.$srow['state'].'</option>';
	}
	?>
	</select></td></tr>
	<tr><td><?php if($cflag==1 || $pcflag==1) echo '<font color=#ff0000>'; else echo '<font color=#000000>'; ?>
	<font face="<?=$font; ?>" size="<?=$size; ?>">Selling Permission</font></td>
	<? $type=$grow['postpermission']; ?>
	<td><input type="radio" value="yes" name="permission" <? if($type=='yes') echo "checked" ?>>Yes
	    <input type="radio" value="no" name="permission" <? if($type=='no') echo "checked" ?>>no
	</td>
	</tr>
 <tr><td width="293"><b><font size=2>Select Plan</font></b></td>
 <td>
 <?
 $fee_query="select * from site_fees where status='Active'";
$fee_table=mysql_query($fee_query);
?>
  <select name=mem_plans>
 <? 
for($i=1; $fee_row=mysql_fetch_array($fee_table); $i++)
 {
$admin_id = $fee_row[admin_id];
$duration = $fee_row[duration];
$registration_fee = $fee_row[registration_fee];
$scheme= $fee_row[scheme];
if($duration==0)
{
$duration = "Unlimited";
}
?>
   <option value=<?= $admin_id ?>
<? if($nowplan==$admin_id){ 
echo " selected";
}
?>
> <?= $scheme ?>, <?=$registration_fee?> <?= $default_cur_code ?>/<?=$duration?> Days </option>
<?
}
?>
</select>
</td></tr>
	<tr><td align="center" colspan="2">
	<input type="hidden" name="cansave" value="0">
	<input type="submit" class="button"   name="submit"  value="<?php if($mode=='edit') echo 'Update'; else echo 'Register'; ?>" style="<?=$button ?>"></td></tr>
	</table></td>
    </form></tr></table>
	<tr align="center">
<td colspan="9"><? require 'include/footer.php'?></td></tr>
</center>
<script language="JavaScript">
function Choosestate() {
var country;
country=document.frm.cboCountry.value;
state=document.frm.cboState;
len = state.length;
for(i=0;i<len;i++) {
state.remove(state.options[i]);
}
if(country!='') {<?php

//------------------Selecting all State from the Database-------------------------------------------

$sql = "select * from site_states";
$result = mysql_query($sql);

//------------------Declaring array variables and storing the state values into it-------------------------------------------

echo "var countryid = new Array;";
echo "var stateid= new Array;";
echo "var statename= new Array;";
$i = 0;
while($row = mysql_fetch_array($result))
{
	$sid = $row[0];
	$cid=$row[2];
	$sname=$row[1];
	$sname=addslashes($sname);
	echo "countryid[$i] = '$cid';";
	echo "stateid[$i]= '$sid';";
	echo "statename[$i] = '$sname';";
	$i++;
	}
	?>
j = 1;

//------------------Adding State for the selected Country to the Combo box-------------------------------------------


for(i=0;i<countryid.length;i++) 
{
if(countryid[i]==country) {
state.options[j]=new Option(statename[i],stateid[i]);
j++;
}	
}
}
}

//------------------End of Function -------------------------------------------
</script>