
        function showInviteEmail(obj) {
                emailWindowType = "invite";
                if (!contactsLoaded)
                     loadContacts("email_recipients");
                
                document.getElementById("email_recipients").value = "";
                document.getElementById("email_message").value = "";
                var loc = document.getElementById("emw");
                var left = DL_GetElementLeft(loc);
                var top = DL_GetElementTop(loc);

                dd.elements["emailWindow"].moveTo(left, top);
                dd.elements["emailWindow"].show();
                document.getElementById("email_recipients").focus();
        }

        function showEventEmail(obj) {
                if (document.getElementById("authenticated").value == "false") {
                        alert("You must be logged in to send an invite");
                        return;
                }

                document.getElementById("bz_friends").value = "";
                document.getElementById("email_recipients").value = "";
                document.getElementById("email_message").value = "";
                var left = DL_GetElementLeft(obj) + obj.offsetWidth;
                var top = DL_GetElementTop(obj);

                dd.elements["emailWindow"].moveTo(left, top);
                dd.elements["emailWindow"].show();
        }

        function getEmailRecipients(){
                var email_recipients = document.getElementById("email_recipients").value;
                                if (trim(email_recipients) == "") {
                                        alert("Email address required");
                                        return null;
                                }

                                email_recipients = replaceSemiColons(email_recipients);
                                var addresses = email_recipients.split(",");

                                addresses = removeBrackets(addresses);

                                //showList(addresses);

                                for (var i = 0; i < addresses.length; i++) {
                                        if (!isEmailValid(trim(addresses[i]))) {
                                                alert(trim(addresses[i]) + " is not a valid email address.");
                                                return null;
                                        }
                                }

                                email_recipients ="";
                                for (var i = 0; i < addresses.length; i++) {
                                        email_recipients += addresses[i]  + ","
                                }
                                email_recipients = email_recipients.substring(0,email_recipients.length-1);
                return email_recipients;
        }

        function emailWindowSendClicked(){
                if (emailWindowType=="invite"){
                        emailInvite();
                } else if (emailWindowType=="picture"){
                        emailPicture();
                } else if (emailWindowType=="thread"){
                        emailThread();
                } else if (emailWindowType=="event"){
                        emailEvent();
                }
        }
        
        function hideEmailWindow(obj) {
                dd.elements["emailWindow"].hide();
                document.getElementById("email_recipients").value="";
                document.getElementById("email_message").value="";
        }

        function showList(s) {
                //alert(s.length);
                for (var i = 0; i < s.length; i++) {
                        alert(s[i]);
                }
        }

        function replaceSemiColons(s) {
                while (s.indexOf(';') > -1) {
                        s = s.replace(";", ",");
                }

                return s;
        }

        function removeBrackets(addresses) {

                for (var i = 0; i < addresses.length; i++) {
                        var found = addresses[i].indexOf('[')
                        if (found > -1)
                                addresses[i] = trim(addresses[i].substring(0, found));
                }

                return addresses;
        }

        function loadContacts(id) {
                if (contactsLoaded)
                        return;
                
                var ary = new Array();
                var response = sendRequest("/myemailcontacts.jsp", "");
                var code = getResponseCode(response);
                var text = trim(getResponseText(response));

                var contacts = text.split(";");
                for (var i = 0; i < contacts.length - 1; i++) {
                        ary[i] = contacts[i];
                }
                contactsLoaded = true;
                var contact_name_auto_completer = new Autocompleter.Local(id, id + '_auto_complete', ary, { tokens: new Array(',', '\n'), fullSearch: true, partialSearch: true })
        }

        var emailWindowUrl = "";
        var emailWindowType ="";
        
        function showEmailWindow(url, objectId, w, h, windowType) {
                   
                if (document.getElementById("in").value =="0"){
                        alert("You must be logged in to send an email.");
                        return;
                }
                emailWindowType = windowType;
                emailWindowUrl = url;
                
                 if (!contactsLoaded)
                     loadContacts("email_recipients");

                
                var left = 0;
                var top = 0;
                if (windowType == "picture") {
                        top = DL_GetElementTop(document.getElementById(objectId)) - 180;
                        left = DL_GetElementLeft(document.getElementById(objectId))-8;
                } else if (windowType == "thread") {
                        top = DL_GetElementTop(document.getElementById(objectId)) + 14;
                        left = DL_GetElementLeft(document.getElementById(objectId))-w+95;
                } else if (windowType == "event") {
                        top = DL_GetElementTop(document.getElementById(objectId)) + 28;
                        left = DL_GetElementLeft(document.getElementById(objectId));
                }


                document.getElementById("email_recipients").style.width = (w * .78) + "px";
                document.getElementById("email_message").style.width = document.getElementById("email_recipients").style.width;

                dd.elements["emailWindow"].resizeTo(w, h);
                dd.elements["emailWindow"].moveTo(left, top);
                //centerWindow("emailWindow", w, h);
                dd.elements["emailWindow"].show();
                document.getElementById("email_recipients").focus();
        }

        function centerWindow(windowId, w, h){
                var s = getViewPointDimension();
                s = s.split(",");

                var _w = (s[0]/2) - (w/2);
                var _h = (s[1]/2) - (h/2);

                var url = new String(window.location);
                
                if (url.indexOf('viewer/') > -1){
                        _h = h + 150;
                }
                dd.elements[windowId].moveTo(_w, _h);
        }

        function getViewPointDimension(){
                var xMax = screen.width, yMax = self.screen.availHeight;
                var lnBodyClientWidth ,lnBodyClientHeight;
                if (document.body.clientWidth)
                {
                lnBodyClientWidth= (document.body.clientWidth > window.screen.width?window.screen.width:document.body.clientWidth);
                lnBodyClientHeight=(document.body.clientHeight > window.screen.height?window.screen.height:document.body.clientHeight);
                }
                else
                {
                lnBodyClientWidth=(window.innerWidth > window.screen.width ? window.screen.width : window.innerWidth);
                lnBodyClientHeight=(window.innerHeight > window.screen.height ? window.screen.height : window.innerHeight);
                }

                return lnBodyClientWidth + "," + lnBodyClientHeight;

        }
        function emailEvent() {
                var email_message = escape(document.getElementById("email_message").value);
                var email_recipients = document.getElementById("email_recipients").value;
                var eventId = document.getElementById("eventId").value;

                if (email_recipients == null)
                        return;

                var code = getResponseCode(sendRequest("/emailContent.jsp", "type=event&eventId=" + eventId + "&email=" + email_recipients + "&msg=" + email_message + "&url=" + escape(emailWindowUrl)));
                if (code == "200") {
                        hideEmailWindow();
                        alert("Event invitation successfully sent");
                } else {
                        alert("A problem occurred sending invitation. All recipients may not have received the invitation.");
                }
        }

        function emailInvite() {
                var email_message = escape(document.getElementById("email_message").value);
                var email_recipients = getEmailRecipients();
                var group = document.getElementById("groupId");
                var groupId = 0;
                if (group == null)  {
                    groupId = "0";
                } else if(group.value =="0") {
                    groupId = "0";
                } else {
                    groupId = group.value;
                }

                if (email_recipients == null)
                        return;

                var code = getResponseCode(sendRequest("/emailContent.jsp", "type=invite&email=" + email_recipients + "&msg=" + email_message + "&groupId=" + groupId));
                if (code == "200") {
                        hideEmailWindow();
                        alert("Invitation successfully sent.");
                } else {
                        alert("A problem occurred sending invitation. All recipients may not have received the invitation.");
                }
        }

        function emailPicture() {
                var email_message = escape(document.getElementById("email_message").value);
                var email_recipients = getEmailRecipients();
                if (email_recipients == null)
                        return;

                var code = getResponseCode(sendRequest("/emailContent.jsp", "type=picture&email=" + email_recipients + "&msg=" + email_message + "&url=" + escape(emailWindowUrl)));
                if (code == "200") {
                        hideEmailWindow();
                        alert("Photo successfully sent.");
                } else {
                        alert("A problem occurred sending picture. All recipients may not have received the invitation.");
                }
        }

        function emailThread() {
                var email_message = escape(document.getElementById("email_message").value);
                var email_recipients = getEmailRecipients();
                if (email_recipients == null)
                        return;

                var code = getResponseCode(sendRequest("/emailContent.jsp", "type=thread&email=" + email_recipients + "&msg=" + email_message + "&url=" + escape(emailWindowUrl)));
                if (code == "200") {
                        hideEmailWindow();
                        alert("Forum thread successfully sent.");
                } else {
                        alert("A problem occurred sending forum thread. All recipients may not have received the invitation.");
                }
        }