// Set focus to login field.
function setFocus(e) {
	// Set focus to input field
	e.focus();
	// Activate field if a key is pressed...
	if (is_opera && is_mac) { // Special handling for opera and mac.
		document.onkeypress = autoFocus;
	} else if (is_major>5) {
		document.onkeydown = autoFocus;
	}
}

// Set focus on keypress to search field automatically, if it is not set to another field.
function autoFocus(e) {
	try {
		e = (e)?e:((event)?event:null);
		if (e.target) {
			var target = e.target;
		}
		else {
			var target = e.srcElement;
		}

		if (!e.metaKey && !e.altKey && !e.ctrlKey && isChar(e.keyCode) && (target.nodeName != "INPUT" || (target.type.toLowerCase() != "text" && target.type.toLowerCase() != "password")) && target.nodeName != "TEXTAREA") {
			setFocusPos(document.qsearch.query, document.qsearch.query.value.length);
			document.qsearch.query.value = "";
			document.qsearch.query.focus();
		}
	}
	catch(e) {
		alert(e);
	}
}

// Set Focus to field and select range
function setFocusPos(field, pos)
{
	if (document.selection) {
		field.focus ();			// Set focus on the element  (IE)

		// Create empty selection range
		var selection = document.selection.createRange ();

		// Move selection start and end to 0 position
		selection.moveStart ('character', -field.value.length);

		// Move selection start and end to desired position
		selection.moveStart ('character', pos);
		selection.moveEnd ('character', 0);
// 		selection.select ();
	}
	// Firefox support
	else if (field.selectionStart || field.selectionStart == '0') {
		field.selectionStart = pos;
		field.selectionEnd = pos;
		field.focus ();
	}
}

// Check if key is a letter...
function isChar(k) {
	if (k >= 65 && k <= 90) // A-Z
		return true;
	else if (is_opera && is_mac && k >= 97 && k < 123)
		return true;
	else if (k >= 192)
		return true;
	return k == 0;
}

