From 06bf3837079bcc2392a95c3effc9a15d8ecce2fa Mon Sep 17 00:00:00 2001 From: abijah Date: Sat, 11 Jul 2009 15:54:44 +0000 Subject: [PATCH] Added a file with some javascript date/time routines. I don't think we want it, but at the same time, I can't bring myself to toss it. May as well capture a revision and I can delete it later. git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@314 97e9348a-65ac-dc4b-aefc-98561f571b83 --- webroot/js/pmgr.js.keep | 382 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 webroot/js/pmgr.js.keep diff --git a/webroot/js/pmgr.js.keep b/webroot/js/pmgr.js.keep new file mode 100644 index 0000000..f1a654a --- /dev/null +++ b/webroot/js/pmgr.js.keep @@ -0,0 +1,382 @@ +/** + * Function : dump() + * Arguments: The data - array,hash(associative array),object + * The level - OPTIONAL + * Returns : The textual representation of the array. + * This function was inspired by the print_r function of PHP. + * This will accept some data as the argument and return a + * text that will be a more readable version of the + * array/hash/object that is given. + * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php + */ +function dump(arr,level) { + var dumped_text = ""; + if(!level) level = 0; + + //The padding given at the beginning of the line. + var level_padding = ""; + for(var j=0;j \"" + value + "\"\n"; + } + } + } else { //Stings/Chars/Numbers etc. + dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; + } + return dumped_text; +} + + +function var_dump(element, limit, depth) +{ + depth = depth?depth:0; + limit = limit?limit:1; + + returnString = '
    '; + + for(property in element) + { + //Property domConfig isn't accessable + if (property != 'domConfig') + { + returnString += '
  1. '+ property + ' (' + (typeof element[property]) +')'; + + if (typeof element[property] == 'number' || typeof element[property] == 'boolean') + returnString += ' : ' + element[property] + ''; + if (typeof element[property] == 'string' && element[property]) + returnString += ':
    ' + + element[property].replace(//g, '&gt;') + '
    '; + + if ((typeof element[property] == 'object') && (depth < limit)) + returnString += var_dump(element[property], limit, (depth + 1)); + + returnString += '
  2. '; + } + } + returnString += '
'; + + if(depth == 0) + { + winpop = window.open("", "","width=800,height=600,scrollbars,resizable"); + winpop.document.write('
'+returnString+ '
'); + winpop.document.close(); + } + + return returnString; +} + + +function htmlEncode(s) +{ + //return s; + return s.replace(/&(?!\w+([;\s]|$))/g, "&") + .replace(//g, ">"); +} + +function addDiv(id_name, div_name, into_div_name, flash, html, script) { + var id = $('#'+id_name).val(); + + html = '
' + + html.replace(/%{id}/g, id) + .replace(/%{remove(:([^}]*))?}/g, + '' + + '' + + ("$2" == "" ? "$2" : 'remove') + '' + '') + + '
'; + + if (script) { + html += ''; + } + + //$("#debug").append(htmlEncode(html)); + $("#"+into_div_name).append(html); + + if (flash) { + $('#'+div_name+'-'+id) + .animate({ backgroundColor: "yellow" }, 300) + .animate({ backgroundColor: "white" }, 500); + } + + id = id - 0 + 1; + $('#'+id_name).val(id); +} + +function removeElement(elem_id) { + $('#'+elem_id).remove(); +} + +// function +// var currentTime = new Date() +// var month = currentTime.getMonth() + 1 +// var day = currentTime.getDate() +// var year = currentTime.getFullYear() +// document.write(month + "/" + day + "/" + year) +// //--> +// var currentTime = new Date() +// var hours = currentTime.getHours() +// var minutes = currentTime.getMinutes() +// if (minutes < 10){ +// minutes = "0" + minutes +// } +// document.write(hours + ":" + minutes + " ") +// if(hours > 11){ +// document.write("PM") +// } else { +// document.write("AM") +// } +// //--> + +function fmtCurrency(amount) { + if (amount == null || isNaN(amount)) + return '-'; + + // Get rid of any extraneous characters, determine + // the sign, and round to the nearest cent. + amount = amount.toString().replace(/\$|\,/g,''); + sign = (amount == (amount = Math.abs(amount))); + amount = (amount+0.0000000001).toFixed(2); + + // Insert thousands separator + while (amount != (amount = amount.replace(/(\d)(\d\d\d[.,])/, "$1,$2"))); + + // Return formatted amount + return (sign?'$':'($') + amount + (sign?'':')'); +} + + +/* + * Date Format 1.2.2 + * (c) 2007-2008 Steven Levithan + * MIT license + * Includes enhancements by Scott Trenda and Kris Kowal + * + * Accepts a date, a mask, or a date and a mask. + * Returns a formatted version of the given date. + * The date defaults to the current date/time. + * The mask defaults to dateFormat.masks.default. + */ +var dateFormat = function () { + var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|\"[^\"]*\"|\'[^\']*\'/g, + timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g, + timezoneClip = /[^-+\dA-Z]/g, + pad = function (val, len) { + val = String(val); + len = len || 2; + while (val.length < len) val = "0" + val; + return val; + }; + + // Regexes and supporting functions are cached through closure + return function (date, mask, utc) { + var dF = dateFormat; + + // You can't provide utc if you skip other args (use the "UTC:" mask prefix) + if (arguments.length == 1 && (typeof date == "string" || date instanceof String) && !/\d/.test(date)) { + mask = date; + date = undefined; + } + + // Passing date through Date applies Date.parse, if necessary + date = date ? new Date(date) : new Date(); + if (isNaN(date)) throw new SyntaxError("invalid date"); + + mask = String(dF.masks[mask] || mask || dF.masks["default"]); + + // Allow setting the utc argument via the mask + if (mask.slice(0, 4) == "UTC:") { + mask = mask.slice(4); + utc = true; + } + + var _ = utc ? "getUTC" : "get", + d = date[_ + "Date"](), + D = date[_ + "Day"](), + m = date[_ + "Month"](), + y = date[_ + "FullYear"](), + H = date[_ + "Hours"](), + M = date[_ + "Minutes"](), + s = date[_ + "Seconds"](), + L = date[_ + "Milliseconds"](), + o = utc ? 0 : date.getTimezoneOffset(), + flags = { + d: d, + dd: pad(d), + ddd: dF.i18n.dayNames[D], + dddd: dF.i18n.dayNames[D + 7], + m: m + 1, + mm: pad(m + 1), + mmm: dF.i18n.monthNames[m], + mmmm: dF.i18n.monthNames[m + 12], + yy: String(y).slice(2), + yyyy: y, + h: H % 12 || 12, + hh: pad(H % 12 || 12), + H: H, + HH: pad(H), + M: M, + MM: pad(M), + s: s, + ss: pad(s), + l: pad(L, 3), + L: pad(L > 99 ? Math.round(L / 10) : L), + t: H < 12 ? "a" : "p", + tt: H < 12 ? "am" : "pm", + T: H < 12 ? "A" : "P", + TT: H < 12 ? "AM" : "PM", + Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""), + o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4), + S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10] + }; + + return mask.replace(token, function ($0) { + return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); + }); + }; +}(); + +// Some common format strings +dateFormat.masks = { + "default": "ddd mmm dd yyyy HH:MM:ss", + shortDate: "m/d/yy", + mediumDate: "mmm d, yyyy", + longDate: "mmmm d, yyyy", + fullDate: "dddd, mmmm d, yyyy", + shortTime: "h:MM TT", + mediumTime: "h:MM:ss TT", + longTime: "h:MM:ss TT Z", + isoDate: "yyyy-mm-dd", + isoTime: "HH:MM:ss", + isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", + isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" +}; + +// Internationalization strings +dateFormat.i18n = { + dayNames: [ + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" + ], + monthNames: [ + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" + ] +}; + +// For convenience... +Date.prototype.format = function (mask, utc) { + return dateFormat(this, mask, utc); +}; + + +// REVISIT : 20090617 +// I would rather use XML to pass from JS to PHP, but at the +// moment things were working just fine with serialize, and +// I'm not keen on redesigning it at the moment. So, here +// is a serialize implementation I found on the web. + +function serialize( mixed_value ) { + // http://kevin.vanzonneveld.net + // + original by: Arpad Ray (mailto:arpad@php.net) + // + improved by: Dino + // + bugfixed by: Andrej Pavlovic + // + bugfixed by: Garagoth + // + input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html) + // + bugfixed by: Russell Walker + // % note: We feel the main purpose of this function should be to ease the transport of data between php & js + // % note: Aiming for PHP-compatibility, we have to translate objects to arrays + // * example 1: serialize(['Kevin', 'van', 'Zonneveld']); + // * returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}' + // * example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'}); + // * returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}' + + var _getType = function( inp ) { + var type = typeof inp, match; + var key; + if (type == 'object' && !inp) { + return 'null'; + } + if (type == "object") { + if (!inp.constructor) { + return 'object'; + } + var cons = inp.constructor.toString(); + match = cons.match(/(\w+)\(/); + if (match) { + cons = match[1].toLowerCase(); + } + var types = ["boolean", "number", "string", "array"]; + for (key in types) { + if (cons == types[key]) { + type = types[key]; + break; + } + } + } + return type; + }; + var type = _getType(mixed_value); + var val, ktype = ''; + + switch (type) { + case "function": + val = ""; + break; + case "undefined": + val = "N"; + break; + case "boolean": + val = "b:" + (mixed_value ? "1" : "0"); + break; + case "number": + val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value; + break; + case "string": + val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\""; + break; + case "array": + case "object": + val = "a"; + /* + if (type == "object") { + var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/); + if (objname == undefined) { + return; + } + objname[1] = serialize(objname[1]); + val = "O" + objname[1].substring(1, objname[1].length - 1); + } + */ + var count = 0; + var vals = ""; + var okey; + var key; + for (key in mixed_value) { + ktype = _getType(mixed_value[key]); + if (ktype == "function") { + continue; + } + + okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key); + vals += serialize(okey) + + serialize(mixed_value[key]); + count++; + } + val += ":" + count + ":{" + vals + "}"; + break; + } + if (type != "object" && type != "array") { + val += ";"; + } + return val; + +}