$().ready(function() {
	$(".useWatermark").each(function(i) {
		if ($(this).attr('title') == null || $(this).attr('title') == "") {
			$(this).attr('title', $(this).val()); // we only want to do this once!
		}
	});
	
	// we will find each input field that will use a watermark
	$(".useWatermark").each(function(i) {
		$(this).keypress(function() {
			if ($.trim($(this).val()) == $(this).attr('title')) {
			  $(this).val("").removeClass("watermark");
			}
		}).keyup(function() {
			if ($.trim($(this).val()) == "") {
				 $(this).val($(this).attr('title')).addClass("watermark");
				 setCaretPosition(this.id, 0);
			}
		});
	});
	
	$(".useWatermark").focus(function () {
		setCaretPosition(this.id, 0);
	});
	
	$(".useWatermark").blur(function () {
		if ($.trim($(this).val()) == "" || $(this).val() == $(this).attr('title')) {
			$(this).val($(this).attr('title')).addClass("watermark");
		}
	});
	
	// for select drop downs
	$('.changeSelect').change(function() {
		if ($(this).attr('selectedIndex') > 0) {
			$(this).removeClass("watermark");
		} else {
			$(this).addClass("watermark");
		}
	});
});

function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);
	
    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else {
                elem.focus();
			}
        }
    }
}