// SYSTEM FUNCTIONS //
function printPage() {
    if (window.print) window.print();
    else alert('Sorry, this feature is not available on your browser.\nPlease use your browser\'s print button.');
}

function sendToFriend(url) {
    url += '&pageurl=' + escape(window.location.href);
    var top = window.screen.height / 2 - 400 / 2;
    var left = window.screen.width / 2 - 400 / 2;
    var win = window.open(url, 'sendtofriend', 'top=' + top + ',left=' + left + ',width=400,height=400,scrollbars=1,resizable=1');
    win.opener = window;
    win.focus();
}

function showGroupChannel(channelId, channelName, channelUrl, linkTarget, navLocation, bgColor, textColor, dividerColor, isLast) {
    if (getCookie('chList') && getCookie('chList').indexOf(channelId) != -1) {
        if (dividerColor != '')
            document.write('<div class="spacer" style="background:' + dividerColor + '"> </div>');
        else
            document.write('<div class="spacer"> </div>');

        if (bgColor != '')
            document.write('<div class="' + navLocation + 'nav" style="background:' + bgColor + '">');
        else
            document.write('<div class="' + navLocation + 'nav">');

        if (textColor != '')
            document.write('<a style="color:' + textColor + '" target="' + linkTarget + '" href="' + channelUrl + '">' + channelName + '</a></div>');
        else
            document.write('<a target="' + linkTarget + '" href="' + channelUrl + '">' + channelName + '</a></div>');

        if (isLast) {
            if (dividerColor)
                document.write('<div class="spacer" style="background:' + dividerColor + '"> </div>');
            else
                document.write('<div class="spacer"> </div>');
        }
    }
}

function showGroupSubChannel(channelId, channelName, channelUrl, linkTarget) {
    if (getCookie('chList') && getCookie('chList').indexOf(channelId) != -1)
        document.write('<a target="' + linkTarget + '" href="' + channelUrl + '">' + channelName + '</a>');
    else
        document.write('&nbsp;');
}

function showRegisterSignIn(showWelcome, showRegister, showSignIn, baseUrl, channelUrl) {
    var userName = getCookie('access_user') || getCookie('extend_user');
    if (userName && showWelcome)
        document.write('<span id="welcome">Welcome ' + userName + ' </span>');
    document.write('<span id="signin">');
    if (userName) {
        document.write('<a href="' + baseUrl + '_profile?returnurl=' + channelUrl + '">Profile</a> | ');
        document.write('<a href="' + baseUrl + '_signout?returnurl=' + channelUrl + '">Sign Out</a>');
    } else {
        if (showRegister)
            document.write('<a href="' + baseUrl + '_register?returnurl=' + channelUrl + '">Register</a>');
        if (showRegister && showSignIn)
            document.write(' | ');
        if (showSignIn)
            document.write('<a href="' + baseUrl + '_signin?returnurl=' + channelUrl + '">Sign In</a>');
    }
    document.write('</span>');
}

function validateSearchText() {
    var srchtxtbox = document.getElementById('searchtextbox');
    if (srchtxtbox.value == '') {
        alert('Please enter one or more search terms.');
        srchtxtbox.focus();
        return false;
    }
    return true;
}

function doSearch(searchpath) {
    if (validateSearchText()) {
        var valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ';
        var searchText = '';
        var temp = document.getElementById('searchtextbox').value;
        for (i = 0; i < temp.length; i++)
            if (valid.indexOf(temp.charAt(i)) > -1)
            searchText += temp.charAt(i);
        location.href = searchpath + '?q=' + searchText;
    }
}

function onSearchEnter(evt, searchUrl) {
    var keyCode = null;
    if (evt.which)
        keyCode = evt.which;
    else if (evt.keyCode)
        keyCode = evt.keyCode;
    if (keyCode == 13) {
        doSearch(searchUrl);
        return false;
    }
    return true;
}

function getCookie(name) {
    var arg = name + '=';
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(' ', i) + 1;
        if (i == 0) break;
    }
    return null;
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(';', offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return document.cookie.substring(offset, endstr);
}

// TEMPLATE FUNCTIONS //
function bookmarkPage() {
    if (document.all) window.external.AddFavorite(window.location.href, document.title);
    else if (window.sidebar) window.sidebar.addPanel(document.title, window.location.href, '');
}

function getDay() {
    var word_day = new Array('Sunday','Monday','Tuesday', 'Wednesday','Thursday','Friday','Saturday');
    var right_now = new Date();
    return word_day[right_now.getDay()];
}

function getDate() {
    var word_month = new Array('January ', 'February ', 'March ', 'April ', 'May ', 'June ', 'July ', 'August ', 'September ', 'October ', 'November ', 'December ')
    var right_now = new Date();
    var right_year = right_now.getYear();
    if (right_year < 2000) right_year = right_year + 1900;
    return word_month[right_now.getMonth()] + right_now.getDate() + ', ' + right_year;
}

function getTime() {
    var right_now = new Date();
    var time;
    var hours = right_now.getHours();
    if (hours >= 12) time = 'PM';
    else time = 'AM';
    if (hours > 12) hours -= 12;
    if (hours == 0) hours = 12;
    var mins = right_now.getMinutes();
    if (mins < 10) mins = '0' + mins;
    return hours + ':' + mins + ' ' + time;
}

// GADGET FUNCTIONS //
function getGadgetUrl(obj) {
    if (obj.src.indexOf('?') == -1) {
        obj.src += '?context=extend';
        if (login = getCookie('extend_login'))
            obj.src += '&login=' + login;
    }
}

// CALENDAR FUNCTIONS //
// TODO: Move these to the calendar control so they're only loaded when needed
function adjustCalendar(calendarViewId, prePostDate, direction) {
    var workingDate = new Date(prePostDate);
    var day = workingDate.getDate();
    var month = workingDate.getMonth() + 1;
    var year = workingDate.getFullYear();

    if (direction == -1) {
        if (month == 1) {
            month = 12;
            year--;
        }
        else
            month--;
    }
    else if (direction == 1) {
        if (month == 12) {
            month = 1;
            year++;
        }
        else
            month++;
    }

    $.get('/controls/calendarofevents_loader.aspx?CalendarViewId=' + calendarViewId + '&EventId=0&WorkingDate=' + month + '/01/' + year + '&Action=ShowCalendar',
        function(response) { $('#ucCalendarOfEvents' + calendarViewId + '_divCalendar').html(response); }
    );
}

function showTodaysEvents(calendarViewId, eventDate) {
    var workingDate = new Date(eventDate);
    var day = workingDate.getDate();
    var month = workingDate.getMonth() + 1;
    var year = workingDate.getFullYear();

    $.get('/controls/calendarofevents_loader.aspx?CalendarViewId=' + calendarViewId + '&EventId=0&WorkingDate=' + month + '/' + day + '/' + year + '&Action=ShowTodaysEvents',
        function(response) { $('#ucCalendarOfEvents' + calendarViewId + '_divCalendar').html(response); }
    );
}

function showEvent(calendarViewId, eventId, eventDate) {
    var workingDate = new Date(eventDate);
    var day = workingDate.getDate();
    var month = workingDate.getMonth() + 1;
    var year = workingDate.getFullYear();

    $.get('/controls/calendarofevents_loader.aspx?CalendarViewId=' + calendarViewId + '&EventId=' + eventId + '&WorkingDate=' + month + '/' + day + '/' + year + '&Action=ShowEvent',
        function(response) { $('#ucCalendarOfEvents' + calendarViewId + '_divCalendar').html(response); }
    );
}
