/****
If a textfield or textarea has a default value, this function will clear the input on focus,
and restore the value if focus is lost and the input has an empty value.

If the default value is 'Password' it will destroy the text input and replace it with a 
Password input with the same id, classes and parent node 

****/

/* The funtion is added when the dom is loaded, to any page with this script attached.
	To prevent it swapping of default values, add a class 'nofocus' to inputs */

sfFocus = function() {
	attachFocus($$("input.focus"));
	attachFocus($$("textarea.focus"));
}

function attachFocus(sfEls){
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {txtInputOnFocus(this,this.defaultValue);}
	}
}

function txtInputOnFocus(txtInput,str){
	if(txtInput.hasFocus){return;}
	txtInput.hasFocus=true;
	if(str=='Password'&&txtInput.value==str){
		txtInput=swapInputType(txtInput,str);
	}
	if(txtInput.value==str)txtInput.value='';
	txtInput.className+=" sffocus";
	txtInput.onblur = function(){
                
		if(txtInput.value==''){
			if(txtInput.type=='password'){
                           txtInput=swapInputType(txtInput,str);
                           
                        }
			txtInput.value=str;
		}
		$(txtInput).removeClassName("sffocus");
		txtInput.hasFocus=false;
	}
}

function swapInputType(txtInput,str){
        var originaltype     = txtInput.type;
	var newInput         = document.createElement('input');
	if(txtInput.type=='password')newInput.type='text';
	else newInput.type='password';
	newInput.name = txtInput.name;
	newInput.value = txtInput.value;
	newInput.id = txtInput.id;
	newInput.className = txtInput.className;
	objParent=txtInput.parentNode;
	objParent.replaceChild(newInput,txtInput);
	newInput.onfocus = function (){txtInputOnFocus(newInput,str)};
	newInput.hasFocus=false;
	window.newInput = newInput;
	newInput.onblur = function(){
		if(newInput.value==''){
			if(newInput.type=='password')newInput=swapInputType(newInput,str);
			newInput.value=str;
		}
		newInput.hasFocus=false;
	}
        if (originaltype != 'password'){
	    setTimeout("newInput.hasFocus=true;newInput.focus();",1);
        }
	return newInput;
}

document.observe("dom:loaded",sfFocus);

sfHover = function() {
	var sfEls =
document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);