magicInputs('#464646', '#A2A2A2', '#841c00');

function magicInputs(focusColor, blurColor, requiredColor) {
	// parameters
	focusColor = focusColor == null? "#000000": focusColor;
	blurColor = blurColor == null? "#cccccc": blurColor;
	requiredColor = requiredColor == null? "#990000": requiredColor;
	
	// clean the document before sending the data
	var forms = document.getElementsByTagName("form");
	var len = forms.length;
	for (var i = 0; i < len; i++) {
		if (magicInputs_isIE()) {
			forms[i].attachEvent("onsubmit", magicInputs_onSubmit);
		} else {
			forms[i].addEventListener("submit", magicInputs_onSubmit, true);
		}
	}
	
	// prepare the input fields
	var labels = document.getElementsByTagName("label");
	var len = labels.length;
	for (var i = 0; i < len; i++) {
		var input = document.getElementById(labels[i].htmlFor);
		if ((input == null) || ((input.type != "text") && (input.type != "textarea"))) {
			continue;
		}
		
		input._value = input.value;
		input.focusText = labels[i].innerHTML;
		input.focusColor = focusColor;
		input.blurColor = blurColor;
		input.requiredColor = requiredColor;
		
		if (magicInputs_isIE()) {
			input.attachEvent("onfocus", magicInputs_onFocus);
			input.attachEvent("onblur", magicInputs_onBlur);
			input.attachEvent("onchange", magicInputs_onChange);
		} else {
			input.addEventListener("focus", magicInputs_onFocus, true);
			input.addEventListener("blur", magicInputs_onBlur, true);
			input.addEventListener("change", magicInputs_onChange, true);
		}
		
		if (input._value.length > 0) {
			input.style.color = input.focusColor;
		} else {
			if (input.className.indexOf("required") >= 0) {
				input.style.color = input.requiredColor;
			} else {
				input.style.color = input.blurColor;
			}
			input.value = input.focusText;
		}
	}
}

function magicInputs_onSubmit() {
	var target = null;
	if (magicInputs_isIE()) {
		target = event.srcElement;
	} else {
		target = this;
	}
	
	// clean the inputs
	var labels = document.getElementsByTagName("label");
	var len = labels.length;
	for (var i = 0; i < len; i++) {
		var input = document.getElementById(labels[i].htmlFor);
		if ((input == null) || ((input.type != "text") && (input.type != "textarea"))) {
			continue;
		}
		if (input._value.length == 0) {
			input.value = "";
		}
	}
}

function magicInputs_onFocus() {
	var target = null;
	if (magicInputs_isIE()) {
		target = event.srcElement;
	} else {
		target = this;
	}
	
	if (target._value.length > 0) {
		return;
	}
	
	target.style.color = target.focusColor;
	target.value = "";
	target.select();
}

function magicInputs_onBlur() {
	var target = null;
	if (magicInputs_isIE()) {
		target = event.srcElement;
	} else {
		target = this;
	}
	
	if (target._value.length > 0) {
		return;
	}
	
	if (target.className.indexOf("required") >= 0) {
		target.style.color = target.requiredColor;
	} else {
		target.style.color = target.blurColor;
	}
	target.value = target.focusText;
}

function magicInputs_onChange() {
	var target = null;
	if (magicInputs_isIE()) {
		target = event.srcElement;
	} else {
		target = this;
	}
	target._value = target.value;
}

/**
 * Detect if the client browser is Internet Explorer
 */
function magicInputs_isIE() {
	var agt = navigator.userAgent.toLowerCase();
	return (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1);
}