PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/common.js

http://phreedom.googlecode.com/
JavaScript | 555 lines | 468 code | 51 blank | 36 comment | 125 complexity | 93381d080af120eb1a265d3bbe275821 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. // +-----------------------------------------------------------------+
  2. // | PhreeBooks Open Source ERP |
  3. // +-----------------------------------------------------------------+
  4. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  5. // | http://www.PhreeSoft.com |
  6. // +-----------------------------------------------------------------+
  7. // | This program is free software: you can redistribute it and/or |
  8. // | modify it under the terms of the GNU General Public License as |
  9. // | published by the Free Software Foundation, either version 3 of |
  10. // | the License, or any later version. |
  11. // | |
  12. // | This program is distributed in the hope that it will be useful, |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  15. // | GNU General Public License for more details. |
  16. // +-----------------------------------------------------------------+
  17. // Path: /includes/common.js
  18. //
  19. /******************************* General Functions ****************************************/
  20. function addLoadEvent(func) {
  21. var oldonload = window.onload;
  22. if (typeof window.onload != 'function') {
  23. window.onload = func;
  24. } else {
  25. window.onload = function() {
  26. if (oldonload) { oldonload(); }
  27. func();
  28. };
  29. }
  30. }
  31. function addUnloadEvent(func) {
  32. var oldonunload = window.onunload;
  33. if (typeof window.onunload != 'function') {
  34. window.onunload = func;
  35. } else {
  36. window.onunload = function() {
  37. if (oldonunload) { oldonunload(); }
  38. func();
  39. };
  40. }
  41. }
  42. // BOS - set up ajax session refresh timer to stay logged in if the browser is active
  43. var sessionClockID = 0; // start a session clock to stay logged in
  44. function refreshSessionClock() {
  45. if (sessionClockID) {
  46. window.clearTimeout(sessionClockID);
  47. $.ajax({
  48. type: "GET",
  49. url: 'index.php?module=phreedom&page=ajax&op=refresh_session'
  50. });
  51. }
  52. sessionClockID = window.setTimeout("refreshSessionClock()", 300000); // set to 5 minutes
  53. }
  54. function clearSessionClock() {
  55. if (sessionClockID) {
  56. window.clearTimeout(sessionClockID);
  57. sessionClockID = 0;
  58. }
  59. }
  60. // EOS - set up ajax session refresh timer to stay logged in if the browser is active
  61. function submit_wait() {
  62. document.getElementById("wait_msg").style.display = "block";
  63. return true; // allow form to submit
  64. }
  65. function clearField(field_name, text_value) {
  66. if (document.getElementById(field_name).value == text_value) {
  67. document.getElementById(field_name).style.color = '';
  68. document.getElementById(field_name).value = '';
  69. }
  70. }
  71. function setField(field_name, text_value) {
  72. if (document.getElementById(field_name).value == '' || document.getElementById(field_name).value == text_value) {
  73. document.getElementById(field_name).style.color = inactive_text_color;
  74. document.getElementById(field_name).value = text_value;
  75. } else {
  76. document.getElementById(field_name).style.color = '';
  77. }
  78. }
  79. function activeField(field, text_value) {
  80. if (field.value == text_value) {
  81. field.style.color = '';
  82. field.value = '';
  83. }
  84. }
  85. function inactiveField(field, text_value) {
  86. if (field.value == '' || field.value == text_value) {
  87. field.style.color = inactive_text_color;
  88. field.value = text_value;
  89. } else {
  90. field.style.color = '';
  91. }
  92. }
  93. function removeElement(parentDiv, childDiv) {
  94. if (childDiv == parentDiv) {
  95. return false;
  96. } else if (document.getElementById(childDiv)) {
  97. var child = document.getElementById(childDiv);
  98. var parent = document.getElementById(parentDiv);
  99. parent.removeChild(child);
  100. } else {
  101. return false;
  102. }
  103. }
  104. function insertValue(dVal, value) {
  105. if (!document.getElementById(dVal)) return;
  106. if (document.getElementById(dVal) && value) {
  107. document.getElementById(dVal).value = value;
  108. document.getElementById(dVal).style.color = '';
  109. } else {
  110. document.getElementById(dVal).value = '';
  111. }
  112. }
  113. function setCheckedValue(radioObj, newValue) {
  114. if (!radioObj) return;
  115. var radioLength = radioObj.length;
  116. if (radioLength == undefined) {
  117. radioObj.checked = (radioObj.value == newValue.toString());
  118. return;
  119. }
  120. for (var i = 0; i < radioLength; i++) {
  121. radioObj[i].checked = false;
  122. if(radioObj[i].value == newValue.toString()) {
  123. radioObj[i].checked = true;
  124. }
  125. }
  126. }
  127. // Numeric functions
  128. function d2h(d, padding) {
  129. if (isNaN(d)) d = parseInt(d);
  130. var hex = Number(d).toString(16);
  131. padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
  132. while (hex.length < padding) hex = "0" + hex;
  133. return hex;
  134. }
  135. function h2d(h) {
  136. return parseInt(h, 16);
  137. }
  138. // date functions
  139. function cleanDate(sDate) { // converts date from locale to mysql friendly yyyy-mm-dd
  140. var tmpArray = new Array();
  141. var keys = date_format.split(date_delimiter);
  142. var parts = sDate.split(date_delimiter);
  143. for (var i=0; i<keys.length; i++) {
  144. switch (keys[i]) {
  145. case 'Y': tmpArray[0] = parts[i]; break;
  146. case 'm': tmpArray[1] = parts[i]; break;
  147. case 'd': tmpArray[2] = parts[i]; break;
  148. }
  149. }
  150. return tmpArray.join('-');
  151. }
  152. function formatDate(sDate) { // converts date from mysql friendly yyyy-mm-dd to locale specific
  153. var tmpArray = new Array();
  154. var keys = date_format.split(date_delimiter);
  155. var parts = sDate.split('-');
  156. for (var i=0; i<keys.length; i++) {
  157. switch (keys[i]) {
  158. case 'Y': tmpArray[i] = parts[0]; break;
  159. case 'm': tmpArray[i] = parts[1]; break;
  160. case 'd': tmpArray[i] = parts[2]; break;
  161. }
  162. }
  163. return tmpArray.join(date_delimiter);
  164. }
  165. // Currency translation functions
  166. function cleanCurrency(amount) {
  167. amount = amount.replace(new RegExp("["+thousands_point+"]", "g"), '');
  168. amount = amount.replace(new RegExp("["+decimal_point+"]", "g"), '.');
  169. return amount;
  170. }
  171. function formatCurrency(amount) { // convert to expected currency format
  172. // amount needs to be a string type with thousands separator ',' and decimal point dot '.'
  173. var factor = Math.pow(10, decimal_places);
  174. var adj = Math.pow(10, (decimal_places+2)); // to fix rounding (i.e. .1499999999 rounding to 0.14 s/b 0.15)
  175. var numExpr = parseFloat(amount);
  176. if (isNaN(numExpr)) return amount;
  177. numExpr = Math.round((numExpr * factor) + (1/adj));
  178. var minus = (numExpr < 0) ? '-' : '';
  179. numExpr = Math.abs(numExpr);
  180. var decimal = (numExpr % factor).toString();
  181. while (decimal.length < decimal_places) decimal = '0' + decimal;
  182. var whole = Math.floor(numExpr / factor).toString();
  183. for (var i = 0; i < Math.floor((whole.length-(1+i))/3); i++)
  184. whole = whole.substring(0,whole.length-(4*i+3)) + thousands_point + whole.substring(whole.length-(4*i+3));
  185. if (decimal_places > 0) {
  186. return minus + whole + decimal_point + decimal;
  187. } else {
  188. return minus + whole;
  189. }
  190. }
  191. function formatPrecise(amount) { // convert to expected currency format with the additional precision
  192. // amount needs to be a string type with thousands separator ',' and decimal point dot '.'
  193. var factor = Math.pow(10, decimal_precise);
  194. var numExpr = parseFloat(amount);
  195. if (isNaN(numExpr)) return amount;
  196. numExpr = Math.round(numExpr * factor);
  197. var minus = (numExpr < 0) ? '-' : '';
  198. numExpr = Math.abs(numExpr);
  199. var decimal = (numExpr % factor).toString();
  200. while (decimal.length < decimal_precise) decimal = '0' + decimal;
  201. var whole = Math.floor(numExpr / factor).toString();
  202. for (var i = 0; i < Math.floor((whole.length-(1+i))/3); i++)
  203. whole = whole.substring(0,whole.length-(4*i+3)) + thousands_point + whole.substring(whole.length-(4*i+3));
  204. if (decimal_precise > 0) {
  205. return minus + whole + decimal_point + decimal;
  206. } else {
  207. return minus + whole;
  208. }
  209. }
  210. function AlertError(MethodName,e) {
  211. if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
  212. else { alert(MethodName + " Exception: " + e.description); }
  213. }
  214. // Chart functions
  215. chartProps = new Object();
  216. google.load('visualization', '1.0', {'packages':['corechart']});
  217. google.setOnLoadCallback(drawChart);
  218. function drawChart() {}
  219. function phreedomChart() {
  220. var modID = chartProps.modID;
  221. var func = chartProps.func;
  222. var d0 = chartProps.d0;
  223. $.ajax({
  224. type: "GET",
  225. url: 'index.php?module=phreedom&page=ajax&op=phreedom&action=chart&modID='+modID+'&fID='+func+'&d0='+d0,
  226. dataType: ($.browser.msie) ? "text" : "xml",
  227. error: function(XMLHttpRequest, textStatus, errorThrown) {
  228. alert ("Ajax Error: " + errorThrown + '-' + XMLHttpRequest.responseText + "\nStatus: " + textStatus);
  229. },
  230. success: phreedomChartResp
  231. });
  232. }
  233. function phreedomChartResp(sXml) {
  234. var xml = parseXml(sXml);
  235. if (!xml) return;
  236. var error = $(xml).find("error").text();
  237. if (error) { alert (error); }
  238. else { // activate the chart response
  239. $('#'+chartProps.divID).dialog("option", "title", $(xml).find("title").text());
  240. $('#'+chartProps.divID).dialog("option", "width", parseInt($(xml).find("width").text())+40);
  241. var data = new google.visualization.DataTable();
  242. data.addColumn('string', $(xml).find("label_text").text());
  243. data.addColumn('number', $(xml).find("value_text").text());
  244. var rowCnt = parseInt($(xml).find("rowCnt").text());
  245. var divID = document.getElementById(chartProps.divID);
  246. data.addRows(rowCnt);
  247. rowCnt = 0;
  248. $(xml).find("chartData").each(function() {
  249. data.setCell(rowCnt, 0, $(this).find("string").text());
  250. data.setCell(rowCnt, 1, parseFloat($(this).find("number").text()));
  251. rowCnt++;
  252. });
  253. var options = {'title':$(xml).find("title").text(), 'width':$(xml).find("width").text(), 'height':$(xml).find("height").text()};
  254. switch ($(xml).find("type").text()) {
  255. default:
  256. case 'pie': var chart = new google.visualization.PieChart(divID); break;
  257. case 'bar': var chart = new google.visualization.BarChart(divID); break;
  258. case 'column': var chart = new google.visualization.ColumnChart(divID); break;
  259. case 'guage': var chart = new google.visualization.Guage(divID); break;
  260. case 'line': var chart = new google.visualization.LineChart(divID); break;
  261. // case 'map': var chart = new google.visualization.Map(divID); break;
  262. }
  263. chart.draw(data, options);
  264. $('#'+chartProps.divID).dialog('open');
  265. }
  266. }
  267. // ******************** functions used for combo box scripting ***********************************
  268. var fActiveMenu = false;
  269. var oOverMenu = false;
  270. if (document.images) { //pre-load images
  271. img_on = new Image();
  272. img_on.src = combo_image_on;
  273. img_off = new Image();
  274. img_off.src = combo_image_off;
  275. }
  276. if (pbBrowser != 'IE') document.onmouseup = mouseSelect(0); // turns off the combo box if not hovering over
  277. function mouseSelect(e) {
  278. if (fActiveMenu) {
  279. if (oOverMenu == false) {
  280. oOverMenu = false;
  281. document.getElementById(fActiveMenu).style.display = "none";
  282. fActiveMenu = false;
  283. return false;
  284. }
  285. return false;
  286. }
  287. return true;
  288. }
  289. function dropDownData(id, text) {
  290. this.id = id;
  291. this.text = text;
  292. }
  293. function buildDropDown(selElement, data, defValue) {
  294. // build the dropdown
  295. for (var i=0; i<data.length; i++) {
  296. newOpt = document.createElement("option");
  297. newOpt.text = data[i].text;
  298. document.getElementById(selElement).options.add(newOpt);
  299. document.getElementById(selElement).options[i].value = data[i].id;
  300. }
  301. if (defValue != false) document.getElementById(selElement).value = defValue;
  302. }
  303. function htmlComboBox(name, values, defaultVal, parameters, width, onChange) {
  304. var field;
  305. field = '<input type="text" name="' + name + '" id="' + name + '" value="' + defaultVal + '" ' + parameters + '>';
  306. field += '<image name="imgName' + name + '" id="imgName' + name + '" src="' + icon_path + '16x16/phreebooks/pull_down_inactive.gif" height="16" width="16" align="absmiddle" style="border:none;" onMouseOver="handleOver(\'imgName' + name + '\'); return true;" onMouseOut="handleOut(\'imgName' + name + '\'); return true;" onclick="JavaScript:cbMmenuActivate(\'' + name + '\', \'combodiv' + name + '\', \'combosel' + name + '\', \'imgName' + name + '\')">';
  307. field += '<div id="combodiv' + name + '" style="position:absolute; display:none; top:0px; left:0px; z-index:5000" onmouseover="javascript:oOverMenu=\'combodiv' + name + '\';" onmouseout="javascript:oOverMenu=false;">';
  308. field += '<select size="10" id="combosel' + name + '" style="width:' + width + '; border-style:none" onclick="JavaScript:textSet(\'' + name + '\', this.value); ' + onChange + ';" onkeypress="JavaScript:comboKey(\'' + name + '\', this, event);">';
  309. field += '</select></div>';
  310. return field;
  311. }
  312. function cbMmenuActivate(idEdit, idMenu, idSel, idImg) {
  313. if (fActiveMenu) return mouseSelect(0);
  314. //alert('idEdit = '+idEdit+' and idMenu = '+idMenu+' and idSel = '+idSel+' and idImg = '+idImg);
  315. oEdit = document.getElementById(idEdit);
  316. oMenu = document.getElementById(idMenu);
  317. oSel = document.getElementById(idSel);
  318. oImg = document.getElementById(idImg);
  319. nTop = oEdit.offsetTop + oEdit.offsetHeight;
  320. nLeft = oEdit.offsetLeft;
  321. while (oEdit.offsetParent != document.body) {
  322. oEdit = oEdit.offsetParent;
  323. nTop += oEdit.offsetTop;
  324. nLeft += oEdit.offsetLeft;
  325. }
  326. oMenu.style.display = "";
  327. oMenu.style.top = nTop + 'px';
  328. oMenu.style.left = (nLeft - oSel.offsetWidth + oImg.offsetLeft + oImg.offsetWidth) + 'px';
  329. fActiveMenu = idMenu;
  330. document.getElementById(idSel).value = document.getElementById(idEdit).value;
  331. document.getElementById(idSel).focus();
  332. return false;
  333. }
  334. function textSet(idEdit, text) {
  335. document.getElementById(idEdit).value = text;
  336. oOverMenu = false;
  337. mouseSelect(0);
  338. document.getElementById(idEdit).focus();
  339. }
  340. function comboKey(idEdit, idSel, e) {
  341. var keyPressed;
  342. if(window.event) {
  343. keyPressed = window.event.keyCode; // IE hack
  344. } else {
  345. keyPressed = e.which; // standard method
  346. }
  347. if (keyPressed == 13 || keyPressed == 32) {
  348. textSet(idEdit,idSel.value);
  349. } else if (keyPressed == 27) {
  350. mouseSelect(0);
  351. document.getElementById(idEdit).focus();
  352. }
  353. }
  354. function handleOver(idImg) {
  355. if (document.images) document.getElementById(idImg).src=img_on.src;
  356. }
  357. function handleOut(idImg) {
  358. if (document.images) document.getElementById(idImg).src=img_off.src;
  359. }
  360. // ***************** START function to build html strings ******************************************
  361. function buildIcon(imagePath, alt, params) {
  362. var image_html = '<img src="' + imagePath + '" alt="' + alt + '" title="' + alt + '" ' + params + ' />';
  363. return image_html;
  364. }
  365. // ***************** START function to set button pressed ******************************************
  366. function showLoading() {
  367. $("#please_wait").show();
  368. }
  369. function hideLoading() {
  370. $("#please_wait").hide();
  371. }
  372. function submitToDo(todo, multi_submit) {
  373. if (!multi_submit) multi_submit = false;
  374. document.getElementById('todo').value = todo;
  375. if (!form_submitted && check_form() && !multi_submit) {
  376. showLoading();
  377. form_submitted = true;
  378. document.getElementById('todo').form.submit();
  379. } else if (multi_submit) {
  380. document.getElementById('todo').form.submit();
  381. }
  382. }
  383. function submitSeq(rowSeq, todo, multi_submit) {
  384. if (!multi_submit) multi_submit = false;
  385. document.getElementById('rowSeq').value = rowSeq;
  386. submitToDo(todo, multi_submit);
  387. }
  388. function submitSortOrder(sField, sOrder) {
  389. document.getElementById('sort_field').value = sField;
  390. document.getElementById('sort_order').value = sOrder;
  391. document.getElementById('todo').form.submit();
  392. }
  393. function searchPage(get_params) {
  394. var searchText = document.getElementById('search_text').value;
  395. location.href = 'index.php?'+get_params+'search_text='+searchText;
  396. }
  397. function periodPage(get_params) {
  398. var searchPeriod = document.getElementById('search_period').value;
  399. location.href = 'index.php?'+get_params+'search_period='+searchPeriod;
  400. }
  401. function jumpToPage(get_params) {
  402. var index = document.getElementById('list').selectedIndex;
  403. var pageNum = document.getElementById('list').options[index].value;
  404. location.href = 'index.php?'+get_params+'&list='+pageNum;
  405. }
  406. function checkEnter(e) {
  407. if(window.event) { // IE
  408. keycode = event.keyCode;
  409. } else if (e.which) { // Netscape/Firefox/Opera
  410. keycode = e.which;
  411. }
  412. if (keycode == 13) document.getElementById('search_text').form.submit();
  413. }
  414. // ajax wrappers
  415. function parseXml(xml) {
  416. if (jQuery.browser.msie) {
  417. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  418. xmlDoc.loadXML(xml);
  419. xml = xmlDoc;
  420. }
  421. if ($(xml).find("debug").text()) alert($(xml).find("debug").text());
  422. if ($(xml).find("error").text()) {
  423. alert($(xml).find("error").text());
  424. return false;
  425. }
  426. return xml;
  427. }
  428. // ajax pair to reload tab on pages
  429. function tabPage(subject, action, rID) {
  430. if (subject) {
  431. $.ajax({
  432. type: "GET",
  433. url: 'index.php?module=phreedom&page=ajax&op=tab_details&mod='+module+'&subject='+subject+'&action='+action+'&rID='+rID,
  434. dataType: ($.browser.msie) ? "text" : "xml",
  435. error: function(XMLHttpRequest, textStatus, errorThrown) {
  436. alert ("Ajax Error: " + XMLHttpRequest.responseText + "\nTextStatus: " + textStatus + "\nErrorThrown: " + errorThrown);
  437. },
  438. success: processTabPage
  439. });
  440. }
  441. }
  442. function processTabPage(sXml) {
  443. var text = '';
  444. var xml = parseXml(sXml);
  445. if (!xml) return;
  446. subject = $(xml).find("subject").text();
  447. if (!subject) alert('no subject returned');
  448. obj = document.getElementById(subject+'_content');
  449. obj.innerHTML = $(xml).find("htmlContents").text();
  450. if ($(xml).find("message").text()) alert($(xml).find("message").text());
  451. }
  452. /************************ Folder Navigation Functions **********************************/
  453. function Toggle(item) {
  454. obj = document.getElementById(item);
  455. if (obj == null) {
  456. visible = false;
  457. } else {
  458. visible = (obj.style.display!="none");
  459. }
  460. key = document.getElementById("img" + item);
  461. if (visible) {
  462. obj.style.display="none";
  463. key.innerHTML="<img src='"+icon_path+"16x16/places/folder.png' width='16' height='16' hspace='0' vspace='0' border='0' />";
  464. } else {
  465. if (obj != null) obj.style.display = "block";
  466. key.innerHTML = "<img src='"+icon_path+"16x16/actions/document-open.png' width='16' height='16' hspace='0' vspace='0' border='0' />";
  467. }
  468. if (document.getElementById('id')) document.getElementById('id').value = '';
  469. if (document.getElementById('doc_title')) document.getElementById('doc_title').value = '';
  470. if (document.getElementById('folder_path')) {
  471. var tempArray = item.split("_");
  472. strDir = (tempArray[2] == '0') ? '/' : build_path(tempArray[2]);
  473. document.getElementById('folder_path').value = strDir;
  474. document.getElementById('parent_id').value = tempArray[2];
  475. }
  476. }
  477. function build_path(id) {
  478. var strTitle;
  479. strTitle = (id != '0') ? dir_idx[id]+'/' : '/';
  480. if (lvl_idx[id]) strTitle = build_path(lvl_idx[id]) + strTitle;
  481. return strTitle;
  482. }
  483. function Expand(tab_type) {
  484. divs = document.getElementsByTagName("DIV");
  485. for (var i=0; i<divs.length; i++) {
  486. div_id = divs[i].id;
  487. if (div_id.substr(0,3) == tab_type) {
  488. divs[i].style.display = "block";
  489. key = document.getElementById("img" + div_id);
  490. key.innerHTML = "<img src='"+icon_path+"16x16/actions/document-open.png' width='16' height='16' hspace='0' vspace='0' border='0' />";
  491. }
  492. }
  493. }
  494. function Collapse(tab_type) {
  495. divs=document.getElementsByTagName("DIV");
  496. for (var i=0; i<divs.length; i++) {
  497. div_id = divs[i].id;
  498. if (div_id.substr(0,3) == tab_type) {
  499. divs[i].style.display="none";
  500. key = document.getElementById("img" + div_id);
  501. key.innerHTML="<img src='"+icon_path+"16x16/places/folder.png' width='16' height='16' hspace='0' vspace='0' border='0' />";
  502. }
  503. }
  504. }