
var MouseOverStyle;
var clientCalcTotalDisplaySum;
var clientCalcRowCorrection;
tooltipControl = null;
document.onmousemove = updateTooltip;


function updateTooltip(e) {
  if (tooltipControl != null) {

    x = (document.all) ? window.event.x + tooltipControl.offsetParent.scrollLeft : e.pageX;
    y = (document.all) ? window.event.y + tooltipControl.offsetParent.scrollTop  : e.pageY;

    matrixPanel = GetElement('ctl00_PanelMatrixPopup');
    if(matrixPanel != null)
    {
    
        x = x - matrixPanel.offsetLeft;
        y = y - matrixPanel.offsetTop;
    }
    
    tooltipControl.style.left = (x + 10) + "px";
    tooltipControl.style.top   = (y + 10) + "px";
  }
}
 
function showTooltip(id) {
  tooltipControl = GetElement(id);
  if(tooltipControl != null)
  {
    tooltipControl.style.display = "block"
  }
}
 
function hideTooltip() {
  if(tooltipControl != null)
  {
    tooltipControl.style.display = "none";
  }
}

// **************************************************************************************
//  Created on:	06.08.2007
//	Created by:	Thomas Beyler, ascaion ag
// ======================================================================================
/// <summary>
// gets a page-object, works for all browsers
// </summary>
// *****************************************************************************
function GetElement(elementName)
{
    if (GetElement)
    {
        return document.getElementById(elementName);
    }
    else if (document.all)
    {
        return document.all(elementName);
    }
    else if (document.layers)
    {
        return document.layers(elementName);
    }
    else
        return null;
}

// **************************************************************************************
//  Created on:	05.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Changes a style of the passed element
/// </summary>
/// *****************************************************************************
function OnMouseOverChangeStyle(element, newStyle)
{
	MouseOverStyle = element.className;
	element.className = newStyle;
	
} // function

// **************************************************************************************
//  Created on:	05.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Resets a style of the passed element
/// </summary>
/// *****************************************************************************
function OnMouseOutResetStyle(element)
{
	element.className = MouseOverStyle;
	
} // function

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Opens a popup window
/// </summary>
/// *****************************************************************************
function OpenHtmlWindow(url)
{
	OpenHtmlWindow(url, 480, 640);
}

function OpenHtmlWindow(url, width, height)
{
	// center window on screen
	posX = (screen.Width - width) / 2;
	posY = (screen.Height - height) / 2;
	
	if (posX < 0) posX = 0;
	if (posY < 0) posY = 0;

	window.open(url, "eapp", "toolbar=no, height=" + height + ", width=" + width + ", top=" + posY + ", left=" + posX + ", screeny=" + posY + ", screenx=" + posX + ", scrollbars=no, directories=no, menubar=no, personalbar=no, status=no, titlebar=no, resizable=yes");
	
} // function

function OpenHtmlWindowWithToolbar(url, width, height)
{
	// center window on screen
	posX = (screen.Width - width) / 2;
	posY = (screen.Height - height) / 2;
	
	if (posX < 0) posX = 0;
	if (posY < 0) posY = 0;

	window.open(url, "eapp", "toolbar=yes, height=" + height + ", width=" + width + ", top=" + posY + ", left=" + posX + ", screeny=" + posY + ", screenx=" + posX + ", scrollbars=no, directories=no, menubar=yes, personalbar=no, status=no, titlebar=no, resizable=yes");
	
} // function

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Formats a float value
/// </summary>
/// *****************************************************************************
function FComma(SS, clientPrefix) 
{
	var T='', S=SS, L=S.length-1, C, j, P = S.indexOf('.')-1;
	S = S.replace('.', GetElement(clientPrefix + 'HiddenFieldDecimalSeperator').value);
	if (P<0) 
		P=L;
	for (j=0; j<=L; j++) 
	{
		T += C = S.charAt(j);
		if ((j<P) && ((P-j)%3==0) && (C!='-')) 
			T+=GetElement(clientPrefix + 'HiddenFieldGroupSeperator').value;
	}
	return T;
}
		
// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Writes a number to a span
/// </summary>
/// *****************************************************************************
function WriteNumberToSpan(span, value, clientPrefix)
{
	// get object
	obj = GetElement(clientPrefix + span);

	// format number
	sum = String(value);
	if (sum.indexOf('.') <= 0)
		sum = sum + '.00';
	if (sum.indexOf('.') == sum.length-2)
		sum = sum + '0';
	
	if(obj.childNodes[0] != null)
	{
	    var sumTextContent = document.createTextNode(sum);
        obj.replaceChild(sumTextContent, obj.childNodes[0]);
    }
    else
    {
        // we need to write into the object itself. While we are at it, 
        // we can change the color of the element
        obj.value = sum;
        if(value >= 0)
            obj.style.color = 'green';
        else
            obj.style.color = 'red';
    }
}

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Writes a number to a control 
/// </summary>
/// *****************************************************************************
function WriteNumberToControl(control, value, clientPrefix)
{
	// get object
	obj = GetElement(clientPrefix + control);
	
	// format number
	sum = String(value);
	if (sum.indexOf('.') <= 0)
		sum = sum + '.00';
	if (sum.indexOf('.') == sum.length-2)
		sum = sum + '0';
	
	obj.value = FComma(sum, clientPrefix);
}

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Returns a culture formating independent value
/// </summary>
/// *****************************************************************************
function GetNeutralValue(value, clientPrefix)
{
    // parse value
	while (value.indexOf(GetElement(clientPrefix + 'HiddenFieldGroupSeperator').value) >= 0)
		value = value.replace(GetElement(clientPrefix + 'HiddenFieldGroupSeperator').value, '');
	
	value = value.replace(GetElement(clientPrefix + 'HiddenFieldDecimalSeperator').value, '.');

	return value;
}

// **************************************************************************************
//  Created on:	07.01.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Checks, if the passed value belongs to a row that should not be accumulated
/// </summary>
/// *****************************************************************************
function GetFilteredValue(obj, value)
{
    // NaN can be returned
    if (isNaN(value))
        return value;
        
    if (obj.parentNode != null && obj.parentNode.parentNode != null)
    {
        if (obj.parentNode.nodeName.toLowerCase() == "td" && obj.parentNode.parentNode.nodeName.toLowerCase() == "tr")
        {
            // if accumulate row is set and not "true", reset the value
            if (obj.parentNode.parentNode.getAttribute("accumulateRow") != null &&
                obj.parentNode.parentNode.getAttribute("accumulateRow") != "yes")
            {
                value = 0.0;
            }
        }
    }
        
    return value;
}

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Renders the sum of a few amount textboxes
/// </summary>
/// *****************************************************************************
function RenderSum(textBoxRawId, labelRawId, clientPrefix)
{
	// extract current grid row
	currentRow = 1;
	
	// build total for this group
	var sum = 0.0;
	var value = 0.0;
	var tempValue = 0.0;
	var correctionValue = 0.0;
	for (i = 1; i <= Number(GetElement(clientPrefix + 'HiddenFieldGridRows').value); i++)
	{
		tempValue = GetNeutralValue(GetElement(textBoxRawId + i).value, clientPrefix);
		tempValue = GetFilteredValue(GetElement(textBoxRawId + i), tempValue);
		
		correctionValue = 0.0;
		if (GetElement(labelRawId + i) != null)
			correctionValue = GetNeutralValue(GetElement(labelRawId + i).innerText, clientPrefix);
		
		if (tempValue != '')
		{
			if (isNaN(tempValue))
			{
				GetElement(textBoxRawId + i).focus();
				GetElement(textBoxRawId + i).select();
				return false;
			}
			
			value = parseFloat(tempValue);
			sum += value;
			
			if (correctionValue != '')
			{
				if (!isNaN(correctionValue))
					sum -= parseFloat(correctionValue);
			}
		}
		
	}
	sum = Math.round(100*sum) / 100;
	
	// correct:
	sum = sum + clientCalcTotalDisplaySum;
	
	// write number
	WriteNumberToControl(GetElement(clientPrefix + 'HiddenFieldTotalId').value, sum, clientPrefix);
	
	return true;
}

// **************************************************************************************
//  Created on:	12.05.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	Renders some values out of textboxes into a span
/// </summary>
/// *****************************************************************************
function RenderSumToSpan(clientPrefix, writeTo, textBoxes)
{

	// get all textboxes
	var textBoxArray = textBoxes.split(';');
	
	var sum = 0.0;
	var value = 0.0;
	var tempValue = 0.0;
	for (i = 0; i < textBoxArray.length; i++)
	{
		tempValue = GetNeutralValue(GetElement(clientPrefix + textBoxArray[i]).value, clientPrefix);
		tempValue = GetFilteredValue(GetElement(clientPrefix + textBoxArray[i]), tempValue);

		if (tempValue != '')
		{
			if (isNaN(tempValue))
			{
				tempValue = '0';
			}
			
			value = parseFloat(tempValue);
			sum += value;
		}
	}
	
	sum = Math.round(100*sum) / 100;
	WriteNumberToSpan(writeTo, sum, clientPrefix);
}

// **************************************************************************************
//  Created on:	20.02.2007
//	Created by:	Daniel Szondy, ascaion ag 
// ======================================================================================
/// <summary>
///	The onkey event for the textboxes that are used in the calculation
/// </summary>
/// *****************************************************************************
function TextBoxKeyDown(event, textBox, clientPrefix, totalSum)
{
	if (totalSum == null)
		totalSum = 0;
		
	clientCalcTotalDisplaySum = totalSum;

	var textBoxName = String(GetElement(clientPrefix + 'HiddenFieldAmountTextBoxId').value);
	var labelName = '';
	if (GetElement(clientPrefix + 'HiddenFieldAmountLabelId') != null)
		labelName = String(GetElement(clientPrefix + 'HiddenFieldAmountLabelId').value);
		
	// extract current grid row
	var currentRow = String(textBox.id);
	currentRow = currentRow.slice(textBoxName.length, currentRow.length);
	
	if (event.type == "blur")
	{
		// calculate sum
		result = RenderSum(textBoxName, labelName, clientPrefix);
		
		// cancel bubble
		event.cancelBubble = true;
		event.returnValue = false;
		
		return;
	}
	else if (event == null)
	{
		// calculate sum
		result = RenderSum(textBoxName, labelName, clientPrefix);
	
		return;
	}
	
	// drop <enter>
	var isNewLine = false;
	if (event.keyCode == 13)
	{   
	    isNewLine = true;
		//event.keyCode = 9;
	}
	
	// check for cursor keys
	if (event.keyCode == 38 || event.keyCode == 40  || isNewLine)
	{
		// cancel bubble
		event.cancelBubble = true;
		event.returnValue = false;
		
		// determine new grid row
		if (event.keyCode == 38)
			newRow = Number(currentRow) - 1;
		else
			newRow = Number(currentRow) + 1;
			
		if (newRow < 1)
			newRow = Number(GetElement(clientPrefix + 'HiddenFieldGridRows').value);
		else if (newRow > Number(GetElement(clientPrefix + 'HiddenFieldGridRows').value))
			newRow = 1;
			
		// get next control
		newId = textBoxName + newRow;
		
		// calculate sum
		result = RenderSum(textBoxName, labelName, clientPrefix);
		
		// everything fine, jump to next element
		if (result)
			GetElement(newId).focus();
	}
	// tab key
	else if (event.keyCode == 9)
	{	
		// calculate sum
		result = RenderSum(textBoxName, labelName, clientPrefix);
		
		// on error cancel proceed
		if (!result)
		{
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
}

// **************************************************************************************
//  Created on:	30.04.2007
//	Created by:	Thomas Beyler, ascaion ag
// ======================================================================================
/// <summary>
///	Used to find the xy-position of any control in the page
/// </summary>
/// *****************************************************************************
function findPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent) 
    {
	    curleft = obj.offsetLeft
	    curtop = obj.offsetTop
	    
	    while (obj = obj.offsetParent) 
	    {
		    curleft += obj.offsetLeft
		    curtop += obj.offsetTop
	    }
    }
    
    return [curleft,curtop];
}

// **********************************************
// Limits a textbox length to a specified length
// **********************************************
function LimitTextBoxLength(sourceId, limit)
{
	LimitTextBoxLength(sourceId, limit);
}

function LimitTextBoxLength(sourceId, limit, lengthInfoId)
{
	if (GetElement(sourceId).value.length >= limit)
	{
		if (event.keyCode >= 65 || event.keyCode == 13 || event.keyCode == 32)
		{
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
}

function LimitTextBoxLengthInfo(sourceId, limit, lengthInfoId)
{
	if (GetElement(sourceId).value.length > limit)
		GetElement(sourceId).value = GetElement(sourceId).value.substr(0, limit);
		
	GetElement(lengthInfoId).value = (limit - GetElement(sourceId).value.length);
}

// **********************************************
// binds the pressing of "enter" to an object click
// **********************************************
function bindEnterKey(objEventDest)
{
    if ((event.keyCode == 13))
    {
        event.cancelBubble = true;
        event.returnValue = false;
        objEventDest.click();
    }
}