﻿// Beeline jQuery plugins/extensions

$.fn.clearSelect = function(label) {
	return this.each(function() {
		if (this.tagName == 'SELECT')
			this.options.length = 0;
		if (label) {
			var dropdownList = this;
			var option = new Option(label, null);
			if ($.browser.msie) {
				dropdownList.add(option);
			}
			else {
				dropdownList.add(option, null);
			}
		}
	});
}

$.fn.fillSelect = function(data, label) {
	return this.clearSelect(label).each(function() {
		if (this.tagName == 'SELECT') {
			var dropdownList = this;
			$.each(data, function(index, optionData) {

				var option = new Option(optionData.Text, optionData.Value);

				if ($.browser.msie) {
					dropdownList.add(option);
				}
				else {
					dropdownList.add(option, null);
				}
			});
		}
	});
}

/*
*  Dev One (http://www.dev-one.com)
*  Watermark jQuery plug-in
*
*/

$.fn.watermark = function(css, text) {
	$(this).unbind('focus');
	$(this).focus(function() {
		$(this).filter(function() {
			return $(this).val() == "" || $(this).val() == text
		}).removeClass(css).val("");
	});

	$(this).unbind('blur');
	$(this).blur(function() {
		$(this).filter(function() {
			return $(this).val() == ""
		}).addClass(css).val(text);
	});

	var input = $(this);
	$(this).closest("form").unbind('submit');
	$(this).closest("form").submit(function() {
		input.filter(function() {
			return $(this).val() == text
		}).val("");
	});

	// Only add the Watermark if there's nothing in there already or if it's being cleared
	if ($(this).val() == "") {
		$(this).addClass(css).val(text);
	}
};

$.fn.valToInt = function() {
	var v = this.val();
	if (v)
		return v;

	return 0;
}
