/*

REFERS TO: http://support.microsoft.com/kb/314279/en-us

BUG: The FOR Attribute of the LABEL Object Resets Focus

SYMPTOMS
When you click a label that is associated with a select box, the element that you selected in the select box is reset to the first element on the list.

RESOLUTION
You can only work around this problem in Internet Explorer 6 by using the onfocusin event. You can basically store the selected index into a temporary variable and reset the index to that variable when the select box obtains focus.



*/

//Set a temp expando to store the current selectedIndex
function SelectOnFocusIn()
{
	try
	{
		var eSrc = window.event.srcElement;
		if (eSrc) 
			eSrc.tmpIndex = eSrc.selectedIndex;
	}
	catch (e)
	{
		//HandleError(e, false);
	}
}

//restore the selectedIndex
function SelectOnFocus()
{
	try
	{
		var eSrc = window.event.srcElement;
		if (eSrc) 
			eSrc.selectedIndex = eSrc.tmpIndex;
	}
	catch (e)
	{
		//HandleError(e, false);
	}
}

/*

   Per non perdere la validazione XHTML (onfocusin non esiste) attacchiamo i gestori di eventi con JS stesso.

*/

function addEvent1(el, evt, fn, capture){
	if (el.addEventListener){
		el.addEventListener(evt, fn, capture);
	} else if (el.attachEvent){
		el.attachEvent('on'+evt, fn);
	}
}


function Register() {

	var selectList = document.getElementsByTagName("select");

	for(var k = 0; k < selectList.length; k++) {

		addEvent1(selectList[k], "focusin", SelectOnFocusIn, false);
		addEvent1(selectList[k], "focus",   SelectOnFocus, false);

	}
}

addEvent1(window, "load", Register, false);
