source: branches/2.2/expressoMail1_2/js/common_functions.js @ 3227

Revision 3227, 28.6 KB checked in by amuller, 14 years ago (diff)

Ticket #1024 - Unifica validação do email no js

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1// BEGIN: FUNCTION RESIZE WINDOW
2if (!expresso_offline) {
3        var _showBar = showBar;
4        var _hideBar = hideBar;
5}
6
7function __showBar(){
8        _showBar();
9        resizeWindow();
10}
11
12function __hideBar(){
13        _hideBar();
14        resizeWindow();
15}
16showBar = __showBar;
17hideBar = __hideBar;
18
19window.onresize = resizeWindow;
20
21function config_events(pObj, pEvent, pHandler)
22{
23    if( typeof pObj == 'object')
24    {
25        if( pEvent.substring(0, 2) == 'on')
26            pEvent = pEvent.substring(2, pEvent.length);
27
28        if ( pObj.addEventListener )
29            pObj.addEventListener(pEvent, pHandler, false);
30        else if( pObj.attachEvent )
31            pObj.attachEvent('on' + pEvent, pHandler );
32    }
33}
34
35function resizeWindow(){
36
37        var divScrollMain = Element("divScrollMain_"+numBox);
38        var table_message = Element("table_message");
39        var content_folders = Element("content_folders");
40        var clientHeight = ((window.innerHeight ? window.innerHeight : document.body.offsetHeight) - 8);
41        var clientWidth = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
42
43        if(divScrollMain){
44                divScrollMain.style.height = (clientHeight - (findPosY(divScrollMain) + (table_message.clientHeight ? table_message.clientHeight : table_message.offsetHeight))) + "px";
45        }
46
47        if(typeof(BordersArray) != 'undefined') {
48                for(var i = 1; BordersArray.length > 1 && i < BordersArray.length;i++) {
49                        var div_scroll = Element("div_message_scroll_"+BordersArray[i].sequence);
50                        var div = Element("content_id_"+BordersArray[i].sequence);
51
52                        if(div){
53                                div.style.height = (clientHeight - (findPosY(div) + (table_message.clientHeight ? table_message.clientHeight : table_message.offsetHeight)+2)) + "px";
54                                 div.style.width = (clientWidth - (findPosX(div)+10)) + "px";
55                        }
56                        if(div_scroll){
57                                div_scroll.style.height = (clientHeight - (findPosY(div_scroll) + (table_message.clientHeight ? table_message.clientHeight : table_message.offsetHeight)+5)) + "px";
58                                div_scroll.style.width = (clientWidth - (findPosX(div_scroll)+15)) + "px";
59                        }
60                }
61        }
62
63        if(content_folders){
64                var search_div = Element("search_div");
65                var contentFoldersY = findPosY(content_folders);
66                content_folders.style.height = (clientHeight - (contentFoldersY + (contentFoldersY > findPosY(search_div) ? 0 : (search_div.clientHeight ? search_div.clientHeight : search_div.offsetHeight) + 5))) + "px";
67        }
68}
69// END: FUNCTION RESIZE WINDOW
70
71var _beforeunload_ = window.onbeforeunload;
72
73window.onbeforeunload = function()
74{
75        return unloadMess();
76}
77
78function unloadMess(){
79        if (typeof(expresso_mail_sync) != "undefined" && expresso_mail_sync.working) {
80                var mess = get_lang("You're about archiving your e-mails from server. Do you really want to stop this action?");
81                return mess;
82        }
83        else {
84                var mess = get_lang("Your message has not been sent and will be discarted.");
85                for (var i = 0; i < BordersArray.length; i++) {
86                        var body = Element('body_' + BordersArray[i].sequence);
87                        if (body && body.contentWindow && body.contentWindow.document.designMode.toLowerCase() == 'on') {
88                                return mess;
89                        }
90                }
91        }
92}
93
94// Translate words and phrases using user language from eGroupware.
95function get_lang(_key) {
96        if (typeof(_key) == 'undefined')
97                return false;
98        var key = _key.toLowerCase();
99        if(array_lang[key])
100                var _value = array_lang[key];
101        else
102                var _value = _key+"*";
103
104        if(arguments.length > 1)
105                for(j = 1; typeof(arguments[j]) != 'undefined'; j++)
106                        _value = _value.replace("%"+j,arguments[j]);
107        return _value;
108}
109
110// Make decimal round, using in size message
111function round(value, decimal){
112        var return_value = Math.round( value * Math.pow( 10 , decimal ) ) / Math.pow( 10 , decimal );
113        return( return_value );
114}
115
116// Change the class of message.
117// In refresh, the flags UnRead and UnSeen don't exist anymore.
118function set_msg_as_read(msg_number, selected){
119        tr_message = Element(msg_number);
120        if (exist_className(tr_message, 'tr_msg_unread'))
121                decrement_folder_unseen();
122        remove_className(tr_message, 'tr_msg_unread');
123        remove_className(tr_message, 'selected_msg');
124        Element("td_message_unseen_"+msg_number).innerHTML = "<img src ='templates/"+template+"/images/seen.gif' title='"+get_lang('Seen')+"'>";
125        connector.purgeCache();
126        return true;
127}
128
129function msg_is_read(msg_number, selected){
130        tr_message = Element(msg_number);
131        return !(tr_message && LTrim(tr_message.className).match('tr_msg_unread'))
132}
133
134function set_msg_as_unread(msg_number){
135        tr_message = Element(msg_number);
136        if ((exist_className(tr_message, 'tr_msg_read') || exist_className(tr_message, 'tr_msg_read2')) && (!exist_className(tr_message, 'tr_msg_unread')))
137                increment_folder_unseen();     
138        remove_className(tr_message, 'selected_msg');
139        add_className(tr_message, 'tr_msg_unread');
140        Element("td_message_unseen_"+msg_number).innerHTML = "<img src ='templates/"+template+"/images/unseen.gif' title='"+get_lang('Unseen')+"'>";
141}
142
143function set_msg_as_flagged(msg_number){
144        var msg = Element(msg_number);
145        remove_className(msg, 'selected_msg');
146        add_className(msg, 'flagged_msg');
147        Element("td_message_important_"+msg_number).innerHTML = "<img src ='templates/"+template+"/images/important.gif' title='"+get_lang('Important')+"'>";
148}
149
150function set_msg_as_unflagged(msg_number){
151        var msg = Element(msg_number);
152        remove_className(msg, 'selected_msg');
153        remove_className(msg, 'flagged_msg');
154        Element("td_message_important_"+msg_number).innerHTML = "&nbsp;&nbsp;&nbsp;";
155}
156
157function removeAll(id){
158        do
159        {
160                if (typeof(Element(id)) == 'undefined')
161                        break;
162                Element(id).parentNode.removeChild(Element(id));
163        }
164        while(Element(id));
165}
166
167function get_current_folder(){
168        return current_folder;
169}
170
171// Kill current box (folder or page).
172function kill_current_box(){
173        var box = document.getElementById("table_box");
174        if (box != null)
175                box.parentNode.removeChild(box);
176        else
177                return false;
178}
179
180// Kill current paging.
181function kill_current_paging(){
182        var paging = Element("span_paging");
183        if (paging != null)
184                paging.parentNode.removeChild(paging);
185}
186
187function show_hide_span_paging(ID){
188        if (Element("span_paging"+ID))
189                Element("span_paging"+ID).style.display = '';
190        if (Element("span_paging"+currentTab))
191                Element("span_paging"+currentTab).style.display = 'none';
192}
193
194//Get the current number of messages in a page.
195function get_messages_number_in_page(){
196        //Get element tBody.
197        main = document.getElementById("tbody_box");
198               
199        // Get all TR (messages) in tBody.
200        main_list = main.childNodes;
201       
202        return main_list.length;
203}
204
205function download_local_attachment(url) {
206        if (div_attachment == null){
207                var div_attachment = document.createElement("DIV");
208                div_attachment.id="id_div_attachment";
209                document.body.appendChild(div_attachment);
210        }
211        div_attachment.innerHTML="<iframe style='display:none;width:0;height:0' name='attachment' src='"+url+"'></iframe>";
212}
213
214function download_attachments(msg_folder, msg_number, idx_file, msg_part, encoding, new_file_name, show_iframe){
215        div_attachment = document.getElementById("id_div_attachment");
216        var params = '';
217       
218        if (div_attachment == null){
219                var div_attachment = document.createElement("DIV");
220                div_attachment.id="id_div_attachment";
221                document.body.appendChild(div_attachment);
222        }
223        if(new_file_name)
224                params = "&newfilename="+escape(new_file_name);
225        if(encoding)
226                params += "&encoding="+encoding;
227
228        div_attachment.innerHTML="<iframe style='display:none;width:0;height:0' name='attachment' src='inc/gotodownload.php?msg_folder="+msg_folder+"&msg_number="+msg_number+"&idx_file="+idx_file+"&msg_part="+msg_part+params+"'></iframe>";
229}
230
231function download_all_attachments(msg_folder, msg_number){
232        var handler_source = function(data){           
233                download_attachments(null, null, data, null,null,'anexos.zip');
234        }
235        cExecute("$this.exporteml.download_all_attachments",handler_source,"folder="+msg_folder+"&num_msg="+msg_number);
236}
237//ADD forwarded files
238function addForwardedFile(id_border,file_name,link){
239        divFiles = document.getElementById("divFiles_"+id_border);
240
241        if (! divFiles)
242                return false;
243       
244        if (divFiles.lastChild)
245                var countDivFiles = parseInt(divFiles.lastChild.id.split('_')[2]) + 1;
246
247        if (! countDivFiles)
248                var countDivFiles = 1;
249       
250        divFile = document.createElement('DIV');
251       
252
253        if (!expresso_offline) {
254                if (!is_ie) {
255                        var inputFile = document.createElement("INPUT");
256                       
257                        var tmp_id_border = document.createAttribute('id_border');
258                        tmp_id_border.value = id_border;
259                       
260                        inputFile.setAttributeNode(tmp_id_border);
261                        inputFile.id = "inputFile_" + id_border + "_" + countDivFiles;
262                        inputFile.type = 'file';
263                        inputFile.size = 50;
264                        inputFile.maxLength = 255;
265                        inputFile.name = 'file_' + countDivFiles;
266                        inputFile.style.display = "none";
267                }
268                else {
269                        var inputFile = document.createElement("link");
270                       
271                        var tmp_id_border = document.createAttribute('id_border');
272                        tmp_id_border.value = id_border;
273                       
274                        inputFile.setAttributeNode(tmp_id_border);
275                        inputFile.id = "inputFile_" + id_border + "_" + countDivFiles;
276                        inputFile.name = 'file_' + countDivFiles;
277                       
278                       
279                }
280               
281        }
282        else {
283                var inputFile = document.createElement("input");
284                inputFile.type = 'hidden';
285                inputFile.name = 'offline_forward_' + countDivFiles;
286        }       
287        divFile.appendChild(inputFile);
288
289        var a_tmp = new Array();
290        a_tmp[0] = "local_";
291        a_tmp[1] = 'file_' + countDivFiles;
292        a_tmp[2] = file_name;
293        s_tmp = escape(connector.serialize(a_tmp));
294        var checkbox = document.createElement("INPUT");
295        checkbox.type = "checkbox";
296        checkbox.id = "checkbox_"+id_border+"_"+countDivFiles;
297        checkbox.name = "local_attachments[]";
298       
299        checkbox.value = s_tmp;
300        divFile.appendChild(checkbox);
301       
302        var link_attachment = document.createElement("A");
303        link_attachment.setAttribute("href", link);
304       
305        link_attachment.innerHTML = file_name;
306        divFile.appendChild(link_attachment);
307       
308        countDivFiles++;
309        divFile.id = "divFile_"+id_border+"_"+countDivFiles;   
310        divFiles.appendChild(divFile);
311
312        checkbox.checked = true; //Incrível, mas no Internet Explorer o checked só funciona efetivamente se o elemente já estiver renderizado... por isso só aqui eu seleciono ele...
313        return inputFile;
314}
315
316// Add Input File Dynamically.
317function addFile(id_border){
318        divFiles = document.getElementById("divFiles_"+id_border);
319        if (! divFiles)
320                return false;
321       
322        if (divFiles.lastChild)
323                var countDivFiles = parseInt(divFiles.lastChild.id.split('_')[2]) + 1;
324
325        if (! countDivFiles)
326                var countDivFiles = 1;
327       
328        divFile = document.createElement('DIV');
329       
330       
331        divFile.innerHTML = "<input type='file' id_border='"+id_border+"' size='50' maxLength='255' onchange=\"function () {validateFileExtension(this.value, this.id.replace('input','div'), this.getAttribute('id_border'));};\" id='"+"inputFile_"+id_border+"_"+countDivFiles+"' name='file_"+countDivFiles+"'>";
332
333       
334        var linkFile = document.createElement("A");
335        linkFile.id = "linkFile_"+id_border+"_"+countDivFiles;
336        linkFile.href='javascript:void(0)';
337        linkFile.onclick=function () {removeFile(this.id.replace("link","div")); return false;};
338        linkFile.innerHTML=get_lang("Remove"); 
339        //divFile.innerHTML += "&nbsp;&nbsp;";
340        divFile.appendChild(linkFile);
341        divFile.id = "divFile_"+id_border+"_"+countDivFiles;   
342        divFiles.appendChild(divFile);
343
344       
345
346        return document.getElementById("inputFile_"+id_border+"_"+countDivFiles);
347}
348//      Remove Input File Dynamically.
349function removeFile(id){
350        var border_id = id.substr(8,1);
351        var el = Element(id);
352        el.parentNode.removeChild(el);
353}
354
355function validateFileExtension(fileName, id, id_border){
356       
357        var error_flag  = false;
358
359        if ( fileName.indexOf('/') != -1 )
360        {
361                if (fileName[0] != '/'){ // file name is windows format?
362                        var file = fileName.substr(fileName.lastIndexOf('\\') + 1, fileName.length);
363                        if ((fileName.indexOf(':\\') != 1) && (fileName.indexOf('\\\\') != 0)) // Is stored in partition or a network file?
364                                error_flag = true;     
365                }
366                else // is Unix
367                        var file = fileName.substr(fileName.lastIndexOf('/') + 1, fileName.length);                                             
368        }
369        else  // is Firefox 3
370                var file = fileName;
371
372        var fileExtension = file.split(".");
373        fileExtension = fileExtension[(fileExtension.length-1)];
374        for(var i=0; i<denyFileExtensions.length; i++)
375        {
376                if(denyFileExtensions[i] == fileExtension)
377                {
378                        error_flag = true;
379                        break;
380                }
381                       
382        }
383
384        if ( error_flag == true )
385        {
386                alert(get_lang('File extension forbidden or invalid file') + '.');
387                removeFile(id);
388                addFile(id_border);
389                return false;
390        }
391        return true;
392}
393
394var setTimeout_write_msg = 0;
395var old_msg = false;
396// Funcao usada para escrever mensagem
397// notimeout = True : mensagem nao apaga
398function write_msg(msg, notimeout){     
399       
400        if (setTimeout_write_msg)
401                clearTimeout(setTimeout_write_msg);
402       
403        var msg_div = Element('em_div_write_msg');
404        var old_divStatusBar = Element("divStatusBar");
405
406        if(!msg_div) {
407                msg_div = document.createElement('DIV');
408                msg_div.id = 'em_div_write_msg';
409                msg_div.className = 'em_div_write_msg';
410                old_divStatusBar.parentNode.insertBefore(msg_div,old_divStatusBar);
411        }
412       
413        if( document.getElementById('JabberMessenger'))
414                loadscript.adIcon();
415
416        msg_div.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tbody><tr><th width="40%"></th><th noWrap class="action_info_th">'+msg+'</th><th width="40%"></th></tr></tbody></table>';
417
418        old_divStatusBar.style.display = 'none';
419        msg_div.style.display = '';     
420        // Nao ponha var na frente!! jakjr
421        handle_write_msg = function(){
422                try{
423                        if(!old_msg)
424                                clean_msg();
425                        else
426                                write_msg(old_msg, true);                       
427                }
428                catch(e){}
429        }
430        if(notimeout)
431                old_msg = msg;
432        else
433                setTimeout_write_msg = setTimeout("handle_write_msg();", 5000);
434}
435// Funcao usada para apagar mensagem sem timeout
436function clean_msg(){
437        old_msg = false;
438        var msg_div = Element('em_div_write_msg');
439        var old_divStatusBar = Element("divStatusBar");
440        if(msg_div)
441                msg_div.style.display = 'none';
442        old_divStatusBar.style.display = '';   
443}
444
445function make_body_reply(body, to, date_day, date_hour){
446        to = to.replace("<","&lt;");
447        to = to.replace(">","&gt;");
448        block_quoted_body ="<br><br>";
449        block_quoted_body += get_lang('At %1, %2 hours, %3 wrote:', date_day, date_hour, to);
450        block_quoted_body += "<blockquote style=\"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;\">";
451        block_quoted_body += body;
452        block_quoted_body += "</blockquote>";
453        return block_quoted_body;
454}
455
456function make_forward_body(body, from, date, subject, to, cc){
457        from = from.replace(/</g,"&lt;");
458        from = from.replace(/>/g,"&gt;");
459        to = to.replace(/</g,"&lt;");
460        to = to.replace(/>/g,"&gt;");
461        var forward_body = '<BR><BR>---------- ' + get_lang('Forwarded message') + ' ----------<BR>';
462        forward_body += get_lang('From') + ': ' + from + '<BR>';
463        forward_body += get_lang('Date') + ': ' + date + '<BR>';
464        forward_body += get_lang('Subject') + ': ' + subject + '<BR>';
465        forward_body += get_lang('To') + ': ' + to+ '<BR>';
466        if(cc != undefined){
467                cc = cc.replace(/</g,"&lt;");
468                cc = cc.replace(/>/g,"&gt;");
469                forward_body += get_lang('CC') + ': ' + cc+ '<BR><BR>';
470        }
471        forward_body += body;
472        return forward_body;
473}
474
475function emMessageSearch(e,value){
476        var     e  = is_ie ? window.event : e;
477        if(e.keyCode == 13) {
478                search_emails(value);
479        }
480}
481
482function validateEmail(email){
483        if (typeof(email) != 'string')
484                return false;
485        var validName = /^[a-z][a-z-_0-9\.]*/i;
486        emailParts = email.split('@');
487        return (validName.test(emailParts[0]) && validateDomain(emailParts[1]));
488}
489function validateDomain(domain){
490        var domainReg = /^(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/i;
491        return (domainReg.test(domain));
492}
493
494function validateUrl(url){
495        var urlReg = /([A-Za-z]{2,7}:\/\/)(.*)/i;
496        urlParts = url.split(urlReg);
497        return (urlParts[1].length > 4 &&  validateDomain(urlParts[2]));
498}
499function emQuickSearch(emailList, field, ID){
500        var quickSearchKeyBegin;
501        var quickSearchKeyEnd;
502        if(expresso_offline) {
503                alert(get_lang('Not allowed in offline mode'));
504                return;
505        }
506               
507        var handler_emQuickSearch = function(data)
508        {
509                window_DropDownContacts = Element('tipDiv');
510                if (window_DropDownContacts.style.visibility != 'hidden'){
511                        window_DropDownContacts.style.visibility = 'hidden';
512                }
513               
514                if ((!data.status) && (data.error == "many results")){
515                        alert(get_lang('More than %1 results. Please, try to refine your search.',200));
516                        return false;
517                }
518                       
519                if (data.length > 0){
520                        if ((field != 'null') && (ID != 'null'))
521                        {
522                                connector.loadScript("QuickCatalogSearch");
523                                if (typeof(QuickCatalogSearch) == 'undefined'){
524                                        setTimeout('emQuickSearch("'+emailList+'", "'+field+'", "'+ID+'")',500);
525                                        return false;
526                                }
527                                QuickCatalogSearch.showList(data, quickSearchKeyBegin, quickSearchKeyEnd);
528                        }
529                        else
530                        {
531                                connector.loadScript("QuickSearchUser");
532                                if (typeof(QuickSearchUser) == 'undefined'){
533                                        setTimeout('emQuickSearch("'+emailList+'", "'+field+'", "'+ID+'")',500);
534                                        return false;
535                                }
536                                QuickSearchUser.showList(data);
537                        }
538                }
539                else
540                        alert(get_lang('None result was found.'));
541                return true;
542        }
543       
544        if ((field != 'null') && (ID != 'null'))
545        {
546                Element(field +'_'+ ID).focus(); //It requires for IE.
547                var i = getPosition(Element(field +'_'+ ID)); //inputBox.selectionStart;
548                var j = --i;
549
550                // Acha o inicio
551        while ((j >= 0) && (emailList.charAt(j) != ',')){j--};
552            quickSearchKeyBegin = ++j;
553
554            // Acha o final
555        while ((i <= emailList.length) && (emailList.charAt(i) != ',')){i++};
556            quickSearchKeyEnd = i;
557
558            // A Chave da Pesquisa
559        var search_for = trim(emailList.substring(quickSearchKeyBegin, quickSearchKeyEnd));
560        }
561        else
562                var search_for = emailList;
563       
564        if (search_for.length < preferences.search_characters_number){
565            alert(get_lang('Your search argument must be longer than %1 characters.', preferences.search_characters_number));
566            return false;
567        }
568       
569        cExecute ("$this.ldap_functions.quicksearch&search_for="+search_for+"&field="+field+"&ID="+ID, handler_emQuickSearch);
570}
571
572function folderbox(){
573        connector.loadScript("TreeS");
574        if (typeof(ttree) == 'undefined'){
575                setTimeout('folderbox()',500);
576                return false;
577        }
578        ttree.make_Window();
579}
580
581function filterbox(){
582        connector.loadScript("filter");
583        connector.loadScript("filters");
584        if (typeof(filters) == 'undefined')
585        {
586                 setTimeout('filterbox()',500);
587                 return false;
588        }
589        filters.Forms();
590}
591
592function sharebox(){
593        var handler_imap_getacl = function(data)
594        {
595                var options = '';
596                for (var x in data)
597                {
598                        options += "<option value='"+data[x].uid+"'>"+data[x].cn+"</option>";
599                }
600                connector.loadScript("sharemailbox");
601                if (typeof(sharemailbox) == 'undefined')
602                {
603                        setTimeout('sharebox()',500);
604                        return false;
605                }
606                sharemailbox.makeWindow(options);
607        }
608        cExecute ("$this.imap_functions.getacl", handler_imap_getacl);
609}
610
611function open_rss(param){
612        connector.loadScript("news_edit");
613        if (typeof(news_edit) == 'undefined')
614        {
615                setTimeout('open_rss(\''+param+'\')',500);
616                return false;
617        }
618        news_edit.read_rss(param);
619        return true;
620}
621
622function editrss(){
623        connector.loadScript("news_edit");
624        if (typeof(news_edit) == 'undefined')
625        {
626                setTimeout('editrss()',500);
627                return false;
628        }
629        news_edit.makeWindow();
630}
631
632
633
634
635function preferences_mail(){
636        location.href="../preferences/preferences.php?appname=expressoMail1_2";
637}
638
639function search_emails(value){
640        connector.loadScript("TreeS");
641        connector.loadScript("search");
642        if (typeof(EsearchE) == 'undefined' || typeof(ttree) == 'undefined'){
643                setTimeout('search_emails("'+value+'")',500);
644                return false;
645        }
646        EsearchE.showForms(value);
647}
648
649function source_msg(id_msg,folder){
650        var num_msg = id_msg.substr(0,(id_msg.length - 2));
651        var handler_source = function(data){
652                download_attachments(null, null, data, null,null,'fonte_da_mensagem.eml');
653        }
654        cExecute("$this.exporteml.export_msg",handler_source,"folder="+url_decode(folder)+"&msgs_to_export="+num_msg);
655}
656
657function url_encode(str){
658    var hex_chars = "0123456789ABCDEF";
659    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
660    var n, strCode, hex1, hex2, strEncode = "";
661
662    for(n = 0; n < str.length; n++) {
663        if (noEncode.test(str.charAt(n))) {
664            strEncode += str.charAt(n);
665        } else {
666            strCode = str.charCodeAt(n);
667            hex1 = hex_chars.charAt(Math.floor(strCode / 16));
668            hex2 = hex_chars.charAt(strCode % 16);
669            strEncode += "%" + (hex1 + hex2);
670        }
671    }
672    return strEncode;
673}
674
675function url_decode(str) {
676
677        var n, strCode, strDecode = "";
678        for (n = 0; n < str.length; n++) {
679            if (str.charAt(n) == "%") {
680                strCode = str.charAt(n + 1) + str.charAt(n + 2);
681                strDecode += String.fromCharCode(parseInt(strCode, 16));
682                n += 2;
683            } else {
684                strDecode += str.charAt(n);
685            }
686        }
687        return strDecode;
688}
689
690function Element (el) {
691        return  document.getElementById(el);
692}
693
694function getPosition(obj)
695{
696        if(typeof obj.selectionStart != "undefined")
697        {
698        return obj.selectionStart;
699        }
700        else if(document.selection && document.selection.createRange)
701        {
702                var M = document.selection.createRange();
703                try
704                {
705                        var Lp = M.duplicate();
706                        Lp.moveToElementText(obj);
707                }
708                catch(e)
709                {
710                        var Lp=obj.createTextRange();
711                }
712               
713                Lp.setEndPoint("EndToStart",M);
714                var rb=Lp.text.length;
715               
716                if(rb > obj.value.length)
717                {
718                        return -1;
719                }
720                return rb;
721        }
722}
723
724function trim(inputString) {
725   if (typeof inputString != "string")
726        return inputString;
727     
728   var retValue = inputString;
729   var ch = retValue.substring(0, 1);
730   while (ch == " ") {
731          retValue = retValue.substring(1, retValue.length);
732          ch = retValue.substring(0, 1);
733   }
734   ch = retValue.substring(retValue.length-1, retValue.length);
735   while (ch == " ") {
736          retValue = retValue.substring(0, retValue.length-1);
737          ch = retValue.substring(retValue.length-1, retValue.length);
738   }
739   while (retValue.indexOf("  ") != -1) {
740          retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
741   }
742   return retValue;
743}
744
745function increment_folder_unseen(){
746        var folder_id = get_current_folder();
747
748        var folder_unseen = Element('dftree_'+folder_id+'_unseen');
749        var abas_unseen = Element('new_m').innerHTML;
750        abas_unseen = abas_unseen.match( /[0-9]+/);
751
752        if (folder_unseen)
753        {
754                folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) + 1);
755        }
756        else
757        {
758                tree_folders.getNodeById(folder_id).alter({caption: tree_folders.getNodeById(current_folder).caption + '<font style=color:red>&nbsp(</font><span id="dftree_'+current_folder+'_unseen" style=color:red>1</span><font style=color:red>)</font>'});
759                tree_folders.getNodeById(folder_id)._refresh();
760        }
761        if (abas_unseen)
762        {
763                abas_unseen = parseInt(abas_unseen) + 1;
764                Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
765        }
766        else{
767                abas_unseen = Element('new_m').innerHTML;
768                abas_unseen = abas_unseen.match( /[0-9]+/);
769                abas_unseen = parseInt(abas_unseen) + 1;
770                Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
771        }
772        if ( current_folder.indexOf( 'INBOX' ) !== 0 && current_folder.indexOf( 'local_' ) !== 0 )
773        {
774                var display_unseen_in_shared_folders = Element('dftree_user_unseen');
775                if ( display_unseen_in_shared_folders )
776                        tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">' + ( parseInt( display_unseen_in_shared_folders.innerHTML) + 1 ) + '</span><font style=color:red>]</font>' + get_lang("Shared folders")});
777                else
778                        tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_user_unseen" style="color:red">1</span><font style=color:red>]</font>' + get_lang("Shared folders")});
779                tree_folders.getNodeById( 'user' )._refresh();
780        }
781        var display_unseen_in_mailbox = Element('dftree_root_unseen');
782        if(!expresso_offline)
783                var node_to_refresh = 'root';
784        else
785                var node_to_refresh = 'local_root';
786        tree_folders.getNodeById( node_to_refresh )._refresh();
787}
788function decrement_folder_unseen(){
789        var folder_id = get_current_folder();
790
791        var folder_unseen = Element('dftree_'+folder_id+'_unseen');
792        var abas_unseen = Element('new_m').innerHTML;
793        abas_unseen = abas_unseen.match( /[0-9]+/);
794
795        if(!folder_unseen || !abas_unseen)
796                return;
797
798        if ((folder_unseen) && (parseInt(folder_unseen.innerHTML) > 1))
799        {
800                folder_unseen.innerHTML = (parseInt(folder_unseen.innerHTML) - 1);
801        }
802        else if (parseInt(folder_unseen.innerHTML) <= 1)
803        {
804                var tmp_folder_name = tree_folders.getNodeById(folder_id).caption.split('<');
805                var folder_name = tmp_folder_name[0];
806                tree_folders.getNodeById(folder_id).alter({caption: folder_name});
807                tree_folders.getNodeById(folder_id)._refresh();
808        }
809        if ((abas_unseen) && (parseInt(abas_unseen) > 1))
810        {
811                abas_unseen = (parseInt(abas_unseen) - 1);
812                Element('new_m').innerHTML = '<font style="color:red">' + abas_unseen + '</font>';
813        }
814        else if (parseInt(abas_unseen) <= 1)
815        {
816                Element('new_m').innerHTML = '0';
817        }
818        if ( current_folder.indexOf( 'INBOX' ) !== 0 )
819        {
820                var display_unseen_in_shared_folders = Element('dftree_user_unseen');
821                if ( display_unseen_in_shared_folders )
822                {
823                        var unseen_in_shared_folders = parseInt( display_unseen_in_shared_folders.innerHTML );
824                        unseen_in_shared_folders--;
825                        if ( unseen_in_shared_folders > 0 )
826                                tree_folders.getNodeById( 'user' ).alter({caption:'<font style=color:red>[</font><span id="dftree_root_unseen" style="color:red">' + unseen_in_shared_folders + '</span><font style=color:red>]</font>' + get_lang("My Folders")});
827                        else
828                                tree_folders.getNodeById( 'user' ).alter({caption:get_lang("Shared folders")});
829                        tree_folders.getNodeById( 'user' )._refresh();
830                }
831        }
832        var display_unseen_in_mailbox = Element('dftree_root_unseen');
833        if ( display_unseen_in_mailbox )
834        {
835                var unseen_in_mailbox = parseInt( display_unseen_in_mailbox.innerHTML );
836                unseen_in_mailbox--;
837                //if ( unseen_in_mailbox > 0 )
838                //      tree_folders.getNodeById( 'root' ).alter({caption:'<font style=color:red>[</font><span id="dftree_root_unseen" style="color:red">' + unseen_in_mailbox + '</span><font style=color:red>]</font>' + get_lang("My Folders")});
839                //else
840                if(!expresso_offline)
841                        var node_to_refresh = 'root';
842                else
843                        var node_to_refresh = 'local_root';
844                tree_folders.getNodeById( node_to_refresh ).alter({caption:get_lang("My Folders")});
845                tree_folders.getNodeById( node_to_refresh )._refresh();
846        }
847}
848
849function LTrim(value){
850        var w_space = String.fromCharCode(32);
851        var strTemp = "";
852        var iTemp = 0;
853
854        var v_length = value ? value.length : 0;
855        if(v_length < 1)
856                return "";
857
858        while(iTemp < v_length){
859                if(value && value.charAt(iTemp) != w_space){
860                        strTemp = value.substring(iTemp,v_length);
861                        break;
862                }
863                iTemp++;
864        }
865        return strTemp;
866}
867
868//changes MENU background color.
869function set_menu_bg(menu)
870{
871        menu.style.backgroundColor = 'white';
872        menu.style.border = '1px solid black';
873        menu.style.padding = '0px 0px';
874}
875//changes MENU background color.
876function unset_menu_bg(menu)
877{
878        menu.style.backgroundColor = '';
879        menu.style.border = '0px';
880        menu.style.padding = '1px 0px';
881}
882
883function array_search(needle, haystack) {
884        var n = haystack.length;
885        for (var i=0; i<n; i++) {
886                if (haystack[i]==needle) {
887                        return true;
888                }
889        }
890        return false;
891}
892
893function lang_folder(fn) {
894        if (fn.toUpperCase() == "INBOX") return get_lang("Inbox");
895        if (special_folders[fn] && typeof(special_folders[fn]) == 'string') {
896                return get_lang(special_folders[fn]);
897        }
898        return fn;
899}
900
901function add_className(obj, className){
902        if (obj && !exist_className(obj, className))
903                obj.className = obj.className + ' ' + className;
904}
905
906function remove_className(obj, className){
907        var re = new RegExp("\\s*"+className);
908        if (obj)
909                obj.className = obj.className.replace(re, ' ');
910}
911
912function exist_className(obj, className){
913        return ( obj && obj.className.indexOf(className) != -1 )
914}
915
916function select_all_messages(select)
917{
918        var main = Element("tbody_box");
919        var main_list = main.childNodes;
920        var len_main_list = main_list.length;
921       
922        if (select)
923        {
924                for (i=0; i<len_main_list; i++)
925                {
926                        Element("check_box_message_"+main_list[i].id).checked = true;
927                        add_className(Element(main_list[i].id), 'selected_msg');
928                }
929        }
930        else
931        {
932                for (i=0; i<len_main_list; i++)
933                {
934                        Element("check_box_message_"+main_list[i].id).checked = false;
935                        remove_className(Element(main_list[i].id), 'selected_msg');
936                }
937        }
938}
939
940function borkb(size){
941        kbyte = 1024;
942        mbyte = kbyte*1024;
943        gbyte = mbyte*1024;
944        if (!size)
945                size = 0;
946        if (size < kbyte)
947                return size + ' B';
948        else if (size < mbyte)
949                return parseInt(size/kbyte) + ' KB';
950        else if (size < gbyte)
951                if (size/mbyte > 100)
952                        return (size/mbyte).toFixed(0) + ' MB';
953                else
954                        return (size/mbyte).toFixed(1) + ' MB';
955        else
956                return (size/gbyte).toFixed(1) + ' GB';
957}
958
959function validate_date(date){
960    if (date.match(/^[0-3][0-9]\/[0-1][0-9]\/\d{4,4}$/))
961    {
962        tmp = date.split('/');
963
964        day = new Number(tmp[0]);
965        month = new Number(tmp[1]);
966        year = new Number(tmp[2]);
967        if (month >= 1 && month <= 12 && day >= 1 && day <= 31)
968        {
969            if (month == 02 && day <= 29)
970            {
971                return true;
972            }
973            return true;
974        }
975        else
976            {
977                return false;
978            }
979    }
980    else
981        {
982            return false;
983        }
984}
Note: See TracBrowser for help on using the repository browser.