/*	name   :control.js
		version: 1.0	 
   	use    : using controls
*/
/*  üöä */

// color-dropdown-box events
var g_color_object=null;   
var g_color_object_name="";

function hideColorControl (f_color_object_name)
{
	hideLayer (f_color_object_name+"_square");
}

function showColorControl (f_color_object_name)
{
	showLayer (f_color_object_name+"_square");
}

function setColor(f_color_object_name, f_color,f_x,f_y)
{         
	var f_input_control = "<input type='hidden' name='"+g_color_object_name+"' value='"+f_color+"'>";
	var f_content="<table border=0 cellspacing=0 cellpadding=0 bgcolor='#"+f_color+"'><tr><td>"+f_input_control+"<a href='javascript:colorOpen(&quot;"+f_color_object_name+"&quot;,"+f_x+","+f_y+");'><img border=0 src='images/pixel.gif' width=20 height=20></a></td></tr></table>";				
	
	var f_controlOhneKlammern =  replace (f_color_object_name, "[", "_");
			f_controlOhneKlammern =  replace (f_controlOhneKlammern, "]", "_");
	
	f_color = replace (f_color, "#", "");
	document.getElementById (f_color_object_name).value = f_color;				
	//f_content = replace (f_content, "'", "&quot;");
	replaceLayerContent (f_controlOhneKlammern+"_square", f_content);
	colorClose(f_controlOhneKlammern);
}

function colorOpen(f_color_object_name,f_x, f_y)
{  
	var f_left, f_right, f_top, f_bottom;     
	g_color_object_name=f_color_object_name;
	
	var f_controlOhneKlammern =  replace (g_color_object_name, "[", "_");
			f_controlOhneKlammern =  replace (f_controlOhneKlammern, "]", "_");

	f_color_object=getLayer (f_color_object_name+"_square");  
	
	if (f_color_object != null && g_color_object == f_color_object)
	{
		colorClose(f_controlOhneKlammern);
	}
	else
	{ 
		//getLayerCoords (f_color_object_name, f_left, f_top, f_right, f_bottom);
		/*
		f_x = 250;
		f_y = 170;
		*/
		//alert (f_x);
		g_color_object=f_color_object;
		moveLayer (f_controlOhneKlammern+"_colorGrid",f_x,f_y);
		showLayer (f_controlOhneKlammern+"_colorGrid");
		
	}
} 

function colorClose(f_color_object_name)
{  
	g_color_object=null;
	hideLayer (f_color_object_name+"_colorGrid");
}

// auswahl im listbox setzen
function setSelectedText (p_listbox_obj, p_value)
{
	var i;
	for (i=0; i < p_listbox_obj.length; i++)
	{
		if (p_listbox_obj[i].value == p_value)
		{
			p_listbox_obj.selectedIndex = i;
			break;
		}
	}
}

// auswahl in radiobutton setzten
function setSelectedRadio (p_radio_obj, p_value)
{
	var i;
	for (i=0; i < p_radio_obj.length; i++)
	{
	if (p_radio_obj[i].value == p_value)
		{
			p_radio_obj[i].click();
			break;
		}
	}
}

// auswahl in listbox ermitteln
function getSelectedText(p_listbox_obj)
{
	return p_listbox_obj[p_listbox_obj.selectedIndex].value;
}   

function getSelectedFullText(p_listbox_obj)
{
	return p_listbox_obj.options[p_listbox_obj.selectedIndex].text;
}   


// auswahl in mutlilistbox ermitteln
function getMultiSelectedText(p_listbox_obj)
{                              
	
	var i;  
	var selectedTexts=new Array ();
	var sel=0; 

	for (i=0;i<p_listbox_obj.length;i++)
	{   
		if (p_listbox_obj.options[i].selected==true)
		{  

			selectedTexts[sel]=p_listbox_obj.options[i].value;
			sel++;
		} 

	}
		
	return selectedTexts;
}	  


// auswahl in radiobutton ermitteln
function getSelectedRadio(p_radio_obj)
{
	var i;
	var f_ret="";
	
	for(i=0;i<p_radio_obj.length;i++)
	{
		if (p_radio_obj[i].checked==true)
		{
			f_ret=p_radio_obj[i].value;
			break;
		}
	}
	return f_ret;
}

// global storage object for type-ahead info, including reset() method
var typeAheadInfo = {last:0, 
                     accumString:"", 
                     delay:500,
                     timeout:null, 
                     reset:function() {this.last=0; this.accumString=""}
                    };
// function invoked by select element's onkeydown event handler
function typeAhead() {
   // limit processing to IE event model supporter; don't trap Ctrl+keys
   if (window.event && !window.event.ctrlKey) {
      // timer for current event
      var now = new Date();
      // process for an empty accumString or an event within [delay] ms of last
      if (typeAheadInfo.accumString == "" || now - typeAheadInfo.last < typeAheadInfo.delay) {
         // make shortcut event object reference
         var evt = window.event;
         // get reference to the select element
         var selectElem = evt.srcElement;
         // get typed character ASCII value
         var charCode = evt.keyCode;
         //alert (evt.keyCode);
         // get the actual character, converted to uppercase
         var newChar =  String.fromCharCode(charCode).toUpperCase();
         if (evt.keyCode == 192) newChar = "Ö";
         if (evt.keyCode == 222) newChar = "Ä";
         if (evt.keyCode == 186) newChar = "Ü";
         // append new character to accumString storage
         typeAheadInfo.accumString += newChar;
         // grab all select element option objects as an array
         var selectOptions = selectElem.options;
         // prepare local variables for use inside loop
         var txt, nearest;
         // look through all options for a match starting with accumString
         for (var i = 0; i < selectOptions.length; i++) {
            // convert each item's text to uppercase to facilitate comparison
            // (use value property if you want match to be for hidden option value)
            txt = selectOptions[i].text.toUpperCase();
            
            //txt = replace (txt, "Ã¶", "Ã–");
            // record nearest lowest index, if applicable
            nearest = (typeAheadInfo.accumString > 
                       txt.substr(0, typeAheadInfo.accumString.length)) ? i : nearest;
            // process if accumString is at start of option text
            if (txt.indexOf(typeAheadInfo.accumString) == 0) {
               // stop any previous timeout timer
               clearTimeout(typeAheadInfo.timeout);
               // store current event's time in object 
               typeAheadInfo.last = now;
               // reset typeAhead properties in [delay] ms unless cleared beforehand
               typeAheadInfo.timeout = setTimeout("typeAheadInfo.reset()", typeAheadInfo.delay);
               // visibly select the matching item
               selectElem.selectedIndex = i;
               // prevent default event actions and propagation
               evt.cancelBubble = true;
               evt.returnValue = false;
               // exit function
               return false;   
            }            
         }
         // if a next lowest match exists, select it
         if (nearest != null) {
            selectElem.selectedIndex = nearest;
         }
      } else {
         // not a desired event, so clear timeout
         clearTimeout(typeAheadInfo.timeout);
      }
      // reset global object
      typeAheadInfo.reset();
   }
   return true;
}			
