﻿var gridScrollElimination = 18;

function showConfirmation(text){
    return showConfirmation(text, '');
}
function showConfirmation(text, action){    
        
    
    var el = $('<a href="" class="gridMenuButtonOn" onclick="jQuery.facebox.close();"><span>TAK</span></a>').attr('href', action);    
    if(!(typeof action == 'string'))
    {
        el = $('<a href=""class="gridMenuButtonOn" onclick="jQuery.facebox.close();"><span>TAK</span></a>').attr('href', $(action).attr('href'));        
    }
    var elFalse = $('<a href="" onclick="return jQuery.facebox.close();" class="gridMenuButtonOn"><span>NIE</span></a>');
    var block = $('<div></div>');
    $('<div class="textBlock questionBlock">'+text+'</div>').appendTo(block);
    var buttons = $('<div class="buttons"></div>');
    el.appendTo(buttons);    
    elFalse.appendTo(buttons);
    buttons.appendTo(block);
    jQuery.facebox(block.html());
    return false;
}


//function showAlert(text){
//    var block = $('<div></div>');
//    var buttons = $('<div class="buttons"></div>');
//    $('<a href="" onclick="return jQuery.facebox.close();" class="gridMenuButtonOn"><span>OK</span></a>').appendTo(buttons);
//    $('<div class="textBlock">'+text+'</div>').appendTo(block);
//    buttons.appendTo(block);
//    jQuery.facebox(block.html());
//    return false;
//}

function showAlert(text, action){
    var block = $('<div></div>');
    var buttons = $('<div class="buttons"></div>');
    var el = $('<a href="" onclick="return jQuery.facebox.close();" class="gridMenuButtonOn"><span>OK</span></a>');
    if(action != null && typeof action != undefined)
    {
        el = $('<a href="" class="gridMenuButtonOn" onclick="jQuery.facebox.close();"><span>OK</span></a>').attr('href', action);    
        if(!(typeof action == 'string'))
        {
            el = $('<a href=""class="gridMenuButtonOn" onclick="jQuery.facebox.close();"><span>OK</span></a>').attr('href', $(action).attr('href'));        
        }
    }
    el.appendTo(buttons);
    $('<div class="textBlock">'+text+'</div>').appendTo(block);
    buttons.appendTo(block);
    jQuery.facebox(block.html());
    return false;
}

var ACRFont = "_files/acr_theserif_plain_pl.swf";
function sifrHeaders() {
    if(typeof sIFR == "function"){
        var sectionHeader = named({sFlashSrc: ACRFont, sColor: "#ffffff", sWmode: 'transparent'})
        sIFR.replaceElement("#mainContainer #pageContent .pageTitleBar .pageTitle", sectionHeader); 
        var stepCount = named({sFlashSrc: ACRFont, sColor: "#ffffff", sWmode: 'transparent'})
        sIFR.replaceElement("#mainContainer #pageContent .pageTitleBar .stepCount", stepCount); 
    }
}

// wyrownuje wysokosci DIV-ow bedacych dzieckiem elementu o ID colsParentID
function vJustifyCols(colsParentID){
    
    var colsParent = document.getElementById(colsParentID);
    if (colsParent == null) {
        return;
    }
    
    var childrenCount = colsParent.childNodes.length;
    if (childrenCount > 0) {
        var colsToJustify = new Array();
        var child;
        for(i=0; i<childrenCount; i++) {
            child = colsParent.childNodes[i];
            if (child.tagName == 'DIV') {
                colsToJustify.push(child.id);
            }
        }
    }
    var maxHeight = 0;
    for (i=0; i<colsToJustify.length; i++) {
        if (document.getElementById(colsToJustify[i]).offsetHeight > maxHeight){
            maxHeight = document.getElementById(colsToJustify[i]).offsetHeight;
        }
    }
    
    if (maxHeight > 0) {
        for(i=0; i<colsToJustify.length; i++) {
            document.getElementById(colsToJustify[i]).style.height = maxHeight + "px";
        }
    }   
}

//dodaje wywolanie funkcji func do loada strony
function addLoadEvent(func) {
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') { 
        window.onload = func; 
    } 
    else { 
        window.onload = function() { 
            if (oldonload) { 
                oldonload(); 
            } 
            func(); 
        } 
    } 
}

function vJustifyMainPageCols() {
    vJustifyCols('mainPageLayout');
}

function repairDefaultButton(button)
{
    var b;
    if(typeof(button) == 'string')
        b = $("#"+button)[0];
    else
        b = $(button)[0];
    if (b && typeof(b.click) == 'undefined') {
        b.click = function() { 
            var result = true;
            if (b.onclick) result = b.onclick();
            if (typeof(result) == 'undefined' || result) {
                eval(b.getAttribute('href'));
            }
        }
    }
}

function DisableButtonOnReportsPage(listBox, buttonSearch, buttonClear)
{
    if(typeof(buttonSearch) == 'string')
        buttonSearch = $("#"+buttonSearch)
    if(typeof(buttonClear) == 'string')
        buttonClear = $("#"+buttonClear)
    if(typeof(listBox) == 'string')
        listBox = $("#"+listBox)

    if  ($(listBox).find("option").length > 1)
    {
        $(buttonSearch).unbind("click").removeClass('gridMenuButtonOff').addClass('gridMenuButtonOn');
        $(buttonClear).unbind("click").removeClass('gridMenuButtonOff').addClass('gridMenuButtonOn');
    }
    else
    {
        $(buttonSearch).unbind("click").attr("href","javascript:void(0);").removeClass('gridMenuButtonOn').addClass('gridMenuButtonOff');
        $(buttonClear).unbind("click").attr("href","javascript:void(0);").removeClass('gridMenuButtonOn').addClass('gridMenuButtonOff');
    }
}

function DisableButtonOnReportsPageWithTwoListBoxes(listBox1 ,listBox2, buttonSearch,buttonClear)
{
    if(typeof(buttonSearch) == 'string')
        buttonSearch = $("#"+buttonSearch)
    if(typeof(buttonClear) == 'string')
        buttonClear = $("#"+buttonClear)
    if(typeof(listBox1) == 'string')
        listBox1 = $("#"+listBox1)
    if(typeof(listBox2) == 'string')
        listBox2 = $("#"+listBox2)
        
    if  (($(listBox1).find("option").length > 1) && ($(listBox2).find("option").length > 1))
    {
        $(buttonSearch).unbind("click").removeClass('gridMenuButtonOff').addClass('gridMenuButtonOn');
        $(buttonClear).unbind("click").removeClass('gridMenuButtonOff').addClass('gridMenuButtonOn');
    }
    else
    {
        $(buttonSearch).unbind("click").attr("href","javascript:void(0);").removeClass('gridMenuButtonOn').addClass('gridMenuButtonOff');
        $(buttonClear).unbind("click").attr("href","javascript:void(0);").removeClass('gridMenuButtonOn').addClass('gridMenuButtonOff');
    }
}

function makeRowsOneClickable(tableID, setRightsFunction)
{
    var tableBody = $("#" + tableID + " tbody");
    var lastRadio, lastChecked, isOneClicked = false, napis = '';
    
    $(":radio", tableBody).mousedown(function()
    {
        napis = "1";
        lastRadio = this;
        lastChecked = this.checked;
    }).mouseup(function()
    {
        isOneClicked = lastRadio == this;
        napis += "3";
    }).keydown(function(e)
    {
        if(e.keyCode == 32)//spacebar
        {
            napis = "a";
            lastRadio = this;
            lastChecked = this.checked;
        }
    }).keyup(function(e, args)
    {
        if(e.keyCode == 32)
        {
            isOneClicked = lastRadio == this;
            napis += "c";
        }
    });
    $(document).mousedown(function()
    {
        napis += "2";
    })
    .mouseup(function()
    {
        isOneClicked &= (napis == "123");
        napis = '';
    }).keydown(function(e)
    {
        if(e.keyCode == 32)
            napis += "b";
    }).keyup(function(e)
    {
        if(e.keyCode == 32)
        {
            isOneClicked &= (napis == "abc");
            napis = '';
        }
    });
    
    $("tr", tableBody).each(function(i, tr)
    {
        $(tr).click(function()
        {
            var selected;
            if(isOneClicked)
                selected = !lastChecked;
            else
                selected = !$(":radio", tr).get(0).checked;
            $(":radio", tr).get(0).checked = selected;
            
            $("tr", tableBody).removeClass("trSelected");
            if(selected)
                $(tr).addClass("trSelected");
            setRightsFunction(tr, selected);
        });
    });
}

function TwoLetters(s)
{
    if(s<=9) return "0"+s;
    return s;
}

function changeDdlDate(){        
    txtDateFrom.attr('readonly','readolny');
    txtDateTo.attr('readonly','readolny');                
    today = new Date();            
    switch(ddlDate.val()){
        case "SelectDateRange":            
            txtDateFrom.removeAttr('readonly');
            txtDateTo.removeAttr('readonly');            
            break;
        case "YesterdayDateRange":                    
            yesterday = new Date();yesterday.setDate(today.getDate() - 1);                                       
            txtDateFrom.val(yesterday.getFullYear()+ "-" + TwoLetters(yesterday.getMonth()+1)+ "-" + TwoLetters(yesterday.getDate()) );
            txtDateTo.val( yesterday.getFullYear()+ "-" + TwoLetters(yesterday.getMonth()+1)+ "-" + TwoLetters(yesterday.getDate()) );
            break;
        case "LestSevenDaysDateDateRange":
            start = new Date();start.setDate(today.getDate() - 7);                
            yesterday = new Date();yesterday.setDate(today.getDate() - 1);                
            txtDateFrom.val(start.getFullYear()+ "-" + TwoLetters(start.getMonth()+1)+ "-" + TwoLetters(start.getDate()) );
            txtDateTo.val( yesterday.getFullYear()+ "-" + TwoLetters(yesterday.getMonth()+1)+ "-" + TwoLetters(yesterday.getDate()) );
            break;
        case "LastWeekDaysDateRange":            
            mon = new Date(); mon.setDate(today.getDate() - today.getDay()-6);
            sun = new Date(); sun.setDate(today.getDate() - today.getDay());
            txtDateFrom.val(mon.getFullYear()+ "-" + TwoLetters(mon.getMonth()+1)+ "-" + TwoLetters(mon.getDate()) );
            txtDateTo.val( sun.getFullYear()+ "-" + TwoLetters(sun.getMonth()+1)+ "-" + TwoLetters(sun.getDate()) );
            break;
        case "LastWeekWorkDaysDateDateRange":
            mon = new Date(); mon.setDate(today.getDate() - today.getDay()-6);
            fri = new Date(); fri.setDate(today.getDate() - today.getDay()-2);
            txtDateFrom.val(mon.getFullYear()+ "-" + TwoLetters(mon.getMonth()+1)+ "-" + TwoLetters(mon.getDate()) );
            txtDateTo.val( fri.getFullYear()+ "-" + TwoLetters(fri.getMonth()+1)+ "-" + TwoLetters(fri.getDate()) );
            break;
        case "ThisMonthDateRange":            
            firstDate = new Date(new Date().setDate( 1 ));
            yesterday = new Date();yesterday.setDate(today.getDate() - 1);
            txtDateFrom.val(firstDate.getFullYear()+ "-" + TwoLetters(firstDate.getMonth()+1)+ "-" + TwoLetters(firstDate.getDate()) );
            txtDateTo.val( yesterday.getFullYear()+ "-" + TwoLetters(yesterday.getMonth()+1)+ "-" + TwoLetters(yesterday.getDate()) );
            break;
        case "PreviousMonthDateRange":                   
            lastDay = new Date( today.setDate(0) );                
            firstDay = new Date(new Date( today.setDate(1) ).setDate( 1 ));
            txtDateFrom.val(firstDay.getFullYear()+ "-" + TwoLetters(firstDay.getMonth()+1)+ "-" + TwoLetters(firstDay.getDate()) );
            txtDateTo.val( lastDay.getFullYear()+ "-" + TwoLetters(lastDay.getMonth()+1)+ "-" + TwoLetters(lastDay.getDate()) );
            break;                               
        }
}
    
function ReportsDatesSet(ddlDateId, txtDateFromId, txtDateToId){    
    ddlDate = $("#"+ddlDateId);
    txtDateFrom = $("#"+txtDateFromId);
    txtDateTo = $("#"+txtDateToId);    
    changeDdlDate();    
    ddlDate.change(changeDdlDate);    
}
