// modified from: www.themaninblue.com/experiment/formFieldFocus/
function focusField(target){
	var formElements=["input.text","input.checkbox","input.radio","select","textarea"]; //elements that will be tested (a dot indicates the "type" attribute of the element)
	var selectedNode=null;
	while(1){
		if((typeof(document.selection)!="undefined")
			&&(document.selection!= null)
				&&(typeof(window.opera)=="undefined")){ // Internet Explorer
			selectedNode=document.selection.createRange().parentElement();
			break;
		}
// W3 selection method (currently only Mozilla and Safari support
// this; however, neither supports ranges inside form objects, so
// this part is redundant, but is included in case they decide to
// include support for this in the future
		if(typeof(window.getSelection)!="undefined"){
			var theSelection=window.getSelection();
			if(typeof(theSelection.baseNode)!="undefined"){ // Safari way to get node a selection starts in
				selectedNode=theSelection.baseNode;
				break;
			}
			if((typeof(theSelection.getRangeAt)!= "undefined")&&(theSelection.rangeCount>0)){ // Mozilla way to get node a selection starts in
				selectedNode=theSelection.getRangeAt(0).startContainer;
				break;
			}
			break;
		}
		break;
	}
	// if selected node was found, check if selection is inside one of the specified form element types
	if(selectedNode!=null){
		for(var i=0;i<formElements.length;++i)
			{if(selectedNode.nodeName.toLowerCase()==formElements[i].replace(/([^.]*)\..*/,"$1")){return false;}}
	}
	var forms=document.forms;
	for(var i=0;i<forms.length;++i){ // check each form element; if one has a value, do not set focus on it
		var formElements=forms[i];
		for(var j=0;j<formElements.length;++j){
			if((formElements[j].getAttribute("type")=="checkbox")||(formElements[j].getAttribute("type") == "radio"))
				{if(formElements[j].checked!=formElements[j].defaultChecked){return false;}}
			else
				{if((typeof(formElements[j].defaultValue)!="undefined")&&(formElements[j].value!=formElements[j].defaultValue)){return false;}}
		}
	}
	target.focus(); // if no form elements found to be focused (or with values), focus on it
	return false;
}
