/** @file
* Drop Down Handler
*
*/
// Free piece of software, without warranty, without responsability... etc.
// use on your own risk
// free usage, free deployment, free whatever ... keep the header intact 
// and keep us posted (info@tensid.ch) if you do some bug fixing :)
//
//
// Created by Tensid AG 2003-2005, Konrad Baechler, http://www.tensid.ch
// wraps the t_dropdown class
// usage: set the parameters (settings below)
// call it like this, within your code
//  t_dropdown("lola", [90,80,20,100], ["Hello", "world", "one two three", "...and the last one"], 2);
//             ^name   ^values         ^texts                                                ^selected
// 
// 

// GLOBAL vars - DO NOT CHANGE THOSE - except for bug fixing :)
var t_idgen = 0;
var t_idprefix = "ts_dd_";
var t_obj_array = new Object();
var t_obj_value = new Object();

// *****************************
// Settings - customize it here
// *****************************
var textClass = "ddedit";
var imgTag = "<button type=\"button\" class=\"ddbutton\" name=\"btn_dropdown\" ";
var buttonClass = "";

var textDepending = 'Depending Drop Down Boxes are going to be reset. OK?';

var selectAction = false;

var tmp_curCaption;

function t_dropdown(v_name, arrValue, arrCaption, v_selected, tc, oce, span_id, new_menu_class, mandatoryFldDef) {
	// try to figure out whether there is somewhere a GUI text available for the message box
	if (document.getElementById('CONFIRM_DEPENDING_DD')) {
		textDepending = document.getElementById('CONFIRM_DEPENDING_DD').value;
	} else {
		try {
			if (top.window.document.getElementById('CONFIRM_DEPENDING_DD')) {
				textDepending = top.window.document.getElementById('CONFIRM_DEPENDING_DD').value;
			} // if
		} catch(e) { } // try ... catch
	} // if .. else
  
	// init
	var menuClass = "ddlayer";
	tmp_curCaption = "";
  
	if (new_menu_class) { menuClass = new_menu_class; }
	if (tc) { textClass = tc; }
	text_name = t_idprefix + "teninputtext" + t_idgen;
	button_name = t_idprefix + "tenbutton" + t_idgen;
	menu_name = t_idprefix + "tenmen" + t_idgen;
	obj_name = t_idprefix + "tenobj" + t_idgen;

	// store the text_name for usage outside
	if (span_id) {
		if ( span_id != "dd_span_" ) {
			storeobj = document.getElementById(span_id);
			storeobj.setAttribute("tensid_id", text_name);
			storeobj.setAttribute("tensid_v_id", v_name);
		}
	}
 
	//wrapper
	document.write('<div class="ddwrapper">');
	document.write('<div class="ddfieldwrapper">');
  
  	// text
	if (textClass=="") { 
		document.writeln('<input name=\"' + text_name + '\" id=\"' + text_name + '\" type=\"text\" style=\"cursor:default;\" '); 
	} else { 
		document.writeln('<input name=\"' + text_name + '\" id=\"' + text_name + '\" type=\"text\" class=\"' + textClass + '\" style=\"cursor:default;\" '); 
	} // if ... else
	document.write('onFocus=\"javascript: this.select();\" ');
	document.write('onKeyUp=\"javascript: var a = Array(');
	for ( t = 0; t < arrCaption.length - 1; t++ ) {
		document.write('\'' + arrCaption[t] + '\',');
	}
	document.write('\'' + arrCaption[arrCaption.length-1] + '\'); ');
	document.write('t_obj_array[\'' + obj_name + '\'].o_m_autocomplete((window.event ? window.event : event), this, a); \" ');
	document.write('onClick=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_toggle();\" ');
	document.write('onBlur=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_hide();\" ');
	if (v_selected >= 0 ) { 
		document.write('value=\"' + arrCaption[v_selected] + '\">'); 
	} else { 
		document.write('value=\"\">'); 
	} // if ... else
  
	document.writeln('<br>');

	// div
	if (menuClass=="") { 
		document.writeln('<div id=\"' + menu_name + '\" ');
		document.write('style=\"position:absolute; width:175px; z-index:800; height: auto; overflow: auto; border: 1px solid #FFFFFF; background-color: #FFFFFF; visibility: hidden\" ');
	} else { 
		document.writeln('<div id=\"' + menu_name + '\" class=\"' + menuClass + '\" style=\"height: auto; overflow: auto; visibility: hidden\" '); 
	}
	document.writeln('onMouseOver=\"javascript: selectAction = true;\" onMouseOut=\"javascript: selectAction = false;\">');

	for ( var i in arrCaption) {
		t_obj_value[t_idprefix + "_item_" + t_idgen + "_" + i] = arrValue[i];
		document.writeln('<div class=\"ddentry\" id=\"' + t_idprefix + "_item_" + t_idgen + "_" + i +'\" ');
		document.write('onClick=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_select(this.id);\" ');
		document.write('onMouseOver=\"javascript: this.className = \'ddentry_highlight\';\" ');
		document.write('onMouseOut=\"javascript: this.className = \'ddentry\';\" ');
		document.write('>');
		if (v_selected == i ) {
			document.write('<div class=\"ddgsausz\" id=\"' + t_idprefix + "_item_" + t_idgen + "_" + i +'_caption\">' + arrCaption[i] + '&nbsp;</div>');
			tmp_curCaption = arrCaption[i];
		} else {
			document.write('<div class=\"gs\" id=\"' + t_idprefix + "_item_" + t_idgen + "_" + i +'_caption\">' + arrCaption[i] + '&nbsp;</div>');
		} 
		document.write('</div>');
	} // for
	document.write('</div>'); // end div

	// check the height of the div and crop it if necessary
	if (i > 7) { 
		document.getElementById(menu_name).style.height = 100 + 'px'; 
	}
  
	document.writeln('</div>'); // closing the ddfield
  
	//	button, takes an Image Tag if there is one
	document.writeln('<div class="ddbuttonwrapper">');
	if (imgTag == "") {
		if (buttonClass == "") {
			document.writeln('<input name=\"' + button_name + '\" id=\"' + button_name + '\" type=\"button\" value=\"...\" '); 
		}	else { 
			document.writeln('<input name=\"' + button_name + '\" id=\"' + button_name + '\" type=\"button\" value=\"...\" class=\"' + buttonClass + '\" '); 
		} // if ... else
		document.write('onClick=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_toggle();\" ');
    	document.write('onBlur=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_hide();\" ');
		document.write('/>'); 
	} else { 
	    document.writeln(imgTag); 
    	document.write('onClick=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_toggle();\" ');
    	document.write('onBlur=\"javascript: t_obj_array[\'' + obj_name + '\'].o_m_hide();\" ');
	    document.write('>...'); 
    	document.write('</button>'); 
	} // if ... else

	// value 
	if ( !document.getElementById(v_name) ) {
		if (v_selected >= 0 ) { 
			document.writeln('<input type=\"hidden\" name=\"' + v_name + '\" id=\"' + v_name + '\" value=\"' + arrValue[v_selected] + '\" ' + mandatoryFldDef + '>');
			document.writeln('<input type=\"hidden\" name=\"' + v_name + '_old\" id=\"' + v_name + '_old\" value=\"' + arrValue[v_selected] + '\">'); 
		} else { 
			document.writeln('<input type=\"hidden\" name=\"' + v_name + '\" id=\"' + v_name + '\" value=\"\" ' + mandatoryFldDef + '>');
			document.writeln('<input type=\"hidden\" name=\"' + v_name + '_old\" id=\"' + v_name + '_old\" value=\"\">'); 
		}
	} else if (v_selected >= 0 ) {
		document.getElementById(v_name).value = arrValue[v_selected];
		document.getElementById(v_name + '_old').value = arrValue[v_selected];
	}

	t_obj_array[obj_name] = new create_dropdown(obj_name, text_name, menu_name, v_name, oce); 
	t_idgen = t_idgen + 1; 
  
	// end ddbutton and ddwrapper
	document.writeln('</div></div>');
}

// define the "class" DROP_DOWN

//constructor
function create_dropdown(o_name, target_obj, menu_obj, value_obj, oce) {
	if (oce) { this.on_change_event = oce; }
	this.name = o_name;
	this.o_height = 0;
	this.o_m_target = document.getElementById(target_obj);
	this.o_m_value = document.getElementById(value_obj);
	this.o_m_value_old = document.getElementById(value_obj + '_old');
	this.o_m_object = document.getElementById(menu_obj);
	this.o_m_stay = 0;
	this.curCaption = tmp_curCaption;

	//methods
	this.o_m_select = o_m_select;
	this.o_m_show = o_m_show; 
	this.o_m_hide = o_m_hide;
	this.o_m_toggle = o_m_toggle; 
	this.o_m_autocomplete = o_m_autocomplete;
}

function o_m_toggle() {
	if (this.o_m_object.style.visibility=="hidden") { 
		// fix a mozilla display bug
		if ( navigator.appName.search(/microsoft/i) == -1 ) {
			var curnode = this.o_m_object.parentNode;
			while (curnode && (curnode.className != 'ctTreeRow') ) {
				curnode = curnode.parentNode ? curnode.parentNode : null;
			} // while
			if (curnode) {
				innernode = curnode;
					if ( curnode.className == 'ctTreeRow' ) {
						while (curnode && (curnode.className != 'ctTreeBox') ) {
							curnode = curnode.parentNode ? curnode.parentNode : null;
						} // while
						if (curnode) {
							curnode.style.display = 'none';
							curnode.style.display = 'block';
						} // if
					} // if
				innernode.style.display = 'none';
				innernode.style.display = 'block';
			} // if
		} // if
		this.o_m_show(); 
	} else {
		this.o_m_hide(); 
	} // if ... else
} // o_m_toggle
  
function o_m_show() {
  //effect handling
  this.o_height = this.o_m_object.offsetHeight;
  this.o_width = this.o_m_object.offsetWidth;
  this.o_m_object.style.visibility = "visible";
} // o_m_show

function o_m_hide() {
	if ( selectAction ) { return true; }
	var obj = this;
	if ( !obj.o_m_object ) { return true; }
	//effect handling
	obj.o_m_object.style.visibility = "hidden";
} // o_m_hide
  
function o_m_select(sel_obj) {
	selectAction = false;
	// no change ?
	if (t_obj_value[sel_obj] == this.o_m_value.value) {
		this.o_m_hide();
		return true;
	}// if change
	
	this.o_m_target.value = document.getElementById(sel_obj+'_caption').firstChild.data;
	this.curCaption = this.o_m_target.value;
	this.o_m_value_old.value = this.o_m_value.value;
	this.o_m_value.value = t_obj_value[sel_obj];
	this.o_m_hide();
	
	re = /([A-Za-z_]+_\d)_/;
	re.exec(sel_obj);
	so = RegExp.$1;
	for ( var i in t_obj_value ) {
		re.exec(i);
		to = RegExp.$1;
		if (so == to) {
		  	if (document.getElementById(i + '_caption').className == 'ddgsausz') {
 		    	document.getElementById(i + '_caption').className = 'gs';
				break;
			} // if
		} // if
	} // for
  
	document.getElementById(sel_obj + '_caption').className = 'ddgsausz';
	
	if (this.on_change_event) { eval(this.on_change_event); }
} // o_m_select

function dropdowneffect(obj, n, ty) {
  if (t_obj_array[obj].o_m_stay == 0) {
		t_obj_array[obj].o_m_object.style.clip = "rect(0px " + t_obj_array[obj].o_width + "px " + n +"px 0px)";
		n = n + (ty * 8);
		if ( ((n <= t_obj_array[obj].o_height) && (ty == 1)) || ((n > 0) && (ty == -1)) ) {
			window.setTimeout("dropdowneffect(\"" + t_obj_array[obj].name + "\", " + n + ", " + ty + ")", 5);
		}	else {
			if (ty == -1) {
				t_obj_array[obj].o_m_object.style.visibility="hidden";
			} else { 
				t_obj_array[obj].o_m_object.style.clip = "rect(0px " + t_obj_array[obj].o_width + "px " + t_obj_array[obj].o_height +"px 0px)"; 
			} // if... else
		} // if ... else
	} // if
} // dropdowneffect

/** Handling Autocomplete functions on drop downs
* 
* Autocomplete functionality is hooked to an onKeyUp Event of an object.
*
* @param object e event 
* @param object n is the element (like an input box)
* @param array ac_array contains possible values
* @return boolean
*/
function o_m_autocomplete(e, n, ac_array) {
	if (e && e.keyCode < 32) {
		if (e.keyCode == 8) {
			n.value = this.curCaption;
			n.select();
		} // if
		return false;
	}
	var tmp = n.value.toLowerCase();
	x = -1;
	for ( t = 0; t < ac_array.length; t++) {
		tmp2 = ac_array[t].toLowerCase();
		tmp2 = tmp2.substr(0, tmp.length);
		if ( tmp == tmp2 ) {
			x = t;
			break;
		} // if
	} // for t
	
	if ( x > -1 ) {
		if ( tmp.length < ac_array[x].length ) {
			n.value = ac_array[x];
			var p = n.parentNode.getElementsByTagName("div");
			var nodecount = p.length;
			for ( t = 0; t < nodecount; t++ ) {
				if ( p[t].id && (p[t].id.search(/_item_/i) > -1) ) {
					eval("r = (p[t].innerHTML.search(/\>[\s]*" + n.value + "/i) > -1);");
					if ( r && (typeof p[t].onclick == "function") ) {
						p[t].onclick();
						break;
					} // if
				} // if
			} // for
			var sub = ac_array[x].substr(tmp.length, ac_array[x].length);
			if ( document.all ) {
				var ran = n.createTextRange();
				ran.findText(sub);
				ran.select();
			} // if
		} // if
	} else {
		n.value = this.curCaption;
		n.select();
	} // if .. else
} // o_m_autocomplete

// end drop down class

