﻿function validateIsNumeric(textBox, e)
{   
	e = e == null ? window.event : e;
	var allowDecimals = textBox.getAttribute("allowDecimals") == "true";
	
	var min = null;
	if(textBox.getAttribute("min") != null)
		min = parseInt(textBox.getAttribute("min"))

	var max = null;
	if(textBox.getAttribute("max") != null)
		max = parseInt(textBox.getAttribute("max"))
	
	var decimalPlaces = false;
	if(textBox.getAttribute("decimalPlaces") != null)
		decimalPlaces = parseInt(textBox.getAttribute("decimalPlaces"))
	
	var valid = true;
	var toReturn;
	
	//Tab
	if(e.keyCode == 9)
		return true;
	
	//Decimal
	if(e.keyCode == 110)
	{
		if(!allowDecimals)
			return false;
			
		if(textBox.value.indexOf(".") == -1)
			return true;
	}

	//Up and down arrows
	if(e.keyCode == 38 || e.keyCode == 40)
	{
		var newVal = textBox.value.length > 0 ? parseInt(textBox.value) : min - 1;
		if(e.keyCode == 38)
			newVal++;
		
		if(e.keyCode == 40)
			newVal--;

		if(min >= 0 && newVal < min)
			valid = false;
		
		if(max >= 0 && newVal > max)
			valid = false;
		
		if(valid)
			textBox.value = newVal;
		
		toReturn = false;
	}
	else
	{
		var ch = getNumericValue(e.keyCode);
		if(ch != null)
		{
			//ensure decimal places
			if(allowDecimals)
			{
				var decimalIndex = textBox.value.indexOf(".");
				if(decimalIndex == -1)
					return true;
				
				if((textBox.value.length - decimalIndex) > decimalPlaces)
					return false;
				else
					return true;
			}
			else
				toReturn = true;
		}
		else
		{
			if(isNavKey(e.keyCode))
				toReturn = true;
			else
				toReturn = false;
		}
	}
	
	return toReturn;
}

function validateMinMax(textBox, e)
{
	var min = parseInt(textBox.getAttribute("min"));
	var max = parseInt(textBox.getAttribute("max"));
	var valid = true;
	var num = parseInt(textBox.value);
	
	if(min >= 0 && num < min)
	{
		valid = false
		num = min;
	}
	
	if(max >= 0 && num > max)
	{
		valid = false
		num = max;
	}
	
	if(!valid)
		textBox.value = num;
}

function validateMinMaxBlur(textBox, e)
{
	validateMinMax(textBox, e);
	
	var min = parseInt(textBox.getAttribute("min"));
	var allowEmpty = textBox.getAttribute("allowEmpty") == "true";
	
	if(textBox.value.length == 0 && !allowEmpty)
		textBox.value = min;
}

function getNumericValue(keyCode)
{
	//Numeric keys
	if(keyCode >= 96 && keyCode <= 105)
		keyCode -= 48;
	
	var ch = String.fromCharCode(keyCode);
	var num = parseInt(ch);
	var valid = !isNaN(num);
	
	return valid ? num : null;
}

function autoIncreaseHeight(txt, event)
{
	var ctrl = false;
	var keyCode = event.keyCode;
	
	if(document.all)
		ctrl = event.ctrlKey
	else
		ctrl = event.ctrlKey
	
	if(ctrl)
	{
		if(keyCode == 40 && txt.rows < 20)
			txt.rows++;
		
		if(keyCode == 38 && txt.rows > 5)
			txt.rows--;
	}
}

function trimMaxLength(txt)
{
	var scrollPos = txt.scrollTop;
	var max = txt.getAttribute("maxLength");
	if(txt.value.length > max)
		txt.value = txt.value.substring(0, max);
		
	txt.scrollTop = scrollPos;
}

var lastKeyCount = -1;
function displayRemainingLength(txt, onLoad)
{
	if(txt == null)
		return;

	if(!onLoad)
		lastKeyCount++;
	
	if(lastKeyCount % 1 > 0)
		return;
		
	var spanId = txt.id + "_remSpan";
	var span = document.getElementById(spanId);
	
	if(span == null)
		return;

	var max = txt.getAttribute("maxLength");
	var count = max - txt.value.length;
	if(count < 0)
		count = 0;

	span.innerHTML = count + " characters remaining";
}

function formatCurrency(input)
{
	var num = input.value;
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents < 10)
		cents = "0" + cents;
	
	for(var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' +	
		num.substring(num.length - (4 * i + 3));

	var newVal = (((sign)?'':'-') + '$' + num + '.' + cents);
	input.value = newVal;
}

function formatPercentage(input)
{
	var num = input.value;
	num = num.toString().replace(/[^0-9.]/g, "");
	
	if(num == "" || isNaN(num) || num < 0)
		num = "0";
	
	var newVal = (num + ' %');
	input.value = newVal;
}

function ensureCase(input)
{
	var caseMode = input.getAttribute("caseMode");
	switch(caseMode)
	{
		case "Uppercase":
			input.value = input.value.toUpperCase();
			break;
			
		case "Lowercase":
			input.value = input.value.toLowerCase();
			break;
	}
}

function maskInput(input)
{
	if(input == null)
		return;
	
	var mask = input.getAttribute("mask");

	if(mask == null || mask.length == 0)
		return;

	var curText = removeNonNumeric(input.value);
	var newText = "";
	
	var maskIndex = 0;
	var textIndex = 0;
	var nextMaskChar;
	var nextTextChar;

	for(var i = 0; i < mask.length; i++)
	{
		nextMaskChar = mask.substr(i, 1);
		
		if(nextMaskChar == "#")
		{
			nextTextChar = curText.substr(textIndex, 1);
			if(nextTextChar.length == 0)
				break;

			textIndex++;
			newText += nextTextChar;
		}
		else
		{
			if(textIndex < curText.length)
				newText += nextMaskChar;
		}
	}
	
	if(curText.length == 0)
		input.value = "";
	else
		input.value = newText;

	if(input.selectionStart)
		input.setSelectionRange(input.selectionEnd + 1, input.selectionEnd + 1);
}

function removeNonNumeric(text)
{
	return text.replace(/[^0-9]/g, "");
}

function validateIsAlpha(textBox, e)
{
	e = e == null ? window.event : e;
	var code = e.keyCode;
	
	//Tab, Arrows, Delete, Backspace, Space, etc.
	if(isNavKey(code))
		return true;	
	
	//Numeric keys
	if(code >= 65 && code <= 90)
		return true;
		
	return false;
}

function ensureTimeNumeric(txt, e)
{
	var evnt = e == null ? window.event : e;
	
	if(evnt.shiftKey && (evnt.keyCode == 59 || evnt.keyCode == 186))
		return true;
	else
		return ensureNumeric(txt, e);
}

function ensureNumeric(txt, e)
{
	var evnt = e == null ? window.event : e;

	//Allow navigation keys
	if(isNavKey(e.keyCode))
		return true;

	var val = getNumericValue(e.keyCode);
	
	if(val == null)
		return false;
	
	return true;
}

function formatTime(txt, e)
{
	var val = txt.value;
	
	var index = val.indexOf(":");
	if(index > -1)
	{
		if(val.length <= index + 2)
			return;
	}

	val = replaceStr(val, ":", "");
	
	if(val.length < 3)
		return;
	
	var hours = val.substr(0, val.length - 2);
	if(hours.length > 2)
		hours = hours.substr(0, 2);
	
	var minutes = val.substr(val.length - 2);

//	if(parseInt(hours) > 12)
//		hours = "12";

//	if(parseInt(hours) < 1)
//		hours = "1";

//	if(parseInt(minutes) > 59)
//		minutes = "45";

//	if(parseInt(minutes) < 0)
//		minutes = "00";
	
	var time = hours + ":" + minutes;
	txt.value = time;
	
	if(txt.selectionStart)
		txt.setSelectionRange(txt.selectionEnd + 1, txt.selectionEnd + 1);
}

function isNavKey(keyCode)
{
	//Tab, Arrows, Delete, Backspace, Space, etc.
	if(keyCode == 32 || keyCode == 13 || keyCode == 9 || keyCode == 8 || keyCode == 46 || (keyCode >= 37 && keyCode <= 40))
		return true;

	return false;
}