﻿$(document).ready(function()
{
	var found = null;

	found = $(".jqIntegersOnly");
	if (found.length > 0)
	{
		AttachIntegersOnly(found);
	}

	found = $(".jqLabeledInput");
	if (found.length > 0)
	{
		AttachLabeledInput(found);
	}
});

function AttachIntegersOnly(elementList)
{
	elementList.keypress(function(e) 
	{
		var key = e.which ? e.which : e.keyCode;
		switch (key)
		{
			case 35:	// End
			case 36:	// Home
			case 37:	// Left
			case 39:	// Right
			case 46:	// Delete
				if (e.which)
				{// varying browser implementations of event keycodes force this hack
					e.preventDefault();
				}
				break;
			case 8:		// Backspace
			case 9:		// Tab
			case 13:	// Enter
			case 16:	// Shift
			case 27:	// Esc
			case 48:	// 0
			case 49:	// 1
			case 50:	// 2
			case 51:	// 3
			case 52:	// 4
			case 53:	// 5
			case 54:	// 6
			case 55:	// 7
			case 56:	// 8
			case 57:	// 9
				break;
			case 99:	// c
			case 118:	// v
			case 120:	// x
				if (!e.ctrlKey)
				{// If not Control key pressed
					e.preventDefault();
				}
				break;
			default:
				if (!(e.ctrlKey || e.altKey))
				{// If not Control or Alt key pressed
					e.preventDefault();
				}
				break;
		}
	});
}

function AttachLabeledInput(elementList)
{
	elementList.each(function()
	{
		$(this).data("title", $(this).attr("title"));
		$(this).removeAttr("title");
		$(this).val($(this).data("title"));
		$(this).addClass("inactiveTextInput");

		$(this).focus(function()
		{
			if ($(this).val() == $(this).data("title"))
			{
				$(this).val("");
				$(this).removeClass("inactiveTextInput");
			}
		})
		.blur(function()
		{
			if ($(this).val() == "")
			{
				$(this).addClass("inactiveTextInput");
				$(this).val($(this).data("title"));
			}
		});
	});
}
