//only allows numbers to be added into a text box when the key press event is called
// actually, it allows " OR " as well - it has to since we have "OR" filtering.
function onlyNumberKeyPressed(id, event)
{
	var textInput = document.getElementById(id);
	var e = event || window.event;
	var code = e.charCode || e.keyCode;
	
	//if not number and not space/OR character, prevent character from being added
	// enter key is okay too (code 13)
	if (!isCharCodeANumber(code) && code != 13)
	{
		//firefox/netscape
		if (e.charCode)
		{
			e.preventDefault();
		}
		
		//ie
		if (e.keyCode)
		{
			e.keyCode = 0;
		}
	}
}

//prevents angular brackets from being added to a text box when the key press event is called
function preventAngularBrackets_KeyPress(event)
{
	var e = event || window.event;
	var code = e.charCode || e.keyCode;
	
	//if '<' or '>', prevent character from being added
	if (code == 60 || code == 62)
	{
		//firefox/netscape
		if (e.charCode)
		{
			e.preventDefault();
		}
		
		//ie
		if (e.keyCode)
		{
			e.keyCode = 0;
		}
	}
}

function extractDecimal(str) {
	var val = "";
	var c;
	var dec = false;
	
	for(var i=0; i<str.length; i++) {
		c = str.charAt(i);
		if(c >= '0' && c <='9')		
			val += c;
			
		if(c=='.' && !dec) {
			val += c;
			dec = true;
		}
	}
	
	return parseFloat(val);
}

function onlyDecimalKeyPressed(id, event)
{
	var textInput = document.getElementById(id);
	var e = event || window.event;
	var code = e.charCode || e.keyCode;
	
	// whether the text has a decimal in it yet
	var dec = textInput.value.indexOf(".")!=-1;
	
	// only add this character if its a number, or if its a decimal point and there isnt a decimal point in the text yet
	if (!isCharCodeANumber(code) && (dec || String.fromCharCode(code)!='.'))
	{
		//firefox/netscape
		if (e.charCode)
		{
			e.preventDefault();
		}
		
		//ie
		if (e.keyCode)
		{
			e.keyCode = 0;
		}
	}
}

function filterWhiteSpaceKeyUp(textId, buttonId)
{
	var text = document.getElementById(textId);
	var button = document.getElementById(buttonId);
	
	if (isOnlyWhiteSpace(text.value))
	{
		button.disabled = true;
	}
	else
	{
		button.disabled = false;
	}
}

//when the Select changes value and the value is the force to number value, the text box will set the on key press event to onlyNumberKeyPressed
//valuesToForceNumbers can handle multiple values; just slipt the values with ','
function forceNumbers_OnChange(selectId, textId, buttonId, valuesToForceNumbers)
{
	var values = valuesToForceNumbers.split(",");
	var select = document.getElementById(selectId);
	var text = document.getElementById(textId);
	
	for (var cnt_Values = 0; cnt_Values < values.length; ++cnt_Values)
	{
		if (select.selectedIndex != -1 && select.options[select.selectedIndex].value == values[cnt_Values])
		{
			text.onkeypress = onlyNumberKeyPressed;
			
			if (!isOnlyNumbers(text.value))
			{
				text.value = "";
				filterWhiteSpaceKeyUp(textId, buttonId);
			}
			
			break;
		}
		else
		{
			text.onkeypress = null;
		}
	}
}

//when the Select changes value and the value is the disable value, it will disable a text box and/or a button
function disable_OnChange(selectId, textId, buttonId, valueToDisable)
{
	var select = document.getElementById(selectId);
	var text = document.getElementById(textId);
	var button = document.getElementById(buttonId);
	
	if (select.selectedIndex != -1)
	{
		if (select.options[select.selectedIndex].value == valueToDisable)
		{
			if (text)
			{
				text.disabled = true;
			}
			
			if (button)
			{
				button.disabled = true;
			}
		}
		else
		{
			if (text)
			{
				text.disabled = false;
			}
			
			if (button)
			{
				button.disabled = false;
			}
			
			if (text && button)
			{
				filterWhiteSpaceKeyUp(textId, buttonId);
			}
		}
	}
	else
	{
		if (text)
		{
			text.disabled = true;
		}
		
		if (button)
		{
			button.disabled = true;
		}
	}
}

//checks to see if the string only has numbers
function isOnlyNumbers(text)
{
	var allowed = "0123456789";
	
	for (var cnt_Char = 0; cnt_Char < text.length; ++cnt_Char)
	{
		if (allowed.indexOf(text.charAt(cnt_Char)) == -1)
		{
			return false;
		}
	}
	
	return true;
}

//checks to see if the string only has spaces or a new line
function isOnlyWhiteSpace(text)
{
	for (var cnt_Char = 0; cnt_Char < text.length; ++cnt_Char)
	{
		if (text.charAt(cnt_Char) != " " && text.charCodeAt(cnt_Char) != 10 && text.charCodeAt(cnt_Char) != 13)
		{
			return false;
		}
	}
	
	return true;
}


//determines if the character code is a number or an " OR " specifier
function isCharCodeANumber(code)
{
	var character = String.fromCharCode(code);
	var allowed = "0123456789 orOR";
	
	//if not number, set the event's character code to zero (empty string)		
	if (allowed.indexOf(character) == -1)
	{
		return false;
	}
	
	return true;
}

//This script and many more are available free online at
//The JavaScript Source!! http:javascript.internet.com
//Written by Steve - http:jsmadeeasy.com/
function getObject(obj)
{
	var theObj;
	
	if(document.all)
	{
		if(typeof obj=="string")
		{
			return document.all(obj);
		}
		else
		{
			return obj.style;
		}
	}
				
	if(document.getElementById)
	{
		if(typeof obj=="string")
		{
			return document.getElementById(obj);
		}
		else
		{
			return obj.style;
		}
	}
			
	return null;
}

function trimString(str) {
	var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
			
//Character Count
function Contar(textBoxID, labelID, text, maxLength)
{
	var textBox = getObject(textBoxID);
	var label = getObject(labelID);
	var charsLeft = maxLength - textBox.value.length;  
				
	if(charsLeft <= 0)
	{
		charsLeft = 0;
		text='<span class="disable"> ' + text + ' </span>';
		textBox.value = textBox.value.substr(0, maxLength);
	}
				
	label.innerHTML = text.replace("{CHAR}",charsLeft);
}
//End of The JavaScript Source stuff