﻿function message(msg_id) {
    switch (msg_id) {
        case "invalid_date": return (lang == "en" ? "invalid date" : "neplatné datum"); break;
        case "invalid_time": return (lang == "en" ? "invalid time" : "neplatný čas"); break;
        case "required_item": return (lang == "en" ? "item required" : "povinná položka"); break;
        case "fill_yes_no": return (lang == "en" ? "fill Yes or No" : "zadejte Ano nebo Ne"); break;
        case "not_integer": return (lang == "en" ? "integer required" : "není celé číslo"); break;
        case "future_date_is_invalid": return (lang == "en" ? "future date is invalid" : "nelze zadat budoucí datum"); break;
        case "past_date_is_invalid": return (lang == "en" ? "past date is invalid" : "nelze zadat prošlé datum"); break;
        case "future_time_is_invalid": return (lang == "en" ? "future time is invalid" : "nelze zadat budoucí čas"); break;
        case "past_time_is_invalid": return (lang == "en" ? "past time is invalid" : "nelze zadat prošlý čas"); break;
        case "invalid_identification_number": return (lang == "en" ? "invalid identification number" : "neplatné IČ"); break;
        case "invalid_birth_number": return (lang == "en" ? "invalid birth number" : "neplatné rodné číslo"); break;
        case "too_short": return (lang == "en" ? "too_short" : "moc krátké"); break;
        case "invalid_account_number": return (lang == "en" ? "invalid account number" : "neplatné číslo účtu"); break;
        case "too_long": return (lang == "en" ? "too long" : "moc dlouhé"); break;
    }
}

var nInput = 0;
var aInput = new Array();

//registruje prikazy pro ukladani
function regInput(item, attr, tag, ctrl_id, validator, type) {
    input = new Object();
    input.item = item;
    input.attr = attr;
    input.tag = tag;
    input.id = ctrl_id;
    input.validator = validator;
    input.typ = type;
    if (ctrl_id != null)
        input.old_value = window.document.all(ctrl_id).value;
    else
        input.old_value = null;
    aInput[nInput] = input;
    nInput = nInput + 1;
}

function Goto(href) {
    document.URL = href;
}

function GotoURL(url, test) {
    if (!test) test = false
    document.all["Output_url"].value = url;
    if (!SavePage()) {
        if (!test || Validation("", ""))
            window.navigate(url);
    }
}

// nastavuje priznak zmen na strance
function PageDirty() {
    window.document.all("Edit_Save").disabled = false;
}

// testuje priznak zmen na strance
function IsPageDirty() {
    if (document.getElementById("Edit_Save") == null)
        return false;
    return (window.document.all("Edit_Save").disabled == false);
}

// maze priznak zmen na strance
function PageOK() {
    window.document.all("Edit_Save").disabled = true;
}

// ukladani stranky
function SavePage() {
    if (IsPageDirty()) {
        if (!confirm('Changes were not saved. Save ?'))
            return;
        window.document.all("Edit_Save").click();
        return true;
    }
    return false;
}

function Trim(str) {
    str = str.replace(/^\s+/g, "");
    return str.replace(/\s+$/g, "");
}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function Mid(str, p) {
    var n = String(str).length;
    if (p > n)
        return "";
    return String(str).substring(p);
}

function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function IsInteger(s) {
    while (s.length > 0 && s.charAt(s.length - 1) == " ")
        s = s.substring(0, s.length - 1);
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (!((c >= "0") && (c <= "9"))) {
            return false;
        }
    }
    return true;
}

function createDate(d, m, y, h, mi) {
    dd = new Date();
    dd.setDate(1);
    dd.setMonth(0);
    dd.setYear(y);
    dd.setMonth(m - 1);
    dd.setDate(d);
    dd.setHours(h);
    dd.setMinutes(mi);
    return dd;
}

function IsDate(d, m, y, h, mi) {
    dd = createDate(d, m, y, h, mi);
    return (dd.getFullYear() == y && dd.getMonth() == (m - 1) && dd.getDate() == d && dd.getHours() == h && dd.getMinutes() == mi);
}

function setCtrlVisibile(ctrl) {
    try {
        if (window.document.all(ctrl).style.visibility == "hidden") {
            window.document.all(ctrl).style.visibility = "visible";
        }
    }
    catch (err) {
    }
}

function setCtrlHidden(ctrl) {
    try {
        if (window.document.all(ctrl).style.visibility == "visible") {
            window.document.all(ctrl).style.visibility = "hidden";
        }
    }
    catch (err) {
    }
}

function getCtrlValue(ctrl) {
    try {
        if (window.document.all(ctrl).type == "checkbox")
            vv = ((window.document.all(ctrl).checked) ? 1 : 0);
        else
            vv = window.document.all(ctrl).value;
        return vv;
    }
    catch (err) {
        return "";
    }
}

function setCtrlValue(ctrl, value) {
    try {
        window.document.all(ctrl).value = value;
    }
    catch (err) {
    }
}

function setCtrlInnerText(ctrl, value) {
    try {
        window.document.all(ctrl).innerText = value;
    }
    catch (err) {
    }
}

function setCtrlInvalid(ctrl) {
    try {
        var cls = " " + Trim(window.document.all(ctrl).className) + " ";
        cls = cls.replace(" invalid ", " ");
        cls = Trim(cls) + " invalid";
        window.document.all(ctrl).className = cls;
    }
    catch (err) {
    }
}

function setCtrlValid(ctrl) {
    try {
        var cls = " " + Trim(window.document.all(ctrl).className) + " ";
        cls = cls.replace(" invalid ", " ");
        cls = Trim(cls);
        window.document.all(ctrl).className = cls;
    }
    catch (err) {
    }
}

function setCtrlClass(ctrl, value) {
    try {
        e = window.document.getElementById(ctrl);
         e.className = value;
        e.setAttribute('className',value);
    }
    catch (err) {
    }
}

function setCtrlFocus(ctrl) {
    try {
        window.document.all(ctrl).focus();
    }
    catch (err) {
    }
}

function setCtrlError(ctrl, msg) {
    if (msg.length > 0) {
        setCtrlInnerText("alert_" + ctrl, msg);
        setCtrlInvalid(ctrl);
    }
    else {
        setCtrlInnerText("alert_" + ctrl, "");
        setCtrlValid(ctrl);
    }
}

function clsItems(s, d) {
    var s = Trim(s);
    var d = d;
    this.GetItem = GetItem;

    function GetItem() {
        if (s.length == 0)
            return "";
        i = s.indexOf(d);
        if (i >= 0) {
            ss = Left(s, i);
            s = Trim(Mid(s, i));
        }
        else {
            ss = s;
            s = "";
        }
        return ss;
    }
}

function Validator(ctrl, validator) {
    var items = new clsItems(validator, " ");
    par = getCtrlValue(ctrl);
    try {
        validator = items.GetItem();
        while (validator.length > 0) {
            switch (validator) {
                case "REQ":
                    if (par.length == 0) {
                        throw message("required_item");
                    }
                    break;
                case "Y/N":
                    par = Trim(par);
                    if (par != 'Y' && par != 'N')
                        throw message("fill_yes_no");
                    break;
                case "Integer":
                    par = Trim(par);
                    if (!IsInteger(par))
                        throw message("not_integer");
                    break;
                case "RC_IC":
                    if (par.length >= 9)
                        return Validator(ctrl, 'RC');
                    else
                        return Validator(ctrl, 'IC');
                    break;
                case "DATE":
                case "TIME":
                    dd = new Date();
                    i = par.indexOf(',');
                    if (i >= 0) {
                        par = par.replace(/,/g, ".")
                        setCtrlValue(ctrl, par);
                    }
                    par = par + " ";
                    i = par.indexOf('.');
                    if (i < 0) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    d = par.substring(0, i);
                    if (!IsInteger(d)) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    d = Number(d);
                    i1 = par.indexOf('.', i + 1);
                    if (i1 < 0) {
                        i1 = par.indexOf(' ', i + 1);
                        if (i1 < 0) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                        y = dd.getFullYear();
                        par = par.substring(0, i1) + "." + y + par.substring(i1)
                        setCtrlValue(ctrl, par);
                    }
                    i1 = par.indexOf('.', i + 1);
                    if (i1 < 0) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    m = par.substring(i + 1, i1);
                    if (!IsInteger(m)) message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    m = Number(m);
                    i2 = validator == "DATE" ? par.length : par.indexOf(' ', i1 + 1);
                    if (i2 < 0) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    y = par.substring(i1 + 1, i2);
                    if (!IsInteger(y)) throw message(validator == "DATE" ? "invalid_date" : "invalid_time");
                    y = Number(y);
                    if (validator == "DATE") {
                        if (!IsDate(d, m, y, 0, 0))
                            throw message("invalid_date");
                    } else {
                        i3 = par.indexOf(':', i2 + 1);
                        if (i3 < 0) throw message("invalid_time");
                        h = par.substring(i2 + 1, i3);
                        if (!IsInteger(h)) throw message("invalid_time");
                        h = Number(h);
                        i4 = par.length;
                        if (i4 < 0) throw message("invalid_time");
                        mi = par.substring(i3 + 1, i4);
                        if (!IsInteger(mi)) throw message("invalid_time");
                        mi = Number(mi);
                        if (!IsDate(d, m, y, h, mi))
                            throw message("invalid_time");
                    }
                    break;
                case "DATE-OLD":
                    if (!Validator(ctrl, 'DATE'))
                        return;
                    if (y * 12 * 31 + m * 31 + d > dd.getFullYear() * 12 * 31 + (dd.getMonth() + 1) * 31 + dd.getDate()) {
                        throw message("future_date_is_invalid");
                    }
                    break;
                case "DATE-NEW":
                    if (!Validator(ctrl, 'DATE'))
                        return;
                    if (y * 12 * 31 + m * 31 + d < dd.getFullYear() * 12 * 31 + (dd.getMonth() + 1) * 31 + dd.getDate())
                        throw message("past_date_is_invalid");
                    break;
                case "TIME-OLD":
                    if (!Validator(ctrl, 'TIME'))
                        return;
                    if (y * 12 * 31 * 24 * 60 + m * 31 * 24 * 60 + d * 24 * 60 + h * 60 + mi > dd.getFullYear() * 12 * 31 * 24 * 60 + (dd.getMonth() + 1) * 31 * 24 * 60 + dd.getDate() * 24 * 60 + dd.getHours() * 60 + dd.getMinutes())
                        throw message("future_time_is_invalid");
                    break;
                case "TIME-NEW":
                    if (!Validator(ctrl, 'TIME'))
                        return;
                    if (y * 12 * 31 * 24 * 60 + m * 31 * 24 * 60 + d * 24 * 60 + h * 60 + mi < dd.getFullYear() * 12 * 31 * 24 * 60 + (dd.getMonth() + 1) * 31 * 24 * 60 + dd.getDate() * 24 * 60 + dd.getHours() * 60 + dd.getMinutes())
                        throw message("past_time_is_invalid");
                    break;
                case "IC":  // kontrola ICO
                    par = Trim(par);
                    if (!IsInteger(par))
                        throw message("invalid_identification_number");
                    if (par.length > 8)
                        throw message("too_long");
                    par = Right("00000000" + par, 8);
                    setCtrlValue(ctrl, par);
                    s = 0;
                    for (i = 1; i < 9; i++)
                        s = s + (9 - i) * Number(par.substring(i - 1, i));
                    s = s % 11;
                    if (Right(par, 1) == "0" && s == 1) break;
                    if (Right(par, 1) == "1" && s <= 1) break;
                    if (Right(par, 1) > "1" && s == 0) break;
                    throw message("invalid_identification_number");
                    break;
                case "RC":
                    par = Trim(par);
                    if (!IsInteger(par))
                        throw message("invalid_birth_number");
                    if (par.length < 9)
                        throw message("too_short");
                    if (par.length > 10)
                        throw message("too_long");
                    y = new Date();
                    y = y.getYear();
                    s = Number(par.substring(0, 2));
                    s = Math.floor(y / 100) - ((s > (y % 100)) ? 1 : 0);
                    y = s * 100 + Number(par.substring(0, 2));
                    m = Number(par.substring(2, 4)) % 50;
                    d = Number(par.substring(4, 6));
                    if (!IsDate(d, m, y))
                        throw message("invalid_birth_number");
                    if (par.length == 9 && y > 1953)
                        throw message("invalid_birth_number");
                    if (par.length == 10 && Number(par) % 11 != 0)
                        throw message("invalid_birth_number");
                    break;
                case "ACCOUNT-NUMBER":
                    par = Trim(par);
                    if (!IsInteger(par))
                        throw message("invalid_account_number");
                    if (par.length > 16)
                        throw message("too_long");
                    par = Right("0000000000000000" + par, 16);
                    if (Number(par.substring(0, 8)) == 0 && Number(par.substring(8, 16)) < 1000)
                        throw message("too_short");
                    window.document.all(ctrl).value = par;
                    s = 0
                    for (i = 0; i < 16; i++)
                        s = s + Number(par.substring(i, i + 1)) * Number("10 5 8 4 2 1 6 3 7 910 5 8 4 2 1".substring(2 * i, 2 * i + 2));
                    s = s % 11;
                    if (s != 0)
                        throw message("invalid_account_number");
                    break;
                case "CLIENT-ID":
                    break;
            }
            validator = items.GetItem();
        }
    }
    catch (err) {
        setCtrlError(ctrl, err);
        return false;
    }
    setCtrlError(ctrl, "");
    return true;
}

function SaveForm(cmd, newdata) {
    var i;
    if (!Validation("", ""))
        return;
    xml = "";
    if (newdata == null)
        newdata = 0;
    item = "";
    for (i = 0; i < nInput; i++) {
        switch (aInput[i].typ) {
            case "listbox":
                var l = document.getElementById(aInput[i].id);
                if (l.children.length == 0) {
                    xml = xml + "<" + aInput[i].item +
                      " " + aInput[i].attr +
                      "/>\r";
                }
                else {
                    for (var li = 0; li < l.children.length; li++) {
                        var o = l.children[li];
                        xml = xml + "<" + aInput[i].item +
                      " " + aInput[i].attr +
                      " id=\"" + o.id + "\" text=" + "\"" + o.text + "\"" +
                      "/>\r";
                    }
                }
                break;
            default:
                vl = getCtrlValue(aInput[i].id);
                if ((aInput[i].old_value != vl) || (newdata == 1)) {
                    if (aInput[i].item.substring(0, 1) == "<") {
                        if (aInput[i].item != item)
                            xml = xml + "</" + item + ">\r";
                        if (aInput[i].item != "") {
                            tag = aInput[i].item
                            xml = xml + "<" + aInput[i].item + " " + aInput[i].attr + ">";
                        }
                        xml = xml + "<" + aInput[i].tag + "><![CDATA[" + vl + "]]></" + aInput[i].tag + ">";
                    }
                }
                break;
        }
        xml = xml + "</" + item + ">\r";
    }
    document.all["Output"].value = xml;
    document.all["Output_cmd"].value = cmd;
    PageOK();
    document.all.tags("form").item("Output_Form").submit();
}

function Validation(ctrl, validator) {
    var i;
    if (ctrl == '') {
        valid = true;
        for (i = 0; i < nInput; i++) {
            if (aInput[i].validator.length > 0) {
                if (!Validator(aInput[i].id, aInput[i].validator))
                    valid = false;
            }
        }
    }
    else {
        PageDirty();
        valid = Validator(ctrl, validator);
    }
    return valid;
}

function DialogOpen(src, left, top, width, height) {
    GotoURL(src);
    //  setCtrlVisibile("DialogWindow");
    //  window.document.all("DialogWindow").style.left=document.body.scrollLeft+(document.body.clientWidth-width)/2;
    //  window.document.all("DialogWindow").style.top=document.body.scrollTop+(document.body.clientHeight-height)/2;
    //  window.document.all("DialogWindow").style.width=width;
    //  window.document.all("DialogWindow").style.height=height;
    //  window.document.all("Dialog").src=src;
}

function DialogResize(dlg_ctrl, left, top, width, height) {
    var d = window.document.all(dlg_ctrl);
    if (height == 0) height = d.offsetHeight;
    if (width == 0) width = d.offsetWidth;
    setCtrlVisibile(dlg_ctrl);
    pw = d.ownerDocument.body.clientWidth;
    ph = d.ownerDocument.body.clientHeight;
    d.style.left = d.ownerDocument.body.scrollLeft + (pw - width) / 2;
    d.style.top = d.ownerDocument.body.scrollTop + (ph - height) / 2;
    d.style.width = width;
    d.style.height = height;
    d.style.zIndex = 1000;
}

function DialogClose(dlg_ctrl) {
    setCtrlHidden(dlg_ctrl);
}

function ctrlResize(ctrl, height, width) {
    if (height < 0) height = ctrl.style.height;
    if (width < 0) width = ctrl.style.width;
    ctrl.style.left = ctrl.ownerDocument.body.scrollLeft + (ctrl.ownerDocument.body.clientWidth - width) / 2;
    ctrl.style.top = ctrl.ownerDocument.body.scrollTop + (ctrl.ownerDocument.body.clientHeight - height) / 2;
    ctrl.style.height = height;
    ctrl.style.width = width;
}

function ListBoxMove(list_box1, list_box2) {
    var l1 = document.getElementById(list_box1);
    var l2 = document.getElementById(list_box2);

    for (var i1 = 0; i1 < l1.children.length; i1++) {
        var o1 = l1.children[i1];
        var id = o1.id;
        if (o1.getAttribute("selected")) {
            var ok = true;
            for (var i2 = 0; i2 < l2.children.length; i2++) {
                if (l2.children[i2].id == id) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                var o2
                o2 = document.createElement("OPTION");
                o2 = new Option(o1.text, o1.text);
                o2.id = o1.id;
                l2.add(o2);
            }
            l1.remove(i1--);
            PageDirty();
        }
    }
}


function DoCmd(action, cmd, cmd_data) {
          
     if(navigator.appName != "Netscape") {
        document.all["cmd"].value =cmd;
        document.all["cmd_data"].value = cmd_data;
        document.all.tags("form").item("Cmd_Form").action = action;
        document.all["cmd"].click();
     }
    else{
           
            var el1= document.getElementById("cmd_data");
            el1.value=cmd_data;
            var el1 = document.getElementById("Cmd_Form");
            el1.action=action;
            var el1= document.getElementById("cmd");
            el1.value=cmd;
            el1.click();
           // document.forms["Cmd_Form"].submit();

   }
 
}

function min2text(m) {
    return ((m - m % 60) / 60).toString() + ":" + ((100 + m % 60).toString()).substr(1);
}


function setStatus(m) {
    window.status = m;
}

function breakPoint(m){
   return m;
 }

  

