/*
 * jQuery WG FORM AJAXIFY
 * Copyright (c) 2009 Roberto Lee (webgenerator.nl)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2009-12-10
 * Rev: 1
 */
/*
REQUIREMENTS

The post page must contain result wrapped around the div defined in the call.
*/
var regex_scriptstring = new RegExp("script","gi"); //Find all script tags
var regex_datachecker = new RegExp("(?:\.js|\<\/head\>)","gi"); // check for .js string and the closing head tag.
var nameofform = "form_ajaxified";
var re_formsdots = new RegExp("•","gi");
//var fullpath_formsmodule = "http://www.dijkstravoermans.wg02.webgenerator.nl/forms/";

(function($){
     $.fn.extend({
         WG_Forms_Ajaxify: function(options) { //Select the form to submit and process
	        var defaults = {
	        	source_url: '',
	        	source_container_form: '', //The form object
	        	on_post_elem_messages_result: 'div#frm_messages',
	        	on_post_elem_messages_success: 'div#success',
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);


            return $(this).each(function(idx) {
            	var html_elem = $(this);

            	//RETRIEVE FORM
				if($(html_elem).is('*'))
				{
							$.ajax({
								type: "GET",
								async: false,
								cache: false,
								url: options.source_url,
								beforeSend: function(){
								},
								success: function(data){
									$(html_elem).append("<form id=\""+nameofform+"\"/>");

									var fullpath_formsmodule_arr = options.source_url.split("/");
									var fullpath_formsmodule = options.source_url.replace(fullpath_formsmodule_arr[fullpath_formsmodule_arr.length-1],'');

									data = data.replace('captcha.aspx',fullpath_formsmodule+'captcha.aspx');
									var ajax_result = $('<div/>').append($.trim(data.replace(regex_scriptstring,"scriptdisabled"))); //Rename the script tags, to disable it
									var ajax_result_form = ajax_result.find(options.source_container_form).is('*') ? ajax_result.find(options.source_container_form).html() : "";
									$(ajax_result_form).find("img").attr('src','bbb');

									if(ajax_result_form != "") { $(html_elem).find("form#" + nameofform).append(ajax_result_form) } //Original form

									//alert($(html_elem).find("form#" + nameofform).html());
									$(html_elem).find("form#" + nameofform).WG_Forms_Ajaxify_Process({
										process_url: options.source_url,
							        	on_post_elem_messages_result: options.on_post_elem_messages_result,
							        	on_post_elem_messages_success: options.on_post_elem_messages_success
									});
								},
								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 + "<br/>Probeer het later nog eens.");
								},
								complete: function(){
									options.cb.call();
								}
						});
				}


            });
		}
    });
})(jQuery);


(function($){
     $.fn.extend({
         WG_Forms_Ajaxify_Process: function(options) {
	        var defaults = {
	        	process_url: '',
	        	on_post_elem_messages_result: '',
	        	on_post_elem_messages_success: '',
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);
            return $(this).each(function(idx) {
				$(this).submit(function() {
									$.ajax({
											type: "POST",
											async: false,
											cache: false,
											url: options.process_url,
											data: $(this).serialize(),
											beforeSend: function(){
											},
											success: function(data){
													var new_ajaxpost_result = "";
													var ajaxpost_result = $('<div/>').append($.trim(data.replace(regex_scriptstring,"scriptdisabled")));

													if(ajaxpost_result.find(options.on_post_elem_messages_result).is('*')) //are there messages?
													{
														new_ajaxpost_result = ajaxpost_result.find(options.on_post_elem_messages_result).html();
														{
															if($(options.elem_form_messages).is('*'))
																$(options.elem_form_messages).html(new_ajaxpost_result);
															else
																alert(" " + $.trim(($(new_ajaxpost_result).text()).replace(re_formsdots, "\n")));
														}
													}
													if(ajaxpost_result.find(options.on_post_elem_messages_success).is('*')) //did the form resulted in a success?
													{
														new_ajaxpost_result = ajaxpost_result.find(options.on_post_elem_messages_success).html();
														$(form_elem).html(new_ajaxpost_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 + "<br/>Probeer het later nog eens.");
											},
											complete: function(){
												options.cb.call();
											}
									});



								return false; //Disable the normal submit
				});

            });
		}
    });
})(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>
*/
