//  Drag and drop dashboard panels
function dashboardDragDrop() {
    $('.column-draggable').sortable({
        connectWith: '.column-draggable',  
        handle: '.handle-draggable',
        cursor: 'move',  
        placeholder: 'placeholder',  
        forcePlaceholderSize: true,  
        opacity: 0.5,
        update: function () {
            var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
            order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
			order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
			order += 'mpf=' + $('#hidMPF').val() + '&';
			order += 'summary=' + $('#hidShowSummary').val() + '&';
			order += 'action=updateRecordsListings';
			$.post("/ColumnCallback.aspx", order);
		}
    }).disableSelection();
}
$(function() {
    if ( $('.panel-draggable').length > 0 ) {
        dashboardDragDrop();
    }
});

// Deleting panels
$(function() {
	$('a.close').click(function() {
		$(this).parents('div.panel:first').remove();
		var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
		order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
		order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
		order += 'mpf=' + $('#hidMPF').val() + '&';
		order += 'summary=' + $('#hidShowSummary').val() + '&';
		order += 'action=updateRecordsListings';
		$.post("/ColumnCallback.aspx", order);
        return false;
	});
});

// Reset panels
$(function() {
	$('a#resetButton').click(function() {
	    var order = 'col1=latest-reports&col2=latest-special-features%2cmy-account&col3=my-pearlfinders%2cyour-review-diary&';
		order += 'action=updateRecordsListings';
		$.post("/ColumnCallback.aspx", order, function() {location.reload();} );
	});
});

// Pulling out panels
$(function () {
    $('a#mpfPullOut').click(function () {
        // remove letter from hidden field
        var letter = $('#hidLetter').val();
        $('#hidMPF').val(function(i, val) {   return val.replace(letter, ''); });
        // update saved panels
        var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
        order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
        order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
        order += 'mpf=' + $('#hidMPF').val() + '&';
        order += 'summary=' + $('#hidShowSummary').val() + '&';
        order += 'action=add&new=';
        if (letter == 'A') {
            order += 'alerts';
        }
        else if (letter == 'B') {
            order += 'watchlist';
        }
        else if (letter == 'C') {
            order += 'campaigns';
        }
        else if (letter == 'D') {
            order += 'saved-stories';
        }
        else if (letter == 'E') {
            order += 'reportsearches';
        }
        else if (letter == 'F') {
            order += 'contactsearches';
        }
        else if (letter == 'G') {
            order += 'companysearches';
        }
        $.post("/ColumnCallback.aspx", order, function () { location.reload(); });
    });
});
$(function () {
    $('a#lrPullOut').click(function () {
        // remove letter from hidden field
        var spid = $('#hidSub').val();
        // update saved panels
        var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
        order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
        order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
        order += 'mpf=' + $('#hidMPF').val() + '&';
        order += 'summary=' + $('#hidShowSummary').val() + '&';
        order += 'action=add&new=latest-reports-' + spid;
        $.post("/ColumnCallback.aspx", order, function () { location.reload(); });
    });
});

// summaries
$(function () {
    $('a#btnShowSummary').click(function () {
        // update saved panels
        var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
        order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
        order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
        order += 'mpf=' + $('#hidMPF').val() + '&';
        order += 'summary=1';
        $.post("/ColumnCallback.aspx", order, function () { location.reload(); });
    });
});
$(function () {
    $('a#btnHideSummary').click(function () {
        // update saved panels
        var order = 'col1=' + $('.column-draggable:nth-child(1)').sortable("toArray") + '&';
        order += 'col2=' + $('.column-draggable:nth-child(2)').sortable("toArray") + '&';
        order += 'col3=' + $('.column-draggable:nth-child(3)').sortable("toArray") + '&';
        order += 'mpf=' + $('#hidMPF').val() + '&';
        order += 'summary=0';
        $.post("/ColumnCallback.aspx", order, function () { location.reload(); });
    });
});

// Dashboard panel accordians
function dashboardAccordians() {
    $(".panel-options").accordion({
        autoHeight: false,
        //collapsible: true,
        header: '.accordian-header',
        navigation: true,
        change: function (event, ui) {
            if (ui.newHeader.parent().parent().attr('id') == 'my-pearlfinders') {
                if (ui.newContent.attr('id') == 'my-pearlfinders-1') {
                    $('#hidLetter').val('A');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-2') {
                    $('#hidLetter').val('B');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-3') {
                    $('#hidLetter').val('C');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-4') {
                    $('#hidLetter').val('D');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-5') {
                    $('#hidLetter').val('E');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-6') {
                    $('#hidLetter').val('F');
                }
                else if (ui.newContent.attr('id') == 'my-pearlfinders-7') {
                    $('#hidLetter').val('G');
                }
            }
            if (ui.newHeader.parent().parent().attr('id') == 'latest-reports') {
                $('#hidSub').val(ui.newContent.attr('id'));
                fixLinks();
            }
        }
    });
}

function fixLinks() {
    var subDate = $("#latest-reports nav:visible time:first").attr('datetime');
    $('#lnkPrint').attr('href', 'Dashboard/Print.aspx?spid=' + $('#hidSub').val() + '&day=' + subDate.substr(8) + '&month=' + subDate.substr(5, 2) + '&year=' + subDate.substr(0, 4));
    $('#lnkExport').attr('href', 'Dashboard/Contacts/Export.aspx?spid=' + $('#hidSub').val() + '&day=' + subDate.substr(8) + '&month=' + subDate.substr(5, 2) + '&year=' + subDate.substr(0, 4));
}

/* function dashboardAccordians() {
    $('.accordian-content').slideUp(0, function() {
        $('.panel').each( function() {
            $(this).find('.accordian-header').first().addClass('ui-state-active');
            $(this).find('.accordian-content').first().slideDown(0);
        });
    });
    $('.accordian-header').click( function() {
        var clicked = $(this);
        $(clicked).parent('.panel-options').find('.accordian-header').removeClass('ui-state-active');
        $(clicked).parent('.panel-options').find('.accordian-content').slideUp(0);
        $(clicked).addClass('ui-state-active');
        $(clicked.attr('href')).slideDown('fast');
        return false;
    });
} */
$(function() {
    if ( $('.accordian-header').length > 0 ) {
        dashboardAccordians();
    }
});

//  Date pickers
function datePickers() {
    var defaults = {
        dateFormat: 'dd/mm/yy',
        firstDay: 1,    // Monday
        maxDate: '1y',   // 1 yr away
        minDate: '-5y'  // 5yr ago
    };
    $('.datepicker').datepicker( defaults );
    if ( $('#quickJump').length > 0 ) {
        $('#quickJumpHidden').datepicker('option', 'onSelect', function (dateText, inst) {
            window.location.href = 'dashboard.aspx?day=' + dateText.substring(0, 2) + '&month=' + dateText.substring(3, 5) + '&year=' + dateText.substring(6);
        });
        $('#quickJump').click( function() {
            $('#quickJumpHidden').datepicker('show');
            return false;
        });
    }
    if ( $('#reviewDiary').length > 0 ) {
        $('#reviewDiaryHidden').datepicker('option', 'onSelect', function(dateText, inst) {
            alert('Review diary date chosen');
        });
        $('#reviewDiary').click( function() {
            $('#reviewDiaryHidden').datepicker('show');
            return false;
        });
    }
}
$(function() {
    if ( $('.datepicker').length > 0 ) {
        datePickers();
    }
});

//  Modal contact information
function showContact(url) {
    $('#contact-modal').addClass('loading');
    $('#contact-modal-draw').remove('#main.contact-detail');
    $('#contact-modal-draw').hide();
    $('#contact-modal').dialog({
        open: function() {
            var target = url + '&' + Math.floor(Math.random()*1001) + ' #main';// random number reduces caching likelihood
            $('#contact-modal-draw').load( target, function() {
                $('#contact-modal').removeClass('loading');
                $('#contact-modal-draw').slideDown(800);
            });
        }
    });
    $('#contact-modal').dialog('open');
}
$(function() {
    if ( $('a.contact-modal').length > 0 ) {
        if ( $('div#contact-modal').length < 1 ) {
            $('body').append('<div id="contact-modal"><div id="contact-modal-draw"></div></div>');
            $('#contact-modal').dialog({
                autoOpen: false,
                draggable: false,
                minHeight: 500,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Contact information',
                width: 870
            });
        }
        $('a.contact-modal').each( function() {
            $(this).click( function() {
                showContact($(this).attr('href'));
                return false;
            });
        })
    }
});

//  Modal "Save to My Pearlfinders"
function showSave(id, saveUrl) {
    $('#save-name-report q').text($('header h1').text());
    $('#save-id-report').val(id);
    $('#save-modal-report').dialog('open');
    $('#save-button-report').click(function () {
        if (($('#save-folder-report').val() == '' && $('#save-new-report').val() == '') || ($('#save-folder-report').val() != '' && $('#save-new-report').val() != '')) {
            alert('You must either:\n- select an existing folder, or;\n- enter the name of a new folder.');
            return false;
        }

        var le_id = id.replace('#', '');
        var foldername = '';
        if ($('#save-folder-report').val() != '') {
            foldername = $('#save-folder-report').val();
        } else if ($('#save-new-report').val() != '') {
            foldername = $('#save-new-report').val();
        }

        $.ajax({
            //this is the file that processes the data
            url: saveUrl,
            //GET method is used
            type: "GET",
            //pass the data        
            data: 'action=add&le_id=' + le_id + '&folder=' + foldername,
            //Do not cache the page
            cache: false,
            //success
            success: function (html) {
                $('#save-modal-report').dialog('close');
                window.location.reload();
            }
        });
    });
}
$(function() {
    if ($('div.panel-actions a.panel-pearlfinders').length > 0 || $('a.srSave').length > 0 || $('a.srSaveHome').length > 0 || $('a.srSaveSub').length > 0) {
        if ( $('div#save-modal-report').length < 1 ) {
            $('form.layout').append('<div id="save-modal-report"><p id="save-name-report"><q></q><p>Where would you like to save this item?</p><input type="hidden" name="saveid" value="" id="save-id-report" /><div class="row"><label for="save-folder-report">An existing folder:</label><select name="savefolder-report" id="save-folder-report"><option value="">--- Select ---</option>' + reportSaveFolders + '</select></div><div class="row"><label class="center">&mdash;<strong>or</strong>&mdash;</label></div><div class="row"><label for="save-new-report">Create new folder:</label> <input type="text" name="savenew-report" value="" id="save-new-report" /></div><br /><div class="row"><input type="submit" value="Save this item" id="save-button-report" class="button" /></div></div>');
            $('#save-modal-report').dialog({
                autoOpen: false,
                draggable: false,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Save to My Pearlfinders',
                width: 460
            });
        }
        $('div.panel-actions a.panel-pearlfinders').each( function() {
            $(this).click( function() {
                showSave($(this).attr('href'), "saved.aspx");
                return false;
            });
        })
        $('a.srSave').each(function () {
            $(this).click(function () {
                showSave($(this).attr('href'), "saved.aspx");
                return false;
            });
        })
        $('a.srSaveHome').each(function () {
            $(this).click(function () {
                showSave($(this).attr('href'), "dashboard/saved.aspx");
                return false;
            });
        })
        $('a.srSaveSub').each(function () {
            $(this).click(function () {
                showSave($(this).attr('href'), "../saved.aspx");
                return false;
            });
        })
    }
});

///////
function showSaveCompanies(id) {
    $('#save-name q').text($('header h1').text());
    $('#save-id').val(id);
    $('#save-modal').dialog('open');
    $('#save-button').click(function () {
        dontShowWarning();
        if (($('#save-folder').val() == '' && $('#save-new').val() == '') || ($('#save-folder').val() != '' && $('#save-new').val() != '')) {
            alert('You must either:\n- select an existing folder, or;\n- enter the name of a new folder.');
            return false;
        }

        var ct_id = id.replace('#', '');
        var foldername = '';
        if ($('#save-folder').val() != '') {
            foldername = $('#save-folder').val();
        } else if ($('#save-new').val() != '') {
            foldername = $('#save-new').val();
        }

        $.ajax({
            //this is the file that processes the data
            url: "watchlist.aspx",
            //GET method is used
            type: "GET",
            //pass the data        
            data: 'action=add&ct_id=' + ct_id + '&folder=' + foldername,
            //Do not cache the page
            cache: false,
            //success
            success: function (html) {
                $('#save-modal').dialog('close');
            }
        });
    });
}
$(function () {
    if ($('div.add-watchlist a.panel-pearlfinders').length > 0) {
        if ($('div#save-modal').length < 1) {
            $('form.layout').append('<div id="save-modal"><p id="save-name"><q></q><p>Where would you like to save this item?</p><input type="hidden" name="saveid" value="" id="save-id" /><div class="row"><label for="save-folder">An existing folder:</label><select name="savefolder" id="save-folder"><option value="">--- Select ---</option>' + companySaveFolders + '</select></div><div class="row"><label class="center">&mdash;<strong>or</strong>&mdash;</label></div><div class="row"><label for="save-new">Create new folder:</label> <input type="text" name="savenew" value="" id="save-new" /></div><br /><div class="row"><input type="submit" value="Save this item" id="save-button" class="button" /></div></div>');
            $('#save-modal').dialog({
                autoOpen: false,
                draggable: false,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Save to My Pearlfinders',
                width: 460
            });
        }
        $('div.add-watchlist a.panel-pearlfinders').each(function () {
            $(this).click(function () {
                showSaveCompanies($(this).attr('href'));
                return false;
            });
        })
    }
});
///////


///////
function showSaveCompaniesPostback() {
    $('#save-name q').text($('header h1').text());
    $('#save-modal').dialog('open');
    $('#save-button').click(function () {
        dontShowWarning();
        if (($('#save-folder').val() == '' && $('#save-new').val() == '') || ($('#save-folder').val() != '' && $('#save-new').val() != '')) {
            alert('You must either:\n- select an existing folder, or;\n- enter the name of a new folder.');
            return false;
        }
        var foldername = '';
        if ($('#save-folder').val() != '') {
            foldername = $('#save-folder').val();
        } else if ($('#save-new').val() != '') {
            foldername = $('#save-new').val();
        }

        $('#' + hiddenFieldId).val(foldername);
        __doPostBack('', '');
    });
    return false;
}
$(function () {
    if ($('div#main.company-results').length > 0) {
        if ($('div#save-modal').length < 1) {
            $('form.layout').append('<div id="save-modal"><p id="save-name"><q></q><p>Where would you like to save the companies?</p><div class="row"><label for="save-folder">An existing folder:</label><select name="savefolder" id="save-folder"><option value="">--- Select ---</option>' + companySaveFolders + '</select></div><div class="row"><label class="center">&mdash;<strong>or</strong>&mdash;</label></div><div class="row"><label for="save-new">Create new folder:</label> <input type="text" name="savenew" value="" id="save-new" /></div><br /><div class="row"><input type="submit" value="Save this item" id="save-button" class="button" /></div></div>');
            $('#save-modal').dialog({
                autoOpen: false,
                draggable: false,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Save to My Pearlfinders',
                width: 460
            });
        }
    }
});
///////


///////
function showSaveContacts() {
    $('#save-name q').text($('header h1').text());
    $('#save-modal').dialog('open');
    $('#save-button').click(function () {
        dontShowWarning();
        if (($('#save-folder').val() == '' && $('#save-new').val() == '') || ($('#save-folder').val() != '' && $('#save-new').val() != '')) {
            alert('You must either:\n- select an existing folder, or;\n- enter the name of a new folder.');
            return false;
        }
        var foldername = '';
        if ($('#save-folder').val() != '') {
            foldername = $('#save-folder').val();
        } else if ($('#save-new').val() != '') {
            foldername = $('#save-new').val();
        }

        $('#' + hiddenFieldId).val(foldername);
        __doPostBack('', '');
    });
    return false;
}
$(function () {
    if ($('div#main.company-contacts').length > 0) {
        if ($('div#save-modal').length < 1) {
            $('form.layout').append('<div id="save-modal"><p id="save-name"><q></q><p>Where would you like to save the contacts?</p><div class="row"><label for="save-folder">An existing folder:</label><select name="savefolder" id="save-folder"><option value="">--- Select ---</option>' + contactSaveFolders + '</select></div><div class="row"><label class="center">&mdash;<strong>or</strong>&mdash;</label></div><div class="row"><label for="save-new">Create new folder:</label> <input type="text" name="savenew" value="" id="save-new" /></div><br /><div class="row"><input type="submit" value="Save this item" id="save-button" class="button" /></div></div>');
            $('#save-modal').dialog({
                autoOpen: false,
                draggable: false,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Save to My Pearlfinders',
                width: 460
            });
        }
    }
});
///////


///////
function showSaveSearch() {
    $('#saveSearch-name q').text($('header h1').text());
    $('#saveSearch-modal').dialog('open');
    $('#saveSearch-button').click(function () {
        dontShowWarning();
        if ($('#saveSearch-new').val() == '') {
            alert('You must enter a name for this search.');
            return false;
        }
        var name = $('#saveSearch-new').val();
        var type = $('#hidType').val();
        var link = $('#hidLink').val();
        var saveUrl = '../SavedSearch.aspx';

        $.ajax({
            //this is the file that processes the data
            url: saveUrl,
            //GET method is used
            type: "GET",
            //pass the data        
            data: 'action=add&type=' + urlencode(type) + '&name=' + urlencode(name) + '&link=' + urlencode(link),
            //Do not cache the page
            cache: false,
            //success
            success: function (html) {
                $('#saveSearch-modal').dialog('close');
            }
        });
    });
    return false;
}
$(function () {
    if ($('a#saveSearch').length > 0) {
        if ($('div#saveSearch-modal').length < 1) {
            $('form.layout').append('<div id="saveSearch-modal"><p id="saveSearch-name"><q></q><p>What would you like to call this search?</p><div class="row"><label for="saveSearch-new">Save as:</label> <input type="text" name="savenew" value="" id="saveSearch-new" maxlength="50" /></div><br /><div class="row"><input type="submit" value="Save this item" id="saveSearch-button" class="button" /></div></div>');
            $('#saveSearch-modal').dialog({
                autoOpen: false,
                draggable: false,
                modal: true,
                resizable: false,
                show: 'fade',
                title: 'Save to My Pearlfinders',
                width: 460
            });
        }
    }
    $('a#saveSearch').each(function () {
        $(this).click(function () {
            showSaveSearch();
            return false;
        });
    })
});
///////

//  Table column striping
function tableColumns() {
    $('td:nth-child(odd)').addClass('odd-col');
}
$(function() {
    if ( $('table.stripe-columns').length > 0 ) {
        tableColumns();
    }
});

//  Overlabelling
function overlabel() {
    $('#account span.overlabel').each(function () {
        var container = $(this);
        $('input', container).focus(function () {
            $('label', container).hide();
        });
        $('input', container).blur(function () {
            if ($(this).val() == "") {
                $('label', container).show();
            }
        });
        $('label', container).click(function () {
            $('input', container).focus();
        });
    });
}
$(function() {
    if ($('#account span.overlabel').length > 0 ) {
        overlabel();
    }
});

//  Splash screen
function splash() {
    $('#slide-us').hide();
    $('#slide-asia').hide();
    $('#splash .slices').show();
    $('#splash .slices img').each( function() {
        $(this).hide();
    });
    $('#slides nav a').each( function() {
        var link = $(this);
        var slice = '#slice-' + link.attr('id');
        link.hover(
            //over
            function() {
                $(slice).fadeIn();
                if (slice == '#slice-us') {
                    $('#slide-generic').hide();
                    $('#slide-us').fadeIn('slow');
                }
		if (slice == '#slice-asia') {
                    $('#slide-generic').hide();
                    $('#slide-asia').fadeIn('slow');
		}
            },
            //off
            function() {
                $(slice).fadeOut('fast');
                if (slice == '#slice-us') {
                    $('#slide-us').hide();
                    $('#slide-generic').fadeIn('slow');
                }
                if (slice == '#slice-asia') {
                    $('#slide-asia').hide();
                    $('#slide-generic').fadeIn('slow');
                }		
            }
        );
    });
}
$(function () {
    if ($('#splash').length > 0) {
        splash();
    }
});

//  Dashboard search tabs
function dashboardSearch() {
    $('#search-bar.dashboard #search-tabs a').each(function () {
        $(this).click(function () {
            $('#search-bar.dashboard #search-tabs a').removeClass('current');
            $(this).addClass('current');
            $('#search-company input[type=hidden]').val($(this).text());
            return false;
        });
    });
}
$(function () {
    if ($('#main.dashboard').length > 0) {
        dashboardSearch();
    }
});

//  Report search tabs
function reportSearch() {
    $('nav.tabs a').each(function () {
        $(this).click(function () {
            $('nav.tabs a').removeClass('current');
            $(this).addClass('current');
            $('input[type=submit]').hide();
            $('input[type=submit].' + $(this).attr('id')).show();
            if ($('input[type=hidden].formType').length > 0) {
                $('input[type=hidden].formType').val($(this).attr('id'));
            }
            return false;
        });
    });
}
$(function () {
    if ($('#main.report-search').length > 0) {
        reportSearch();
    }
});

// Checkbox chicanery
$(function () {
    $('span.chkPortfolioAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkPortfolio').attr('checked', false);
        }
    });
    $('input.chkPortfolio').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkPortfolioAll input').attr('checked', false);
        }
    });
    $('span.chkSectorAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkSector').attr('checked', false);
        }
    });
    $('input.chkSector').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkSectorAll input').attr('checked', false);
        }
    });
    $('span.chkDisciplineAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkDiscipline').attr('checked', false);
        }
    });
    $('input.chkDiscipline').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkDisciplineAll input').attr('checked', false);
        }
    });
    $('span.chkContactsAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkContacts').attr('checked', false);
        }
    });
    $('input.chkContacts').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkContactsAll input').attr('checked', false);
        }
    });
    $('span.chkReportsAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkReports').attr('checked', false);
        }
    });
    $('input.chkReports').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkReportsAll input').attr('checked', false);
        }
    });
    $('span.chkFinancialAll input').click(function () {
        if ($(this).attr('checked')) {
            $('input.chkFinancial').attr('checked', false);
        }
    });
    $('input.chkFinancial').click(function () {
        if ($(this).attr('checked')) {
            $('span.chkFinancialAll input').attr('checked', false);
        }
    });
});

//  Dashboard Drag & Drop
function dndLatestReports() {
    $('#latest-reports li').draggable({
        cursor: 'move',
        helper: 'clone',
        scope: '#latest-reports li'
    });
    $('#my-account li').droppable({
        activeClass: 'ui-droppable-scope',
        hoverClass: 'ui-droppable-highlight',
        scope: '#latest-reports li',
        tolderance: 'pointer',
        drop: function (event, ui) {
            var qs = 'pfid=' + ui.draggable.attr('id') + '&';
            qs += 'colleague=' + $(this).attr('id');
            $.get('Dashboard/share.aspx', qs);
        }
    });
}
$(function() {
    if ( $('#main.dashboard').length > 0 ) {
        dndLatestReports();
    }
});

// Company Synopsis
function showSynopsis() {
    $('#companyOverview').hide();
}
$(function () {
    if ($('#companySynopsis').length > 0) {
        showSynopsis();
        $('a#lnkSynopsis').click(function () {
            $('#companySynopsis').hide();
            $('#companyOverview').show();
        });
    }
});

// Sector Synopsis
function showSectorSynopsis() {
    $('#sectorOverview').hide();
}
$(function () {
    if ($('#sectorSynopsis').length > 0) {
        showSectorSynopsis();
        $('a#lnkSynopsis').click(function () {
            $('#sectorSynopsis').hide();
            $('#sectorOverview').show();
        });
    }
});

// Sector Controls
$(function () {
    $('select.sectorList').click(function () {
        $('select.disciplineList option:selected').removeAttr("selected");
    });
    $('select.disciplineList').click(function () {
        $('select.sectorList option:selected').removeAttr("selected");
    });
});

/*
// Scrolling panels
function scrollingPanels() {
    $('div.scroller').each(function () {
        var id = $(this).attr('id');
        $('div#' + id + '-scroller').SetScroller({
            velocity: 30,
            direction: 'vertical',
            onmouseout: 'pause'
        });
    });
    $('#home-clients-up').click(function () {
        $('div.scroller').each(function () {
            var id = $(this).attr('id');
            $('div#' + id + '-scroller').PauseScroller();
        });
    });
    $('#home-clients-down').click(function () {
        $('div.scroller').each(function () {
            var id = $(this).attr('id');
            $('div#' + id + '-scroller').PauseScroller();
        });
    });


}
$(function () {
    if ($('div.scroller').length > 0) {
        scrollingPanels();
    }
});
*/

var dont_show_warning = false;

function dontShowWarning() {
    dont_show_warning = true;
}

function closePageWarning() {

    if (!$("input[type=checkbox]").is(":checked"))
    {
        return;
    }

    // Bail if one of the submits was clicked
    if (dont_show_warning) {
        return;
    }

    return 'If you leave the page your selections will not be saved. Click Cancel to stay on this page.';
}

function urlencode(str) {
    // URL-encodes string  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/urlencode    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();
    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
} 

