//****************************************************************
//****************************************************************
//	COPYRIGHT 2008, Vertex Software
//****************************************************************
//****************************************************************

//----------------------------------------------------------------
//----------------------------------------------------------------
// For INPUT or TEXTAREA tags that use the js_defaultText class, sets the
// default text to whatever test is defined for the field title
// attribute. When the user focuses on the field, the defaut text is
// removed. If the user does not chaneg the text and leaves the field,
// the default text is added back. Defautl text will NOT be posted back
// with form data.
//----------------------------------------------------------------
//----------------------------------------------------------------



//-----------------------------------------------------------
// defaultText
//-----------------------------------------------------------
function defaultText( element ) {
	var kUnfocusedClass = "unfocused";
	var mElement = element;
	var mASPNET__doPostBack = __doPostBack;
	
	Initialize(element);
	

	//-----------------------------------------------------------
	// Initialize
	//-----------------------------------------------------------
	function Initialize( element ) {
		try {
			if (!element || element.tagName != "INPUT" || !element.title || element.type != "text") return;
			$(element).focus( onfocus );
			$(element).blur( onblur );
			$(element).change(onchange);
			$(element.form).submit( function() { onsubmit(element); } );
			$(element).blur();
			__doPostBack = onsubmit;
			}
		catch (error) {
			alert( "defaultText.Initialize: " + error.description  );
			}
		}


	//-----------------------------------------------------------
	// WasElementChanged
	//-----------------------------------------------------------
	function WasElementChanged( element ) {
		try {
			if (element.value && element.value != element.title) return true; 
			if (!element.form.changed || !element.form.changed[ element.name || element.id ]) return false;
			}
		catch (error) {
			alert( "defaultText:WasElementChanged " + error.description );
			}
		return true;
		}


	//-----------------------------------------------------------
	// onsubmit
	//-----------------------------------------------------------
	function onsubmit(eventTarget, eventArgument) {
		try {
			if (element.value && !WasElementChanged(element)) element.value = "";
			mASPNET__doPostBack(eventTarget, eventArgument || "");
			}
		catch (error) {
			// Throws error in certain cases - ignore
			//alert( "defaultText.onsubmit: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// onchange
	//-----------------------------------------------------------
	function onchange( ) {
		try {
			if (!this.form.changed) this.form.changed = {};
			this.form.changed[ this.name || this.id ] = this;
			}
		catch (error) {
			alert( "defaultText.onchange: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// onfocus
	//-----------------------------------------------------------
	function onfocus( ) {
		try {
			if (WasElementChanged(this)) return; 
			this.value = "";
			this.style.color = this.defaultColor || "";
			$(this).removeClass( kUnfocusedClass );
			}
		catch (error) {
			alert( "defaultText.onfocus: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// onblur
	//-----------------------------------------------------------
	function onblur( ) {
		try {
			if (WasElementChanged(this)) return; 
			this.value = this.title;
			this.defaultColor = this.style.color;
			this.style.color = "gray";
			$(this).addClass( kUnfocusedClass );
			}
		catch (error) {
			alert( "defaultText.onblur: " + error.description );
			}
		}

	}


AttachBehaviors("defaultText");