Some more l10n strings
[ajatus.git] / js / ajatus.utils.js
blob864473377ac5cbb3a5b64d702004952d23ce3adc
1 /*
2 * This file is part of
4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
6 *
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
15 (function($){
16 $.ajatus = $.ajatus || {};
18 $.ajatus.utils = {
20 $.ajatus.utils.array = {
21 has_match: function(needles, haystack) {
22 if ( typeof haystack != 'object'
23 || haystack.length <= 0)
25 return false;
28 if (typeof needles == 'object') {
29 var matched = false;
30 $.each(needles, function(i,needle){
31 if ($.inArray(needle, haystack) != -1) {
32 matched = true;
34 // if (typeof(haystack[needle]) != 'undefined') {
35 // matched = true;
36 // }
37 });
38 return matched;
39 } else if (typeof needles == 'string') {
40 var matched = false;
41 if ($.inArray(needles, haystack) != -1) {
42 matched = true;
44 return matched;
47 return false;
51 $.ajatus.utils.object = {
52 clone: function(obj) {
53 if(obj == null || typeof(obj) != 'object') {
54 return obj;
56 var temp = {};
57 for(var key in obj) {
58 temp[key] = $.ajatus.utils.object.clone(obj[key]);
61 return temp;
65 // Put focus on given form element (0 == first) (skips hidden fields)
66 $.fn.set_focus_on = function(eid) {
67 var elem = $('input:visible:not(:hidden)', this).get(eid);
68 var select = $('select:visible', this).get(eid);
70 if (select && elem) {
71 if (select.offsetTop < elem.offsetTop) {
72 elem = select;
76 var textarea = $('textarea:visible', this).get(eid);
77 if (textarea && elem) {
78 if (textarea.offsetTop < elem.offsetTop) {
79 elem = textarea;
83 if (elem) {
84 elem.focus();
87 return this;
90 $.ajatus.utils.pause = function(ms) {
91 var date = new Date();
92 var currDate = null;
94 do { currDate = new Date(); }
95 while(currDate - date < ms);
98 $.ajatus.utils.generate_id = function() {
99 random_key = Math.floor(Math.random()*4013);
100 return (10016486 + (random_key * 22423));
103 $.ajatus.utils.get_url_arg = function(name) {
104 var value = null;
106 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
107 var exp = "[\\?&]"+name+"=([^&#]*)";
108 var regex = new RegExp( exp );
109 var results = regex.exec(window.location.href);
110 if (results != null) {
111 value = results[1];
114 return value;
117 function gup( name )
119 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
120 var regexS = "[\\?&]"+name+"=([^&#]*)";
121 var regex = new RegExp( regexS );
122 var results = regex.exec( window.location.href );
123 if( results == null )
124 return "";
125 else
126 return results[1];
130 * Following functions are taken from the jquery form plugin.
131 * Plugin can be found from http://www.malsup.com/jquery/form/
133 $.fieldValue = function(el, successful) {
134 var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
135 if (typeof successful == 'undefined') successful = true;
137 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
138 (t == 'checkbox' || t == 'radio') && !el.checked ||
139 (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
140 tag == 'select' && el.selectedIndex == -1))
141 return null;
143 if (tag == 'select') {
144 var index = el.selectedIndex;
145 if (index < 0) return null;
146 var a = [], ops = el.options;
147 var one = (t == 'select-one');
148 var max = (one ? index+1 : ops.length);
149 for(var i=(one ? index : 0); i < max; i++) {
150 var op = ops[i];
151 if (op.selected) {
152 // extra pain for IE...
153 var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
154 if (one) return v;
155 a.push(v);
158 return a;
160 return el.value;
162 $.fn.formToArray = function(semantic) {
163 var a = [];
164 if (this.length == 0) return a;
166 var form = this[0];
167 var els = semantic ? form.getElementsByTagName('*') : form.elements;
168 if (!els) return a;
169 for(var i=0, max=els.length; i < max; i++) {
170 var el = els[i];
171 var n = el.name;
172 if (!n) continue;
174 if (semantic && form.clk && el.type == "image") {
175 // handle image inputs on the fly when semantic == true
176 if(!el.disabled && form.clk == el)
177 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
178 continue;
181 var v = $.fieldValue(el, true);
182 if (v && v.constructor == Array) {
183 for(var j=0, jmax=v.length; j < jmax; j++)
184 a.push({name: n, value: v[j]});
186 else if (v !== null && typeof v != 'undefined')
187 a.push({name: n, value: v});
190 if (!semantic && form.clk) {
191 // input type=='image' are not found in elements array! handle them here
192 var inputs = form.getElementsByTagName("input");
193 for(var i=0, max=inputs.length; i < max; i++) {
194 var input = inputs[i];
195 var n = input.name;
196 if(n && !input.disabled && input.type == "image" && form.clk == input)
197 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
200 return a;
203 * Form plugin functions end
206 })(jQuery);