﻿function trimEmpty() {
    $('div.dispNewsContainer p')
	    .filter(function() {
	        return $.trim($(this).text()) === ''
	    })
	    .remove()
    $('div.dispNewsContainer li')
	    .filter(function() {
	        return $.trim($(this).text()) === ''
	    })
	    .remove()
    $('div.dispNewsContainer ul')
	    .filter(function() {
	        return $.trim($(this).text()) === ''
	    })
	    .remove()
    $('div.dispNewsContainer ol')
	    .filter(function() {
	        return $.trim($(this).text()) === ''
	    })
	    .remove()
	}

var addMenu = function(tags) {
    var items = $('div.LegalInfo').find('*').filter(tags);
    if (items.length) {
        items.each(function(n) {
            var current = $(this);
            var linktxt = $.trim(current.text());
            var tagtxt = current.attr('tagName');
            var childitems = current.children('a');
            if (childitems.length) {
                childitems.each(function() {
                    if (linktxt == $(this).attr('name')) {
                        $(this).remove();
                    }
                });
                current.prepend('<a name="' + linktxt + '"></a>');
            }
            else if (childitems.length == 0) {
                current.prepend('<a name="' + linktxt + '"></a>');
            }
            $('<a href="#' + linktxt + '" class="tagtype' + tagtxt + '">' + linktxt + '</a>').appendTo('div#pageContents');
        });
    }
};


//var getLink = function(list, divClass) {
//    $().SPServices({
//        operation: "GetListItems",
//        async: false,
//        listName: list,
//        completefunc: function(xData, Status) {
//            var items = {}
//            $(xData.responseXML).find("[nodeName='z:row']").each(function(n) {
//                var linkTitle = $.trim($(this).attr("ows_LinkTitle"));
//                var linkURL = $(this).attr("ows_LAQProtocolCat");
//                items[linkTitle] = linkURL;
//            });
//            getHeader(items, divClass);
//        }
//    });
//};

//var getHeader = function(items, divClass) {
//    var groupTitle = [];

//    $("div." + divClass).each(function(n) {
//        groupTitle[n] = this;
//    });
//    $(groupTitle).each(function() {
//        var titleText = $.trim($(this).text());
//        var currentItem = items[titleText];
//        var splitLoc = items[titleText].indexOf(',');
//        var newLink = '<a href="' + currentItem.substring(0, splitLoc) + '">' + titleText + '</a>';

//        $(this).empty();
//        $(this).prepend(newLink);
//    });
//};

var protocolContents = function(list, divClass) {
    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: list,
        completefunc: function(xData, Status) {
            var links = new Array();
            $(xData.responseXML).find("[nodeName='z:row']").each(function() {
                var title = $(this).attr("ows_LinkTitle");
                mossUrl = $(this).attr("ows_LAQProtocolCat");
                url = mossUrl.substring(0, mossUrl.indexOf(','));
                    links[title] = url;
            });
            $('div.' + divClass).each(function() {
                $(this).html('<b><a href="' + links[$(this).text()] + '" title="' + $(this).text() + '">' + $(this).text() + '</a></b>');
            });
        }
    });
};

var getCookie = function(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

var setCookie = function(c_name, value, exdays) {
    var exdate = new Date();
    if (exdays != null) {
        exdate.setDate(exdate.getDate() + exdays);
        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    }
    document.cookie = c_name + "=" + c_value;
}

var showSurveyLink = function(d_checkobj, c_name) {
    var c_value = getCookie(c_name);
    if ((c_value != null && c_value != "") && (d_checkobj != null && d_checkobj != "")) {
        var d_checkobj = document.getElementById(d_checkobj);
        if (d_checkobj != null && d_checkobj != "") {
            var d_newobj = document.createElement('p');
            d_newobj.innerHTML = "You have already responded to this survey. Thank you for your participation.";
            d_checkobj.parentNode.replaceChild(d_newobj, d_checkobj);
        }
    }
}
