<?php 
$active = 'Sell Fuel';
$pageName = 'sellcoal';

require_once('config/config.php');
require_once(CLASS_DIR.'dbClass.php');
require_once(INCLUDES_DIR.'header.php');

include('hitCounter.php');
include('checklogin.php');

$obj = new DBClass();
$obj1 = new DBClass();
$obj3 = new DBClass();
$obj2 = new DBClass();
$obj9 = new DBClass();

// TO GET CRN NUMBER
$sql9 = "SELECT * FROM ".TBL_USER." WHERE (email = '".$_COOKIE['email']."' OR email = '".$_SESSION['email']."')";
$rs9 = $obj9->executeQry($sql9,$link);
$row9 = $obj9->fetchrow();


$email = ($_SESSION['email']=='')?$_COOKIE['email']:$_SESSION['email'];

if(isset($_POST['submit']) && $_POST['submit'] == 'Next'){
	$crn = strlen($row9['crn']);	
	if($crn == '9'){
	 @header('Location: sellcoal_step2.php');
	 exit;
	}               
	else{ 
	@header('Location: sellcoal.php?action=done');   
	exit;
	}
}

if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
	
		$valid = validmess($_POST['name'], "Name"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['organisation'], "Organisation"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['address'], "Address"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['district'], "City"); if(isset($valid) && $valid != ''){ $error[] = $valid;}        
        $valid = validmess($_POST['category_of_service'], "Category of service"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['state'], "State"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = validmess($_POST['pincode'], "Pin"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = checkEmail($_POST['email1'], "Email"); if(isset($valid) && $valid != ''){ $error[] = $valid;}	
        $valid = validmess($_POST['phone'], "Phone"); if(isset($valid) && $valid != ''){ $error[] = $valid;}	
        $valid = validmess($_POST['mobile1'], "Mobile"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
		
		$name = ChangeFormat($_POST['name']);
		$organisation = ChangeFormat($_POST['organisation']);
		$address = ChangeFormat($_POST['address']);
		
		if(($_POST['country'] == 'other')) {            
          $valid = validmess($_POST['othercountry'], "Country"); if(isset($valid) && $valid != ''){ $mess[] = $valid;} 
          $country = $_POST['othercountry'];
        }
        else {
          $country = $_POST['country'];
        }
        
        if(($_POST['state'] == 'other')) {            
          $valid = validmess($_POST['otherstate'], "State"); if(isset($valid) && $valid != ''){ $mess[] = $valid;} 
          $state = $_POST['otherstate'];
        }
        else {
          $state = $_POST['state'];
        }
        
        if(($_POST['district'] == 'other')) {            
          $valid = validmess($_POST['othercity'], "City"); if(isset($valid) && $valid != ''){ $mess[] = $valid;} 
          $city = $_POST['othercity'];
        }
        else {
          $city = $_POST['district'];
        }
		
		$cos1='';
		$cos2='';
		$cos3='';
		foreach ($_POST['category_of_service'] as $cos)
		{
				if(!$cos1)
				{
				$cos1 = $cos;
				}
				else
				{
					if(!$cos2)
						{
							$cos2=$cos;
						}
					else{
							$cos3=$cos;
						}
				}
		}
		
		if($error == ''){
			
			$sql = "SELECT * FROM ".TBL_THEME." WHERE (memberID = '".$_COOKIE['memberID']."' OR memberID = '".$_SESSION['memberID']."')";
			$rs = $obj->executeQry($sql,$link);
			$memberID = ($_SESSION['memberID']=='')?$_COOKIE['memberID']:$_SESSION['memberID'];
			if($obj->getRow() < 1){		
				$sql2 = "INSERT INTO ".TBL_THEME." (themeID,memberID) VALUES ('0005','".$memberID."')";
				$rs2 = $obj2->executeQry($sql2,$link);	
			}
						
			$sqll = "INSERT INTO ".TBL_DIRECTORY." (person_name,organisation_name,crn,category_of_service,category_of_service2,category_of_service3,address,district,state,country,pincode,phone,fax,mobile1,mobile2,email1,email2,memberID,added_on,company_details,website) VALUES ('".trim($name)."','".trim($organisation)."','".trim($row9['crn'])."','".$cos1."','".$cos2."','".$cos3."','".trim($address)."','".trim($city)."','".trim($state)."','".trim($country)."','".trim($_POST['pincode'])."','".trim($_POST['phone'])."','".trim($_POST['fax'])."','".trim($_POST['mobile1'])."','".trim($_POST['mobile2'])."','".trim($_POST['email1'])."','".trim($_POST['email2'])."','".$memberID."','".date("Y-m-d")."','".$_POST['company_details']."','".$_POST['website']."') ";
			$rs2 = $obj->executeQry($sqll,$link);	
			
			if($rs2){
				$crn = strlen($row9['crn']);	
				if($crn == '9'){
			     @header('Location: sellcoal_step2.php');
			     exit;
			    }               
                else{ 
			    @header('Location: sellcoal.php?action=done');   
				exit;
                }
			}
		}
}

?>
<script type="text/javascript" src="citySelection.js"></script>
<script src="JqueryLoader.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.5/mootools-yui-compressed.js"></script>



<script type="text/javascript">
	function checkCat(thisform,limit)
	{
			var c=0;
			for(var i=0; i< thisform.options.length; i++)
			{
					if(thisform.options[i].selected && thisform.options[i].value != "")
					{
							c++;
					}
			}
		if(c>0 && thisform.options[0].value=="")
			thisform.options[0].selected = false;	
			
			if(c>limit && limit)
			{
					alert("You can select "+limit+" categories only !");
					for(var i=0; i< thisform.options.length; i++)
					{
							if(thisform.options[i].selected)
							{
									thisform.options[i].selected = false;
									thisform.focus();
							}
					}
			}
	}
</script>

<!--workarea start------------------------->
<div class="workarea">
	<div class="workareatop">
		<img src="images/workareatopcorner_03.PNG" alt="" />
	</div>
	<!--workareamid start--------------------->
	<div class="workareamid">
		<div id="floatleft" style="width:590px; float:left;">
			<div class="welcomediv">
			
                        <h2>Directory Entry</h2>
<div id="preloader"><img src="ajax-loader.gif"  style="border:none;"/></div>
<div class="err_msg"><?php ShowError($error); ShowInfo($info);?></div>

<?php if(isset($_GET['action']) && $_GET['action'] == 'done') { ?>
		<div>Thanks for submitting Directory Entry.</div>
<?php } else {

$sql = "SELECT * FROM ".TBL_DIRECTORY." WHERE (memberID = '".$_COOKIE['memberID']."' OR memberID = '".$_SESSION['memberID']."')";
$rs = $obj->executeQry($sql,$link);
$row1 = $obj->fetchrow();
?>
	
<div class="contactform">
	<form action="" method="POST">
		<table>
			<tr>
				<td align="left" class="formtxt">Name of the contact person<span class="style1">*</span></td>
				<td align="left" class="form_field"><input name="name" type="text" class="txt_box2" <?php if(isset($row1['person_name']) || $row1['person_name'] != '') { ?>readonly="readonly"<?php } ?> value="<?php echo ($_POST['name'] == '') ? $row1['person_name'] : $_POST['name']; ?>" style="width:270px;" /></td>
			</tr>
			<tr>
				<td align="left" class="formtxt">Organisation<span class="style1">*</span></td>
				<td><input name="organisation" type="text" class="txt_box2" <?php if(isset($row1['organisation_name']) || $row1['organisation_name'] != '') { ?>readonly="readonly"<?php } ?>  value="<?php echo ($_POST['organisation'] == '') ? $row1['organisation_name'] : $_POST['organisation']; ?>" style="width:270px" /></td>
			</tr>
			<tr>
				<td align="left" class="formtxt">Customer Receipt Number<span class="style1">*</span></td>
				<td><input name="crn" type="text" class="txt_box2" <?php if(isset($row9['crn']) || $row9['crn'] != '') { ?>readonly="readonly" <?php } ?>  value="<?php echo $row9['crn'];?>" style="width:270px"></td>
			</tr>
			<tr>
				<td align="left" class="formtxt">Category of Services<span class="style1">*</span></td>
				<td><?php if(isset($row1['category_of_service']) || $row1['category_of_service'] != '') { ?> 
					<input name="category_of_service" type="text"  style="width:270px" class="txt_box2" readonly="readonly"  value="<?php echo $row1['category_of_service']. " , " .$row1['category_of_service2'] . " , " . $row1['category_of_service3'];?>"/><?php } else { ?>
					
					<select name="category_of_service[ ]" class="txt_box2"  style="width:270px" onchange="return checkCat(this,3);" multiple>
						<?php 						
						$sql2 = "SELECT * FROM ".TBL_OPTIONS." where `option`='categoryofservices' and `value`!='' and `status`=0 ORDER BY value ASC";						
						$rs2 = $obj1->executeQry($sql2,$link);						
						while($row2 = $obj1->fetchrow()){ 
						?>          
						<option value="<?php echo $row2['value'];?>" <?php if($_POST['category_of_service'] == $row2['value']) echo 'selected'; ?>><?php echo $row2['value'];?></option>
		
		<?php } ?>
			</select><?php } ?></td>
			</tr>
			<tr <?php if(isset($row1['address']) || $row1['address'] != '') { ?>style="display:none"<?php } ?>>
				<td colspan="2" align="right"><font size="1">Select Maximum 3 category using &quot;ctrl&quot; key</font></td>
			</tr>
			<tr>
				<td>Address<span class="style1">*</span></td>
				<td><input name="address" type="text" class="txt_box2" <?php if(isset($row1['address']) || $row1['address'] != '') { ?>readonly="readonly" <?php } ?>  value="<?php echo ($_POST['address'] == '') ? $row1['address'] : $_POST['address']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Country<span class="style1">*</span></td>
				<td><?php if(isset($row1['country']) || $row1['country'] != '') { ?>
							<input name="country" type="text" class="txt_box2" readonly="readonly" value="<?php echo $row1['country'];?>"  style="width:270px"/>
						<?php } else { ?>
					 <select id="country" name="country" class="txt_box2" onblur="change_country1(this.value)"  style="width:270px">
								<option value="">--Select Country--</option>
									<?php 									
									$obj1 = new DBClass();									
									$sql2 = "SELECT * FROM ".TBL_OPTIONS." where `option`='Country' and `value`!='' and `status`=0 ORDER BY value ASC";									
									$rs2 = $obj1->executeQry($sql2,$link);									
									while($row2 = $obj1->fetchrow()){ 									
									?>          
							<option value="<?php echo $row2['value'];?>" <?php if($_POST['country'] == $row2['value']) echo 'selected'; ?>><?php echo $row2['value'];?></option>
									<?php } ?>
							<option value="other">other</option> 
					</select>  
				<?php } ?>
				</td>
			</tr>
			<tr id="othercountry" <?php if($_POST['country']!='other') { ?> style="display:none"<?php } ?>>
                <td>Country Name <span class="style1">*</span></td>
                <td><input type="text" name="othercountry"  class="txt_box2"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>State<span class="style1">*</span></td>
				<td><?php if(isset($row1['state']) || $row1['state'] != '') { ?>
							<input name="state" type="text" class="txt_box2" readonly="readonly" value="<?php echo $row1['state'];?>" style="width:270px"/>
						<?php } else { ?>
					<select id="state" name="state" class="txt_box2" onchange="change_state(this.value)"  style="width:270px">
								<option value="">--Select State--</option>
									<?php 
									$obj2 = new DBClass();																		
									$sql2 = "SELECT DISTINCT State FROM ".TBL_STATE." WHERE country = 'India' ORDER BY State ASC";										
									$rs2 = $obj2->executeQry($sql2,$link);										
									while($row2 = $obj2->fetchrow()){ 
									?> 
								<option value="<?php echo $row2['State']; ?>" <?php if($_POST['state'] == $row2['State']) { ?>selected="selected"<?php } ?>><?php echo $row2['State']; ?></option>

								<?php } ?>
								<option value="other">other</option> 
					</select>
					<?php } ?>
				</td>
			</tr>
			<tr id="otherstate" <?php if($_POST['state']!='other') { ?> style="display:none"<?php } ?>>
                <td>State Name <span class="style1">*</span></td>
                <td><input type="text" name="otherstate"  class="txt_box2"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>District<span class="style1">*</span></td>
				<td><?php if(isset($row1['district']) || $row1['district'] != '') { ?>
							<input name="district" type="text" class="txt_box2" readonly="readonly" value="<?php echo $row1['district'];?>" style="width:270px"/>
						<?php } else { ?>
						<select  id="city" name="district" class="txt_box2" onblur="change_city(this.value)"  style="width:270px">
									<option value="">--Select District--</option>
									<?php                     
									$obj1 = new DBClass();								
									$sql2 = "SELECT * FROM ".TBL_STATE." where `city`!='' ORDER BY city ASC";								
									$rs2 = $obj1->executeQry($sql2,$link);								
									while($row2 = $obj1->fetchrow()){ 								
									?>
									<option value="<?php echo $row2['city'];?>" <?php if($_POST['district'] == $row2['city']) { ?>selected="selected"<?php } ?>><?php echo $row2['city'];?></option>
									<?php } ?>
									<option value="other">other</option> 
						</select> 
						<?php } ?>
				</td>
			</tr>
			<tr id="othercity" <?php if($_POST['city']!='other') { ?> style="display:none"<?php } ?>>
                <td>District Name <span class="style1">*</span></td>
                <td><input type="text" name="othercity"  class="txt_box2"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Pin Code<span class="style1">*</span></td>
				<td><input name="pincode" type="text" class="txt_box2" <?php if(isset($row1['pincode']) || $row1['pincode'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['pincode'] == '') ? $row1['pincode'] : $_POST['pincode']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Phone No.<span class="style1">*</span></td>
				<td><input name="phone" type="text" class="txt_box2" <?php if(isset($row1['phone']) || $row1['phone'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['phone'] == '') ? $row1['phone'] : $_POST['phone']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Fax No.</td>
				<td><input name="fax" type="text" class="txt_box2" <?php if(isset($row1['fax']) || $row1['fax'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['fax'] == '') ? $row1['fax'] : $_POST['fax']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Mobile No.<span class="style1">*</span></td>
				<td><input name="mobile1" type="text" class="txt_box2" <?php if(isset($row1['mobile1']) || $row1['mobile1'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['mobile1'] == '') ? $row1['mobile1'] : $_POST['mobile1']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Alternate Mobile No.</td>
				<td><input name="mobile2" type="text" class="txt_box2" <?php if(isset($row1['mobile2']) || $row1['mobile2'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['mobile2'] == '') ? $row1['mobile2'] : $_POST['mobile2']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Email Id<span class="style1">*</span></td>
				<td><input name="email1" readonly="readonly" type="text" class="txt_box2" value="<?php echo $email?>" style="width:270px"/></td>
			</tr>
			<tr>
				<td>Alternate Email Id</td>
				<td><input name="email2" type="text" class="txt_box2" <?php if(isset($row1['email2']) || $row1['email2'] != '') { ?>readonly="readonly"<?php } ?> value="<?php echo ($_POST['email2'] == '') ? $row1['email2'] : $_POST['email2']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td>Website</td>
				<td align="left" class="formtxt"><input name="website" type="text" class="txt_box2" <?php if(isset($row1['website']) || $row1['website'] != '') { ?>readonly="readonly" <?php } ?> value="<?php echo ($_POST['website'] == '') ? $row1['website'] : $_POST['website']; ?>"  style="width:270px"/></td>
			</tr>
			<tr>
				<td valign="top">About your company</td>
				<td align="left" class="formtxt"><textarea class="txt_box2"  rows="5" name="company_details"  style="width:270px"><?php echo ($_POST['company_details'] == '') ? $row1['company_details'] : $_POST['company_details']; ?></textarea></td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2"><input name="submit" type="submit" class="login_btn"  <?php if(isset($row1['person_name']) || $row1['person_name'] != '') { ?>value="Next"<?php } else {?> value="Submit" <?php } ?>/></td>
			</tr>
		</table>
	</form>
</div>
<?php } ?>


						</div>
				</div>
				<?php require_once(INCLUDES_DIR.'left_bar.php');?>
                <!--floatright close-------------------->
                <div style="clear:both;"></div>
            </div>
            <!--workareamid close--------------------->
            <div class="workareabottom">
                <img src="images/workareabottomcorner_03.PNG" alt="" />
            </div>
        </div>
    <!--workarea close------------------------->
<?php require_once(INCLUDES_DIR.'footer.php');?>