if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; } } function getRequest() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } } var wndprocess = null; var lastpos = null; function posSelect(e) { if (lastpos) lastpos.style.backgroundColor = ""; e.style.backgroundColor = "yellow"; lastpos = e; el("position").value = e.id; } /** silent */ function performnm(cmd, params, parent) { perform(cmd, params, parent, true); } function perform(cmd, params, parent, silent) { if (silent === true); else silent = false; var r = getRequest(); if (parent) r.parent = parent; var post = "async=" + cmd; if (!params) ; else { var i = -1; for (i = 0; i < params.length; i++) { var p = params[i]; if (typeof p == "string") { post += "&" + p; } else if (typeof p == "object") { if (!p.items) p.items = "*"; var e = p.src; if (typeof e == "string") e = el(e); if (!e) continue; post += pf(e, p.items); } } } r.open("POST", location.href, true); r.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); if (wndprocess == null) { var d = document.createElement("div"); d.id = "dlg_" + (++did); d.isNew = true; d.innerHTML = "
Processing...
"; d.style.position = "absolute"; d.className = "wnd"; d.style.display = "none"; d.style.left = document.body.scrollLeft + (document.body.clientWidth - 200) / 2; d.style.top = document.body.scrollTop + (document.body.clientHeight - 200) / 2; d.style.width = "200px"; d.modal = true; d.noremove = true; document.body.appendChild(d); wndprocess = d; } else { wndprocess.isNew = true; } r.silent = silent; if (!silent) openWnd(wndprocess, 300); r.onreadystatechange = function() { try { if (r.readyState == 4) { if (!r.silent) closeWnd(); if (r.status == 200) { if (r.getResponseHeader("Content-Type").match(/^application\/json/)) { processJSON(r); } else throw "Unexpected response " + r.responseText; } else { throw "Error " + r.status + " in request"; } } } catch (e) { showError(e); } }; r.send(post); } function showError(message) { var d = document.createElement("div"); d.id = "dlg_" + (++did); d.innerHTML = "
" + message + "
"; d.style.position = "absolute"; d.className = "wnd"; d.style.overflow = "auto"; d.style.padding = "2px"; d.style.borderWidth = 1; d.style.borderColor = "red"; d.style.borderStyle = "solid"; //d.style.width = "200px"; d.style.maxWidth = "50%"; d.modal = true; d.isNew = true; document.body.appendChild(d); openWnd(d); d.style.left = document.body.scrollLeft + (document.body.clientWidth - d.offsetWidth) / 2; d.style.top = document.body.scrollTop + (document.body.clientHeight - d.offsetHeight) / 2; el("btnc_" + did).focus(); } var wnds = []; var did = 0; var wndmodal = null; var tmrWnd = null; function openWnd(wnd, timeout) { if (wnd.modal && wnd.isNew) { modal(true, timeout ? 0 : 0.3); if (timeout) { if (tmrWnd) clearTimeout(tmrWnd); tmrWnd = setTimeout(function() { if (wndmodal) { wndmodal.style.opacity = 0.3; wndmodal.style.filter = "alpha(opacity=30)"; } wnd.isNew = false; wnd.style.display = 'block'; wnd.style.zIndex = wnds.length * 2 + 1; tmrWnd = null; }, timeout); wnds.push(wnd); return; } wnd.isNew = false; wnds.push(wnd); wnd.style.display = 'block'; wnd.style.zIndex = wnds.length * 2 + 1; } } function closeWnd(did) { if (tmrWnd) { clearTimeout(tmrWnd); tmrWnd = null; } var d = null; if (!did) { if (wnds.length > 0) d = wnds.pop(); } else { if (typeof did == "object") d = did; else d = el(did); var i = wnds.indexOf(d); if (i > -1) wnds.splice(i, 1); } modal(false); if (!d) return false; if (!d.noremove) d.parentElement.removeChild(d); else { d.style.display = 'none'; } return true; } function modal(show, opacity) { if (!show) { if (wndmodal == null) return; wndmodal.style.display = 'none'; for (i = wnds.length - 1; i >= 0; i--) { if (wnds[i].modal) { wndmodal.style.zIndex = i * 2 + 2; wndmodal.style.display = 'block'; break; } } return; } if (opacity === 0) ; else if (opacity) ; else opacity = 0.3; if (wndmodal == null) { var d = document.createElement("div"); d.style.position = "absolute"; d.style.backgroundColor = "#dddddd"; d.style.left = 0; d.style.top = 0; d.style.display = "block"; document.body.appendChild(d); wndmodal = d; } else d = wndmodal; d.style.opacity = opacity; d.style.filter = "alpha(opacity=" + (opacity * 100) + ")"; d.style.width = document.documentElement.offsetWidth; d.style.height = document.documentElement.offsetHeight; d.style.zIndex = wnds.length * 2 + 2; d.style.display = 'block'; } function processJSON(ar) { var json = ar.responseText; var js = ""; try { js = eval(json); } catch (e) { showError(e + "\n\n" + json); return; } if (!js.result) throw "Missing result"; if (js.result == "error") throw js.description; if (js.result != "OK") throw "Wrong result: " + js.result; if (!js.items) throw "Missing items"; var i = -1; for (i = 0; i < js.items.length; i++) { var item = js.items[i]; if (item.type == "download") { location.href = item.url; } else if (item.type == "value") { var e = el(item.name); if (!e) continue; e.value = item.value; } else if (item.type == "content") { var e = el(item.name); if (!e) continue; e.innerHTML = item.content; } else if (item.type == "exec") { eval(item.content); } else if (item.type == "popup") { var d = el(item.name); if (!d) { d = document.createElement("div"); d.isNew = true; d.className = "wnd"; } d.id = item.name; d.style.position = "absolute"; if (item.width) d.style.width = item.width; if (ar.parent) { var x; var y; if (item.left) x = item.left; else x = 0; if (item.top) y = item.top; else y = 0; item.left = x + getOffsetParentLeft(ar.parent); item.top = y + getOffsetParentTop(ar.parent) + ar.parent.offsetHeight; } if (item["class"]) d.className = item["class"]; if (item.bgcolor) d.style.backgroundColor = item.bgcolor; d.modal = true; d.innerHTML = item.content; if (item.modal) d.modal = true; document.body.appendChild(d); openWnd(d); if (item.left) { if (item.left == "center") { //if (item.width) d.style.left = document.body.scrollLeft + (document.body.clientWidth - d.offsetWidth) / 2; } else { d.style.left = item.left; } } if (item.height) d.style.height = item.height; if (item.top) { if (item.top == "center") { d.style.top = document.body.scrollTop + (document.body.clientHeight - d.offsetHeight) / 2; } else { d.style.top = item.top; } } if (d.offsetHeight + d.offsetTop - document.body.scrollTop > document.body.offsetHeight) { if (d.offsetTop - d.offsetHeight > 0) { d.style.top = d.offsetTop - d.offsetHeight; } } if (item.focus) { var e = el(item.focus); if (e) e.focus(); } } } } function async(url, post, complete) { var r = getRequest(); if (!post) post = ""; r.open("POST", url, true); r.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); r.onreadystatechange = function() { if (r.readyState == 4) { if (r.status == 200) { if (r.getResponseHeader("Content-Type").match(/^application\/json/)) { var js = eval(r.responseText); if (js.result == "error") { alert(js.description); return; } else if (js.result == "location") { location.href = js.location; return; } } else { alert("Unexpected response:\n\n" + r.responseText); } } } } r.send(post); } function eid(e) { return document.getElementById(e); } function f(container, field) { if (!container) container = document.body; else if (typeof container == "string") container = eid(container); if (!container) return ""; var i; for (i = 0; i < container.children.length; i++) { var c = container.children[i]; if (c.id != field) { var r = f(c, field); if (r) return r; continue; } if (c.tagName == "INPUT") return c.value; } } function escape(sz) { sz = encodeURIComponent(sz); return sz.replace(/\n/g, "%0A").replace(/&/g, "%26").replace(/\r/g,"").replace(/\+/g,"%2B").replace(/\t/g, "%09"); } function pf(container, fields) { var i; var result = ""; for (i = 0; i < container.children.length; i++) { var c = container.children[i]; if (fields == "*" || fields.indexOf(c.id) > -1) { if (c.tagName == "INPUT") { if (c.type == "checkbox" || c.type == "radio") { if (c.checked) result += "&" + c.id + "=" + 1; } else result += "&" + c.id + "=" + c.value; if (c.id == fields) return result; } else if (c.tagName == "SELECT") { if (!c.multiple) { result += "&" + c.id + "=" + c.value; } else { var j; for (j = 0; j < c.options.length; j++) { result += "&" + c.id + "[]=" + c.options[j].value; } } if (c.id == fields) return result; } else if (c.tagName == "TEXTAREA") { result += "&" + c.id + "=" + escape(c.value); } else result += pf(c, "*"); } else { result += pf(c, fields); } } return result; } function doRequest(url, e, postdata) { var r = getRequest(); r.url = url; r.postdata = postdata; r.parent = e; r.open("POST", url, false); if (postdata) { r.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); r.setRequestHeader("Content-length", postdata.length); r.send(postdata); } else r.send("-"); return processAjax(r); } function processAjax(r) { if (r.readyState != 4) return; r.onreadystatechange = null; var rtext = r.responseText; var stext = r.statusText; var status = r.status; if (status != 200) { alert(status + " / " + stext + "\n\n" + rtext.substring(0, 300)); return false; } if (rtext.substr(0, 15) == ''); if (i != -1) { var s = rtext.substring(15, i); rtext = rtext.substring(i + 5); i = s.search(","); if (i != -1) { status = s.substring(0, i); stext = s.substring(i + 1); } } var result = false; if (status == 446) { var cmd = stext.split(","); if (cmd[0] == "exec") { eval(rtext); result = true; } else if (cmd[0] == "popup") { var w = ""; if (cmd.length > 4) w = cmd[4]; var h = ""; if (cmd.length > 5) h = cmd[5]; var x = 0; var y = 0; var title = ""; if (cmd.length > 6) title = cmd[6]; if (r.parent) { x = getOffsetParentLeft(r.parent); y = getOffsetParentTop(r.parent) + r.parent.offsetHeight; } else { if (cmd.length > 2) x = document.body.scrollLeft + cmd[2] * 1; if (cmd.length > 3) y = document.body.scrollTop + cmd[3] * 1; } if (rtext.length == 0) { hidepopup(cmd[1]); result = true; } else { var d = newDiv(cmd[1], x, y, w, h, title); setDivText(d, rtext); popupdiv(d, true); if (!x) d.style.left = document.body.scrollLeft + (document.body.clientWidth - d.offsetWidth) / 2; if (!y) d.style.top = document.body.scrollTop + (document.body.clientHeight - d.offsetHeight) / 2; if (d.offsetHeight + d.offsetTop > document.body.offsetHeight) { if (d.offsetTop - d.offsetHeight > 0) { d.style.top = d.offsetTop - d.offsetHeight; } } result = "popup"; } } else if (cmd[0] == "location") { location.href = cmd[1]; result = true; } else if (cmd[0] == "window") { if (!rtext) window.open(cmd[1]); else window.open(rtext); result = true; } else if (cmd[0] == "update") { var d = el(cmd[1]); if (!d) return false; d.innerHTML = rtext; result = true; } else if (cmd[0] == "updaterows") { var tbl = el(cmd[1]); if (!tbl) throw "Missing table: [" + cmd[1] + "]"; var temp = document.createElement("div"); temp.innerHTML = rtext; var tmptbl = temp.children[0]; if (tmptbl.tagName != "TABLE") { alert("Bad response: " + rtext); return false; } var newrows = temp.children[0].rows; var nrows = 0; if (tbl.tHead) nrows = tbl.tHead.rows.length; var lastdel = nrows; var i = 0; while (i < newrows.length) { if (newrows[i].cells.length == 0) { var j = 0; while (j < tbl.rows.length) { if (tbl.rows[j].id == newrows[i].id) { lastdel = j; tbl.deleteRow(tbl.rows[j].rowIndex); } else j++; } i++; } else { var editrow; if (tbl.rows.length == nrows) editrow = tbl.insertRow(nrows); else { if (lastdel >= tbl.rows.length) editrow = tbl.insertRow(tbl.rows.length); else editrow = tbl.rows[lastdel]; } //if (console) console.log("Add row: " + newrows[i].id); editrow.parentElement.insertBefore(newrows[i], editrow); if (editrow.cells.length == 0) tbl.deleteRow(editrow.rowIndex); } } if (cmd.length > 2) result = cmd[2]; else result = "updaterows"; } else { alert(rtext); result = false; } } else if (status == 444) { if (rtext == "submit") { docmd("refresh"); result = true; } else { alert(rtext); result = false; } } else if (status == 445) { if (confirm(rtext)) return doRequest(r.url + "&confirmed=1", r.debugresult, r.postdata); } else if (status == 448) { var r = eval(rtext); return r; } else if (status == 447) { var f = stext; var s; if (s = prompt(rtext, "")) { if (r.onend) { var url = {url: r.url + "&" + f + "=" + s, post: r.postdata}; asyncRequest("", url, r.onend); } else return doRequest(r.url + "&" + f + "=" + s, r.debugresult, r.postdata); } result = false; } else { alert("Error processing request to server: [" + status + "] " + stext + "\n" + rtext); result = false; } if (result !== false) { if (r.onend) { if (typeof r.onend == "string") eval(r.onend); else r.onend(r); } } return result; } else { if (r.place) { var pos = el(r.place); if (pos) pos.innerHTML = r.responseText; } if (r.onend) { if (typeof r.onend == "string") eval(r.onend); else r.onend(r); } return rtext; } } function getOffsetParentTop(e) { if (offsetParent(e)) { return e.offsetTop - e.scrollTop + getOffsetParentTop(offsetParent(e)); } return e.offsetTop - e.scrollTop; } function getOffsetParentLeft(e) { if (offsetParent(e)) return e.offsetLeft - e.scrollLeft + getOffsetParentLeft(offsetParent(e)); return e.offsetLeft - e.scrollLeft; } function offsetParent(e) { try { return e.offsetParent; } catch (ex) { return document.body; } } function newDiv(id, x, y, w, h, title) { var d; if ((d = el(id)) != null) { } else { d = document.createElement("div"); d.clearOnExit = true; d.id = id; document.forms["frm"].appendChild(d); d.className = "popup"; d.style.display = "none"; if (title) { var c = document.createElement("table"); d.appendChild(c); c.style.cursor = "pointer"; c.border = 0; c.cellPadding = 2; c.cellSpacing = 0; d.head = c; c.style.border = "1px solid whitesmoke"; c.style.backgroundColor = "#0A246A"; var r = c.insertRow(0); var c = r.insertCell(0); c.style.color = "white"; c.style.fontWeight = "bold"; c.innerHTML = title; var c = r.insertCell(1); c.style.marginBottom = "2px"; c.style.cursor = "pointer"; c.style.height = "20px"; c.style.textAlign = "right"; c.innerHTML = ""; c = document.createElement("div"); d.appendChild(c); d.content = c; } } d.style.left = x; d.style.top = y; d.style.width = w; if (d.content) d.content.style.height = h; else d.style.height = h; return d; } function setDivText(e, t) { var p; if (e.content) p = e.content; else p = e; p.innerHTML = t; if (e.head) { setTimeout(function() { e.head.style.width = e.head.parentElement.clientWidth - 4; }, 1); } } function popupdiv(place, dopop, where) { p = place; if (typeof(p) == "string") { p = document.getElementById(p); if (!p) { alert(place + " not found"); return; } } if ((p.style.display == 'none') || (dopop)) { p.style.display = 'block'; addPopup(p); if (where) { p.style.left = getOffsetParentLeft2(p, where); p.style.top = getOffsetParentTop2(p, where) + where.offsetHeight; } } else { removePopup(p); p.style.display = 'none'; } } function removePopup(popup) { var newpopups = new Array(); for (i = 0; i < popups.length; i++) { if (popups[i] != popup) newpopups[newpopups.length] = popups[i]; } popups = newpopups; if ((popup.parentElement) && (popup.clearOnExit)) popup.parentElement.removeChild(popup); } function addPopup(popup) { for (i = 0; i < popups.length; i++) { if (popups[i] == popup) return; } if (popups.length == 0) popup.style.zIndex = 5; else popup.style.zIndex = popups[popups.length - 1].style.zIndex + 1; popups[popups.length] = popup; } function hidepopup(clear) { if (popups.length) { var place = popups[popups.length - 1]; if ((clear) || (place.clearOnExit)) place.innerHTML = ""; popupdiv(popups[popups.length - 1], false); cancelbubble(); return true; } return false; } popups = new Array(); function cancelbubble() { if (event) if (event.preventDefault) event.preventDefault(); else event.returnValue = false; if (window.event) window.event.cancelBubble = true; } function listMove(src, dst, order) { var i = 0; if (!order) order = false; if (typeof src == "string") src = el(src); if (typeof dst == "string") dst = el(dst); if (!src || !dst) return; var list = []; for (i = 0; i < dst.options.length; i++) list.push(dst.options[i]); dst.options.length = 0; var i = 0; while (i < src.options.length) { var o = src.options[i]; if (o.selected) { if (!o.order && o.getAttribute("order")) o.order = o.getAttribute("order"); var no = document.createElement("OPTION"); copyAttribs(o, no); no.value = o.value; no.text = o.text; no.order = o.order; no.selected = true; var found = false; if (order) { var di; for (di = 0; di < list.length; di++) { var oo = list[di]; if (!oo.order && oo.getAttribute("order")) oo.order = oo.getAttribute("order"); if (oo.order * 1 > no.order * 1) { list.splice(di, 0, no); src.remove(i); found = true; break; } } } if (!found) { list.push(no); src.remove(i); } } else { i++; } } dst.options.length = list.length; for (i = 0; i < list.length; i++) dst.options[i] = list[i]; } function copyAttribs(src, dst) { for (var i = 0; i < src.attributes.length; i++) { var a = src.attributes[i]; dst.setAttribute(a.name, a.value); } } function popupMenu(mnu, event) { var mnu = el(mnu); document.body.appendChild(mnu); mnu.style.left = event.x; mnu.style.top = event.y; mnu.style.display = "block"; mnu.noremove = true; var i = wnds.indexOf(mnu); if (i > -1) wnds.splice(i, 1); wnds.push(mnu); return mnu; } function checkAll(name, state, e) { if (!e) e = document.body; if (e.tagName == "INPUT") { if (e.type == "checkbox" && e.id && e.id.substr(0, name.length) == name) { e.checked = state; } return; } var i; for (i = 0; i < e.children.length; i++) { checkAll(name, state, e.children[i]); } }