// change 3986: 2003-06-27 ST created this script

// this script contains functions used by the multiple select widget

// timer identifiers for showing and hiding the fake tooltip
var multiSelShowTTId = 0, multiSelHideTTId = 0;
var type_ahead_dd_open = 0;
var widget_open = 0;
// select element has received focus; show the multiple select
// widget if it is not open already, hide it otherwise (i.e. hide
// it if the user left the multiple select by clicking the corresponding
// single select)
function multiSelFocus(sSelId, mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size)
{
	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var sDiv = document.getElementById(sDivId);

	if (sSel.disabled == false) {
		multi_select_expand(sSel,mSel,WT_Entry_Style,min_size_flag,min_size);

		if (mSel.style.display != "block") { // show multiple select
			if (sSel.offsetHeight) {
				mSel.style.top = sSel.offsetHeight;
			}
			mSel.style.display = "block";
			mSel.focus();

			if( mSel.offsetWidth < sSel.offsetWidth ) {
				mSel.style.width = sSel.offsetWidth;
			}
		}
		else { // hide multiple select
			mSel.focus();
			mSel.style.display = "none";
		}
		sDiv.style.oldZIndex = sDiv.style.zIndex;
		sDiv.style.zIndex = "10";
	}
}
function multitxtFocus(sSelId, mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size)
{

	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var sDiv = document.getElementById(sDivId);
	
	if (sSel.disabled == false) {
		multi_select_expand(sSel,mSel,WT_Entry_Style,min_size_flag,min_size);

		if (mSel.style.display != "block") { // show multiple select
			if (sSel.offsetHeight) {
				mSel.style.top = sSel.offsetHeight;
			}
			mSel.style.display = "block";
			mSel.focus();
		}
		else { // hide multiple select
			sSel.focus();
			mSel.style.display = "none";
		}
		sDiv.style.oldZIndex = sDiv.style.zIndex;
		sDiv.style.zIndex = "10";
		type_ahead_blur(sSelId,mSelId);
		type_ahead_dd_open = 1;
	}
	
}


function multitxtUpdate(sSelId, mSelId, ttId)
{
	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var i, txtStr;
	
	if (mSel.style.display != "none") {
		txtStr = "";
		for (i=0; i<mSel.length; i++) {
			if (mSel.options[i].selected) {
				txtStr = txtStr + "," + mSel.options[i].text;
			}
		}
		if (document.all) { // IE: set the value of the fake tooltip
			document.getElementById(ttId).innerHTML = txtStr.substr(1);
		}
		else { // other: set the value of the real tooltip
			sSel.title = txtStr.substr(1);
		}
	}
}


function multitxtCheckUpdate(sSelId, mSelId, ttId)
{

	var enterKeys = ";13;27;"
	var eveKey = findValidKey();

	if (enterKeys.indexOf(eveKey) > -1) {
		multitxtUpdate(sSelId, mSelId, ttId);
		var mSel = document.getElementById(mSelId);
		mSel.blur();
	}
}



// update the value of the single select widget while the user
// is changing selection in the multiple select widget
function multiSelUpdate(sSelId, mSelId, sDivId, foo, ttId)
{
	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var i, valStr, txtStr;

	var allSelected = false;
	var numSelected = 0;

	if (mSel.style.display != "none") {
		valStr = "";
		txtStr = "";
		for (i=0; i<mSel.length; i++) {
			if (mSel.options[i].selected) {
				valStr = valStr + "," + mSel.options[i].value;
				txtStr = txtStr + "," + mSel.options[i].text;

				if( mSel.options[i].text == 'ALL' ) {
					allSelected = true;
				}
				numSelected++;
			}
		}

		// is "ALL" and something else is selected, remove "ALL"
		if( allSelected && ( numSelected > 1 ) ) {
			for( i = 0; i < mSel.length; i++ ) {
				if( mSel.options[ i ].text == 'ALL' ) {
					mSel.options[ i ].selected = false;
					break;
				}
			}

			var valStrArray = valStr.split( "," );
			var txtStrArray = txtStr.split( "," );

			valStr = "";
			txtStr = "";

			for( i = 1; i < txtStrArray.length; i++ ) {
				if( txtStrArray[ i ] != 'ALL' ) {
					valStr = valStr + ',' + valStrArray[ i ];
					txtStr = txtStr + ',' + txtStrArray[ i ];
				}
			}

			numSelected--;
		}

		sSel.options[0].value = valStr.substr(1);
		sSel.options[0].text = txtStr.substr(1);

		// if nothing is selected, select "ALL"
		if( numSelected == 0 ) {
			sSel.options[0].text = 'ALL';

			for( i = 0; i < mSel.length; i++ ) {
				if( mSel.options[ i ].text == 'ALL' ) {
					mSel.options[ i ].selected = true;
					break;
				}
			}
		}

		if (document.all && ( ttId != null )) { // IE: set the value of the fake tooltip
			document.getElementById(ttId).innerHTML = txtStr.substr(1);
		}
		else { // other: set the value of the real tooltip
			sSel.title = txtStr.substr(1);
		}
	}

	if( window.event != null ) {
		if( ! ( window.event.altKey || window.event.ctrlKey || window.event.shiftKey ) ) {
			//multiSelBlur(sSelId, mSelId, sDivId);
			if( foo != null ) {
				eval( foo );
			}
		}
	}
}

function type_ahead_blur(sSelId,mSelId) {
	if (type_ahead_dd_open == 0) {
		var sSel = document.getElementById(sSelId);
		var mSel = document.getElementById(mSelId);
	
		var temp_array = sSel.value.split(",");
		for (x=0; x<mSel.length; x++) {
			mSel.options[x].selected = false;
		}
		for (i=0;i<temp_array.length;i++) {
			for (x=0; x<mSel.length; x++) {
				if (Trim(mSel.options[x].text.toUpperCase()) == Trim(temp_array[i].toUpperCase())) {
					mSel.options[x].selected = true;
				}
			}
		}
		return true;
	}
	else {
		return false;
	}

}

function multitxtBlur(sSelId,mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size)
{
	type_ahead_dd_open = 0;
	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var sDiv = document.getElementById(sDivId);
	
	multi_select_contract(sSel,mSel,WT_Entry_Style,min_size_flag,min_size);
	
	if (mSel.style.display != "none") {
		// do not close the widget immediatelly, so it is possible to
		// prevent immediate reopening of the multiple select widget
		// if the user clicks the corresponding single select right
		// after making multiple selections
		setTimeout("document.getElementById('" +
			mSelId + "').style.display = 'none'", 50);
	}
	sDiv.style.zIndex = sDiv.style.oldZIndex;
	sSel.focus();
}

function multiSelBlur(sSelId,mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size)
{
	var sSel = document.getElementById(sSelId);
	var mSel = document.getElementById(mSelId);
	var sDiv = document.getElementById(sDivId);
	
	multi_select_contract(sSel,mSel,WT_Entry_Style,min_size_flag,min_size);
	if (mSel.style.display != "none") {
		// do not close the widget immediatelly, so it is possible to
		// prevent immediate reopening of the multiple select widget
		// if the user clicks the corresponding single select right
		// after making multiple selections
		setTimeout("document.getElementById('" +
			mSelId + "').style.display = 'none'", 50);
	}
	sDiv.style.zIndex = sDiv.style.oldZIndex;
}

// show a fake tooltip -- IE does not show the "title" property
// of the select element
function showTooltip(evt, ttId)
{
	if (!document.all) { // if not IE, do not do anything
		return;
	}
	var tt = document.getElementById(ttId);
	var ttTop, ttLeft;
	if (tt.innerHTML != "") {
		evt = (evt) ? evt : ((event) ? event : null);
		ttTop = (evt.offsetY + 10) + "px";
		ttLeft = (evt.offsetX + 0) + "px";
		tt.style.top = ttTop;
		tt.style.left = ttLeft;
		multiSelShowTTId = setTimeout("document.getElementById('" +
			ttId + "','" + "').style.display = \"block\";", 1000);
		multiSelHideTTId = setTimeout("hideTooltip('" + ttId + "');", 10000);
	}
}

// hide fake tooltip
function hideTooltip(ttId)
{
	if (!document.all) { // if not IE, do not do anything
		return;
	}
	var tt = document.getElementById(ttId);
	tt.style.display = "none";
	clearTimeout(multiSelShowTTId);
	clearTimeout(multiSelHideTTId);
}



// expand the multiSelection box 
function multi_select_expand(sSel, mSel,WT_Entry_Style,min_size_flag,min_size) {
	if (WT_Entry_Style == 2 && min_size_flag ==1) {
		if (widget_open == 0) {
			mSel.style.width = 'auto';
			sSel.focus();
			widget_open=1;
		}
	}
}
// contract the multiSelection box
function multi_select_contract(sSel, mSel,WT_Entry_Style,min_size_flag,min_length) {
	if (WT_Entry_Style == 2 && min_size_flag ==1) {
		if (widget_open == 1  ) {
			mSel.style.width = min_length;
			widget_open=0;
		}
	}
}
// expand the singleSelection box 
function single_select_expand(obj,hidObj,WT_Entry_Style,min_size_flag,min_size) {
	if (WT_Entry_Style == 2 && min_size_flag ==1) {
		var hid = document.getElementById(hidObj);
		if (hid.value == 0) {
			obj.style.width = 'auto';
			var resizeWidth = obj.clientWidth;
			if (resizeWidth < min_size)
				obj.style.width = min_size;
			obj.focus();
			hid.value = 1;
		}
	}
}

// contract the singleSelection box
function single_select_contract(obj,hidObj,WT_Entry_Style,min_size_flag,min_size) {
	if (WT_Entry_Style == 2 && min_size_flag ==1) {
		var hid = document.getElementById(hidObj);
		if (hid.value == 1) {
			obj.style.width = min_size;
			hid.value = 0;
		}
	}
}

function Trim(TheString)
{
	var len;

	len = TheString.length;
 	while(TheString.substring(0,1) == " "){ //trim left
 		TheString = TheString.substring(1, len);
  		len = TheString.length;
	}

 	while(TheString.substring(len-1, len) == " "){ //trim right
 		TheString = TheString.substring(0, len-1);
  		len = TheString.length;
 	}
 	return TheString;
}

function findValidKey() {
	return ";"+window.event.keyCode+";";
}


function typeAhead (type,forcematch,sSelId, mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size) {
	var listid = document.getElementById(mSelId);
	var sSel   = document.getElementById(sSelId);
	
	if (listid && listid.length && listid.length > 0) {
		
		var found = false;
		var popupKeys = ";27;40;"

		if (type == 1) {
			var t_sSel = sSel.value.split(",");
			var v_sSel = t_sSel[t_sSel.length - 1].toUpperCase();
			var o_sSel = '';
			for (i=0;i<t_sSel.length-1;i++) {
				o_sSel = o_sSel + t_sSel[i] + ',';
			}
			var forwardKeys = ";38;";
			var backwardKeys = "";
			var blockedKeys = ";37;39;"
		}
		else {
			var v_sSel = sSel.value.toUpperCase();
			var o_sSel = '';
			var forwardKeys = ";39;38;";
			var backwardKeys = ";37;";
			var blockedKeys = ""
		}
		
		var cursorKeys = forwardKeys + backwardKeys + blockedKeys + popupKeys
		var eveKey = findValidKey();

		if (cursorKeys.indexOf(eveKey) > -1) {
			if (forwardKeys.indexOf(eveKey) > -1) {
				for (var i = 0; i < listid.length; i++) {
					if (listid.options[i].text.toUpperCase().indexOf(v_sSel) == 0) {
						found=true; break;
					} 
				}
				var newValue = found ? listid.options[i + 1].text : '';
				sSel.value = o_sSel + newValue;
			}
			else if (backwardKeys.indexOf(eveKey) > -1) {
				for (var i = 0; i < listid.length; i++) {
					if (listid.options[i].text.toUpperCase().indexOf(v_sSel) == 0) {
						found=true; break;
					} 
				}
				if (i >= 1) {
					var newValue = found ? listid.options[i - 1].text : '';
					sSel.value = o_sSel + newValue;
				}
				else {
					sSel.value = o_sSel + '';
				}
			}
			else if (blockedKeys.indexOf(eveKey) > -1) {
				sSel.value = sSel.value;
			}
			else if (popupKeys.indexOf(eveKey) > -1) {
				multitxtFocus(sSelId, mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size);
			}
			else {
				sSel.value = o_sSel + '';
			}
		}
		else{
			for (var i = 0; i < listid.length; i++) {
				if (listid.options[i].text.toUpperCase().indexOf(v_sSel) == 0) {
					found=true; break;
				} 
			}
			if (sSel.createTextRange) {
				if (( forcematch && !found )) {
					sSel.value=sSel.value.substring(0,sSel.value.length-1); 
					typeAhead(type,forcematch,sSelId,mSelId, sDivId,WT_Entry_Style,min_size_flag,min_size);
					return;
				}
				var blockRange = sSel.createTextRange();
				var oldValue = blockRange.text;
				var newValue = found ? listid.options[i].text : oldValue;
				if (newValue.toUpperCase() != v_sSel) {
					sSel.value = o_sSel + newValue;
					var blockRangeNew = sSel.createTextRange();
					blockRangeNew.moveStart('character', oldValue.length) ;
					blockRangeNew.select();
				} 
			}// end createtextrange;
		}// end cursor keys
	}
} // end function 
