
function show_popup(id, width, title) {
    if (width == undefined || width == null) width = "300";
    if (title == undefined || title == null) title = "&nbsp;";

    var popup = new Popup(width, title);
    var div = document.createElement("DIV");
    div.id = createUID("popup_inner");
    html_content = document.getElementById(id).innerHTML;
    
    id_replace = createUID('_');
    div.innerHTML = replaceAll(html_content, '__id__', id_replace);
    
    popup.setContent(div);
    popup.show();
}

function show_alert(content, width, title, button_class ) {
    if (width == undefined || width == null) width = "300";
    if (title == undefined || title == null) title = "&nbsp;";

    var popup = new Popup(width, title);

    var div = document.createElement("DIV");
    div.id = createUID("popup_inner");
    div.innerHTML = content;

    div.appendChild(document.createElement('HR'));

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    buttons.appendChild(createCancelButton(popup, button_class) );
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();
}

function showFeatureLayer() {
    var popup = new Popup(null, null, "new_feature_popup");

    var div = E('DIV');
    var new_feature = $('new_feature');
    div.innerHTML = new_feature.innerHTML;
    div.id = 'new_feature';

    var closeButton = childWithID(div, 'close_button');
    if (closeButton) {
        closeButton.onclick = function() {
            popup.close();
            return false;
        }
    }

    popup.setContent(div);
    popup.show();
}

function popupHelp( question, answer, height )
{
   var popup;

   var e_question = escape( question );
   var e_answer = escape( answer );

   var url = "help.php?q=" + e_question + "&a=" + e_answer;

    popup = window.open( url, "heya", "width=350, height=" + height + ", left=350, top=200, resizable=yes, close=yes, minimizable=yes, scrollbars=yes, status=no, titlebar=no" );

   if ( window.focus )
   {
      popup.focus();
   }
}

function popwin(path,width,height){
window.open(path,
            "",
            "toolbar=no,"+
            "location=no,"+
            "directories=no,"+
            "status=no,"+
            "menubar=no,"+
            "scrollbars=yes,"+
            "resizable=no,"+
            "copyhistory=no,"+
            "width="+width+","+
            "height="+height+"+")
}

function groupEmailPopup( value, form_id, hidden_id, age_restriction, msg ){
    var popup = new Popup(400, 'Group Email' );
    var div = document.createElement('DIV');
    
    var span = document.createElement('SPAN');
    if(msg){
        span.innerHTML = msg + '<br/><br/>'; 
    }
    if(age_restriction){
        span.innerHTML += 'There is an age restriction for this group. '
            + 'I confirm that I am over '+age_restriction+': '
            + '<input type="checkbox" id="age_restriction_checkbox"><br/><br/>';
    }
    span.innerHTML += 'Send me an email if there\'s a new message thread in this group.' 
    div.appendChild( span );
    div.appendChild(document.createElement("BR"));
    
    if( value != 'all' && value != 'moderator' && value != 'none' ){
        value = 'none';
    }
    
    keys = new Array( 'all', 'moderator', 'none' );
    labels = new Array( 'Yes.', 'Only if from the moderator.', 'No thanks.' );
    ids = new Array();
    for( key = 0; key<keys.length; key++ ){
        id = 'group_email_' + keys[key];
        ids[key] = id;
        radio = createFormInput( 'radio', 'email_notification', keys[key] );
        radio.id = id;
        if(keys[key]==value){
            radio.defaultChecked = true;
            radio.checked = true;
        }
        div.appendChild( radio );
        var label = document.createElement('LABEL');
        /*
        The reason element.class and label.for cannot be used is simply that 'class' and 'for' are reserved words in JavaScript, 
        and thus they had to find different names for those. Chose className and htmlFor, both of which work consistently cross-browser.
        And setAttribute doesn't work in IE 'This is probably because IE considers setAttribute as setting the property of the actual javascript object.'
        */
        if( ie ){
            label.htmlFor = id;
        }else{
            label.setAttribute( 'for', id );
        }
        label.innerHTML = labels[key] + '&nbsp;&nbsp&nbsp;';
        div.appendChild( label );
    }
    
    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    var link = createButton('btn_save');
    link.onclick = function() {
        for( key = 0; key<ids.length; key++ ){
            if( document.getElementById( ids[key] ).checked ){
                document.getElementById(hidden_id).value = document.getElementById( ids[key] ).value;
                break;
            }
        }
        if(age_restriction){
            document.getElementById('confirm_age_restriction').value = 
                (document.getElementById('age_restriction_checkbox').checked) ? '1' : '0';
        }
        document.getElementById(form_id).submit();
        return false;
    };
    buttons.appendChild(link);
    buttons.appendChild(createCancelButton(popup));
    
    div.appendChild(buttons);
    popup.setContent(div);
    popup.show();
}

function quickAddForList(list_id) {
    var popup = new Popup(470, "Add Items to List", null, 325);

    var div = document.createElement('DIV');
    div.className = 'quick_add';

    var tabs = document.createElement('UL');
    tabs.id = "tabs";
    tabs.style.margin = "0px";
    tabs.style.padding = "0px";

    var tab = document.createElement('li');
    tab.id = popup.getId() + '_tab_0';
    tab.innerHTML = "<a href=\"#\" onclick=\"toggle_tab('" + popup.getId() + "', 0); return false;\">Search</a>";
    tab.className = "selected";
    tabs.appendChild(tab);

    var tab = document.createElement('li');
    tab.id = popup.getId() + '_tab_1';
    tab.innerHTML = "<a href=\"#\" onclick=\"toggle_tab('" + popup.getId() + "', 1); return false;\">Create Item</a>";
    tabs.appendChild(tab);

    div.appendChild(tabs);

    var table = document.createElement('TABLE');
    table.style.width = "100%";
    table.style.clear = 'both';

    var tbody = document.createElement('TBODY');

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.colSpan = "3";
    cell.style.verticalAlign = 'top';

    result_id = createUID('result');

    var form = document.createElement('FORM');
    form.onsubmit = function() {
        listEntitySearch(result_id, this);
        return false;
    }

    form.id = createUID('form');

    var span = document.createElement("SPAN");
    span.className="searchGo";
    span.innerHTML = "<a href=\"#\" onclick=\"listEntitySearch('" + result_id + "', document.getElementById('" + form.id + "')); return false;\"><em>Go</em></a></span>";
    form.appendChild(span);
    
    var select = document.createElement("SELECT");
    select.name="entity_type";
    add_option(select, createFormOption('<Select Entity Type>', ''));
    add_option(select, createFormOption('Event', 'event'));
    add_option(select, createFormOption('Venue', 'venue'));
    add_option(select, createFormOption('Artist', 'artist'));

    form.appendChild(select);

    form.appendChild(document.createTextNode(' '));

    var input = createFormInput('text', 'q');
    input.size = "25";
    form.appendChild(input);

    cell.appendChild(form);

    row.appendChild(cell);
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    var result_select = document.createElement("SELECT");
    result_select.style.width = "200px";
    result_select.size = 10;
    result_select.multiple = true;
    result_select.id = result_id;

    cell.appendChild(result_select);
    row.appendChild(cell);

    var chosen_select = document.createElement("SELECT");
    chosen_select.name = "list_items_to_add[]";
    chosen_select.style.width = "200px";
    chosen_select.size = 10;
    chosen_select.multiple = true;

    var cell = document.createElement('TD');
    cell.style.textAlign = "center";
    var add_button = createFormInput('button', 'add');
    add_button.value = "->";
    add_button.onclick = function() {
        for (i=0; i<result_select.options.length; i++) {
            var option = result_select.options[i];
            if (option.selected && option.value) {
                add_option(chosen_select, createFormOption(option.text, option.value));
            }
        }
    }
    cell.appendChild(add_button);
    
    cell.appendChild(document.createElement('BR'));

    var remove_button = createFormInput('button', 'remove');
    remove_button.value = "X";
    remove_button.onclick = function() {
        i = 0;
        while (i<chosen_select.options.length) {
            var option = chosen_select.options[i];
            if (option.selected) {
                chosen_select.remove(i);
            } else {
                i++;
            }
        }
    }

    cell.appendChild(remove_button);

    row.appendChild(cell);

    var cell = document.createElement('TD');

    var form = document.createElement('FORM');
    form.id = createUID('form');
    form.method = 'POST';

    form.appendChild(createFormInput('hidden', '_submit_check', 'add_items'));
    form.appendChild(createFormInput('hidden', 'list_id', list_id));
    form.appendChild(chosen_select);

    cell.appendChild(form);
    
    row.appendChild(cell);
    tbody.appendChild(row);
    table.appendChild(tbody);

    var searchDiv = document.createElement('DIV');
    searchDiv.id = popup.getId() + '_content_0';
    searchDiv.appendChild(table);

    searchDiv.appendChild(createCancelButton(popup));

    var span = document.createElement("SPAN");
    span.className="addBtn";
    span.innerHTML = "<a href=\"#\" onclick=\"var form = document.getElementById('" + form.id + "'); var select = getFormField(form, 'list_items_to_add[]'); for (i=0; i<select.options.length; i++) {select.options[i].selected = true;} form.submit(); return false;\"><em>Add Items</em></a>";
    searchDiv.appendChild(span);

    d = document.createElement("DIV");
    d.className = "clear";
    searchDiv.appendChild(d);

    div.appendChild(searchDiv);

    var createDiv = document.createElement('DIV');
    createDiv.id = popup.getId() + '_content_1';
    createDiv.style.display = 'none';

    var form = document.createElement('FORM');
    form.method = "POST";
    form.id = createUID('form');
    form.appendChild(createFormInput('hidden', '_submit_check', 'create_entity'));
    form.appendChild(createFormInput('hidden', 'list_id', list_id));
    
    var table = document.createElement('TABLE');
    table.style.width = "100%";
    table.style.clear = 'both';

    var tbody = document.createElement('TBODY');

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Name:</strong>";
    row.appendChild(cell);

    var cell = document.createElement('TD');
    var name = createFormInput('text', 'name');
    name.style.width="100%";
    cell.appendChild(name);
    row.appendChild(cell);
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>URL:</strong>";
    row.appendChild(cell);

    var cell = document.createElement('TD');
    var url = createFormInput('text', 'url');
    url.style.width="100%";
    cell.appendChild(url);
    row.appendChild(cell);
    tbody.appendChild(row);
    table.appendChild(tbody);
    form.appendChild(table);
    createDiv.appendChild(form);

    createDiv.appendChild(createCancelButton(popup));

    var span = document.createElement("SPAN");
    span.className="createBtn";
    span.innerHTML = "<a href=\"#\" onclick=\"var form = document.getElementById('" + form.id + "'); form.submit(); return false;\"><em>Create</em></a>";
    createDiv.appendChild(span);

    d = document.createElement("DIV");
    d.className = "clear";
    searchDiv.appendChild(d);

    div.appendChild(createDiv);

    popup.setContent(div);

    popup.show();
}

function postMessage(reply_to, mb, mb_id, message_id, admin_mode, add_html, on_submit) {
    debugger;
    if (reply_to) {
        title = "Reply To " + $("who_"+reply_to).innerHTML;
    } else {
        title = "Post Your Message";
    }

    var popup = new Popup(400, title);

    message = null;
    is_edit = false;
    is_reply = false;
    if (mb != undefined && mb_id != undefined && message_id != undefined) {
        new Ajax.Request('ajax_load_message.php', {
            method: 'get',
            asynchronous: false,
            parameters: {
                board: mb,
                board_id: mb_id,
                message_id:  message_id,
                admin_mode: admin_mode
            },
            onSuccess: function(t){
                message = t.responseJSON.content;
            }
        });
        if (message != null) is_edit = true;
    }

    if (!is_edit && reply_to) {
        new Ajax.Request('ajax_load_message.php', {
            method: 'get',
            asynchronous: false,
            parameters: {
                board: mb, 
                board_id: mb_id,
                message_id:  reply_to,
                admin_mode: admin_mode
            },
            onSuccess: function(t){
                message = t.responseJSON.content;
            }
        });
        if (message != null) is_reply = true;
    }

    var div = document.createElement("DIV");
    div.className="commentForm";

    if (reply_to) {
        var d = document.createElement("DIV");
        d.style.overflow="auto";
        d.style.height="100px";
        d.style.border="1px solid gray";
        d.style.width = "100%";
        d.innerHTML = document.getElementById('content_'+reply_to).innerHTML;
        div.appendChild(d);
        div.appendChild(document.createElement("BR"));
    }
    
    var form = document.createElement("FORM");
    form.method = "POST";
    form.id = createUID("post_message");

    if( add_html ){
        var add_html_element = document.createElement('DIV');
        add_html_element.innerHTML = add_html;
        form.appendChild( add_html_element );
    }
    
    var label = document.createElement("LABEL");
    label.innerHTML = "Subject";
    form.appendChild(label);

    form.appendChild(document.createElement("BR"));
    
    var subject = createFormInput('text', 'subject');
    /*
    Use pixel width for size of input field and textarea (this popups are always the same size).
    This stops IE6 from resizing the textarea when some text is typed in to the field. 
    */
    subject.style.width = "373px";
    subject.className = "input";
    if (is_edit) {
        subject.value = message['subject'];
    } else if (reply_to) {
        if (is_reply) v = message['subject'];
        else v = document.getElementById("subject_"+reply_to).innerHTML;
        if (v.indexOf('RE:') == -1) {
            v = "RE: " + v;
        }
        subject.value = v;
    }
    form.appendChild(subject);

    form.appendChild(document.createElement("BR"));
    form.appendChild(document.createElement("BR"));

    var textarea = document.createElement("TEXTAREA");
    textarea.name = "message_body";
    textarea.style.width = "373px";
    textarea.rows = 10;
    textarea.className="input";
    
    if (is_edit) {
        textarea.value = message['body'];
    }
    
    form.appendChild(textarea);

    if (reply_to) {
        form.appendChild(createFormInput('hidden', 'reply_to', reply_to));
    }

    if (is_edit) {
        form.appendChild(createFormInput('hidden', '_submit_check', 'update_message'));
        form.appendChild(createFormInput('hidden', 'message_id', message_id));
    } else {
        form.appendChild(createFormInput('hidden', '_submit_check', 'post_message'));
    }

    form.appendChild(createFormInput('hidden', 'board', mb));
    form.appendChild(createFormInput('hidden', 'board_id', mb_id));

    div.appendChild(form);

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    var link = createButton('btn_post');

    link.onclick = function() {
        if (on_submit) on_submit();

        form.submit();
        return false;
    };

    buttons.appendChild(link);
    buttons.appendChild(createCancelButton(popup));
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();

    if (subject.value == "") subject.focus();
    else textarea.focus();
}

function addTags(user_id, current_tags, limit) {
    var popup = new Popup(400, 'Add Tags');

    var tag_selector = new TagSelector('event_tags', limit, 'event', 'general');
    var div = E("DIV");
    div.id = 'add_tags_popup';

    var form = E("FORM");
    form.method = "POST";
    form.id = 'add_tags_form';
    form.appendChild(tag_selector.getElement());

    for(var i=0; i<current_tags.length; i++) {
        tag_selector.select(current_tags[i].toJSON(), current_tags[i][0], 
                            current_tags[i][1] == user_id);
    }
    var submit_check = createFormInput('hidden','_submit_check','tag_event');
    form.appendChild(submit_check);

    var tip = E("P");
    tip.style.margin = '0 0 10px 0';
    tip.style.fontSize = '11px';
    tip.innerHTML = "Adding tags (e.g. salsa, free, downtown) makes it easier for people to find this event.  Separate tags by commas.";

    var add_btn = createButton('btn_add');
    add_btn.onclick = function() {
        form.submit();
        return false;
    };

    div.appendChild(form);
    div.appendChild(tip);
    div.appendChild(add_btn);

    popup.setContent(div);
    popup.show();
    tag_selector.input.focus();
}

function sendInviteReminders(invite_id) {
    ajax_popup('ajax_send_invite_reminders.php?invite_id='+invite_id, 285, 'Send Reminders');
}

function sendToFriends(logged_in, message_body, url, invite_id_filter, event_id_filter, name, email, subject, limit, can_blast) {
    if (typeof(can_blast) == 'undefined')
        can_blast = false;
    if (typeof(limit) == 'undefined')
        limit = 450;
    message_popup('send_to_friends', logged_in, 'Send to Friends - Let others know!', 
                  message_body, url, invite_id_filter, event_id_filter,name, email, subject, limit, can_blast);
}

function inviteFriends(message_body, url) {
    message_popup('invite_friends', true, 'Invite Friends - Let others know!', message_body, url);
}

function sendEmailBlast(invite_id) {
    var popup = ajax_popup('ajax_send_email_blast.php?invite_id='+invite_id, 350, 'Send Email Blast');
    var div = childWithID(popup.contentDiv, 'invite_organizer_popup');
    new AddressListSelection(div, childWithID(div, 'add_list_words'), childWithID(div, 'lists'), childWithID(div, 'before_optional_message'));
}

function addFriendsSelector(friendsContainer, friends, send_btn) {
    // Select Friends...
    var leftFriendsContainer = document.createElement("DIV");
    leftFriendsContainer.className = "friendChooser left";
    {
        var table = E('TABLE');
        table.cellSpacing = "0";
        table.cellPadding = "0";
        table.className = 'popuptabs';
        var tbody = E('TBODY');
        var row = E('TR');
        var cell = E('TD');
        cell.id = 'friends_tab_0';
        cell.className = 'selected';
        var link = E('A');
        link.href = '#';
        link.innerHTML = 'My Friends';
        link.onclick = function() {
            toggle_tab('friends', 0);
            return false;
        };
        cell.appendChild(link);
        row.appendChild(cell);

        var cell = E('TD');
        cell.className = 'spacer';
        cell.innerHTML = '&nbsp;';
        row.appendChild(cell);

        var cell = E('TD');
        cell.id = 'friends_tab_1';
        var link = E('A');
        link.href = '#';
        link.innerHTML = 'My Contacts';
        link.onclick = function() {
            toggle_tab('friends', 1);
            return false;
        };
        cell.appendChild(link);
        row.appendChild(cell);
        tbody.appendChild(row);
        table.appendChild(tbody);

        leftFriendsContainer.appendChild(table);

        var optionsSelect = document.createElement("SELECT");
        optionsSelect.id = 'friends_content_0';
        optionsSelect.multiple = true;
        optionsSelect.style.borderTop = '0px';
        
        var friends_list = {};
        for (i=0; i<friends.length; i++) {
            if (friends[i][2] == 'friend') {
                var option = new Option();
                option.value = friends[i][0];
                option.text = friends[i][1];
                option.title = friends[i][1];
                friends_list[friends[i][0]] = true;
                add_option(optionsSelect, option);
            }
        }
        
        leftFriendsContainer.appendChild(optionsSelect);

        var optionsSelectContacts = document.createElement("SELECT");
        optionsSelectContacts.id = 'friends_content_1';
        optionsSelectContacts.multiple = true;
        optionsSelectContacts.style.display = 'none';
        optionsSelectContacts.style.borderTop = '0px';
        
        for (i=0; i<friends.length; i++) {
            if (friends[i][2] == 'contact') {
                var option = new Option();
                option.value = friends[i][0];
                option.text = friends[i][1];
                option.title = friends[i][1];
                add_option(optionsSelectContacts, option);
            }
        }
        
        leftFriendsContainer.appendChild(optionsSelectContacts);
    }
        
    // Add / Remove
    var midFriendsContainer = document.createElement("DIV");
    midFriendsContainer.className = "friendButtons";
    {
        var addButton = document.createElement("INPUT");
        addButton.type = "submit";
        addButton.value = "Add "+String.fromCharCode(187);
        addButton.disabled = true;

        midFriendsContainer.appendChild(addButton);
        
        midFriendsContainer.appendChild(document.createElement("BR"));
        midFriendsContainer.appendChild(document.createElement("BR"));
            
        var removeButton = document.createElement("INPUT");
        removeButton.type = "submit";
        removeButton.value = String.fromCharCode(171)+" Remove";
        removeButton.disabled = true;
            
        midFriendsContainer.appendChild(removeButton);
    }
        
    // Chosen
    var rightFriendsContainer = document.createElement("DIV");
    rightFriendsContainer.className = "friendChooser right";
    {
        var label = document.createElement("LABEL");
        label.innerHTML = "<strong>Selected</strong>";
        rightFriendsContainer.appendChild(label);
        rightFriendsContainer.appendChild(document.createElement("BR"));

        var select = document.createElement("SELECT");
        select.name = "send_to_friends[]";
        select.multiple = true;
        rightFriendsContainer.appendChild(select);
    }
        
    // Logic
    optionsSelect.onchange = function () {
        addButton.disabled = this.selectedIndex == -1;
    }
    optionsSelectContacts.onchange = function () {
        addButton.disabled = this.selectedIndex == -1;
    }
    select.onchange = function () {
        removeButton.disabled = this.selectedIndex == -1;
    }
        
    var moveOptions = function(button, a, b) {
        for (var i = 0; i < a.options.length; i++) {
            var option = a.options[i]
            if (option.selected) {
                var optObj = new Option();
                optObj.text = option.text;
                optObj.value = option.value;
                optObj.title = option.title;
                add_option(b, optObj);
                a.remove(i);
                i--;
            }
        }
        sortSelect(b);
        button.disabled = true;
        return false;
    }

    var moveOptionsBack = function(button, source, friends, contacts) {
        for (var i = 0; i < source.options.length; i++) {
            var option = source.options[i]
            if (option.selected) {
                var optObj = new Option();
                optObj.text = option.text;
                optObj.value = option.value;
                optObj.title = option.title;
                if (friends_list[option.value]) {
                    add_option(friends, optObj);
                } else {
                    add_option(contacts, optObj);
                }
                source.remove(i);
                i--;
            }
        }
        sortSelect(friends);
        sortSelect(contacts);
        button.disabled = true;
        return false;
    }
        
    addButton.onclick = function () {
        return moveOptions(this, optionsSelect.style.display == 'none' ? optionsSelectContacts : optionsSelect, select);
    }
        
    removeButton.onclick = function () {
        return moveOptionsBack(this, select, optionsSelect, optionsSelectContacts);
    }
        
    friendsContainer.appendChild(leftFriendsContainer);
    friendsContainer.appendChild(midFriendsContainer);
    friendsContainer.appendChild(rightFriendsContainer);
        
    var clearDiv = document.createElement("DIV");
    clearDiv.style.clear = "both";
    friendsContainer.appendChild(clearDiv);
    
    // attach to send_btn
    send_btn.old_onclick = send_btn.onclick;
    send_btn.onclick = function() {
        for (var i = 0; i < select.options.length; i++) {
            select.options[i].selected = true;
        }

        return send_btn.old_onclick();
    };
}

function sendToPhone(entity_type, entity_id, logged_in, cellphone, carrier) {
    var popup = new Popup(450, "Send to Phone");

    var div = document.createElement('DIV');

    var form = document.createElement("FORM");
    form.method = "POST";
    form.id = createUID("send_to_phone");

    var label = document.createElement("SPAN");
    label.innerHTML = "We do not have your phone information.  Please tell us where to send the " + entity_type + " information.";
    form.appendChild(label);

    form.appendChild(document.createElement("BR"));
    form.appendChild(document.createElement("BR"));

    var label = document.createElement("b");
    label.innerHTML = "Cell Phone&nbsp;&nbsp;";
    form.appendChild(label);

    var input = createFormInput('text', 'cellphone');
    input.size=10;
    input.maxLength=10;
    input.className = "input";
    input.value = cellphone;
    form.appendChild(input);

    var spacer = document.createElement('SPAN');
    spacer.innerHTML = "&nbsp;";
    form.appendChild(spacer);

    var select = document.createElement("SELECT");
    select.name="carrier";
    add_option(select, createFormOption('Select a carrier', ''));
    add_option(select, createFormOption('ALLTEL', 'ALLTEL', carrier));
    add_option(select, createFormOption('AT&T Wireless', 'AT&T Wireless', carrier));
    add_option(select, createFormOption('Cingular', 'Cingular', carrier));
    add_option(select, createFormOption('Nextel', 'Nextel', carrier));
    add_option(select, createFormOption('Sprint', 'Sprint', carrier));
    add_option(select, createFormOption('T-Mobile', 'T-Mobile', carrier));
    add_option(select, createFormOption('Verizon', 'Verizon', carrier));

    form.appendChild(select);

    var spacer = document.createElement('SPAN');
    spacer.innerHTML = "&nbsp;&nbsp;";
    form.appendChild(spacer);

    var label = document.createElement("i");
    label.innerHTML = "e.g. 6171231234";
    form.appendChild(label);

    form.appendChild(document.createElement("BR"));

    if (logged_in) {
        var checkbox = createFormInput('checkbox', 'update_profile');
        checkbox.value = 'true';
        checkbox.checked = true;
        form.appendChild(checkbox);
        var label = document.createElement('LABEL');
        label.innerHTML = "Save in Profile? (Only you can see it.)";
        form.appendChild(label);
    }

    form.appendChild(document.createElement("BR"));
    form.appendChild(document.createElement("BR"));
    
    var error = document.createElement("i");
    error.id = form.id + '_error';
    error.style.color="red";
    form.appendChild(error);

    form.appendChild(createFormInput('hidden', '_submit_check', 'send_to_phone'));
    form.appendChild(createFormInput('hidden', 'entity_type', entity_type));
    form.appendChild(createFormInput('hidden', 'entity_id', entity_id));

    form.onsubmit = function() {
        var error = document.getElementById(this.id + '_error');

        result = validateSendToPhone(this);
        if (result != null) {
            error.innerHTML = result;
            return false;
        } else {
            return true;
        }
    }


    if (cellphone != undefined && cellphone != '' && 
        carrier != undefined && carrier != '') {
        document.body.appendChild(form);

        result = validateSendToPhone(form);
        if (result != null) {
            error.innerHTML = result;
            document.body.removeChild(form);
        } else {
            // things are ok to submit
            form.style.display = "none";
            form.submit();
            return;
        }
    }

    div.appendChild(form);

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    var link = createButton('btn_send');

    link.onclick = function() {
        form.submit();
        return false;
    };

    buttons.appendChild(link);
    buttons.appendChild(createCancelButton(popup));
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();
}

function createEditList(list_id) {
    var list = null;
    if (list_id == undefined) title = 'Create List';
    else {
        title = 'Edit List';
        var result = loadJSON('ajax_manage_user_lists.php?action=fetch_list&list_id=' + list_id);
        if (result['data']) {
            list = result['data'];
        }
    }

    var popup = new Popup(400, title);

    var div = document.createElement('DIV');
    div.className = 'create_list';

    var form = document.createElement("FORM");
    form.method = "POST";
    if (list == null) form.action = "lists.php";
    form.id = createUID("create_list");

    form.onsubmit = function() {
        return true;
    }

    var table = document.createElement('TABLE');
    table.cellspacing="0";
    table.cellpadding="0";
    table.width="100%";

    var tbody = document.createElement('TBODY');

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.colSpan = "2";
    cell.innerHTML = "<strong>Name</strong>";
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    cell = document.createElement('TD');
    cell.colSpan = "2";
    var name = createFormInput('text', 'name');
    name.style.width = "100%";
    if (list && list['name']) name.value = list['name'];
    cell.appendChild(name);
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.colSpan = "2";
    cell.innerHTML = "<strong>Public URL</strong>";
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    cell = document.createElement('TD');
    cell.colSpan = "2";
    var name = createFormInput('text', 'url_name');
    name.style.width = "100%";
    if (list && list['url_name']) name.value = list['url_name'];
    cell.appendChild(name);
    row.appendChild(cell);

    tbody.appendChild(row);

    row = document.createElement('TR');
    cell = document.createElement('TD');
    cell.className = "header";
    cell.colSpan = "2";
    cell.innerHTML = "<strong>Description</strong>";
    row.appendChild(cell);

    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    cell = document.createElement('TD');
    cell.colSpan = "2";
    var textarea = document.createElement("TEXTAREA");
    textarea.name = "description";
    textarea.style.width = "100%";
    textarea.rows = 7;
    textarea.className="input";
    if (list && list['description']) textarea.value = list['description'];

    cell.appendChild(textarea);
    row.appendChild(cell);

    tbody.appendChild(row);
    
    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Category</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="category_id";
    add_option(select, createFormOption('<Select a Category>', ''));

    categoryValue = null;
    if (list && list['category_id']) categoryValue = list['category_id'];

    var result = loadJSON("ajax_manage_user_lists.php?action=fetch_categories");
    data = result['data'];
    for (i=0; i<data.length; i++) {
        add_option(select, createFormOption(data[i]['text'], data[i]['value'], categoryValue));
    }

    cell.appendChild(select);
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>City</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="metro";
    add_option(select, createFormOption('All', ''));

    metroValue = defaultMetro;
    if (list && list['metro']) metroValue = list['metro'];

    var metros = loadJSON("ajax_metro_list.php");
    for (i=0; i<metros.length; i++) {
        add_option(select, createFormOption(metros[i][1], metros[i][0], metroValue));
    }

    cell.appendChild(select);
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Ordering</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="ordering";

    orderingValue = 'fixed';
    if (list && list['ordering']) orderingValue = list['ordering'];

    add_option(select, createFormOption('Creator Determined', 'fixed', orderingValue));
    add_option(select, createFormOption('Commuity Voted', 'rating', orderingValue));
    cell.appendChild(select);
    row.appendChild(cell);

    // NOTE: hide for now
    row.style.display = 'none';
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Share with</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="viewable_by";

    viewableByValue = 'everyone';
    if (list && list['viewable_by']) viewableByValue = list['viewable_by'];

    add_option(select, createFormOption('Everyone', 'everyone', viewableByValue));
    add_option(select, createFormOption('Friends', 'friends', viewableByValue));
    add_option(select, createFormOption('Friends Friends', 'friends2', viewableByValue));
    cell.appendChild(select);
    row.appendChild(cell);

    // NOTE: hide for now
    row.style.display = 'none';
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>&nbsp;&nbsp;&nbsp; and group</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="group_viewable_by";
    add_option(select, createFormOption('<Select a Group>', ''));

    groupViewableByValue = null;
    if (list && list['group_viewable_by']) groupViewableByValue = list['group_viewable_by'];

    var data = loadJSON("ajax_group_list.php");
    for (i=0; i<data.length; i++) {
        add_option(select, createFormOption(data[i][0], data[i][1], groupViewableByValue));
    }
    cell.appendChild(select);
    row.appendChild(cell);

    // NOTE: hide for now
    row.style.display = 'none';
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Can others add items?</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');

    editableByUserValue = 0;
    if (list && list['editable_by_user']) editableByUserValue = list['editable_by_user'];

    var radio = createFormInput('radio', 'editable_by_user', 'yes');
    if (editableByUserValue == 1) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "Yes&nbsp;&nbsp;";
    cell.appendChild(label);

    var radio = createFormInput('radio', 'editable_by_user', 'no');
    if (editableByUserValue == 0) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "No";
    cell.appendChild(label);
    row.appendChild(cell);

    // NOTE: hide for now
    row.style.display = 'none';
    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Publish for others to see?</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');

    published = 1;
    if (list && list['published']) published = list['published'];

    var radio = createFormInput('radio', 'published', 'yes');
    if (published == 1) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "Yes&nbsp;&nbsp;";
    cell.appendChild(label);

    var radio = createFormInput('radio', 'published', 'no');
    if (published == 0) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "No";
    cell.appendChild(label);
    row.appendChild(cell);

    // NOTE: hide for now
    row.style.display = 'none';
    tbody.appendChild(row);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Show index for items?</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');

    showListNumbers = 0;
    if (list && list['show_list_numbers']) showListNumbers = list['show_list_numbers'];

    var radio = createFormInput('radio', 'show_list_numbers', 'yes');
    if (showListNumbers == 1) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "Yes&nbsp;&nbsp;";
    cell.appendChild(label);

    var radio = createFormInput('radio', 'show_list_numbers', 'no');
    if (showListNumbers == 0) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "No";
    cell.appendChild(label);
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Show default header?</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');

    showDefaultHeader = 0;
    if (list && list['show_default_header']) showDefaultHeader = list['show_default_header'];

    var radio = createFormInput('radio', 'show_default_header', 'yes');
    if (showDefaultHeader == 1) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "Yes&nbsp;&nbsp;";
    cell.appendChild(label);

    var radio = createFormInput('radio', 'show_default_header', 'no');
    if (showDefaultHeader == 0) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "No";
    cell.appendChild(label);
    row.appendChild(cell);

    tbody.appendChild(row);

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Show interested?</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');

    showInterested = 0;
    if (list && list['show_interested']) showInterested = list['show_interested'];

    var radio = createFormInput('radio', 'show_interested', 'yes');
    if (showInterested == 1) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "Yes&nbsp;&nbsp;";
    cell.appendChild(label);

    var radio = createFormInput('radio', 'show_interested', 'no');
    if (showInterested == 0) {radio.defaultChecked = true; radio.checked = true; }
    cell.appendChild(radio);
    var label = document.createElement('LABEL');
    label.innerHTML = "No";
    cell.appendChild(label);
    row.appendChild(cell);

    tbody.appendChild(row);

    table.appendChild(tbody);
    form.appendChild(table);

    if (list == null) {
        form.appendChild(createFormInput('hidden', '_submit_check', 'create_list'));
    } else {
        form.appendChild(createFormInput('hidden', '_submit_check', 'update_list'));
        form.appendChild(createFormInput('hidden', 'list_id', list_id));
    }

    div.appendChild(form);

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    var link = createButton('btn_send');

    link.onclick = function() {
        if (form.onsubmit()) form.submit();
        return false;
    };

    buttons.appendChild(link);
    buttons.appendChild(createCancelButton(popup));
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();
}


function addItemToList(entity_type, entity_id) {
    var popup = new Popup(400, 'Add Item To List');

    var div = document.createElement('DIV');
    div.className = 'create_list';

    var form = document.createElement("FORM");
    form.method = "POST";
    form.id = createUID("create_list");

    var table = document.createElement('TABLE');
    table.cellspacing="0";
    table.cellpadding="0";
    table.width="100%";

    var tbody = document.createElement('TBODY');

    var row = document.createElement('TR');
    var cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>List</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var select = document.createElement("SELECT");
    select.name="list_id";
    select.style.width="100%";
    add_option(select, createFormOption('<Select a List>', ''));

    var data = loadJSON("ajax_manage_user_lists.php?action=listing")['data'];
    for (i=0; i<data.length; i++) {
        var option = document.createElement("OPTION");
        option.text = data[i]['text'];
        option.value = data[i]['value'];
        option.title = data[i]['text'];
        add_option(select, option);
    }
    cell.appendChild(select);
    row.appendChild(cell);

    tbody.appendChild(row);

    row = document.createElement('TR');
    cell = document.createElement('TD');
    cell.className = "header";
    cell.innerHTML = "<strong>Description</strong>";
    row.appendChild(cell);

    cell = document.createElement('TD');
    var textarea = document.createElement("TEXTAREA");
    textarea.name = "description";
    textarea.style.width = "100%";
    textarea.rows = 7;
    textarea.className="input";

    cell.appendChild(textarea);
    row.appendChild(cell);

    tbody.appendChild(row);
    table.appendChild(tbody);
    form.appendChild(table);
    form.appendChild(createFormInput('hidden', 'entity_type', entity_type));
    form.appendChild(createFormInput('hidden', 'entity_id', entity_id));

    div.appendChild(form);

    div.appendChild(createCancelButton(popup));

    var span = document.createElement("SPAN");
    span.className="sendBtn";
    span.innerHTML = "<a href=\"#\" onclick=\"var form = document.getElementById('" + form.id + "'); handleResponse(loadJSON('ajax_manage_user_lists.php?action=add_item', null, getFormParameters(form))); var el = document.getElementById('" + popup.getId() + "'); if (el.ifrm) {el.ifrm.style.visibility='hidden';} el.style.display = 'none'; document.body.removeChild(el); return false;\"><em>Send</em></a>";
    div.appendChild(span);

    d = document.createElement("DIV");
    d.className = "clear";
    div.appendChild(d);

    popup.setContent(div);
    popup.show();
}

function createEditorsPick(event_id, 
                           event_title,
                           logged_in_user_id, logged_in_user_first_name, 
                           poster_id, poster_first_name
                           ) {
    var popup = new Popup(390, "Make this event an Editors' Pick");

    var div = document.createElement("DIV");

    var form = document.createElement("FORM");
    form.method = "POST";
    form.id = createUID("post_editors_pick");

    var label = document.createElement("LABEL");
    label.innerHTML = "Title";
    form.appendChild(label);

    form.appendChild(document.createElement("BR"));
    
    var title = createFormInput('text', 'title');
    /*
    Use pixel width for size of input field and textarea (this popups are always the same size).
    This stops IE6 from resizing the textarea when some text is typed in to the field. 
    */
    title.style.width = "373px";
    title.className = "input";
    title.value = event_title;
    form.appendChild(title);

    form.appendChild(document.createElement("BR"));
    form.appendChild(document.createElement("BR"));

    var label = document.createElement("LABEL");
    label.innerHTML = "Picker:&nbsp;&nbsp;";
    form.appendChild(label);
    
    var select = document.createElement("SELECT");
    select.name = "picker_id";

    var optObj = document.createElement('option');
    optObj.text = logged_in_user_first_name;
    optObj.value = logged_in_user_id;
    add_option(select, optObj);

    var optObj = document.createElement('option');
    optObj.text = poster_first_name;
    optObj.value = poster_id;
    add_option(select, optObj);

    form.appendChild(select);
    
    form.appendChild(document.createElement("BR"));
    form.appendChild(document.createElement("BR"));

    var textarea = document.createElement("TEXTAREA");
    textarea.name = "editors_take";
    textarea.style.width = "373px";
    textarea.rows = 10;
    textarea.className="input";
    
    form.appendChild(textarea);

    form.appendChild(createFormInput('hidden', '_submit_check', 'create_editors_pick'));
    form.appendChild(createFormInput('hidden', 'event_id', event_id));

    div.appendChild(form);

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    var link = createButton('btn_post');
    link.onclick = function() {
        form.submit();
        return false;
    };

    buttons.appendChild(link);
    buttons.appendChild(createCancelButton(popup));
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();

    if (title.value == "") title.focus();
    else textarea.focus();
}

function unsubscribeEmails(form_name,user_id){
    var popup = new Popup(415, "Unsubscribe emails from your invites");
    
    var div = document.createElement("DIV");
    
    var form = document.createElement("FORM");
    form.method = "POST";
    form.id = createUID(form_name);
    form.name = form_name
    
    form.appendChild(createFormInput('hidden', '_submit_check', form_name));
    form.appendChild(createFormInput('hidden', 'user_id', user_id));
    
    var desc = document.createElement('P');
    desc.innerHTML = 'Need to unsubscribe people from your invites? Just enter their email below,'
        + ' separated by commas or spaces, and these users will be blocked from receiving'
        + ' your invites.';
    form.appendChild(desc);
    
    var label = document.createElement('SPAN');
    label.innerHTML = 'Enter emails separated by commas or spaces:';
    form.appendChild(label);
    
    var textarea = document.createElement("TEXTAREA");
    textarea.name = "emails";
    textarea.id = "emails";
    textarea.style.width = "400px";
    textarea.rows = 4;
    form.appendChild(textarea);
    form.appendChild(document.createElement("BR"));
    div.appendChild(form);
    
    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    
    var save_btn = createButton('btn_unsubscribe');
    save_btn.onclick = function () {
        if(!confirm('Are you sure you want to unsubscribe these users?\n'
            +' They will no longer receive any invites from you.')){
            popup.close();
            return false;
        }
        var span = document.createElement('SPAN');
        span.innerHTML = '<b>processing...</b>&nbsp;&nbsp;';
        this.parentNode.replaceChild(span, this);
        form.submit();
        return false;
    }
    
    buttons.appendChild(save_btn);
    buttons.appendChild(createCancelButton(popup));
    div.appendChild(buttons);
    
    popup.setContent(div);
    popup.show();
}

var p_callback = null;
function registerPlaxoCallback(func) {
    p_callback = func;
}

function getPlaxoCallback() {
    return p_callback;
}

function onABCommComplete() {
    var func = getPlaxoCallback();
    if (func) {
        func();
        registerPlaxoCallback(null);
    }
}

function parseEmails(input) {
    var results = new Array();
    var email = new RegExp("[\\w\\.\\+\\-=]+@([\\w][\\w\\-]*\\.)+[a-z]{2,6}\\b", 'i');

    index = 0;
    while (true) {
        var ar = email.exec(input.substring(index));
        if (!ar) break;
        results.push(ar[0]);
        index += ar.index + ar[0].length;
    }
    return results;
}
