/*
Special select
April 29, 2005
special_select is a javascript module that displays a textbox together with a selectbox,
the selectbox is associated with a javascript array to display a list of items, this 
list is also used to find a match when typing text, when a match is found typing a * 
will complete the match. Before you type in the * you can preview what match was found
(it is displayed in the background behind the typed text)

usage:

special_select(name,number,default_str,onchange_str,color,shadow_color,border_color,show_pulldown)

The name of the field needs to be listed in a selects_array:
var selects_array= new Array ('mydata1','mydata2');

The data needs to be in an array:
for best results use a blank entry in the array as a first item, otherwise it may
prove difficult to select the first data item.

mydata1_array=new Array("","my","data","array");
mydata2_array=new Array("","my","data","array");

in the calling html file use:
<body onload="highlight_all();">
<table>
<tr>
<script language="javascript">
special_select(mydata1,'1',default_value,);
special_select(mydata2,'1');
</script>
</tr>
<tr>
<script language="javascript">
special_select(mydata1,'2');
special_select(mydata2,'2');
</script>
</tr>
</table>
</body>
Note: start numbering with 1, don't skip numbers, more than about 30 
special selects starts to slow things down.

data is stored and available in mydata1_1, mydata1_2, mydata2_1, mydata2_2 respectively.
 */

var formfields_array = new Array();

function popup_toggle(text_field,number)
{
// determine type of field by using only part until the first _
items=text_field.split("_");
popup_div=items[0];

var select_list=document.getElementById(popup_div+"_"+number+"_popup_select");

if (document.getElementById(popup_div +"_"+number+ "_popup").style.display == 'inline')
	{
	hide_popup_selects();

	document.getElementById(popup_div +"_"+number+ "_popup").style.display='none';

	clear_select(select_list);
// focus does not seem to work
	document.getElementById(popup_div +"_"+number+ "_text").focus();
	}
else
	{
	hide_popup_selects();

	document.getElementById(popup_div +"_"+number+ "_popup").style.display='inline';

	clear_select(select_list);

	selection_array=eval(popup_div+"_array");

	// set selection
	add_item(select_list,'','');
	for (var i=0;i<selection_array.length;i++){add_item(select_list,selection_array[i],selection_array[i]);}

	highlight_match(popup_div,number);

	document.getElementById(popup_div +"_"+number+ "_popup_select").focus();
	}
}

function add_item(select_list,item_text,item_value,default_selected,selected_now)
{
newitem=  new Option(item_text,item_value,default_selected,selected_now);
select_list.options[select_list.length]=newitem;
}

function clear_select(select_list)
{
for (var i=select_list.length;i>=0;i--)
	{
	select_list.options[i]=null;
	}
}

function popup_select(text_field,number)
{
popup_toggle(text_field,number)
}

function hide_popup_selects()
{
for (var i=0;i<selects_array.length;i++)
	{
	for (j=1;j<formfields_array.length;j++)
		{
		if (document.getElementById(selects_array[i]+"_"+j+"_popup"))document.getElementById(selects_array[i]+"_"+j+"_popup").style.display='none';

		window.status=selects_array[i]+"_"+j+"_popup";
		}
	}
}

function highlight_all()
{
for (var i=0;i<selects_array.length;i++)
	{
	for (j=1;j<formfields_array.length;j++)
		{
		highlight_match(selects_array[i],j);
		}
	}
}



function hide_popup_select(select_name,number)
{
popup_name=select_name +"_"+ number + "_popup";

document.getElementById(popup_name).style.display='none';
}

function return_result(value_to_return)
{
hide_popup_selects();
}

function fill_select(name,number,default_str)
{
var select_list=document.getElementById(name+"_"+number+"_popup_select");

clear_select(select_list);

selection_array=eval(name+"_array");

// set selection
add_item(select_list,'','');
for (var i=0;i<selection_array.length;i++){add_item(select_list,selection_array[i],selection_array[i]);}
}

function highlight_match(fieldname,number)
{
fieldname_for_value=fieldname+"_"+number;

var select_list=document.getElementById(fieldname+"_"+number+"_popup_select");
var real_text_field=document.getElementById(fieldname_for_value);
var text_field=document.getElementById(fieldname_for_value+"_text");
var shadow_text_field=document.getElementById(fieldname_for_value+"_shadow");

org_text_value=text_field.value;
// remove * if found (replace '*' with '')
text_value=text_field.value.replace(/\*/g,"");
text_field.value=text_value;
text_length=text_value.length;

// clear selection
for (var i=0;i<select_list.length;i++){select_list[i].selected=false;}

var found_i=0;
selection_array=eval(fieldname+"_array");

// find match in array 
for (var i=0;i<selection_array.length;i++)
	{
	var reg_exp = new RegExp("^"+text_field.value,"i"); 

	if(reg_exp.test(selection_array[i]))
		{
		found_i=i;

		// but make sure we have a unique match, so the next entry may NOT match as well
		if (i+1<selection_array.length && found_i)
			{
			if(reg_exp.test(selection_array[i+1])){found_i='';}
			}

	i=selection_array.length;}
	}

// if last char is a * set the value to the selected item
if (org_text_value != text_value && found_i){text_field.value=selection_array[found_i]}
if (found_i)
	{
// set the value to the matching part
text_field.value=selection_array[found_i].substring(0,text_length);

	if (selection_array[found_i] == text_field.value || text_field.value == '')
		{
		shadow_text_field.value='';
		shadow_text_field.style.backgroundColor='white';
		}
	else
		{
		shadow_text_field.value=selection_array[found_i];
		shadow_text_field.style.backgroundColor='white';
		}
	}
else
	{
	if (text_field.value == '')
		{
		shadow_text_field.value='';
		shadow_text_field.style.backgroundColor='white';
		}
	else
		{
		shadow_text_field.value='';
		shadow_text_field.style.backgroundColor='#FFAAAA';
		}
	}

if (shadow_text_field.value)
	{real_text_field.value=shadow_text_field.value;}
else
	{real_text_field.value=text_field.value;}

// and set selected in list
if (select_list[found_i] && found_i)select_list[found_i+1].selected=true;
}

var last_keycode='';
function usekey(key_event,name,number,context)
{
// enter_to_tab
// not okay for gecko
//if (key_event.keyCode == '13')key_event.keyCode=9;

mykeycode=key_event.keyCode;

        if (mykeycode == '9') // Tab
                {
// should make all popups invisible
hide_popup_selects();
		}
// CANNOT use F1
        if (mykeycode == '113' && context) // F2
                {
		}
// CANNOT use F3 in IE
// CANNOT use F4 in IE
// CANNOT use F5 in IE

        if (mykeycode == '123') // F12
                {
                // update...
                if (mainform1.formtype.value != 'form')
                        {
                        if (mainform1.f12_pressed)
                                {
                                if (mainform1.f12_pressed.value != 'true')update();
                                mainform1.f12_pressed.value='true';
                                }
                        else
                                {
                                update();
                                }
                        }
                }

        last_keycode=mykeycode;

        if (name && number)
                {
formfield_to_move_from=document.getElementById(formfields_array[number][name]+"_text");

//if (!formfield_to_move_from.value || mykeycode == '191') // 191 = shift!
//if (!formfield_to_move_from.value || mykeycode == '16') // 16 = ?
//{
//popup_select(name,number);
//}
//else
{
window.status=name+":"+number+":"+mykeycode;

                if (mykeycode == '38')number--;
                if (mykeycode == '40')number++;
                if (mykeycode == '38' || mykeycode == '40')
                        {
                        prev_number=number;

                        if (number < 1)number=formfields_array.length-1;
                        if (number >= formfields_array.length || number == 1000)number=1;

                        if (formfields_array[number][name])
                                {
                                formfield_to_move_to=document.getElementById(formfields_array[number][name]+"_text");

                                if (formfield_to_move_to)
                                        {
                                        formfield_to_move_to.focus();
                                        }
                                else
                                        {
                                        number=prev_number;
                                        }
                                }
                        else
                                {
                                number=prev_number;
                                }
                        }
                }
}

}



var first_run="1";

function special_select(name,number,default_str,onchange_str,color,shadow_color,border_color,show_pulldown,simple_select)
{
var text_back_up='-1.4em';
var select_back_up='-1.0em';

//color="#000000";
//shadow_color="#666666";
//border_color=color;

if (!color)color="#000000";
if (!shadow_color)shadow_color="#666666";
if (!border_color)border_color=color;

//document.write("<td valign=\"top\">");
document.write("<div style=\"position:relative;left:0px;top:0px;width:100%;");
document.write("border:1px solid "+border_color+";line-height:1em;font-size:1em;\">");

// needed to reserve space for absolute positioned elements within
document.write("<input type=\"text\" style=\"visibility:hidden;width:100%;font-size:0.55em;line-height:0.55em;\">");
document.write("<span ");
document.write("style=\"position:absolute;top:0px;left:0px;margin:0px;width:100%;\" ");
document.write(">");

document.write("<input type=text id='"+name+"_"+number+"_shadow' name='"+name+"_"+number+"_shadow' ");
document.write("style=\"background-color:white;border:0px solid black;color:"+shadow_color+";width:100%;\" ");


document.write(">");
document.write("</span>");

document.write("<input type=\"hidden\"  id='"+name+"_"+number+"' name='"+name+"_"+number+"' ");
document.write(" style=\"visibility:hidden;\" notab tabindex=9999 onfocus=\"blur();\"  ");
document.write(">");

if (show_pulldown == 'true')
  {
  if (!simple_select)
    {
    document.write("<span ");
    document.write("style=\"position:absolute;top:0px;left:100%;");
    document.write("font-family:arial;margin:0px;line-height:1.1em;font-size:1.1em;");
    document.write("z-index:100;border: 0px solid red;\" ");
    document.write("onclick=\"popup_toggle('"+name+"','"+number+"');\""); 
    document.write(">");
    document.write("<span ");
    document.write("style=\"position:absolute;left:-1em;width:1em;background-color:#aaaaff;\" ");
    document.write(">");

    document.write("<center>V</center>");
    //document.write("<img style=\"width:100%;\" src=\"/images/arrow_down_button.gif\" ");
    //document.write(">");
    document.write("</span>");

    document.write("</span>");
    }
  }

document.write("<span ");
document.write("style=\"position:absolute;top:0px;left:0px;margin:0px;width:100%;\" ");
document.write(">");

document.write("<input type=\"text\" id=\""+name+"_"+number+"_text\"  name=\""+name+"_"+number+"_text\"");  
document.write(" notab value=\""+default_str+"\"");  
document.write("style=\"background-color:transparent;border:0px solid black;color:"+color+";width:100%;\" ");


if (simple_select)
  {
  document.write(" onfocus=\"hide_popup_selects();fill_select('"+name+"','"+number+"');popup_toggle('"+name+"','"+number+"');\" "); 
  }
else
  {
  document.write("onkeyup=\"highlight_match('"+name+"','"+number+"');\" ");  
  document.write("onkeydown=\"usekey(event,'"+name+"','"+number+"');\" ");
document.write("onblur=\"document.getElementById('"+name+"_"+number+"_text').value=document.getElementById('"+name+"_"+number+"').value;highlight_match('"+name+"','"+number+"');"+onchange_str+"\"");
  document.write(" onfocus=\"hide_popup_selects();fill_select('"+name+"','"+number+"');highlight_match('"+name+"','"+number+"')\" >");
  }
document.write("</span>");
 

document.write("<span class=\"popup\" id=\""+name+"_"+number+"_popup\" name=\""+name+"_"+number+"_popup\" ");
document.write("style=\"position:absolute;display:none;top:0px;left:0px;width:100%;");
document.write("z-index:1000;margin:0px;\"");

if (simple_select)
  {
  document.write(" onfocus=\"fill_select('"+name+"','"+number+"');\" ");
  }
document.write(">");

document.write("<input type=\"hidden\" id=\""+name+"_"+number+"_popup_text\"");
document.write(" name=\""+name+"_"+number+"_popup_text\" >");
document.write("<select id=\""+name+"_"+number+"_popup_select\" name=\""+name+"_"+number+"_popup_select\" ");
;

document.write("onchange=\"document.getElementById('"+name+"_"+number+"_text').value=this[this.selectedIndex].value;highlight_match('"+name+"','"+number+"');document.getElementById('"+name+"_"+number+"_text').focus();hide_popup_select('"+name+"','"+number+"');"+onchange_str+"\" "); 
document.write(" style=\"width:100%;margin:0px;\" ");
document.write(">");
document.write("</select></span>");

document.write("</div>");
//document.write("</td>");

if (!formfields_array[number])formfields_array[number]=new Array ();
formfields_array[number][name]=name+"_"+number;
//alert("formfields_array[number][name]="+name+"_"+number);


}


