source: branches/1.2/contactcenter/js/cc.js @ 503

Revision 503, 73.3 KB checked in by niltonneto, 16 years ago (diff)

Implementação para trazer os contatos compartilhados.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1  /******************************************************************************\
2  * eGroupWare - Contacts Center                                                                                                *
3  * http://www.egroupware.org                                                                                                   *
4  * Written by:                                                                                                                                 *
5  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>                   *
6  *  - Jonas Goes <jqhcb@users.sourceforge.net>                                                                 *
7  *  sponsored by Thyamad - http://www.thyamad.com                                                              *
8  * -------------------------------------------------------------------------   *
9  *  This program is free software; you can redistribute it and/or modify it    *
10  *  under the terms of the GNU General Public License as published by the              *
11  *  Free Software Foundation; either version 2 of the License, or (at your             *
12  *  option) any later version.                                                                                                 *
13  \******************************************************************************/
14
15/***********************************************\
16*                      TODO                     *
17\***********************************************/
18
19/*
20 * function setHeightSpace ()
21 *
22 */
23
24/***********************************************\
25*                   CONSTANTS                   *
26\***********************************************/
27
28var CC_STATUS_FULL_ADD = 2;
29var CC_STATUS_QUICK_ADD = 1;
30
31var CC_card_image_width = 245;
32var CC_card_image_height = 130;
33var CC_card_extra = 16;
34
35
36/***********************************************\
37*               GLOBALS VARIABLES               *
38\***********************************************/
39
40var CC_visual = 'table';
41/* Cards Variables */
42var CC_actual_letter = 'a';
43var CC_last_letter = 'a';
44var CC_actual_page = 1;
45var CC_npages = 0;
46var CC_max_cards = new Array();
47var CC_conn_count=0;
48
49var CC_old_icon_w = 0;
50var CC_old_icon_h = 0;
51
52/* Tabs Variables */
53var CC_last_tab = 0;
54
55/* Pseudo-Semafores */
56var CC_tree_available = false;
57var CC_full_add_const = false;
58var CC_full_add_photo = false;
59       
60var CC_last_height = window.innerHeight;
61var CC_last_width = window.innerWidth;
62
63/* Contact Full Info */
64var CC_contact_full_info;
65var CC_br_index;
66
67/* Addresses Variables */
68var CC_addr_last_selected = 0;
69
70/* Connections Variables */
71var CC_conn_last_selected = 0;
72
73/***********************************************\
74 *           FULL ADD/EDIT FUNCTIONS           *
75\***********************************************/
76
77function createPhotoFrame()
78{
79        photo_frame = document.createElement('iframe');
80        document.body.appendChild(photo_frame);
81
82        if (is_ie)
83        {
84                photo_form  = photo_frame.contentWindow.document.createElement('form');
85                photo_input = photo_frame.contentWindow.document.createElement('input');
86        }
87        else
88        {
89                 photo_form  = photo_frame.contentDocument.createElement('form');
90                 photo_input = photo_frame.contentDocument.createElement('input');
91        }
92       
93        photo_frame.id = 'cc_photo_frame';
94        photo_frame.style.position = 'absolute';
95        //photo_frame.style.visibility = 'hidden';
96        photo_frame.style.top = '600px';
97        photo_frame.style.left = '0px';
98       
99        photo_form.id = 'cc_photo_form';
100        photo_form.method = 'POST';
101        photo_form.enctype = 'multipart/form-data';
102       
103        photo_input.id = 'cc_photo_input';
104        photo_input.type = 'file';
105       
106        if (is_ie)
107        {
108                photo_frame.contentWindow.document.body.appendChild(photo_form);
109        }
110        else
111        {
112                photo_frame.contentDocument.body.appendChild(photo_form);
113        }
114        photo_form.appendChild(photo_input);
115       
116}
117
118/********* Full Add Auxiliar Functions ****************/
119function selectOption (id, option)
120{
121        var obj = Element(id);
122        var max = obj.options.length;
123       
124        if (option == undefined)
125        {
126                obj.selectedIndex = 0;
127        }
128        else
129        {
130                for (var i = 0; i < max; i++)
131                {
132                        if (obj.options[i].value == option)
133                        {
134                                obj.selectedIndex = i;
135                                break;
136                        }
137                }
138        }
139}
140
141function selectRadio (id, index)
142{
143        var obj = Element(id);
144        var max = obj.options.length;
145        for (var i = 0; i < max; i++)
146        {
147                i == index ? obj.options[i].checked = true : obj.options[i].checked = false;
148        }
149}
150
151function clearSelectBox(obj, startIndex)
152{
153        var nOptions = obj.options.length;
154
155        for (var i = nOptions - 1; i >= startIndex; i--)
156        {
157                obj.removeChild(obj.options[i]);
158        }
159}
160/********** Open/Close FullAdd *************/
161function openFullAdd(){
162        // Build the FullAdd Window.
163        if(!fullAddWin && !is_ie)
164                __f();
165
166        resetFullAdd();
167        populateFullAddConst();
168        fullAddWin.open();
169        tabs._showTab('cc_contact_tab_0');
170        Element("cc_conn_type_1").checked = false;     
171        Element("cc_conn_type_2").checked = false;
172        Element("cc_conn_type_sel").disabled = true;
173        Element("cc_conn_type_sel").selectedIndex = 0;
174}
175
176function closeFullAdd(){
177        fullAddWin.close();
178}
179/********** New Contact *************/
180function newContact(){
181        openFullAdd();
182}
183/************ Edit Contact *************/
184function editContact (id){
185        openFullAdd();
186        populateFullEdit(id,'bo_people_catalog');
187}
188function editSharedContact (id){
189        openFullAdd();
190        populateFullEdit(id,'bo_shared_catalog_manager');
191}
192/************ Edit Group *************/
193function editGroup(id){
194        populateEditGroup(id); 
195}
196/*
197        Updates all the constant fields in the
198        full add window, like Prefixes, Suffixes,
199        Countries and Types
200*/
201function populateFullAddConst()
202{
203        CC_full_add_const = false;
204       
205        setTimeout('populateFullAddConstAsync()', 10);
206}
207
208function populateFullAddConstAsync()
209{
210        var handler = function(responseText)
211        {
212                //Element('cc_debug').innerHTML = responseText;         
213                var data = unserialize(responseText);
214                var i = 1;
215                var j;
216               
217                if (typeof(data) != 'object')
218                {
219                        showMessage(Element('cc_msg_err_contacting_server').value);
220                        return;
221                }
222               
223                /* Populate Prefixes */
224                for (j in data[0])
225                {
226                        Element('cc_pd_prefix').options[i] = new Option(data[0][j], j);
227                        i++;
228                }
229               
230                /* Populate Suffixes */
231                i = 1;
232                for (j in data[1])
233                {
234                        Element('cc_pd_suffix').options[i] = new Option(data[1][j], j);
235                        i++;
236                }
237
238                /* Populate Addresses Types */
239                i = 1;
240                for (j in data[2])
241                {
242                        Element('cc_addr_types').options[i] = new Option(data[2][j], j);
243                        i++;
244                }
245
246                /* Populate Countries */
247                i = 1;
248                for (j in data[3])
249                {
250                        Element('cc_addr_countries').options[i] = new Option(data[3][j], j);
251                       
252                        if (j == 'BR' || j == 'br')
253                        {
254                                CC_br_index = i;
255                        }
256                       
257                        i++;
258                }
259               
260                /* Populate Connection Types */
261                i = 1;
262                for (j in data[4])
263                {
264                        Element('cc_conn_type').options[i] = new Option(data[4][j], j);
265                        i++;
266                }
267               
268                /* Populate Relations Types */
269                i = 0;
270                for (j in data[5])
271                {
272                        Element('cc_rels_type').options[i] = new Option(data[5][j], j);
273                        i++;
274                }
275               
276                CC_full_add_const = true;
277
278        };
279
280        Connector.newRequest('populateFullAddConst', CC_url+'get_contact_full_add_const', 'GET', handler);
281}
282
283function populateFullEdit (id,catalog)
284{
285        var handler = function(responseText)
286        {
287                //Element('cc_debug').innerHTML = responseText;
288                var data = unserialize(responseText);
289
290                if (typeof(data) != 'object' || data['result'] != 'ok')
291                {
292                        showMessage(Element('cc_msg_err_contacting_server').value);
293                        return;
294                }
295
296                resetFullAdd();
297
298                CC_contact_full_info = data;
299                Element('cc_full_add_contact_id').value = data['cc_full_add_contact_id'];
300                populatePersonalData(data['personal']);
301                //populateRelations(data['relations']);
302        };
303
304        Connector.newRequest('populateFullEdit', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_full_data&id=' + id + "&catalog="+catalog, 'GET', handler);
305}
306
307function populateEditGroup (id)
308{
309        var handler = function(responseText)
310        {                       
311                var data = unserialize(responseText);
312
313                Element('group_id').value = data['id_group'];                                                           
314                var options_contact_list = Element('span_contact_list');
315                var select_contact_list = '<select id="contact_list" multiple name="contact_list[]" style="width:280px" size="10">';
316                select_contact_list += data['contact_list'] + "</select>";
317                options_contact_list.innerHTML = select_contact_list;
318
319                if(data['id_group']) {
320               
321                        if(data['contact_in_list']) {                                   
322                                for(i = 0; i < data['contact_in_list'].length; i++) {                           
323                                        option = document.createElement('option');
324                                        option.value = data['contact_in_list'][i]['id_connection'];
325                                        option.text = data['contact_in_list'][i]['names_ordered']+' ('+data['contact_in_list'][i]['connection_value']+')';                             
326                                        Element('contact_in_list').options[Element('contact_in_list').options.length] = option;
327                                }
328                        }               
329                       
330                        Element('title').value =  data['title'];
331                }
332                               
333                if (typeof(data) != 'object' || data['result'] != 'ok')
334                {
335                        showMessage(Element('cc_msg_err_contacting_server').value);
336                        return;
337                }
338                ccAddGroup.window.open();
339        };
340               
341        id = typeof(id) == 'undefined' ? id = 0 :  id;
342       
343        ccAddGroup.clear(true);                 
344        Connector.newRequest('populateEditGroup', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_group&id='+id, 'GET', handler);
345}
346
347
348function resetFullAdd()
349{
350        /* Clear information container */
351        CC_contact_full_info = new Array();
352
353        /* Clear Fields */
354        Element('cc_full_add_form_personal').reset();
355        Element('cc_full_add_form_addrs').reset();
356
357        /* Personal Data */
358        Element('cc_full_add_contact_id').value = null;
359        Element('cc_pd_photo').src = 'templates/default/images/photo.png';
360
361        /* Addresses */
362        resetAddressFields();
363
364        /* Connections */
365        CC_conn_last_selected = '_NONE_';
366        Element("cc_phone_default").options.selectedIndex = '-1';
367        Element("cc_email_default").options.selectedIndex = '-1';
368        Element("div_cc_conn_is_default").style.display = 'none';
369        clearConn();
370}
371
372function postFullAdd()
373{
374        if (!checkFullAdd())
375        {
376                return false;
377        }
378       
379        /* First thing: Send Photo */
380        if (Element('cc_pd_select_photo').value != '' && !is_ie)
381        {
382                var nodes;
383                var form, frame, old_frame;
384
385                CC_full_add_photo = false;
386
387                old_frame = Element('cc_photo_frame');
388                if (!old_frame)
389                {
390                        frame = document.createElement('iframe');
391                }
392                else
393                {
394                        frame = old_frame;
395                }
396               
397                frame.id = 'cc_photo_frame';
398                frame.style.visibility = 'hidden';
399                frame.style.top = '0px';
400                frame.style.left = '0';
401                frame.style.position = 'absolute';
402                document.body.appendChild(frame);
403
404                form = frame.contentDocument.createElement('form');
405               
406                var id_contact = Element('cc_full_add_contact_id').value;
407                form.id = 'cc_form_photo';
408                form.method = 'POST';
409                form.enctype = 'multipart/form-data';
410                form.action = 'http://'+ document.domain + Element('cc_root_dir').value+'../index.php?menuaction=contactcenter.ui_data.data_manager&method=post_photo&id='+(id_contact != '' && id_contact != 'null' ? id_contact : '');
411               
412                var input_clone = Element('cc_pd_select_photo').cloneNode(false);
413                form.appendChild(input_clone);
414               
415                frame.contentDocument.body.appendChild(form);
416                form.submit();
417
418                CC_full_add_photo = true;
419        }
420        else if (Element('cc_pd_select_photo_t').value != '' && is_ie)
421        {
422                CC_full_add_photo = false;
423
424                var frame = Element('cc_photo_frame');
425                var form = frame.contentWindow.document.all['cc_photo_form'];
426                var id_contact = Element('cc_full_add_contact_id').value;
427                form.action = 'http://'+ document.domain + Element('cc_root_dir').value+'../index.php?menuaction=contactcenter.ui_data.data_manager&method=post_photo&id='+(id_contact != '' && id_contact != 'null' ? id_contact : '');
428
429                form.submit();
430
431                setTimeout('Element(\'cc_photo_frame\').src = \'cc_photo_frame.html\'', 1000);
432                CC_full_add_photo = true;
433        }
434
435        setTimeout('postFullAddInfo()', 100);
436}
437
438function postFullAddInfo()
439{
440        var handler = function (responseText)
441        {
442                var data = unserialize(responseText);
443
444                if (typeof(data) != 'object')
445                {
446                        showMessage(Element('cc_msg_err_contacting_server').value);
447                        return;
448                }
449
450                if (data['status'] != 'ok')
451                {
452                        showMessage(data['msg']);
453                        return;
454                }
455
456                fullAddWin.close();
457                updateCards();
458        };
459
460        Connector.newRequest('postFullAddInfo', CC_url+'post_full_add', 'POST', handler, getFullAddData());
461}
462
463function getFullAddData()
464{
465        var data = new Array();
466        var empty = true;
467        var replacer = '__##AND##__';
468       
469        data['commercialAnd'] = replacer;
470       
471        if (Element('cc_full_add_contact_id').value != '' && Element('cc_full_add_contact_id').value != 'null')
472        {
473                data['id_contact'] = replaceComAnd(Element('cc_full_add_contact_id').value, replacer);
474                data.length++;
475        }
476
477        /* Status: Full Added */
478        data['id_status'] = CC_STATUS_FULL_ADD;
479       
480        /* Personal Data */
481        data['alias']         = replaceComAnd(Element('cc_pd_alias').value, replacer);
482        data['id_prefix']     = replaceComAnd(Element('cc_pd_prefix').value, replacer);
483        data['given_names']   = replaceComAnd(Element('cc_pd_given_names').value, replacer);
484        data['family_names']  = replaceComAnd(Element('cc_pd_family_names').value, replacer);
485        data['names_ordered'] = replaceComAnd(data['given_names']+" "+data['family_names'], replacer);
486        data['id_suffix']     = replaceComAnd(Element('cc_pd_suffix').value, replacer);;
487        data['birthdate_0']   = replaceComAnd(Element('cc_pd_birthdate_0').value, replacer);
488        data['birthdate_1']   = replaceComAnd(Element('cc_pd_birthdate_1').value, replacer);
489        data['birthdate_2']   = replaceComAnd(Element('cc_pd_birthdate_2').value, replacer);
490//      data['sex']           = Element('cc_pd_sex').value == 1 ? 'M' : Element('cc_pd_sex').value == 2 ? 'F' : null;
491        data['pgp_key']       = replaceComAnd(Element('cc_pd_gpg_finger_print').value, replacer);
492        data['notes']         = replaceComAnd(Element('cc_pd_notes').value, replacer);
493
494        data.length += 14;
495
496        /* Addresses */
497        saveAddressFields();
498        data['addresses'] = CC_contact_full_info['addresses'];
499
500        /* Connection */
501        saveConnFields();
502
503        if (CC_contact_full_info['connections'])
504        {
505                var connNumber = 0;
506                for (var type in CC_contact_full_info['connections'])
507                {
508                        if (type == 'length')
509                        {
510                                continue;
511                        }
512
513                        if (typeof(data['connections']) != 'object')
514                        {
515                                data['connections'] = new Array();
516                        }
517                       
518                        for (var i in CC_contact_full_info['connections'][type])
519                        {
520                                if (i == 'length')
521                                {
522                                        continue;
523                                }
524
525                                if (typeof(data['connections']['connection'+connNumber]) != 'object')
526                                {
527                                        data['connections']['connection'+connNumber] = new Array(5);
528                                }
529                               
530                                data['connections']['connection'+connNumber]['id_connection'] = CC_contact_full_info['connections'][type][i]['id'];
531                                data['connections']['connection'+connNumber]['id_typeof_connection'] = type;
532                                data['connections']['connection'+connNumber]['connection_name'] = CC_contact_full_info['connections'][type][i]['name'];
533                                data['connections']['connection'+connNumber]['connection_value'] = CC_contact_full_info['connections'][type][i]['value'];
534                                if(Element("cc_"+(type == 1 ? 'email' : 'phone')+"_default").value) {
535                                        if(Element("cc_"+(type == 1 ? 'email' : 'phone')+"_default").value == CC_contact_full_info['connections'][type][i]['name']){
536                                                data['connections']['connection'+connNumber]['connection_is_default']  = 'TRUE';
537                                        }
538                                        else
539                                                data['connections']['connection'+connNumber]['connection_is_default']  = 'FALSE';
540                                }
541
542//                              data['connections']['connection'+connNumber].length = 5;
543                               
544                                empty = false;
545                                connNumber++;
546                                data['connections'].length++;
547                        }
548
549                }
550               
551                if (!empty)
552                {
553                        data.length++;
554                        empty = true;
555                }
556        }
557       
558        if (CC_contact_full_info['removed_conns'])
559        {
560                empty = false;
561               
562                if (typeof(data['connections']) != 'object')
563                {
564                        data['connections'] = new Array();
565                        data.length++;
566                }
567
568                data['connections']['removed_conns'] = CC_contact_full_info['removed_conns'];
569                data['connections'].length++;
570        }
571
572        return 'data=' + escape(serialize(data));
573}
574
575function checkFullAdd()
576{
577        /* Check Personal Data */
578
579        if (Element('cc_pd_given_names').value == '')
580        {
581                showMessage(Element('cc_msg_err_empty_field').value + " => " + Element('cc_pd_given_names').name);
582                return false;
583        }
584
585        /* Check Addresses */
586
587        /* Check Connections */
588
589        saveConnFields();
590
591        var comp = /^[a-zA-Z\d(-)\.@_ -]{0,200}$/;
592        if (CC_contact_full_info['connections']){
593                for (var type in CC_contact_full_info['connections']){
594                        for (var i in CC_contact_full_info['connections'][type]){
595                                if((CC_contact_full_info['connections'][type][i]['value'].length < 4) ||
596                                        (!comp.test(CC_contact_full_info['connections'][type][i]['value']))){
597                                        showMessage('Endereço para conexão de ' + CC_contact_full_info['connections'][type][i]['name'] + ', não é válido');
598                                        return false;
599                                }
600                        }
601                        var _options_default = Element("cc_"+(type == 1 ? 'email' : 'phone')+"_default");
602                        if(_options_default.value == '-1') {
603                                alert("É necessário escolher um "+ (type == 1 ? 'E-mail' : 'Telefone')+" como padrão!");
604                                return false;
605                        }
606                }
607               
608        }
609
610        /* Check Relations */
611
612        return true;
613}
614
615/********* Personal Data Functions *********/
616/*
617 * data[0] => cc_pd_select_photo
618 * data[1] => cc_pd_alias
619 * data[2] => cc_pd_given_names
620 * data[3] => cc_pd_family_names
621 * data[4] => cc_pd_full_name
622 * data[5] => cc_pd_suffix
623 * data[6] => cc_pd_birthdate
624 * data[7] => cc_pd_sex SELECT
625 * data[8] => cc_pd_prefix
626 * data[9] => cc_pd_gpg_finger_print
627 * data[10] => cc_pd_notes
628 */
629
630function populatePersonalData (data)
631{
632        for (i in data)
633        {
634                switch(i)
635                {
636                        case 'cc_pd_suffix':
637                        case 'cc_pd_sex':
638                        case 'cc_pd_prefix':
639                                selectOption(i, data[i]);
640                                break;
641
642                        case 'cc_pd_photo':
643                                if (data[i])
644                                {
645                                        Element(i).src =  data[i] + '&'+ Math.random();
646                                }
647                                break;
648
649                        default:
650                                Element(i).value = data[i] == undefined ? '' : data[i];
651                }
652        }
653
654        return;
655}
656
657/********* End Personal Data Functions *********/
658
659
660/********* Addresses Functions *********/
661function resetAddressFields()
662{
663        Element('cc_addr_types').selectedIndex = 0;
664       
665        Element('cc_addr_countries').selectedIndex = 0;
666        Element('cc_addr_countries').disabled = true;
667       
668        Element('cc_addr_states').selectedIndex = 0;
669        Element('cc_addr_states').disabled = true;
670        Element('cc_addr_states_new').disabled = true;
671        Element('cc_addr_states_new').readonly = true;
672        Element('cc_addr_states_new').value = '';
673
674        Element('cc_addr_cities').selectedIndex = 0;
675        Element('cc_addr_cities').disabled = true;
676        Element('cc_addr_cities_new').disabled = true;
677        Element('cc_addr_cities_new').readonly = true;
678        Element('cc_addr_cities_new').value = '';
679
680        Element('cc_addr_id').value = '';
681
682        resetAddrFillingFields();
683}
684
685function resetAddrFillingFields()
686{
687        Element('cc_addr_1').value = '';
688        Element('cc_addr_2').value = '';
689        Element('cc_addr_other').value = '';
690        Element('cc_addr_complement').value = '';
691        Element('cc_addr_postal_code').value = '';
692        Element('cc_addr_po_box').value = '';
693        Element('cc_addr_is_default').checked = false;
694}
695
696function disableAddrFillingFields()
697{
698        Element('cc_addr_1').readonly = true;
699        Element('cc_addr_1').disabled = true;
700        Element('cc_addr_2').readonly = true;
701        Element('cc_addr_2').disabled = true;
702        Element('cc_addr_other').readonly = true;
703        Element('cc_addr_other').disabled = true;
704        Element('cc_addr_complement').readonly = true;
705        Element('cc_addr_complement').disabled = true;
706        Element('cc_addr_postal_code').readonly = true;
707        Element('cc_addr_postal_code').disabled = true;
708        Element('cc_addr_po_box').readonly = true;
709        Element('cc_addr_po_box').disabled = true;
710        Element('cc_addr_is_default').readonly = true;
711        Element('cc_addr_is_default').disabled = true;
712}
713
714function updateAddressFields()
715{
716        var type = Element('cc_addr_types');
717        var oldSelected = type.value;
718       
719        saveAddressFields();
720       
721        if (oldSelected == '_NONE_')
722        {
723                resetAddressFields();
724                return true;
725        }
726       
727        CC_addr_last_selected = type.selectedIndex;
728       
729        Element('cc_addr_countries').disabled = false;
730       
731        var data = CC_contact_full_info['addresses'];
732        var addrIndex  = 'address'+Element('cc_addr_types').value;
733       
734        if (typeof(data) != 'object' || typeof(data[addrIndex]) != 'object')
735        {
736                resetAddressFields();
737                Element('cc_addr_countries').disabled = false;
738                Element('cc_addr_countries').selectedIndex = CC_br_index;
739                type.value = oldSelected;
740                updateAddrStates();
741                return true;
742        }
743       
744        var addrTypeID = Element('cc_addr_types').value;
745       
746        data = CC_contact_full_info['addresses'][addrIndex];
747       
748        Element('cc_addr_id').value                              = data['id_address']           ? data['id_address']                    : '';
749        Element('cc_addr_1').value                              = data['address1']                      ? data['address1']                              : '';
750        Element('cc_addr_2').value                              = data['address2']                      ? data['address2']                              : '';
751        Element('cc_addr_complement').value   = data['complement']              ? data['complement']            : '';
752        Element('cc_addr_other').value                          = data['address_other'] ? data['address_other'] : '';
753        Element('cc_addr_postal_code').value    = data['postal_code']           ? data['postal_code']           : '';
754        Element('cc_addr_po_box').value                 = data['po_box']                                ? data['po_box']                : '';
755        Element('cc_addr_is_default').checked   = data['address_is_default'] == '1' ? true: false;
756
757        Element('cc_addr_countries').value    = data['id_country'];
758        updateAddrStates();
759}
760
761function updateAddrStates()
762{
763        var states = Element('cc_addr_states');
764        if (Element('cc_addr_countries').value == '_NONE_')
765        {
766                states.disabled = true;
767                states.selectedIndex = 0;
768                clearSelectBox(states, 4);
769                updateAddrCities();     
770                return;
771        }
772
773        updateAddrFillingFields();
774        populateStates();
775}
776
777function populateStates()
778{
779        var states = Element('cc_addr_states');
780        var cities = Element('cc_addr_cities');
781        var handler = function (responseText)
782        {
783                var data = unserialize(responseText);
784               
785                clearSelectBox(states, 1);
786                       
787                if (typeof(data) != 'object')
788                {
789                        showMessage(Element('cc_msg_err_contacting_server').value);
790       
791                        return;
792                }
793
794                if (data['status'] == 'empty')
795                {
796                        states.disabled = true;
797                        cities.disabled = true;                 
798                        states.selectedIndex = 0;
799                        cities.selectedIndex = 0;
800                        return;
801                }
802                else if (data['status'] != 'ok')
803                {
804                        showMessage(data['msg']);
805                        states.disabled = true;
806                        states.selectedIndex = 0;
807                        updateAddrCities();
808                        return;
809                }
810                states.disabled = false;
811                var i = 1;
812                for (var j in data['data'])
813                {
814                        states.options[i] = new Option(data['data'][j], j);
815                        i++;
816                }
817
818                states.disabled = false;
819                states.selectedIndex = 0;
820
821                data = CC_contact_full_info['addresses'];
822                var addrIndex = 'address'+Element('cc_addr_types').value;
823                if (data && data[addrIndex])
824                {
825                        states.value = data[addrIndex]['id_state'];
826                        if (states.value == '_NEW_')
827                        {
828                                if (CC_contact_full_info['addresses']['new_states'][addrIndex])
829                                {
830                                        Element('cc_addr_states_new').value = CC_contact_full_info['addresses']['new_states'][addrIndex];
831                                }
832                                updateAddrNewStateOnMouseOut();
833                        }
834                        updateAddrCities();
835                }
836        };
837       
838        Connector.newRequest('populateStates', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_states&country='+Element('cc_addr_countries').value, 'GET', handler);
839}
840
841function updateAddrCities()
842{
843        var states = Element('cc_addr_states');
844        var cities = Element('cc_addr_cities');
845        var newState = Element('cc_addr_states_new');
846        var requestStr;
847
848        switch (states.value)
849        {
850                case '_NONE_':
851                        newState.readonly = true;
852                        newState.disabled = true;
853                        newState.value = '';
854
855                        cities.disabled = true;
856                        cities.selectedIndex = 0;
857                        updateAddrFillingFields();
858                        return;
859
860                case '_NEW_':
861
862                        newState.readonly = false;
863                        newState.disabled = false;
864                        updateAddrNewStateOnMouseOut();
865                       
866                        cities.disabled = false;
867                        clearSelectBox(cities, 3);
868                        cities.selectedIndex = 1;
869                        updateAddrFillingFields();
870                        return;
871
872                case '_SEP_': return;
873
874                case '_NOSTATE_':
875                        clearSelectBox(cities, 3);
876                       
877                        cities.disabled = false;
878                        cities.selectedIndex = 0;
879                       
880                        requestStr = 'country='+Element('cc_addr_countries').value;
881                        break;
882               
883                default:
884                        requestStr = 'country='+Element('cc_addr_countries').value+'&state='+states.value;
885        }
886
887        newState.readonly = true;
888        newState.disabled = true;
889        newState.value = '';
890
891        populateCities(requestStr);
892}
893
894function populateCities(requestStr)
895{
896        var cities = Element('cc_addr_cities');
897       
898        var handler = function (responseText)
899        {
900                var data = unserialize(responseText);
901               
902                clearSelectBox(cities, 1);
903               
904                if (typeof(data) != 'object')
905                {
906                        showMessage(Element('cc_msg_err_contacting_server').value);
907                       
908                        return;
909                }
910
911                if (data['status'] == 'empty')
912                {
913                        cities.disabled = true;
914                        cities.selectedIndex = 0;
915                        return;
916                }
917                else if (data['status'] != 'ok')
918                {
919                        showMessage(data['msg']);
920                        cities.disabled = true;
921                        cities.selectedIndex = 0;
922                        updateAddrFillingFields();
923                        return;
924                }
925                cities.disabled = false;
926                var i = 1;
927                for (var j in data['data'])
928                {
929                        cities.options[i] = new Option(data['data'][j], j);
930                        i++;
931                }
932
933                cities.disabled = false;
934                cities.selectedIndex = 0;
935
936                data = CC_contact_full_info['addresses'];
937                var addrIndex = 'address'+Element('cc_addr_types').value;
938                if (data && data[addrIndex])
939                {
940                        cities.value = data[addrIndex]['id_city'];
941
942                        if (cities.value == '_NEW_')
943                        {
944                                if (CC_contact_full_info['addresses']['new_cities'][addrIndex])
945                                {
946                                        Element('cc_addr_cities_new').value = CC_contact_full_info['addresses']['new_cities'][addrIndex];
947                                }
948                                updateAddrNewCityOnMouseOut();
949                        }
950                }
951        };
952       
953        Connector.newRequest('populateCities', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_cities&'+requestStr, 'GET', handler);
954}
955
956function updateAddrNewStateOnMouseOver ()
957{
958        if (Element('cc_addr_states_new').value == Element('cc_msg_type_state').value && Element('cc_addr_states').selectedIndex == 1)
959        {
960                Element('cc_addr_states_new').value = '';
961        }
962}
963
964function updateAddrNewStateOnMouseOut ()
965{
966        if (Element('cc_addr_states_new').value.length == 0 && Element('cc_addr_states').selectedIndex == 1)
967        {
968                Element('cc_addr_states_new').value = Element('cc_msg_type_state').value;
969        }
970}
971
972function updateAddrFillingFields()
973{
974        var countries = Element('cc_addr_countries');
975        var cities = Element('cc_addr_cities');
976        var newCity = Element('cc_addr_cities_new');
977
978        if (countries.value == '_NONE_')
979        {
980                newCity.readonly = true;
981                newCity.disabled = true;
982                newCity.value = '';
983                disableAddrFillingFields();
984                return;
985        }
986       
987        Element('cc_addr_1').readonly = false;
988        Element('cc_addr_1').disabled = false;
989
990        Element('cc_addr_2').readonly = false;
991        Element('cc_addr_2').disabled = false;
992
993        Element('cc_addr_other').readonly = false;
994        Element('cc_addr_other').disabled = false;
995
996        Element('cc_addr_complement').readonly = false;
997        Element('cc_addr_complement').disabled = false;
998
999        Element('cc_addr_postal_code').readonly = false;
1000        Element('cc_addr_postal_code').disabled = false;
1001
1002        Element('cc_addr_po_box').readonly = false;
1003        Element('cc_addr_po_box').disabled = false;
1004
1005        Element('cc_addr_is_default').readonly = false;
1006        Element('cc_addr_is_default').disabled = false;
1007
1008        switch (cities.value)
1009        {
1010                case '_NONE_':
1011                        newCity.readonly = true;
1012                        newCity.disabled = true;
1013                        newCity.value = '';
1014
1015                        //resetAddrFillingFields();
1016                       
1017                        return;
1018
1019                case '_NEW_':
1020
1021                        newCity.readonly = false;
1022                        newCity.disabled = false;
1023                        updateAddrNewCityOnMouseOut();
1024                       
1025                        break;
1026
1027                case '_SEP_': return;
1028
1029                default:
1030                        newCity.readonly = true;
1031                        newCity.disabled = true;
1032                        newCity.value = '';
1033        }
1034}
1035
1036function updateAddrNewCityOnMouseOver ()
1037{
1038        if (Element('cc_addr_cities_new').value == Element('cc_msg_type_city').value && Element('cc_addr_cities').selectedIndex == 1)
1039        {
1040                Element('cc_addr_cities_new').value = '';
1041        }
1042}
1043
1044function updateAddrNewCityOnMouseOut ()
1045{
1046        if (Element('cc_addr_cities_new').value.length == 0 && Element('cc_addr_cities').selectedIndex == 1)
1047        {
1048                Element('cc_addr_cities_new').value = Element('cc_msg_type_city').value;
1049        }
1050}
1051
1052function saveAddressFields ()
1053{
1054        var lastIndex = CC_addr_last_selected;
1055
1056        if (lastIndex == 0)
1057        {
1058                return true;
1059        }
1060       
1061        var addrFields = new Array('cc_addr_1',
1062                                   'cc_addr_2',
1063                                                           'cc_addr_complement',
1064                                                           'cc_addr_other',
1065                                                           'cc_addr_postal_code',
1066                                                           'cc_addr_po_box',
1067                                                           'cc_addr_countries',
1068                                                           'cc_addr_states',
1069                                                           'cc_addr_cities');
1070
1071        var empty = true;
1072       
1073        for (var i = 0; i < 8; i++)
1074        {
1075                var field = Element(addrFields[i]);
1076                if (field.value && field.value != '_NONE_' && field.value != '_SEP_')
1077                {
1078                        empty = false;
1079                }
1080        }
1081                               
1082        if (empty)
1083        {
1084                return true;
1085        }
1086
1087        if (!CC_contact_full_info['addresses'])
1088        {
1089                CC_contact_full_info['addresses'] = new Array();
1090        }
1091
1092        var addrInfo = CC_contact_full_info['addresses']['address'+Element('cc_addr_types').options[lastIndex].value];
1093
1094        if (!addrInfo)
1095        {
1096                addrInfo = new Array();
1097        }
1098
1099        addrInfo['id_address'] = Element('cc_addr_id').value;
1100
1101        switch(Element('cc_addr_countries').value)
1102        {
1103                case '_SEP_':
1104                case '_NONE_':
1105                        addrInfo['id_country'] = false;
1106                        break;
1107
1108                default:
1109                        addrInfo['id_country'] = Element('cc_addr_countries').value;
1110               
1111        }
1112
1113        switch(Element('cc_addr_states').value)
1114        {
1115                case '_SEP_':
1116                case '_NONE_':
1117                case '_NEW_':
1118                case '_NOSTATE_':
1119                        addrInfo['id_state'] = false;
1120                        break;
1121
1122                default:
1123                        addrInfo['id_state'] = Element('cc_addr_states').value;
1124               
1125        }
1126
1127        switch(Element('cc_addr_cities').value)
1128        {
1129                case '_SEP_':
1130                case '_NONE_':
1131                case '_NEW_':
1132                        addrInfo['id_city'] = false;
1133                        break;
1134
1135                default:
1136                        addrInfo['id_city'] = Element('cc_addr_cities').value;
1137               
1138        }       
1139
1140        addrInfo['id_typeof_address']  = Element('cc_addr_types').options[lastIndex].value;
1141        addrInfo['address1']           = Element('cc_addr_1').value ? Element('cc_addr_1').value : false;
1142        addrInfo['address2']           = Element('cc_addr_2').value ? Element('cc_addr_2').value : false;
1143        addrInfo['complement']         = Element('cc_addr_complement').value ? Element('cc_addr_complement').value : false;
1144        addrInfo['address_other']      = Element('cc_addr_other').value ? Element('cc_addr_other').value : false;
1145        addrInfo['postal_code']        = Element('cc_addr_postal_code').value ? Element('cc_addr_postal_code').value : false;
1146        addrInfo['po_box']             = Element('cc_addr_po_box').value ? Element('cc_addr_po_box').value : false;
1147        addrInfo['address_is_default'] = Element('cc_addr_is_default').checked ? '1' : '0';
1148
1149        CC_contact_full_info['addresses']['address'+Element('cc_addr_types').options[lastIndex].value] = addrInfo;
1150
1151        if (Element('cc_addr_cities').value == '_NEW_' &&
1152            Element('cc_msg_type_city').value !=  Element('cc_addr_cities_new').value &&
1153                Element('cc_addr_cities_new').value != '')
1154        {
1155                var addrRootInfo = CC_contact_full_info['addresses']['new_cities'];
1156               
1157                if (!addrRootInfo)
1158                {
1159                        addrRootInfo = new Array();
1160                }
1161               
1162                var i = addrRootInfo.length;
1163                addrRootInfo[addrInfo['id_typeof_address']] = new Array();
1164                addrRootInfo[addrInfo['id_typeof_address']]['id_country'] = Element('cc_addr_countries').value;
1165                addrRootInfo[addrInfo['id_typeof_address']]['id_state']   = Element('cc_addr_states').value.charAt(0) != '_' ? Element('cc_addr_states').value : null;
1166                addrRootInfo[addrInfo['id_typeof_address']]['city_name']  = Element('cc_addr_cities_new').value;
1167                CC_contact_full_info['addresses']['new_cities'] = addrRootInfo;
1168        }
1169
1170        if (Element('cc_addr_states').value == '_NEW_' &&
1171            Element('cc_msg_type_state').value !=  Element('cc_addr_states_new').value &&
1172                Element('cc_addr_states_new').value != '')
1173        {
1174                var addrRootInfo = CC_contact_full_info['addresses']['new_states'];
1175               
1176                if (!addrRootInfo)
1177                {
1178                        addrRootInfo = new Array();
1179                }
1180               
1181                var i = addrRootInfo.length;
1182                addrRootInfo[addrInfo['id_typeof_address']] = new Array();
1183                addrRootInfo[addrInfo['id_typeof_address']]['id_country'] = Element('cc_addr_countries').value;
1184                addrRootInfo[addrInfo['id_typeof_address']]['state_name'] = Element('cc_addr_states_new').value;
1185                CC_contact_full_info['addresses']['new_states'] = addrRootInfo;
1186        }
1187
1188        return true;
1189}
1190
1191
1192/********* End Addresses Functions *********/
1193
1194
1195
1196/********* Begin Connections Functions ************/
1197function connGetHTMLLine ()
1198{
1199        var _label = (CC_contact_full_info['connections']
1200                && typeof(CC_contact_full_info['connections'][CC_conn_last_selected])!= 'undefined'
1201                && typeof(CC_contact_full_info['connections'][CC_conn_last_selected][CC_conn_count]) != 'undefined'
1202                ? CC_contact_full_info['connections'][CC_conn_last_selected][CC_conn_count]['name']
1203                : Element("cc_conn_type_sel").value);
1204
1205        var cc_conn_default = Element("cc_phone_default").style.display == '' ? Element("cc_phone_default") : Element("cc_email_default");
1206        cc_conn_default.disabled = false;
1207        var idx_conn = 0;
1208        for(idx_conn; idx_conn < cc_conn_default.options.length; idx_conn++)
1209                if(cc_conn_default.options[idx_conn].value == _label)
1210                        break; 
1211       
1212        if(idx_conn == cc_conn_default.options.length)
1213                cc_conn_default.options[idx_conn] = new Option (_label,_label, false,false);
1214                       
1215        if (!document.all)
1216        {
1217                return '<td style="position: absolute; left: 0; top: 0; z-index: -1; visibility: hidden"><input id="cc_conn_id_' + CC_conn_count + '" type="hidden" value="_NEW_"><input id="cc_conn_is_default_' + CC_conn_count + '" type="hidden" value="false"></td>'+
1218                //'<td style="width: 30px;" align="right"><input name="cc_conn_is_default" id="cc_conn_is_default_'+ CC_conn_count +'" type="radio"></td>'+
1219                '<td style="width: 10px;" align="right"><input id="cc_conn_name_'+CC_conn_count+'" type="hidden"><td style="width: 100px;" align="right"><span style="width: 150px;" id="cc_conn_label_'+CC_conn_count+'">'+_label+'</span></td>' +
1220                '<td align="left"><input id="cc_conn_value_'+ CC_conn_count +'" style="width: 150px;" type="text">&nbsp;' +
1221                '<img align="top" alt="X" src="templates/default/images/x.png" style="width:18px; height:18px" onclick="javascript:removeConnField(\'cc_conn_tr_' + CC_conn_count + '\')"></td>';
1222        }
1223        else
1224        {
1225                var tds = new Array();
1226                var inputs = new Array();
1227                var img = document.createElement('img');
1228
1229                for (var i = 0; i < 4; i++)
1230                {
1231                        tds[i] = document.createElement('td');
1232                }
1233
1234                tds[0].style.position = 'absolute';
1235                tds[0].style.visibility = 'hidden';
1236                tds[0].style.zIndex = '-1';
1237
1238                var remove_id = 'cc_conn_tr_'+CC_conn_count;
1239                img.alt = 'X';
1240                img.src = 'templates/default/images/x.png';
1241                img.style.width = '18px';
1242                img.style.height = '18px';
1243                img.align = 'top';
1244                img.onclick = function(e){ removeConnField(remove_id);};
1245               
1246                for (var i = 0; i < 3; i++)
1247                {
1248                        inputs[i] = document.createElement('input');
1249                }
1250
1251                inputs[0].id = 'cc_conn_id_'+CC_conn_count;
1252                inputs[0].type = 'hidden';
1253                inputs[0].value = '_NEW_';
1254
1255                inputs[1].id = 'cc_conn_name_'+CC_conn_count;
1256                inputs[1].type = 'hidden';             
1257
1258                inputs[2].id = 'cc_conn_value_'+CC_conn_count;
1259                inputs[2].type = 'text';
1260                inputs[2].style.width = '150px';
1261
1262                var _span = document.createElement("SPAN");
1263                _span.style.width = "150px";
1264                _span.id = "cc_conn_label_"+CC_conn_count;
1265                _span.innerHTML = _label;
1266                tds[0].appendChild(inputs[0]);
1267                tds[1].appendChild(inputs[1]);
1268                tds[1].align = 'right';
1269                tds[1].appendChild(_span);
1270                tds[2].appendChild(inputs[2]);
1271                tds[2].align = 'left';
1272                tds[2].innerHTML +="&nbsp;";
1273                tds[2].appendChild(img);
1274
1275                return tds;
1276        }
1277}
1278
1279function connAddNewLine ()
1280{
1281
1282        var _emptyLine = (!CC_contact_full_info['connections']
1283                || typeof(CC_contact_full_info['connections'][CC_conn_last_selected]) == 'undefined'
1284                || typeof(CC_contact_full_info['connections'][CC_conn_last_selected][CC_conn_count]) == 'undefined');
1285       
1286        if(_emptyLine) {       
1287               
1288                if(Element("cc_conn_type_sel").value == '-1'){
1289                                return false;   
1290                }
1291               
1292                for(k = 0; k < CC_conn_count; k++) {
1293                        if(Element("cc_conn_name_"+k) && Element("cc_conn_name_"+k).value != "" && Element("cc_conn_name_"+k).value == Element("cc_conn_type_sel").value) {
1294                                alert('Você já possui uma entrada para o tipo "'+Element("cc_conn_type_sel").value+'"!');
1295                                Element("cc_conn_type_sel").options.selectedIndex = 0;                         
1296                                return false;
1297                        }
1298                }
1299        }       
1300        if (!document.all)
1301        {
1302                var obj = addHTMLCode('cc_conn', 'cc_conn_tr_'+CC_conn_count, connGetHTMLLine(),'tr');
1303        }
1304        else
1305        {
1306                var tds = connGetHTMLLine();
1307                var tr = document.createElement('tr');
1308                var tbody = Element('cc_conn');
1309
1310                tr.id = 'cc_conn_tr_'+CC_conn_count;
1311                tbody.appendChild(tr);
1312
1313                for (var i = 0; i < 4; i++)
1314                {
1315                        tr.appendChild(tds[i]);
1316                }
1317        }
1318        Element("cc_conn_name_"+CC_conn_count).value = Element("cc_conn_type_sel").value;
1319        Element("cc_conn_type_sel").options.selectedIndex = 0;
1320        CC_conn_count++;
1321
1322        return CC_conn_count;
1323}
1324
1325function connRemoveLine(id)
1326{       
1327        var p = Element(id).parentNode;
1328        var cc_conn_default = Element("cc_phone_default").style.display == '' ? Element("cc_phone_default") : Element("cc_email_default");
1329        var _label = Element("cc_conn_label_"+(id.substring(11,13))).innerHTML;
1330        for(var i = 0;i < cc_conn_default.options.length; i++) {
1331                if(cc_conn_default.options[i].value == _label) {
1332                        cc_conn_default.options[i] = null;
1333                        break;
1334                }
1335        }
1336        if(cc_conn_default.options.length == 1)
1337                cc_conn_default.disabled = true;
1338
1339        removeHTMLCode(id);
1340
1341        return;
1342        connRefreshClass(p.childNodes);
1343}
1344
1345function connRefreshClass(Nodes)
1346{
1347        for (var i = 2; i < Nodes.length; i++)
1348        {
1349                Nodes.item(i).className = i % 2 ? 'row_on' : 'row_off';
1350        }
1351}
1352
1353function clearConn()
1354{
1355        var connParent = Element('cc_conn').childNodes;
1356        var i;
1357
1358        for (i = connParent.length - 1; i >= 0; i--)
1359        {
1360                if (connParent[i].id)
1361                {
1362                        connRemoveLine(connParent[i].id);
1363                }
1364        }
1365       
1366        CC_conn_count = 0;
1367}
1368
1369function removeConnField(id)
1370{
1371        var count = id.substring(id.lastIndexOf('_')+1);
1372        if (Element('cc_conn_id_'+count).value != '_NEW_')
1373        {
1374                if (typeof(CC_contact_full_info['removed_conns']) != 'object')
1375                {
1376                        CC_contact_full_info['removed_conns'] = new Array();
1377                }
1378
1379                CC_contact_full_info['removed_conns'][CC_contact_full_info['removed_conns'].length] = Element('cc_conn_id_'+count).value;
1380        }
1381
1382        connRemoveLine(id);
1383}
1384
1385function updateConnFields()
1386{
1387       
1388        var connID;
1389        var i;
1390        var cc_conn_type_sel = Element("cc_conn_type_sel");
1391        var cc_phone_default = Element("cc_phone_default");
1392        var cc_email_default = Element("cc_email_default");
1393        var div_cc_conn_is_default = Element("div_cc_conn_is_default");
1394        var cc_conn_is_default = '';
1395        var selected_index = '';
1396       
1397        cc_conn_type_sel.disabled = false;
1398        div_cc_conn_is_default.style.display = "";
1399       
1400        for(var i = 0;i < cc_conn_type_sel.options.length; i++)
1401                cc_conn_type_sel.options[i--] = null;   
1402               
1403        if(Element('cc_conn_type_1').checked) {
1404            var lang_new_email = Element('cc_msg_new_email').value;
1405            var lang_main = Element('cc_msg_main').value;
1406            var lang_alternative = Element('cc_msg_alternative').value;
1407            cc_conn_type_sel[0] = new Option(lang_new_email,'-1');     
1408            cc_conn_type_sel[1] = new Option(lang_main,lang_main);
1409            cc_conn_type_sel[2] = new Option(lang_alternative,lang_alternative);       
1410                connID = 1;
1411                selected_index = cc_email_default.options.selectedIndex;
1412                for(var i = 0;i < cc_email_default.options.length; i++) {
1413                        cc_email_default.options[i--] = null;
1414                }
1415
1416                var lang_select_email = Element('cc_msg_select_email').value;
1417                cc_email_default.options[0] = new Option(lang_select_email,'-1');               
1418                cc_phone_default.style.display = 'none';
1419                cc_email_default.style.display = '';                   
1420                cc_conn_is_default = cc_email_default;
1421        }
1422        else if(Element('cc_conn_type_2').checked) {
1423            var lang_new_telephone = Element('cc_msg_new_phone').value;
1424            var lang_home = Element('cc_msg_home').value;
1425            var lang_cellphone = Element('cc_msg_cellphone').value;
1426            var lang_work = Element('cc_msg_work').value;
1427            var lang_fax = Element('cc_msg_fax').value;
1428            var lang_pager = Element('cc_msg_pager').value;
1429            cc_conn_type_sel[0] = new Option(lang_new_telephone,'-1'); 
1430            cc_conn_type_sel[1] = new Option(lang_home,lang_home);
1431            cc_conn_type_sel[2] = new Option(lang_cellphone,lang_cellphone);   
1432            cc_conn_type_sel[3] = new Option(lang_work,lang_work);
1433            cc_conn_type_sel[4] = new Option(lang_fax,lang_fax);
1434            cc_conn_type_sel[5] = new Option(lang_pager,lang_pager);
1435
1436                connID = 2;
1437                selected_index = cc_phone_default.options.selectedIndex;
1438                for(var i = 0;i < cc_phone_default.options.length; i++) {
1439                        cc_phone_default.options[i--] = null;
1440                }
1441
1442                var lang_choose_phone = Element('cc_msg_choose_phone').value;           
1443                cc_phone_default.options[0] = new Option(lang_choose_phone,'-1');
1444                cc_email_default.style.display = 'none';
1445                cc_phone_default.style.display = '';
1446                cc_conn_is_default = cc_phone_default;
1447        }
1448                       
1449        Element("cc_conn_type_sel").options.selectedIndex = 0;
1450        /* First save the data */
1451        saveConnFields();
1452
1453        CC_conn_last_selected = connID;
1454       
1455        clearConn();
1456       
1457        if (connID == '_NONE_')
1458        {       cc_conn_is_default.disabled = true;
1459                return;
1460        }
1461       
1462        /* If no data already available, return */
1463        if (!CC_contact_full_info['connections'])
1464        {
1465                cc_conn_is_default.disabled = true;
1466                return;
1467        }
1468        cc_conn_is_default.disabled = (!CC_contact_full_info['connections'][connID] || CC_contact_full_info['connections'][connID].length == 0);
1469        /* Put the information that's already available */
1470        for (i in CC_contact_full_info['connections'][connID])
1471        {
1472                var num = connAddNewLine();
1473                Element('cc_conn_id_'+i).value = CC_contact_full_info['connections'][connID][i]['id'];
1474                Element('cc_conn_name_'+i).value = CC_contact_full_info['connections'][connID][i]['name'];
1475                Element('cc_conn_value_'+i).value = CC_contact_full_info['connections'][connID][i]['value'];
1476
1477                if(!selected_index || (selected_index == '-1' && CC_contact_full_info['connections'][connID][i]['is_default'])){
1478                        for(var j = 0;j < cc_conn_is_default.options.length; j++){
1479                                if(cc_conn_is_default.options[j].value == CC_contact_full_info['connections'][connID][i]['name']) {
1480                                        selected_index = j;
1481                                        break;
1482                                }
1483                        }
1484                }
1485        }       
1486        if(cc_conn_is_default.options.length > selected_index)
1487                cc_conn_is_default.options.selectedIndex = (selected_index == "-1" ? 0 : selected_index);
1488}
1489
1490function saveConnFields()
1491{
1492        if (CC_conn_last_selected != 0 && CC_conn_last_selected != '_NONE_')
1493        {
1494                var nodes = Element('cc_conn').childNodes;
1495                var k = 0;
1496
1497                if (typeof(CC_contact_full_info['connections']) != 'object' || CC_contact_full_info['connections'] == null)
1498                {
1499                        CC_contact_full_info['connections'] = new Array();
1500                        CC_contact_full_info['connections'][CC_conn_last_selected] = new Array();
1501                }
1502                else if (typeof(CC_contact_full_info['connections'][CC_conn_last_selected]) != 'object')
1503                {
1504                        CC_contact_full_info['connections'][CC_conn_last_selected] = new Array();
1505                }
1506                else
1507                {
1508                        delete CC_contact_full_info['connections'][CC_conn_last_selected];
1509                        CC_contact_full_info['connections'][CC_conn_last_selected] = new Array();
1510                }
1511
1512                for (var i = 0; i < nodes.length; i++)
1513                {
1514                        if (nodes[i].id)
1515                        {
1516                                var subNodes = nodes[i].childNodes;
1517                                var found = false;
1518                               
1519                                for (var j = 0; j < subNodes.length; j++)
1520                                {
1521                                        if (subNodes[j].childNodes.length > 0 &&
1522                                            subNodes[j].childNodes[0].id)
1523                                        {
1524                                                /* Check for the Connection Info array */
1525                                                if (typeof(CC_contact_full_info['connections'][CC_conn_last_selected][k]) != 'object')
1526                                                {
1527                                                        CC_contact_full_info['connections'][CC_conn_last_selected][k] = new Array();
1528                                                }
1529                                               
1530                                            if (subNodes[j].childNodes[0].id.indexOf('cc_conn_name') != -1)
1531                                                {
1532                                                        if (subNodes[j].childNodes[0].value)
1533                                                        {
1534                                                                CC_contact_full_info['connections'][CC_conn_last_selected][k]['name'] = subNodes[j].childNodes[0].value;
1535                                                        }
1536                                                        else
1537                                                        {
1538                                                                CC_contact_full_info['connections'][CC_conn_last_selected][k]['name'] = '';
1539                                                        }
1540                                                }
1541                                                else if (subNodes[j].childNodes[0].id.indexOf('cc_conn_value') != -1)
1542                                                {
1543                                                        if (subNodes[j].childNodes[0].value)
1544                                                        {
1545                                                                CC_contact_full_info['connections'][CC_conn_last_selected][k]['value'] = subNodes[j].childNodes[0].value;
1546                                                        }
1547                                                        else
1548                                                        {
1549                                                                CC_contact_full_info['connections'][CC_conn_last_selected][k]['value'] = '';
1550                                                        }
1551                                                }
1552                                                else if (subNodes[j].childNodes[0].id.indexOf('cc_conn_id') != -1)
1553                                                {
1554                                                        CC_contact_full_info['connections'][CC_conn_last_selected][k]['id'] = subNodes[j].childNodes[0].value;
1555                                                }
1556
1557                                                found = true;
1558                                        }
1559                                }
1560                               
1561                                if (found)
1562                                {
1563                                        k++;
1564                                }
1565                        }
1566                }
1567
1568                if (CC_contact_full_info['connections'].length == 0)
1569                {
1570                        delete CC_contact_full_info['connections'];
1571                }
1572
1573                if (CC_contact_full_info['connections'][CC_conn_last_selected].length == 0)
1574                {
1575                        delete CC_contact_full_info['connections'][CC_conn_last_selected];
1576                }
1577               
1578        }
1579
1580        return;
1581}
1582
1583/***********************************************\
1584*               VIEW CARDS FUNCTIONS            *
1585\***********************************************/
1586function removeAllEntries()
1587{
1588        var handler = function (responseText)
1589        {
1590                var data = unserialize(responseText);
1591                if (typeof(data) != 'object') {
1592                        showMessage(Element('cc_msg_err_contacting_server').value);
1593                        return;
1594                }
1595                if (data['status'] != 'ok')     {
1596                        showMessage(data['msg']);
1597                        return;
1598                }
1599                setTimeout('updateCards()',80);
1600        };
1601        var number = randomString().toLowerCase();
1602        var result = '';
1603       
1604        if(!is_ie)
1605                result = prompt("Essa operação removerá TODOS os seus \ncontatos pessoais,  e  NÃO  PODERÁ  ser \ndesfeita. Digite o código abaixo:\n\tCódigo de confirmação: "+number);
1606        else
1607                result = prompt("Essa operação removerá TODOS os seus contatos pessoais,  e  NÃO  PODERÁ  ser desfeita. Digite o seguinte código de confirmação: "+number,"");
1608
1609        if(result) {
1610                if(result.toLowerCase() == number)
1611                        Connector.newRequest('removeAllEntries', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=remove_all_entries', 'GET', handler);
1612                else
1613                        alert('Código Incorreto');
1614        }       
1615}
1616
1617function removeEntry(id, type)
1618{
1619        var question = showMessage(type == 'groups' ? Element('cc_msg_group_remove_confirm').value: Element('cc_msg_card_remove_confirm').value, 'confirm');
1620
1621        if (!question)
1622        {
1623                return;
1624        }
1625       
1626        var handler = function (responseText)
1627        {
1628                var data = unserialize(responseText);
1629
1630                if (typeof(data) != 'object')
1631                {
1632                        showMessage(Element('cc_msg_err_contacting_server').value);
1633                        return;
1634                }
1635               
1636                if (data['status'] != 'ok')
1637                {
1638                        showMessage(data['msg']);
1639                        return;
1640                }
1641               
1642                setTimeout('updateCards()',80);;
1643        };
1644               
1645        typeArg = (type == 'groups' ? 'group' : 'entry');
1646               
1647        Connector.newRequest('removeEntry', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=remove_'+typeArg+'&remove=' + id, 'GET', handler);
1648}
1649
1650function updateCards()
1651{
1652        setHeightSpace();
1653        setMaxCards(getMaxCards());
1654        showCards(getActualLetter(), getActualPage());
1655}
1656
1657
1658window.onresize = function ()
1659{
1660        updateCards();
1661}
1662
1663
1664function setHeightSpace ()
1665{
1666        /*
1667        var w_height = 0;
1668        var w_extra = 200;
1669       
1670        if (document.body.clientHeight)
1671        {
1672                w_height = parseInt(document.body.clientHeight);
1673        }
1674        else
1675        {
1676                w_height = 500;
1677        }
1678        if (w_height < 500)
1679        {
1680                w_height = 500;
1681        }
1682        Element('cc_card_space').style.height = (w_height - w_extra) + 'px';
1683        */
1684}
1685
1686function selectLetter (letter_id)
1687{
1688        for (var i = 0; i < 28; i++)
1689        {
1690                if ( i == letter_id )
1691                {
1692                        Element('cc_letter_' + i).className = 'letter_box_active';
1693                }
1694                else
1695                {
1696                        Element('cc_letter_' + i).className = 'letter_box';
1697                }
1698        }
1699}
1700
1701function clearLetterSelection()
1702{
1703        for (var i = 0; i < 28; i++)
1704        {
1705                Element('cc_letter_' + i).className = 'letter_box';
1706        }
1707}
1708function getActualPage ()
1709{
1710        return CC_actual_page;
1711}
1712
1713function getActualLetter ()
1714{
1715        return CC_actual_letter;
1716}
1717
1718function getFirstPage ()
1719{
1720        return 1;
1721}
1722
1723function getPreviousPage ()
1724{
1725        if ( CC_actual_page > 1 )
1726        {
1727                return CC_actual_page - 1;
1728        }
1729        else
1730        {
1731                return 1;
1732        }
1733}
1734
1735function getNextPage ()
1736{
1737        if ( CC_actual_page < CC_npages )
1738        {
1739                return CC_actual_page + 1;
1740        }
1741        else
1742        {
1743                return CC_npages;
1744        }
1745}
1746
1747function getLastPage ()
1748{
1749        return CC_npages;
1750}
1751
1752function setPages (npages, actual_page, showing_page)
1753{
1754        var html_pages = '';
1755        var n_lines = 0;
1756        var page_count = 0;
1757
1758        if (CC_npages == 0)
1759        {
1760                html_pages = '';
1761        }
1762        else
1763        {
1764                var page = 1;
1765                if (showing_page > 10 || (!showing_page && actual_page > 10))
1766                {
1767                        var final_page = showing_page? showing_page-11 : actual_page-11;
1768                        if (final_page < 1)
1769                        {
1770                                final_page = 1;
1771                        }
1772                       
1773                        html_pages += '<a href="javascript:setPages('+npages+', '+ actual_page +', '+ final_page +')">...</a> ';
1774
1775                        page = showing_page ? showing_page : actual_page;
1776                }
1777               
1778                for (; page <= npages; page++)
1779                {
1780                        if (page_count > 10)
1781                        {
1782                                html_pages += '<a href="javascript:setPages('+npages+', '+ actual_page +', '+ page +');">...</a>';
1783                                break;
1784                        }
1785                        if ( page == actual_page )
1786                        {
1787                                html_pages += '<b>'+page+'</b>';
1788                        }
1789                        else
1790                        {
1791                                html_pages += '<a href="javascript:showCards(\'' + CC_actual_letter + '\',' + page + ')">' + page + '</a>';
1792                        }
1793                        html_pages += '&nbsp;';
1794                        page_count++;
1795                }
1796        }
1797
1798        if (actual_page <= 1)
1799        {
1800                Element('cc_panel_arrow_first').onclick = '';
1801                Element('cc_panel_arrow_previous').onclick = '';
1802                Element('cc_panel_arrow_first').style.cursor = 'auto';
1803                Element('cc_panel_arrow_previous').style.cursor = 'auto';
1804        }
1805        else
1806        {
1807                Element('cc_panel_arrow_first').onclick = function (event) { showCards(getActualLetter(), getFirstPage()); };
1808                Element('cc_panel_arrow_previous').onclick = function (event) { showCards(getActualLetter(), getPreviousPage()); };
1809                if (is_mozilla)
1810                {
1811                        Element('cc_panel_arrow_first').style.cursor = 'pointer';
1812                        Element('cc_panel_arrow_previous').style.cursor = 'pointer';
1813                }
1814                Element('cc_panel_arrow_first').style.cursor = 'hand';
1815                Element('cc_panel_arrow_previous').style.cursor = 'hand';
1816        }
1817
1818        if (actual_page == CC_npages)
1819        {
1820                Element('cc_panel_arrow_next').onclick = '';
1821                Element('cc_panel_arrow_last').onclick = '';
1822                Element('cc_panel_arrow_next').style.cursor = 'auto';
1823                Element('cc_panel_arrow_last').style.cursor = 'auto';
1824        }
1825        else
1826        {
1827                Element('cc_panel_arrow_next').onclick = function (event) { showCards(getActualLetter(), getNextPage()); };
1828                Element('cc_panel_arrow_last').onclick = function (event) { showCards(getActualLetter(), getLastPage()); };
1829                if (is_mozilla)
1830                {
1831                        Element('cc_panel_arrow_next').style.cursor = 'pointer';
1832                        Element('cc_panel_arrow_last').style.cursor = 'pointer';
1833                }
1834                Element('cc_panel_arrow_next').style.cursor = 'hand';
1835                Element('cc_panel_arrow_last').style.cursor = 'hand';
1836        }
1837       
1838        Element('cc_panel_pages').innerHTML = html_pages;
1839}
1840
1841function populateCards(data, type)
1842{
1843        if (data[3].length >= 100 )
1844        {
1845                alert("Critério de pesquisa muito abrangente, achados " + data[3].length + " resultados");
1846                for (i = 0; i < (Math.sqrt(data[3].length)-1); i++)
1847                        for (j = 0; j < 3; j++)
1848                                document.getElementById("cc_card:"+j+":"+i).innerHTML = '';
1849                return false;
1850        }
1851       
1852        if(type == 'groups')
1853                return populateGroupsInCards(data);
1854       
1855        var pos = 0;
1856        var ncards = data[3].length;
1857       
1858        if (typeof(data[3]) == 'object' && ncards > 0)
1859        {
1860                for (var i = 0; i < CC_max_cards[1]; i++)
1861                {
1862                        for (var j = 0; j < CC_max_cards[0]; j++)
1863                        {
1864                                id = 'cc_card:'+j+':'+i;
1865                       
1866                                for (var k = 0; k < data[2].length; k++)
1867                                {
1868                                        if(!(ccTree.catalog_perms & 2))
1869                                        {
1870                                                switch(data[2][k])
1871                                                {
1872                                                        case 'cc_mail' :
1873
1874                                                                if(data[3][pos][k] === 'none')
1875                                                                        data[3][pos][k] = '&nbsp';
1876                                                                break;
1877                                                        case 'cc_phone' :
1878
1879                                                                if(data[3][pos][k] === 'none')
1880                                                                        data[3][pos][k] = '&nbsp';
1881                                                                break;
1882                                                }
1883                                               
1884                                        }
1885                                       
1886                                       
1887                                        /*if(data[2][k] ==  'cc_mail' && data[3][pos][k] == 'none' && !(ccTree.catalog_perms & 2) ) {
1888                                                Element(id).style.display = 'none';
1889                                                continue;
1890                                        }*/
1891                               
1892                                        if(data[3][pos][k] != 'none')
1893                                        {
1894                                                switch (data[2][k])
1895                                                {
1896                                                        case 'cc_name':
1897                                                                if (data[3][pos][k].length > 50)
1898                                                                {
1899                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 50);
1900                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
1901                                                                }
1902                                                                else
1903                                                                {
1904                                                                        Element(id+':'+data[2][k]).innerHTML = data[3][pos][k];
1905                                                                }
1906                                                                break;
1907                                                       
1908                                                        case 'cc_mail':
1909                                                               
1910                                                                if (data[3][pos][k].length > (CC_visual == 'table'  ? 50 : 20))
1911                                                                {
1912                                                                        Element(id+':'+data[2][k]).innerHTML = data[5] + data[3][pos][k] + '\')">'+ adjustString(data[3][pos][k], (CC_visual == 'table'  ? 50 : 20))+'</span>';
1913                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
1914                                                                }
1915                                                                else
1916                                                                {
1917                                                                        Element(id+':'+data[2][k]).innerHTML = data[5] + data[3][pos][k] + '\')">'+ data[3][pos][k]+'</span>';                                                                 
1918                                                                }
1919                                                                break;
1920                                                       
1921                                                        case 'cc_phone':
1922                                                                if (data[3][pos][k].length > 20)
1923                                                                {
1924                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 20);
1925                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
1926                                                                }
1927                                                                else
1928                                                                {
1929                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 20);
1930                                                                }
1931                                                                Element(id+':cc_phone').innerHTML = data[3][pos][k];
1932                                                                break;
1933
1934                                                        case 'cc_title':
1935                                                                if (data[3][pos][k].length > 15)
1936                                                                {
1937                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 15);
1938                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
1939                                                                }
1940                                                                else
1941                                                                {
1942                                                                        Element(id+':'+data[2][k]).innerHTML = data[3][pos][k];
1943                                                                }
1944                                                                break;
1945
1946                                                        case 'cc_id':
1947                                                                var id_contact = data[3][pos][k];
1948                                                                Element(id+':'+data[2][k]).value = data[3][pos][k];
1949                                                                Element(id+':cc_photo').src = '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_photo' + (data[4][pos] != 0 ? '&id='+data[3][pos][k] : '');
1950                                                                if(ccTree.catalog_perms == 1)
1951                                                                        Element(id+':cc_icon_data').innerHTML =  '<span title="'+Element('cc_msg_copy_to_catalog').value+'" id="' + id + ':ccQuickAdd" onmouseout="window.status=\'\';" onclick="ccQuickAddContact.showList(\''+ Element(id+':cc_id').value + '\');return true;" style="cursor: pointer; cursor: hand; z-index: 1"><img src="templates/default/images/address-conduit-16.png" align="center"></span>';
1952                                                                break;
1953
1954                                                        case 'cc_forwarding_address':
1955                                                                var contacts = data[3][pos][k];
1956                                                                                                                               
1957                                                                if( !contacts)
1958                                                                        break;
1959                                                                                                                                                                                                                                                                                                                               
1960                                                                Element(id+':cc_icon_group').innerHTML =  '<span title="'+Element('cc_participants').value+'"  onmouseout="window.status=\'\';" onclick="ccListParticipants.showList(\''+ Element(id+':cc_id').value + '\');return true;" style="cursor: pointer; cursor: hand; z-index: 1"><img src="templates/default/images/people-mini.png" align="center">&nbsp;&nbsp;</span>';
1961                                                                break;
1962
1963                                                        default:
1964                                                                if (data[3][pos][k].length > 10)
1965                                                                {
1966                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 10);
1967                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
1968                                                                }
1969                                                                else
1970                                                                {
1971                                                                        Element(id+':'+data[2][k]).innerHTML = data[3][pos][k];
1972                                                                }
1973                                                }
1974                                        }
1975                                }
1976                                if (type == "shared") {
1977                                        if (data[3][pos][8] & 4)
1978                                                eval("document.getElementById(id + ':cc_card_edit').onclick = function(){editSharedContact(Element('"+id+"' + ':cc_id').value);};");
1979                                        else
1980                                                document.getElementById(id + ':cc_card_edit').onclick = function(){
1981                                                        alert(Element('cc_msg_not_allowed').value);
1982                                                };
1983                                        if (data[3][pos][8] & 8)
1984                                                eval("document.getElementById(id + ':cc_card_remove').onclick = function(){removeEntry(Element('" + id + "' + ':cc_id').value);};");
1985                                        else
1986                                                document.getElementById(id + ':cc_card_remove').onclick = function(){
1987                                                        alert(Element('cc_msg_not_allowed').value);
1988                                                };
1989                                }
1990                                if (--ncards == 0)
1991                                {
1992                                        j = CC_max_cards[0];
1993                                        i = CC_max_cards[1];
1994                                }
1995       
1996                                pos++;
1997                        }
1998                }
1999        }
2000}
2001
2002function populateGroupsInCards(data)
2003{       
2004        var pos = 0;
2005        var contacts = data[5];         
2006        var ncards = data[3].length;
2007       
2008        if (typeof(data[3]) == 'object' && ncards > 0)
2009        {
2010                for (var i = 0; i < CC_max_cards[1]; i++)
2011                {
2012                        for (var j = 0; j < CC_max_cards[0]; j++)
2013                        {
2014                                id = 'cc_card:'+j+':'+i;
2015                       
2016                                for (var k = 0; k < data[2].length; k++)
2017                                {
2018                               
2019                                        if(data[3][pos][k] != 'none')
2020                                        {       
2021                                                                                       
2022                                                switch (data[2][k])
2023                                                {                                                       
2024                                                        case 'cc_title':
2025                                                                if (data[3][pos][k].length > 50)
2026                                                                {
2027                                                                        Element(id+':'+data[2][k]).innerHTML = adjustString(data[3][pos][k], 50);
2028                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
2029                                                                }
2030                                                                else
2031                                                                {
2032                                                                        Element(id+':'+data[2][k]).innerHTML = data[3][pos][k];
2033                                                                }
2034                                                                break;
2035                                                       
2036                                                        case 'cc_short_name':
2037                                                                if (data[3][pos][k].length > (CC_visual == 'table'  ? 50 : 20))
2038                                                                {
2039                                                                        Element(id+':'+data[2][k]).innerHTML = data[5] + ''+data[3][pos][k] +'\')">'+adjustString(data[3][pos][k], (CC_visual == 'table'  ? 50 : 20))+'</span>';
2040                                                                        Element(id+':'+data[2][k]).title = data[3][pos][k];
2041                                                                }
2042                                                                else
2043                                                                {
2044                                                                        Element(id+':'+data[2][k]).innerHTML = data[5] + ''+data[3][pos][k] + '\')">'+data[3][pos][k]+'</span>';
2045                                                                }
2046                                                                break;
2047                                                       
2048                                                        case 'cc_contacts':
2049
2050                                                                var id_group = data[3][pos][k-1];
2051                                                                var title = data[3][pos][k-3];
2052                                                                contacts = data[3][pos][k];
2053                                                                var contact = "";
2054                                                                var email = "";
2055
2056                                                                for (var d = 0; d < contacts.length; d++) {                                                                                                                                                                                                                                                                                                     
2057                                                                        contact += contacts[d]['names_ordered']+ ",";
2058                                                                        email += contacts[d]['connection_value']+",";
2059                                                                }
2060                                                               
2061                                                                Element(id+':cc_participantes').innerHTML = '<span title="Ver Participantes" id="' + id + ':ccQuickAdd" onmouseout="window.status=\'\';" onclick="ccListParticipants.showList(\''+ Element(id+':cc_id').value+'value\', \''+contact+'\', \''+email+'\', \''+title+'\', \''+id_group+'\');return true;" style="cursor: pointer; cursor: hand; z-index: 1"><img title="Ver participantes" align="center" src="templates/default/images/people-mini.png">&nbsp;&nbsp</span>';
2062                                                                                                                                                                       
2063                                                                break;
2064                                                       
2065                                                        case 'cc_id':
2066                                                                var id_contact = data[3][pos][k];
2067                                                                Element(id+':'+data[2][k]).value = data[3][pos][k];
2068                                                                                                                       
2069                                                                break;
2070                                                               
2071                                                }
2072                                        }
2073                                }
2074       
2075                                if (--ncards == 0)
2076                                {
2077                                        j = CC_max_cards[0];
2078                                        i = CC_max_cards[1];
2079                                }
2080       
2081                                pos++;
2082                        }
2083                }
2084        }
2085}
2086
2087function adjustString (str, max_chars)
2088{
2089        if (str.length > max_chars)
2090        {
2091                return str.substr(0,max_chars) + '...';
2092        }
2093        else
2094        {
2095                return str;
2096        }
2097}
2098
2099function setMaxCards (maxcards)
2100{
2101        CC_max_cards = maxcards;
2102        ncards = maxcards[0] * maxcards[1];
2103
2104        var handler = function (responseText)
2105        {
2106                showMessage('ok');
2107        };
2108
2109        Connector.newRequest('setMaxCards', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=set_n_cards&ncards=' + ncards, 'GET');
2110}
2111
2112function getMaxCards ()
2113{
2114        var coord = new Array();
2115       
2116        //Element('cc_card_space').innerHTML = '';
2117        //return;
2118
2119        card_space_width = parseInt(Element('cc_main').offsetWidth) - parseInt(Element('cc_left').offsetWidth) - parseInt(CC_card_extra);
2120        //card_space_width = parseInt(is_ie ? document.body.offsetWidth : window.innerWidth) - parseInt(Element('cc_left').offsetWidth) - parseInt(CC_card_extra) - 40;
2121        card_space_height = parseInt(Element('cc_card_space').offsetHeight) - parseInt(CC_card_extra);
2122       
2123        card_width = CC_card_image_width + CC_card_extra;
2124        card_height = CC_card_image_height + CC_card_extra;
2125
2126        ncols = parseInt(card_space_width / card_width);
2127        nlines = parseInt(card_space_height / card_height);
2128       
2129        coord[0] = ncols;
2130        //coord[1] = nlines;
2131        coord[1] = 10;
2132
2133        //alert( 'WIDTH: ' + card_space_width + ' / ' + card_width + ' = ' + card_space_width / card_width + "\n" +
2134        //      'HEIGHT: ' + card_space_height + ' / ' + card_height + ' = ' + card_space_height / card_height );
2135
2136        return coord;
2137}
2138
2139function getCardHTML (id, type)
2140{
2141                if(type == 'groups') {
2142                        html_card = '<td id="' + id + '" style="width: ' + CC_card_image_width + 'px; height: ' + CC_card_image_height + '">' +
2143            '<div style="border: 0px solid #999; position: relative;">' +
2144                                '<img src="templates/default/images/card.png" border="0" width="' + CC_card_image_width +'" height="' + CC_card_image_height + '"i ondblclick="editContact(Element(\'' + id + ':cc_id\').value);">' +
2145                                '<img title="'+Element('cc_msg_group_edit').value+'" id="' + id + ':cc_card_edit" style="position: absolute; top: 35px; left: 222px; width: 18px; height: 18px; cursor: pointer; cursor: hand; z-index: 1" onclick="editGroup(Element(\'' + id + ':cc_id\').value);" onmouseover="resizeIcon(\''+id+':cc_card_edit\',0)" onmouseout="resizeIcon(\''+id+':cc_card_edit\',1)" src="templates/default/images/cc_card_edit.png">' +
2146                                '<img title="'+Element('cc_msg_group_remove').value+'" id="' + id + ':cc_card_remove" style="position: absolute; top: 78px; left: 223px; width: 15px; height: 14px; cursor: pointer; cursor: hand; z-index: 1" onclick="removeEntry(Element(\'' + id + ':cc_id\').value,\'groups\');" onmouseover="resizeIcon(\''+id+':cc_card_remove\',0)" onmouseout="resizeIcon(\''+id+':cc_card_remove\',1)" src="templates/default/images/cc_x.png">' +
2147                                '<span id="' + id + ':cc_title" style="position: absolute; top: 30px; left: 75px; width: 135px; border: 0px solid #999; font-weight: bold; font-size: 10px; text-align: center; height: 10px;" onmouseover="//Element(\''+id+':cc_name_full\').style.visibility=\'visible\'" onmouseout="//Element(\''+id+':cc_name_full\').style.visibility=\'hidden\'"></span>' +
2148                                '<span id="' + id + ':cc_participantes" style="cursor: pointer; cursor: hand; z-index: 1;position: absolute; top: 15px; left: 15px"></span>' +
2149                                '<span onMouseOver="this.title = \''+Element('cc_send_mail').value+' => '+'\'+document.getElementById(\''+id + ':cc_title\').innerHTML" id="' + id + ':cc_short_name" style="position: absolute; top: 105px; left: 75px; width: 135px; border: 0px solid #999; font-weight: normal; font-size: 10px; text-align: center; height: 10px;"></span>' +
2150                                '<input id="' + id + ':cc_id" type="hidden">' +
2151                        '</div>' + '</td>';
2152               
2153                }
2154                else {
2155                        html_card = '<td id="' + id + '" style="width: ' + CC_card_image_width + 'px; height: ' + CC_card_image_height + '">' +
2156                                '<div style="border: 0px solid #999; position: relative;">' +
2157                                        '<img src="templates/default/images/card.png" border="0" width="' + CC_card_image_width +'" height="' + CC_card_image_height + '"i ondblclick="editContact(Element(\'' + id + ':cc_id\').value);">' +
2158                                                ( ccTree.catalog_perms == 1 ?
2159                                                '<span id="' + id + ':cc_icon_data" style="position: absolute; top: 35px; left: 222px; width: 18px; height: 18px; cursor: pointer; cursor: hand; z-index: 1"></span>':'') +
2160                                                (ccTree.catalog_perms & 2 ?
2161                                                '<img title="'+Element('cc_msg_card_edit').value+'" id="' + id + ':cc_card_edit" style="position: absolute; top: 35px; left: 222px; width: 18px; height: 18px; cursor: pointer; cursor: hand; z-index: 1" onclick="editContact(Element(\'' + id + ':cc_id\').value);" onmouseover="resizeIcon(\''+id+':cc_card_edit\',0)" onmouseout="resizeIcon(\''+id+':cc_card_edit\',1)" src="templates/default/images/cc_card_edit.png">' +
2162                                                '<img title="'+Element('cc_msg_card_remove').value+'" id="' + id + ':cc_card_remove" style="position: absolute; top: 78px; left: 223px; width: 15px; height: 14px; cursor: pointer; cursor: hand; z-index: 1" onclick="removeEntry(Element(\'' + id + ':cc_id\').value);" onmouseover="resizeIcon(\''+id+':cc_card_remove\',0)" onmouseout="resizeIcon(\''+id+':cc_card_remove\',1)" src="templates/default/images/cc_x.png">' : '') +
2163                                                '<img id="' + id + ':cc_photo" style="position: absolute; top: 15px; left: 7px;" src="" border="0" ondblclick="editContact(Element(\'' + id + ':cc_id\').value);">' +
2164                                                '<span id="' + id + ':cc_company" style="position: absolute; top: 5px; left: 75px; width: 135px; border: 0px solid #999; font-weight: bold; font-size: 14px; text-align: center; height: 10px;" onmouseover="//Element(\''+id+':cc_company_full\').style.visibility=\'visible\'" onmouseout="//Element(\''+id+':cc_company_full\').style.visibility=\'hidden\'"></span>' +
2165                                                '<span style="cursor: pointer; cursor: hand; z-index: 1;position: absolute; top: 100px; left: 35px"  valign="bottom" id="' + id + ':cc_icon_group">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>'+
2166                                                '<span id="' + id + ':cc_name" style="position: absolute; top: 30px; left: 75px; width: 135px; border: 0px solid #999; font-weight: bold; font-size: 10px; text-align: center; height: 10px;" onmouseover="//Element(\''+id+':cc_name_full\').style.visibility=\'visible\'" onmouseout="//Element(\''+id+':cc_name_full\').style.visibility=\'hidden\'"></span>' +
2167                                                '<span id="' + id + ':cc_title" style="position: absolute; top: 60px; left: 75px; width: 135px; border: 0px solid #999; font-weight: normal; font-size: 12px; text-align: center; height: 10px;"></span>' +
2168                                                '<span id="' + id + ':cc_phone" style="position: absolute; top: 90px; left: 75px; width: 135px; border: 0px solid #999; font-weight: normal; font-size: 10px; text-align: center; height: 10px;"></span>' +
2169                                                '<span id="' + id + ':cc_mail" style="position: absolute; top: 105px; left: 75px; width: 135px; border: 0px solid #999; font-weight: normal; font-size: 10px; text-align: center; height: 10px;"></span>' +
2170                                                '<span id="' + id + ':cc_alias" style="position: absolute; top: 95px; left: 10px; width: 60px; border: 0px solid #999; font-weight: normal; font-size: 9px; text-align: center; height: 10px;"></span>' +
2171                                        '<input id="' + id + ':cc_id" type="hidden">' +
2172                                '</div>' + '</td>';
2173                }
2174       
2175
2176        return html_card;
2177}
2178
2179function getTableHTML (id, type)
2180{
2181                        var bg = "";
2182                        if(!is_ie)
2183                                bg = "this.style.background=\'\'";
2184                        else
2185                                bg = "this.style.background=\'#EEEEEE\'";
2186                        if(type == 'groups') {
2187                                html_card = '<tr width="40%" id="' + id + '" onmouseout="'+bg+'" onmouseover="this.style.background=\'LIGHTYELLOW\'" bgcolor="EEEEEE"><td width="auto" style="font-weight: normal; font-size: 10px; text-align: left; height: 10px;">' +
2188                                        '<span id="' + id + ':cc_participantes" style="cursor: pointer; cursor: hand; z-index: 1"></span>' +
2189                                        '<span id="' + id + ':cc_title"></span></td>' +
2190                                        '<td width="40%" style="solid #999; font-weight: normal; font-size: 10px; text-align: left; height: 10px"><span onMouseOver="this.title = \''+Element('cc_send_mail').value+' => '+'\'+document.getElementById(\''+id + ':cc_title\').innerHTML"  id="' + id + ':cc_short_name"></span></td>' +
2191                                        '<td align="left" width="55px">'+
2192                                        '<img  title="'+Element('cc_msg_group_edit').value+'" id="' + id + ':cc_card_edit" style=" cursor: pointer; cursor: hand; z-index: 1;width: 18px; height: 18px;"  onclick="editGroup(Element(\'' + id + ':cc_id\').value);" src="templates/default/images/cc_card_edit.png">' +
2193                                        '&nbsp;&nbsp;|&nbsp;&nbsp;'+
2194                                        '<img  title="'+Element('cc_msg_group_remove').value+'" id="' + id + ':cc_card_remove" style="width: 15px; height: 14px; cursor: pointer; cursor: hand; z-index: 1" onclick="removeEntry(Element(\'' + id + ':cc_id\').value,\'groups\');" src="templates/default/images/cc_x.png">'  +
2195                                        '<input id="' + id + ':cc_id" type="hidden">'+                                                                         
2196                                        '</td></tr>';
2197                                               
2198                       
2199                        }
2200                        else {
2201                                html_card = '<tr  style="height:20px" id="' + id + '" onmouseout="'+bg+'" onmouseover="this.style.background=\'LIGHTYELLOW\'" bgcolor="EEEEEE"><td width="auto" style="font-weight: normal; font-size: 10px; text-align: left; height: 10px;">' +                                       
2202                                        '<span valign="bottom" id="' + id + ':cc_icon_group">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="' + id + ':cc_name"></span></td>' +
2203                                        '<td style="solid #999; font-weight: normal; font-size: 10px; text-align: left; height: 10px"><span onMouseOver="this.title = \''+Element('cc_send_mail').value+' => '+'\'+document.getElementById(\''+id + ':cc_name\').innerHTML" id="' + id + ':cc_mail"></span></td>' +
2204                                        '<td align="center" nowrap><span style="solid #999; font-weight: normal; font-size: 10px;height: 10px" id="' + id + ':cc_phone"></span></td>' +         
2205                                        ( ccTree.catalog_perms == 1 ?
2206                                        '<td align="left"><span valign="bottom" id="' + id + ':cc_icon_data"></span></td>':'') +
2207                                        '<td align="left" >'+
2208                                        (ccTree.catalog_perms & 2 ?
2209                                        '<img  title="'+Element('cc_msg_card_edit').value+'" id="' + id + ':cc_card_edit" style=" cursor: pointer; cursor: hand; z-index: 1;width: 18px; height: 18px;"  onclick="editContact(Element(\'' + id + ':cc_id\').value);" src="templates/default/images/cc_card_edit.png">' +
2210                                        '&nbsp;&nbsp;|&nbsp;&nbsp;'+
2211                                        '<img title="'+Element('cc_msg_card_remove').value+'" id="' + id + ':cc_card_remove" style="width: 15px; height: 14px; cursor: pointer; cursor: hand; z-index: 1" onclick="removeEntry(Element(\'' + id + ':cc_id\').value);" src="templates/default/images/cc_x.png">' : '') +
2212                                        '<input id="' + id + ':cc_id" type="hidden">'+
2213                                        '<input type="hidden" id="' + id + ':cc_photo">' +
2214                                        '<span id="' + id + ':cc_title" style="display:none"></span>' +
2215                                        '<span id="' + id + ':cc_alias" style="display:none"></span>' +
2216                                        // Esse campo é necessário se o contato possui dados no campo cc_company
2217                                        '<span id="' + id + ':cc_company" style="display:none"></span>' +
2218                                        '</td></tr>';
2219                        }
2220
2221        return html_card;
2222}
2223
2224function drawTable(ncards, type)
2225{
2226        var pos;
2227       
2228        html_cards = '<div id="divScrollMain" style="overflow:auto;z-index:1"><table width="100%" border="0" cellpadding="0" cellspacing="3">';
2229       
2230        if (ncards > 0)
2231        {
2232
2233                for (var i = 0; i < CC_max_cards[1]; i++)
2234                {
2235                        html_cards += '';
2236                        for (var j = 0; j < CC_max_cards[0]; j++)
2237                        {
2238                                html_cards += getTableHTML('cc_card:' + j + ':' + i, type);
2239                                if (--ncards == 0)
2240                                {
2241                                        j = CC_max_cards[0];
2242                                        i = CC_max_cards[1];
2243                                }
2244                        }
2245                        html_cards += '';
2246                }
2247                if((ccTree.catalog_perms & 2) && type != 'groups' && type!='shared')
2248                        html_cards += '<tr><td colspan=4 align="right"><button id="cc_button_tools" value="" type="button" onclick="javascript:removeAllEntries()">Remover Todos</button></td></tr>';
2249        }       
2250        else if (CC_max_cards != 0)
2251        {
2252                html_cards += '<tr><td  align="center">' + Element('cc_msg_no_cards').value + '</td></tr>';
2253        }
2254        else
2255        {
2256                html_cards += '<tr><td  align="center">' + Element('cc_msg_err_no_room').value + '</td></tr>';
2257        }
2258
2259        html_cards += '</table></div>';
2260
2261        Element('cc_card_space').innerHTML = html_cards;
2262}
2263
2264function drawCards(ncards, type)
2265{
2266        var pos;
2267        html_cards = '<div id="divScrollMain" style="overflow:auto;z-index:1">';
2268        html_cards += '<table  border="0" cellpadding="0" cellspacing="' + CC_card_extra + '">';
2269       
2270        if (ncards > 0)
2271        {
2272                for (var i = 0; i < CC_max_cards[1]; i++)
2273                {
2274                        html_cards += '<tr>';
2275                        for (var j = 0; j < CC_max_cards[0]; j++)
2276                        {
2277                                html_cards += getCardHTML('cc_card:' + j + ':' + i, type);
2278                                if (--ncards == 0)
2279                                {
2280                                        j = CC_max_cards[0];
2281                                        i = CC_max_cards[1];
2282                                }
2283                        }
2284                        html_cards += '</tr>';
2285                }
2286                if((ccTree.catalog_perms & 2) && type != 'groups' && type!='shared')
2287                        html_cards += '<tr><td colspan=3 align="right"><button id="cc_button_tools" value="" type="button" onclick="javascript:removeAllEntries()">Remover Todos</button></td></tr>';
2288        }       
2289        else if (CC_max_cards != 0)
2290        {
2291                html_cards += '<tr><td>' + Element('cc_msg_no_cards').value + '</td></tr>';
2292        }
2293        else
2294        {
2295                html_cards += '<tr><td>' + Element('cc_msg_err_no_room').value + '</td></tr>';
2296        }
2297
2298        html_cards += '</table></div>';
2299
2300        Element('cc_card_space').innerHTML = html_cards;
2301}
2302
2303function showCards (letter,page, ids)
2304{
2305        var data  = new Array();
2306
2307        if ( letter != CC_actual_letter )
2308        {
2309                CC_actual_page = '1';
2310        }
2311        else
2312        {
2313                CC_actual_page = page;
2314        }
2315
2316        CC_actual_letter = letter;
2317
2318        if (CC_max_cards[0] == 0)
2319        {
2320
2321                if(CC_visual == 'cards')
2322                        drawCards(0);
2323                else if(CC_visual == 'table')
2324                        drawTable(0);
2325                       
2326                setPages(0,0);
2327                return;
2328        }
2329
2330        var handler = function (responseText)
2331        {
2332                var data = new Array();
2333                data = unserialize(responseText);
2334                if (data[0] == '0')
2335                {                               
2336                        Element('cc_type_contact').value = data[1];                     
2337                        CC_npages = 0;
2338                        CC_actual_page = 1;
2339                        if(CC_visual == 'cards')
2340                                drawCards(0);
2341                        else if(CC_visual == 'table')
2342                                drawTable(0);
2343                        setPages(0,0);
2344                        return;
2345                }
2346                else
2347                Element('cc_type_contact').value = data[10];
2348               
2349//              Element('cc_debug').innerHTML = responseText;                                           
2350               
2351                if (typeof(data) != 'object')
2352                {
2353                        showMessage(Element('cc_msg_err_contacting_server').value);
2354                        return;
2355                }
2356               
2357                if (typeof(data[3]) == 'object')
2358                {
2359                        CC_npages = parseInt(data[0]);
2360                        CC_actual_page = parseInt(data[1]);                     
2361                        if(CC_visual == 'cards')
2362                                drawCards(data[3].length, data[10]);
2363                        else if(CC_visual == 'table')
2364                                drawTable(data[3].length, data[10]);
2365                        resizeWindow();
2366                        populateCards(data, data[10]);
2367                        setPages(data[0], data[1]);
2368                       
2369                }
2370                else
2371                {
2372                        showMessage(Element('cc_msg_err_contacting_server').value);
2373                        return;
2374                }
2375        };
2376
2377        var info = "letter="+letter+"&page="+CC_actual_page+"&ids="+ids;
2378        Connector.newRequest('showCards', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_cards_data', 'POST', handler, info);
2379}
2380
2381function clearCards()
2382{
2383        clearLetterSelection();
2384        setHeightSpace();
2385        setMaxCards(getMaxCards());
2386
2387        if(CC_visual == 'cards')
2388                drawCards(0);
2389        else if(CC_visual == 'table')
2390                drawTable(0);
2391
2392        setPages(0,0);
2393        return;
2394}
2395
2396/***********************************************\
2397*        COMMON ENTRY FUNCTIONS                *
2398\***********************************************/
2399
2400function ccChangeVisualization(type)
2401{
2402        var table_h = Element('cc_panel_table');
2403        var cards_h = Element('cc_panel_cards');
2404       
2405        switch (type)
2406        {
2407                case 'cards':
2408                        cards_h.style.display = 'none';
2409                        table_h.style.display = 'inline';
2410                        break;
2411
2412                case 'table':
2413                        table_h.style.display = 'none';
2414                        cards_h.style.display = 'inline';
2415                        break;
2416        }
2417       
2418        CC_visual = type;
2419        showCards(getActualLetter(), getActualPage());
2420}
2421
2422function ccSearchUpdate(ids)
2423{
2424        Element('cc_panel_letters').style.display = 'none';
2425        Element('cc_panel_search').style.display  = 'inline';
2426       
2427        if(CC_visual == 'cards')
2428                drawCards(0);
2429        else if(CC_visual == 'table')
2430                drawTable(0);
2431
2432        if (!ids)
2433        {
2434                ccSearchHide();
2435                return;
2436        }
2437       
2438        var sIds = serialize(ids);
2439
2440        if (CC_actual_letter != 'search')
2441        {
2442                CC_last_letter = CC_actual_letter;
2443        }
2444        showCards('search', '1', sIds);
2445}
2446
2447function ccSearchHidePanel()
2448{
2449        Element('cc_panel_search').style.display  = 'none';
2450        Element('cc_panel_letters').style.display = 'inline';
2451        if (CC_actual_letter == 'search')
2452        {
2453                CC_actual_letter = CC_last_letter;
2454        }
2455}
2456
2457function ccSearchHide()
2458{
2459        Element('cc_panel_search').style.display  = 'none';
2460        Element('cc_panel_letters').style.display = 'inline';
2461        clearCards();
2462        //showCards(CC_last_letter, '1');
2463}
2464
2465/***********************************************\
2466*               QUICK ADD FUNCTIONS             *
2467\***********************************************/
2468
2469
2470function resetQuickAdd ()
2471{
2472        Element('cc_qa_alias').value = '';
2473        Element('cc_qa_given_names').value = '';
2474        Element('cc_qa_family_names').value = '';
2475        Element('cc_qa_phone').value = '';
2476        Element('cc_qa_email').value = '';
2477}
2478
2479function getQuickAdd ()
2480{
2481        var data = new Array();
2482        data[0] = Element('cc_qa_alias').value;
2483        data[1] = Element('cc_qa_given_names').value;
2484        data[2] = Element('cc_qa_family_names').value;
2485        data[3] = Element('cc_qa_phone').value;
2486        data[4] = Element('cc_qa_email').value;
2487       
2488        return data;
2489}
2490
2491function sendQuickAdd ()
2492{
2493        var data = getQuickAdd();
2494       
2495        var str = serialize(data);
2496
2497        if (!str)
2498        {
2499                return false;
2500        }
2501
2502        var handler = function (responseText)
2503        {
2504                setTimeout('updateCards()',100);;
2505        }
2506
2507        resetQuickAdd();
2508
2509        Connector.newRequest('quickAdd', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=quick_add', 'POST', handler, 'add='+escape(str));
2510}
2511
2512function connectVoip (phoneUser, typePhone){
2513        var handler_voip = function (responseText){
2514                if(!responseText) {
2515                        alert("Erro conectando servidor VoIP.");
2516                }
2517                else{
2518                        data = unserialize(responseText);
2519                        alert("Requisitando chamada para o ramal: "+data);
2520                }
2521        }
2522        Connector.newRequest('voip', "../../expressoMail1_2/controller.php?action=expressoMail1_2.functions.callVoipconnect&to="+phoneUser+"&typePhone="+typePhone, 'POST', handler_voip);
2523        }
Note: See TracBrowser for help on using the repository browser.