source: trunk/contactcenter/js/cc.js @ 294

Revision 294, 72.5 KB checked in by niltonneto, 16 years ago (diff)

Correção da lista de contatos na janela de adicionar/editar grupo.
No Internet Explorer não aparecia, pois era usado o innerHTML do Select.

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