﻿// MaxLength property doesn't work on MultiLine TextBoxes.
// Sources from http://www.codeproject.com/useritems/Textarea_Length_Validator.asp

// keep user from entering more than maxLength characters
function doKeypress(control)
{
	maxLength = control.attributes["maxLength"].value;
	value = control.value;
	if(maxLength && value.length > maxLength-1)
	{
		event.returnValue = false;
		maxLength = parseInt(maxLength);
	}
}

// cancel default behaviour
function doBeforePaste(control)
{
	maxLength = control.attributes["maxLength"].value;
	if(maxLength)
		event.returnValue = false;
}

// cancel default behaviour and create a new paste routine
function doPaste(control)
{
	maxLength = control.attributes["maxLength"].value;
	value = control.value;
	if(maxLength)
	{
		event.returnValue = false;
		maxLength = parseInt(maxLength);
		var oTR = control.document.selection.createRange();
		var iInsertLength = maxLength - value.length + oTR.text.length;
		var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
		oTR.text = sData;
	}
}

// removes a specified list of units of the textbox
function RemoveUnit(textbox, removeNonNumericCharacters)
{
	textbox.value = textbox.value.replace("CHF", "");
	textbox.value = textbox.value.replace("USD", "");
	textbox.value = textbox.value.replace("EUR", "");
	textbox.value = textbox.value.replace("JPY", "");
	textbox.value = textbox.value.replace("kg/m2", "");
	textbox.value = textbox.value.replace("kg", "");
	textbox.value = textbox.value.replace("m3", "");
	textbox.value = textbox.value.replace("m2", "");
	textbox.value = textbox.value.replace("m", "");
	textbox.value = textbox.value.replace(" ", "");
	
	if (removeNonNumericCharacters)
		RemoveNonNumericCharacters(textbox);
	
	SetCursorAtEnd(textbox);
	
	textbox.style.color = '';
}

// removes non-numeric characters of the textbox
function RemoveNonNumericCharacters(textbox)
{
	textbox.value = textbox.value.replace(/[^0-9]/g, "");
}

// sets the cursor at the end of the textbox
function SetCursorAtEnd(textbox)
{
	if (textbox.createTextRange)
	{
		var r = (textbox.createTextRange());
		r.moveStart('character', (textbox.value.length));
		r.collapse();
		r.select();
	}
}

// adds the unit at the end of the textbox value
function SetUnit(textbox, unit, color)
{
	// trim unit
	unit = unit.replace(/^\s+|\s+$/g,"");
	
	if (textbox.value.length > 0)
	{
		textbox.style.color = '';
		textbox.value = textbox.value + ' ' + unit;
	}
	else
	{
		textbox.style.color = color;
		textbox.value = unit;
	}
}

// checks if the pressed key is a number
// use: onkeypress="return IsNumberKey(event)"
function IsNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

// checks if the pressed key is valid for a decimal number (0-9 and . and ,)
// use: onkeypress="return IsDecimalNumberKey(event)"
function IsDecimalNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if ((charCode > 47 && charCode < 58) || charCode == 44 || charCode == 46)
		return true;

	return false;
}
