/****
Once DOM is loaded, searches for inputs with the class 'focus', then extends onfocus and onblur as follows:

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, and will restore if focus is lost and value is empty

****/
var inputs = {
	init : function() {
		inputs.attachFocus($$("input.focus"));
	},
	attachFocus : function(sfEls){
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onfocus=function() {inputs.txtInputOnFocus(this,this.defaultValue);}
		}
	},
	txtInputOnFocus : function(txtInput,str){
		if(txtInput.hasFocus){return;}
		txtInput.hasFocus=true;
		if(str=='Password'&&txtInput.value==str){
			txtInput=inputs.swapInputType(txtInput,str);
		}
		if(txtInput.value==str)txtInput.value='';
		txtInput.className+=" hasfocus";
		txtInput.onblur = function(){
			if(txtInput.value==''){
				if(txtInput.type=='password')txtInput=inputs.swapInputType(txtInput,str);
				txtInput.value=str;
			}
			$(txtInput).removeClassName("hasfocus");
			txtInput.hasFocus=false;
		}
	},
	swapInputType : function(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 (){inputs.txtInputOnFocus(newInput,str)};
		newInput.hasFocus=false;
		window.newInput = newInput;
		newInput.onblur = function(){
			if(newInput.value==''){
				if(newInput.type=='password')newInput=inputs.swapInputType(newInput,str);
				newInput.value=str;
			}
			newInput.hasFocus=false;
		}
		setTimeout("newInput.hasFocus=true;newInput.focus();",1);
		return newInput;
	}
}
document.observe("dom:loaded", inputs.init);

function swapRadios(elm,state){
	var frm = $('frm_customiser');
	for(i=0;i<frm.elements.length;i++){
		if(frm.elements[i].name==elm)frm.elements[i].disabled=state;
	}
}
