﻿function confirmDelete(delUrl, message) {
    if (confirm(message)) {
        document.location = delUrl;
    }
}


// menu

var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id) {
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    // get new layer and show it
    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose() {

    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
    closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

// close layer when click-out
document.onclick = mclose;


function HeaderSelectBox(currentURL, forumMenuStr, userProfileStr, subscriptionsStr, latestTopicsStr, user) {

    var selectString = '<ul id="sddm">';
    selectString += '<li><a href="#" onmouseover="mopen(\'m1\')" onmouseout="mclosetime()">' + forumMenuStr + '</a>';
    selectString += '<div id="m1" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">'

    if (user != "") {
        selectString += '<a href="' + currentURL + '/_layouts/SocialSquared/EditProfile.aspx">' + userProfileStr + '</a>';
        selectString += '<a href="?action=ViewSubscriptions">' + subscriptionsStr + '</a>';
    }

    selectString += '<a href="?action=ViewLatestTopics">' + latestTopicsStr + '</a>';
    selectString += '</div>';
    selectString += '</li>';
    selectString += '</ul>';

    var headerSelect = document.getElementById('headerSelect');

    if (headerSelect != null) {
        headerSelect.innerHTML = selectString;
    }
}

// end menu

//File Upload

function add_new_file(field, clientId) {

    // Get the number of files previously uploaded.
    var count = parseInt(document.getElementById('file_count').value);

    // Get the name of the file that has just been uploaded.
    var file_name = document.getElementById(clientId + "_newfile_" + count + "_").value;

    // Hide the file upload control containing the information about the file that was just uploaded.
    document.getElementById('newfile_row').style.display = "none";
    document.getElementById('newfile_row').id = clientId + "_newfile_row_" + count + "_";

    // Get a reference to the table containing the uploaded pictures.        
    var table = document.getElementById('files_table');

    // Insert a new row with the file name and a delete button.
    var row = table.insertRow(table.rows.length);
    row.id = "inserted_file_" + count + "_";
    var cell0 = row.insertCell(0);

    cell0.innerHTML = '<input type="text" class="btn" size="70" disabled="disabled" name="inserted_file_' + count + '_" value="' + file_name + '" /><input type="button" name="delete_' + count + '_" value="Delete" onclick="delete_inserted(this, \'' + clientId + '\')" class="btn" />';

    // Increment count of the number of files uploaded.
    ++count;

    // Insert a new file upload control in the table.
    var row = table.insertRow(table.rows.length);
    row.id = "newfile_row";
    var cell0 = row.insertCell(0);
    cell0.innerHTML = '<input type="file" name="' + clientId + '_newfile_' + count + '_" id="' + clientId + '_newfile_' + count + '_" runat="server"  onchange="add_new_file(this, \'' + clientId + '\')" class="btn" size="70"/>';

    // Update the value of the file hidden input tag holding the count of files uploaded.
    document.getElementById('file_count').value = count;
}

function delete_inserted(field, clientId) {
    // Get the field name.

    var name = field.name;

    // Extract the file id from the field name.
    var id = name.substr(name.indexOf('_') + 1, name.lastIndexOf('_') - name.indexOf('_') - 1);

    // Hide the row displaying the uploaded file name.
    document.getElementById("inserted_file_" + id + "_").style.display = "none";

    // Get a reference to the uploaded file control.
    var control = document.getElementById(clientId + "_newfile_" + id + "_");

    // Remove the new file control.
    control.parentNode.removeChild(control);
}

// Web Part Width Adjustment

function confirmDelete(delUrl, message) {
    if (confirm(message)) {
        document.location = delUrl;
    }
}

//File Upload

function add_new_file(field, clientId) {

    // Get the number of files previously uploaded.
    var count = parseInt(document.getElementById('file_count').value);

    // Get the name of the file that has just been uploaded.
    var file_name = document.getElementById(clientId + "_newfile_" + count + "_").value;

    // Hide the file upload control containing the information about the file that was just uploaded.
    document.getElementById('newfile_row').style.display = "none";
    document.getElementById('newfile_row').id = clientId + "_newfile_row_" + count + "_";

    // Get a reference to the table containing the uploaded pictures.        
    var table = document.getElementById('files_table');

    // Insert a new row with the file name and a delete button.
    var row = table.insertRow(table.rows.length);
    row.id = "inserted_file_" + count + "_";
    var cell0 = row.insertCell(0);

    cell0.innerHTML = '<input type="text" class="btn" size="70" disabled="disabled" name="inserted_file_' + count + '_" value="' + file_name + '" /><input type="button" name="delete_' + count + '_" value="Delete" onclick="delete_inserted(this, \'' + clientId + '\')" class="btn" />';

    // Increment count of the number of files uploaded.
    ++count;

    // Insert a new file upload control in the table.
    var row = table.insertRow(table.rows.length);
    row.id = "newfile_row";
    var cell0 = row.insertCell(0);
    cell0.innerHTML = '<input type="file" name="' + clientId + '_newfile_' + count + '_" id="' + clientId + '_newfile_' + count + '_" runat="server"  onchange="add_new_file(this, \'' + clientId + '\')" class="btn" size="70"/>';

    // Update the value of the file hidden input tag holding the count of files uploaded.
    document.getElementById('file_count').value = count;
}

function delete_inserted(field, clientId) {
    // Get the field name.

    var name = field.name;

    // Extract the file id from the field name.
    var id = name.substr(name.indexOf('_') + 1, name.lastIndexOf('_') - name.indexOf('_') - 1);

    // Hide the row displaying the uploaded file name.
    document.getElementById("inserted_file_" + id + "_").style.display = "none";

    // Get a reference to the uploaded file control.
    var control = document.getElementById(clientId + "_newfile_" + id + "_");

    // Remove the new file control.
    control.parentNode.removeChild(control);
}

// Web Part Width Adjustment

function AdjustWidth(clietntId) {

    // Get root Div
    var root = document.getElementById("root" + clietntId);

    // Get subRoot Div
    var subRoot = document.getElementById("subRoot" + clietntId);

    // Wb Part min width for the best design
    var minWidth = "500";

    // make sure that web part is 100%, not an explicit width is set by user
    if (subRoot.style.width == "100%") {
        // get the web part actual width
        var offsetWidth = subRoot.clientWidth;

        //if actual with is less than the desired/min with
        if (offsetWidth < minWidth) {
            // set subroot with to desired/min width
            subRoot.style.width = minWidth + "px";

            //set root width to web part actual with
            root.style.width = offsetWidth + "px";

            // add automtaic scrollbars to root
            root.style.overflowX = "auto";
            root.style.overflowY = "hidden";

        }
    }
}

function DeleteConfirmation(paid, message) {
    var answer = confirm(message)
    if (answer) {
        var delPostAttchIds = FindFirstDescendant("delPostContainer", "input");

        if (delPostAttchIds != null) {
            delPostAttchIds.value = delPostAttchIds.value + ":" + paid;
            document.forms[0].submit();
        }
    }
}

function HideDeletedAttchMents() {
    var delPostAttchIds = FindFirstDescendant("delPostContainer", "input");
    if (delPostAttchIds != null) {
        if (delPostAttchIds.value != "") {
            var paid = delPostAttchIds.value.split(":")

            for (var i = 0; i < paid.length; i++) {
                var attachmentContainer = document.getElementById("attach" + paid[i]);

                if (attachmentContainer != null) {
                    attachmentContainer.style.display = "none";
                }
            }
        }
    }
}

function FindFirstDescendant(parent, tagname) {
    parent = document.getElementById(parent);
    var descendants = parent.getElementsByTagName(tagname);

    if (descendants.length)
        return descendants[0];
    return null;
}

function ShowHideTags(hide) {
    var existingTags = document.getElementById("existingTags");

    if (hide) {
        existingTags.style.display = "none";
    }
    else {
        existingTags.style.display = "block";
    }
}

function InsertTags() {
    var spanTags = document.getElementById("spanTags");
    var txtTags = spanTags.getElementsByTagName("input")[0];

    var selectedTags = "";

    if (typeof document.forms[0].chkTags != "undefined") {
        //one chechbox
        if (typeof document.forms[0].chkTags.length == "undefined") {
            var singleCheckbox = document.getElementById("chkTags0");

            if (singleCheckbox != null) {
                txtTags.value = singleCheckbox.value;
            }
        }
        else {
            for (var i = 0; i < document.forms[0].chkTags.length; i++) {
                if (document.forms[0].chkTags[i].checked) {
                    selectedTags += document.forms[0].chkTags[i].value + delimeter;
                }
            }

            if (selectedTags != "") {
                txtTags.value = selectedTags.substring(0, selectedTags.length - 1);
            }
        }
    }
    existingTags.style.display = "none";
}

function PostAnonymously(chb) {
    if (chb.checked) {
        if (document.getElementById("ShowName") != null)
            document.getElementById("ShowName").style.display = "block";
        if (document.getElementById("ShowEmail") != null)
            document.getElementById("ShowEmail").style.display = "block";

    }
    else {
        if (document.getElementById("ShowName") != null)
            document.getElementById("ShowName").style.display = "none";
        if (document.getElementById("ShowEmail") != null)
            document.getElementById("ShowEmail").style.display = "none";
    }


}
