/*
 *
 *	AJAX REQUEST FUNCTIONS
 *
 */


//
//	make an internal request using prototype ajax framework
//
	//	first function = defaults to sending via 'get'
		function internal_request(action,name,value)
		{
			int_req(action,name,value,"get");
		}
	
	//	second function = sends request via 'post'
		function internal_request_post(action,name,value)
		{
			//value = escape(value);
			int_req(action,name,value,"post");
		}
		
	//	third function = generic send request
		function int_req(action,name,value,method)
		{					
		//	send the request to internal_request.php
			$.ajax({
				data: "action=" + action + "&name=" + name + "&value=" + escape(value),
				dataType: "json",
				success: processResponse,
				type: method,
				url: "http://www.diagacht.co.uk/functions/internal_request.php"
			});
		}
		
	//	process AJAX response
		var processResponse = function(t)
		{
		//	various functions
			var do_nothing = function(t) {
				return true;
			}
			
			var show_response = function(t) {
				alert(t.toString);
			}
			
			var refresh = function(t) {
				window.location.reload();
			}

		//	perform response if result
			if(t.result == "true" || t.func != "") {
				var e = t.func + "(t)";
				eval(e);
			}else{
				$("error_text").innerHTML = t.message;
			}
		}





/*
 *
 *	USER FORM VALIDATION
 *
 */


function show_error_msg(error_msg){
	$("#error_text").text(error_msg);
}

function show_star(id,show){	
	if(show == true)
		$(id).text("*");
	else
		$(id).text("");
}

function check_value(l){
	if(l.length < 4){
		alert("Please enter a search term of at least 4 letters.");
		return false;
	}
}





/*
 *
 *	ADMIN AJAX FUNCTIONS
 *
 */


function simple_add_edit(name,unique_id,original,description)
{
	if(original.substr(0,1) == "#"){
		original = $(original).html();
	}

	if(new_value = prompt("Please enter the new " + description + ".",original)){
		internal_request("database",name,unique_id + "~" + new_value);
	}
}

function simple_remove(name,unique_id)
{
	var proceed = confirm("Are you sure you want to remove this value?");
	if(proceed) internal_request("database",name,unique_id+"~remove");
}




/* For adding, editing, deleting subsections */

	function subsection_edit(subsection,original)
	{
		var new_name = prompt("Please enter a new name for this subsection.",original) || false;
		if(new_name.length > 0) internal_request("custom","subsection_edit",subsection + "~" + new_name);
	}
	
	function subsection_add(section,first)
	{
		if(first){
			var proceed = confirm("If you add a new subsection, all items in this section\nwill be added to the new subsection.\n\n Do you want to proceed?");
		}else{
			var proceed = true;
		}		
		
		if(proceed)
		{
			var new_name = prompt("Please enter a name for the new subsection.") || false;
			if(new_name.length > 0) internal_request("custom","subsection_add",section + "~" + new_name + "~" + first);
		}
	}
	
	function subsection_delete(subsection)
	{
		var proceed = confirm("Are you sure you want to delete this subsection?");
		if(proceed) internal_request("custom","subsection_delete",subsection);
	}
	
/* For adding, editing and deleting items */

	function item_add(subsection)
	{
		var new_name = prompt("Please enter a name for this item.") || false;
		if(new_name.length > 0) internal_request("custom","item_add",subsection + "~" + new_name);
	}
	
	function item_rename(item,name)
	{
		var new_name = prompt("Please enter a new name for this item.",name) || false;
		if(new_name.length > 0) internal_request("custom","item_rename",item + "~" + new_name);
	}
	
	function item_delete(item)
	{
		var proceed = confirm("Are you sure you want to delete this item?");
		if(proceed) internal_request("custom","item_delete",item);
	}
	
	function item_attachment_add()
	{
		$("#item_file").attr("class","show")		
		$("#item_file_description").attr("class","hide");
	}
	
	function item_attachment_cancel()
	{
		$("#item_file").attr("class","hide")		
		$("#item_file_description").attr("class","show");
	}
	
	function item_attachment_ftp(item)
	{
		if(typeof(item) == "string")
		{
			internal_request("custom","item_attachment_add_ftp",item);
		}
		else if(typeof(item) == "number")
		{
			var popup = window.open("admin/upload_ftp.php?" + item);
		}
	}
	
	function item_attachment_remove(file)
	{
		var proceed = confirm("Are you sure you want to remove this attached file?");
		if(proceed) internal_request("custom","item_attachment_remove",file);
	}
	
	function item_attachment_remove_all(item)
	{
		var proceed = confirm("Are you sure you want to remove all attached files?");
		if(proceed) internal_request("custom","item_attachment_remove_all",item);
	}
	
/* Save a fulltext variable */

	function save_fulltext(unique_id,source,save_function)
	{			
		source = encode(source);
		internal_request_post("database",save_function,unique_id + "~" + source);
	}
	
/* Saves HTML from the admin item page */

	function save_html(unique_id,source){
		save_fulltext(unique_id,source,"item_html");
	}
	
/* Edit and save fulltext site variable */

	function edit_fulltext_site_var(unique_id)
	{
		var source = escape($("#new_" + unique_id).val());
		var popup = window.open("admin/fulltext/.php?unique_id=" + unique_id + "&save_function=save_fulltext_site_var&html=" + source);
	}
	
	function save_fulltext_site_var(unique_id,source){
		save_fulltext(unique_id,source,"site_var_fulltext");
	}





/*
 *
 *	SITE-SPECIFIC FORM FUNCTIONS
 *
 */


function show_error_msg(error_msg){
	var eL = document.getElementById("error_text");
	if(eL.className == "error_hide"){
		eL.className = "error_show";
	}
	eL.innerHTML = error_msg;
}

function show_star(id,show){
	var eL = document.getElementById(id);
	
	if(show == true)
		eL.innerHTML = "*";
	else
		eL.innerHTML = "";
}





/*
 *
 *	BEHAVIOUR FRAMEWORK - RULES
 *
 */


var main_rules = {

/* Main Side */

	/* Header */
	
		"#quick_search" : function(el){
			el.onsubmit = function(){
				return check_value($("#what").val(),"#what");
			}
		},


/* Admin Side */

	/* Home */
	
		"form.vars_group tr" : function(el){
			var changed_bgcolor = "#FF9933";
			var current_bgcolor = "white";
		
			el.onmouseover = function(){
				$(el).css("backgroundColor","#FFCC66");
			};			
			el.onmouseout = function(){
				$(el).css("backgroundColor",current_bgcolor);
			};
			el.onchange = function(){
				current_bgcolor = changed_bgcolor;
			};
		},
		
	/* Item */
	
		"#save_fulltext" : function(el){
			el.onclick = function(){
			//	show 'saving' text	
				$("#saving").html("Saving...");
				$("#saving").css("display","inline");
				
				var save = function(){
				//	create wym instance and update
					var wym = $.wymeditors(0);
					wym.update();
					
				//	get info				
					var id = $("#unique_id").html();
					var sc = wym.xhtml();
					
				//	save using Ajax save function
					var allowed_save_functions = new Array("save_fulltext","save_html");
					var save_function = $("#save_function").html();
					
					if($.inArray(save_function,allowed_save_functions))
					{
						var s = save_function + "(id,sc);";
						eval(s);
					}
				}
				
				window.setTimeout(save,100); // this is because in Firefox 'Saving text...' won't appear straight away
				return false;
			}
		},
		
		"#published" : function(el){
			el.onclick = function(){
				var id = $("#unique_id").html();
				var ch = el.checked ? 1 : 0;
				internal_request("database","item_published",id + "~" + ch);
			}
		}
	
};

Behaviour.register(main_rules);





/*
 *
 *	MAIN SCRIPT FILE
 *
 */


//	if there is an onload, add it to the load event
		
//
//	initialise wymeditor
//
	
	
//
//	various speficic AJAX response functions
//
	var item_html_saved = function(t) {
		$("#saving").text("Saved");
		
		var fade = function(){$("#saving").fadeOut("slow")}				
		window.setTimeout(fade,500);
	}
		
	var cls = function() {
		window.close();
		window.location.replace("./");
	}
	
	var site_var_fulltext_saved = function(t) {
		item_html_saved(t);
		window.setTimeout(cls,1500);
	}
	
	var item_attachment_added = function(t) {
		cls();
	}
	
	var goto = function(t) {
		window.location.replace(t.returnvar);
	}
	
//
//	set the sort field/order to the default
//
	function sort_default(){
		
		internal_request("class","sort_field","date_written");
		internal_request("class","sort_order","DESC");
	}
	
	function encode(s){ return encodeURIComponent(escape(s)); }





