
	var error_step1 = 0;
	var error_step2 = 0;
	var error_step3 = 0;
	var error_step4 = 0;
	var error_check_step1 = 0;
	var error_check_step2 = 0;
	var error_check_step3 = 0;
	var error_check_step4 = 0;
	var wizard_submitted = 0;
	var advanced_submitted = 0;
	var new_secure_links = 0;
	var total_new_secure_links = 0;
	var new_optin_links = 0;
	var total_new_optin_links = 0;
	
	function wizard_submit() {		
		
		error_step1 = 1;
		error_step2 = 1;
		error_step3 = 1;
		error_step4 = 1;
		
		wizard_error_check();
		
		if(error_check_step1==1 || error_check_step2==1 || error_check_step3 == 1 || error_check_step4 == 1) {
			
			jQuery('#wizard_error_text').hide().fadeIn(200);
			
		} else {
			
			jQuery('#optin_campaign_form_wizard').wrap('<form id="wizard_form_id" method="post" action=""></form>');
			jQuery('#wizard_form_id').submit();
			
		}
		
	}
	
	
	function advanced_submit() {		
		
		
		advanced_error_check();
		
		if(advanced_error_check_step1==1 || advanced_error_check_step2==1 || advanced_error_check_step3 == 1 || advanced_error_check_step4 == 1) {
			
			jQuery('#advanced_error_text').hide().fadeIn(200);
			
		} else {
			
			jQuery('#optin_campaign_form').wrap('<form id="advanced_form_id" method="post" action=""></form>');
			jQuery('#advanced_form_id').submit();
			
		}
		
	}
	
	
	function advanced_error_check() {		
		
		name = jQuery('#advanced_name').val();
		description = jQuery('#advanced_description').val();
		mailer_selected = 0;
		secure_link_selected = 0;
		optin_link_created = 0;
		optin_link_name = html_encode(jQuery('#advanced_optin_link_name').val());
		optin_link_secure_link = jQuery('#advanced_optin_link_secure_link').val();
		optin_link_optin_form = jQuery('#advanced_optin_form_select').val();
		
		jQuery('.advanced_select_mailer').each(function(index, obj){
			if(jQuery(this).prop('checked')) { mailer_selected = 1; }
		});
		
		jQuery('.advanced_select_link').each(function(index, obj){
			if(jQuery(this).prop('checked')) { 
			
				url = jQuery('#advanced_link_url_'+jQuery(this).attr('value')).html();	
								
				if(typeof url !== 'undefined') {
				
					if(url.indexOf("*") == -1) {
				
						secure_link_selected = 1; 
					
					}
				}				
			}
		});
		
		if(new_secure_links > 0) {
			secure_link_selected = 1;			
		}
		
		if(new_optin_links > 0) {
			optin_link_created = 1;			
		} else if(optin_link_name.length >= 5 && optin_link_secure_link != 0 && optin_link_optin_form != 0) {
			optin_link_created = 1;					
		}
		
		if(jQuery('#advanced_existing_optin_link_table_container').length > 0) { optin_link_created = 1; }
			
		if(name.length<5) {
			
			advanced_error_check_step1 = 1;
			jQuery("#advanced_step1_error_text").hide().fadeIn(200);
			jQuery("#advanced_step1").attr('class', 'opt_advanced_step_box_error');
			
		} else {		
			
			advanced_error_check_step1 = 0;
			jQuery("#advanced_step1_error_text").hide();
			jQuery("#advanced_step1").attr('class', 'opt_advanced_step_box_success');				
			
		}		
				
		if(mailer_selected==0) {
			
			advanced_error_check_step2 = 1;
			jQuery("#advanced_step2_error_text").hide().fadeIn(200);			
			jQuery("#advanced_step2").attr('class', 'opt_advanced_step_box_error');
			
		} else {		
			
			advanced_error_check_step2 = 0;
			jQuery("#advanced_step2_error_text").hide();
			jQuery("#advanced_step2").attr('class', 'opt_advanced_step_box_success');				
			
		}	
		
		if(secure_link_selected==0) {			
			
			advanced_error_check_step3 = 1;
			jQuery("#advanced_step3_error_text").hide().fadeIn(200);			
			jQuery("#advanced_step3").attr('class', 'opt_advanced_step_box_error');
			
		} else {		
			
			advanced_error_check_step3 = 0;
			jQuery("#advanced_step3_error_text").hide();
			jQuery("#advanced_step3").attr('class', 'opt_advanced_step_box_success');				
			
		}
		
		if(optin_link_created==0) {
			
			advanced_error_check_step4 = 1;
			jQuery("#advanced_step4_error_text").hide().fadeIn(200);			
			jQuery("#advanced_step4").attr('class', 'opt_advanced_step_box_error');
			
		} else {		
			
			advanced_error_check_step4 = 0;
			jQuery("#advanced_step4_error_text").hide();
			jQuery("#advanced_step4").attr('class', 'opt_advanced_step_box_success');				
			
		}
		
		
		if(advanced_submitted == 1  && (advanced_error_check_step1 == 1 || advanced_error_check_step2 == 1  || advanced_error_check_step3 == 1  || advanced_error_check_step4 == 1 )) {
			
			jQuery("#advanced_error_text").hide().fadeIn(200);
			
		} else {
		
			jQuery("#advanced_error_text").hide();			
			
		}
		
	}
	
	
	function wizard_error_check() {		
		
		name = jQuery('#wizard_name').val();
		description = jQuery('#wizard_description').val();
		mailer_selected = 0;
		secure_link_selected = 0;
		optin_link_created = 0;
		optin_link_name = html_encode(jQuery('#wizard_optin_link_name').val());
		optin_link_secure_link = jQuery('#wizard_optin_link_secure_link').val();
		optin_link_optin_form = jQuery('#wizard_optin_form_select').val();
		
		jQuery('.wizard_select_mailer').each(function(index, obj){
			if(jQuery(this).prop('checked')) { mailer_selected = 1; }
		});
		
		jQuery('.wizard_select_link').each(function(index, obj){
			if(jQuery(this).prop('checked')) { 
			
				url = jQuery('#wizard_link_url_'+jQuery(this).attr('value')).html();	
								
				if(typeof url !== 'undefined') {
				
					if(url.indexOf("*") == -1) {
				
						secure_link_selected = 1; 
					
					}
				}				
			}
		});
		
		if(new_secure_links > 0) {
			secure_link_selected = 1;			
		}
		
		if(new_optin_links > 0) {
			optin_link_created = 1;			
		} else if(optin_link_name.length >= 5 && optin_link_secure_link != 0 && optin_link_optin_form != 0) {
			optin_link_created = 1;					
		}
		
		if(jQuery('#wizard_existing_optin_link_table_container').length > 0) { optin_link_created = 1; }
		
			
			
		if(error_step1 == 0) {
			
			jQuery("#wizard_step1").attr('class', 'opt_wizard_step_box');
			
		} else if(name.length<5 && error_step1 == 1) {
			
			error_check_step1 = 1;
			jQuery("#wizard_step1_error_text").hide().fadeIn(200);
			jQuery("#wizard_step1").attr('class', 'opt_wizard_step_box_error');
			
		} else if(error_step1 == 1) {		
			
			error_check_step1 = 0;
			jQuery("#wizard_step1_error_text").hide();
			jQuery("#wizard_step1").attr('class', 'opt_wizard_step_box_success');				
			
		}		
				
		
		
		if(error_step2 == 0) {
			
			jQuery("#wizard_step2").attr('class', 'opt_wizard_step_box');
			
		} else if(mailer_selected==0 && error_step2 == 1) {
			
			error_check_step2 = 1;
			jQuery("#wizard_step2_error_text").hide().fadeIn(200);			
			jQuery("#wizard_step2").attr('class', 'opt_wizard_step_box_error');
			
		} else if(error_step2 == 1) {		
			
			error_check_step2 = 0;
			jQuery("#wizard_step2_error_text").hide();
			jQuery("#wizard_step2").attr('class', 'opt_wizard_step_box_success');				
			
		}	
		
			
		if(error_step3 == 0) {
			
			jQuery("#wizard_step3").attr('class', 'opt_wizard_step_box');
			
		} else if(secure_link_selected==0 && error_step3 == 1) {			
			
			error_check_step3 = 1;
			jQuery("#wizard_step3_error_text").hide().fadeIn(200);			
			jQuery("#wizard_step3").attr('class', 'opt_wizard_step_box_error');
			
		} else if(error_step3 == 1) {		
			
			error_check_step3 = 0;
			jQuery("#wizard_step3_error_text").hide();
			jQuery("#wizard_step3").attr('class', 'opt_wizard_step_box_success');				
			
		}
		
			
		if(error_step4 == 0) {
			
			jQuery("#wizard_step4").attr('class', 'opt_wizard_step_box');
			
		} else if(optin_link_created==0 && error_step4 == 1) {
			
			error_check_step4 = 1;
			jQuery("#wizard_step4_error_text").hide().fadeIn(200);			
			jQuery("#wizard_step4").attr('class', 'opt_wizard_step_box_error');
			
		} else if(error_step4 == 1) {		
			
			error_check_step4 = 0;
			jQuery("#wizard_step4_error_text").hide();
			jQuery("#wizard_step4").attr('class', 'opt_wizard_step_box_success');				
			
		}
		
		
		if(wizard_submitted == 1  && (error_check_step1 == 1 || error_check_step2 == 1  || error_check_step3 == 1  || error_check_step4 == 1 )) {
			
			jQuery("#wizard_error_text").hide().fadeIn(200);
			
		} else {
		
			jQuery("#wizard_error_text").hide();			
			
		}
		
	}
	

	function do_wizard_step(step,next_button) {	
		
		window.scrollTo(0, 0);
		
		wizard_error_check();
		
		if(step==1) {
		
			jQuery('#wizard_form1').fadeIn(200);
			jQuery('#wizard_form2').hide();			
			jQuery('#wizard_form3').hide();			
			jQuery('#wizard_form4').hide();
			jQuery("#wizard_step1").attr('class', 'opt_wizard_step_box_selected');
			jQuery('#wizard_next_step').attr('value', 'Next Step');
			jQuery('#wizard_next_step').attr('onclick', 'error_step1=1; do_wizard_step(2,1);');
			jQuery('#wizard_previous_step').attr('value', 'Previous Step');
			jQuery('#wizard_previous_step').attr('disabled', 'disabled');
			jQuery('#wizard_previous_step').removeAttr('onclick');
			jQuery('#wizard_step_text').html('Step 1: Campaign details');
			
			
		} else if(step==2) {
			
			if(next_button==1 && error_check_step1==1) {		
		
			} else {
		
				jQuery('#wizard_form1').hide();
				jQuery('#wizard_form2').fadeIn(200);			
				jQuery('#wizard_form3').hide();			
				jQuery('#wizard_form4').hide();
				jQuery("#wizard_step2").attr('class', 'opt_wizard_step_box_selected');		
				jQuery('#wizard_next_step').attr('value', 'Next Step');
				jQuery('#wizard_next_step').attr('onclick', 'error_step2=1; do_wizard_step(3,1)');
				jQuery('#wizard_previous_step').attr('value', 'Previous Step');
				jQuery('#wizard_previous_step').attr('onclick', 'do_wizard_step(1,0)');
				jQuery('#wizard_previous_step').removeAttr('disabled');
				jQuery('#wizard_step_text').html('Step 2: Select the lists your subscribers will be added to');
			
			}
					
			
		} else if(step==3) {
			
			if(next_button==1 && error_check_step2==1) {
				
			} else {
			
				jQuery('#wizard_form1').hide();
				jQuery('#wizard_form2').hide();			
				jQuery('#wizard_form3').fadeIn(200);			
				jQuery('#wizard_form4').hide();
				jQuery("#wizard_step3").attr('class', 'opt_wizard_step_box_selected');	
				jQuery('#wizard_next_step').attr('value', 'Next Step');
				jQuery('#wizard_next_step').attr('onclick', 'error_step3=1; do_wizard_step(4,1)');
				jQuery('#wizard_previous_step').attr('value', 'Previous Step');
				jQuery('#wizard_previous_step').attr('onclick', 'do_wizard_step(2,0)');
				jQuery('#wizard_previous_step').removeAttr('disabled');	
				jQuery('#wizard_step_text').html('Step 3: Define this campaign\'s secure pages');
			
			}
		
		} else if(step==4) {
			
			if(next_button==1 && error_check_step3==1) {
				
			} else {
				
				populate_secure_link_dropdown('wizard_');
				jQuery('#wizard_form1').hide();
				jQuery('#wizard_form2').hide();			
				jQuery('#wizard_form3').hide();			
				jQuery('#wizard_form4').fadeIn(200);
				jQuery("#wizard_step4").attr('class', 'opt_wizard_step_box_selected');		
				
				if(campaign_editing==1) {
					jQuery('#wizard_next_step').attr('value', 'Finalize Editing');
				} else {
					jQuery('#wizard_next_step').attr('value', 'Create Campaign');
				}				
				
				jQuery('#wizard_next_step').attr('onclick', 'error_step4=1; wizard_submit();');
				jQuery('#wizard_previous_step').attr('value', 'Previous Step');
				jQuery('#wizard_previous_step').attr('onclick', 'do_wizard_step(3,0)');
				jQuery('#wizard_previous_step').removeAttr('disabled');	
				jQuery('#wizard_step_text').html('Step 4: Create optinlinks');
			
			}
			
		}
				
	}


	function populate_secure_link_dropdown(prefix) {		
		
		html = '';
		
		jQuery('.'+prefix+'select_link').each(function(index, obj){
			if(jQuery(this).prop('checked')) { 			
				
				url = jQuery('#'+prefix+'link_url_'+jQuery(this).attr('value')).html();	
				
				if(typeof url !== 'undefined') {
					
					if(url.indexOf("*") == -1) {
					
						html+="<option value='"+jQuery(this).attr('value')+"'>";
						html+=jQuery('#'+prefix+'link_name_'+jQuery(this).attr('value')).html();
						html+="</option>";
						
					}		
				}
			}
		});
		
		jQuery('.'+prefix+'new_secure_link_id').each(function(index, obj){
				
			url = jQuery('#'+prefix+'new_secure_link_url_'+jQuery(this).html()).val();	
				
			if(typeof url !== 'undefined') {
				
				if(url.indexOf("*") == -1) {
				
					html+="<option value='new_"+jQuery(this).html()+"'>";
					html+=jQuery('#'+prefix+'new_secure_link_name_'+jQuery(this).html()).val();
					html+="</option>";
					
				}
			}
				
		});
		
		if(html=='') {			
			
			html="<option value=0>Please select or create a non-wildcard secure page on the previous step!</option>";
			
		}
	
		jQuery('#'+prefix+'optin_link_secure_link').html(html);
		
	}

	function checkbox_toggle(checkbox_id) {
		
		if(document.getElementById(checkbox_id).checked) {
			
			jQuery('#'+checkbox_id).removeAttr('checked');			
			
		} else {
			
			jQuery('#'+checkbox_id).attr('checked','checked');		
			
		}
		
	}


	function radio_toggle(radio_id) {
		
		if(document.getElementById(radio_id).checked) {
			
			
		} else {
			
			jQuery('#'+radio_id).attr('checked','checked');		
			
		}
		
	}
	
	function html_encode(value){
		if (value) {
			return jQuery('<div />').text(value).html();
		} else {
			return '';
		}
	}
 
	function html_decode(value) {
		if (value) {
			return jQuery('<div />').html(value).text();
		} else {
			return '';
		}
	}
		
	
	function secure_link_form_handler(prefix) {
		
		secure_form_error = 0;		
		
		secure_link_name = html_encode(jQuery('#'+prefix+'secure_link_name').val());
		secure_link_url = html_encode(jQuery('#'+prefix+'secure_link_url').val());
		secure_link_description = html_encode(jQuery('#'+prefix+'secure_link_description').val());
		
		if(secure_link_name.length < 5 || (secure_link_url.substring(0,7) != 'http://' && secure_link_url.substring(0,8) != 'https://')) {
		
			secure_form_error = 1;
			error_text = '';
			
			if(secure_link_name.length < 5) { 
				error_text += '<strong>ERROR:</strong> Secure page name is required and must be at least 5 characters<br>';
			}
		
			if(secure_link_url.substring(0,7) != 'http://' && secure_link_url.substring(0,8) != 'https://') { 
				error_text += '<strong>ERROR:</strong> Secure page URL is required and must start with http:// or https://';					
			}		
			
			jQuery('#'+prefix+'secure_link_warning_text').html(error_text);
			jQuery('#'+prefix+'secure_link_warning_text').hide().fadeIn(200);
			
			
		} else {
			
			new_secure_links++;
			total_new_secure_links++;
			secure_form_error = 0;		
			jQuery('#'+prefix+'secure_link_warning_text').hide();
			jQuery('#'+prefix+'new_secure_link_table_container').fadeIn(200);
					
			if(new_secure_links%2==1) {row_class = "opt_table_row"; } else { row_class = "opt_table_row_alt"; }		
						
			jQuery('<tr class="'+row_class+'" id="'+prefix+'secure_link_row_'+new_secure_links+'"><td style="padding-right:0;"><span class="'+prefix+'new_secure_link_id">'+new_secure_links+'</span><input name="'+prefix+'new_secure_link_name_'+new_secure_links+'" id="'+prefix+'new_secure_link_name_'+new_secure_links+'" type="hidden" value="'+secure_link_name+'"><input name="'+prefix+'new_secure_link_url_'+new_secure_links+'" id="'+prefix+'new_secure_link_url_'+new_secure_links+'" type="hidden" value="'+secure_link_url+'"><input name="'+prefix+'new_secure_link_description_'+new_secure_links+'" id="'+prefix+'new_secure_link_description_'+new_secure_links+'" type="hidden" value="'+secure_link_description+'"></td><td>'+secure_link_name+'</td><td>'+secure_link_url+'</td><td align="right"><a href="javascript:secure_link_form_load_edit(\''+prefix+'\','+new_secure_links+',\'new\')">Edit</a><a id="'+prefix+'delete_new_secure_link_'+new_secure_links+'" href="javascript:secure_link_form_delete(\''+prefix+'\','+new_secure_links+')" class="opt_trash">Trash</a></td></tr>').hide().appendTo('#'+prefix+'new_secure_link_table > tbody:last').fadeIn(200);			
			
			jQuery('#'+prefix+'secure_link_name').val('');
			jQuery('#'+prefix+'secure_link_url').val('');
			jQuery('#'+prefix+'secure_link_description').val('');
			
			jQuery('#'+prefix+'step3_warning_text').fadeOut(200);
		
		}		
		
	}
	
	function secure_link_form_load_edit(prefix,id,type) {
		
		if(type=='new') {
			
			jQuery('#'+prefix+'secure_link_name').val(jQuery('#'+prefix+'new_secure_link_name_'+id).val());
			jQuery('#'+prefix+'secure_link_url').val(jQuery('#'+prefix+'new_secure_link_url_'+id).val());
			jQuery('#'+prefix+'secure_link_description').val(jQuery('#'+prefix+'new_secure_link_description_'+id).val());
			secure_page_name = jQuery('#'+prefix+'new_secure_link_name_'+id).val();	
						
			
		} else if(type='existing') {
			
			jQuery('#'+prefix+'secure_link_name').val(jQuery('#'+prefix+'link_name_'+id).html());
			jQuery('#'+prefix+'secure_link_url').val(jQuery('#'+prefix+'link_url_'+id).html());
			jQuery('#'+prefix+'secure_link_description').val(jQuery('#'+prefix+'edit_secure_link_description_'+id).val());		
			secure_page_name = jQuery('#'+prefix+'link_name_'+id).html();	
			
		}
		
		jQuery('#'+prefix+'existing_secure_link_table_container').slideUp();
		jQuery('#'+prefix+'new_secure_link_table_container').slideUp();
		jQuery('#'+prefix+'create_secure_link').fadeIn(200);
		jQuery('#'+prefix+'show_new_link_form').hide();
		jQuery('#'+prefix+'hide_new_link_form').hide();
		jQuery('#'+prefix+'secure_link_cancel_button').fadeIn(200);
		jQuery('#'+prefix+'secure_link_edit_text').fadeIn(200);
		jQuery('#'+prefix+'secure_page_edit_name_text').hide().html('<b>Editing Secure Page: </b>'+secure_page_name).fadeIn(200);
		jQuery('#'+prefix+'secure_link_button').val('Save Secure Page Changes');
		jQuery('#'+prefix+'secure_link_button').attr('onclick',"secure_link_form_edit('"+prefix+"','"+id+"','"+type+"')"); 
		
		
		setTimeout(scrollToElement('#'+prefix+'secure_page_edit_name_text',500,-100),250);
		
		
		
	}
		
	function secure_link_form_edit(prefix,id,type) {
		
		secure_form_error = 0;		
		
		secure_link_name = html_encode(jQuery('#'+prefix+'secure_link_name').val());
		secure_link_url = html_encode(jQuery('#'+prefix+'secure_link_url').val());
		secure_link_description = html_encode(jQuery('#'+prefix+'secure_link_description').val());
				
		if(secure_link_name.length < 5 || (secure_link_url.substring(0,7) != 'http://' && secure_link_url.substring(0,8) != 'https://')) {
		
			secure_form_error = 1;
			error_text = '';
			
			if(secure_link_name.length < 5) { 
				error_text += '<strong>ERROR:</strong> Secure page name is required and must be at least 5 characters<br>';
			}
		
			if(secure_link_url.substring(0,7) != 'http://' && secure_link_url.substring(0,8) != 'https://') { 
				error_text += '<strong>ERROR:</strong> Secure page URL is required and must start with http:// or https://';					
			}		
			
			jQuery('#'+prefix+'secure_link_warning_text').html(error_text);
			jQuery('#'+prefix+'secure_link_warning_text').hide().fadeIn(200);		
			setTimeout(scrollToElement('#'+prefix+'secure_page_edit_name_text',500,-100),250);
			
			
		} else {
			
			if(type=='new') {		
							
				
				jQuery('#'+prefix+'secure_link_row_'+id).html('<td style="padding-right:0;">'+id+'<input name="'+prefix+'new_secure_link_name_'+id+'" id="'+prefix+'new_secure_link_name_'+id+'" type="hidden" value="'+secure_link_name+'"><input name="'+prefix+'new_secure_link_url_'+id+'" id="'+prefix+'new_secure_link_url_'+id+'" type="hidden" value="'+secure_link_url+'"><input name="'+prefix+'new_secure_link_description_'+id+'" id="'+prefix+'new_secure_link_description_'+id+'" type="hidden" value="'+secure_link_description+'"></td><td>'+secure_link_name+'</td><td>'+secure_link_url+'</td><td align="right"><a href="javascript:secure_link_form_load_edit(\''+prefix+'\','+id+',\'new\')">Edit</a><a id="'+prefix+'delete_new_secure_link_'+id+'" href="javascript:secure_link_form_delete(\''+prefix+'\','+id+')" class="opt_trash">Trash</a></td>').hide().fadeIn(200);				
							
			} else if(type='existing') {
				
				jQuery('#'+prefix+'existing_secure_link_'+id).html('<td style="padding-right:0;"><input type="checkbox" style="width:17px;" name="'+prefix+'link_'+id+'" id="'+prefix+'link_'+id+'" value="'+id+'" class="'+prefix+'select_link" checked="checked"><input name="'+prefix+'edit_secure_link_name_'+id+'" id="'+prefix+'edit_secure_link_name_'+id+'" type="hidden" value="'+secure_link_name+'"><input name="'+prefix+'edit_secure_link_url_'+id+'" id="'+prefix+'edit_secure_link_url_'+id+'" type="hidden" value="'+secure_link_url+'"><input name="'+prefix+'edit_secure_link_description_'+id+'" id="'+prefix+'edit_secure_link_description_'+id+'" type="hidden" value="'+secure_link_description+'"></td><td style="font-style:italic" id="'+prefix+'link_name_'+id+'">'+secure_link_name+'</td><td style="font-style:italic" id="'+prefix+'link_url_'+id+'">'+secure_link_url+'</td><td align="right"><a href="javascript:secure_link_form_load_edit(\''+prefix+'\','+id+',\'existing\')">Edit</a></td>').hide().fadeIn(200);	
				
				
			}
						
			secure_link_form_stop_editing(prefix);
			
		}
		
	}
	
	function secure_link_form_stop_editing(prefix) {
		
		jQuery('#'+prefix+'secure_link_name').val('');
		jQuery('#'+prefix+'secure_link_url').val('');
		jQuery('#'+prefix+'secure_link_description').val('');			
					
		jQuery('#'+prefix+'secure_link_cancel_button').fadeOut(200);
		jQuery('#'+prefix+'secure_link_edit_text').fadeOut(200);
		jQuery('#'+prefix+'secure_link_button').val('Create Secure Page');
		jQuery('#'+prefix+'secure_link_button').attr('onclick',"secure_link_form_handler('"+prefix+"')");
		
		jQuery('#'+prefix+'create_secure_link').fadeOut(200);jQuery('#'+prefix+'hide_new_link_form').hide();jQuery('#'+prefix+'show_new_link_form').fadeIn(200);
		jQuery('#'+prefix+'secure_link_edit_text').hide();
		
		jQuery('#'+prefix+'existing_secure_link_table_container').slideDown();
		
		if(total_new_secure_links > 0) {
			jQuery('#'+prefix+'new_secure_link_table_container').slideDown();
		}
		jQuery('#'+prefix+'secure_page_edit_name_text').hide();
		
		setTimeout(scrollToElement('#'+prefix+'secure_page_edit_name_text',500,-100),250);
			
		
	}
	
	function secure_link_form_delete(prefix,id) {
		jQuery('#'+prefix+'secure_link_row_'+id).remove();
		total_new_secure_links--;
		if(total_new_secure_links<1) {
			
			jQuery('#'+prefix+'new_secure_link_table_container').fadeOut(200);
			
		}
	}
	
	
	function create_optinlink(prefix) {				
				
		optin_link_name = html_encode(jQuery('#'+prefix+'optin_link_name').val());
		optin_link_secure_link = jQuery('#'+prefix+'optin_link_secure_link').val();
		optin_link_optin_form = jQuery('#'+prefix+'optin_form_select').val();
		
		optin_link_secure_link_text = jQuery('#'+prefix+'optin_link_secure_link option:selected').text();
		optin_link_optin_form_text = jQuery('#'+prefix+'optin_form_select option:selected').text();
				
		if(optin_link_name.length < 5 || optin_link_secure_link == 0 || optin_link_optin_form == 0) {
		
			optin_link_form_error = 1;
			error_text = '';
			
			if(optin_link_name.length < 5) { 
				error_text += '<strong>ERROR:</strong> OptinLink name is required and must be at least 5 characters<br>';
			}
			
			if(optin_link_secure_link == 0) { 
				error_text += '<strong>ERROR:</strong> A valid target secure page is not selected<br>';
			}
			
			if(optin_link_optin_form == 0) { 
				error_text += '<strong>ERROR:</strong> A valid optin form is not selected<br>';
			}
					
			jQuery('#'+prefix+'optin_link_warning_text').html(error_text);
			jQuery('#'+prefix+'optin_link_warning_text').hide().fadeIn(200);
			
			
		} else {
					
			if(prefix=='') { 			
				jQuery('#optin_link_form').wrap('<form id="optin_form_id" method="post" action=""></form>');
				jQuery('#optin_form_id').submit();
			}
						
			new_optin_links++;
			total_new_optin_links++;
			optin_link_form_error = 0;		
			jQuery('#'+prefix+'optin_link_warning_text').hide();
			jQuery('#'+prefix+'new_optin_link_table_container').hide().fadeIn(200);
			
			if(jQuery('#'+prefix+'optin_link_not_required').prop('checked')) { optin_link_not_required = 1; } else { optin_link_not_required = 0; }
			if(jQuery('#'+prefix+'getresponse_optin_link_double_optin').prop('checked')) { getresponse_optin_link_double_optin = 1; } else { getresponse_optin_link_double_optin = 0; }
			if(jQuery('#'+prefix+'getresponse_optin_link_unsubscribe').prop('checked')) { getresponse_optin_link_unsubscribe = 1; } else { getresponse_optin_link_unsubscribe = 0; }
			if(jQuery('#'+prefix+'optin_no_redirect').prop('checked')) { optin_no_redirect = 1; } else { optin_no_redirect = 0; }
			if(jQuery('#'+prefix+'optin_open_in_new_tab').prop('checked')) { optin_open_in_new_tab = 1; } else { optin_open_in_new_tab = 0; }
			if(jQuery('#'+prefix+'secure_link_type_1').prop('checked')) { secure_link_type = 1; }
			if(jQuery('#'+prefix+'secure_link_type_2').prop('checked')) { secure_link_type = 2; }	
					
			if(new_optin_links%2==1) {row_class = "opt_table_row"; } else { row_class = "opt_table_row_alt"; }		
			
			html =  '<tr class="'+row_class+'" id="'+prefix+'new_optin_link_row_'+new_optin_links+'">';
			html += '<td style="padding-right:0;"><span id="'+prefix+'new_optin_link_id">'+new_optin_links+'</span>';
			html += '<input name="'+prefix+'new_optin_link_name_'+new_optin_links+'" id="'+prefix+'new_optin_link_name_'+new_optin_links+'" type="hidden" value="'+optin_link_name+'">';
			html += '<input name="'+prefix+'new_optin_link_secure_link_'+new_optin_links+'" id="'+prefix+'new_optin_link_secure_link_'+new_optin_links+'" type="hidden" value="'+optin_link_secure_link+'">';
			html += '<input name="'+prefix+'new_optin_link_optin_form_'+new_optin_links+'" id="'+prefix+'new_optin_link_optin_form_'+new_optin_links+'" type="hidden" value="'+optin_link_optin_form+'">';
			
			if(optin_link_not_required == 1) {
				
				html += '<input name="'+prefix+'new_optin_link_not_required_'+new_optin_links+'" id="'+prefix+'new_optin_link_not_required_'+new_optin_links+'" type="hidden" value="1">';
			
			}
			
			if(optin_no_redirect == 1) {
				
				html += '<input name="'+prefix+'new_optin_no_redirect_'+new_optin_links+'" id="'+prefix+'new_optin_no_redirect_'+new_optin_links+'" type="hidden" value="1">';
			
			}
			
			if(optin_open_in_new_tab == 1) {
				
				html += '<input name="'+prefix+'new_optin_open_in_new_tab_'+new_optin_links+'" id="'+prefix+'new_optin_open_in_new_tab_'+new_optin_links+'" type="hidden" value="1">';
			
			}
			
			if(getresponse_optin_link_double_optin == 1) {
				
				html += '<input name="'+prefix+'new_getresponse_optin_link_double_optin_'+new_optin_links+'" id="'+prefix+'new_getresponse_optin_link_double_optin_'+new_optin_links+'" type="hidden" value="1">';
				
			}
			
			if(getresponse_optin_link_unsubscribe == 1) {
				
				html += '<input name="'+prefix+'new_getresponse_optin_link_unsubscribe_'+new_optin_links+'" id="'+prefix+'new_getresponse_optin_link_unsubscribe_'+new_optin_links+'" type="hidden" value="1">';
				
			}
			
			if(secure_link_type == 1) {
				
				html += '<input name="'+prefix+'new_secure_link_type_'+new_optin_links+'" id="'+prefix+'new_secure_link_type_'+new_optin_links+'" type="hidden" value="1">';
				
			}
			
			if(secure_link_type == 2) {
				
				html += '<input name="'+prefix+'new_secure_link_type_'+new_optin_links+'" id="'+prefix+'new_secure_link_type_'+new_optin_links+'" type="hidden" value="2">';
				
			}
			
			html += '</td>';
			html += '<td>'+optin_link_name+'</td>';
			html += '<td>'+optin_link_secure_link_text+'</td>';
			html += '<td>'+optin_link_optin_form_text+'</td>';
			html += '<td align="right"><a href="javascript:load_edit_optinlink(\''+prefix+'\','+new_optin_links+',\'new_\')">Edit</a><a id="'+prefix+'delete_new_optin_link_'+new_optin_links+'" href="javascript:delete_optinlink(\''+prefix+'\','+new_optin_links+')" class="opt_trash">Trash</a></td></tr>';
				
										
			jQuery(html).hide().appendTo('#'+prefix+'new_optin_link_table > tbody:last').fadeIn(200);			
			
			jQuery('#'+prefix+'optin_link_name').val('');
			
				
		}
				
	}
	
	
	function edit_optinlink(prefix,id,type) {

		optin_link_name = html_encode(jQuery('#'+prefix+'optin_link_name').val());
		optin_link_secure_link = jQuery('#'+prefix+'optin_link_secure_link').val();
		optin_link_optin_form = jQuery('#'+prefix+'optin_form_select').val();
		
		optin_link_secure_link_text = jQuery('#'+prefix+'optin_link_secure_link option:selected').text();
		optin_link_optin_form_text = jQuery('#'+prefix+'optin_form_select option:selected').text();
					
		if(optin_link_name.length < 5 || optin_link_secure_link == 0 || optin_link_optin_form == 0) {
		
			optin_link_form_error = 1;
			error_text = '';
			
			if(optin_link_name.length < 5) { 
				error_text += '<strong>ERROR:</strong> OptinLink name is required and must be at least 5 characters<br>';
			}
			
			if(optin_link_secure_link == 0) { 
				error_text += '<strong>ERROR:</strong> A valid target secure page is not selected<br>';
			}
			
			if(optin_link_optin_form == 0) { 
				error_text += '<strong>ERROR:</strong> A valid optin form is not selected<br>';
			}
					
			jQuery('#'+prefix+'optin_link_warning_text').html(error_text);
			jQuery('#'+prefix+'optin_link_warning_text').hide().fadeIn(200);
			
			setTimeout(scrollToElement('#'+prefix+'optin_link_edit_name_text',500,-100),250);
			
		} else {
			
			optin_link_form_error = 0;		
			jQuery('#'+prefix+'optin_link_warning_text').hide();
			
				
			if(total_new_optin_links>0) {		
				jQuery('#'+prefix+'new_optin_link_table_container').slideDown();
			}
			
			jQuery('#'+prefix+'existing_optin_link_table_container').slideDown();
				
			if(type!='existing_') {
				jQuery('#'+prefix+'optin_link_table_container').hide().fadeIn(200);
			}
			
			
			if(jQuery('#'+prefix+'optin_link_not_required').prop('checked')) { optin_link_not_required = 1; } else { optin_link_not_required = 0; }
			if(jQuery('#'+prefix+'optin_no_redirect').prop('checked')) { optin_no_redirect = 1; } else { optin_no_redirect = 0; }
			if(jQuery('#'+prefix+'optin_open_in_new_tab').prop('checked')) { optin_open_in_new_tab = 1; } else { optin_open_in_new_tab = 0; }
			if(jQuery('#'+prefix+'getresponse_optin_link_double_optin').prop('checked')) { getresponse_optin_link_double_optin = 1; } else { getresponse_optin_link_double_optin = 0; }
			if(jQuery('#'+prefix+'getresponse_optin_link_unsubscribe').prop('checked')) { getresponse_optin_link_unsubscribe = 1; } else { getresponse_optin_link_unsubscribe = 0; }
			if(jQuery('#'+prefix+'secure_link_type_1').prop('checked')) { secure_link_type = 1; }
			if(jQuery('#'+prefix+'secure_link_type_2').prop('checked')) { secure_link_type = 2; }	
			
			html = '';
			
			if(type!='existing_') {
			html += '<td style="padding-right:0; font-style:italic;"><span id="'+prefix+'new_optin_link_id">'+id+'</span></td>';
			}
			
			html += '<td style="font-style:italic">'+optin_link_name+'</td>';
			html += '<td style="font-style:italic">'+optin_link_secure_link_text+'</td>';
			html += '<td style="font-style:italic">'+optin_link_optin_form_text+'</td>';
			html += '<td align="right"><a href="javascript:load_edit_optinlink(\''+prefix+'\',\''+id+'\',\''+type+'\')" onclick="jQuery(\'#'+prefix+'show_new_optin_link_form\').hide();jQuery(\'#'+prefix+'hide_new_optin_link_form\').fadeIn(200);">Edit</a>';
			
			
			if(type!='existing_') {
				html += '<a id="'+prefix+'delete_optin_link_'+id+'" href="javascript:delete_optinlink(\''+prefix+'\',\''+id+'\')" class="opt_trash">Trash</a>';
			}
			html += '<input name="'+prefix+type+'optin_link_name_'+id+'" id="'+prefix+type+'optin_link_name_'+id+'" type="hidden" value="'+optin_link_name+'">';
			html += '<input name="'+prefix+type+'optin_link_secure_link_'+id+'" id="'+prefix+type+'optin_link_secure_link_'+id+'" type="hidden" value="'+optin_link_secure_link+'">';
			html += '<input name="'+prefix+type+'optin_link_optin_form_'+id+'" id="'+prefix+type+'optin_link_optin_form_'+id+'" type="hidden" value="'+optin_link_optin_form+'">';
			
			if(optin_link_not_required == 1) {
				
				html += '<input name="'+prefix+type+'optin_link_not_required_'+id+'" id="'+prefix+type+'optin_link_not_required_'+id+'" type="hidden" value="1">';
			
			}
			
			if(optin_no_redirect == 1) {
				
				html += '<input name="'+prefix+type+'optin_no_redirect_'+id+'" id="'+prefix+type+'optin_no_redirect_'+id+'" type="hidden" value="1">';
			
			}
			
			if(optin_open_in_new_tab == 1) {
				
				html += '<input name="'+prefix+type+'optin_open_in_new_tab_'+id+'" id="'+prefix+type+'optin_open_in_new_tab_'+id+'" type="hidden" value="1">';
			
			}
			
			if(getresponse_optin_link_double_optin == 1) {
				
				html += '<input name="'+prefix+type+'getresponse_optin_link_double_optin_'+id+'" id="'+prefix+type+'getresponse_optin_link_double_optin_'+id+'" type="hidden" value="1">';
				
			}
			
			if(getresponse_optin_link_unsubscribe == 1) {
				
				html += '<input name="'+prefix+type+'getresponse_optin_link_unsubscribe_'+id+'" id="'+prefix+type+'getresponse_optin_link_unsubscribe_'+id+'" type="hidden" value="1">';
				
			}
			
			if(secure_link_type == 1) {
				
				html += '<input name="'+prefix+type+'secure_link_type_'+id+'" id="'+prefix+type+'secure_link_type_'+id+'" type="hidden" value="1">';
				
			}
			
			if(secure_link_type == 2) {
				
				html += '<input name="'+prefix+type+'secure_link_type_'+id+'" id="'+prefix+type+'secure_link_type_'+id+'" type="hidden" value="2">';
				
			}
			
			html += '</td></tr>';
						
			jQuery('#'+prefix+type+'optin_link_row_'+id).hide().html(html).fadeIn(200);					
			
			if(prefix=='wizard_') {			
				jQuery('#'+prefix+'optin_link_button').val('Create Another OptinLink');
			} else {
				jQuery('#'+prefix+'optin_link_button').html('Create Another OptinLink');				
			}
			
			jQuery('#'+prefix+'optin_link_name').val('');
			
			
			jQuery('#'+prefix+'create_optin_link').fadeOut(200);
			jQuery('#'+prefix+'optin_link_cancel_button').fadeOut(200);
			jQuery('#'+prefix+'optin_link_edit_text').fadeOut(200);
			jQuery('#'+prefix+'optin_link_edit_name_text').fadeOut(200);
			jQuery('#'+prefix+'optin_link_button').attr('onclick',"create_optinlink('"+prefix+"')"); 
			jQuery('#'+prefix+'show_new_optin_link_form').fadeIn(200);	
			
			setTimeout(scrollToElement('#'+prefix+'optin_link_edit_name_text',500,-100),250);
			
				
		}
				
	
		
	}
	
	function cancel_edit_optinlink(prefix) {
				
		jQuery('#'+prefix+'existing_optin_link_table_container').slideDown();
		jQuery('#'+prefix+'show_new_optin_link_form').fadeIn(200);	
		jQuery('#'+prefix+'optin_link_cancel_button').fadeOut(200);
		jQuery('#'+prefix+'optin_link_edit_text').fadeOut(200);
		jQuery('#'+prefix+'optin_link_edit_name_text').fadeOut(200);
		jQuery('#'+prefix+'create_optin_link').fadeOut(200);
		
		if(total_new_optin_links>0) {		
			jQuery('#'+prefix+'new_optin_link_table_container').slideDown();
		}
		
		jQuery('#'+prefix+'optin_link_name').val('');		
					
			if(prefix=='wizard_') {			
				jQuery('#'+prefix+'optin_link_button').val('Create Another OptinLink');
			} else {
				jQuery('#'+prefix+'optin_link_button').html('Create Another OptinLink');				
			}
		jQuery('#'+prefix+'optin_link_button').attr('onclick',"create_optinlink('"+prefix+"')"); 
		
		setTimeout(scrollToElement('#'+prefix+'optin_link_edit_name_text',500,-100),250);
	
		
	}
	
	function load_edit_optinlink(prefix,id,type) {			
		
		jQuery('#'+prefix+'existing_optin_link_table_container').slideUp();			
		jQuery('#'+prefix+'new_optin_link_table_container').slideUp();				
		
		jQuery('#'+prefix+'optin_link_edit_name_text').hide().html('<b>Editing OptinLink:</b> '+jQuery('#'+prefix+type+'optin_link_name_'+id).val()).fadeIn(200);
		
		jQuery('#'+prefix+'hide_new_optin_link_form').fadeOut(200);
		jQuery('#'+prefix+'show_new_optin_link_form').fadeOut(200);
		
		jQuery('#'+prefix+'optin_link_name').val(jQuery('#'+prefix+type+'optin_link_name_'+id).val());
		jQuery('#'+prefix+'optin_link_secure_link').val(jQuery('#'+prefix+type+'optin_link_secure_link_'+id).val());
		jQuery('#'+prefix+'optin_form_select').val(jQuery('#'+prefix+type+'optin_link_optin_form_'+id).val());
		
		if(jQuery('#'+prefix+type+'optin_link_not_required_'+id).val()==1) { 
			jQuery('#'+prefix+'optin_link_not_required').attr('checked','checked');	
		} else {
			jQuery('#'+prefix+'optin_link_not_required').removeAttr('checked');				
		}
		
		if(jQuery('#'+prefix+type+'getresponse_optin_link_double_optin_'+id).val()==1) {
			jQuery('#'+prefix+'getresponse_optin_link_double_optin').attr('checked','checked');			
		} else {
			jQuery('#'+prefix+'getresponse_optin_link_double_optin').removeAttr('checked');				
		}
			
		if(jQuery('#'+prefix+type+'getresponse_optin_link_unsubscribe_'+id).val()==1) {
			jQuery('#'+prefix+'getresponse_optin_link_unsubscribe').attr('checked','checked');
		} else {
			jQuery('#'+prefix+'getresponse_optin_link_unsubscribe').removeAttr('checked');				
		}
			
		if(jQuery('#'+prefix+type+'optin_open_in_new_tab_'+id).val()==1) {
			jQuery('#'+prefix+'optin_open_in_new_tab').attr('checked','checked');
		} else {
			jQuery('#'+prefix+'optin_open_in_new_tab').removeAttr('checked');				
		}
			
		if(jQuery('#'+prefix+type+'optin_no_redirect_'+id).val()==1) {
			jQuery('#'+prefix+'optin_no_redirect').attr('checked','checked');
		} else {
			jQuery('#'+prefix+'optin_no_redirect').removeAttr('checked');				
		}
		
		if(jQuery('#'+prefix+type+'secure_link_type_'+id).val()==1 || jQuery('#'+prefix+type+'secure_link_type_'+id).val()=='Popup') {
			jQuery('#'+prefix+'secure_link_type_1').attr('checked','checked');
		} else if(jQuery('#'+prefix+type+'secure_link_type_'+id).val()==2 || jQuery('#'+prefix+type+'secure_link_type_'+id).val()=='Direct') {
			jQuery('#'+prefix+'secure_link_type_2').attr('checked','checked');			
		}

		
		jQuery('#'+prefix+'create_optin_link').fadeIn(200);
		jQuery('#'+prefix+'optin_link_cancel_button').fadeIn(200);
		if(prefix=='wizard_') {		
		jQuery('#'+prefix+'optin_link_button').val('Save OptinLink Changes');
		} else {
		jQuery('#'+prefix+'optin_link_button').html('Save OptinLink Changes');
		}
		jQuery('#'+prefix+'optin_link_edit_text').fadeIn(200);
		jQuery('#'+prefix+'optin_link_button').attr('onclick',"edit_optinlink('"+prefix+"','"+id+"','"+type+"');"); 
	
		
		
		setTimeout(scrollToElement('#'+prefix+'optin_link_edit_name_text',500,-100),250);
		
		
	}
	
	function delete_optinlink(prefix,id) {
		
		jQuery('#'+prefix+'new_optin_link_row_'+id).remove();
		jQuery('#'+prefix+'new_details_row_'+id).remove();
		total_new_optin_links--;
		if(total_new_optin_links<1) {
			
			jQuery('#'+prefix+'new_optin_link_table_container').fadeOut(200);
			
		}
		
	}
	
	/*Function overrides for the preview form - */
	var $current_previewed_form_container = null;
	var $current_previewed_form_overlay = null;
	function opt_preview_form(prefix){
		var id_to_preview = jQuery('#'+prefix+'optin_form_select').val();
		$current_previewed_form_container = jQuery('#optin_link_form_container' + id_to_preview);
		$current_previewed_form_overlay = jQuery('#optin_link_overlay' + id_to_preview);
		
		$current_previewed_form_container.fadeIn();
		$current_previewed_form_overlay.fadeIn();
	}
	
	function opt_form_hover(form_id,split_test_item_id){
	}
	
	function opt_form_click(form_id,split_test_item_id){
	}
	
	function opt_form_close(form_id,split_test_item_id){
		$current_previewed_form_container.fadeOut();
		$current_previewed_form_overlay.fadeOut();
		
		$current_previewed_form_container = null;
		$current_previewed_form_overlay = null;
	}
	
	function scrollToElement(selector, time, verticalOffset) {
    time = typeof(time) != 'undefined' ? time : 1000;
    verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
    element = jQuery(selector);
    offset = element.offset();
    offsetTop = offset.top + verticalOffset;
    jQuery('html, body').animate({
        scrollTop: offsetTop
    }, time);
	}