var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
var root;
var tag_select;
var tag_list;
var message_empty_list;

function empty(id) { 
    if (document.getElementById(id) && !document.getElementById('review')) {
        document.getElementById(id).value = '';
        
        document.getElementById(id).removeAttribute('onclick');
    }
}

//pass min and max -measured against window width
function P7_MinMaxW(a,b){
    var nw="auto",w=document.documentElement.clientWidth;

    if(w>=b){nw=b+"px";}if(w<=a){nw=a+"px";}return nw;
}

function submitDataBlock(action) {
    if (document.getElementById(action + '-button')) { 
        var actionButton = document.getElementById(action + '-button');
    
        form = actionButton.parentNode;

        while (form.tagName != 'FORM') {  
           form = form.parentNode;
        }
    }

    if (document.getElementById(action + '-submit')) {
        var actionSubmitInput = document.getElementById(action + '-submit');

        actionSubmitInput.parentNode.removeChild(actionSubmitInput); 
        form.submit();
        actionButton.appendChild(actionSubmitInput);
    }
}

function saveEducation(position,action) { 
    var bottom_update_publication_button = document.getElementById(action + '-education-bottom-button');
    var bottom_update_publication_submit = document.getElementById(action + '-education-bottom-submit');
    var top_update_publication_button = document.getElementById(action + '-education-bottom-button');
    var top_update_publication_submit = document.getElementById(action + '-education-top-submit');
    
    if (position == 'top')
        var update_publication_button = document.getElementById(action + '-education-bottom-button');
    else
        var update_publication_button = document.getElementById(action + '-education-top-button'); 

    form = update_publication_button;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    }

    bottom_update_publication_submit.parentNode.removeChild(bottom_update_publication_submit); 
    top_update_publication_submit.parentNode.removeChild(top_update_publication_submit);

    form.submit();

    bottom_update_publication_button.appendChild(bottom_update_publication_submit);
    top_update_publication_button.appendChild(top_update_publication_submit);  
}

function browse() {
    var fileName = document.getElementById('file').value;
}

function login() {
    submitDataBlock('login');
}

function logout() { 
    submitDataBlock('logout');
}

function next() { 
    submitDataBlock('next');
}

function quickSearch(buttonPosition) { 
    var bottomSearchButton = document.getElementById('bottom-quick-search-button');
    var bottomSearchSubmitInput = document.getElementById('bottom-quick-search-submit');
    
    if (buttonPosition != 'bottom') 
        var searchButton = document.getElementById('bottom-quick-search-button');    
    
    form = searchButton.parentNode;

    while (form.tagName != 'FORM')
       form = form.parentNode;
    
    bottomSearchSubmitInput.parentNode.removeChild(bottomSearchSubmitInput);
    
    form.submit();

    bottomSearchButton.appendChild(bottomSearchSubmitInput);
}

function reply(button_position) { 
    var button_bottom_reply = document.getElementById('bottom-reply-button');
    var button_top_reply = document.getElementById('top-reply-button');

    var input_bottom_reply = document.getElementById('bottom-reply');
    var input_top_reply = document.getElementById('top-reply');
    
    if (button_position == 'top')
        var button_reply = document.getElementById('bottom-reply-button');    
    else
        var button_reply = document.getElementById('top-reply-button');    
        
    form = button_reply.parentNode;

    while (form.tagName != 'FORM')
       form = form.parentNode;
    
    input_bottom_reply.parentNode.removeChild(input_bottom_reply); 
    input_top_reply.parentNode.removeChild(input_top_reply);
    
    form.submit();

    button_bottom_reply.appendChild(input_bottom_reply);
    button_top_reply.appendChild(input_top_reply);  
}

function sendMessage(button_position) { 
    var button_bottom_send = document.getElementById('bottom-send-button');
    var input_bottom_send = document.getElementById('bottom-send');
    
    if (button_position == 'top')
        var button_send_message = document.getElementById('bottom-send-button');
    else
        var button_send_message = document.getElementById('bottom-send-button'); 

    form = button_send_message.parentNode;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    }
    
    select = document.createElement('select');
    option = document.createElement('option');
    option.setAttribute('selected','selected');
    select.setAttribute('multiple','multiple');
    select.setAttribute('name','recipients[]');
    select.setAttribute('style',null);
    select.style.display = 'none';
    
    if (document.getElementById('list-box')) {
        list_box = document.getElementById('list-box');
 
        while(list_box.firstChild) {
            selected_option = option.cloneNode(option);            
            selected_option.setAttribute('value',list_box.firstChild.getAttribute('id'));
            select.appendChild(selected_option);                
            list_box.removeChild(list_box.firstChild);
        }
 
        form.appendChild(select);
    }

    input_bottom_send.parentNode.removeChild(input_bottom_send); 
    form.submit();

    button_bottom_send.appendChild(input_bottom_send);
}

function savePublication(buttonPosition) { 
    var bottomSavePublicationButton = document.getElementById('bottom-save-publication-button');
    var bottomSavePublicationSubmitInput = document.getElementById('bottom-save-publication-submit');
    var topSavePublicationButton = document.getElementById('top-save-publication-button');
    var topSavePublicationSubmitInput = document.getElementById('top-save-publication-submit');
    
    if (buttonPosition == 'top')
        var savePublicationButton = document.getElementById('bottom-save-publication-button');
    else
        var savePublicationButton = document.getElementById('top-save-publication-button'); 

    form = savePublicationButton.parentNode;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    }

    bottomSavePublicationSubmitInput.parentNode.removeChild(bottomSavePublicationSubmitInput); 
    topSavePublicationSubmitInput.parentNode.removeChild(topSavePublicationSubmitInput);

    if (!document.getElementsByTagName('document_id')[0] || !document.getElementsByTagName('document_id')[0].getAttribute('value')) {
        tag_index = 13;
        
        if (document.getElementById('tag-field')) {
            tag_field = document.getElementById('tag-field');
        
            while (document.getElementById('list_element_' + tag_index) || tag_index < 43) {
                if (document.getElementById('list_element_' + tag_index)) {
                var input = document.createElement('input'); 
                input.setAttribute('name',tag_index);
                input.setAttribute('value','on');
                input.setAttribute('type','hidden');
                tag_field.appendChild(input,null);
                }
                
                tag_index = tag_index + 1; 
            }
        }
    }

    form.submit();

    bottomSavePublicationButton.appendChild(bottomSavePublicationSubmitInput);
    topSavePublicationButton.appendChild(topSavePublicationSubmitInput);    
}

function signUp() {
    submitDataBlock('sign-up');
}

function search(buttonPosition) { 
    var bottomSearchButton = document.getElementById('bottom-search-button');
    var bottomSearchSubmitInput = document.getElementById('bottom-search-submit');

    if (document.getElementById('top-search-button')) {
        var topSearchButton = document.getElementById('top-search-button');
        var topSearchSubmitInput = document.getElementById('top-search-submit');
    }
    
    if (buttonPosition == 'top' || buttonPosition != 'bottom') 
        var searchButton = document.getElementById('bottom-search-button');    
    else
        var searchButton = document.getElementById('top-search-button'); 
    
    form = searchButton.parentNode;

    while (form.tagName != 'FORM')
       form = form.parentNode;
    
    bottomSearchSubmitInput.parentNode.removeChild(bottomSearchSubmitInput);
    
    if (topSearchSubmitInput) 
        topSearchSubmitInput.parentNode.removeChild(topSearchSubmitInput);
    
    form.submit();

    bottomSearchButton.appendChild(bottomSearchSubmitInput);    
    
    if (topSearchButton)
        topSearchButton.appendChild(topSearchSubmitInput);
}

function sendFeedback() {
    var signUpButton = document.getElementById("send-feedback-button");
    var submitInput = document.getElementById('send-feedback-input');

    submitInput.parentNode.removeChild(submitInput);

    var form = signUpButton.parentNode;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    } 

    form.submit();
    signUpButton.appendChild(submitInput);
}

function upload() { 
    submitDataBlock('upload');
}

function register() {
    submitDataBlock('register');
}

function submitPayment() {
    var submitPaymentButton = document.getElementById("submit-payment-button");
    var submitPaymentInput = document.getElementById('submit-payment');

    document.getElementById('credit_card').removeAttribute('disabled');
    document.getElementById('wire_transfer').removeAttribute('disabled');
    document.getElementById('country_code').removeAttribute('disabled');
    document.getElementById('prefix_name').removeAttribute('disabled');
    document.getElementById('mastercard').removeAttribute('disabled');
    document.getElementById('visa').removeAttribute('disabled');
    
    submitPaymentInput.parentNode.removeChild(submitPaymentInput);

    var form = submitPaymentButton.parentNode;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    } 
    
    insertElements();

    form.submit();
    submitPaymentButton.appendChild(submitPaymentInput);
}

function updateProfile(buttonPosition) {
    var bottomSubmitButton = document.getElementById("bottom-submit-button");
    var bottomSubmitInput = document.getElementById('bottom-update');

    if (buttonPosition == 'top')
        var submitButton = document.getElementById("bottom-submit-button");
    else 
        var submitButton = document.getElementById("top-submit-button");

    var topSubmitButton = document.getElementById("top-submit-button");
    var topSubmitInput = document.getElementById('top-update');
    
    bottomSubmitInput.parentNode.removeChild(bottomSubmitInput);
    topSubmitInput.parentNode.removeChild(topSubmitInput);
    
    var form = submitButton.parentNode;

    while (form.tagName != 'FORM') {  
       form = form.parentNode;
    } 

    form.submit();
    
    bottomSubmitButton.appendChild(bottomSubmitInput);
    topSubmitButton.appendChild(topSubmitInput);
}

function htmlElement(tag_name,id,classes,style,inner_html,name,type,for_value) {
    this.tag_name = tag_name;
    this.id = id;
    this.classes = classes;
    this.style = style;
    this.element = document.createElement(tag_name);

    if (inner_html)
        this.element.innerHTML = inner_html;
    
    if (name)
        this.element.setAttribute('name',name)

    if (type)
        this.element.setAttribute('type',type);
        
    if (for_value)
        this.element.setAttribute('for',for_value);

    if (navigator.appName == 'Netscape')
        class_name = 'class';
    else
        class_name = 'className';
    

    this.element.setAttribute('id',id);
    
    value_class = '';
    value_style = '';
    
    for (class_index in classes)
        value_class = value_class + " " + classes[class_index];
    
    for (style_index in style)
        value_style = value_style + " " + style[style_index] + ';';
    
    this.element.setAttribute(class_name,value_class);
    this.element.setAttribute('style',value_style);
}

function cssOverwrite() {

    var buttonElementIdIndex = 0;
    var inputElementIdIndex = 0;
    var action_img_tag = "";
    var action_index = 0;

    var themeDirectory = "uploads/images/blue/"; 

    var buttonBackgroundStyles = new Array();
    var buttonWidthStyles = new Array();

    var root;
     
    var submitInputElementIds = new Array();
    var submitInputElements = new Array();
    var submitButtonElementIds = new Array();
    var submitButtonElements = new Array();
    var actions = new Array();

    $button_style_index = 0

    while ($button_style_index < 23) {
        buttonWidthStyles[$button_style_index] = "";
        
        if ($button_style_index == 9 || $button_style_index == 12)
            buttonWidthStyles[$button_style_index] = "124px";
            
        $button_style_index = $button_style_index + 1;
    }
    
    buttonBackgroundStyles[0] = "button-add-this-education.png";
    buttonBackgroundStyles[1] = "button-save-publication.png";
    buttonBackgroundStyles[2] = "button-search.png";
    buttonBackgroundStyles[3] = "button-send.png";
    buttonBackgroundStyles[4] = "button-submit.png";
    buttonBackgroundStyles[5] = "button-browse.png";
    buttonBackgroundStyles[6] = "button-login.png";
    buttonBackgroundStyles[7] = "button-logout.png";
    buttonBackgroundStyles[8] = "button-next.png";
    buttonBackgroundStyles[9] = "button-sign-up.png";
    buttonBackgroundStyles[10] = "button-upload.png";
    buttonBackgroundStyles[11] = "button-search.png";
    buttonBackgroundStyles[12] = "button-sign-up.png";
    buttonBackgroundStyles[13] = "button-save-publication.png";
    buttonBackgroundStyles[14] = "button-search.png";
    buttonBackgroundStyles[15] = "button-submit.png";
    buttonBackgroundStyles[16] = "button-update-this-education.png";
    buttonBackgroundStyles[17] = "button-submit.png";
    buttonBackgroundStyles[18] = "button-submit.png";
    buttonBackgroundStyles[19] = "button-quick-search.png";
    buttonBackgroundStyles[20] = "button-add-this-education.png";
    buttonBackgroundStyles[21] = "button-update-this-education.png";
    buttonBackgroundStyles[22] = "button-reply.png";
    buttonBackgroundStyles[23] = "button-reply.png";
    
    submitInputElementIds[0] = "add-education-bottom-submit";
    submitInputElementIds[1] = "bottom-save-publication-submit";
    submitInputElementIds[2] = "bottom-search-submit";
    submitInputElementIds[3] = "bottom-update";
    submitInputElementIds[4] = "login-submit";
    submitInputElementIds[5] = "next-submit";
    submitInputElementIds[6] = "top-search-submit";
    submitInputElementIds[7] = "upload-submit";
    submitInputElementIds[8] = "next-submit";
    submitInputElementIds[9] = "register-submit";
    submitInputElementIds[10] = "logout-submit";
    submitInputElementIds[11] = "top-save-publication-submit";
    submitInputElementIds[12] = "sign-up-submit";
    submitInputElementIds[13] = "top-update";
    submitInputElementIds[14] = "bottom-send";
    submitInputElementIds[16] = "update-education-bottom-submit";
    submitInputElementIds[17] = "submit-payment";
    submitInputElementIds[18] = "send-feedback-input";
    submitInputElementIds[19] = "bottom-quick-search-submit";
    submitInputElementIds[20] = "add-education-top-submit";
    submitInputElementIds[21] = "update-education-top-submit";
    submitInputElementIds[22] = "bottom-reply";
    submitInputElementIds[23] = "top-reply";
    
    submitButtonElementIds[0] = "add-education-bottom-button";
    submitButtonElementIds[1] = "bottom-save-publication-button";
    submitButtonElementIds[2] = "bottom-search-button";
    submitButtonElementIds[3] = "bottom-send-button";
    submitButtonElementIds[4] = "bottom-submit-button";
    submitButtonElementIds[5] = "browse-button";
    submitButtonElementIds[6] = "login-button";
    submitButtonElementIds[7] = "logout-button";
    submitButtonElementIds[8] = "next-button";
    submitButtonElementIds[9] = "register-button";
    submitButtonElementIds[10] = "upload-button";
    submitButtonElementIds[11] = "search-button";
    submitButtonElementIds[12] = "sign-up-button";
    submitButtonElementIds[13] = "top-save-publication-button";
    submitButtonElementIds[14] = "top-search-button";
    submitButtonElementIds[15] = "top-submit-button";
    submitButtonElementIds[16] = "update-education-bottom-button";
    submitButtonElementIds[17] = "submit-payment-button";
    submitButtonElementIds[18] = "send-feedback-button";
    submitButtonElementIds[19] = "bottom-quick-search-button";
    submitButtonElementIds[20] = "add-education-top-button";
    submitButtonElementIds[21] = "update-education-top-button";
    submitButtonElementIds[22] = "bottom-reply-button";
    submitButtonElementIds[23] = "top-reply-button";
    
    actions[0] = "logout";
    actions[1] = "search";
    actions[2] = "sign-up";
    actions[3] = "edit-education";
    actions[4] = "next";
    actions[5] = "sign-in";
    actions[6] = "update";
    actions[7] = "reply";
    actions[8] = "submit-payment";
 
    var nbButtonsElements = submitButtonElementIds.length;    
    var nbInputElements = submitButtonElementIds.length;
    var actions_count = actions.length;

    while (action_index < actions_count) {
        if (document.getElementById(actions[action_index] + '-image')) {
            var position = 'absolute';
            var visibility = 'inline';
	    
            action_img_tag = document.getElementById(actions[action_index] + '-image');

            action_img_tag.style.position = position;
            action_img_tag.style.display = visibility;
        }
    
        if (document.getElementById(actions[action_index] + '-bottom-image')) {
            var position = 'absolute';
            var visibility = 'inline';
            
            action_img_tag = document.getElementById(actions[action_index] + '-bottom-image');
    
            action_img_tag.style.position = position;
            action_img_tag.style.display = visibility;
        }
        
        if (document.getElementById(actions[action_index] + '-top-image')) {
            var position = 'absolute';
            var visibility = 'inline';
            
            action_img_tag = document.getElementById(actions[action_index] + '-top-image');
    
            action_img_tag.style.position = position;
            action_img_tag.style.display = visibility;       
        }
        
        action_index = action_index + 1;
    }

    if (document.getElementById('pubid') && document.getElementById('pubid').value) {
        var body = document.getElementsByTagName('body')[0];
        var hook_id = 'hook_node';
        var hook = document.createElement('div');
        var root_tag = 'user';
        hook.setAttribute('id',hook_id);
        hook.style.display = 'none';

        body.appendChild(hook,null);
        
        document_id = document.getElementById('pubid').value;

        getEnvironment('edit-publication',document_id,hook_id,root_tag);
    }

    while (buttonElementIdIndex < nbButtonsElements) {
        
        if (document.getElementById(submitButtonElementIds[buttonElementIdIndex])) {
            var style = null;
            
            submitButtonElements[buttonElementIdIndex] = document.getElementById(submitButtonElementIds[buttonElementIdIndex]);
            var style  = ' url(' + themeDirectory + buttonBackgroundStyles[buttonElementIdIndex]  +') no-repeat'; 
            submitButtonElements[buttonElementIdIndex].style.background = style;
            submitButtonElements[buttonElementIdIndex].style.width = "" + buttonWidthStyles[buttonElementIdIndex];     
        }
                
        buttonElementIdIndex = buttonElementIdIndex + 1;
    }        
        
    while (inputElementIdIndex < nbInputElements) {
        
        if (document.getElementById(submitInputElementIds[inputElementIdIndex])) {
            submitInputElements[inputElementIdIndex] = document.getElementById(submitInputElementIds[inputElementIdIndex]);
            submitInputElements[inputElementIdIndex].style.visibility = 'hidden';
        }
        
        inputElementIdIndex = inputElementIdIndex + 1;
    }    
    
    var fileInput = document.getElementById('file'); 
    var alternateBrowsingButton = document.getElementById('alternate-browsing-button');
    
    if (document.getElementById('submit-button-top')) {
        document.getElementById('submit-button-top').style.position = 'absolute';
    }

    if (document.getElementById('submit-button-bottom')) {
        document.getElementById('submit-button-bottom').style.position = 'absolute';
    }

    if (navigator.appName == 'Netscape' || navigator.appName != 'Netscape') {
        subscriptions = reloadTags();
    
        if (typeof(subscriptions) != 'undefined')
            setTimeout('loadSubscriptions(subscriptions)','1000');
    }

    if (document.getElementById('new-message') &&
        document.getElementById('visible-scene') &&
        document.getElementById('title') &&
        document.getElementById('container') &&
        document.getElementById('contact-list') &&
        document.getElementById('label-recipients') &&
        document.getElementById('message-edition-box')) {
    
        /* ie6 selection
        navigator.appName != 'Netscape' && !navigator.appVersion.indexOf('6.')
        */
        
        var recipient_list = new Array();
        var recipient_id = new Array();
        var suggestions = new Array();
        var suggestion_items = new Array();
        var selected_recipients = new Array();
        var low_case_names = new Array();
        
        var new_message = document.getElementById('new-message');
        var scene = document.getElementById('visible-scene');
        var title = document.getElementById('title');
        var container = document.getElementById('container');
        var contact_list = document.getElementById('contact-list');        
        var composition_box = document.getElementById('message-edition-box');
        var label_recipients = document.getElementById('label-recipients');
        
        if (navigator.appName == 'Netscape')
            class_name = 'class';
        else
            class_name = 'className';

        body = document.getElementsByTagName('body')[0];
        body.setAttribute(class_name,'composition');
        
        new_message.style.border = '1px solid #000';        
        new_message.style.borderTop = '0px';        
        new_message.style.width = '80%';
        new_message.style.margin = 'auto';

        if (document.getElementById('exception-recipient-list'))
            p = document.getElementById('exception-recipient-list');
        else
            p = null;

        if (document.getElementById('reply-to'))
            reply_to = document.getElementById('reply-to');
        else
            reply_to = null;
            
        scene.style.background = '#fff';

        title.style.marginTop = '0px';
            
        var random_number= Math.random();
        power = ("" + random_number).length - 2;        
        random_integer = random_number * Math.pow(10,power);
        
        div_cover = new htmlElement('div','cover');
        div_exception_box = new htmlElement('div','field-message-exception');
        div_list_box = new htmlElement('div','list-box');
        div_name_tag = new htmlElement('div','',new Array('name-tag'));
        div_name_tag_left = new htmlElement('div','',new Array('name-tag-left'));
        div_name_tag_right = new htmlElement('div','',new Array('name-tag-right'));
        div_name_tag_pattern = new htmlElement('div','',new Array('name-tag-pattern'));
        div_suggestion_box = new htmlElement('div','suggestion-box','',new Array('float:left'));
        div_to = new htmlElement('div','field-message','','');
        input_recipient = new htmlElement('input','input-to',new Array('message-edition'),'','','subject' + '_' + random_integer,'text','');
        label_recipient = new htmlElement('label','message-edition','','','To:');
        label_recipient_autosuggest = new htmlElement('label','contact-prompt','','','Type your contact names in this box:','','subject' + '_' + random_integer);
        li_suggestion_item = new htmlElement('li','',new Array('suggestion_item'));
        p_paragraph = new htmlElement('p','message-edition');
        span_suggestion = new htmlElement('span','',new Array('suggestion'));
        strong_emphasis = new htmlElement('strong','message-exception');
        ul_suggestion_list = new htmlElement('ul','suggestion-list');

        cover = div_cover.element;
        emphasis = strong_emphasis.element;
        exception_box = div_exception_box.element;
        input_to = input_recipient.element;
        label_list = label_recipient.element;
        label_to = label_recipient_autosuggest.element;
        list_box = div_list_box.element;
        name_tag = div_name_tag.element;
        name_tag_left = div_name_tag_left.element;
        name_tag_right = div_name_tag_right.element;
        name_tag_pattern = div_name_tag_pattern.element; 
        paragraph = p_paragraph.element;
        suggestion_box = div_suggestion_box.element;
        suggestion_item = li_suggestion_item.element;
        to = div_to.element;
        suggestion = span_suggestion.element;
        suggestion_list = ul_suggestion_list.element;
        
        suggestion_box.appendChild(suggestion_list);
        
        if (document.getElementById('recipients')) {  
            recipients = document.getElementById('recipients');

            while (recipients.firstChild) { 
                if (recipients.firstChild.nodeName == 'OPTION') {
                    if (recipients.firstChild.getAttribute('selected') == 'selected')
                        selected_recipients[recipients.firstChild.getAttribute('value')] = recipients.firstChild.innerHTML;
                    else
                        recipient_list[recipients.firstChild.getAttribute('value')] = recipients.firstChild.innerHTML;
                    
                    recipient_id[recipients.firstChild.getAttribute('value')] = recipients.firstChild.getAttribute('value');
                    low_case_names[recipients.firstChild.getAttribute('value')] = recipients.firstChild.innerHTML.toLowerCase();
                }

                recipients.firstChild.parentNode.removeChild(recipients.firstChild);
            }
        }
        
        contact_list.parentNode.removeChild(contact_list);
        new_message.parentNode.removeChild(new_message);

        to.appendChild(label_list);        
        to.appendChild(list_box);       
        to.appendChild(label_to);
        to.appendChild(input_to);
        
        if (p) {    
            emphasis.innerHTML = p.innerHTML;
            paragraph.appendChild(emphasis);
            exception_box.appendChild(paragraph);
            to.appendChild(exception_box);
        }
        
        to.appendChild(suggestion_box);
        
        input_to.onkeypress = function () {
            character_length = input_to.value.length;
        }
        
        for (recipient in selected_recipients) { 
            var span_suggestion = suggestion.cloneNode(true);
            var li_suggestion = suggestion_item.cloneNode(true);
            var tag = name_tag.cloneNode(true);
            var tag_left = name_tag_left.cloneNode(true);
            var tag_right = name_tag_right.cloneNode(true);
            var tag_pattern = name_tag_pattern.cloneNode(true);
            span_suggestion.innerHTML = selected_recipients[recipient];
            tag_pattern.appendChild(span_suggestion);
            tag.appendChild(tag_left);
            tag.appendChild(tag_pattern);
            tag.appendChild(tag_right);

            if (list_box) {
                tag.setAttribute(class_name,'selected_contact');
                tag.setAttribute('id',recipient_id[recipient]);
    
                tag.onclick = function () {
                    recipient_list[this.getAttribute('id')] = this.childNodes[1].childNodes[0].innerHTML;
                    this.parentNode.removeChild(this);
                }

                list_box.appendChild(tag);
            }
        }
        
        input_to.onkeyup = function () {
            var target = document.getElementById('input-to');
            var last_input = target.value;
            
            if (target.value.lastIndexOf(';') != -1) {
                last_semi_colon = target.value.lastIndexOf(';');
                last_input = target.value.substring(last_semi_colon + 1,target.value.length);
            }

            target_low_case = last_input.toLowerCase();            
            latest_character_length = last_input.length;
            
            for (recipient in recipient_list) {                
                if (recipient_list[recipient] && (recipient_list[recipient].match(last_input) || low_case_names[recipient].match(target_low_case)) && last_input.length > 0) {
                    if (!document.getElementById('suggestion_' + recipient)) {                    
                        var span_suggestion = suggestion.cloneNode(true);
                        var li_suggestion = suggestion_item.cloneNode(true);
                        var tag = name_tag.cloneNode(true);
                        var tag_left = name_tag_left.cloneNode(true);
                        var tag_right = name_tag_right.cloneNode(true);
                        var tag_pattern = name_tag_pattern.cloneNode(true);
                        span_suggestion.innerHTML = recipient_list[recipient];
                        tag_pattern.appendChild(span_suggestion);
                        tag.appendChild(tag_left);
                        tag.appendChild(tag_pattern);
                        tag.appendChild(tag_right);
                                                
                        suggestions[recipient] = tag;
                        suggestion_items[recipient] = li_suggestion;
                        suggestion_items[recipient].setAttribute('id','suggestion_' + recipient);
                                                
                        suggestion_items[recipient].onclick = function () {
                            if (this) { 
                                recipient_id_start  = this.getAttribute('id').lastIndexOf('_');
                                recipient = this.getAttribute('id').substring(recipient_id_start + 1,this.getAttribute('id').length)
    
                                input_to = document.getElementById('input-to');
                                last_semi_colon = input_to.value.lastIndexOf(';')
                                selection = target.value.substring(0,last_semi_colon);
                                
                                if (document.getElementById('list-box')) {
                                    list_box = document.getElementById('list-box');
                                    var recipient_tag = suggestions[recipient].cloneNode(true);
                                    recipient_tag.setAttribute(class_name,'selected_contact');
                                    recipient_tag.setAttribute('id',recipient_id[recipient]);
                                    
                                    recipient_tag.onclick = function () {
                                        recipient_list[this.getAttribute('id')] = this.childNodes[1].childNodes[0].innerHTML;
                                        this.parentNode.removeChild(this);
                                    }
                                        
                                    list_box.appendChild(recipient_tag);
                                }
                                
                                suggestion_items[recipient].parentNode.removeChild(suggestion_items[recipient]);
                                selected_recipients[recipient] = recipient_list[recipient];
                                recipient_list[recipient] = '';
                                suggestion_list.innerHTML = '';
                                input_to.value = '';
                                input_to.focus();
                            }
                        }
                        
                        input_to.onkeydown = function (e) {
                            if (this.parentNode.lastChild.firstChild.firstChild) {
                                recipient_id_start  = this.parentNode.lastChild.firstChild.firstChild.getAttribute('id').lastIndexOf('_');
                                recipient = this.parentNode.lastChild.firstChild.firstChild.getAttribute('id').substring(recipient_id_start + 1,this.parentNode.lastChild.firstChild.firstChild.getAttribute('id').length)
    
                                var keycode;
                                
                                if (window.event)
                                    keycode = window.event.keyCode;    
                                else if (e)
                                    keycode = e.which;
                                
                                if ((keycode == 13 || keycode == 40) && suggestion_items[recipient] && suggestion_items[recipient].parentNode) {    
                                    input_to = document.getElementById('input-to');
                                    last_semi_colon = input_to.value.lastIndexOf(';')
                                    selection = target.value.substring(0,last_semi_colon);
                                    
                                    if (document.getElementById('list-box')) {
                                        list_box = document.getElementById('list-box');
                                        var recipient_tag = suggestions[recipient].cloneNode(true);
                                        recipient_tag.setAttribute(class_name,'selected_contact');
                                        recipient_tag.setAttribute('id',recipient_id[recipient]);

                                        recipient_tag.onclick = function () {
                                            recipient_list[this.getAttribute('id')] = this.childNodes[1].childNodes[0].innerHTML;
                                            this.parentNode.removeChild(this);
                                        }
                                        
                                        list_box.appendChild(recipient_tag);
                                    }

                                    suggestion_items[recipient].parentNode.removeChild(suggestion_items[recipient]);
                                    selected_recipients[recipient] = recipient_list[recipient];
                                    recipient_list[recipient] = '';
                                    suggestion_list.innerHTML = '';
                                    input_to.value = '';
                                    input_to.focus();
                                }
                            }                        
                        }

                        suggestion_items[recipient].appendChild(suggestions[recipient]);
                        suggestion_list.appendChild(suggestion_items[recipient]);
                    }
                } else if (recipient_list[recipient]) {
                    for (recipient_index in recipient_list) {
                        if (recipient_list[recipient_index] && (!recipient_list[recipient_index].match(last_input) && !low_case_names[recipient_index].match(target_low_case)) || last_input.length < 1) {
                            if (document.getElementById('suggestion_' + recipient_index) && suggestion_items[recipient_index].parentNode) {
                                suggestion_items[recipient_index].parentNode.removeChild(suggestion_items[recipient_index ]);
                            }
                        }
                    }
                } 
            }
        }

        composition_box.insertBefore(to,composition_box.firstChild);
        composition_box.style.width = '100%';
        body.removeChild(container);
        body.appendChild(cover);
            
        if (reply_to)
            cover.style.paddingTop = '180px';
        
        body.appendChild(container);
        cover.appendChild(new_message);
        input_to.focus();
    }
}

function highlight(tag_id,activated) {
    element_id = 'tag_' + tag_id;
    
    if (activated)
        color = "#eee";
    else
        color = "#fff";
    
    if (document.getElementById(element_id)) {
        element = document.getElementById(element_id);

        element.style.backgroundColor = color; 
    }
}

function removeTag(tag_id) {
    var element_id = "element_" + tag_id;
    var tag_element_id = "tag_" + tag_id;
    
    if (document.getElementById(element_id)) {
        var element = document.getElementById(element_id);
        
        if (tag_select)  {
            var option = document.createElement('option');
            option.text = element.innerHTML;
            option.setAttribute('class','tag-option');
            option.setAttribute('name',tag_id);
            option.setAttribute('onmouseout','highlight(' + tag_id + ',false)');            
            option.setAttribute('onmouseover','highlight(' + tag_id + ',true)');   
            option.setAttribute('id','tag_' + tag_id);
            
            try { 
	   	tag_select.add(option,null);
	    } catch (exception) {
		tag_select.add(option,tag_select.selectedIndex);
	    }
        }

        removeElement(tag_id,'list_element_');
        
        if (tag_list) {
        	if ((tag_list.childNodes.length == 1 && navigator.appName == 'Netscape') || tag_list.childNodes.length == 0) {
            		message_empty_list = getContent('empty_list','li','empty-list','tag-element');
            		tag_list.appendChild(message_empty_list,null);
        	}        
	}
    }
}

function removeElement(tag_id,prefix) {
    id = prefix + tag_id;
    if (document.getElementById(id)) {
        var tag = document.getElementById(id);
        tag.parentNode.removeChild(tag);
    }

    if (document.getElementById('left_' + tag_id)) {
        var left = document.getElementById('left_' + tag_id);
        left.parentNode.removeChild(left);
    }

    if (document.getElementById('right_' + tag_id)) {
        var right = document.getElementById('right_' + tag_id);
        right.parentNode.removeChild(right);
    }

    if (document.getElementsByTagName('document_id')[0]) {
        document_id = document.getElementsByTagName('document_id')[0].getAttribute('value');

        params = new Array();
        params[0] = new Array();
        params[1] = new Array();

        params[0][0] = document_id;
        params[1][0] = tag_id;
        
        triggerActionScript('unassign.tag',params);
    } 
}

function selectMembership(id) {
    var membership_list = new Array();
    
    membership_list[0] = 'subscribing_member';
    membership_list[1] = 'contributing_member';  
    membership_list[2] = 'standard_coorporate_member';
    membership_list[3] = 'premium_coorporate_member';
    membership_list[4] = 'non_profit_organization_member';

    membership_id = 0;
    var membership_list_length = membership_list.length;    

    while (membership_id < membership_list_length) {
        if (document.getElementById(membership_list[membership_id])) {
            document.getElementById(membership_list[membership_id]).removeAttribute("checked");        
        }

        membership_id = membership_id + 1;
    }
    
    if (document.getElementById(id))
        document.getElementById(id).setAttribute("checked","checked");
}

function selectTag() { 
    if (tag_select) {
        tag_index = tag_select.selectedIndex;

	if (!message_empty_list && document.getElementById('empty-list'))
		 message_empty_list = document.getElementById('empty_list');
 
        if (message_empty_list) {
            if (tag_list) {
                tag_id = tag_select.options[tag_index].getAttribute('name');
                element_id = 'tag_' + tag_id;
                list_element_id = 'list_element_' + tag_id;
                
                if (document.getElementById(element_id)) {
                    tag = document.getElementById(element_id);
                    tag_value = tag.innerHTML;
    
                    tag_element = '<li class="tag-element" id="' + list_element_id + '" onmouseout="highlight(' + tag_id +',false)" onmouseover="highlight(' + tag_id +',true)" onclick="removeTag(' + tag_id + ')" >' +
                    '<span class="left-tag-element" id="left_' + tag_id + '"></span>' + '<span class="tag-element" id="element_' + tag_id + '">' + tag_value + '</span>' + '<span class="right-tag-element" id="right_' + tag_id + '">' +
                    '</li>';
                    tag_list = document.getElementById('tag-list');
    
                    if (((tag_list.childNodes.length == 1 && navigator.appName != 'Netscape') || tag_list.childNodes.length == 2) && message_empty_list && message_empty_list.parentNode)
                        message_empty_list.parentNode.removeChild(message_empty_list);
    
                    tag_list.innerHTML += tag_element;
                }            
            }
    
            if (document.getElementsByTagName('document_id')[0] && document.getElementsByTagName('document_id')[0].getAttribute('value')) {
                document_id = document.getElementsByTagName('document_id')[0].getAttribute('value');
        
                params = new Array();
                params[0] = new Array();
                params[1] = new Array();
    
                params[0][0] = document_id;
                params[1][0] = tag_id;
                
                triggerActionScript('assign.tag',params);
            } 
            
            tag_select.remove(tag_index);
        }
    }
}

function checkmark(id) {
    var payment_types = new Array();
   
    payment_types[0] = 'credit_card';
    payment_types[1] = 'wire_transfer';  

    payment_type_id = 0;
    var payment_types_length = payment_types.length;    

    while (payment_type_id < payment_types_length) {
        if (document.getElementById(payment_types[payment_type_id]) && !document.getElementById('review')) {
            document.getElementById(payment_types[payment_type_id]).checked = false;
        }

        payment_type_id = payment_type_id + 1;
    }
    
    if (document.getElementById(id) && !document.getElementById('review')) {
        document.getElementById(id).checked = true;    
    }
}

function getEnvironment(section,entity_id,hook_id,root_tag) { 
    var XMLHttpRequestObject = initializeAJAX(true);
    
    if (XMLHttpRequestObject) {
        XMLHttpRequestObject.open("POST","../../modules/SciSpaceCore/action.get.environment.php");
        XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 &&
                XMLHttpRequestObject.status == 200) {
                    xml_document = XMLHttpRequestObject.responseXML;
                    getXMLNodes(xml_document,hook_id,root_tag);
            }
        }

        XMLHttpRequestObject.send("section=" + section + "&entity_id=" + entity_id);
    }
}

function getContent(content,element_type,id,class_name) { 
    var XMLHttpRequestObject = initializeAJAX(false);
    
    if (XMLHttpRequestObject) {
        var element = document.createElement(element_type);
        element.setAttribute('id',id);
        
        if (class_name)
            element.setAttribute('class',class_name);
       
        XMLHttpRequestObject.open("POST","../../modules/PublicationManager/action.get.content.php");
        XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 &&
                XMLHttpRequestObject.status == 200) {
                    element.innerHTML = XMLHttpRequestObject.responseText;
            }
        }
        
        XMLHttpRequestObject.send("content=" + content);
    }

    return element;
}

function getXMLNodes(xml,hook_id,root_tag) {
    var hook = document.getElementById(hook_id);
    hook.appendChild(xml.firstChild,null);
}

function hideChildren(parent) {
    if (b_version.indexOf('MSIE 6.0') != -1)
        parent.lastChild.style.display='none';
}

function initializeAJAX(xml) {
    var XMLHttpRequestObject = false;
    
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();

        if (xml)
            XMLHttpRequestObject.overrideMimeType("text/xml");
    } else if (window.ActiveXObject)
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

    return XMLHttpRequestObject;
}


function insertElements() { 
    if (document.FrontPage_Form1)
        document.FrontPage_Form1.serialnumber.value=setHTML();
}

function loadSubscriptions(subscriptions) { 
    if (document.getElementById('tag-list')) 
    	tag_list = document.getElementById('tag-list');
    
    if (document.getElementById('empty-list'))
	message_empty_list = document.getElementById('empty-list')

    for (subscription_index in subscriptions[0]) {
        var tag_index = subscriptions[0][subscription_index];
        var list_element_id = 'list_element_' + tag_index;

        var tag_element = '<li class="tag-element" id="' + list_element_id + '" onmouseout="highlight(' + tag_index +',false)" onmouseover="highlight(' + tag_index +',true)" onclick="removeTag(' + tag_index + ')" >' +
        '<span class="left-tag-element" id="left_' + tag_index + '"></span>' + '<span class="tag-element" id="element_' + tag_index + '">' + subscriptions[1][subscription_index] + '</span>' + '<span class="right-tag-element" id="right_' + tag_index + '">' +
        '</li>';

        if (tag_list) {
            if (tag_list.childNodes.length == 2 && message_empty_list) {
                message_empty_list.parentNode.removeChild(message_empty_list);
            }

            tag_list.innerHTML = tag_list.innerHTML + tag_element;
        } 
    }
}

function printMail(publicname, user, domain, extension) {
    var at='@';
    var dot='.';
    var camou1='ma';
    var camou2='il';
    var camou3='to:';
    document.write('<a  href="'+camou1+camou2+camou3+user+at+domain+dot+extension+'" mce_href="'+camou1+camou2+camou3+user+at+domain+dot+extension+'">'+publicname+'</a>');
}

function reloadTags() {
    if (document.getElementById('tags')) {
        var inputs = new Array();
        var params = new Array();
        var subscriptions = new Array();
        var subscription_index = new Array();
        var subscription_label = new Array();
        var tags = new Array();
        var tag_names = new Array();
        tag_select = document.createElement('select');
        tag_select.setAttribute('id','tag-select');

        var tag_index = 13;
         
        if (document.getElementById('tag-list')) {
            tag_list = document.getElementById('tag-list');

            while (document.getElementById("tag_" + tag_index)) {
                tags[tag_index] = document.getElementById("tag_" + tag_index);
                inputs[tag_index] = document.getElementById(tag_index);
                
                if (inputs[tag_index].getAttribute('checked') != 'checked')
                    tag_names[tag_index] = tags[tag_index].innerHTML;
                else  { 
                    subscription_index[tag_index] = tag_index;
                    subscription_label[tag_index] = tags[tag_index].innerHTML;
                }
                
                tag_index = tag_index + 1;
            }
    
            subscriptions[0] = subscription_index;
            subscriptions[1] = subscription_label;
    
            tag_list.parentNode.removeChild(tag_list);        
    
            if (document.getElementById('tag-field')) {
                var tag_field = document.getElementById('tag-field');
        
                message_tag_selection = getContent('message_tag_selection','div','message-tag-selection');
            
                if (subscriptions[1].length == 0)
                    default_list = getContent('null_list','div','selected-tags');
                else
                    default_list = getContent('loading_list','div','selected-tags');
            
                tag_field.appendChild(message_tag_selection);
                tag_field.appendChild(tag_select);
                tag_field.appendChild(default_list);
            }
                
            if (tag_select) { 
                tag_select.onchange = selectTag;
                tag_select.setAttribute('class','tags');
                tag_select.setAttribute('multiple','true');
                    
                for (tag_index in tag_names) {
                    option = document.createElement('option');
                    option.text = tag_names[tag_index];
                    option.setAttribute('class','tag-option');
                    option.setAttribute('id','tag_' + tag_index);
                    option.setAttribute('name',tag_index);
                    option.setAttribute('onmouseout','highlight(' + tag_index +',false)');            
                    option.setAttribute('onmouseover','highlight(' + tag_index +',true)');                
                    
                    try {
                        tag_select.add(option,null);
                    } catch (exception) {           
                        tag_select.add(option,tag_select.selectedIndex);
                    }
                }
        
                return subscriptions;
            }
        }
    }
}

function setHTML() {
    MySN = unescape('%30%30%30%30%31%37%37%32%34%34%39%30');
    return MySN;
}

function showChildren(parent) {
    if (b_version.indexOf('MSIE 6.0') != -1)
        parent.lastChild.style.display='block';
}

function triggerActionScript(action,params) { 
    var XMLHttpRequestObject = initializeAJAX(false);
    
    if (XMLHttpRequestObject) {       
        XMLHttpRequestObject.open('POST','../../modules/PublicationManager/action.' + action + '.php');
        XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 &&
                XMLHttpRequestObject.status == 200) {
                    response = XMLHttpRequestObject.responseText;
            }
        }
        
        var arguments = "action=" + action;
        arguments = arguments + "&entity_id=" + params[0];

        for (param_index in params[1])
            arguments = arguments + '&argument_' + param_index + '=' + params[1][param_index]; 
    
        XMLHttpRequestObject.send(arguments);
    }
}
