<?php
/*================================================================+\
|| # PHPRetro - An extendable virtual hotel site and management
|+==================================================================
|| # Copyright (C) 2009 Yifan Lu. All rights reserved.
|| # http://www.yifanlu.com
|| # Parts Copyright (C) 2009 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|| # All images, scripts, and layouts
|| # Copyright (C) 2009 Sulake Ltd. All rights reserved.
|+==================================================================
|| # PHPRetro is provided "as is" and comes without
|| # warrenty of any kind. PHPRetro is free software!
|| # License: GNU Public License 3.0
|| # http://opensource.org/licenses/gpl-license.php
\+================================================================*/

$page['allow_guests'] = true;
require_once('./includes/core.php');
require_once('./includes/session.php');
$lang->addLocale("credits.credits");

$page['id'] = "jobs_create";
$page['name'] = 'Create Business';
$page['bodyid'] = "home";
$page['cat'] = "bussiness";

require_once('./templates/community_header.php');
?>

<style type="text/css">
input[type="text"], input[type="password"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  width: 175px;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
input[type="submit"] {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
select {
  background-color: #F1F1F1;
  border: 1px solid #999999;
  padding: 5px;
  font-family: verdana;
  font-size: 10px;
  color: #666666;
}
</style>

<?php
if($user->name == "Guest"){
  header("Location: ".PATH);
}

if(isset($_POST['corp_submit']))
{
  $corp_name = $input->FilterText($_POST['corp_name']);
  $corp_deposit = strip_tags($input->FilterText($_POST['corp_deposit']));
  if($corp_name == NULL || $corp_deposit == NULL){
    $error_message = '<font color="red"><strong>You have left a field blank.</strong></font><br /><br />';
  }else{
    if(!is_numeric($corp_deposit)){
      $error_message = '<font color="red"><strong>Business deposit must be a number.</strong></font><br /><br />';
    }else{
      if($corp_deposit < 500){
        $error_message = '<font color="red"><strong>You must deposit atleast 500 credits.</strong></font><br /><br />';
      }else{
        $check_credits_query = mysql_query("SELECT credits FROM users WHERE name = '".$user->name."'");
        $check_credits = mysql_fetch_assoc($check_credits_query);
        if($corp_deposit > $check_credits['credits']){
          $error_message = '<font color="red"><strong>You do not have '.$corp_deposit.' credits on you to deposit.</strong></font><br /><br />';
        }else{
          $check_name_query = mysql_query("SELECT * FROM jobs_corps WHERE corp_name = '".$corp_name."'");
          if(mysql_num_rows($check_name_query) > 0){
            $error_message = '<font color="red"><strong>The business name '.$corp_name.' has already been created.</strong></font><br /><br />';
          }else{
            mysql_query("INSERT into jobs_corps (id, corp_name, corp_balance, owner) VALUES (NULL, '".$corp_name."', '".$corp_deposit."', '".$user->id."')");
            $get_corp_id = mysql_query("SELECT id FROM jobs_corps WHERE corp_name = '".$corp_name."'");
            $get_corp = mysql_fetch_assoc($get_corp_id);
            mysql_query("UPDATE users SET credits = credits - '".$corp_deposit."' WHERE name = '".$user->name."';");
            $error_message = '<font color="green"><strong>You have successfully created the '.$corp_name.'.</strong></font><br /><br />';
          }
        }
      }
    }
  }
}
?>
<div id="container">
	<div id="content" style="position: relative" class="clearfix">
    <div id="column1" class="column">

				<div class="habblet-container ">
						<div class="cbb clearfix blue ">

							<h2 class="title">Create a Business
							</h2>
<p class="credits-countries-select">
<center>
<?php echo $error_message; ?>
<table width="180px">
<tr>
<td>
<form name="post" action="" method="post">
Corp Name:<br />
<input type="text" name="corp_name" value="<?php echo $corp_name; ?>" /><br />
<br />
Corp Deposit:<br />
<input type="text" name="corp_deposit" value="<?php echo $corp_deposit; ?>" /><br />
<br />
<center><input type="submit" name="corp_submit" value="Create Business" /></center><br />
</form>
</td>
</tr>
</table>
</center>
					</div>
				</div>
</div>
<div id="column2" class="column">
				<div class="habblet-container ">
						<div class="cbb clearfix blue ">

							<h2 class="title">My Business
							</h2>
						<div id="purse-habblet">

<div class="box-content">
<img src="http://www.habbosolutions.org/resources/images/habbocharacters/frank_15.gif" align="right">

<?php
$getinfo = mysql_query("SELECT * FROM users WHERE name = '".$user->name."'");
$info = mysql_fetch_array($getinfo);
$getevenmoreinfo = mysql_query("SELECT * FROM jobs_corps WHERE owner = '".$info['id']."'");
$corpcount = mysql_num_rows($getevenmoreinfo);
if($corpcount > 0) {
      while($s = mysql_fetch_array($getevenmoreinfo)) {
            echo '<a href="jobs_edit?id='.$s['id'].'">'.$s['corp_name'].'</a><br />';
      }
}else{
      echo 'Sorry, but you do not have any corps.<br />';
}
?>
<br />
<a href="<?php echo PATH; ?>/jobs_create">Create Business</a>
</div>
</div>


					</div>
				</div

<?php require_once('./templates/community_footer.php'); ?>