/*
(C) www.dhtmlgoodies.com, September 2005

Version 1.2, November 8th - 2005 - Added <iframe> background in IE
Version 1.3, November 12th - 2005 - Fixed top bar position in Opera 7
Version 1.4, December 28th - 2005 - Support for Spanish and Portuguese

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

*/

var EXTRAYEARS = 3;

var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];

var inputYear;
var inputMonth
var inputDay;

var currentSelectedMonth;
var currentSelectedYear;

var returnDateToYear;
var returnDateToMonth;
var returnDateToDay;

var today;

var MSIE = false;
var Opera = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)MSIE=true;
if(navigator.userAgent.indexOf('Opera')>=0)Opera=true;
var iframeObj = false;

function displayCalendar(yearInput,monthInput,dayInput,inputObj)
{
    inputYear = yearInput.options[yearInput.selectedIndex].value;
    inputMonth = monthInput.options[monthInput.selectedIndex].value/1 - 1;
    inputDay = dayInput.options[dayInput.selectedIndex].value/1;

    currentSelectedMonth = inputMonth;
    currentSelectedYear = inputYear;

    returnDateToYear = yearInput;
    returnDateToMonth = monthInput;
    returnDateToDay = dayInput;

    var calendarDiv = document.getElementById('calendarDiv');
    if(!calendarDiv){
        initCalendar();
        calendarDiv = document.getElementById('calendarDiv');
    }
    else
    {
        writeCalendarContent();
    }

    positionCalendar(inputObj);
    calendarDiv.style.visibility = 'visible';
    calendarDiv.style.display = 'block';
    if(iframeObj)
    {
        iframeObj.style.display = '';
        iframeObj.style.height = '140px';
        iframeObj.style.width = '195px';
    }

    updateYearDiv();
    updateMonthDiv();

    logClick();
}

function logClick()
{
    var logReq;
    var url = "/loginfo/logCalendar";
    if ( window.XMLHttpRequest )
    {
       logReq = new XMLHttpRequest();
    }
    else if ( window.ActiveXObject )
    {
       logReq = new ActiveXObject( "Microsoft.XMLHTTP" );
   }

   logReq.open( "GET", url, true );
   logReq.send( null );
}



function initCalendar()
{
    today = new Date();

    var placeholder;
    // var placeholder = document.getElementById('placeHolder');
    if (!document.getElementById('placeHolder')) {
        placeholder = document.createElement('div');
        placeholder.setAttribute('id', 'placeHolder');
        document.body.appendChild(placeholder);
    } else {
        placeholder = document.getElementById('placeHolder');
    }

    if(MSIE)
    {
        iframeObj = document.createElement('IFRAME');
        iframeObj.style.position = 'absolute';
        iframeObj.border='0px';
        iframeObj.style.border = '0px';
        iframeObj.style.backgroundColor = '#FF0000';
        placeholder.appendChild(iframeObj);
    }

    var calendarDiv = document.createElement('DIV');
    calendarDiv.id = 'calendarDiv';
    calendarDiv.style.zIndex = 1000;
    placeholder.appendChild( calendarDiv );

    writeTopBar();

    if(!currentSelectedYear)
    {
        currentSelectedMonth = today.getMonth();
        currentSelectedYear = today.getFullYear();
    }

    writeCalendarContent();
}


function updateYearDiv()
{
    var div = document.getElementById('yearDropDown');
    var yearItems = div.getElementsByTagName('DIV');
    var startYear = today.getFullYear();
    for(var no=0;no<yearItems.length;no++){
        if(currentSelectedYear==(today.getFullYear() + no))
        {
            yearItems[no].className='highlighted';
            activeSelectBoxYear = yearItems[no];
        }
        else
        {
            yearItems[no].className='';
        }
    }
}

function updateMonthDiv()
{
    for(no=0;no<12;no++)
    {
        document.getElementById('monthDiv_' + no).className = '';
    }
    document.getElementById('monthDiv_' + currentSelectedMonth).className='highlighted';
    activeSelectBoxMonth = 	document.getElementById('monthDiv_' + currentSelectedMonth);
}

/** Create header **/

function writeTopBar()
{
    var topBar = document.createElement('DIV');
    topBar.className = 'topBar';
    topBar.id = 'topBar';

    var calendarDiv = document.getElementById('calendarDiv');
    calendarDiv.appendChild(topBar);

    // Left arrow
    var leftDiv = document.createElement('DIV');
    leftDiv.style.marginRight = '1px';
    var img = document.createElement('IMG');
    img.src = 'http://www.ucch.org/images/left.gif';
    img.onmouseover = highlightArrow;
    img.onclick = switchMonth;
    img.onmouseout = highlightArrow;
    leftDiv.appendChild(img);
    topBar.appendChild(leftDiv);
    if(Opera)leftDiv.style.width = '16px';

    // Right arrow
    var rightDiv = document.createElement('DIV');
    rightDiv.style.marginRight = '1px';
    var img = document.createElement('IMG');
    img.src = 'http://www.ucch.org/images/right.gif';
    img.onclick = switchMonth;
    img.onmouseover = highlightArrow;
    img.onmouseout = highlightArrow;
    rightDiv.appendChild(img);
    if(Opera)rightDiv.style.width = '16px';
    topBar.appendChild(rightDiv);


    // Month selector
    var monthDiv = document.createElement('DIV');
    monthDiv.id = 'monthSelect';
    monthDiv.onmouseover = highlightSelect;
    monthDiv.onmouseout = highlightSelect;
    monthDiv.onclick = showMonthDropDown;
    var span = document.createElement('SPAN');
    span.innerHTML = monthArray[currentSelectedMonth];
    span.id = 'calendar_month_txt';
    monthDiv.appendChild(span);

    var img = document.createElement('IMG');
    img.src = 'http://www.ucch.org/images/down.gif';
    img.style.position = 'absolute';
    img.style.right = '0px';
    monthDiv.appendChild(img);
    monthDiv.className = 'selectBox';
    if(Opera){
        img.style.cssText = 'float:right;position:relative';
        img.style.position = 'relative';
        img.style.styleFloat = 'right';
    }
    topBar.appendChild(monthDiv);

    var monthPicker = createMonthDiv();
    monthPicker.style.left = '37px';
    monthPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
    monthPicker.style.width ='60px';
    monthPicker.id = 'monthDropDown';

    calendarDiv.appendChild(monthPicker);

    // Year selector
    var yearDiv = document.createElement('DIV');
    yearDiv.onmouseover = highlightSelect;
    yearDiv.onmouseout = highlightSelect;
    yearDiv.onclick = showYearDropDown;
    var span = document.createElement('SPAN');
    span.innerHTML = currentSelectedYear;
    span.id = 'calendar_year_txt';
    yearDiv.appendChild(span);
    topBar.appendChild(yearDiv);

    var img = document.createElement('IMG');
    img.src = 'http://www.ucch.org/images/down.gif';
    yearDiv.appendChild(img);
    yearDiv.className = 'selectBox';

    if(Opera){
        yearDiv.style.width = '50px';
        img.style.cssText = 'float:right';
        img.style.position = 'relative';
        img.style.styleFloat = 'right';
    }

    var yearPicker = createYearDiv();
    yearPicker.style.left = '113px';
    yearPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
    yearPicker.style.width = '35px';
    yearPicker.id = 'yearDropDown';
    calendarDiv.appendChild(yearPicker);

    var img = document.createElement('IMG');
    img.src = 'http://www.ucch.org/images/close.gif';
    img.style.styleFloat = 'right';
    img.onmouseover = highlightClose;
    img.onmouseout = highlightClose;
    img.onclick = closeCalendar;
    topBar.appendChild(img);
    if(!document.all){
        img.style.position = 'absolute';
        img.style.right = '2px';
    }
}

function createMonthDiv()
{
    var div = document.createElement('DIV');
    div.className='monthYearPicker';
    div.id = 'monthPicker';

    for(var no=0;no<monthArray.length;no++)
    {
        var subDiv = document.createElement('DIV');
        subDiv.innerHTML = monthArray[no];
        subDiv.onmouseover = highlightMonthYear;
        subDiv.onmouseout = highlightMonthYear;
        subDiv.onclick = selectMonth;
        subDiv.id = 'monthDiv_' + no;
        subDiv.style.width = '56px';
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
        if(currentSelectedMonth && currentSelectedMonth==no)
        {
            subDiv.className='highlighted';
            activeSelectBoxMonth = subDiv;
        }
    }
    return div;
}


function createYearDiv()
{

    if(!document.getElementById('yearDropDown'))
    {
        var div = document.createElement('DIV');
        div.className='monthYearPicker';
    }
    else
    {
        var div = document.getElementById('yearDropDown');
        var subDivs = div.getElementsByTagName('DIV');
        for(var no=0;no<subDivs.length;no++){
            subDivs[no].parentNode.removeChild(subDivs[no]);
        }
    }

    var startYear = today.getFullYear();

    for(var no=startYear;no<(startYear + EXTRAYEARS);no++)
    {
        var subDiv = document.createElement('DIV');
        subDiv.innerHTML = no;
        subDiv.onmouseover = highlightMonthYear;
        subDiv.onmouseout = highlightMonthYear;
        subDiv.onclick = selectYear;
        subDiv.id = 'yearDiv' + no;
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
        if(currentSelectedYear && currentSelectedYear==no){
            subDiv.className='highlighted';
            activeSelectBoxYear = subDiv;
        }
    }
    return div;
}


/** Create content **/


function writeCalendarContent()
{
    var calendarContentDivExists = true;
    var calendarContentDiv = document.getElementById('calendar_content_div');
    var calendarDiv = document.getElementById('calendarDiv');
    if(!calendarContentDiv)
    {
        calendarContentDiv = document.createElement('DIV');
        calendarContentDiv.id = 'calendar_content_div';
        calendarDiv.appendChild(calendarContentDiv);
        calendarContentDivExists = false;
    }

    var d = new Date();
    d.setMonth(currentSelectedMonth);
    d.setFullYear(currentSelectedYear);
    d.setDate(0);

    document.getElementById('calendar_year_txt').innerHTML = currentSelectedYear;
    document.getElementById('calendar_month_txt').innerHTML = monthArray[currentSelectedMonth];

    var existingTable = calendarContentDiv.getElementsByTagName('TABLE');
    if(existingTable.length>0)
    {
        calendarContentDiv.removeChild(existingTable[0]);
    }

    var calTable = document.createElement('TABLE');
    calTable.cellSpacing = '0';
    calendarContentDiv.appendChild(calTable);
    var calTBody = document.createElement('TBODY');
    calTable.appendChild(calTBody);
    var row = calTBody.insertRow(-1);

    for(var no=0;no<dayArray.length;no++)
    {
        var cell = row.insertCell(-1);
        cell.innerHTML = dayArray[no];
    }
    var row = calTBody.insertRow(-1);


    for(var no=0;no<d.getDay();no++)
    {
        var cell = row.insertCell(-1);
        cell.innerHTML = '&nbsp;';
    }

    var colCounter = d.getDay();
    var daysInMonth = daysInMonthArray[currentSelectedMonth];
    if( daysInMonth==28 )
    {
        if(isLeapYear(currentSelectedYear))daysInMonth=29;
    }

    for(var no=1;no<=daysInMonth;no++)
    {

        d.setDate(no-1);
        if(colCounter>0 && colCounter%7==0)
        {
            var row = calTBody.insertRow(-1);
        }
        var cell = row.insertCell(-1);
        cell.innerHTML = no;

        if ( currentSelectedYear >= today.getFullYear() &&
                ( no >= today.getDate()  ||  currentSelectedMonth > today.getMonth() || currentSelectedYear > today.getFullYear() ) )
        {
            cell.onclick = pickDate;
        }
        else
        {
            cell.className='disabled';
        }

        if(currentSelectedYear==inputYear && currentSelectedMonth == inputMonth && no==inputDay)
        {
            cell.className='highlighted';
        }

        if (colCounter%7 >=5 )
        {
            cell.className=cell.className + ' weekend';
        }
        colCounter++;
    }


    if(!document.all)
    {
        if(calendarContentDiv.offsetHeight)
        {

            document.getElementById('topBar').style.top = calendarContentDiv.offsetHeight + document.getElementById('topBar').offsetHeight -1 + 'px';
            }
        else
        {
            document.getElementById('topBar').style.top = '';
            document.getElementById('topBar').style.bottom = '0px';
        }

    }

    if(iframeObj)
    {
        if(!calendarContentDivExists)setTimeout('resizeIframe()',350);else setTimeout('resizeIframe()',10);
    }
}


function isLeapYear(inputYear)
{
    if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
    return false;
}

/** Element positioning **/

function positionCalendar(inputObj)
{
    var calendarDiv = document.getElementById('calendarDiv');
    calendarDiv.style.left = getleftPos(inputObj) + 'px';
    calendarDiv.style.top = getTopPos(inputObj) + 'px';
    if(iframeObj)
    {
        iframeObj.style.left = calendarDiv.style.left;
        iframeObj.style.top =  calendarDiv.style.top;
    }
}

function getTopPos(inputObj)
{
    var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
    while( (inputObj = inputObj.offsetParent) != null )
    {
        returnValue += inputObj.offsetTop;
    }
    return returnValue;
}

function getleftPos(inputObj)
{
    var returnValue = inputObj.offsetLeft;
    while( (inputObj = inputObj.offsetParent) != null )
    {
        returnValue += inputObj.offsetLeft;
    }
    return returnValue;
}

/** EVENTS **/



function highlightArrow()
{
    if(this.src.indexOf('over')>=0)
    {
        if(this.src.indexOf('left')>=0) this.src = 'http://www.ucch.org/images/left.gif';
        if(this.src.indexOf('right')>=0) this.src = 'http://www.ucch.org/images/right.gif';
    }
    else
    {
        if(this.src.indexOf('left')>=0) this.src = 'http://www.ucch.org/images/left_over.gif';
        if(this.src.indexOf('right')>=0) this.src = 'http://www.ucch.org/images/right_over.gif';
    }
}


function highlightSelect()
{
    if(this.className=='selectBox')
    {
        this.className = 'selectBoxOver';
        this.getElementsByTagName('IMG')[0].src = 'http://www.ucch.org/images/down_over.gif';
    }
    else
    {
        this.className = 'selectBox';
        this.getElementsByTagName('IMG')[0].src = 'http://www.ucch.org/images/down.gif';
    }
}


function highlightClose()
{
    if(this.src.indexOf('over')>=0)
    {
        this.src = 'http://www.ucch.org/images/close.gif';
    }
    else
    {
        this.src = 'http://www.ucch.org/images/close_over.gif';
    }
}



function closeCalendar()
{
    document.getElementById('yearDropDown').style.display='none';
    document.getElementById('monthDropDown').style.display='none';

    var calendarDiv = document.getElementById('calendarDiv');
    calendarDiv.style.display='none';
    if(iframeObj) iframeObj.style.display='none';
    if(activeSelectBoxMonth) activeSelectBoxMonth.className='';
    if(activeSelectBoxYear) activeSelectBoxYear.className='';
}



function pickDate()
{
    var year = currentSelectedYear/1;
    var month = currentSelectedMonth/1;
    var day = this.innerHTML/1;

    for(var no=0;no<returnDateToYear.options.length;no++)
    {
        if(returnDateToYear.options[no].value==year)
        {
            returnDateToYear.selectedIndex=no;
            break;
        }
    }
    for(var no=0;no<returnDateToMonth.options.length;no++)
    {
        if(returnDateToMonth.options[no].value==month + 1)
        {
            returnDateToMonth.selectedIndex=no;
            break;
        }
    }
    for(var no=0;no<returnDateToDay.options.length;no++)
    {
        if(returnDateToDay.options[no].value==day)
        {
            returnDateToDay.selectedIndex=no;
            break;
        }
    }
    if (returnDateToDay.onchange && day != inputDay) returnDateToDay.onchange();
    if (returnDateToMonth.onchange && month != inputMonth) returnDateToMonth.onchange();
    if (returnDateToYear.onchange && year != inputYear) returnDateToYear.onchange();

    closeCalendar();
}

function switchMonth()
{
    if(this.src.indexOf('left')>=0)
    {
        if ( currentSelectedYear > today.getFullYear() || ( currentSelectedYear == today.getFullYear() && currentSelectedMonth > today.getMonth() ) )
        {
            currentSelectedMonth=currentSelectedMonth-1;;
            if(currentSelectedMonth<0)
            {
                currentSelectedMonth=11;
                currentSelectedYear=currentSelectedYear-1;
            }
        }
    }
    else
    {
        if ( currentSelectedYear < (today.getFullYear() + EXTRAYEARS - 1) ||  currentSelectedMonth != 11)
        {
            currentSelectedMonth=currentSelectedMonth/1+1;
            if(currentSelectedMonth>11)
            {
                currentSelectedMonth=0;
                currentSelectedYear=currentSelectedYear/1+1;
            }
         }
    }
    writeCalendarContent();
}

function showYearDropDown()
{
    if(document.getElementById('yearDropDown').style.display=='block')
    {
        document.getElementById('yearDropDown').style.display='none';
    }
    else
    {
        document.getElementById('yearDropDown').style.display='block';
        document.getElementById('monthDropDown').style.display='none';
        updateYearDiv();
    }

}

function selectYear()
{
    document.getElementById('calendar_year_txt').innerHTML = this.innerHTML
    currentSelectedYear = this.innerHTML.replace(/[^\d]/g,'');
    document.getElementById('yearDropDown').style.display='none';
    if(activeSelectBoxYear)
    {
        activeSelectBoxYear.className='';
    }
    activeSelectBoxYear=this;
    this.className='highlighted';
    writeCalendarContent();
}

var activeSelectBoxMonth = false;
var activeSelectBoxYear = false;

function showMonthDropDown()
{
    if(document.getElementById('monthDropDown').style.display=='block')
    {
        document.getElementById('monthDropDown').style.display='none';
    }
    else
    {
        document.getElementById('monthDropDown').style.display='block';
        document.getElementById('yearDropDown').style.display='none';
        updateMonthDiv();
    }
}

function highlightMonthYear()
{
    //if(activeSelectBoxMonth) activeSelectBoxMonth.className='';

    if(this.className=='monthYearActive')
    {
        this.className='';
    }
    else if (this.className=='highlighted monthYearActive')
    {
        this.className=='highlighted'
    }
    else if (this.className=='highlighted')
    {
        this.className=='highlighted monthYearActive'
    }
    else
    {
        this.className ='monthYearActive';
        activeSelectBoxMonth = this;
    }
}


function selectMonth()
{
    document.getElementById('calendar_month_txt').innerHTML = this.innerHTML
    currentSelectedMonth = this.id.replace(/[^\d]/g,'');

    document.getElementById('monthDropDown').style.display='none';
    for(var no=0;no<monthArray.length;no++)
    {
        document.getElementById('monthDiv_'+no).className='';
    }
    this.className='highlighted';
    activeSelectBoxMonth = this;
    writeCalendarContent();
}

/** IE fixes **/


function cancelCalendarEvent()
{
    return false;
}

function resizeIframe()
{
    iframeObj.style.width = calendarDiv.offsetWidth + 'px';
    iframeObj.style.height = calendarDiv.offsetHeight + 'px' ;
}// JavaScript Document
