<?php

include("inc.all.php");
$q2 = new CDb();

get_logged_info();

$insert = "";

if ($_POST['name'] == '' || $_POST['website_name'] == '' || $_POST['number_of_friends'] == '' || $_POST['mandatory_number_of_friends'] == ''){
	header("Location: new_campaign.ag.php?err=1");
	exit;
}

if ($_POST['number_of_friends'] < $_POST['mandatory_number_of_friends']){
	header("Location: new_campaign.ag.php?err=2");
	exit;
}

if (($_POST['number_of_friends']+0) == 0){
	header("Location: new_campaign.ag.php?err=3");
	exit;
}

//if (($_POST['number_of_friends']+0) == 0 || ($_POST['mandatory_number_of_friends']+0) == 0){
//	header("Location: new_campaign.ag.php?err=3");
//	exit;
//}

if ($_POST['use_reward_email_just_taf']){
	$insert .="
	use_reward_email_just_taf='".$_POST['use_reward_email_just_taf']."',
	number_of_friends_to_qualify_bonus1='".$_POST['number_of_friends_to_qualify_bonus1']."',
	warn_members_if_no_qualify1='".$_POST['warn_members_if_no_qualify1']."',
	gift_email_subject_line1='".addslashes($_POST['gift_email_subject_line1'])."',
	gift_email_body1='".addslashes($_POST['gift_email_body1'])."',";
}

if ($_POST['use_joined_bonus']){
	$insert .="
	number_of_friends_to_qualify_bonus2='".$_POST['number_of_friends_to_qualify_bonus2']."',
	use_joined_bonus='".$_POST['use_joined_bonus']."',
	gift_email_subject_line2='".addslashes($_POST['gift_email_subject_line2'])."',
	gift_email_body2='".addslashes($_POST['gift_email_body2'])."',";
}

if ($_POST['send_confirmation_to_sender']){
	$insert .="
	send_confirmation_to_sender='".$_POST['send_confirmation_to_sender']."',
	ty_email_subject='".addslashes($_POST['ty_email_subject'])."',
	ty_email_body='".addslashes($_POST['ty_email_body'])."',";
}

if ($_POST['send_click_notification']){
	$insert .="
	send_click_notification='".$_POST['send_click_notification']."',
	click_email_subject='".addslashes($_POST['click_email_subject'])."',
	click_email_body='".addslashes($_POST['click_email_body'])."',";
}

if ($_POST['send_join_notification']){
	$insert .="
	send_join_notification='".$_POST['send_join_notification']."',
	join_email_subject='".addslashes($_POST['join_email_subject'])."',
	join_email_body='".addslashes($_POST['join_email_body'])."',";
}
if ($_POST['submit_name']){
	$insert .="
	submit_name='".addslashes($_POST['submit_name'])."',";
}
if ($_POST['captcha']){
	$insert .="
	captcha='".$_POST['captcha']."',";
}
if ($_POST['use_aff_link']){
	$insert .="
	use_aff_link='".$_POST['use_aff_link']."',";
}
if ($_POST['gift_url']){
	$insert .="
	gift_url='".$_POST['gift_url']."',";
}
if ($_POST['gift_url2']){
	$insert .="
	gift_url2='".$_POST['gift_url2']."',";
}

if ($_POST['bypass_link']){
	$insert .="
	bypass_link='".$_POST['bypass_link']."',
	bypass_link_text='".addslashes($_POST['bypass_link_text'])."',";
}
if ($_POST['enable_ip_ban']){
	$insert .="
	enable_ip_ban='".$_POST['enable_ip_ban']."',
	nr_tries='".$_POST['nr_tries']."',";
}else $insert .="enable_ip_ban='".$_POST['enable_ip_ban']."',";
if ($_POST['subscribe_to_my_arp']){
	$insert .="
	arp_type='".$_POST['arp_type']."',
	arp_email='".$_POST['arp_email']."',
	arp_message_subject='".addslashes($_POST['arp_message_subject'])."',
	arp_message_body='".addslashes($_POST['arp_message_body'])."',";
}

$q2->query("INSERT INTO campaigns SET 
	member_id='".$q->f("id")."',
	name='".addslashes($_POST['name'])."',
	website_name='".addslashes($_POST['website_name'])."',
	number_of_friends='".$_POST['number_of_friends']."',
	mandatory_number_of_friends='".$_POST['mandatory_number_of_friends']."',
	link='".$_POST['link']."',
	enable_link_tracking='".$_POST['enable_link_tracking']."',
	invitation_email_subject='".addslashes($_POST['invitation_email_subject'])."',
	invitation_email_body='".addslashes($_POST['invitation_email_body'])."',
	include_do_not_email_me_again_link='".$_POST['include_do_not_email_me_again_link']."',
	allow_members_to_edit_emails='".$_POST['allow_members_to_edit_emails']."',
	allow_members_to_enter_preciding_note='".$_POST['allow_members_to_enter_preciding_note']."',
	landing_page='".$_POST['landing_page']."',".$insert."
	subscribe_to_my_arp='".$_POST['subscribe_to_my_arp']."',
	html_text='".$_POST['html_text']."'
	");

if ($_POST['subscribe_to_my_arp'] && $_POST['arp_type'] == '2'){
	$q->query("SELECT LAST_INSERT_ID() AS last FROM campaigns");
	$q->next_record();
	$last = $q->f("last");
	$query="INSERT INTO autoresponder_config SET field='method', value='".$_POST["method"]."', campaign_id='$last'";
	$q->query($query);
	$query="INSERT INTO autoresponder_config SET field='url', value='".$_POST["url"]."', campaign_id='$last'";
	$q->query($query);
	$query="INSERT INTO autoresponder_config SET field='first_name', value='".$_POST["first_name"]."', campaign_id='$last'";
	$q->query($query);
	$query="INSERT INTO autoresponder_config SET field='email', value='".$_POST["email"]."', campaign_id='$last'";
	$q->query($query);
	
	for ($i=1;$i<=10;$i++)
	{
		$query="INSERT INTO autoresponder_config SET field='".$_POST["field".$i]."', value='".$_POST["value".$i]."', campaign_id='$last'";
		$q->query($query);
//		$query="INSERT INTO autoresponder_config SET field='value".$i."', value='".$_POST["value".$i]."', campaign_id='$last'";
//		$q->query($query);
	}

}

header("Location: view_campains.ag.php");
?>