<?
$_SESSION['show_form'] = 1;

$table = 'contact';
$date = date('Y-m-d H:i:s', time());

$admin_email_page = 'signup_admin_email';
$client_email_page = 'signup_client_email';
$client_message_page = 'signup_client_message';
$client_error_page = 'signup_client_error';

$default_category_id = 2;

if ($_SESSION['security_code'] == $_POST['txtCaptcha'] and $_SESSION['security_code'] != '') {
	
	$_POST['date_created'] = $date;
	$_POST['status'] = 'enabled';
	
	$query = "SELECT * FROM ".$table;
	$mysqldb1->query($query);
	for ($i = 1; $i < $mysqldb1->numberFields(); $i++) {
		$SQL .= $mysqldb1->fieldName($i)."='".clean($_POST[$mysqldb1->fieldName($i)])."', ";
	}
	$SQL = rtrim($SQL, ', ');
	
	$query = "INSERT INTO ".$table." SET ".$SQL;
	
	//fb($query);
	
	$record_created = $mysqldb1->query($query);
	
	$client_name = $_POST['first_name'].' '.$_POST['last_name'];
	$admin_subject = 'Newsletter Subscription';
	
	// ADMIN EMAIL
	$query = "SELECT * FROM page WHERE name='".$admin_email_page."'";
	fb($query);
	$mysqldb1->query($query);
	$admin_mail = $mysqldb1->fetchArray();
	
	// REPLACE PLACE HOLDERS WITH FORM VARIABLES
	$adminHTML = str_replace('../..', 'http://'.$domain, html_entity_decode($admin_mail['content']));
	$adminHTML = str_replace('#DOMAIN#', $domain, $adminHTML);
	
	$query = "SELECT * FROM ".$table." WHERE id=".mysql_insert_id();
	
	$mysqldb1->query($query);
	for ($i = 0; $i < $mysqldb1->numberFields(); $i++) {
		$adminHTML = str_replace('#'.strtoupper($mysqldb1->fieldName($i)).'#', clean($_POST[$mysqldb1->fieldName($i)]), $adminHTML);
	}
	
	$mail_admin = new Rmail();
	$mail_admin->setPriority('high');
    $mail_admin->setFrom($client_name.' <'.$_POST['email'].'>');
    $mail_admin->setSubject($admin_subject);
    $mail_admin->setCc($stats_email);
    $mail_admin->setHTML($adminHTML);
	
	$admin_mailed = false;
	if ($record_created) {
    	$admin_mailed = $mail_admin->send(array($admin_email));
		
		$query = "
		INSERT INTO
			contact_category_link
		SET
			contact_id=".mysql_insert_id().", 
			contact_category_id=".$default_category_id.", 
			status=1";
		$mysqldb1->query($query);
		
	} else {
		fb('SQL INSERT FAILED!');
	}
	
	if ($admin_mailed) {
		
		$client_subject = 'Newsletter Subscription';
		
		// CLIENT EMAIL
		$query = "SELECT * FROM page WHERE name='".$client_email_page."'";
		fb($query);
		$mysqldb1->query($query);
		$client_mail = $mysqldb1->fetchArray();
		
		// REPLACE PLACE HOLDERS WITH FORM VARIABLES
		$clientHTML = str_replace('../..', 'http://'.$domain, html_entity_decode($client_mail['content']));
		$clientHTML = str_replace('#DOMAIN#', $domain, $clientHTML);
		
		$query = "SELECT * FROM ".$table." WHERE id=".mysql_insert_id();
		
		$mysqldb1->query($query);
		for ($i = 0; $i < $mysqldb1->numberFields(); $i++) {
			$clientHTML = str_replace('#'.strtoupper($mysqldb1->fieldName($i)).'#', clean($_POST[$mysqldb1->fieldName($i)]), $clientHTML);
		}
		
		$mail_client = new Rmail();
		$mail_client->setPriority('high');
		$mail_client->setFrom($domain.' <'.$admin_email.'>');
		$mail_client->setSubject($client_subject);
		$mail_client->setHTML($clientHTML);
		
		$client_mailed = $mail_client->send(array($_POST['email']));
				
		if ($client_mailed) {
			$_SESSION['show_form'] = 0;
			$_SESSION["security_code"] = '';
		}
	}
}
?>
<?
	$query = "SELECT * FROM page WHERE name='".$_SESSION['page']."'";
	$mysqldb1->query($query);
	$page = $mysqldb1->fetchArray();
		
	// FIXES Z-INDEX ISSUES
	if ($page['external'] !=  '') { 
		$page['external'] = str_replace('></embed></object>', 'wmode="opaque"></embed></object>', html_entity_decode($page['external']));
	}
	
	$content = str_replace('#VIDEO#', $page['external'], html_entity_decode($page['content']));
	$content = str_replace('../..', 'http://'.$domain, $content);
?>

<div id="page_frame">
  <div id="page_header"><? print $page['heading']; ?></div>
  <div id="page_content">
    <? 
if ($_SESSION['show_form']) {
	print $content;
} else {
    if ($admin_mailed) {
    	$query = "SELECT * FROM page WHERE name='".$client_message_page."'";
	} else {
		$query = "SELECT * FROM page WHERE name='".$client_error_page."'";
	}
    fb($query);
    $mysqldb1->query($query);
    $form = $mysqldb1->fetchArray();
    print html_entity_decode($form['content']);
} 
?>
    <? if ($_SESSION['show_form']) { ?>
    <div id="form" style="display:none;">
      <form action="/page/signup" method="post" name="signup" id="signup">
        <fieldset>
          <legend>Newsletter Signup</legend>
          <div>
            <label for="first_name">First Name</label>
            <div class="star">*</div>
            <input name="first_name" type="text" id="first_name" size="26" class="required"/>
          </div>
          <div>
            <label for="last_name">Last Name</label>
            <div class="star">*</div>
            <input name="last_name" type="text" id="last_name" size="26" class="required"/>
          </div>
          <div>
            <label for="email">Email</label>
            <div class="star">*</div>
            <input name="email" type="text" id="email" size="26" class="required email"/>
          </div>
        </fieldset>
      <fieldset>
      	<legend>Security Check</legend>
        <div> <img id="imgCaptcha" src="/control/captcha/image.php" /><br />
          <input name="txtCaptcha" type="text" id="txtCaptcha" maxlength="5" />
          Please enter the code into the box. </div>
      </fieldset>
        <div>
          <input class="submit" type="submit" value="Submit"/>
        </div>
      </form>
    </div>
    <? } ?>
  </div>
  <div id="page_footer"></div>
</div>
