﻿function DropDownList_ValidateGuidSelection(source, arguments) {
    var ddl = document.getElementById(source.controltovalidate);
    if (ddl != null) {
        var value = ddl[ddl.selectedIndex].value;
        arguments.IsValid = (value != '00000000-0000-0000-0000-000000000000');
    }
    else {
        arguments.IsValid = false;
    }
}

function DropDownList_ValidatePositiveSelection(source, arguments) {
    var ddl = document.getElementById(source.controltovalidate);
    if (ddl != null) {
        var value = ddl[ddl.selectedIndex].value;
        arguments.IsValid = (value > 0);
    }
    else {
        arguments.IsValid = false;
    }
}

var animation;
var fadeOutTimeout = 0;

/**funkcje dla pokazywania/ ukrywania plików pomocy **/
var ifHelpShow = false;
function ShowHelpDiv(isShow) {
    ifHelpShow = isShow;
    if (isShow) {
        var divelem = document.getElementById('divManuals');
        var butelem = butelem = document.getElementById('ashp');
        divelem.style.top = (getPixelTop(butelem, 'body') + 15) + 'px';
        divelem.style.display = 'block';
    }
    else {
        setTimeout(function () { if (ifHelpShow == false) document.getElementById('divManuals').style.display = 'none'; }, 1000);
    }
}
/**funkcje dla pokazywania/ ukrywania plików pomocy **/

//dir - FadeIn, FadeOut
function FadeElement(elementId, dir) {
    if (animation && animation.get_effect() == dir) return;
    var obj = document.getElementById(elementId);
    if (obj) {
        var tmpAnimation = new Sys.Extended.UI.Animation.FadeAnimation();
        tmpAnimation.set_target(obj);
        tmpAnimation.set_effect(dir);
        tmpAnimation.set_duration(0.5);
        tmpAnimation.set_fps(25);

        if (dir == "FadeIn") {
            tmpAnimation.set_effect(Sys.Extended.UI.Animation.FadeEffect.FadeIn);
            tmpAnimation.set_minimumOpacity($common.getElementOpacity(obj));
            if (fadeOutTimeout) window.clearTimeout(fadeOutTimeout);
            tmpAnimation.play();
            if (animation) animation.stop();
            animation = tmpAnimation;
        }
        else {
            tmpAnimation.set_effect(Sys.Extended.UI.Animation.FadeEffect.FadeOut);
            tmpAnimation.set_maximumOpacity($common.getElementOpacity(obj));
            fadeOutTimeout = window.setTimeout(function () {
                tmpAnimation.play();
                if (animation) animation.stop();
                animation = tmpAnimation;
            }, 500);
        }
    }
}

var mailingTable;
var btnRefreshId;

function ValidatePlayOff(tbx, minValue, maxValue) {
    try {
        var number = parseInt(tbx.value);
        if (number < minValue || number > maxValue) {
            alert('Wartość ' + tbx.value + ' jest poza dopuszczalnym zakresem ' + minValue + ' - ' + maxValue);
            tbx.value = '';
        }
    }
    catch (err) {
    }
}

function copyToClipboard(s) {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("Text", s);
    }
}


//zamyka okno, w którym ta funkcja jest wykonana
//Marcin Teodorczyk
function CloseParentWindow(isDataChanged) {
    var rw = GetRadWindow();
    if (rw) {
        rw.Argument = isDataChanged;
        rw.close();
    }
    else {
        window.opener = 'x';
        window.close();
    }
    return false;
}
//zwraca obiekt typu RadWindow
//jeżeli brak to zwraca null
//Marcin Teodorczyk
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else {
        if (window.frameElement) {
            if (window.frameElement.radWindow)
                oWindow = window.frameElement.radWindow;
        }
    }
    return oWindow;
}

//Funkcja zostanie wykonana jeżeli okno PopUp zostanie zamknięte
function OnClientClose(radWindow) {
    if (radWindow.Argument == true) {
        if (mailingTable) {
            for (var i = 0; i < mailingTable.length; i++) {
                var cbx = document.getElementById(mailingTable[i]);
                if (cbx)
                    cbx.checked = false;
            }
        }
        var btn = document.getElementById(btnRefreshId);
        if (btn)
            btn.click();
    }
    return false;
}

//Metody do obsługi Cookie
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000 + 4));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

//Metody do obsługi ...
function CalculateScores(value, txbIIid, txbIIIid, txbIVid, txbSid) {

    var txbII = document.getElementById(txbIIid);
    var txbIII = document.getElementById(txbIIIid);
    var txbIV = document.getElementById(txbIVid);
    var txbS = document.getElementById(txbSid);

    txbII.value = Math.round(value * 0.6) == 0 ? 1 : Math.round(value * 0.6);
    txbIII.value = Math.round(value * 0.4) == 0 ? 1 : Math.round(value * 0.4);
    txbIV.value = Math.round(value * 0.2) == 0 ? 1 : Math.round(value * 0.2);
    txbS.value = Math.round(value * 1.5) == 0 ? 1 : Math.round(value * 1.5);



}
function OnKeyDown(evt) {
    var e = evt ? evt : window.event;
    if (e.keyCode == 13) {
        var myind = e.srcElement.tabIndex;
        var ctrl = getControlByTabIndex(e.srcElement.form, myind + 1)
        if (ctrl) {
            if (ctrl.type == "text")
                ctrl.select();
            else
                ctrl.focus();
        }
        return false;
    }
    return true;
}

function getControlByTabIndex(form, index) {
    try {
        var i = form.length - 1;
        for (a = 0; a < i; a++) {
            var d = form.elements[a].tabIndex;
            if (d == parseInt(index))
                return form.elements[a];
        }
        return null;
    }
    catch (err)
    { }
    return null;
}
function CheckAll(cbxchecked, sourceGridClientId, controlId) {
    var sourceGrid = document.getElementById(sourceGridClientId);
    if (sourceGrid == null)
        return;
    var sourceGridTrs = sourceGrid.getElementsByTagName("tr");
    for (var i = 1; i < sourceGridTrs.length; i++) {
        controlsCollection = sourceGridTrs[i].getElementsByTagName("input");
        var cbx = getControl(controlsCollection, controlId);
        if (cbx != null)
            cbx.checked = cbxchecked
    }
}

function getControl(controlsCollection, name) {
    for (var i = 0; i < controlsCollection.length; i++) {
        var id = controlsCollection[i].getAttribute("id");
        if (id.indexOf(name) != -1) {
            return controlsCollection[i];
        }
    }
    return null;
}

//Funkcja wywołująca event przycisku po naciśnięciu klawisza ENTER
//Marcin Teodorczyk
function clickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (evt.keyCode == 13) {
            bt.click();
            return false;
        }
    }
}

//Funkcja wywołująca event linkButtona
//Krzysztof Koziarski
function clickLnkButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        bt.click();
        return false;
    }
}
//Function to show and hide div , by stering from checkbox
//Kornel Misiejuk
function ChangeView(cb, divel) {
    if (document.getElementById(cb).checked) {
        document.getElementById(divel).style.display = 'block';
    } else {
        document.getElementById(divel).style.display = 'none';
    }
}

//funkcja sluzy do ukrywani lub pokazywania div'a zawierajacego podreczne menu
//divid - id diva ktory ma zostac pokazany
//button - przycisk ktory wywołał pokazanie div'a
//hiddenInGridId - id pola ukrytego zawierajacego id osoby
//hiddenId - id pola ukrytego do ktorego zostanie przekopiowany identyfikator osoby
//Kornel Misiejuk
function ShowHideNewMenu(divid, button, hiddenInGridId, hiddenId) {
    var butelem = document.getElementById(button);
    var divelem = document.getElementById(divid);
    var hiddenInGridId = document.getElementById(hiddenInGridId);
    var hiddenId = document.getElementById(hiddenId);

    if (divelem.style.display == 'none') {
        HideDivsFromTable();
        RecreteColors();
        divelem.style.display = 'block';
        hiddenId.value = hiddenInGridId.value;
        divelem.style.left = getPixelLeft(butelem, 'body') + 'px';
        divelem.style.top = (getPixelTop(butelem, 'body') + 20) + 'px';

        ChangeColorTR(butelem);
    } else {
        divelem.style.display = 'none';
        RecreteColors();
    }
}

//funkcja sluzy do ukrywani lub pokazywania div'a zawierajacego podreczne menu
//divid - id diva ktory ma zostac pokazany
//button - przycisk ktory wywołał pokazanie div'a
function ShowHide(divid, button) {
    var butelem;
    if (button.tagName == null)
        butelem = document.getElementById(button);
    else
        butelem = button;
    var divelem = document.getElementById(divid);
    if (divelem.style.display == 'none') {
        HideDivsFromTable();
        RecreteColors();
        divelem.style.display = 'block';
        divelem.style.left = getPixelLeft(butelem, 'body') + 'px';
        divelem.style.top = (getPixelTop(butelem, 'body') + 20) + 'px';
        ChangeColorTR(divelem);
    } else {
        divelem.style.display = 'none';
        RecreteColors();
    }
}

//function to hide all divs , that id starts with 'grid'
//Kornel Misiejuk
function HideDivsFromTable() {
    var cells = document.getElementsByTagName("div");
    for (var i = 0; i < cells.length; i++) {
        if (cells[i]['className'] == "gridopt") {
            cells[i].style.display = 'none';
        }
    }
}


//function to get position - left on screen of control elem to tagname element parent
function getPixelLeft(elem, tagName) {
    try {
        var left = 0;
        while (elem.tagName.toLowerCase() != tagName.toLowerCase()) {
            left += elem.offsetLeft; //+ elem.clientLeft;
            elem = elem.offsetParent;
        }
        return left;
    }
    catch (err) {
        err.description += '>getPixelLeft';
        throw err;
    }
}

//function to get position - top on screen of control elem to tagname element parent
function getPixelTop(elem, tagName) {
    if (elem == null)
        return -1;
    try {
        var top = 0;
        while (elem.tagName.toLowerCase() != tagName.toLowerCase()) {
            top += elem.offsetTop; //+ elem.clientTop;
            elem = elem.offsetParent;
        }
        return top;
    }
    catch (err) {
        err.description += '>getPixelTop';
        throw err;
    }
}

function ChangeColorTR(elem) {
    var temp;
    temp = elem;

    while (temp.tagName != "TR") {
        temp = temp.parentNode;
        if (temp == null)
            break;
    }

    //potrzeba tylko aby w każdym masterpage było menu
    AddClass__CssFriendlyAdapters(temp, "selectedRow");
}

function RecreteColors() {
    var trs = document.getElementsByTagName("tr")
    for (var i = 0; i < trs.length; i++) {
        RemoveClass__CssFriendlyAdapters(trs[i], "selectedRow");
    }

}

//Pokazac divid w zaleznosci czy na dropdown 
//list 'select' jest wybrana wartosc wart  
function ShowOnSelect(selectid, divid, wart) {
    div = document.getElementById(divid);
    if (document.getElementById(selectid).value == wart) {
        div.style.display = "block";
    }
    else {
        div.style.display = "none";
    }

}

function cbxsCheck(name) {
    var regex = new RegExp(name);
    var cells = document.getElementsByTagName("input");
    for (var i = 0; i < cells.length; i++) {
        id = cells[i].getAttribute("id");
        if (regex.test(id)) {
            cells[i].checked = true;
        }
    }
}

function cbxsUncheck(name) {
    var regex = new RegExp(name);
    var cells = document.getElementsByTagName("input");
    for (var i = 0; i < cells.length; i++) {
        id = cells[i].getAttribute("id");
        if (regex.test(id)) {
            cells[i].checked = false;
        }
    }
}

function cbxAccepted(cbx, name) {
    var regex = new RegExp(name);
    var cells = document.getElementsByTagName("input");
    for (var i = 0; i < cells.length; i++) {
        id = cells[i].getAttribute("id");
        if (regex.test(id) && cells[i].disabled == false) {
            cells[i].checked = cbx.checked;
        }
    }
}

/*
function cbxCheckAll(cbxToTipyfy, selectWithClassName)
{
var span;
var checkbox;
var spans = document.getElementsByTagName("span");
for(var i = 0; i < spans.length; i++)
{
span = spans[i];
if(span['className'] == selectWithClassName)
{
checkbox = span.getElementsByTagName("input");
if(checkbox != null)
{
checkbox.checked = cbxToTipyfy.checked;
}
}
}
}
*/

function cbxReject(cbx, cbxName, spanName) {
    var cbxRe = new RegExp(cbxName);
    var spanRe = new RegExp(spanName);
    var cells;

    cells = document.getElementsByTagName("input");
    for (var i = 0; i < cells.length; i++) {
        id = cells[i].getAttribute("id");
        if (cbxRe.test(id)) {
            cells[i].checked = cbx.checked;
        }
    }

    cells = document.getElementsByTagName("span");
    for (var i = 0; i < cells.length; i++) {
        id = cells[i].getAttribute("id");
        if (spanRe.test(id)) {
            if (cbx.checked) {
                cells[i].style.display = 'block';
            }
            else {
                cells[i].style.display = 'none';
            }
        }
    }
}


//otwiera nowe okno przeglądarki.
//Krzysztof Szepietowski
function openWindow(url, name) {
    testwindow = window.open(url, name, "location=1,status=1,scrollbars=1,width=10,height=10");
    testwindow.moveTo(0, 0);
    testwindow.focus();
}

function openReportWindow(url) {
    ScrWidth = 640;
    ScrHeight = 480;

    if (window.screen) {
        ScrWidth = window.screen.width;
        ScrHeight = window.screen.height
    }
    PosX = Math.round((ScrWidth) / 2);
    PosY = Math.round((ScrHeight) / 2);
    Window = window.open(url, null, 'left=' + PosX + ',top' + PosY + ',location=0,status=1,resizable=1,menubar=0,toolbar=0,scrollbars=1');
    Window.focus();

    return false;
}

function openInfoWindow(url, name, width, height) {
    PosX = 50;
    PosY = 50;
    Window = window.open(url, name, 'left=' + PosX + ',top' + PosY + ',location=0,status=1,resizable=1,menubar=0,toolbar=0,scrollbars=1, width=' + width + ',height=' + height + '"');
    Window.focus();

    return false;
}


function openWarningWindow(url) {
    PosX = 50;
    PosY = 50;
    Window = window.open(url, null, 'left=' + PosX + ',top' + PosY + ',location=1,status=1,resizable=1,menubar=0,toolbar=0,scrollbars=1, width=920, height=420"');
    Window.focus();

    return false;
}

function OpenFileWindow(width, height) {
    ScrWidth = 800;
    ScrHeight = 600;

    if (window.screen) {
        ScrWidth = window.screen.width;
        ScrHeight = window.screen.height
    }
    PosX = Math.round((ScrWidth - width) / 2);
    PosY = Math.round((ScrHeight - height) / 2);
    var strWindowFeatures = "menubar=no, toolbar=no, resizable=no,scrollbars=yes,status=no,";
    strWindowFeatures = strWindowFeatures + 'left=' + PosX + ',top' + PosY + ',width=' + width + ',height=' + height;
    Window = window.open('UploadFile.aspx', null, strWindowFeatures);
    Window.focus();
}

function OpenLogoWindow(width, height) {
    ScrWidth = 640;
    ScrHeight = 480;

    if (window.screen) {
        ScrWidth = window.screen.width;
        ScrHeight = window.screen.height
    }
    PosX = Math.round((ScrWidth - width) / 2);
    PosY = Math.round((ScrHeight - height) / 2);

    Window = window.open('UploadLogo.aspx', null, 'left=' + PosX + ',top' + PosY + ',width=' + width + ',height=' + height + 'scrollbars=yes', true);
    Window.focus();
}

//formatuje zawartość pola do pobierania HCP.
//Krzysztof Szepietowski
function formatHCP(txt, correctEmpty) {
    var re = new RegExp(',[0-9]{1}$');
    if (!txt.value.match(re)) {
        if (txt.value == '' && correctEmpty) {
            txt.value = '00,0';
        }

        if (txt.value.match(new RegExp('^[-+]?[0-9]{1,}$'))) {
            txt.value = txt.value + ',0';
        }

        if (txt.value.match(new RegExp('^[-+]?[0-9]{1,},$'))) {
            txt.value = txt.value + '0';
        }

        if (txt.value.length > 5) {
            txt.value = txt.value.substring(txt.value.length - 5, txt.value.length);
        }
    }
}

//Zamienia pierwszą literę na dużą w tekstboksie
function CapitalFirstLetter(ctrl) {
    var length = ctrl.value.length;
    var firstLetter = ctrl.value.substring(0, 1);
    var other = ctrl.value.substring(1, length);
    var capitalLetter = null;

    if (length != 0) {
        capitalLetter = firstLetter.toUpperCase();
        ctrl.value = capitalLetter + other;
    }
}

var _fieldWasEmpty;

//funckcja wykrozystywana na formularzu do podawania wyników
//Krzysztof Szepietowski
function MoveToNextField(field, divData) {
    if (field != null) {

        var value = 1000;

        if (field.value != "x" && field.value != "X") {
            value = parseInt(field.value);
        }

        if (value > 1) {
            var soroundingElement = document.getElementById(divData);
            if (soroundingElement != null) {
                var divs = soroundingElement.getElementsByTagName("input");

                var tabIndeks = parseInt(field.tabIndex);
                tabIndeks = tabIndeks + 1;

                for (var i = 0; i < divs.length; i++) {
                    if (divs[i].tabIndex == tabIndeks) {
                        divs[i].focus();
                        divs[i].select();
                    }
                }

                if (field.value != null && field.value.length > 1) {
                    var firstDigit = field.value.substring(0, 1);
                    if (firstDigit != 1 && _fieldWasEmpty) {
                        field.value = firstDigit;
                    }
                }

                //                if (field.value == "x" || field.value == "X") {
                //                    field.value = -1;
                //                }
            }
        }
    }
}

//funckcja wykrozystywana na formularzu do podawania wyników
//Krzysztof Szepietowski
function DetermineIsEmpty(field) {
    if (field != null) {
        if (field.value != null && field.value.length > 0) {
            _fieldWasEmpty = false;
        }
        else {
            _fieldWasEmpty = true;
        }
    }
}

//funckcja wykrozystywana na formularzu do podawania wyników, oblicza wynik bez odwołań asynchronicznych
//par - par dolka
//handicapResults - liczna uderzen handicapowych dla tego dołka
//strokesClientId - właściwość ClientID kontrolki do której wpisywana jest liczba uderzen
//stbNettoClientId - właściwość ClientID span'u do którego wstawiany jest wynik z pojedyńczego dołka
//stbBruttoClientId - właściwość ClientID span'u do którego wstawiany jest wynik z pojedyńczego dołka
//stbNettoControlId - właściwość Id span'u do którego wstawiany jest wynik z pojedyńczego dołka
//stbBruttoControlId - właściwość Id span'u do którego wstawiany jest wynik z pojedyńczego dołka
//sumStbNettControlId - właściwość Id spanu do którego wpisywana jest suma wyników po wszystkich 18 - stu dolkach
//sumStbBruttoControlId - właściwość Id spanu do którego wpisywana jest suma wyników po wszystkich 18 - stu dolkach
//inStbNettControlId - właściwość Id spanu do którego wpisywana jest suma wyników dla dolkow 10 - 18
//inStbBruttoControlId - właściwość Id spanu do którego wpisywana jest suma wyników dla dolkow 10 - 18
//outStbNettControlId - właściwość Id spanu do którego wpisywana jest suma wyników dla dolkow 1 - 9
//outStbBruttoControlId - właściwość Id spanu do którego wpisywana jest suma wyników dla dolkow 10 - 18
//number - numer dołka
//Krzysztof Szepietowski
function ShowResults(par, handicapResults, strokesClientId,
                    stbNettoClientId, stbBruttoClientId, stbNettoControlId,
                    stbBruttoControlId, sumStbNettControlId, sumStbBruttoControlId,
                    inStbNettControlId, inStbBruttoControlId, outStbNettControlId, outStbBruttoControlId,
                    strokesControlId, inStrokesControlId, outStrokesControlId, sumStrokesControlId, number) {
    var strokesControl = document.getElementById(strokesClientId);
    var stbNettoControl = document.getElementById(stbNettoClientId);
    var stbBruttoControl = document.getElementById(stbBruttoClientId);

    if (strokesControl != null && stbNettoControl != null && stbBruttoControl != null) {
        var strokes = 0;
        if (strokesControl.value != null) {
            if (strokesControl.value == "x" || strokesControl == "X") {
                strokes = 100;
            }
            else {
                strokes = parseInt(strokesControl.value);
            }
        }

        if (strokes > 0) {
            var strokesNetto = strokes - handicapResults;
            var resultNetto = par - strokesNetto;
            var resultBrutto = par - strokes;

            stbNettoControl.innerHTML = GetStablefordPoints(resultNetto);
            stbBruttoControl.innerHTML = GetStablefordPoints(resultBrutto);
        }
        else {
            stbNettoControl.innerHTML = "";
            stbBruttoControl.innerHTML = "";
        }
    }

    /////////////// obliczanie i wyswietlanie sumy ///////////////
    var tmp;

    var stbNettoSum = 0;
    var stbBruttoSum = 0;
    var stbNettoIn = 0;
    var stbBruttoIn = 0;
    var stbNettoOut = 0;
    var stbBruttoOut = 0;
    var strokesSum = 0;
    var strokesIn = 0;
    var strokesOut = 0;

    var regexStbNetto = new RegExp(stbNettoControlId);
    var regexStbBrutto = new RegExp(stbBruttoControlId);
    var regexStrokes = new RegExp(strokesControlId);

    var regexStbNettoSum = new RegExp(sumStbNettControlId);
    var regexStbBruttoSum = new RegExp(sumStbBruttoControlId);
    var regexStbNettoIn = new RegExp(inStbNettControlId);
    var regexStbBruttoIn = new RegExp(inStbBruttoControlId);
    var regexStbNettoOut = new RegExp(outStbNettControlId);
    var regexStbBruttoOut = new RegExp(outStbBruttoControlId);

    var regexStrokesIn = new RegExp(inStrokesControlId);
    var regexStrokesOut = new RegExp(outStrokesControlId);
    var regexStrokesSum = new RegExp(sumStrokesControlId);

    var stbNettoSumCC = 0;
    var stbBruttoSumCC = 0;
    var stbNettoInCC = 0;
    var stbBruttoInCC = 0;
    var stbNettoOutCC = 0;
    var stbBruttoOutCC = 0;
    var strokesSumCC = 0;
    var strokesInCC = 0;
    var strokesOutCC = 0;

    var spans = document.getElementsByTagName("span");

    for (var i = 0; i < spans.length; i++) {
        var id = spans[i].getAttribute("id");

        if (regexStbNetto.test(id)) {
            if (spans[i].innerHTML != "") {
                tmp = parseInt(spans[i].innerHTML);
                if (tmp > 0) {
                    stbNettoSum += tmp;
                }
            }
        }
        if (regexStbBrutto.test(id)) {
            if (spans[i].innerHTML != "") {
                tmp = parseInt(spans[i].innerHTML);
                if (tmp > 0) {
                    stbBruttoSum += tmp;
                }
            }
        }

        if (number <= 9) {
            if (regexStbNetto.test(id)) {
                if (spans[i].innerHTML != "" && spans[i].className == "out") {
                    tmp = parseInt(spans[i].innerHTML);
                    if (tmp > 0) {
                        stbNettoOut += tmp;
                    }
                }
            }
            if (regexStbBrutto.test(id)) {
                if (spans[i].innerHTML != "" && spans[i].className == "out") {
                    tmp = parseInt(spans[i].innerHTML);
                    if (tmp > 0) {
                        stbBruttoOut += tmp;
                    }
                }
            }
        }
        else {
            if (regexStbNetto.test(id)) {
                if (spans[i].innerHTML != "" && spans[i].className == "in") {
                    tmp = parseInt(spans[i].innerHTML);
                    if (tmp > 0) {
                        stbNettoIn += tmp;
                    }
                }
            }
            if (regexStbBrutto.test(id)) {
                if (spans[i].innerHTML != "" && spans[i].className == "in") {
                    tmp = parseInt(spans[i].innerHTML);
                    if (tmp > 0) {
                        stbBruttoIn += tmp;
                    }
                }
            }
        }

        if (regexStbNettoSum.test(id)) {
            stbNettoSumCC = spans[i];
        }
        if (regexStbBruttoSum.test(id)) {
            stbBruttoSumCC = spans[i];
        }

        if (regexStbNettoIn.test(id)) {
            stbNettoInCC = spans[i];
        }
        if (regexStbBruttoIn.test(id)) {
            stbBruttoInCC = spans[i];
        }

        if (regexStbNettoOut.test(id)) {
            stbNettoOutCC = spans[i];
        }
        if (regexStbBruttoOut.test(id)) {
            stbBruttoOutCC = spans[i];
        }
    } //for

    var inputs = document.getElementsByTagName("input");
    for (var j = 0; j < inputs.length; j++) {
        var id = inputs[j].getAttribute("id");

        if (regexStrokes.test(id)) {
            if (inputs[j].value != "") {
                tmp = parseInt(inputs[j].value);
                if (tmp > 0) {
                    strokesSum += tmp;
                }
            }
        }

        if (number <= 9) {
            if (regexStrokes.test(id)) {
                if (inputs[j].value != "" && inputs[j].className == "out") {
                    tmp = parseInt(inputs[j].value);
                    if (tmp > 0) {
                        strokesOut += tmp;
                    }
                }
            }
        }
        else {
            if (regexStrokes.test(id)) {
                if (inputs[j].value != "" && inputs[j].className == "in") {
                    tmp = parseInt(inputs[j].value);
                    if (tmp > 0) {
                        strokesIn += tmp;
                    }
                }
            }
        }

        if (regexStrokesSum.test(id)) {
            strokesSumCC = inputs[j];
        }
        if (regexStrokesIn.test(id)) {
            strokesInCC = inputs[j];
        }
        if (regexStrokesOut.test(id)) {
            strokesOutCC = inputs[j];
        }
    }

    if (stbNettoSumCC != null) {
        if (stbNettoSum > 0) {
            stbNettoSumCC.innerHTML = stbNettoSum;
        }
        else {
            stbNettoSumCC.innerHTML = "";
        }
    }
    if (stbBruttoSumCC != null) {
        if (stbBruttoSum > 0) {
            stbBruttoSumCC.innerHTML = stbBruttoSum;
        }
        else {
            stbBruttoSumCC.innerHTML = "";
        }
    }
    if (strokesSumCC != null) {
        if (strokesSum > 0) {
            strokesSumCC.value = strokesSum
        }
        else {
            strokesSumCC.value = "";
        }
    }

    if (number <= 9) {
        if (stbNettoOutCC != null) {
            if (stbNettoOut > 0) {
                stbNettoOutCC.innerHTML = stbNettoOut;
            }
            else {
                stbNettoOutCC.innerHTML = "";
            }
        }
        if (stbBruttoOutCC != null) {
            if (stbBruttoOut > 0) {
                stbBruttoOutCC.innerHTML = stbBruttoOut;
            }
            else {
                stbBruttoOutCC.innerHTML = "";
            }
        }
        if (strokesOutCC != null) {
            if (strokesOut > 0) {
                strokesOutCC.value = strokesOut;
            }
            else {
                strokesOutCC.value = "";
            }
        }
    }
    else {
        if (stbNettoInCC != null) {
            if (stbNettoIn > 0) {
                stbNettoInCC.innerHTML = stbNettoIn;
            }
            else {
                stbNettoInCC.innerHTML = "";
            }
        }
        if (stbBruttoInCC != null) {
            if (stbBruttoIn > 0) {
                stbBruttoInCC.innerHTML = stbBruttoIn;
            }
            else {
                stbBruttoInCC.innerHTML = "";
            }
        }
        if (strokesInCC != null) {
            if (strokesIn > 0) {
                strokesInCC.value = strokesIn;
            }
            else {
                strokesInCC.value = "";
            }
        }
    }
}

//funkcja pomocnicza uzywana wewnatrz ShowResults
//diff - roznica pomiedzy par a strokes
function GetStablefordPoints(diff) {
    if (diff == 4 || diff == 5) // teoretycznie jest to mozliwe ze roznica bedzie 5
    {
        return 6;
    }
    else if (diff == 3) {
        return 5;
    }
    else if (diff == 2) {
        return 4;
    }
    else if (diff == 1) {
        return 3;
    }
    else if (diff == 0) {
        return 2;
    }
    else if (diff == -1) {
        return 1;
    }
    else {
        return 0;
    }
}

//funkcja przewija strone do wiersza zawierającego w ukrytym polu wartośc "value"
function scrollToControl(valueId) {
    var inputs = null;
    var input = null;

    var valueControl = document.getElementById(valueId);

    if (valueControl == null) {
        window.scrollTo(0, 0);
        return false;
    }

    if (valueControl.value == '') {
        window.scrollTo(0, 0);
        return false;
    }

    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].value == valueControl.value && inputs[i].id != valueId) {
            input = inputs[i];
            break;
        }
    }

    if (input != null) {
        input.parentNode.innerHTML += "<span id='" + valueControl.value + "'></span>";
    }

    var control = document.getElementById(valueControl.value);
    if (control != null) {
        //określenie przesunięcia elementu więcej informacji na stronie http://www.quirksmode.org/js/findpos.html
        var curtop = 0;
        if (control.offsetParent) {
            curtop = control.offsetTop
            while (control = control.offsetParent) {
                curtop += control.offsetTop
            }
        }

        //bez tego odejmowania przewijanie odbywa sie do krawedzi strony
        curtop -= 40;
        window.scrollTo(0, curtop);
    }

    return false;
}

/*proste zastepstwo collapsible panel*/
function MyCollapsePanel(panid, img) {
    if (document.getElementById(panid).style.display == 'none') {
        img.src = img.src.replace(/sort_asc/, "sort_desc");
        document.getElementById(panid).style.display = 'block';
    }
    else {
        img.src = img.src.replace(/sort_desc/, "sort_asc");
        document.getElementById(panid).style.display = 'none';
    }
}

//kasowanie whitespaców, bardzo szybkie dla kazdje przegladarki http://blog.stevenlevithan.com/archives/faster-trim-javascript
function string_trim(str) {
    var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    for (var i = 0; i < str.length; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    for (i = str.length - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

//zamyka okno, w którym ta funkcja jest wykonana
//Marcin Teodorczyk
function CloseRadWindow(hfDataChangeId) {
    var rw = GetRadWindow();
    if (rw) {
        rw.Argument = document.getElementById(hfDataChangeId).value;
        rw.close();
    }
    else {
        window.opener = 'x';
        window.close();
    }
    return false;
}

function GetBrowserName() {
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName = navigator.appName;
    var fullVersion = '' + parseFloat(navigator.appVersion);
    var majorVersion = parseInt(navigator.appVersion, 10);
    var nameOffset, verOffset, ix;

    // In MSIE, the true version is after "MSIE" in userAgent
    if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
        browserName = "Microsoft Internet Explorer";
        fullVersion = nAgt.substring(verOffset + 5);
    }
    // In Opera, the true version is after "Opera" 
    else if ((verOffset = nAgt.indexOf("Opera")) != -1) {
        browserName = "Opera";
        fullVersion = nAgt.substring(verOffset + 6);
    }
    // In Chrome, the true version is after "Chrome" 
    else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
        browserName = "Chrome";
        fullVersion = nAgt.substring(verOffset + 7);
    }
    // In Safari, the true version is after "Safari" 
    else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
        browserName = "Safari";
        fullVersion = nAgt.substring(verOffset + 7);
    }
    // In Firefox, the true version is after "Firefox" 
    else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
        browserName = "Firefox";
        fullVersion = nAgt.substring(verOffset + 8);
    }
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) {
        browserName = nAgt.substring(nameOffset, verOffset);
        fullVersion = nAgt.substring(verOffset + 1);
        if (browserName.toLowerCase() == browserName.toUpperCase()) {
            browserName = navigator.appName;
        }
    }
    // trim the fullVersion string at semicolon/space if present
    if ((ix = fullVersion.indexOf(";")) != -1) fullVersion = fullVersion.substring(0, ix);
    if ((ix = fullVersion.indexOf(" ")) != -1) fullVersion = fullVersion.substring(0, ix);

    majorVersion = parseInt('' + fullVersion, 10);
    if (isNaN(majorVersion)) {
        fullVersion = '' + parseFloat(navigator.appVersion);
        majorVersion = parseInt(navigator.appVersion, 10);
    }

    return browserName;
}

function formatHCP(txt, correctEmpty) {
    var re = new RegExp(',[0-9]{1}$');
    if (!txt.value.match(re)) {
        if (txt.value == '' && correctEmpty) {
            txt.value = '00,0';
        }

        if (txt.value.match(new RegExp('^[-+]?[0-9]{1,}$'))) {
            txt.value = txt.value + ',0';
        }

        if (txt.value.match(new RegExp('^[-+]?[0-9]{1,},$'))) {
            txt.value = txt.value + '0';
        }

        if (txt.value.length > 5) {
            txt.value = txt.value.substring(txt.value.length - 5, txt.value.length);
        }
    }
}
