/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */

addEvent(window, 'load', init_cleardefaultpage, false);

function init_cleardefaultpage() {
	var deb = "";
    var formInputs = document.getElementsByTagName('input');
    
    $(document).find(".cleardefault").each(function(){
    
    //for (var i = 0; i < formInputs.length; i++) {
        //var theInput = formInputs[i];
        var theInput = this;
        deb += theInput.name + "\n"
        //deb += i + ".." 
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
			

            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    //}
    
    });
    
    //alert(deb);
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == target.defaultText) {
		if(target.className.match(/\bfieldpassword\b/)){
			trasformInputPassword(target)
		}
        target.value = '';
    }
}

function replaceDefaultText(e) {
	var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == '' && target.defaultText) {
		if(target.className.match(/\bfieldpassword\b/)){
			trasformInputPassword(target)
		}
        target.value = target.defaultText;
    }
}

function trasformInputPassword(obj){
	defaulttxt = $(obj).attr("defaultText");
	//alert($(obj).val() + " - " + $(obj).attr("type"));
	if($(obj).attr("type")!="password"){
		padre = $(obj).parent();
		var inputnew = document.createElement("input"); 
		inputnew.type="password";
		inputnew.name=obj.name;
		inputnew.id=obj.id
		inputnew.defaultText=defaulttxt;
		inputnew.value="";
		$(inputnew).addClass("fieldpassword");
		
		$(inputnew).blur(function() {
			trasformInputPassword(inputnew);
		});	/*
		$(inputnew).focus(function() {
			trasformInputPassword(inputnew);
		});*/
		
		$(padre).find("input").each(function(){
			$(this).remove();
		});
		$(inputnew).focus();
        $(padre).append(inputnew);
        
        inputnew.focus();
        inputnew.focus()
        //inputnew.onblur = trasformInputPassword(inputnew);
        //addEvent(inputnew, 'focus', clearDefaultText, false);
        //addEvent(inputnew, 'blur', trasformInputPassword, false);
		
	}else if($(obj).val()==""){
		padre = $(obj).parent();
		var inputnew = document.createElement("input"); 
		inputnew.type="text";
		inputnew.name=obj.name;
		inputnew.defaultText=defaulttxt;
		inputnew.value=defaulttxt;
		$(inputnew).addClass("fieldpassword");
		
		$(inputnew).blur(function() {
			trasformInputPassword(inputnew);
		});
		$(inputnew).focus(function() {
			trasformInputPassword(inputnew);
		});
		/*
		$(inputnew).focus(function() {
			//alert("vai?");
			clearDefaultText(event);
		});*/
		
		/*addEvent(inputnew, 'focus', clearDefaultText, false);
        addEvent(inputnew, 'blur', replaceDefaultText, false);*/

		$(padre).find("input").remove();
        $(padre).append(inputnew);
		 
		//inputnew.onblur = trasformInputPassword(inputnew);
		
		

	}
}
