/*
	Description:	javascript functions used by framework
	Author:			Charlie Evans (charlie@netconcepts.com)
	Created:		10/10/2005

	- Notes ----------------------------------------------------------------

	------------------------------------------------------------------------

*/
	var dyna_pop_options = new Array();
	var do_hover = true;
						
	/**
	 *	This is a very handy function written by Simon Willison:
	 *	http://simon.incutio.com/archive/2004/05/26/addLoadEvent
	 *
	 *	It allows you to queue up a whole series of events to be triggered when the document loads.
	 *	If you simply use window.onload = func, then you run the risk of overwriting existing functions that are supposed to run when the onload event is triggered.
	 */
	function addLoadEvent(func) {
		var oldonload = window.onload;

		if (typeof window.onload != 'function') window.onload = func;
		else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}


	// copying function for unload
	function addUnloadEvent(func) {
		var oldonunload = window.onunload;

		if (typeof window.onunload != 'function') window.onunload = func;
		else {
			window.onunload = function() {
				oldonunload();
				func();
			}
		}
	}

	/**
	 *	check the status of the associated checkbox and disable/enable date components
	 */
	function date_disable(field_id) {
		
		// get current status
		var disable = document.getElementById(field_id+"_disable").checked;
		
		// attempt to disable/enable each date component
		var components = ['day', 'month', 'year', 'hour', 'minute', 'second', 'time'];
		for (var i in components) {
			
			var el = document.getElementById(field_id + "_" + components[i]);
			if (el) {
				el.selectedIndex = 0;
				el.disabled = disable;
			}
		}		
	}

	/**
	 *	set each of the day/month/year select component from a Calendar object (function used as onUpdate callback from Calendar)
	 */	
	function calendar_set_date_elements(calendar) {

		var prefix = calendar.params.fieldPrefix;

		var fields = {
			day: '%d',
			month: '%m',
			year: '%Y'
		};

		for (field in fields) {
			var element = document.getElementById(prefix + "_" + field);
			var field_val = calendar.date.print(fields[field]);
						
			for (count=0; count<element.length; count++) {
				if (element.options[count].value == parseInt(field_val, 10)) {
					element.selectedIndex = count;
					break;
				}
			}		
		}	
	}

	/**
	 *	when the date select boxes are changed - we need to update the hidden calendar field so the popup will acknowledge the change
	 */		
	function update_calendar_field(field_id) {
		
		var calendar_field_value = '';
		var components = ['year', 'month', 'day'];
		
		for (i in components) {
			var element_value = document.getElementById(field_id + "_" + components[i]).value;
			if (element_value.length < 2) element_value = '0' + element_value;
			calendar_field_value += '/' + element_value;
		}
		
		document.getElementById('calendar_field_' + field_id).value = calendar_field_value.substring(1);
	}

	function confirm_link(question, link) {
		if (confirm(question)) location.href=link;
	}

	function popup(link, width, height) {
		window.open(link, '', "toolbar=no,menubar=no,directories=no,status=no,scrollbars=auto,width=" + width + ",height=" + height);
	}

	function rs_confirm(elementId, confirm_msg, href, row_class, current_class) {
		if (document.getElementById(elementId)) {
			do_hover = false;
			var row_class = (typeof(row_class)=='undefined') ? 'row_delete' : row_class;
			var current_class = (typeof(current_class)=='undefined') ? document.getElementById(elementId).className : current_class;
			document.getElementById(elementId).className = row_class;
			if (confirm(confirm_msg)) location.href = href;
			else {
				document.getElementById(elementId).className = current_class;
				do_hover = true;
			}
		}
	}

	function rs_row_hover(theRow, row_class) {	
		if (do_hover) {
			var row_class = (typeof(row_class)=='undefined') ? 'hover_row_color' : row_class;
			theRow.className = row_class;
		}
	}


	function dyna_pop(selector_id, options_id) {

		// retrieve selected value of selector
		var selector_obj = document.getElementById(selector_id);
		var selector_value = selector_obj.options[selector_obj.selectedIndex].value;

		// populate options array if any options exist for the selected value
		var options_array = dyna_pop_options[options_id][selector_value];
		if (options_array) {						

			// reset options array
			var options_obj = document.getElementById(options_id);
			if (options_obj.options) options_obj.options.length = 0;

			// create new Option object for each option and manually set selected property
			// note - using the 'selected' argument to Option was buggy
			for (count=0; count<options_array.length; count++) {
				options_obj.options[count] = new Option(options_array[count][0], options_array[count][1]);
				if (options_array[count][2]) options_obj.options[count].selected = true;
			}
		}
	}

	function get_selected_option(list_id) {

		// check for and retrieve selected value
		var list_obj = document.getElementById(list_id);
		
		if (list_obj.selectedIndex >= 0) {
			var text = list_obj.options[list_obj.selectedIndex].text;
			var value = list_obj.options[list_obj.selectedIndex].value;
			return new Option(text,value);
		}
		else return false;
	}
	
	function add_option(hidden_id, list_id, option, index) {
					
		// get list and hidden objects
		var list_obj = document.getElementById(list_id);
		var hidden_obj = document.getElementById(hidden_id);
		
		// make copy of option with selected property set to true
		var hidden_option = new Option(option.text, option.value);
		hidden_option.selected = true;
				
		// fix up indexes
		if (index===undefined || index > list_obj.length || list_obj.length==0) index = false;
		else if (index < 0) index = 0;
				
		// check option doesn't already exist
		for (count=0; count<list_obj.length; count++) {
			if (list_obj.options[count].value == option.value) {
				return -1;
			}
		}
				
		// simply add to end of list if no index defined
		if (index===false) {
			hidden_obj.options[list_obj.length] = hidden_option;
			list_obj.options[list_obj.length] = option;
											
			return list_obj.length-1;
		}
		else {

			// get sub_array of options at index point onwards
			var tail = new Array();
			for (count=index; count<list_obj.length; count++) {
				tail.push(list_obj.options[count]);
			}

			// insert option at index point and increment index for adding tail
			hidden_obj.options[index] = hidden_option;
			list_obj.options[index] = option;
			index++;

			// add tail to end of array
			for (count=0; count<tail.length; count++) {

				// make copy of option with selected property set to true
				var hidden_option = new Option(tail[count].text, tail[count].value);
				hidden_option.selected = true;

				hidden_obj.options[index+count] = hidden_option;
				list_obj.options[index+count] = tail[count];
			}

			return index-1;
		}			
	}
	
	
	function list_action(hidden_id, list_id, action, destination) {
	
		// get selected value
		var selected_option = get_selected_option(list_id);
		if (selected_option === false) return;
		
		var list_obj = document.getElementById(list_id);
		var hidden_obj = document.getElementById(hidden_id);
				
		if (action == 'add') {
			add_option(hidden_id, destination, selected_option);
			list_obj.selectedIndex = -1;
		}
		else if (action == 'remove') {
			hidden_obj.options[list_obj.selectedIndex] = null;
			list_obj.options[list_obj.selectedIndex] = null;
			list_obj.selectedIndex = -1;
		}
		else {
			if (action == 'top') index = 0;
			if (action == 'up') index = list_obj.selectedIndex-1;
			if (action == 'down') index = list_obj.selectedIndex+1;
			if (action == 'bottom') index = false;
						
			hidden_obj.options[list_obj.selectedIndex] = null;
			list_obj.options[list_obj.selectedIndex] = null;
			list_obj.selectedIndex = add_option(hidden_id, list_id, selected_option, index);
		}		
	}

	function obfuscate(user, domain, mailto_link, link_text) {
		if (mailto_link) {
			document.write('<a href=\"mailto:' + user + '@' + domain + '\">');
			if (link_text) document.write(link_text + '</a>');
			else document.write(user + '@' + domain + '</a>');
		}
		else document.write(user + '@' + domain);
	}

	function check_select_other(selector, span_id, other_id) {
		var val = selector.options[selector.selectedIndex].value;
		var display = (val == other_id) ? 'inline' : 'none';
		document.getElementById(span_id).style.display = display;
	}
	
