/* * * * * * * * * * * * * * * * * * * * * * * * *
 *			 Seir Anphin Form Utility            *
 *			  Copyright Darrel Grant             *
 *			  http://www.anphin.com	             *
 *        (Except the 90% by Kevin Roth)         * 
 * * * * * * * * * * * * * * * * * * * * * * * * */

/***
 * Name: formutils
 * Desc: Provides general help functions for more 
 * specific form tasks.
 * Dependencies: sa2.js, dom.js
 */
formutils = {
	
	// Attach an event to "elm" which 
	// popups up a calendar that, when clicked, 
	// puts a date in "target_field"
	popup_calendar : function(elm, target_field, min_date, max_date) {

	},
	
	
	/**
	 * ajax_autocomplete_search
	 * 
	 * -take value of namefield_id,
	 * -make a request to request_page using ajax.makerequest()
	 * -set a callback which puts names under namefield_id and puts an ID 
	 * matching the users partial or complete input in idfield_id
	 */
	ajax_autocomplete_search : function(idfield_id, request_page, skip) {
		var namefield_id = idfield_id + '_search';
		
		if (typeof latest_server_query == 'undefined') {
			latest_server_query = new Array();
			latest_server_query[idfield_id] = '';
		}
		
		var search = $(namefield_id).value;
		var server_query = request_page + '&search=' + search;
		var popup = idfield_id + '_assist';
		
		if (server_query != latest_server_query[idfield_id] && search != '') {
			// Make a request to request_page 
			if (skip != 'TRUE') {
				ajax.makeRequest('GET', server_query, function() { 
					formutils.ajax_autocomplete_callback(idfield_id, popup); 
				});
			}
			latest_server_query[idfield_id] = server_query;
		}
		//var t = setTimeout("formutils.ajax_autocomplete_search('" + idfield_id + "', '" + request_page + "', 'false');", 1000);
	},

	/**
	 * This function needlessly utilizes JSON. I just wanted to play with it...
	 */	
	ajax_autocomplete_callback : function(idfield, targetid) {
		if (ajax.xhr.readyState == 4) {
			if (ajax.xhr.status == 200) {
				var response = ajax.xhr.responseText;
				var target = $(targetid);

				if (response != '0') {
					
					var pairs = eval('(' + response + ')');
					var string = '';

					for (key in pairs.items) {
						string += '<div><a href="#" class="tooltip ' + idfield + '">' + pairs.items[key].name + '</a></div>';
					}
					if (pairs.items[0].id != 0) {
						target.innerHTML = string;
						target.style.visibility = 'visible';
					}

					$(idfield).value = pairs.items[0].id;
				} else {
					// No matching item was found
					target.style.visibility = 'hidden';
					$(idfield).value = '0';
				}
			} else {
				return '';
			}
			return ajax.xhr.status;
		}
		return '';
	}
}

	/**
	 * Singleton utility collection.
	 * 
	 * Dependencies: 
	 * dragdrop.js
	 */

function resizable_textarea(textarea, container) {
	this.textarea = textarea; // The <textarea> form field
	this.container = container; // The containing <div>
}

textarea_resizer = {
	onMove_callback : function(newPos, element, rt) {
		textarea_resizer.updateHeight(newPos.Y, element, rt);
		//textarea_resizer.updateWidth(newPos.X, element, rt);
	},

	/**
	 * I had try/catch blocks here during development. 
	 * FYI: putting alerts() inside functions
	 * called on the mousemove event has undesirable 
	 * consequences. mousemove fires event handlers RAPIDLY.
	 */
	updateHeight: function(y, element, rt) {
		rt.container.style.height = (y + 5) + 'px';
		rt.textarea.style.height = (y - 5) + 'px';
	},

	updateWidth : function(x, element, rt) {
		// This causes the jumping and the acceleration. Why?
		//rt.container.style.width =  (x + 5) + 'px'; // This line causes the acceleration
		//rt.textarea.style.width = (x - 5) + 'px'; // This line causes the jumping
	}
}


