4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
16 $.ajatus = $.ajatus || {};
21 $.ajatus.utils.to_boolean = function(value) {
22 if (typeof value == 'boolean') {
26 if (typeof value == 'string') {
27 if (value === 'true') {
29 } else if (value === '1') {
35 if (typeof value == 'number') {
46 * Renders pretty printed version from given value
47 * Original pretty_print function by Damien Katz <damien_katz@yahoo.com>
48 * Modified to work with Ajatus by Jerry Jalava <jerry.jalava@gmail.com>
49 * @param {Mixed} val Value to render
50 * @param {Number} indent Current indent level (Default: 4)
51 * @param {String} linesep Line break to be used (Default: "\n")
52 * @param {Number} depth Current line depth (Default: 1)
53 * @returns Pretty printed value
56 $.ajatus.utils.pretty_print = function(val, indent, linesep, depth) {
57 var indent = typeof indent != 'undefined' ? indent : 4;
58 var linesep = typeof linesep != 'undefined' ? linesep : "\n";
59 var depth = typeof depth != 'undefined' ? depth : 1;
61 var propsep = linesep.length ? "," + linesep : ", ";
65 for (var i = 0; i < indent * depth; i++) {
74 return $.ajatus.converter.toJSON(val);
79 if (val.constructor == Date) {
80 return $.ajatus.converter.toJSON(val);
84 if (val.constructor == Array) {
86 for (var index = 0; index < val.length; index++) {
87 buf.push(index > 0 ? propsep : linesep);
89 tab, $.ajatus.utils.pretty_print(val[index], indent, linesep, depth + 1)
94 buf.push(linesep, tab.substr(indent))
101 for (var key in val) {
102 if (! val.hasOwnProperty(key)) {
106 buf.push(index > 0 ? propsep : linesep);
108 tab, $.ajatus.converter.toJSON(key), ": ",
109 $.ajatus.utils.pretty_print(val[key], indent, linesep, depth + 1)
115 buf.push(linesep, tab.substr(indent));
127 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
128 * Digest Algorithm, as defined in RFC 1321.
129 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
130 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
131 * Distributed under the BSD License
132 * See http://pajhome.org.uk/crypt/md5 for more info.
134 * Modified to work with Ajatus by Jerry Jalava <jerry.jalava@gmail.com>
135 * @param {Mixed} val Value to render
136 * @param {Number} indent Current indent level (Default: 4)
137 * @param {String} linesep Line break to be used (Default: "\n")
138 * @param {Number} depth Current line depth (Default: 1)
139 * @returns Pretty printed value
142 $.ajatus.utils.md5 = {
143 _hexcase: 0, /* hex output format. 0 - lowercase; 1 - uppercase */
144 _b64pad: "", /* base-64 pad character. "=" for strict RFC compliance */
145 _chrsz: 8, /* bits per input character. 8 - ASCII; 16 - Unicode */
148 * Calculate the MD5 of an array of little-endian words, and a bit length
150 _core_md5: function(x, len) {
152 x[len >> 5] |= 0x80 << ((len) % 32);
153 x[(((len + 64) >>> 9) << 4) + 14] = len;
160 for (var i = 0; i < x.length; i += 16) {
166 a = $.ajatus.utils.md5._md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
167 d = $.ajatus.utils.md5._md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
168 c = $.ajatus.utils.md5._md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
169 b = $.ajatus.utils.md5._md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
170 a = $.ajatus.utils.md5._md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
171 d = $.ajatus.utils.md5._md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
172 c = $.ajatus.utils.md5._md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
173 b = $.ajatus.utils.md5._md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
174 a = $.ajatus.utils.md5._md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
175 d = $.ajatus.utils.md5._md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
176 c = $.ajatus.utils.md5._md5_ff(c, d, a, b, x[i+10], 17, -42063);
177 b = $.ajatus.utils.md5._md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
178 a = $.ajatus.utils.md5._md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
179 d = $.ajatus.utils.md5._md5_ff(d, a, b, c, x[i+13], 12, -40341101);
180 c = $.ajatus.utils.md5._md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
181 b = $.ajatus.utils.md5._md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
183 a = $.ajatus.utils.md5._md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
184 d = $.ajatus.utils.md5._md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
185 c = $.ajatus.utils.md5._md5_gg(c, d, a, b, x[i+11], 14, 643717713);
186 b = $.ajatus.utils.md5._md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
187 a = $.ajatus.utils.md5._md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
188 d = $.ajatus.utils.md5._md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
189 c = $.ajatus.utils.md5._md5_gg(c, d, a, b, x[i+15], 14, -660478335);
190 b = $.ajatus.utils.md5._md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
191 a = $.ajatus.utils.md5._md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
192 d = $.ajatus.utils.md5._md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
193 c = $.ajatus.utils.md5._md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
194 b = $.ajatus.utils.md5._md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
195 a = $.ajatus.utils.md5._md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
196 d = $.ajatus.utils.md5._md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
197 c = $.ajatus.utils.md5._md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
198 b = $.ajatus.utils.md5._md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
200 a = $.ajatus.utils.md5._md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
201 d = $.ajatus.utils.md5._md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
202 c = $.ajatus.utils.md5._md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
203 b = $.ajatus.utils.md5._md5_hh(b, c, d, a, x[i+14], 23, -35309556);
204 a = $.ajatus.utils.md5._md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
205 d = $.ajatus.utils.md5._md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
206 c = $.ajatus.utils.md5._md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
207 b = $.ajatus.utils.md5._md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
208 a = $.ajatus.utils.md5._md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
209 d = $.ajatus.utils.md5._md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
210 c = $.ajatus.utils.md5._md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
211 b = $.ajatus.utils.md5._md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
212 a = $.ajatus.utils.md5._md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
213 d = $.ajatus.utils.md5._md5_hh(d, a, b, c, x[i+12], 11, -421815835);
214 c = $.ajatus.utils.md5._md5_hh(c, d, a, b, x[i+15], 16, 530742520);
215 b = $.ajatus.utils.md5._md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
217 a = $.ajatus.utils.md5._md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
218 d = $.ajatus.utils.md5._md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
219 c = $.ajatus.utils.md5._md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
220 b = $.ajatus.utils.md5._md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
221 a = $.ajatus.utils.md5._md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
222 d = $.ajatus.utils.md5._md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
223 c = $.ajatus.utils.md5._md5_ii(c, d, a, b, x[i+10], 15, -1051523);
224 b = $.ajatus.utils.md5._md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
225 a = $.ajatus.utils.md5._md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
226 d = $.ajatus.utils.md5._md5_ii(d, a, b, c, x[i+15], 10, -30611744);
227 c = $.ajatus.utils.md5._md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
228 b = $.ajatus.utils.md5._md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
229 a = $.ajatus.utils.md5._md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
230 d = $.ajatus.utils.md5._md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
231 c = $.ajatus.utils.md5._md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
232 b = $.ajatus.utils.md5._md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
234 a = $.ajatus.utils.md5._safe_add(a, olda);
235 b = $.ajatus.utils.md5._safe_add(b, oldb);
236 c = $.ajatus.utils.md5._safe_add(c, oldc);
237 d = $.ajatus.utils.md5._safe_add(d, oldd);
240 return Array(a, b, c, d);
244 * These functions implement the four basic operations the algorithm uses.
246 _md5_cmn: function(q, a, b, x, s, t) {
247 return $.ajatus.utils.md5._safe_add($.ajatus.utils.md5._bit_rol($.ajatus.utils.md5._safe_add($.ajatus.utils.md5._safe_add(a, q), $.ajatus.utils.md5._safe_add(x, t)), s),b);
249 _md5_ff: function(a, b, c, d, x, s, t) {
250 return $.ajatus.utils.md5._md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
252 _md5_gg: function(a, b, c, d, x, s, t) {
253 return $.ajatus.utils.md5._md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
255 _md5_hh: function(a, b, c, d, x, s, t) {
256 return $.ajatus.utils.md5._md5_cmn(b ^ c ^ d, a, b, x, s, t);
258 _md5_ii: function(a, b, c, d, x, s, t) {
259 return $.ajatus.utils.md5._md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
263 * Calculate the HMAC-MD5, of a key and some data
265 _core_hmac_md5: function(key, data) {
266 var bkey = $.ajatus.utils.md5._str2binl(key);
267 if(bkey.length > 16) bkey = $.ajatus.utils.md5._core_md5(bkey, key.length * $.ajatus.utils.md5._chrsz);
269 var ipad = Array(16), opad = Array(16);
270 for(var i = 0; i < 16; i++) {
271 ipad[i] = bkey[i] ^ 0x36363636;
272 opad[i] = bkey[i] ^ 0x5C5C5C5C;
275 var hash = $.ajatus.utils.md5._core_md5(ipad.concat($.ajatus.utils.md5._str2binl(data)), 512 + data.length * $.ajatus.utils.md5._chrsz);
277 return $.ajatus.utils.md5._core_md5(opad.concat(hash), 512 + 128);
281 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
282 * to work around bugs in some JS interpreters.
284 _safe_add: function(x, y) {
285 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
286 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
287 return (msw << 16) | (lsw & 0xFFFF);
291 * Bitwise rotate a 32-bit number to the left.
293 _bit_rol: function(num, cnt) {
294 return (num << cnt) | (num >>> (32 - cnt));
298 * Convert a string to an array of little-endian words
299 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
301 _str2binl: function(str) {
303 var mask = (1 << $.ajatus.utils.md5._chrsz) - 1;
304 for (var i = 0; i < str.length * $.ajatus.utils.md5._chrsz; i += $.ajatus.utils.md5._chrsz) {
305 bin[i>>5] |= (str.charCodeAt(i / $.ajatus.utils.md5._chrsz) & mask) << (i%32);
311 * Convert an array of little-endian words to a string
313 _binl2str: function(bin) {
315 var mask = (1 << $.ajatus.utils.md5._chrsz) - 1;
316 for(var i = 0; i < bin.length * 32; i += $.ajatus.utils.md5._chrsz) {
317 str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
323 * Convert an array of little-endian words to a hex string.
325 _binl2hex: function(binarray) {
326 var hex_tab = $.ajatus.utils.md5._hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
328 for (var i = 0; i < binarray.length * 4; i++) {
329 str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
335 * Convert an array of little-endian words to a base-64 string
337 _binl2b64: function(binarray) {
338 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
340 for (var i = 0; i < binarray.length * 4; i += 3) {
341 var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
342 for (var j = 0; j < 4; j++) {
343 if (i * 8 + j * 6 > binarray.length * 32) {
344 str += $.ajatus.utils.md5._b64pad;
346 str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
353 _hex_md5: function(s) {
354 return $.ajatus.utils.md5._binl2hex($.ajatus.utils.md5._core_md5($.ajatus.utils.md5._str2binl(s), s.length * $.ajatus.utils.md5._chrsz));
356 _b64_md5: function(s) {
357 return $.ajatus.utils.md5._binl2b64($.ajatus.utils.md5._core_md5($.ajatus.utils.md5._str2binl(s), s.length * $.ajatus.utils.md5._chrsz));
359 _str_md5: function(s) {
360 return $.ajatus.utils.md5._binl2str($.ajatus.utils.md5._core_md5($.ajatus.utils.md5._str2binl(s), s.length * $.ajatus.utils.md5._chrsz));
362 _hex_hmac_md5: function(key, data) {
363 return $.ajatus.utils.md5._binl2hex($.ajatus.utils.md5._core_hmac_md5(key, data));
365 _b64_hmac_md5: function(key, data) {
366 return $.ajatus.utils.md5._binl2b64($.ajatus.utils.md5._core_hmac_md5(key, data));
368 _str_hmac_md5: function(key, data) {
369 return $.ajatus.utils.md5._binl2str($.ajatus.utils.md5._core_hmac_md5(key, data));
372 encode: function(string, encoding, data) {
373 var encoded_string = '';
374 if (typeof encoding == 'undefined') {
375 var encoding = 'hex';
380 encoded_string = $.ajatus.utils.md5._b64_md5(string);
383 encoded_string = $.ajatus.utils.md5._str_md5(string);
386 encoded_string = $.ajatus.utils.md5._hex_hmac_md5(string, data);
389 encoded_string = $.ajatus.utils.md5._b64_hmac_md5(string, data);
392 encoded_string = $.ajatus.utils.md5._str_hmac_md5(string, data);
396 encoded_string = $.ajatus.utils.md5._hex_md5(string);
400 return encoded_string;
402 match: function(string, md5, encoding) {
403 var encoded_string = $.ajatus.utils.md5.encode(string, encoding);
405 if (encoded_string === md5) {
413 $.ajatus.utils.base64 = {
416 'A','B','C','D','E','F','G','H',
417 'I','J','K','L','M','N','O','P',
418 'Q','R','S','T','U','V','W','X',
419 'Y','Z','a','b','c','d','e','f',
420 'g','h','i','j','k','l','m','n',
421 'o','p','q','r','s','t','u','v',
422 'w','x','y','z','0','1','2','3',
423 '4','5','6','7','8','9','+','/'
426 reverseBase64Chars: null,
428 encode: function(value) {
432 function setBase64Str(str) {
437 function readBase64() {
439 return $.ajatus.utils.base64.END_OF_INPUT;
441 if (base64Count >= base64Str.length) {
442 return $.ajatus.utils.base64.END_OF_INPUT;
444 var c = base64Str.charCodeAt(base64Count) & 0xff;
452 var inBuffer = new Array(3);
455 while (!done && (inBuffer[0] = readBase64()) != $.ajatus.utils.base64.END_OF_INPUT) {
456 inBuffer[1] = readBase64();
457 inBuffer[2] = readBase64();
458 result += ($.ajatus.utils.base64.base64Chars[ inBuffer[0] >> 2 ]);
459 if (inBuffer[1] != $.ajatus.utils.base64.END_OF_INPUT) {
460 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[0] << 4) & 0x30) | (inBuffer[1] >> 4)]);
461 if (inBuffer[2] != $.ajatus.utils.base64.END_OF_INPUT) {
462 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
463 result += ($.ajatus.utils.base64.base64Chars[inBuffer[2] & 0x3F]);
465 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[1] << 2) & 0x3c)]);
470 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[0] << 4) & 0x30)]);
476 if (lineCount >= 76) {
484 decode: function(value) {
488 if ($.ajatus.utils.base64.reverseBase64Chars == null) {
489 $.ajatus.utils.base64.reverseBase64Chars = [];
490 for (var i=0; i < $.ajatus.utils.base64.base64Chars.length; i++) {
491 $.ajatus.utils.base64.reverseBase64Chars[$.ajatus.utils.base64.base64Chars[i]] = i;
495 function setBase64Str(str) {
500 function readReverseBase64() {
502 return $.ajatus.utils.base64.END_OF_INPUT;
506 if (base64Count >= base64Str.length) {
507 return $.ajatus.utils.base64.END_OF_INPUT;
509 var nextCharacter = base64Str.charAt(base64Count);
511 if ($.ajatus.utils.base64.reverseBase64Chars[nextCharacter]){
512 return $.ajatus.utils.base64.reverseBase64Chars[nextCharacter];
514 if (nextCharacter == 'A') {
518 return $.ajatus.utils.base64.END_OF_INPUT;
533 var inBuffer = new Array(4);
535 while (!done && (inBuffer[0] = readReverseBase64()) != $.ajatus.utils.base64.END_OF_INPUT
536 && (inBuffer[1] = readReverseBase64()) != $.ajatus.utils.base64.END_OF_INPUT)
538 inBuffer[2] = readReverseBase64();
539 inBuffer[3] = readReverseBase64();
540 result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
541 if (inBuffer[2] != $.ajatus.utils.base64.END_OF_INPUT){
542 result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
543 if (inBuffer[3] != $.ajatus.utils.base64.END_OF_INPUT){
544 result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
559 * UTF-8 data encode / decode
560 * http://www.webtoolkit.info/
562 * @returns Encoded/Decoded string
565 $.ajatus.utils.utf8 = {
566 encode: function(string) {
567 string = string.replace(/\r\n/g,"\n");
570 for (var n = 0; n < string.length; n++) {
571 var c = string.charCodeAt(n);
574 utftext += String.fromCharCode(c);
575 } else if ( (c > 127)
578 utftext += String.fromCharCode((c >> 6) | 192);
579 utftext += String.fromCharCode((c & 63) | 128);
581 utftext += String.fromCharCode((c >> 12) | 224);
582 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
583 utftext += String.fromCharCode((c & 63) | 128);
589 decode: function(utftext) {
594 while ( i < utftext.length ) {
595 c = utftext.charCodeAt(i);
598 string += String.fromCharCode(c);
600 } else if ( (c > 191)
603 c2 = utftext.charCodeAt(i+1);
604 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
607 c2 = utftext.charCodeAt(i+1);
608 c3 = utftext.charCodeAt(i+2);
609 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
618 $.ajatus.utils.array = {
619 has_match: function(needles, haystack) {
620 if ( typeof haystack != 'object'
621 || haystack.length <= 0)
626 if (typeof needles == 'object') {
628 $.each(needles, function(i,needle){
629 if ($.inArray(needle, haystack) != -1) {
632 // if (typeof(haystack[needle]) != 'undefined') {
637 } else if (typeof needles == 'string') {
639 if ($.inArray(needles, haystack) != -1) {
649 $.ajatus.utils.object = {
650 clone: function(obj) {
651 if(obj == null || typeof(obj) != 'object') {
655 for(var key in obj) {
656 temp[key] = $.ajatus.utils.object.clone(obj[key]);
663 $.ajatus.utils.pause = function(ms) {
664 var date = new Date();
667 do { currDate = new Date(); }
668 while(currDate - date < ms);
671 $.ajatus.utils._random_key = 0;
672 $.ajatus.utils.generate_id = function() {
673 $.ajatus.utils._random_key += 1;
675 var date = new Date();
676 var random_key = Math.floor(Math.random()*4013);
677 var random_key2 = Math.floor(Math.random()*3104);
679 return $.ajatus.utils.md5.encode(Math.floor(((date.getTime()/1000)+random_key2) + (10016486 + (random_key * 22423)) * random_key / random_key2).toString() + "_" + $.ajatus.utils._random_key).toString().substr(0,8);
682 $.ajatus.utils.get_url_arg = function(name) {
685 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
686 var exp = "[\\?&]"+name+"=([^&#]*)";
687 var regex = new RegExp( exp );
688 var results = regex.exec(window.location.href);
689 if (results != null) {
696 $.ajatus.utils.load_script = function(url, callback, cb_args) {
697 $('head').append('<script type="text/javascript" charset="utf-8" src="'+url+'"></script>');
698 if (typeof callback == 'string') {
699 if ( typeof cb_args == 'undefined'
700 || typeof cb_args != 'object')
705 setTimeout('eval("var fn = eval('+callback+'); fn.apply(fn, [\''+cb_args.join("','")+'\']);")', 300);
709 $.ajatus.utils.doc_generator = function(prefix, count, type, data, value_tpl) {
715 if (typeof value_tpl != 'undefined') {
716 doc.value = value_tpl;
718 if (typeof type == 'undefined') {
719 var type = $.ajatus.preferences.client.content_types['note'];
721 if (typeof data == 'undefined') {
726 $.each(type.original_schema, function(i,n){
735 if (typeof prefix == 'undefined') {
736 var prefix = 'ajatus';
738 if (typeof count == 'undefined') {
742 function prepare_title(title, id) {
743 var full_title = title + " ";
744 full_title += (id).toString();
748 for (var i=0; i<count; i++) {
749 var new_doc = new $.ajatus.document(doc);
751 if ( typeof data.title == 'undefined'
752 && typeof new_doc.value['title'] != 'undefined')
754 new_doc.value['title'].val = $.ajatus.i10n.get("Generated doc title (%s)", [prefix]) + " " + i;
757 $.each(data, function(x,n){
758 if (typeof new_doc.value[x] != 'undefined') {
759 new_doc.value[x].val = n;
763 var now = $.ajatus.formatter.date.js_to_iso8601(new Date());
767 creator: $.ajatus.preferences.local.user.email,
769 revisor: $.ajatus.preferences.local.user.email
772 new_doc = $.ajatus.document.modify_metadata(new_doc, new_metadata);
774 new_doc._id = prefix + "_gen_doc_" + (i).toString();
775 docs.push($.ajatus.utils.object.clone(new_doc));
781 // Put focus on given form element (0 == first) (skips hidden fields)
782 $.fn.set_focus_on = function(eid) {
783 var elem = $('input:visible:not(:hidden)', this).get(eid);
784 var select = $('select:visible', this).get(eid);
786 if (select && elem) {
787 if (select.offsetTop < elem.offsetTop) {
792 var textarea = $('textarea:visible', this).get(eid);
793 if (textarea && elem) {
794 if (textarea.offsetTop < elem.offsetTop) {
807 * Following functions are taken from the jquery form plugin.
808 * Plugin can be found from http://www.malsup.com/jquery/form/
810 $.fieldValue = function(el, successful) {
811 var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
812 if (typeof successful == 'undefined') successful = true;
814 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
815 (t == 'checkbox' || t == 'radio') && !el.checked ||
816 (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
817 tag == 'select' && el.selectedIndex == -1))
820 if (tag == 'select') {
821 var index = el.selectedIndex;
822 if (index < 0) return null;
823 var a = [], ops = el.options;
824 var one = (t == 'select-one');
825 var max = (one ? index+1 : ops.length);
826 for(var i=(one ? index : 0); i < max; i++) {
829 // extra pain for IE...
830 var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
839 $.fn.formToArray = function(semantic) {
841 if (this.length == 0) return a;
844 var els = semantic ? form.getElementsByTagName('*') : form.elements;
846 for(var i=0, max=els.length; i < max; i++) {
851 if (semantic && form.clk && el.type == "image") {
852 // handle image inputs on the fly when semantic == true
853 if(!el.disabled && form.clk == el)
854 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
858 var v = $.fieldValue(el, true);
859 if (v && v.constructor == Array) {
860 for(var j=0, jmax=v.length; j < jmax; j++)
861 a.push({name: n, value: v[j]});
863 else if (v !== null && typeof v != 'undefined')
864 a.push({name: n, value: v});
867 if (!semantic && form.clk) {
868 // input type=='image' are not found in elements array! handle them here
869 var inputs = form.getElementsByTagName("input");
870 for(var i=0, max=inputs.length; i < max; i++) {
871 var input = inputs[i];
873 if(n && !input.disabled && input.type == "image" && form.clk == input)
874 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
880 * Form plugin functions end