﻿function init()
{    
    setupLayout();
    enableTabs();
    setupRollovers();   
    setupCalendar();

    window.onresize = windowOnResize;
}

function windowOnResize()
{
        scwShow(document.getElementById('ctl00_rightCalendarDate'), document.getElementById('ctl00_rightCalendarDate'))    
}

function setupLayout()
{
    var left = document.getElementById('left');
    var middle = document.getElementById('middle');
    
    if (left && middle)
    {
        if (left.offsetHeight < middle.offsetHeight)
        {
            left.style.height = parseInt(middle.offsetHeight) - 8 + "px";
        }
        else
        {
            middle.style.height = parseInt(left.offsetHeight) - 8 + "px";
        }
    }
}

function setupRollovers()
{
    var imgs = document.getElementsByTagName("IMG");
    
    for (var i = 0; i < imgs.length; i++)
    {
        var img = imgs[i];

        // set current button on
        
        if (img.parentNode && img.parentNode.href && (unescape(img.parentNode.href) == unescape(location.href)))
        {
            img.src = img.src.replace('_off', '_on');
        }
        else if (img.src.match(/.*_off/))
        {        
            img.onmouseover = rollOn;
            img.onmouseout = rollOff;
        }
        
    }
}


function rollOn()
{
    this.src = this.src.replace(/_off/, '_on');
}

function rollOff()
{
    this.src = this.src.replace(/_on/, '_off');
}

function updateLabel(destId, textbox)
{
    var val = textbox.value;
    
    val = val.replace(/[^a-zA-Z0-9\-]/g, '').toLowerCase();
 
    textbox.value = val;   
    document.getElementById(destId).innerHTML = val;
}

function clearSearchBox(box)
{
    // clear on focus
    if (box.value.length > 0 && box.value.substr(0, 1) == '[')   
    {
        box.value = '';
    }
        
    box.style.color = '';
    
}

function addTag(tag, id)
{
    var o = document.getElementById(id);
    
    if (o)
    {
        if (o.value.length > 0)
            o.value += ' ';
          
        if (tag.indexOf(' ') >= 0)  
            o.value += '"' + tag + '"';
        else
            o.value += tag;
    }
}

function setupCalendar()
{
    if (document.getElementById('ctl00_rightCalendarDate'))
    {
        scwNextAction=calendarOnClick;//.runsAfterSCW(this)
        scwShow(document.getElementById('ctl00_rightCalendarDate'), document.getElementById('ctl00_rightCalendarDate'))
    }
}

function calendarOnClick(obj)
{
    var d8 = document.getElementById('ctl00_rightCalendarDate').innerHTML;
    setupCalendar(); // re-display
    
    var link = 'Events.aspx?pv=day&df=' + escape(d8);
    
    if (location.href.toLowerCase().indexOf('public/') >= 0)
        link = '../pages/' + link;
    else if (location.href.toLowerCase().indexOf('account/') >= 0)
        link = '../pages/' + link;
    else if (location.href.toLowerCase().indexOf('pages/') < 0)
        link = 'pages/' + link;
        
    location.href = link;
}

function setDate(obj)
{
    // if dateTo is before dateFrom, make it an hour later
    var day1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_day');
    var month1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_month');
    var year1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_year');
    var hour1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_hour');
    var minute1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_minute');
    var amPm1 = document.getElementById('ctl00_pageContent_FormView1_DateFrom_amPm');
    
    var d81 = new Date(parseInt(year1.options[year1.selectedIndex].text), month1.selectedIndex, day1.selectedIndex + 1, hour1.selectedIndex + (amPm1.selectedIndex == 1 ? 12 : 0), minute1.selectedIndex * 5, 0);

    var day2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_day');
    
    if (day2)
    {
        var month2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_month');
        var year2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_year');
        var hour2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_hour');
        var minute2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_minute');
        var amPm2 = document.getElementById('ctl00_pageContent_FormView1_DateTo_amPm');
        
        var d82 = new Date(parseInt(year2.options[year2.selectedIndex].text), month2.selectedIndex, day2.selectedIndex + 1, hour2.selectedIndex + (amPm2.selectedIndex == 1 ? 12 : 0), minute2.selectedIndex * 5, 0);

        if (d82 < d81)
        {
            day2.selectedIndex = day1.selectedIndex;
            month2.selectedIndex = month1.selectedIndex;
            year2.selectedIndex = year1.selectedIndex;            
            hour2.selectedIndex = Math.min(11, hour1.selectedIndex + 1);                
            minute2.selectedIndex = minute1.selectedIndex;
            amPm2.selectedIndex = amPm1.selectedIndex;
        }
     }   
}