﻿$(document).ready(function() {
	PHP_Form.WYSIWYG.load();
	PHP_Form.datepick_bind();
	/*
	$('#loadingBox').ajaxSend(function(r,s){
		$('#loadingBox').show();
	});
	
	$("#loadingBox").ajaxComplete(function(request, settings){
	   $('#loadingBox').hide();
	 });
	*/
});

PHP_Form = {
	AJAX_fetch_url: '/3rd-party/Forms/lib/FetchAJAX.php',
	/**
	 * Fetch results from AJAX call.
	 * @param {} data
	 *  Includes: formID, hash, f_name, [c_name], params
	 * @param {} successfunc
	 * @param {} errorfunc
	 */
	FetchAJAX: function(data, successfunc, errorfunc) {
		$.ajax({
			type: 'post',
			data: data,
			url: PHP_Form.AJAX_fetch_url,
			dataType: 'json',
			success: successfunc,
			error: errorfunc
		});
	},
	WYSIWYG: {
		default_settings: 'webware_ardc',
		processed_textareas: [],
		generate: function(mode) {
			if (typeof(WYSIWYG) == 'object') {
				$('textarea.wysiwyg-textarea:visible:not(.wysiwyg-processed)').each(function(){
					var settings = eval(this.getAttribute('settings') != '' ? this.getAttribute('settings') : PHP_Form.WYSIWYG.default_settings);
					if (mode == 'load') {
						WYSIWYG.attach(this.id, settings);
					} else if (mode == 'attach') {
						WYSIWYG.setSettings(this.id, settings);
						WYSIWYG_Core.includeCSS(WYSIWYG.config[this.id].CSSFile);
						WYSIWYG._generate(this.id, settings);
					}
					PHP_Form.WYSIWYG.processed_textareas.push(this.id);
					$(this).addClass('wysiwyg-processed');
				});
			}
		},
		load: function() {
			PHP_Form.WYSIWYG.generate('load');
		},
		attach: function() {
			PHP_Form.WYSIWYG.generate('attach');
		}
	},
	datepick_bind: function() {
		if ($.datepicker) {
			$('input.datepick').datepicker({dateFormat: 'dd/mm/yy'});
		}
	}
}
