<?PHP

if (!$_POST){ header('Location: '.$site_url); }

/*

FIELD NAMES: 
campaign_id
campaign_account_id_fk
campaign_name
campaign_300x250
campaign_300x250_impression
campaign_728x90
campaign_728x90_impression
campaign_300x600
campaign_300x600_impression
campaign_970x250
campaign_970x250_impression
campaign_wallpaper
campaign_wallpaper_impression
campaign_date
campaign_date_repeat
campaign_signature
campaign_signature_timestamp
campaign_deploy_timestamp
campaign_screenshot_timestamp
campaign_invoice_timestamp
campaign_notes
campaign_archive
campaign_archive_timestamp
campaign_archive_prior_id_fk

*/

/* Need to ltrim and rtrim commas before insertion */

//Insert into database
//$pass = 'abc123';
$errmsg = NULL;
$insert_success = NULL;
//Check for blank fields
if ((!$_POST[name])) $errmsg = $errmsg."Campaign name cannot be left blank, ";  

if ($_POST['300x250'] == 1 && $_POST['300x250_impression'] == "") { $errmsg = $errmsg."You must specify a number of impressions if you select 300x250 ad unit, "; }
if ($_POST['300x250'] == 1 && $_POST['300x250_impression'] != "" && !is_numeric($_POST['300x250_impression'])) { $errmsg = $errmsg."300x250 impressions must be a number, "; }

if ($_POST['728x90'] == 1 && $_POST['728x90_impression'] == "") { $errmsg = $errmsg."You must specify a number of impressions if you select 728x90 ad unit, "; }
if ($_POST['728x90'] == 1 && $_POST['728x90_impression'] != "" && !is_numeric($_POST['728x90_impression'])) { $errmsg = $errmsg."728x90 impressions must be a number, "; }

if ($_POST['300x600'] == 1 && $_POST['300x600_impression'] == "") { $errmsg = $errmsg."You must specify a number of impressions if you select 300x600 ad unit, "; }
if ($_POST['300x600'] == 1 && $_POST['300x600_impression'] != "" && !is_numeric($_POST['300x600_impression'])) { $errmsg = $errmsg."300x600 impressions must be a number, "; }

if ($_POST['970x250'] == 1 && $_POST['970x250_impression'] == "") { $errmsg = $errmsg."You must specify a number of impressions if you select 970x250 ad unit, "; }
if ($_POST['970x250'] == 1 && $_POST['970x250_impression'] != "" && !is_numeric($_POST['970x250_impression'])) { $errmsg = $errmsg."970x250 impressions must be a number, "; }

if ($_POST['wallpaper'] == 1 && $_POST['wallpaper_impression'] == "") { $errmsg = $errmsg."You must specify a number of impressions if you select wallpaper ad unit, "; }
if ($_POST['wallpaper'] == 1 && $_POST['wallpaper_impression'] != "" && !is_numeric($_POST['wallpaper_impression'])) { $errmsg = $errmsg."Wallpaper impressions must be a number, "; }

if ($_POST['month']==0) $errmsg = $errmsg.'Start month cannot be left blank, ';
if ($_POST['day']==0) $errmsg = $errmsg.'Start day cannot be left blank, ';

if (strlen($_POST['notes']) < 4) $errmsg = $errmsg."Notes cannot be left blank, ";
if (isset($errmsg)) {
   $errmsg = rtrim($errmsg, ", ");
   $errmsg = $errmsg."<br />";
}


// begin error message check
if(!$errmsg){

	$account_id = $_POST['account_id'];

	$name = trim($_POST['name']);

	$date_repeat = NULL;
	if (isset($_POST['day_repeat']) && isset($_POST['month_repeat'])) { 
		$year_repeat = $_POST['year_repeat'];
		$month_repeat = $_POST['month_repeat'];
		$day_repeat = $_POST['day_repeat']; 
		$date_repeat = "$year_repeat-$month_repeat-$day_repeat";
	}
	
	$ad_300x250 = $_POST['300x250'];
	$ad_300x250_impression = trim($_POST['300x250_impression']);
	$ad_728x90 = $_POST['728x90'];
	$ad_728x90_impression = trim($_POST['728x90_impression']);
	$ad_300x600 = $_POST['300x600'];
	$ad_300x600_impression = trim($_POST['300x600_impression']);
	$ad_970x250 = $_POST['970x250'];
	$ad_970x250_impression = trim($_POST['900x250_impression']);
	$ad_wallpaper = $_POST['wallpaper'];
	$ad_wallpaper_impression = trim($_POST['wallpaper_impression']);
   
	$year = $_POST['year'];
	$month = $_POST['month'];
	$day = $_POST['day'];
	$date = "$year-$month-$day";

	$notes = mysql_escape_string(nl2br(substr(trim($_POST['notes']), 0, 4096)));

  	// begin boolean check to see if we should insert the coach into the dbase
	if($_POST[insert_campaign] == true){
		$sql = "INSERT INTO io_campaign (`campaign_account_id_fk`, `campaign_name`, `campaign_300x250`, `campaign_300x250_impression`, `campaign_728x90`, `campaign_728x90_impression`, `campaign_300x600`, `campaign_300x600_impression`, `campaign_970x250`, `campaign_970x250_impression`, `campaign_wallpaper`, `campaign_wallpaper_impression`, `campaign_date`, `campaign_date_repeat`, `campaign_notes`) VALUES ('$account_id', '$name', '$ad_300x250', '$ad_300x250_impression', '$ad_728x90', '$ad_728x90_impression', '$ad_300x600', '$ad_300x600_impression', '$ad_970x250', '$ad_970x250_impression', '$ad_wallpaper', '$ad_wallpaper_impression', '$date', '$date_repeat', '$notes')";
	//echo("campaign entered!<br />");
	//echo($sql);
	mysql_query($sql) OR die(mysql_error());
	//exit();
	$insert_success = true;
  }else{
	 //echo("campaign NOT entered.");
	 $confirm_campaign = true;
  }
  // end insert check  
    
}
// end error message check
?>
