



function body_onload()
{   
    var body_left = document.getElementById('body_left');
    var body_right = $('#body_right');
    var body_left_box_space = document.getElementById('body_left_box_space');

    if (!body_left || !body_left_box_space)
        return false;

    if (body_right.height() - body_left.clientHeight > 0)           //CAUTION: in IE, clientHeight sometimes doesn't work for some reason
    {
        body_left_box_space.style.paddingBottom = body_right.height() - body_left.clientHeight + "px";
    }
    
    if (document.getElementById('reportTableGA') != undefined)
    {
        show_first_report()
    } 
    
     if (document.getElementById('reportTableBR') != undefined)
    {
        show_first_report()
    }
    
    var httpRequest = makeRequest ();    
    httpRequest.open ('GET', '/ImageHelper.aspx', true);
    httpRequest.send(null);

    $(window).resize(function () {
        setMainMenuSpace();
    });

    leftSpace();
}

function pressEnter (e, valueStr)
{
	var keynum;
	var keychar;
	var numcheck;
						
	if(window.event) // IE
	    keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
	    keynum = e.which;
	
	//keychar = String.fromCharCode(keynum);
	//alert(keynum);
	
	if (keynum == 13)
	{
	    var f = document.getElementById('aspnetForm');
	    var x = document.getElementById('ctl00$OnEnter');
	    x.value = valueStr;
	    f.submit();
	    event.returnValue = false;
	    return false;
	}
}

function submitOnEnter(e)
{
    pressEnter(e,"search")
}

function loginOnEnter(e)
{
    pressEnter(e,"login")
}


function makeRequest (url)
{
    var httpRequest;

    if (window.XMLHttpRequest) // Mozilla, Safari, .
    {
        httpRequest = new XMLHttpRequest ();
        
        if (httpRequest.overrideMimeType)
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) // IE
    { 
        try
        {
            httpRequest = new ActiveXObject ("Msxml2.XMLHTTP");
        } 
        catch (e)
        {
            try
            {
                httpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert (e);
            }
        }
    }

    if (!httpRequest)
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
    }
    
    return httpRequest;
 }

function alertContents (httpRequest)
{
    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
            alert (httpRequest.responseText);
        }
        else
        {
            alert ('There was a problem with the request.');
        }
    }
}

function sendEmail (tbMessageText, tbEmail)
{
    ebody = $get(tbMessageText).value;
    esender= $get(tbEmail).value;

    var httpRequest = makeRequest ();
    httpRequest.onreadystatechange = function () { sendEmail_ReadyStateChange (httpRequest); };
    httpRequest.open ('POST', '/SendEmail.aspx', true);
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.send ("tbMessageText=" +ebody + "&tbEmail=" + esender);  
}

function sendEmail_ReadyStateChange (httpRequest)
{      
    var label = document.getElementById('lMessageSendError');
    label.style.display = "block"

    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
            label.innerHTML = httpRequest.responseText;
        }
        else
        {
            alert ('There was a problem with the request.');
        }
    }
        
}


function showRelationChartsPricePopup (report, companyId, rerpotName,uniquier)
{
    getPriceAndshowReportPricePopup 
    ( 
        report,
        companyId, 
        function (price) { ShowRealtionChartsPopup (rerpotName + " " + price,uniquier); } 
    );
}


function resolveRelationChartsAndShowPricePopup(report, companyId, rerpotName, currency, basicUrl, silverUrl)
{
    //If user has Silverlight 4 (or higher) installed, show SL version of Business Charts
    var hasSL = Silverlight.isInstalled("4.0");         //'isInstalled' defined in Silverlight.js
    if (document.documentElement && document.documentElement.clientHeight) 
    {
        silverUrl = silverUrl + "&height=" + document.documentElement.clientHeight;    
    }
    
    var url = (hasSL ? silverUrl : basicUrl);

    getPriceAndshowReportPricePopup (
        report,
        companyId,
        function (price) { ShowPopupDialog1(rerpotName + " " + price + " " + currency, url); }
    );
}

function showReportPricePopup (report, companyId, rerpotName, currency, url)
{
    shiftSilverCharts();
    
    getPriceAndshowReportPricePopup 
    ( 
        report,
        companyId, 
        function (price) { ShowPopupDialog1 (rerpotName  + " " + price + " " + currency, url); } 
    );
}


function showReportPricePopupForNewWindow(report, companyId, rerpotName, currency, url, target) {
    getPriceAndshowReportPricePopup
    (
        report,
        companyId,
        function (price) { ShowPopupDialog1ForNewWindow(rerpotName + " " + price + " " + currency, url, target); }
    );
}


function showReportPricePopupWithDate (report, companyId, rerpotName, currency, url)
{
    shiftSilverCharts();
    
    getPriceAndshowReportPricePopup 
    ( 
        report,
        companyId,        
        function (price) { ShowPopupDialog1WithChildDateRangePopup (rerpotName + " " +price + " " + currency, url,companyId); } 

    );
}


//In case SilverCharts are shown, shift them to make space for pop-up
function shiftSilverCharts() {
    $('#frameBox').width("1px");
}

//Fix SilverCharts padding if they're shown. Used on pop-ups dispose
function fixSilverCharts() {
    $("#frameBox").width("100%");
}

function updateAccountFromSilverChart() {
    var httpRequest = makeRequest();
    httpRequest.onreadystatechange = function () { updateAccountFromSilverChart_ReadyStateChange(httpRequest); };
    var uniquier = new Date(); //If url parameters are not changed no request to server
    var url = '/Accounting.aspx?updateAccountInfo=true&uniquier=' + uniquier.getTime();
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}

function updateAccountFromSilverChart_ReadyStateChange(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            document.getElementById('aAmountValueEuroAfter1').innerHTML = getElementValue(httpRequest.responseText, '<AmountEuroAfter>', '</AmountEuroAfter>');         
        }
    }
};

function getElementValue(text, startElement, endElement) {
    return text.substring(text.indexOf(startElement) + startElement.length, text.indexOf(endElement));
}

function getPriceAndshowReportPricePopup (report, companyId, callback)
{
    var httpRequest = makeRequest ();
    httpRequest.onreadystatechange = function () { getReportPrice_ReadyStateChange (httpRequest, callback); };
    var uniquier = new Date();
    var url = '/Accounting.aspx?report=' + report + '&companyId=' + companyId + '&uniquier=' + uniquier.getTime();
    httpRequest.open ('GET', url, true);
    httpRequest.send (null);
}

function getReportPrice_ReadyStateChange (httpRequest, callback )
{
    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
            if (httpRequest.responseText.indexOf('<Error>') != -1)
                ShowErrorPopup(httpRequest.responseText.replace('<Error>', '').replace('</Error>', ''));
            else
                callback(httpRequest.responseText.replace('<price>', '').replace('</price>', ''));
        }
        else
        {
            alert ("Failed to retrieve report price.");
        }
    }
}

function getCapitalMarketDate (httpRequest, callback )
{      
    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
            var dateArray= new String();
            dateArray  = httpRequest.responseText.split('<TradeDateSeparator>', 2)
        
            callback(dateArray[0],dateArray[1]);
        }
        else
        {
            alert ("Failed to retrieve report price.");
        }
    }
}


function getCapitalMarketPeriodForPopUp (companyId, callback)
{
    var httpRequest = makeRequest ();
    httpRequest.onreadystatechange = function () { getCapitalMarketDate (httpRequest, callback); };
    var uniquier = new Date()
    var url = '/CapitalMarketPeriodForPopUp.aspx?subjectId=' + companyId;
    httpRequest.open ('GET', url, true);
    httpRequest.send (null);
}

function getPrice(currency, price,callback)
{
    var httpRequest = makeRequest ();
    httpRequest.onreadystatechange = function () 
    { 
        if (httpRequest.readyState == 4)
        {
            if (httpRequest.status == 200)
            {
               var text = httpRequest.responseText;
               text = text.substring(text.indexOf("<price>")+7,text.indexOf("</price>"))
               callback(text);
            }
            else
            {
                alert ("Failed to retrieve report price.");
            }
        } 
    };
    var uniquier = new Date()
    var url = '/FormaterService.aspx?currency=' + currency + '&price=' + price + '&uniquier=' + uniquier.getTime()
    httpRequest.open ('GET', url, true);
    httpRequest.send (null);
}

function getPriceSKK(price,callback)
{
    return getPrice("SKK",price,callback);
}

function getPriceEuro(price,callback)
{
    return getPrice("EMU",price,callback);
}

function disableLabelUserExpiredWarning()
{ 
    var elementSpan=document.getElementById("ctl00_ContentPlaceHolder1_UserExpiredWarning");
    if (elementSpan!=null)
        elementSpan.style.display = "none";
        
    elementSpan=document.getElementById("ctl00_ContentPlaceHolder1_lblError");
    if (elementSpan!=null)
        elementSpan.style.display = "none";
}

function setMainMenuSpace() {
    var body_width;
    var min_width;

    if ($('#body_left').is(":visible"))
        min_width = 800;
    else
        min_width = 642;

    if ($('#chbTopMenuHeader').is(":visible")) {
        $('#ctl00_main_menu').css('width', '');
        body_width = $('#chbTopMenuHeader').width();
    }
    else {
        if ($(window).width() < min_width) {
            $('#ctl00_main_menu').css('width', min_width + 'px');
            body_width = min_width;
        }
        else {
            $('#ctl00_main_menu').css('width', '');
            body_width = $('#ctl00_main_menu').width();
        }
    }

    $('#ctl00_main_body_table').css('width', body_width + 'px');
    
}

