<?php
/*
License: Copyright 2010 Daniel Watrous, All Rights Reserved (see README.txt in the plugin directory)
*/

// ajax inclusion for WordPress
function process_optin_form_javascript() {
?>
<script type="text/javascript" >
var processButton = '<a href="" onclick="processForm(); return false;"><img src="<?php echo get_plugin_url() ?>/images/process_form.png" style="border: 0px;"></a>';
var loadingButton = '<img src="<?php echo get_plugin_url() ?>/images/ajax-loading.gif" style="border: 0px;">';
function processForm() {
	
	var data = {
		action: 'process_optin_form',
		optinform: document.optincrushersettings.optincrusher_rawhtmloptinform.value
	};
	
	// hide button, show loading
	ReplaceContentInContainer('process_button', loadingButton);

	// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
	jQuery.post(ajaxurl, data, function(response) {
		document.optincrushersettings.optincrusher_processedoptinform.value = response;
		ReplaceContentInContainer('process_button', processButton);
	});
};

function ReplaceContentInContainer(id,content) {
	var container = document.getElementById(id);
	container.innerHTML = content;
}
</script>
<?php
}

// test script for form extraction
global $regex_form, $regex_input, $regex_htmltag, $regex_attribute, $regex_tagtype, $unwanted_attributes;
$regex_form = '/(<form\b[^>]*>)(.*?)(<\/form>)/ims';
$regex_input = '/(.*?)(<input\b[^>]*>)/ims';
$regex_htmltag = '/(<[^>]+>)/ims';
$regex_attribute = '/([^\s]*?)=(")?(?(2)(.*?)"|(.*?)[\s|>])/ims';
$regex_tagtype = '/<([a-zA-Z]*?)\s/i';
$unwanted_attributes = array('style','class','id','onsubmit','target');
global $style_class_submit;
$style_class_label = "formidentifier";
$style_class_submit = "submitbutton";

function process_optin_form_callback() {
	global $regex_form, $regex_input, $style_class_label;

	$optinform = stripslashes ($_POST['optinform']);

	// process form
	preg_match($regex_form, $optinform, $form_components);
	//echo str_replace ("\n" ,'<br />' , htmlentities(print_r($form_components, true)));
	// clear style information from opening form tag
	$newform = clear_tag_style($form_components[1])."\n";
	// process inputs and labels
	preg_match_all ($regex_input, $form_components[2], $inputs);
	foreach ($inputs[2] as $key=>$input) {
		$input_tag = clear_tag_style($input);
		$label = prepare_label($inputs[1][$key], $style_class_label);
		$newform .= $label.' '.$input_tag."\n";
	}
	$newform .= $form_components[3]."\n";
	
	echo $newform;
	die();
}

function clear_tag_style($tag) {
	global $regex_tagtype, $regex_attribute, $unwanted_attributes, $style_class_submit;
	preg_match ($regex_tagtype, $tag, $htmltagtype);
	preg_match_all ($regex_attribute, $tag, $attributes);
	//echo str_replace ("\n" ,'<br />' , htmlentities(print_r($attributes, true)));
	$newtag = '<'.$htmltagtype[1];
	foreach ($attributes[1] as $key=>$attribute) {
		if (!in_array ($attribute, $unwanted_attributes)) {
			$attribute_value = (($attributes[2][$key]=='"') ? $attributes[3][$key]:$attributes[4][$key]);
			$newtag .= ' '.$attribute.'="'.$attribute_value.'"';
			if ($attribute=='type' && $attribute_value=='submit') $newtag .= ' class="'.$style_class_submit.'"';
		}
	}
	$newtag .= ' />';
	return $newtag;
}

function prepare_label($block, $labelclass) {
	global $regex_htmltag;
	$label = trim( preg_replace ($regex_htmltag, '', $block));
	if ($label != '') $label = '<span class="'.$labelclass.'">'.$label.'</span>';
	return $label;
}

?>