
/**
* array to keep track which formfields have helptext enabled
*/
var FieldsWithHelpText = new Array();


/**
* Get all input-fields that have a title/alternative text as helptext in a formfield
* and enable the helptext for these fields
*/
function InitHelpText(){
	var inputFields = document.getElementsByTagName('input');

	if(0 == inputFields.length) return;


	for(i = 0; i < inputFields.length; i++){
		// skip buttons and search-field
		if('search' == inputFields[i].getAttribute('name')) continue;
		if('text' != inputFields[i].getAttribute('type')) continue;

		// automatically select field content when clicking inside the field
//		inputFields[i].onclick = function(){ this.select();};

		// 'hasAttribute' doesnt work on IE!
		//if(!inputFields[i].hasAttribute('title')) continue;
		//if(!inputFields[i].hasAttribute('id')) continue;
		if(!inputFields[i].getAttribute('title')) continue;
		if(!inputFields[i].getAttribute('id')) continue;

		// field is of type 'text', has a 'title' and 'id'
		EnableHelpText(inputFields[i].getAttribute('id'));
	}
}

/**
* show title/alternative text as helptext in a formfield
*/
function EnableHelpText(elementID){
	if(FormElement = document.getElementById(elementID)){
		if(!in_array(elementID, FieldsWithHelpText)){
			//element is not yet in the array
			FieldsWithHelpText.push(elementID);

			if (document.body.addEventListener) {
				FormElement.addEventListener('focus', ShowHideHelpText, false);
				FormElement.addEventListener('blur', ShowHideHelpText, false);
			}else if (FormElement.attachEvent){
				FormElement.attachEvent('onfocus', ShowHideHelpText);
				FormElement.attachEvent('onblur', ShowHideHelpText);
			}
		}
		//show helptext if field is empty, OR if field contains helptext (because of Firefox form-autofill!)
		if('' == FormElement.value || 0 == FormElement.getAttribute('title').indexOf(FormElement.value)){
			FormElement.style.color 		= '#666666';
			//FormElement.style.fontStyle	= 'italic';
			FormElement.value = FormElement.getAttribute('title');
		}
	}
}

/**
* hide helptext if a field gets focus.
* show helptext again if field has not been modified
*/
function ShowHideHelpText(evt){
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if(evt){
		FormElement = (evt.target)? evt.target : (evt.srcElement ? evt.srcElement : evt);

		if('blur' == evt.type){
			if('' == FormElement.value || null == FormElement.value){
				FormElement.style.color 		= '#666666';
				//FormElement.style.fontStyle	= 'italic';
				FormElement.value = FormElement.getAttribute('title');
			}
		//temp solution - if helptext is longer than fieldsize, use start of helptext to compare
		}else if(0 == FormElement.getAttribute('title').indexOf(FormElement.value)){
				FormElement.value = '';
				FormElement.style.color 		= '#000000';
				FormElement.style.fontStyle	= 'normal';
		}
	}		
}

/**
* clear helptext in formfields before sending the form,
* otherwise form-validation will not work
*/
function ClearHelpText(elementID){
	if(FormElement = document.getElementById(elementID)){
		if(0 == FormElement.getAttribute('title').indexOf(FormElement.value)){
			FormElement.value 		= '';
			FormElement.style.color = '#000000';
		}
	}
}

/**
* show helptext in a formfield
*/
function ShowHelpText(elementID){
	if(FormElement = document.getElementById(elementID)){
		if('' == FormElement.value || null == FormElement.value){
			FormElement.style.color 		= '#666666';
			//FormElement.style.fontStyle	= 'italic';
			FormElement.value = FormElement.getAttribute('title');
		}
	}
}


/**
* clear all helptext in formfields before sending the form,
* otherwise form-validation will not work
*/
function ClearAll(){
	if(FieldsWithHelpText.forEach){
		FieldsWithHelpText.forEach(ClearHelpText);
	}else{
		//Internet Explorer has no 'forEach' method for arrays
		for (var n = 0; n < FieldsWithHelpText.length; n++){
			ClearHelpText(FieldsWithHelpText[n]);
		}
	}
}

/**
* show all helptext in formfields after they've been cleared,
* useful in situations where client-side validation did not validate the form
*/
function ShowAll(){
	if(FieldsWithHelpText.forEach){
		FieldsWithHelpText.forEach(EnableHelpText);
	}else{
		//Internet Explorer has no 'forEach' method for arrays?
		for (var n = 0; n < FieldsWithHelpText.length; n++){
			EnableHelpText(FieldsWithHelpText[n]);
		}		
	}
}