/*
 * jQuery WG AJAXPOST
 * Copyright (c) 2009 Roberto Lee (webgenerator.nl)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2009-09-02
 * Rev: 1
 */
/*
REQUIREMENTS

The post page must contain result wrapped around the div defined in the call.
*/
(function($){
     $.fn.extend({
         WG_Forms_AjaxPost: function(options) { //Select the form to submit and process
	        var defaults = {
	        	url: '',
	        	defaultvalue_attribute: 'rel',
	        	fields_and_values: {},
	        	dest_elem_result_messages: 'div#frm_messages',
	        	dest_elem_success_message: 'div#success',
	        	elem_form_messages: 'div#form_messages',
	        	message_on_ajax_error: '',
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);
            return $(this).each(function(idx) {
            	var form_elem = $(this);

				$(this).submit(function() {
					var posturl = $.trim(options.url) != ''? options.url : $(this).attr('action');
					var poststring = "";
					$.each(options.fields_and_values, function(i, val) {
						var inputvalue = $(eval("options.fields_and_values." + i)).val();
						var defaultvalue= $.trim(options.defaultvalue_attribute) != "" ? $(eval("options.fields_and_values." + i)).attr(options.defaultvalue_attribute) : "";
						poststring += "&" + i + "=";
						poststring += inputvalue != defaultvalue ? inputvalue : "";
					});

					if(posturl != "")
					{
						$.ajax({
								type: "POST",
								async: false,
								cache: false,
								url: posturl + "&action=process",
								data: poststring,
								beforeSend: function(){
								},
								success: function(data){
										var regex_scriptstring = new RegExp("script","gi");
										var ajax_result = $('<div/>').append($.trim(data.replace(regex_scriptstring,"scriptdisabled")));

										var new_result = "";
										if(ajax_result.find(options.dest_elem_result_messages).is('*'))
										{
											new_result = ajax_result.find(options.dest_elem_result_messages).html();
											var regex_datachecker = new RegExp("(?:\.js|\<\/head\>)","gi"); // check for .js string and the closing head tag.
											//if(new_result.indexOf(".js") == -1)
											if(!regex_datachecker.test(new_result))
											{
												if($(options.elem_form_messages).is('*'))
												{
													$(options.elem_form_messages).html(new_result);
													//$(options.elem_form_messages).html($(new_result).text());
												}
												else
												{
													//var re = new RegExp(String.fromCharCode(183),"gi");
													var re = new RegExp("•","gi");
													alert(" " + $.trim(($(new_result).text()).replace(re, "\n")));
												}
											}
										}
										if(ajax_result.find(options.dest_elem_success_message).is('*'))
										{
											new_result = ajax_result.find(options.dest_elem_success_message).html();
											$(form_elem).html(new_result);
										}
								},
								error: function(xhr, ajaxOptions, thrownError){
									var errMsg =  $.trim(options.message_on_ajax_error) != "" ? options.message_on_ajax_error : xhr.statusText + " ("+xhr.status+")";
									$(form_elem).html(errMsg);
								},
								complete: function(){
									options.cb.call();
								}
						});
					}
				    return false;
				});
            });
		}
    });
})(jQuery);



/*
 * jQuery WG Forms Default Value
 * Copyright (c) 2009 Roberto Lee (webgenerator.nl)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2009-09-02
 * Rev: 1
 */
(function($){
     $.fn.extend({
         WG_Forms_DefaultValue: function(options) { //Select the form to submit and process

			var defaults = {
	        	default_value_color: '#ACACAC',
	        	filled_value_color: '#000000',
	        	defaultvalue_attribute: 'rel',
				cb: function(){}
	        };

	        var options = $.extend(defaults, options);

            return $(this).each(function(idx) {
            	var inputfield = $(this);
				var defaultvalue = inputfield.attr(options.defaultvalue_attribute);

				inputfield.bind("focus click blur",function(){
					inputfield.css('color', options.filled_value_color);
					if (inputfield.val() == defaultvalue)
					{
						inputfield.val("");
					}
				});

				inputfield.blur(function(){
					SetDefaultValue();
				});

				function SetDefaultValue()
				{
					if ((inputfield.val() == "") || (inputfield.val() == defaultvalue))
					{
						inputfield.val(defaultvalue).css('color', options.default_value_color);
					}
				}
				SetDefaultValue();

            	options.cb.call();
            });

		}
    });
})(jQuery);



(function($) {

   $.fn.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
      return this;
   }
})(jQuery);

jQuery.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
};

/*EXAMPLE USAGE
<script type="text/javascript" src="http://www.seo-coach.wg02.webgenerator.nl/jquery_wg_form_ajaxpost.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    	$("#ajaxform").WG_Forms_AjaxPost({
    		fields_and_values: {
    			"field_14": $("input[name=frm_bedrijfsnaam]"),
    			"field_15": $("input[name=frm_contactperson]"),
    			"field_16": $("input[name=frm_website]"),
    			"field_17": $("input[name=frm_email]"),
    			"field_18": $("input[name=frm_telephone]")
    		}
    	});
    	$("#ajaxform input[type=text]").WG_Forms_DefaultValue();
    });
</script>
<style>
	.ajaxinp
	{
	margin: 0px 0px 3px 0px;
	width: 190px;
	}

	.btn_send
	{
		border: 1px solid #FFF;
		background-color: #64B054;
	}
</style>

<form id="ajaxform" action="http://www.seo-coach.wg02.webgenerator.nl/forms/?fid=2">
	<input class="ajaxinp" type="text" name="frm_bedrijfsnaam" 	id="frm_bedrijfsnaam" 	rel="Bedrijfsnaam" value="" />
	<input class="ajaxinp" type="text" name="frm_contactperson" id="frm_contactperson" 	rel="Contactpersoon" value="" />
	<input class="ajaxinp" type="text" name="frm_website" 		id="frm_website" 		rel="Website" value="" />
	<input class="ajaxinp" type="text" name="frm_email" 		id="frm_email" 			rel="E-mail adres" value="" />
	<input class="ajaxinp" type="text" name="frm_telephone" 	id="frm_telephone" 		rel="Telefoon" value="" />
	<input type="submit" value="Verzend" name="Verzend" id="button" class="btn_send"/>
</form>

<div id="form_messages" style="clear: both;"></div>
*/
