/*
	General JavaScript Code Library
*/

var currentPopup;
var currentSubPopup;

function validateInteger( val )
{
	if( (val >= 48 && val <= 57) || val == 45 )
	{
		return true;
	}
	
	return false;
}

function validateWildCard( val )
{
	if( val == 37 )
	{
		return true;
	}
}

function validateNumeric( val, allowMinus, allowDecimalDot )
{
	// Allow minus and decimal dot and [0-9]
	if( (val == 45 && allowMinus == true ) || (val == 46 && allowDecimalDot == true ) || ( val >= 48 && val <= 57) )
	{
		return true;
	}
	
	return false;
}

function allowOnlyDateTimeCharacters( textbox )
{
	var val = window.event.keyCode;
	if( (val < 45 || val > 58) && val != 32 )
	{
		//window.alert( 
		//"Only the following characters are valid for entering a date: [0-9] - & : & /"
		//);
		window.event.returnValue = 0;
		textbox.focus();
		return false;
	}
	
	return true;
}


function allowOnlyNumericCharacters( textbox, allowMinus, allowDecimalDot )
{
	var r = textbox.createTextRange();
	var charOk = false;
	var val = window.event.keyCode;
	
	if( validateNumeric( val, allowMinus, allowDecimalDot ) )
	{	
		if( textbox.value.indexOf( "-" ) >= 0 && val == 45 )
		{
			charOk = false;
		}	
		else if( textbox.value.indexOf( "." ) >= 0  && ( val == 46 ) )
		{
			// Only one dot is allowed
			charOk = false;
		}
		else
		{
			charOk = true;
		}
			
		
		if( charOk == true )
		{
			return true;
		}
		else
		{
			window.event.returnValue = 0;
			textbox.focus();
			return false;
		}
	}
	
	window.event.returnValue = 0;
	textbox.focus();
	return false;			
}

function getKeyCode()
{
	return window.event.keyCode;
}

function getCaretPosition( inp )
{

	if(inp.selectionEnd)
	return inp.selectionEnd;

	if(inp.createTextRange)
	{
		var docrange = document.selection.createRange();
		var inprange = inp.createTextRange();
		inprange.setEndPoint('EndToStart', docrange);
		return inprange.text.length;
	}

	return inp.value.length; // sucks, punt

}
		
		
function onKeyPressNumericTextBox( textbox, allowNegative, scale, precision )
{
	//alert( 'scale' + scale );
	//alert( 'precision' + precision );

	if( document.selection.createRange().text == textbox.value )
	{
		textbox.value = '';
	}

	var r = textbox.createTextRange();

	var charOk = false;

	var keyCode = getKeyCode();
	
	if( ( keyCode == 45 && allowNegative ) ||	// Allow Minus Sign if allowed
		( keyCode == 46 && precision > 0 ) ||	// Allow decimal dot if precision > 0
		( keyCode >= 48 && keyCode <= 57 ))		// Allow characters between 0 and 9
	{
		var caretPos = getCaretPosition( textbox );
		
		// Minus sign is only allowed at the first position in the textbox
		if( keyCode == 45 )
		{ 
			if( caretPos == 0 )
			{
				charOk = true;
			}
		}
		else if( keyCode == 46 )
		{
			// Only allow a decimal dot if not yet present.
			var posDot = textbox.value.indexOf( "." );
			if( posDot == -1 && caretPos >= ( textbox.value.length - precision ))
			{
				charOk = true;
			}
		}
		else if( keyCode >= 48 && keyCode <= 57 )
		{
			var posStart = textbox.value.indexOf( "-" );
			if( posStart == -1 )
			{
				posStart = 0;
			}
			var posDot = textbox.value.indexOf( "." );
			var posDec = posDot >= 0 ? textbox.value.indexOf( "." ) + 1 : -1;

			if( posDot >= 0 && caretPos <= posDot && textbox.value.substring( posStart, posDot ).length <= ( scale - precision ) )
			{
				charOk = true;
			}

			else if( posDot == -1 && textbox.value.substr( posStart ).length < ( scale - precision ) )
			{
				charOk = true;
			}
			else if( posDec >= 0 && caretPos > posDot && textbox.value.substr( posDot ).length <= precision )
			{
				charOk = true;
			}
		}
	}

	if( !charOk )
	{
		// Cancel the event, do not print character in textbox.
		window.event.returnValue = 0;
		textbox.focus();
		return false;
	}
	
	return true;			
}
function allowOnlyIntegerCharacters( textbox )
{
	var val = window.event.keyCode;
	if( !validateInteger( val ) )
	{
		window.event.returnValue = 0;
		textbox.focus();
		return false;
	}
	
	return true;			
}

function allowOnlyIntegerCharacters( textbox, allowWildCards )
{
	var val = window.event.keyCode;
	if( !validateInteger( val ) )
	{
		if( allowWildCards )
		{	
			if( validateWildCard( val ) )
			{
			return true;
			}
		}	
		window.event.returnValue = 0;
		textbox.focus();
		return false;
	}
		
	return true;			
}

function toggleItem(item, img, imgExpand, imgCollapse)
{
	//alert(item.style.visibility);
	if(item.style.display == "none")
	{
		item.style.display = "block";
		img.src = imgCollapse;
	}
	else
	{
		item.style.display = "none";
		img.src = imgExpand;
	}
}
			
function doPopup(item, x,y)
{
	if(currentPopup)
	{
		currentPopup.style.visibility = "hidden";
	}
	
	currentPopup = item;
	
	item.style.posLeft = x;
	item.style.posTop = y;
	item.style.visibility = "visible";
	
	//window.status = "X=" + x + ";Y=" + y;
}
/*
function togglePopup(item, show)
{
	if(show == "true")
	{
		item.style.visibility = "visible";
	}
	else
	{
		item.style.visibility = "hidden";
	}
}
*/	
function hiLite(item, state)
{
	if(state == "hi")
	{
		//item.style.border = "0.03cm solid #cccccc";
		item.style.backgroundColor = "#ffffff";
	}
	else
	{
		//item.style.border = "";
		item.style.backgroundColor = "#eeeeee";					
	}
}

function absTop(el)
{ 
	//finds the top
	var n = el.offsetTop;
	var s = n + "(" + el.tagName + ")+";
	while (el.parentElement)
	{
		el = el.parentElement;
		n += el.offsetTop;
		//s = s + el.offsetTop + "(" + el.tagName + ")+";
	}
	
	//window.status = s + "=(" + n + ")";
	
	return n;
}	

function absLeft(el)
{ //finds the top
	var n = el.offsetLeft;
	var s = n + "(" + el.tagName + ")+";
	while (el.parentElement)
	{
		disableAllInputs();
		el = el.parentElement;
		n += el.offsetLeft;
		s = s + el.offsetLeft + "(" + el.tagName + ")+";
	}
	window.status = s + "=(" + n + ")";
	return n;
}

// Function that goes through all elements and disables all input elements.
function disableInput()
{
	
	for( i = 0; i < document.all.length; i++ )
	{
		//window.alert(document.all[i].tagName);
		
		if( document.all[i].tagName == "INPUT" )
		{
			document.all[i].disabled = true;
		}
	}
}

// Function that hide all the messagebox errormessage
function hideMessageBox()
{
	var spana; 
	
	if( document.all ) 
	{ 
		spana = document.all.tags( "span" ); 
	} 
	else if( document.getElementsByTagName ) 
	{ 
		spana = document.getElementsByTagName( "span" );
	}
	
	if( spana )
	{
		for( var i=0; i < spana.length; i++ ) 
		{
			if( spana[i].id.substring( 0, 11 ) == "_messagebox" )
			{
				spana[i].style.visibility = "hidden";
			}
		}
	} 
}