//****************************************************************
//****************************************************************
//	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;

	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();
			}
		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( element ) {
		try {
			if (element.value && !WasElementChanged(element)) element.value = "";
			}
		catch (error) {
			alert( "defaultText.onsubmit: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// onchange
	//-----------------------------------------------------------
	function onchange( ) {
		try {
			if (!this.form.changed) this.form.changed = {};
			this.form.changed[ this.name || this.id ] = this;
			// 26AUG2009 RFM - DEFECT #2470 Added to that progromaric changes which wont' call focus remove styles
			if (this.value != this.title) RemoveDefaultStyles( this );
			}
		catch (error) {
			alert( "defaultText.onchange: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// onfocus
	//-----------------------------------------------------------
	function onfocus( ) {
		try {
			if (WasElementChanged(this)) return; 
			this.value = "";
			// 26AUG2009 RFM - DEFECT #2470 Factored to RemoveDefaultStyles
			//this.style.color = this.defaultColor || "";
			//$(this).removeClass( kUnfocusedClass );
			RemoveDefaultStyles( this );
			}
		catch (error) {
			alert( "defaultText.onfocus: " + error.description );
			}
		}



	//-----------------------------------------------------------
	// RemoveDefaultStyles
	//-----------------------------------------------------------
	function RemoveDefaultStyles( element ) {
		try {
			element.style.color = element.defaultColor || "";
			$(element).removeClass( kUnfocusedClass );
			}
		catch (error) {
			alert( "RemoveDefaultStyles: " + 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");