<?

include("inc.top.php");
//include("lib/inc.general.functions.php");
$q=new Cdb;
$q2 = new CDb();
$arp = 0;
$q2->query("UPDATE campaigns SET 
	color_bg_main='".$_GET['color_bg_main_'.$_GET['id']]."',
	color_bg_table='".$_GET['color_bg_table_'.$_GET['id']]."',
	color_border_table='".$_GET['color_border_table_'.$_GET['id']]."',
	border_table='".$_GET['border_table_'.$_GET['id']]."',
	font='".$_GET['fonts_'.$_GET['id']]."',
	size='".$_GET['sizes_'.$_GET['id']]."',
	font_color='".$_GET['color_font_'.$_GET['id']]."',
	style='".$_GET['style_'.$_GET['id']]."'
	WHERE id='".$_GET['id']."'
");
	$color_bg_main=$_GET['color_bg_main_'.$_GET['id']];
	$color_bg_table=$_GET['color_bg_table_'.$_GET['id']];
	$color_border_table=$_GET['color_border_table_'.$_GET['id']];
	$border_table=$_GET['border_table_'.$_GET['id']];
	$fonts=$_GET['fonts_'.$_GET['id']];
	$sizes=$_GET['sizes_'.$_GET['id']];
	$color_font=$_GET['color_font_'.$_GET['id']];
	$style=$_GET['style_'.$_GET['id']];


	get_logged_info();
	$member_id=$q->f("id");
	
	$q->query("SELECT * FROM members WHERE id='$member_id'");
	$q->next_record();
	$aff_link = $q->f('aff_url');
	
	$q->query("SELECT * FROM campaigns WHERE member_id=$member_id AND id='".$_GET['id']."'");
	$q->next_record();
	
		$captcha = $q->f('captcha');
	
/*if ($q->f("subscribe_to_my_arp")==1 && $q->f("arp_type")==2){
	$arp = 1;
	$query="select * from autoresponder_config where campaign_id='".$_GET['id']."' order by id";
	$q2->query($query);
	while ($q2->next_record()){

		if ($q2->f("field") == "first_name"){
			$t->set_var("member_name",$q2->f("value"));
			$first_name_awb = $q2->f("value");
		}
		if ($q2->f("field") == "email"){
			$t->set_var("member_email",$q2->f("value"));
			$email_awb = $q2->f("value");
		}
	}
	$t->set_file("html_string", "template.ads2.htm");
	$t->set_file("ads_row", "template.ads_row2.html");

}else{
	$t->set_file("html_string", "template.ads.htm");
	$t->set_file("ads_row", "template.ads_row.html");

}*/

$t->set_file("html_string", "template.ads.htm");
$t->set_file("ads_row", "template.ads_row.html");
$switch = false;
if ($q->f("subscribe_to_my_arp")==1 && $q->f("arp_type")==2){
	$arp = 1;

	$query="select * from autoresponder_config where campaign_id='".$_GET['id']."' order by id";
	$q2->query($query);

	$switch = false;



	while ($q2->next_record()){



		if (($q2->f("field") == "url" && strpos($q2->f('value'),'prosender')) || ($q2->f("field") == "url" && strpos($q2->f('value'),'aweber'))){

			$t->set_var("request_email","");			

			$switch = true;

		}

		if ($q2->f("field") == "first_name")

			$first_name_awb = $q2->f("value");

	}

}

$t->set_var("color_bg_main","#".($color_bg_main ? $color_bg_main : 'FFFFFF'));
$t->set_var("color_border_table","#".($color_border_table ? $color_border_table : 'FFFFFF'));
$t->set_var("color_bg_table","#".($color_bg_table ? $color_bg_table : 'FFFFFF'));
$t->set_var("color_font","#".($color_font ? $color_font : 'FFFFFF'));
$t->set_var("border_table",$border_table ? $border_table.' style="border-collapse:collapse; border:solid"' : '0');
$t->set_var("font",$fonts);
$t->set_var("size","font-size: $sizes"."pt;");
$t->set_var("style",$style == 'bold' ? 'font-weight:bold' : "font-style:$style");
if ($switch) {
	$t->set_var("textarea",'<textarea name="body1" cols="60" rows="6" id="body1" style="display:none">{body}</textarea> ');  //$friens_fields.='<input name="subject" id="subject" value="{subject}" size="50" type="text" {readonly_mail}> {preceding_note}  <textarea name="body1" cols="60" rows="6" id="body1" {readonly_mail}>{body}</textarea> <textarea name="body" cols="60" rows="6" id="body" style="display:none">{body}</textarea>';
   	$t->set_var("switch_val","1");
	}else {
		$t->set_var("textarea","");
		$t->set_var("switch_val",0);
	}


//		if ($q->f("subscribe_to_my_arp")==1 && $q->f("arp_type")==2){
//
//			$t->set_var("form2",'<form id="form2" name="form2" method="post" action="{url}send_mail2.php?id={campaign_id}" target="iframe" style="display:none">			{arp_subscription}'."<table style=\'display:none\'>{signup_list_b}</table>".'</form>			<iframe name="iframe" style="display:none">			</iframe>');			
//			$t->set_var("form2items", "copy_fields(\'form1\',\'form2\');"."document.form2.member_name.value = document.form1.{first_name_value}.value;			document.form2.member_email.value = document.form1.{arp_email_value}.value;gift_email();						if (validate()){				xmlHttp(\'".get_setting("site_full_url")."send_mail2.php?id={campaign_id}\',\'\',\'post\',[[[[[form:\'form2\']]]]]); 				/*document.getElementById(\'div_form\').style.display = \'none\';				document.getElementById(\'div_wait\').style.display = \'\';					setTimeout(\'document.form1.submit()\',10000);*/			}");
//			
//			$query="select * from autoresponder_config where campaign_id='".$_GET['id']."' order by id";
//			$q2->query($query);
//			$arp_subscription='';
//			while ($q2->next_record())
//			{
//
//					if ($q2->f("field")!='' && $q2->f("field")!="email" && $q2->f("field")!="first_name" && $q2->f("field")!="method" && $q2->f("field")!="url")
//						$arp_subscription.='<input type="hidden" name="'.$q2->f("field").'" value="'.$q2->f("value").'">';
//
//					if ($q2->f("field")=="email"){
//						$t->set_var("arp_email_value",$q2->f("value"));
//						$arp_email_value = $q2->f("value");
//						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';
//					}
//					if ($q2->f("field")=="first_name"){
//						$t->set_var("first_name_value",$q2->f("value"));
//						$arp_first_name_value = $q2->f("value");
//						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';
//					}
//					
////					echo $q->f("value");
//					if ($q2->f("field")=="url") $t->set_var("form_switch","http://".$q2->f("value"));
//					$t->set_var("arp_action", "send_mail2.php");
//					if ($q2->f("field")=="method") $t->set_var("send_method", ($q2->f("value") == 1 ? "GET" : "POST"));
//					$t->set_var("arp_method", "POST");
//				}
//				$t->set_var("arp_fields", $arp_subscription);
//		}else{

		if ($arp==1){

			$t->set_var("form2",($switch ? "" : '<form id="form2" name="form2" method="post" action="{arp_action}" target="iframe" style="display:none">
					{arp_subscription}'."<table style='display:none'>{signup_list_b}</table>".'</form>			
					<iframe name="iframe" style="display:none">			</iframe>'));			
			$t->set_var("form2items",($switch ? "if (validate())[[[[if (gift_email()) { change();form1.submit(); } ]]]] " :"document.form2.{first_name_noaweber}.value = document.form1.{first_name_value}.value;
			document.form2.{email_value}.value = document.form1.{arp_email_value}.value;
			if (validate()){ if (gift_email()) { change(); 
				document.form2.submit();
				document.getElementById('div_form').style.display = 'none';
				document.getElementById('div_wait').style.display = '';
				setTimeout('document.form1.submit()',10000);

} }"));
			$query="select * from autoresponder_config where campaign_id='".$_GET['id']."' order by id";
			$q2->query($query);
			$arp_subscription='';
			while ($q2->next_record())
			{

					if ($q2->f("field")!='' && $q2->f("field")!="email" && $q2->f("field")!="first_name" && $q2->f("field")!="method" && $q2->f("field")!="url")
						if ($q->f("field") == "redirect" && $switch){

							$arp_subscription.='<input type="hidden" name="'.$q2->f("field").'" value="'.get_setting("site_full_url").'send_mail.php?campaign_id={campaign_id}">';
						}else{
							$arp_subscription.='<input type="hidden" name="'.$q2->f("field").'" value="'.$q2->f("value").'">';
						}

//					if ($q2->f("field")=="email"){
//						$t->set_var("arp_email_value",$q2->f("value"));
//						$arp_email_value = $q2->f("value");
//						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';
//					}
//					if ($q2->f("field")=="first_name"){
//						$t->set_var("first_name_value",$q2->f("value"));
//						$arp_first_name_value = $q2->f("value");
//						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';
//					}
					
				if ($switch){


					if ($q2->f("field")=="email"){

						$t->set_var("arp_email_value",$q2->f("value"));
						$t->set_var("member_email",$q2->f("value"));

						$arp_email_value = $q2->f("value");

						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';

					}

					if ($q2->f("field")=="first_name"){

						$t->set_var("first_name_value",$q2->f("value"));
			$t->set_var("member_name",$q2->f("value"));

						$arp_first_name_value = $q2->f("value");

						//$arp_subscription.='<input type="hidden" name="'.$q->f("value").'">';

					}

					

//					echo $q->f("value");

					if ($q2->f("field")=="url") $t->set_var("form_switch","http://".$q2->f("value"));

					$t->set_var("arp_action", "{url}send_mail.php?campaign_id={campaign_id}");

					if ($q2->f("field")=="method") $t->set_var("send_method", ($q2->f("value") == 1 ? "GET" : "POST"));

					$t->set_var("arp_method", "POST");

				}else{

					if ($q2->f("field")=="email"){

						$t->set_var("arp_email_value","member_email");
						$t->set_var("member_email",'member_email');

						$t->set_var("email_value",$q2->f("value"));

						$arp_email_value = $q2->f("value");

						$arp_subscription.='<input type="hidden" name="'.$q2->f("value").'">';

					}

					if ($q2->f("field")=="first_name"){

						$t->set_var("first_name_value","member_name");

						$t->set_var("member_name","member_name");

						$t->set_var("first_name_noaweber",$q2->f("value"));

						$arp_first_name_value = $q2->f("value");

						$arp_subscription.='<input type="hidden" name="'.$q2->f("value").'">';

					}

					$t->set_var("form_switch","{url}send_mail.php?campaign_id={campaign_id}");

					$t->set_var("send_method","POST");

					if ($q2->f("field")=="url") $t->set_var("arp_action", "http://".$q2->f("value"));

					if ($q2->f("field")=="method") $t->set_var("arp_method", ($q2->f("value") == 1 ? "GET" : "POST"));

				}			}
				$t->set_var("arp_fields", $arp_subscription."<input type='hidden' name='campaign_id' value='{campaign_id}'>");

		}else{

			$t->set_var("form2","");
			$t->set_var("form_switch","{url}send_mail.php?campaign_id={campaign_id}");
			$t->set_var("send_method","POST");
			$t->set_var("arp_fields", "");
			$t->set_var("member_name", "member_name");
			$t->set_var("member_email", "member_email");
			$t->set_var("form2items", "if (validate()) { if (gift_email()) { change(); document.form1.submit(); } }");
		}


	$t->set_var("url",get_setting("site_full_url"));
	//captcha
//	if ($arp){
//		$t->set_var("captcha",'[[captcha]]');
//		if ($q->f("use_reward_email_just_taf") == 1 && $q->f("warn_members_if_no_qualify1") == 1)
//			$min_gift_email = $q->f("number_of_friends_to_qualify_bonus1");
//		else 
//			$min_gift_email = "-1";
//	}else{
		if ($captcha){
			$t->set_var("captcha","xmlHttp('".get_setting("site_full_url")."captcha.php','captcha','get')");
			$t->set_var("captcha_includes","<script language=JavaScript src='".get_setting("site_full_url")."js/ajax.js'></script>");
			$t->set_var("captcha_div",'<p align="center">
				<div id="captcha" align="center"></div>
		    </p>');
			}else{
			$t->set_var("captcha","");
			$t->set_var("captcha_includes","");
			$t->set_var("captcha_div",'');

		}
		if ($q->f("use_reward_email_just_taf") == 1 /*&& $q->f("warn_members_if_no_qualify1") == 1*/)
				$t->set_var("min_gift_email",$q->f("number_of_friends_to_qualify_bonus1"));
			else 
				$t->set_var("min_gift_email","-1");
			$t->set_var("min_fields",$q->f("mandatory_number_of_friends"));
			$t->set_var("fields",$q->f("number_of_friends"));
	
	
	//end captcha
	$t->set_var("campaign_id",$_GET['id']);
	$t->set_var("mid",$member_id);
	$t->set_var("submit_name",$q->f("submit_name"));
	$min_fields = $q->f("mandatory_number_of_friends");
	$fields = $q->f("number_of_friends");
	
	if ($q->f("allow_members_to_edit_emails") == 0){
		$t->set_var("readonly_mail", "readonly");
	}
	
	if ($q->f("use_aff_link") == 1){

		$t->set_var("aff_link", '
		  <tr style="border:none">
		  	<td align="center" style="border:none"><br><a href="'.$aff_link.'" title="Powered by ViralFriendGenerator.com" target="_blank"><font style="font-size:14px">Powered by ViralFriendGenerator.com</font></a><br><br></td>
		  </tr>
		');
	}else{
		$t->set_var("aff_link","");
	}	
	
	
	if ($q->f("bypass_link")){
		preg_match("'^http.*'",$q->f("landing_page"),$match);

		 $t->set_var("bypass_link", ($match[0] ? "<a href='".$q->f("landing_page")."'>".stripslashes($q->f("bypass_link_text"))."</a>" : "<a href='http://".$q->f("landing_page")."'>".stripslashes($q->f("bypass_link_text"))."</a>"));
	}

	$t->set_var("subject",stripslashes($q->f("invitation_email_subject")));	
	
	$message = stripslashes($q->f("invitation_email_body"));
	$message = str_replace("[[sitename]]",stripslashes($q->f("website_name")),$message);
	$message = str_replace("[[link]]",$q->f("link"),$message);
	$message = str_replace("'","\'",$message);
	
	
//	$friens_fields ='	  <input name="member_name" id="member_name">    <input name="member_email" id="member_email">';
//	
	for ($i=1; $i<=$q->f("number_of_friends");$i++){
		$t->set_var("id",$i);
		$friens_fields .= $t->parse("adds_row", "ads_row",true);
	}	
	
////   echo $friens_fields;
	$query="select * from autoresponder_config where campaign_id='".$_GET['id']."' order by id";
	$q2->query($query);
	$friens_fields ='';
	while ($q2->next_record()){

		if ($q2->f("field")!='' && $q2->f("field")!="method" && $q2->f("field")!="url" && $q2->f("field")!="email" && $q2->f("field")!="first_name")

				$friens_fields.='<input type="hidden" name="'.$q2->f("field").'" value="'.$q2->f("value").'">';
		elseif ($q2->f("field")=="email" || $q2->f("field")=="first_name"){
				$friens_fields.='<input type="hidden" name="'.$q2->f("value").'">';
		
		}
	}
	
	$t->set_var("signup_list_b",$friens_fields);
	if ($q->f("allow_members_to_enter_preciding_note") == 1){
		$t->set_var("preceding_note", "<input type='text' size='50' name='note' id='note' value='Enter your preceding note here'>");
	}else{
		$t->set_var("preceding_note","");
	}
	$t->set_var("body",stripslashes($message));
	$html_string = $t->pparse2("out", "html_string");
	
	$html_string=str_replace("(check)","{2,4}",$html_string);
//	$arp ?	$html_string = str_replace('enterchr13', "\\r",$html_string) : '';
	if (is_writable("templates/taf_form.html")){
		if (!$handle = fopen("templates/taf_form.html","w")){
			echo "Cannot open file (templates/taf_form.html)";
        	exit;
		}
		
		fwrite($handle,$html_string);
		fclose($handle);
	}
	
//	if ($arp){
//		$copy = copy("templates/taf_form.html","templates/TAF-Form_raw".$_GET['id'].".html");
//		if (!$copy){
//			echo "Error Writing File";
//		}
//		$copy = copy("templates/template.ads_arp.htm","templates/TAF-Form_arp_".$_GET['id'].".html");
//		if (!$copy){
//			echo "Error Writing File";
//		}
//		FFileRead("templates/TAF-Form_arp_".$_GET['id'].".html", $ff);
//		$ff = str_replace("{min_fields}",$min_fields,$ff);
//		$ff = str_replace("{fields}",$fields,$ff);
//		$ff = str_replace("{min_gift_email}",$min_gift_email,$ff);
//		$ff = str_replace("{member_name}",$first_name_awb,$ff);
//		$ff = str_replace("{member_email}",$email_awb,$ff);
//		$filename_for_user = "TAF-Form_arp_".$_GET['id'].".html";
//		header ("Content-Type: application/octet-stream"); 
//		header ("Content-Length: " . strlen($ff)); 
//		header ("Content-Disposition: attachment; filename=$filename_for_user"); 
//		echo $ff;
//	}else{
		$copy = copy("templates/taf_form.html","templates/TAF-Form_".$_GET['id'].".html");
		if (!$copy){
			echo "Error Writing File";
		}
		FFileRead("templates/TAF-Form_".$_GET['id'].".html", $ff);
				$ff = str_replace("[[[[","{",$ff);
		$ff = str_replace("]]]]","}",$ff);
		$filename_for_user = "TAF-Form_".$_GET['id'].".html";
		header ("Content-Type: application/octet-stream"); 
		header ("Content-Length: " . strlen($ff)); 
		header ("Content-Disposition: attachment; filename=$filename_for_user"); 

		echo $ff;

	
?>