﻿function GetBrowserSize()
{
    var width = 0, height = 0;
    if (typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        width = window.innerWidth;
        height = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }
    return [width, height];
}

function GetBrowserHeight()
{
    var height = 0;
    if (typeof(window.innerWidth) == 'number')
        //Non-IE
        height = window.innerHeight;
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        //IE 6+ in 'standards compliant mode'
        height = document.documentElement.clientHeight;
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        //IE 4 compatible
        height = document.body.clientHeight;
    return height;
}

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 GetScrollY()
{
    var scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number')
        //Netscape compliant
        scrOfY = window.pageYOffset;
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
        //DOM compliant
        scrOfY = document.body.scrollTop;
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    return scrOfY;
}

function GetPageSize()
{
    var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
    var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
    /*var yWithScroll = 0, xWithScroll = 0;
    if (window.innerHeight && window.scrollMaxY)
    {
        // Firefox
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
        // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    }
    else
    {
        // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        pageWidth = document.body.offsetWidth + document.body.offsetLeft;
        pageHeight = document.body.offsetHeight + document.body.offsetTop;
    }
    return [xWithScroll, yWithScroll];*/
    //alert( 'The height is ' + _docHeight + ' and the width is ' + _docWidth );
    return [_docHeight, _docWidth];
} 

function GetPageHeight()
{
    return (document.height !== undefined) ? document.height : document.body.offsetHeight;
}

function GetPageWidth()
{
    // this part is NOT reusable
    // return with of container div, not page
    //return 790;
    return (document.width !== undefined) ? document.width : document.body.offsetWidth;
}

function GetElementHeight(obj)
{
	if (document.layers) return obj.clip.height;
	else return obj.offsetHeight;
}

function GetElementWidth(obj)
{
	if (document.layers) return obj.clip.width;
	else return obj.offsetWidth;
}

function GetElementSize(obj)
{
    var _objHeight = (document.layers) ? obj.height : obj.offsetHeight;
    var _objWidth = (document.layers) ? obj.width : obj.offsetWidth;
    return [_objWidth, _objHeight];
}

function GetObjectPosition(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent)
		{
		    //if (obj.id != 'container')
		    //{
			    curleft += obj.offsetLeft;
			    curtop += obj.offsetTop;
            //}
		}
	}
	return [curleft, curtop];
}

function ShowStaticHint(obj, message)
{
    var hint = document.getElementById('HintDiv');
    var text = document.getElementById('HintDivText');
    if (hint)
    {
        if (hint.style.display == 'block') return;
        text.innerHTML = message;
        var position = GetObjectPosition(obj);
        var scrolls = GetScrollXY();
        var browserSize = GetBrowserSize();
        hint.style.left = position[0] + 'px';
        if (position[1] - scrolls[1] < browserSize[1] / 2)
        {
            // change styles
            document.getElementById('Arrow_bl').className = 'hide';
            document.getElementById('Arrow_br').className = 'hide';            
            
            // position div lower than object
            hint.style.bottom = '';
            hint.style.top = position[1] - 10 + GetElementHeight(obj) + 'px';
            if (position[0] - scrolls[0] < browserSize[0] / 2)
            {
                // Top Left
                document.getElementById('Arrow_tr').className = 'hide';
                document.getElementById('Arrow_tl').className = 'show';
                hint.style.right = '';
                hint.style.left = position[0] + Math.round(GetElementWidth(obj) / 2) - 41 + 'px';
            }
            else
            {
                // Top Right
                document.getElementById('Arrow_tl').className = 'hide';
                document.getElementById('Arrow_tr').className = 'show';
                hint.style.left = '';
                hint.style.right = GetPageWidth() - Math.round(GetElementWidth(obj) / 2) - 41 - position[0] + 'px';
            }
        }
        else
        {
            // change styles
            document.getElementById('Arrow_tl').className = 'hide';
            document.getElementById('Arrow_tr').className = 'hide';
            // position div higher than object
            hint.style.top = '';
            hint.style.bottom = GetPageHeight() + 0 - position[1] + 0 + 'px';
            
            if (position[0] - scrolls[0] < browserSize[0] / 2)
            {
                // Bottom Left
                document.getElementById('Arrow_br').className = 'hide';
                document.getElementById('Arrow_bl').className = 'show';
                hint.style.right = '';
                hint.style.left = position[0] + Math.round(GetElementWidth(obj) / 2) - 41 + 'px';
            }
            else
            {
                // Bottom Right
                document.getElementById('Arrow_bl').className = 'hide';
                document.getElementById('Arrow_br').className = 'show';
                hint.style.left = '';
                hint.style.right = GetPageWidth() - Math.round(GetElementWidth(obj) / 2) - 41 - position[0] + 'px';
            }
        }
        hint.style.display = 'block';
    }
    else alert('hint div not found');
}

function ShowDynamicHint(obj, params)
{
    var hint = document.getElementById('HintDiv');
    var text = document.getElementById('HintDivText');
    if (hint)
    {
        if (hint.style.display == 'block') return;
        text.innerHTML = 'Please wait ...';
        var position = GetObjectPosition(obj);
        var scrolls = GetScrollXY();
        var browserSize = GetBrowserSize();
        hint.style.left = position[0] + 'px';
        if (position[1] - scrolls[1] < browserSize[1] / 2)
        {
            // change styles
            //document.getElementById('Hint_top').className = 'arrow-tl';
            document.getElementById('Hint_btm').className = 'Hint_btm';
            // position div lower than object
            hint.style.bottom = '';
            hint.style.top = position[1] + 5 + GetElementHeight(obj) + 'px';
            if (position[0] - scrolls[0] < browserSize[0] / 2)
            {
                document.getElementById('Hint_top').className = 'arrow-tl';
                hint.style.right = '';
                hint.style.left = position[0] + 0 + 'px';
            }
            else
            {
                document.getElementById('Hint_top').className = 'arrow-tr';
                hint.style.left = '';
                hint.style.right = GetPageWidth() - 15 - position[0] + 'px';
            }
        }
        else
        {
            // change styles
            document.getElementById('Hint_top').className = 'Hint_top';
            //document.getElementById('Hint_btm').className = 'arrow-bl';
            // position div higher than object
            hint.style.top = '';
            hint.style.bottom = GetPageHeight() - 5 - position[1] - 5 + 'px';
            hint.style.left = position[0] + 0 + 'px';
            if (position[0] - scrolls[0] < browserSize[0] / 2)
            {
                document.getElementById('Hint_btm').className = 'arrow-bl';
                hint.style.right = '';
                hint.style.left = position[0] + 0 + 'px';
            }
            else
            {
                document.getElementById('Hint_btm').className = 'arrow-br';
                hint.style.left = '';
                hint.style.right = GetPageWidth() - 15 - position[0] + 'px';
            }
        }
        hint.style.display = 'block';
        PageMethods.GetHint(params, HintCallbackSucceeded, HintCallbackFailed);
    }
    else alert('hint div not found');
}

function HintCallbackSucceeded(result)
{
    // display the result
    var hint = document.getElementById('HintDivText');
    hint.innerHTML = result;
}

function HintCallbackFailed(error)
{
    var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();
    // display the error
    var hint = document.getElementById('HintDivText');
    hint.innerHTML = 
        //"Stack Trace: " +  stackTrace + "<br/>" +
        "Service Error: " + message + "<br/>" +
        "Status Code: " + statusCode + "<br/>" +
        "Exception Type: " + exceptionType + "<br/>" +
        "Timedout: " + timedout;
}

function HideHint()
{
    var hint = document.getElementById('HintDiv');
    if (hint)
    {
        hint.style.display = 'none';
        hint.innerHtml = '';
    }
    else alert('hint div not found');
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
