


/****
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)"));
}

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 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;
	}
	setTimeout("newInput.hasFocus=true;newInput.focus();",1);
	return newInput;
}

document.observe("dom:loaded",sfFocus);