<?php

/**
 * Generally used functions/constants related to html popups
 *
 * @package Cubit
 * @subpackage XPopup
 */
if (!defined("XPOPUP_LIB")) {
    define("XPOPUP_LIB", true);
    /**
     * some of these functions are closely interlinked with the ajax functions,
     * so take caution when changing some things.
     *
     * such pieces are show with "///" comments
     */
    /**
     * determine how to get to dateselect.php
     */
    $dateselect = relpath("dateselect.php");
    /**
     * XPopup javascript and a function to display data selection popup
     *
     */
    global $JS_XPOPUP;
    addglobals("JS_XPOPUP");
    $JS_XPOPUP = "\n\t<script type=\"application/x-javascript\">\n\t\tdocument.onmousemove = getMouseXY;\n\n\t\tfunction findPosXY(obj) {\n\t\t\tobj.x = findPosX;\n\t\t\tobj.y = findPosY;\n\t\t\treturn true;\n\t\t}\n\n\t\tfunction findPosX(eElement) {\n\t\t\tif (!eElement && this) {\n\t\t\t\teElement = this;\n\t\t\t}\n\n\t\t\tvar DL_bIE = document.all ? true : false;\n\n\t\t\tvar nLeftPos = eElement.offsetLeft;\n\t\t\tvar eParElement = eElement.offsetParent;\n\n\t\t\twhile (eParElement != null) {\n\t\t\t\tif(DL_bIE) {\n\t\t\t\t\tif( (eParElement.tagName != 'TABLE') && (eParElement.tagName != 'BODY') ) {\n\t\t\t\t\tnLeftPos += eParElement.clientLeft;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif(eParElement.tagName == 'TABLE') {\n\t\t\t\t\tvar nParBorder = parseInt(eParElement.border);\n\t\t\t\t\tif(isNaN(nParBorder)) {\n\t\t\t\t\t\tvar nParFrame = eParElement.getAttribute('frame');\n\t\t\t\t\t\tif(nParFrame != null) {\n\t\t\t\t\t\t\tnLeftPos += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if(nParBorder > 0) {\n\t\t\t\t\t\tnLeftPos += nParBorder;\n\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnLeftPos += eParElement.offsetLeft;\n\t\t\t\teParElement = eParElement.offsetParent;\n\t\t\t}\n\t\t\treturn nLeftPos;\n\t\t}\n\n\t\tfunction findPosY(eElement) {\n\t\t\tif (!eElement && this) {\n\t\t\t\teElement = this;\n\t\t\t}\n\n\t\t\tvar DL_bIE = document.all ? true : false;\n\n\t\t\tvar nTopPos = eElement.offsetTop;\n\t\t\tvar eParElement = eElement.offsetParent;\n\n\t\t\twhile (eParElement != null) {\n\t\t\t\tif(DL_bIE) {\n\t\t\t\t\tif( (eParElement.tagName != 'TABLE') && (eParElement.tagName != 'BODY') ) {\n\t\t\t\t\tnTopPos += eParElement.clientTop;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif(eParElement.tagName == 'TABLE') {\n\t\t\t\t\tvar nParBorder = parseInt(eParElement.border);\n\t\t\t\t\tif(isNaN(nParBorder)) {\n\t\t\t\t\t\tvar nParFrame = eParElement.getAttribute('frame');\n\t\t\t\t\t\tif(nParFrame != null) {\n\t\t\t\t\t\t\tnTopPos += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if(nParBorder > 0) {\n\t\t\t\t\t\tnTopPos += nParBorder;\n\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tnTopPos += eParElement.offsetTop;\n\t\t\t\teParElement = eParElement.offsetParent;\n\t\t\t}\n\t\t\treturn nTopPos;\n\t\t}\n\n\t\t// Temporary variables to hold mouse x-y pos.s\n\t\tvar mouseX = 0\n\t\tvar mouseY = 0\n\n\t\t// Main function to retrieve mouse x-y pos.s\n\t\tfunction getMouseXY(e) {\n\t\t\tmouseX = e.pageX;\n\t\t\tmouseY = e.pageY;\n\n\t\t\t// catch possible negative values in NS4\n\t\t\tif (mouseX < 0) {\n\t\t\t\tmouseX = 0;\n\t\t\t}\n\t\t\tif (mouseY < 0) {\n\t\t\t\tmouseY = 0;\n\t\t\t}\n\n\t\t\tmoveXLayerAct();\n\n\t\t\treturn true;\n\t\t}\n\n\t\t/**\n\t\t * register/unregister mouse movement\n\t\t */\n\t\tvar movingXLayer = false;\n\t\tvar initposXMouse = 0;\n\t\tvar initposYMouse = 0;\n\t\tvar initposXLayer = 0;\n\t\tvar initposYLayer = 0;\n\t\tvar moveXNoAction = true;\n\n\t\tfunction moveXLayer(status) {\n\t\t\tif (movingXLayer = status) {\n\t\t\t\tinitposXMouse = mouseX;\n\t\t\t\tinitposYMouse = mouseY;\n\n\t\t\t\tlayer = getObject(XPopupLayer());\n\n\t\t\t\tfindPosXY(layer);\n\t\t\t\tinitposXLayer = layer.x();\n\t\t\t\tinitposYLayer = layer.y();\n\n\t\t\t\tmoveXNoAction = true;\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * actual layer moving with mouse\n\t\t */\n\t\tfunction moveXLayerAct() {\n\t\t\tif (!movingXLayer) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlayer = getObject(XPopupLayer());\n\t\t\tpropTop = initposYLayer - (initposYMouse - mouseY);\n\t\t\tpropLeft = initposXLayer - (initposXMouse - mouseX);\n\n\t\t\tmoveXNoAction = !moveXNoAction;\n\t\t\tif (moveXNoAction) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (propTop < 0) {\n\t\t\t\tpropTop = 0;\n\t\t\t}\n\n\t\t\tif (propLeft < 0) {\n\t\t\t\tpropLeft = 0;\n\t\t\t}\n\n\t\t\tif ((toomuch = (propTop + xp.offsetHeight) - window.innerHeight) > 0) {\n\t\t\t\tpropTop -= toomuch;\n\t\t\t}\n\n\t\t\tif ((toomuch = (propLeft + xp.offsetWidth) - window.innerWidth) > 0) {\n\t\t\t\tpropLeft -= toomuch;\n\t\t\t}\n\n\t\t\t// now set the position\n\t\t\txp.style.top = propTop;\n\t\t\txp.style.left = propLeft;\n\t\t}\n\n\t\t/**\n\t\t * returns the layer XPopup uses's name\n\t\t */\n\t\tfunction XPopupLayer() {\n\t\t\treturn 'x_popup';\n\t\t}\n\n\t\t// obj = object we are moving over, content is html to fill it with\n\t\tXPopupHideTimer = false;\n\t\tXPopupActive = false;\n\t\tXPopupObject = false;\n\t\tXPopupContent = false;\n\t\tXPopupShowTimer = false;\n\t\tXPopupDuration = false;\n\t\tXPopupShowClose = false;\n\t\tfunction XPopupShow(content, object) {\n\t\t\tXPopupNoHide();\n\n\t\t\tif (XPopupShowTimer == false && XPopupContent != content) {\n\t\t\t\tXPopupContent = content;\n\t\t\t\tif (object) {\n\t\t\t\t\tXPopupObject = object;\n\t\t\t\t}\n\t\t\t\tXPopupShowTimer = setTimeout('XPopupShowAct()', 50);\n\t\t\t} else if (XPopupContent != content) {\n\t\t\t\tclearTimeout(XPopupShowTimer);\n\t\t\t\tXPopupShowTimer = false;\n\t\t\t}\n\t\t}\n\n\t\tfunction XPopupShowAct() {\n\t\t\tif (XPopupActive != false) return;\n\n\t\t\txp = document.getElementById('x_popup');\n\n\t\t\t/// XPopupContent is set to null in the dateSelPopup() function\n\t\t\t/// called through ajax to make date selection popups.\n\t\t\t/// if changing this expression to match something other than\n\t\t\t/// null, remember to change the value dateSelPopup() passes\n\t\t\t/// aswell\n\t\t\tif (XPopupContent != null) {\n\t\t\t\txp.innerHTML =\n\t\t\t\t\t'<table bgcolor=\"#fdeb89\" style=\"border: 1px dashed black\">'\n\t\t\t\t\t+'<tr><td align=\"right\">[<a href=\"javascript: XPopupHideAct()\">Close</a>]</td></tr>'\n\t\t\t\t\t+'<tr><td>'\n\t\t\t\t\t\t+ XPopupContent +\n\t\t\t\t\t'</td></tr>'\n\t\t\t\t\t+'</table>';\n\t\t\t}\n\n\t\t\tif (XPopupObject) {\n\t\t\t\t// get the object we clicked on\n\t\t\t\to = XPopupObject;\n\t\t\t\tfindPosXY(o);\n\n\t\t\t\t// calculate a position where popup will be 100% visible\n\t\t\t\tpropTop = o.y() + o.offsetHeight;\n\t\t\t\tpropLeft = o.x();\n\t\t\t} else {\n\t\t\t\tpropTop = mouseY - (xp.offsetHeight / 2);\n\t\t\t\tpropLeft = mouseX;\n\t\t\t}\n\n\t\t\tif ((toomuch = (propTop + xp.offsetHeight) - (window.innerHeight + window.pageYOffset)) > 0) {\n\t\t\t\tpropTop -= toomuch;\n\t\t\t}\n\n\t\t\tif ((toomuch = (propLeft + xp.offsetWidth) - (window.innerWidth + window.pageXOffset)) > 0) {\n\t\t\t\tpropLeft -= toomuch + 15;\n\t\t\t}\n\n\t\t\tif (propTop < 0) {\n\t\t\t\tpropTop = 0;\n\t\t\t}\n\n\t\t\tif (propLeft < 0) {\n\t\t\t\tpropLeft = 0;\n\t\t\t}\n\n\t\t\t// now set the position\n\t\t\txp.style.top = propTop;\n\t\t\txp.style.left = propLeft;\n\n\t\t\t// max width/height\n\t\t\tif (xp.offsetWidth > 300) {\n\t\t\t\txp.style.width = 300;\n\t\t\t}\n\n\t\t\txp.style.visibility = 'visible';\n\t\t\tXPopupShowTimer = false;\n\t\t}\n\n\t\tfunction XPopupHide() {\n\t\t\tif (XPopupHideTimer == false) {\n\t\t\t\tXPopupHideTimer = setTimeout('XPopupHideAct()', 500);\n\t\t\t}\n\t\t}\n\n\t\tfunction XPopupNoHide() {\n\t\t\tif (XPopupHideTimer != false) {\n\t\t\t\tclearTimeout(XPopupHideTimer);\n\t\t\t\tXPopupHideTimer = false;\n\t\t\t}\n\t\t}\n\n\t\tfunction XPopupHideAct() {\n\t\t\txp = document.getElementById('x_popup');\n\t\t\txp.style.visibility = 'hidden';\n\t\t\txp.innerHTML = '';\n\n\t\t\tXPopupHideTimer = false;\n\t\t\tXPopupActive = false;\n\t\t\tXPopupContent = false;\n\t\t\tXPopupObject = false;\n\n\t\t\tif (XPopupShowTimer != false) {\n\t\t\t\tclearTimeout(XPopupShowTimer);\n\t\t\t\tXPopupShowTimer = false;\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * initiates the date selection popup\n\t\t */\n\t\tdateSelPopup_updateKeys = null;\n\t\tfunction dateSelPopup(idpfx, GWPP, arraykeys) {\n\t\t\tif (arraykeys) {\n\t\t\t\tdateSelPopup_updateKeys = arraykeys.split(',');\n\t\t\t\tv1 = dateSelPopup_updateKeys[0];\n\n\t\t\t\t/* get date field values */\n\t\t\t\tf_day = getObject(idpfx + '[' + v1 + ']_day');\n\t\t\t\tf_month = getObject(idpfx + '[' + v1 + ']_month');\n\t\t\t\tf_year = getObject(idpfx + '[' + v1 + ']_year');\n\t\t\t} else {\n\t\t\t\tdateSelPopup_updateKeys = null;\n\n\t\t\t\t/* get date field values */\n\t\t\t\tf_day = getObject(idpfx + '_day');\n\t\t\t\tf_month = getObject(idpfx + '_month');\n\t\t\t\tf_year = getObject(idpfx + '_year');\n\t\t\t}\n\n\t\t\t/* set default popup date values */\n\t\t\tval_day = (f_day && f_day.value == '') ? '" . date("d") . "' : f_day.value;\n\t\t\tval_month = (f_month && f_month.value == '') ? '" . date("m") . "' : f_month.value;\n\t\t\tval_year = (f_year && f_year.value == '') ? '" . date("Y") . "' : f_year.value;\n\n\t\t\t/* build get */\n\t\t\tget = 'date_selection=t'\n\t\t\t\t+ '&GWPP=' + GWPP\n\t\t\t\t+ '&idprefix=' + idpfx\n\t\t\t\t+ '&day=' + val_day\n\t\t\t\t+ '&month=' + val_month\n\t\t\t\t+ '&year=' + val_year\n\n\t\t\t/* do the request */\n\t\t\tajaxRequest('{$dateselect}', XPopupLayer(), AJAX_SET | AJAX_EXE, get,\n\t\t\t\tdateSelPopupAct);\n\t\t}\n\n\t\t/**\n\t\t * updates the date selection popup to specified month. popup\n\t\t * uses this to move to previous/next month/year.\n\t\t */\n\t\tfunction dateSelMove(idpfx, day, month, year, sday, smonth, syear, GWPP) {\n\t\t\tget = 'date_selection=t'\n\t\t\t\t+ '&GWPP=' + GWPP\n\t\t\t\t+ '&idprefix=' + idpfx\n\t\t\t\t+ '&day=' + day\n\t\t\t\t+ '&month=' + month\n\t\t\t\t+ '&year=' + year\n\t\t\t\t+ '&sday=' + sday\n\t\t\t\t+ '&smonth=' + smonth\n\t\t\t\t+ '&syear=' + syear;\n\n\t\t\tajaxRequest('{$dateselect}', XPopupLayer(), AJAX_SET, get);\n\t\t}\n\n\t\t/**\n\t\t * updates the date selection popup to month/year selected by\n\t\t * the dropdowns\n\t\t */\n\t\tfunction dateSelMoveBySelect(idpfx, day, sday, smonth, syear, GWPP) {\n\t\t\tdocument.getElementById('datesel_loading').style.height = '200px';\n\t\t\tdocument.getElementById('datesel_loading').style.visibility = 'visible';\n\t\t\tdocument.getElementById('datesel_calender').style.visibility = 'hidden';\n\n\t\t\tmon = getObject('datesel_move_month').value;\n\t\t\tyear = getObject('datesel_move_year').value;\n\n\t\t\tdateSelMove(idpfx, day, mon, year, sday, smonth, syear, GWPP);\n\t\t}\n\n\t\t/**\n\t\t * function to popup the date selection\n\t\t */\n\t\t/// this is specified as exec function in ajax call with object to\n\t\t/// position with as 6th parameter.\n\t\t/// null is specified as popup text so XPopupShowAct() doesn't\n\t\t/// update the contents.\n\t\tfunction dateSelPopupAct(layer) {\n\t\t\tXPopupShow(null);\n\t\t}\n\n\t\t/**\n\t\t * updates the forms with newly selected dates\n\t\t *\n\t\t * @param string idpfx form field prefix\n\t\t * @param int day\n\t\t * @param int month\n\t\t * @param int year\n\t\t */\n\t\tfunction dateSelUpdate(idpfx, day, month, year, textfield) {\n\t\t\tXPopupHideAct();\n\n\t\t\tif (dateSelPopup_updateKeys) {\n\t\t\t\tfor (i = 0; i < dateSelPopup_updateKeys.length; ++i) {\n\t\t\t\t\tv = dateSelPopup_updateKeys[i];\n\n\t\t\t\t\tf_day = getObject(idpfx + '[' + v + ']_day');\n\t\t\t\t\tf_month = getObject(idpfx + '[' + v + ']_month');\n\t\t\t\t\tf_year = getObject(idpfx + '[' + v + ']_year');\n\n\t\t\t\t\tf_day.value = day;\n\t\t\t\t\tf_month.value = month;\n\t\t\t\t\tf_year.value = year;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tf_day = getObject(idpfx + '_day');\n\t\t\t\tf_month = getObject(idpfx + '_month');\n\t\t\t\tf_year = getObject(idpfx + '_year');\n\n\t\t\t\tf_day.value = day;\n\t\t\t\tf_month.value = month;\n\t\t\t\tf_year.value = year;\n\t\t\t}\n\t\t}\n\t</script>";
}
/* LIB END */
     * this function is automatically called by template.php so simply requiring
     * template.php in all cases (AJAX and NOT) will give desired effects.
     *
     * the AJAX constant (settings.php) is used to determine whether or not
     * the output is requested with AJAX or not
     *
     * @see AJAX
     * @param string $OUT html output
     */
    function AJAX_OUT($OUT)
    {
        if (AJAX) {
            header("Content-Type: application/xml");
            print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
            print "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
            print $OUT;
            print "</div>";
            exit;
        }
    }
    global $JS_AJAX;
    addglobals("JS_AJAX");
    /**
     * Ajax requests: javascript functionality.
     *
     * include this variable in your output if you need to make AJAX requests.
     * if you are simply returning output requested by AJAX this is not necesary.
     */
    $JS_AJAX = "\n<script type=\"application/x-javascript\">\nvar AJAX_SET = 1;\nvar AJAX_ADD = 2;\nvar AJAX_OBJ = 4;\nvar AJAX_CLS = 8;\nvar AJAX_EXE = 16;\n\nvar AJAX_RSPTXT = '__ajax_resp';\n\n// args:\n// 1: page\n// 2: layername\n// 3: action (above)\n// 4: gets vars (optional)\n// 5: exec function\n// 6: exec function arg1\n// 7: exec function arg2\n// 8: exec function arg3\nfunction ajaxRequest() {\n\tvar argv = ajaxRequest.arguments;\n\n\tif (document.getElementById) {\n\t\tvar x = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();\n\t}\n\n\tif (x) {\n\t\tx.reqLayer = argv[1];\n\t\tx.reqWindow = window;\n\t\tx.reqAction = argv[2];\n\t\tif (argv[4]) x.reqFunction = argv[4];\n\t\tif (argv[5]) x.reqFuncARG1 = argv[5];\n\t\tif (argv[6]) x.reqFuncARG2 = argv[6];\n\t\tif (argv[7]) x.reqFuncARG3 = argv[7];\n\t\tx.onreadystatechange = function() {\n\t\t\tif (x.readyState == 4 && x.status == 200) {\n\t\t\t\tif (x.reqAction & AJAX_ADD) {\n\t\t\t\t\tajaxLayerAdd(x.reqLayer, x.responseText);\n\t\t\t\t}\n\n\t\t\t\tif (x.reqAction & AJAX_OBJ) {\n\t\t\t\t\tajaxObjLayerSet(x.reqLayer, x.responseText);\n\t\t\t\t}\n\n\t\t\t\tif (x.reqAction & AJAX_SET) {\n\t\t\t\t\tajaxLayerSet(x.reqLayer, x.responseText);\n\t\t\t\t}\n\n\t\t\t\tif (x.reqAction & AJAX_CLS) {\n\t\t\t\t\tx.reqWindow.close();\n\t\t\t\t}\n\n\t\t\t\tif (x.reqAction & AJAX_EXE) {\n\t\t\t\t\tif (x.reqFuncARG1 == AJAX_RSPTXT) {\n\t\t\t\t\t\tx.reqFuncARG1 = x.responseText;\n\t\t\t\t\t}\n\t\t\t\t\tx.reqFunction(x.reqFuncARG1, x.reqFuncARG2, x.reqFuncARG3);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (argv[3]) {\n\t\t\turl = argv[0] + '?' + argv[3] + '&AJAX=true';\n\t\t} else {\n\t\t\turl = argv[0] + '?AJAX=true';\n\t\t}\n\t\tx.open(\"GET\", url, true);\n\t\tx.send(null);\n\t} else {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nfunction ajaxObjLayerSet(l, content) {\n\tl.innerHTML = content;\n}\n\nfunction ajaxLayerSet(l, content) {\n\tgetObj(l).innerHTML = content;\n}\n\nfunction ajaxLayerAdd(l, content) {\n\tlcontent = getObj(l).innerHTML;\n\tajaxLayerSet(l, lcontent + content);\n}\n\nfunction getObj(id) {\n\tif (document.all)\n\t\treturn document.all[id];\n\n\treturn document.getElementById(id);\n}\n</script>";
}
/* LIB END */
#
#
#
#
#
#
#
#
#
#
#
if (!defined("DOCMAN_LIB")) {
    define("DOCMAN_LIB", true);
    global $DOCLIB_DOCTYPES, $DOCLIB_DOCTYPESIN;
    addglobals("DOCLIB_DOCTYPES");
    addglobals("DOCLIB_DOCTYPESIN");
    $DOCLIB_DOCTYPES = array("inv" => "Invoice", "ninv" => "Non-Stock Invoice", "note" => "Credit Note", "prec" => "Petty Cash Receipt", "pur" => "Purchase", "ipur" => "International Purchase", "npur" => "Non-Stock purchase", "empl" => "Employee Document");
    # Document types Input
    $DOCLIB_DOCTYPESIN = array("inv" => "Invoice", "ninv" => "Non-Stock Invoice", "note" => "Credit Note", "prec" => "Petty Cash Receipt", "pur" => "Purchase", "ipur" => "International Purchase", "npur" => "Non-Stock purchase", "empl" => "Employee");
    //cubit_autoglobal("DOCLIB_DOCTYPES");
    //cubit_autoglobal("DOCLIB_DOCTYPESIN");
    # Extra Input
    function xin($typeid, $xin = "")
    {
        global $DOCLIB_DOCTYPESIN;
        if (!preg_match("/\\d/", $typeid)) {
            return "<tr class='bg-odd'><td>{$DOCLIB_DOCTYPESIN[$typeid]} No.</td>\n\t\t<td><input type=text size=4 name=xin value='{$xin}'></td></tr>";
        }
    }
    function xin_gw($typeid, $xin = "")
    {
}
/* check if this is an ajax query and set constant as needed */
if (isset($_POST["AJAX"]) || isset($_GET["AJAX"])) {
    if (isset($_POST["AJAX"])) {
        unset($_POST["AJAX"]);
    }
    if (isset($_GET["AJAX"])) {
        unset($_GET["AJAX"]);
    }
    define("AJAX", true);
} else {
    define("AJAX", false);
}
/* popup messages options */
define("MSGS_CHECKTIME", 15000);
addglobals("MSGS_NOALERT");
$MSGS_NOALERT = array("view_req.php");
// Seed random number generator & get md5 of random number
srand((double) microtime() * 1000000);
define("RAND_NO", rand());
define("RAND_MD5", md5(RAND_NO));
// work days in month
define("WORK_DAYS_MONTH", "27.25");
// items to show in lists (with offsets/limits this is the limit)
// old  // define("SHOW_LIMIT", 250);
define("SHOW_LIMIT", 100);
// Account type settings
define("MIN_INC", "1");
define("MAX_INC", "1999");
define("MIN_EXP", "2000");
define("MAX_EXP", "4999");