﻿var classDisabledRow = "DisabledRow";
var arrayDisablePanels = [];

function getCurrentStyle(element) 
{
    var w = (element.ownerDocument ? element.ownerDocument : element.documentElement).defaultView;
    return ((w && (element !== w) && w.getComputedStyle) ? w.getComputedStyle(element, null) : element.style);
}
//---------------------------------------------------------
function AbsoluteBottom(id)
{
	var curElem = document.getElementById(id);
	var height = curElem.offsetHeight;
	return AbsoluteTop(id)+height;
}

//---------------------------------------------------------
function AbsoluteTop(id)
{
	return getLocation(id).y;
}
////---------------------------------------------------------
//if(document.body.getClientRects)
//{
	var getLocation = function (id) {
	
		var element = document.getElementById(id);
		// For a document element, return zero.
		if (element.self || element.nodeType === 9) return {x:0,y:0};

		// clientRects[0] will give the top left corner of the first rectangle of the element,
		// which can be different from the top left corner of the bounding box in case the element
		// is wrapping. This is consistent with what other browsers are giving as coordinates.
		var clientRects = element.getClientRects();
		if (!clientRects || !clientRects.length) {
			return {x:0,y:0};
		}

		var w = element.ownerDocument.parentWindow;
		// substract 2px, the border of the viewport.
		// It can be changed in IE6 by applying a border style to the HTML element, but this is not supported by Atlas.
		// It cannot be changed in IE7.
		var offsetL = w.screenLeft - top.screenLeft - top.document.documentElement.scrollLeft + 2;
		var offsetT = w.screenTop - top.screenTop - top.document.documentElement.scrollTop + 2;

		// If the element is in an iFrame, the parent window of the owner document is that iFrame,
		// which introduces into the client rects of its children an offset that includes the coordinates
		// of the frame relative to the top viewport, its padding, margin and frame border, its scrolling
		// which we need to add to the offset.
		// In other non-iFrame cases, the offsets below will evaluate as zero because top === w.
		var f = w.frameElement || null;
		if (f) {
			// frameBorder has a default of "1" so undefined must map to 1, but that creates
			// a 2 pixel difference in the coordinates. A value of "0" (the string "0") evaluates
			// as true in a boolean context so (f.frameBorder || 1) * 2 evaluates as 0 for "0" and
			// as 2 for undefined and "1".
			var fstyle = f.currentStyle;
			offsetL += (f.frameBorder || 1) * 2 +
				(parseInt(fstyle.paddingLeft) || 0) +
				(parseInt(fstyle.borderLeftWidth) || 0) -
				element.ownerDocument.documentElement.scrollLeft;
			offsetT += (f.frameBorder || 1) * 2 +
				(parseInt(fstyle.paddingTop) || 0) +
				(parseInt(fstyle.borderTopWidth) || 0) -
				element.ownerDocument.documentElement.scrollTop;
		}

		var clientRect = clientRects[0];
		return {x:clientRect.left - offsetL,y:clientRect.top - offsetT}; 
	}
//}
//else
//{
	var getLocation = function(id) {
	 
		var element = document.getElementById(id);
		//For a document element, return zero.
		if ((element.window && (element.window === element)) || element.nodeType === 9) return new Sys.UI.Point(0,0);

		var offsetX = 0;
		var offsetY = 0;
		var previous = null;
		var previousStyle = null;
		var currentStyle = null;
		for (var parent = element; parent; previous = parent, previousStyle = currentStyle, parent = parent.offsetParent) 
		{
			var tagName = parent.tagName;
			currentStyle = getCurrentStyle(parent);

			// Firefox has its own quirk, which is that non-absolutely positioned elements that are
			// direct children of body get the body offset counted twice.
			if ((parent.offsetLeft || parent.offsetTop) &&
				!((tagName === "BODY") &&
				(!previousStyle || previousStyle.position !== "absolute"))) 
			{

				offsetX += parent.offsetLeft;
				offsetY += parent.offsetTop;
			}

			// This code works around a difference in behavior in Opera and Safari which includes
			// clientLeft and clientTop in the computedstyle offset.
			if (previous !== null && currentStyle) 
			{
				// This is to workaround a known bug in IE and Firefox:
				// <table> and <td> have strange behavior with offsetLeft/offsetTop and clientLeft/clientTop.
				// Say you have the following html: <table style="border-width:25px"><tr><td></table>
				// The offsetLeft and offsetTop for the <td> will be 25, but the client/borderLeft and
				// client/borderTop for the <table> will also be 25.  So if you count the client/borderLeft and
				// client/borderTop for the <table>, you will be double-counting the table border.
				if ((tagName !== "TABLE") && (tagName !== "TD") && (tagName !== "HTML")) 
				{
					offsetX += parseInt(currentStyle.borderLeftWidth) || 0;
					offsetY += parseInt(currentStyle.borderTopWidth) || 0;
				}
				if (tagName === "TABLE" &&
					(currentStyle.position === "relative" || currentStyle.position === "absolute")) 
				{
					offsetX += parseInt(currentStyle.marginLeft) || 0;
					offsetY += parseInt(currentStyle.marginTop) || 0;
				}
			}
		}
		currentStyle =  getCurrentStyle(element);
        var elementPosition = currentStyle ? currentStyle.position : null;
        var elementPositioned = elementPosition && (elementPosition !== "static");
        // If an element is absolutely positioned, its parent's scroll should not be subtracted, except on Opera.
        if (!elementPosition || (elementPosition !== "absolute")) 
        {
            // In Firefox and Safari, all parent's scroll values must be taken into account.
            // In IE, only the offset parent's because positioned elements are offset-parented to BODY and
            // don't need scroll substraction. Non-positioned elements are offset-parented to their parent,
            // which may be scrolled.
            for (var parent = element.parentNode; parent; parent = parent.parentNode) 
            {
                // In IE quirks mode, the <body> element has bogus values for scrollLeft and scrollTop.
                // So we do not use the scrollLeft and scrollTop for the <body> element.  This does not
                // break the standards mode behavior. (VSWhidbey 426176)
                tagName = parent.tagName;

                if ((tagName !== "BODY") && (tagName !== "HTML") && (parent.scrollLeft || parent.scrollTop)) 
                {
                    offsetX -= (parent.scrollLeft || 0);
                    offsetY -= (parent.scrollTop || 0);

                    currentStyle = getCurrentStyle(parent);
                    offsetX += parseInt(currentStyle.borderLeftWidth) || 0;
                    offsetY += parseInt(currentStyle.borderTopWidth) || 0;
                }
            }
        }
        return  {x:offsetX,y:offsetY};
	}
//}
//---------------------------------------------------------
function AbsoluteLeft(id)
{
	return getLocation(id).x;
}
//---------------------------------------------------------
function AbsoluteRight(id)
{
	var curElem = document.getElementById(id);
	var width = curElem.offsetWidth;
	return AbsoluteLeft(id)+width;
}
//---------------------------------------------------------

function GetWindowSize()
{
	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement &&( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		myWidth = document.documentElement.clientWidth;
		myHeight =document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight =document.body.clientHeight;
	}
	return [myWidth,myHeight];
}
//---------------------------------------------------------
function GetScrollXY() 
{
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) 
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} 
	else if( document.documentElement &&
		( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	
	}
	return [ scrOfX, scrOfY ];
}
//---------------------------------------------------------
function scrollIntoView(elementId)
{
	var windowSize = GetWindowSize();
	var scrollOffsets = GetScrollXY();
	
	var elementRight = AbsoluteRight(elementId);
	var elementBottom =  AbsoluteBottom(elementId);

	var offsetX = elementRight - (windowSize[0]+scrollOffsets[0]);
	var offsetY = elementBottom - (windowSize[1]+scrollOffsets[1]);
	
	var element = document.getElementById(elementId);
	
	if(offsetX > 0 && offsetY > 0)
	{
		window.scrollBy(offsetX+25,offsetY+25);
	}
	else if(offsetX > 0)
	{
		window.scrollBy(offsetX+25,0);
	}
	else if(offsetY > 0)
	{
		window.scrollBy(0,offsetY+25);
	}
}


function getFirstVisibleRowFromLowerBoundary(upperBoundaryIndex,rows)
{
	for(var i=upperBoundaryIndex;i>=0;i--)
	{
		if(rows[i].style.display !="none")
		{
			return i;
		}
	}
	throw new Error("No visible row found");
}

//---------------------------------------------------------
function DisableRowRange(start,end,rows)
{
	var panel = document.createElement('div');
	var id = "dp"+ arrayDisablePanels.length;
	
	end = getFirstVisibleRowFromLowerBoundary(end,rows);
	var length = rows.length;
	if(start >=0 && start < length && end >=0 && end < length)
	{
		var bottom = AbsoluteBottom(rows[end].id);
		var top =  AbsoluteTop(rows[start].id);
		
		panel.style.top =top +"px";
		panel.style.left = AbsoluteLeft(rows[start].id) + "px";
		
		panel.style.height = bottom-top +"px";
		panel.style.width =rows[start].offsetWidth +"px"
		panel.style.position = "absolute";
		panel.id = id;
		panel.className = classDisabledRow;
		document.body.appendChild(panel);
		
		arrayDisablePanels.push(id);
	}
}

//---------------------------------------------------------
function EnableRows()
{
	if(typeof(arrayDisablePanels) != "undefined")
	{
		var length=arrayDisablePanels.length;
		for(var i=0;i<length;i++)
		{
			document.body.removeChild(document.getElementById(arrayDisablePanels[i]));
		}
		arrayDisablePanels.length = 0;
	}
}

var defaultSelectCellCssClass ="";
var defaultDeselectCellCssClass ="";
var criteriaToSelect = {};
var criteriaToDeselect = {};
var selectedCriteria = {};

function selectCell(cell,className)
{
	if(cell)
	{
		cell.className = className;
	}
}

function deselectCell(cell,className)
{
	if(cell)
	{
		cell.className = className;
	}
}

function getEventElement(arg)
{
	var element = null;
	if(typeof(arg) != "undefined")
	{
		return arg.target;
	}
	else
	{
		return event.srcElement;
	}
}

function isCriteriumToSelect(element)
{
	return criteriaToSelect[element.id];
}

function isCriteriumToDeselect(element)
{
	return criteriaToDeselect[element.id];
}

function zwMouseOut(arg)
{
	var element = getEventElement(arg);
	if(element)
	{
		if(selectedCriteria[element.id])
		{
			return;
		}
		if(isCriteriumToDeselect(element))
		{
		    restoreCriteriumClass(element);
		}
	}
}

function zwMouseOver(arg)
{
	var element = getEventElement(arg);
	if(element)
	{
		if(selectedCriteria[element.id])
		{
			return ;
		}
		if(defaultSelectCellCssClass.length >0 && isCriteriumToSelect(element))
		{
			element.className = defaultSelectCellCssClass;
		}
	}
}
	
	

