/*
	History.Observer.start();

	History.Registry.set({
		id: 'eshop',
		onStateChange: function(state) {
			af = History.get('af');

			switch (state) {
				case 'orders':
					if (af == "orders" ||
						af == "order_details") {
						ajax_update_af('/office/shop/orders.php');
					} else if (af == "new_order") { // Manual Order Input
						ajax_update_af('/office/shop/new_order.php');
					} else {
						//ajax_update('get', '/office/shop/orders.php?af=orders', 'app_frame');
						History.set('af', 'orders');
						ajax_update_af('/office/shop/orders.php');
					}
					break;
				case 'products':
					if (af == "view_products" ||
						af == "add_product" ||
						af == "edit_product" ||
						af == "csv_import") {
						ajax_update_af('/office/shop/products.php');
					} else {
						History.set('af', 'view_products');
						ajax_update_af('/office/shop/products.php');
					}
					break;
				case 'pages':
					History.set('af', 'pages');
					ajax_update_af('/office/shop/pages.php');
					break;
				case 'images':
					History.set('af', 'upload_images');
					ajax_update_af('/office/shop/images.php');
					break;
				case 'settings':
					History.set('af', 'settings');
					ajax_update_af('/office/shop/settings.php');
					break;
			}
		}
	});

	History.Registry.set({
		id: 'webb', // web builder
		onStateChange: function(state) {
			af = History.get('af');

			switch (state) {
				case 'templates':
					if (af != "templates") {
						History.set('af', 'templates');
					}
					ajax_update_af('/office/web_builder/design.php');
					break;
				case 'images':
					if (af != "images") {
						History.set('af', 'images');
					}
					ajax_update_af('/office/web_builder/design.php');
					break;
				case 'colours':
					if (af != "colours") {
						History.set('af', 'colours');
					}
					ajax_update_af('/office/web_builder/design.php');
					break;
				case 'pages':
					if (af != "pages") {
						History.set('af', 'pages');
					}
					ajax_update_af('/office/web_builder/pages.php');
					break;
				case 'seo':
					if (af != "seo") {
						History.set('af', 'seo');
					}
					ajax_update_af('/office/web_builder/pages.php');
					break;
				case 'blog':
					if (af != "blog") {
						History.set('af', 'blog');
					}
					ajax_update_af('/office/web_builder/pages.php');
					break;
				case 'template_builder':
					if (af != "template_builder") {
						History.set('af', 'template_builder');
					}
					ajax_update_af('/office/web_builder/design.php');
					break;
			}
		}
	});
*/
	// **********************************************************************************************

	function ajax_update(method, url, div2update, callback)
	{
		url_pars = url.split("?");
		var url = url_pars[0];
		var pars = '?'+url_pars[1];
		
		/*var myAjax = new Ajax.Updater(
					{success: div2update}, 
					url, 
					{
						method: method, 
						parameters: pars,
						evalScripts: true 
						*//*onFailure: reportError*//*
					});*/
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				evalScripts: true, 
				/*onFailure: reportError*/
				onComplete: function(request)	{
					if (div2update == "ajax_status") {
						ajax_display_status(request.responseText);
					} else if(div2update) {
						d2u = div2update.split(",");
						d2u_l = d2u.length;
						if (d2u_l > 1) {
							rr = request.responseText.split("%ajax%");
							for (i=0; i < d2u_l; i++) {
								d2u2 = d2u[i].replace(/[\r\n\t]/g, '');
								if (d2u2 == "ajax_status") {
									ajax_display_status(rr[i]);
								} else {
									$(d2u2).update(rr[i]);
									//ajax_hilight_fade(d2u2, 0);
								}
							}
						} else {
							$(div2update).update(request.responseText);
							$(div2update).scrollTop = 0;
							//ajax_hilight_fade(div2update, 0);
						}
					}
					if (callback && typeof(callback) == 'string') {
					     callback = eval(callback);
					     callback(request.responseText);   
					}
				}
			});
		return false;
	}

	function ajax_status_hilight(div2update, type, fade_away)
	{
		Effect.ScrollTo(div2update, { duration: 0.25 });

		if (type == "success") {
			bgcolor = "#ffffcc";
			$('ajax_status').setStyle({
				color: '666600', 
				background: '#ffffcc', 
				border: '1px solid #aaaa88'
			});
		} else if (type == "warning") {
			bgcolor = "#ffddcc";
			$('ajax_status').setStyle({
				color: '996600', 
				background: bgcolor, 
				border: '1px solid #ccaa88'
			});
		} else if (type == "error") {
			bgcolor = "#ffcccc";
			$('ajax_status').setStyle({
				color: '660000', 
				background: bgcolor, 
				border: '1px solid #aa8888'
			});
		}

		$(div2update).show();
		new Effect.Highlight(div2update, { startcolor: '#ffff99', endcolor: bgcolor, duration: 0.5, keepBackgroundImage: false });
		if (fade_away) {
			$(div2update).fade({ duration: 1.0, from: 1, to: 0, delay: 5 });
		}
	}

	/*
	function ajax_post_update(form_id, div2update)
	{
		var url = '<?=$_SERVER['PHP_SELF']?>';
		var pars = Form.serialize($(form_id));
		
		var myAjax = new Ajax.Updater(
					{success: div2update}, 
					url, 
					{
						method: 'post', 
						parameters: pars, 
						evalScripts: true 
						*/ /*onFailure: reportError*/ /*
					});
	}
	*/

	function ajax_submit(url, form_id, div2update, callback)
	{
		var pars = Form.serialize($(form_id));
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars, 
				evalScripts: true, 
				/*onFailure: reportError*/
				onComplete: function(request)	{
					if (div2update == "ajax_status") {
						ajax_display_status(request.responseText);
					} else if(div2update) {
						d2u = div2update.split(",");
						d2u_l = d2u.length;
						if (d2u_l > 1) {
							rr = request.responseText.split("%ajax%");
							for (i=0; i < d2u_l; i++) {
								d2u2 = d2u[i].replace(/[\r\n\t]/g, '');
								if (d2u2 == "ajax_status") {
									ajax_display_status(rr[i]);
								} else {
									$(d2u2).update(rr[i]);
									$(d2u2).scrollTop = 0;
									//ajax_hilight_fade(d2u2, 0);
								}
							}
						} else {
							$(div2update).update(request.responseText);
							$(div2update).scrollTop = 0;
							//ajax_hilight_fade(div2update, 0);
						}
					}
					//if (callback) { alert(typeof(callback)); }
					if (callback && typeof(callback) == 'string') {
					     callback = eval(callback);
					     callback(request.responseText);   
					}
				}
			});
	}

	function ajax_display_status(responseText) 
	{	
		r = responseText.split("%ajax_status%");
		type = r[0];
		type = type.replace(/[\r\n\t]/g, '');
		response = r[1];
		div2update = "ajax_status";
		if (type == "success") {
			$(div2update).update(response);
			ajax_status_hilight(div2update, type, 1);
		} else if (type == "error" || type == "warning") {
			$(div2update).update(response);
			ajax_status_hilight(div2update, type, 0);
		} /*else {
			$(div2update).update(request.responseText);
			ajax_status_hilight(div2update, type, 0);
		}*/
	}

	function ajax_update_hash(hash_url)
	{
		af_url  = location.hash.split("&af=");
		if (af_url.length) {
			location.hash = af_url[0] + "&"+ hash_url;
		} else {
			location.hash = location.hash + "&"+ hash_url;
		}
	}

	function ajax_set_hash(hash_url)
	{
		location.hash = hash_url;
	}

	function ajax_update_af(php_file) 
	{
		af_url  = location.hash.split("&af=");
		af_url2 = "?af="+af_url[1];
		ajax_update('get', php_file + af_url2, 'app_frame');
	}


function addHiddenInputField(formElement, fieldName, fieldValue) {
   var inputElement = document.createElement("input")
   inputElement.setAttributeNode(createHtmlAttribute("type", "hidden"))
   inputElement.setAttributeNode(createHtmlAttribute("name", fieldName))
   inputElement.setAttributeNode(createHtmlAttribute("value", fieldValue))
   formElement.appendChild(inputElement)
   return
}

function createHtmlAttribute(name, value) {
   var attribute = document.createAttribute(name)
   attribute.nodeValue = value
   return attribute
}

