//------------------------------------------------------------
// Javascript Popup Calendar Date Selector
// Created by Amy Rochelle
// 01.24.2009
// Notes:
//  - customizable by CSS
//  - blank.html must exist in same folder as page using this javascript for HTTPs sites
//  - onclick provided for postback in .NET applications -- just include hidden button (id=btn_postback) on same page as javascript
//  - change variable pathToImages value for images to show
//  - change selectBoxHighlightColor value for select box highlight color
//------------------------------------------------------------

// Variables
//------------------------------------------------------------
var pathToImages = '../images/';    // Relative to your HTML file
var selectBoxHighlightColor = '#fff'; // Highlight color of select boxes

var turnOffYearSpan = false;                    // true = only show this Year and next, false = show +/- 5 years
var weekStartsOnSunday = true;        // true = start the week on Sunday, false = start the week on Monday
var showWeekNumber = false;            // true = show week number,  false = do not show week number

// Format of current day at the bottom of the calendar
// [todayString] = the value of todayString
// [dayString] = day of week (examle: mon, tue, wed...)
// [UCFdayString] = day of week (examle: Mon, Tue, Wed...) ( First letter in uppercase)
// [day] = Day of month, 1..31
// [monthString] = Name of current month
// [year] = Current year
var todayStringFormat = '[todayString] [UCFdayString]. [day]. [monthString] [year]';

var calendar_offsetTop = 0;
var calendar_offsetLeft = 0;
var calendarDiv = false;

// browser check
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 monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var monthArrayShort = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var dayArray = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
var weekString = 'Week';
var todayString = 'Today is';

if (weekStartsOnSunday) {
   var tempDayName = dayArray[6];
   for(var theIx = 6; theIx > 0; theIx--) dayArray[theIx] = dayArray[theIx-1];
   dayArray[0] = tempDayName;
}

var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
var currentMonth;
var currentYear;
var calendarContentDiv;
var returnDateTo;
var returnFormat;
var activeSelectBoxMonth;
var activeSelectBoxYear;

var returnDateToYear;
var returnDateToMonth;
var returnDateToDay;
var inputYear;
var inputMonth;
var inputDay;
var selectBoxMovementInProgress = false;
var activeSelectBox = false;

// fix for IE frame problem on dropdowns
var iframeObj = false;
var iframeObj2 =false;

var activeSelectBoxMonth = false;
var activeSelectBoxDirection = false;
//--------------------------------------------------------------


function IE_fix(obj) {
        if(!iframeObj2) return;
        iframeObj2.style.display = 'block';
        iframeObj2.style.height =document.getElementById(obj).offsetHeight+1;
        iframeObj2.style.width=document.getElementById(obj).offsetWidth;
        iframeObj2.style.left=getLeftPos(document.getElementById(obj))+1-calendar_offsetLeft;
        iframeObj2.style.top=getTopPos(document.getElementById(obj))-document.getElementById(obj).offsetHeight-calendar_offsetTop;
}

function IE_hideFrame() {        
    if(iframeObj2) iframeObj2.style.display = 'none';
}

function cancelCalendarEvent() {
    return false;
}

function isLeapYear(inputYear) {
    if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
    return false;
}

function highlightMonthYear() {
    if(activeSelectBoxMonth)activeSelectBoxMonth.className='';
    activeSelectBox = this;

    if(this.className=='monthYearActive') {
        this.className='';
    } else {
        this.className = 'monthYearActive';
        activeSelectBoxMonth = this;
    }

    if(this.innerHTML.indexOf('-')>=0 || this.innerHTML.indexOf('+')>=0){
        if(this.className=='monthYearActive')
            selectBoxMovementInProgress = true;
        else
            selectBoxMovementInProgress = false;
        if(this.innerHTML.indexOf('-')>=0)activeSelectBoxDirection = -1; else activeSelectBoxDirection = 1;
    } else selectBoxMovementInProgress = false;
}

function showMonthDropDown() {
    if(document.getElementById('monthDropDown').style.display=='block') {
        document.getElementById('monthDropDown').style.display='none';
        IE_hideFrame(); // fix for IE frame problem on dropdowns
    } else {
        document.getElementById('monthDropDown').style.display='block';
        document.getElementById('yearDropDown').style.display='none';
        if (MSIE) IE_fix('monthDropDown');
    }
}

function showYearDropDown() {
    if(document.getElementById('yearDropDown').style.display=='block') {
        document.getElementById('yearDropDown').style.display='none';
        IE_hideFrame(); //fix for IE frame problem on dropdowns
    } else {
        document.getElementById('yearDropDown').style.display='block';
        document.getElementById('monthDropDown').style.display='none';
        if (MSIE) IE_fix('yearDropDown');
    }
}

function selectMonth() {
    document.getElementById('calendar_month_txt').innerHTML = this.innerHTML
    currentMonth = this.id.replace(/[^\d]/g,'');
    document.getElementById('monthDropDown').style.display='none';
    IE_hideFrame(); // fix for IE frame problem on dropdowns
    for(var no=0;no<monthArray.length;no++) {
        document.getElementById('monthDiv_'+no).style.color='';
    }
    this.style.color = selectBoxHighlightColor;
    activeSelectBoxMonth = this;
    writeCalendarContent();
}

function selectYear() {
    document.getElementById('calendar_year_txt').innerHTML = this.innerHTML
    currentYear = this.innerHTML.replace(/[^\d]/g,'');
    document.getElementById('yearDropDown').style.display='none';
    IE_hideFrame();// fix for IE frame problem on dropdowns
    if(activeSelectBoxYear) {
        activeSelectBoxYear.style.color='';
    }
    activeSelectBoxYear=this;
    this.style.color = selectBoxHighlightColor;
    writeCalendarContent();
}

function switchMonth() {
    if(this.src.indexOf('left')>=0) {
        currentMonth=currentMonth-1;
        if(currentMonth<0) {
            currentMonth=11;
            currentYear=currentYear-1;
            document.getElementById('calendar_year_txt').innerHTML = currentYear;
        }
    } else {
        currentMonth=currentMonth+1;
        if(currentMonth>11) {
            currentMonth=0;
            currentYear=currentYear/1+1;
            document.getElementById('calendar_year_txt').innerHTML = currentYear;
        }
    }
    document.getElementById('calendar_month_txt').innerHTML = monthArray[currentMonth];
    writeCalendarContent();
}

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.onclick = selectMonth;
        subDiv.id = 'monthDiv_' + no;
        subDiv.style.width = '56px';
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
        if(currentMonth && currentMonth==no){
            subDiv.style.color = selectBoxHighlightColor;
            activeSelectBoxMonth = subDiv;
        }
    }
    return div;
}

function changeSelectBoxYear(e,inputObj) {
    if(!inputObj)inputObj =this;
    var yearItems = inputObj.parentNode.getElementsByTagName('div');
    if(inputObj.innerHTML.indexOf('-')>=0) {
        var startYear = yearItems[1].innerHTML/1 -10;
        if(activeSelectBoxYear) {
            activeSelectBoxYear.style.color='';
        }
    } else {
        var startYear = yearItems[1].innerHTML/1 +10;
        if(activeSelectBoxYear) {
            activeSelectBoxYear.style.color='';
        }
    }

    for(var no=1;no<yearItems.length-1;no++) {
        yearItems[no].innerHTML = startYear+no-1;
        yearItems[no].id = 'yearDiv' + (startYear/1+no/1-1);
    }
    
    if(activeSelectBoxYear) {
        activeSelectBoxYear.style.color='';
        if(document.getElementById('yearDiv'+currentYear)) {
            activeSelectBoxYear = document.getElementById('yearDiv'+currentYear);
            activeSelectBoxYear.style.color=selectBoxHighlightColor;
        }
    }
}

function updateYearDiv() {
    var yearSpan = 5;
    if (turnOffYearSpan) yearSpan = 0;
    var div = document.getElementById('yearDropDown');
    var yearItems = div.getElementsByTagName('div');
    for(var no=1;no<yearItems.length-1;no++) {
        yearItems[no].innerHTML = currentYear/1 -yearSpan + no;
        if(currentYear==(currentYear/1 -yearSpan + no)) {
            yearItems[no].style.color = selectBoxHighlightColor;
            activeSelectBoxYear = yearItems[no];
        } else {
            yearItems[no].style.color = '';
        }
    }
}

function updateMonthDiv() {
    for(no=0;no<12;no++) {
        document.getElementById('monthDiv_' + no).style.color = '';
    }
    document.getElementById('monthDiv_' + currentMonth).style.color = selectBoxHighlightColor;
    activeSelectBoxMonth =     document.getElementById('monthDiv_' + currentMonth);
}

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 d = new Date();
    if(currentYear) d.setFullYear(currentYear);
    var startYear = d.getFullYear()/1 - 5;
    var yearSpan = 10;
    if (! turnOffYearSpan) {
        var subDiv = document.createElement('div');
        subDiv.innerHTML = '&nbsp;&nbsp;- ';
        subDiv.onclick = changeSelectBoxYear;
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
    } else {
       startYear = d.getFullYear()/1 - 0;
       yearSpan = 2;
    }

    for(var no=startYear;no<(startYear+yearSpan);no++) {
        var subDiv = document.createElement('div');
        subDiv.innerHTML = no;
        subDiv.onclick = selectYear;
        subDiv.id = 'yearDiv' + no;
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
        if(currentYear && currentYear==no) {
            subDiv.style.color = selectBoxHighlightColor;
            activeSelectBoxYear = subDiv;
        }
    }
    if (! turnOffYearSpan) {
        var subDiv = document.createElement('div');
        subDiv.innerHTML = '&nbsp;&nbsp;+ ';
        subDiv.onclick = changeSelectBoxYear;
        subDiv.onselectstart = cancelCalendarEvent;
        div.appendChild(subDiv);
    }
    return div;
}

function closeCalendar() {
    calendarDiv.style.display='none';
    if(iframeObj){
        iframeObj.style.display='none';
        IE_hideFrame();// fix for IE frame problem on dropdowns
    }
    if(activeSelectBoxMonth)activeSelectBoxMonth.className='';
    if(activeSelectBoxYear)activeSelectBoxYear.className='';
}

function writeTopBar()
{
    var topBar = '';
    topBar = document.createElement('div');
    topBar.className = 'topBar';
    topBar.id = 'topBar';
    calendarDiv.appendChild(topBar);
    
    // Left arrow
    var leftDiv = document.createElement('div');
    leftDiv.className = 'arrow';
    var img = document.createElement('img');
    img.src = pathToImages + 'cal_left.gif';
    img.onclick = switchMonth;
    leftDiv.appendChild(img);
    topBar.appendChild(leftDiv);
    if(Opera)leftDiv.style.width = '16px';

        // Right arrow
    var rightDiv = document.createElement('div');
    rightDiv.className = 'arrow';
    var img = document.createElement('IMG');
    img.src = pathToImages + 'cal_right.gif';
    img.onclick = switchMonth;
    rightDiv.appendChild(img);
    if(MSIE)
    if(Opera)rightDiv.style.width = '16px';
    topBar.appendChild(rightDiv);
    
    // Month selector
    var monthDiv = document.createElement('div');
    monthDiv.id = 'monthSelect';
    monthDiv.onclick = showMonthDropDown;
    var span = document.createElement('span');
    span.innerHTML = monthArray[currentMonth];
    span.id = 'calendar_month_txt';
    monthDiv.appendChild(span);
    var img = document.createElement('img');
    img.src = pathToImages + 'cal_down.gif';
    img.style.position = 'absolute';
    img.style.right = '0px';
    monthDiv.appendChild(img);
    monthDiv.className = 'selectBox';
    monthDiv.style.height = '15px';
    monthDiv.style.width = '77px';
    topBar.appendChild(monthDiv);
    var monthPicker = createMonthDiv();
    monthPicker.style.left = '33px';
    monthPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight -1 + 'px';
    monthPicker.style.width ='80px';
    monthPicker.id = 'monthDropDown';
    calendarDiv.appendChild(monthPicker);

    // Year selector
    var yearDiv = document.createElement('div');
    yearDiv.onclick = showYearDropDown;
    var span = document.createElement('span');
    span.innerHTML = currentYear;
    span.id = 'calendar_year_txt';
    yearDiv.appendChild(span);
    var img = document.createElement('img');
    img.src = pathToImages + 'cal_down.gif';
    img.style.position = 'absolute';
    img.style.right = '0px';
    yearDiv.appendChild(img);
    yearDiv.className = 'selectBox';
    yearDiv.style.height = '15px';
    yearDiv.style.width = '44px';
    topBar.appendChild(yearDiv);
    var yearPicker = createYearDiv();
    yearPicker.style.left = '116px';
    yearPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight - 1 + 'px';
    yearPicker.style.width = '47px';
    yearPicker.id = 'yearDropDown';
    calendarDiv.appendChild(yearPicker);

    // close
    var closeDiv = document.createElement('div');
    closeDiv.style.styleFloat = 'right';
    var imgClose = document.createElement('img');
    imgClose.src = pathToImages + 'cal_close.gif';
    imgClose.onclick = closeCalendar;
    closeDiv.appendChild(imgClose);
    topBar.appendChild(closeDiv);
}

function writeCalendarContent() {
    var calendarContentDivExists = true;
    if(!calendarContentDiv) {
        calendarContentDiv = document.createElement('div');
        calendarDiv.appendChild(calendarContentDiv);
        calendarContentDivExists = false;
    }
    currentMonth = currentMonth/1;
    var d = new Date();
    d.setFullYear(currentYear);
    d.setDate(1);
    d.setMonth(currentMonth);

    var dayStartOfMonth = d.getDay();
    if (! weekStartsOnSunday) {
      if(dayStartOfMonth==0)dayStartOfMonth=7;
      dayStartOfMonth--;
   }
    var existingTable = calendarContentDiv.getElementsByTagName('table');
    if(existingTable.length>0) {
        calendarContentDiv.removeChild(existingTable[0]);
    }

    var calTable = document.createElement('table');
    calTable.width = '100%';
    calTable.cellSpacing = '0';
    calendarContentDiv.appendChild(calTable);

    var calTBody = document.createElement('tbody');
    calTable.appendChild(calTBody);
    var row = calTBody.insertRow(-1);
    row.className = 'calendar_week_row';
  if (showWeekNumber) {
      var cell = row.insertCell(-1);
       cell.innerHTML = weekString;
       cell.className = 'calendar_week_column';
       cell.style.backgroundColor = selectBoxRolloverBgColor;
    }

    for(var no=0;no<dayArray.length;no++) {
        var cell = row.insertCell(-1);
        cell.className = 'weekdays';
        cell.innerHTML = dayArray[no];
    }

    var row = calTBody.insertRow(-1);

   if (showWeekNumber) {
       var cell = row.insertCell(-1);
       cell.className = 'calendar_week_column';
       cell.style.backgroundColor = selectBoxRolloverBgColor;
       var week = getWeek(currentYear,currentMonth,1);
       cell.innerHTML = week;    
    }
    
    for(var no=0;no<dayStartOfMonth;no++) {
        var cell = row.insertCell(-1);
        cell.innerHTML = '&nbsp;';
    }

    var colCounter = dayStartOfMonth;
    var daysInMonth = daysInMonthArray[currentMonth];
    if(daysInMonth==28) {
        if(isLeapYear(currentYear)) daysInMonth=29;
    }

    for(var no=1;no<=daysInMonth;no++) {
        d.setDate(no-1);
        if(colCounter>0 && colCounter%7==0) {
            var row = calTBody.insertRow(-1);
         if (showWeekNumber) {
            var cell = row.insertCell(-1);
            cell.className = 'calendar_week_column';
            var week = getWeek(currentYear,currentMonth,no);
            cell.innerHTML = week;
            cell.style.backgroundColor = selectBoxRolloverBgColor;
         }
        }
        
        var cell = row.insertCell(-1);
        if(currentYear==inputYear && currentMonth == inputMonth && no==inputDay) {
            cell.className='activeDay';
        }
        cell.innerHTML = no;
        cell.onclick = pickDate;
        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);
    }
    
    document.getElementById('calendar_month_txt').innerHTML = monthArray[currentMonth];
    document.getElementById('calendar_year_txt').innerHTML =currentYear;
}

function resizeIframe()
{
    iframeObj.style.width = calendarDiv.offsetWidth + 'px';
    iframeObj.style.height = calendarDiv.offsetHeight + 'px' ;
}

function pickTodaysDate()
{
    var d = new Date();
    currentMonth = d.getMonth();
    currentYear = d.getFullYear();
    pickDate(false,d.getDate());
}

function pickDate(e,inputDay)
{
    var month = currentMonth/1 +1;
    if(month<10) month = '0' + month;
    var day;
    if(!inputDay && this) day = this.innerHTML; else day = inputDay;

    if(day/1<10) day = '0' + day;
    if(returnFormat) {
        returnFormat = returnFormat.replace('dd',day);
        returnFormat = returnFormat.replace('mm',month);
        returnFormat = returnFormat.replace('yyyy',currentYear);
        returnFormat = returnFormat.replace('d',day/1);
        returnFormat = returnFormat.replace('m',month/1);

        returnDateTo.value = returnFormat;
        try {
            returnDateTo.onchange();
        } catch(e) {

        }
    } else {
        for(var no=0;no<returnDateToYear.options.length;no++) {
            if(returnDateToYear.options[no].value==currentYear) {
                returnDateToYear.selectedIndex=no;
                break;
            }
        }
        for(var no=0;no<returnDateToMonth.options.length;no++) {
            if(returnDateToMonth.options[no].value==parseInt(month)) {
                returnDateToMonth.selectedIndex=no;
                break;
            }
        }
        for(var no=0;no<returnDateToDay.options.length;no++) {
            if(returnDateToDay.options[no].value==parseInt(day)) {
                returnDateToDay.selectedIndex=no;
                break;
            }
        }
    }
    //used to postback in .NET applications when date is selected
    if(document.getElementById('btn_postback') != null)
        document.getElementById('btn_postback').click();
        
    closeCalendar();
}

function getWeek(year,month,day){
    if (! weekStartsOnSunday) day = (day/1); else day = (day/1)+1;
    year = year /1;
    month = month/1 + 1;
  var a = Math.floor((14-(month))/12);
  var y = year+4800-a;
  var m = (month)+(12*a)-3;
  var jd = day + Math.floor(((153*m)+2)/5) + (365*y) + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) - 32045;
  var d4 = (jd+31741-(jd%7))%146097%36524%1461;
  var L = Math.floor(d4/1460);
  var d1 = ((d4-L)%365)+L;
  NumberOfWeek = Math.floor(d1/7) + 1;
  return NumberOfWeek;
}

function writeBottomBar()
{
    var d = new Date();
    var bottomBar = document.createElement('div');

    bottomBar.id = 'bottomBar';

    bottomBar.style.cursor = 'pointer';
    bottomBar.className = 'todaysDate';
    var todayStringFormat = '[todayString] [dayString], [monthString] [day], [year]';

    var subDiv = document.createElement('div');
    subDiv.onclick = pickTodaysDate;
    subDiv.id = 'todaysDateString';
    subDiv.style.width = (calendarDiv.offsetWidth - 5) + 'px';
    var day = d.getDay();
    if (! weekStartsOnSunday) {
      if(day==0)day = 7;
      day--;
   }

    var bottomString = todayStringFormat;
    bottomString = bottomString.replace('[monthString]',monthArrayShort[d.getMonth()]);
    bottomString = bottomString.replace('[day]',d.getDate());
    bottomString = bottomString.replace('[year]',d.getFullYear());
    bottomString = bottomString.replace('[dayString]',dayArray[day]);
    bottomString = bottomString.replace('[UCFdayString]',dayArray[day]);
    bottomString = bottomString.replace('[todayString]',todayString);

    subDiv.innerHTML = todayString + ': ' + d.getDate() + ', ' + monthArrayShort[d.getMonth()] + ', ' +  d.getFullYear();
    subDiv.innerHTML = bottomString ;
    bottomBar.appendChild(subDiv);

    calendarDiv.appendChild(bottomBar);
}

function getTopPos(inputObj)
{
  var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
  while((inputObj = inputObj.offsetParent) != null) returnValue += inputObj.offsetTop;
  return returnValue + calendar_offsetTop;
 //return returnValue + calendar_offsetTop-140; //move top of calendar up. NOTE: need to change leftPos +20 when moving top up
}

function getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null) returnValue += inputObj.offsetLeft;
  return returnValue + calendar_offsetLeft;
    //return returnValue + calendar_offsetLeft-190;  //right aligns calendar to calendar graphic if tight squeeze
    //return returnValue + calendar_offsetLeft+20; //needed when calendar is moved up to clear calendar graphic
}

function positionCalendar(inputObj)
{
    calendarDiv.style.left = getLeftPos(inputObj) + 'px';
    calendarDiv.style.top = getTopPos(inputObj) + 'px';
    if(iframeObj) {// fix for IE frame problem on dropdowns
        iframeObj.style.left = calendarDiv.style.left;
        iframeObj.style.top =  calendarDiv.style.top;
        iframeObj2.style.left = calendarDiv.style.left;
        iframeObj2.style.top =  calendarDiv.style.top;
    }
}

function initCalendar()
{
    if(MSIE){// fix for IE frame problem on dropdowns
        iframeObj = document.createElement('iframe');
        iframeObj.style.filter = 'alpha(opacity=0)';
        iframeObj.style.position = 'absolute';
        iframeObj.border='0px';
        iframeObj.style.border = '0px';
        iframeObj.style.backgroundColor = '#FF0000';
        iframeObj2 = document.createElement('iframe');
        iframeObj2.style.position = 'absolute';
        iframeObj2.border='0px';
        iframeObj2.style.border = '0px';
        iframeObj2.style.height = '1px';
        iframeObj2.style.width = '1px';
        
        // Added for HTTPS
        iframeObj2.src = 'blank.html';
        iframeObj.src = 'blank.html';
        document.body.appendChild(iframeObj2);
        document.body.appendChild(iframeObj);
    }

    calendarDiv = document.createElement('div');
    calendarDiv.id = 'calendarDiv';
    calendarDiv.style.zIndex = 1000;

    document.body.appendChild(calendarDiv);
    writeBottomBar();
    writeTopBar();

    if(!currentYear){
        var d = new Date();
        currentMonth = d.getMonth();
        currentYear = d.getFullYear();
    }
    writeCalendarContent();
}

function calendarSortItems(a,b)
{
    return a/1 - b/1;
}

function displayCalendar(inputField,format,buttonObj)
{
    var validformat=/^\d{1,2}\/\d{1,2}\/\d{2,4}$; ///Basic check for format validity
    if(inputField.value.length>0 && validformat.test(inputField.value)) { //if value in field is not a date then use today's date

        if(!format.match(/^[0-9]*?$/gi)) {
            var items = inputField.value.split(/[^0-9]/gi);
            var positionArray = new Array();
            positionArray['m'] = format.indexOf('mm');
            if(positionArray['m']==-1) positionArray['m'] = format.indexOf('m');
            positionArray['d'] = format.indexOf('dd');
            if(positionArray['d']==-1) positionArray['d'] = format.indexOf('d');
            positionArray['y'] = format.indexOf('yyyy');

            var positionArrayNumeric = Array();
            positionArrayNumeric[0] = positionArray['m'];
            positionArrayNumeric[1] = positionArray['d'];
            positionArrayNumeric[2] = positionArray['y'];

            positionArrayNumeric = positionArrayNumeric.sort(calendarSortItems);
            var itemIndex = -1;
            for(var no=0;no<positionArrayNumeric.length;no++) {
                if(positionArrayNumeric[no]==-1) continue;
                itemIndex++;
                if(positionArrayNumeric[no]==positionArray['m']) {
                    currentMonth = items[itemIndex]-1;
                    continue;
                }
                if(positionArrayNumeric[no]==positionArray['y']) {
                    currentYear = items[itemIndex];
                    continue;
                }
                if(positionArrayNumeric[no]==positionArray['d']) {
                    tmpDay = items[itemIndex];
                    continue;
                }
            }

            currentMonth = currentMonth / 1;
            tmpDay = tmpDay / 1;
        } else {
            var monthPos = format.indexOf('mm');
            currentMonth = inputField.value.substr(monthPos,2)/1 -1;
            var yearPos = format.indexOf('yyyy');
            currentYear = inputField.value.substr(yearPos,4);
            var dayPos = format.indexOf('dd');
            tmpDay = inputField.value.substr(dayPos,2);
        }
        
    } else {
        var d = new Date();
        currentMonth = d.getMonth();
        currentYear = d.getFullYear();
        tmpDay = d.getDate();
    }

    inputYear = currentYear;
    inputMonth = currentMonth;
    inputDay = tmpDay/1;

    if(!calendarDiv) {
        initCalendar();
    } else {
        if(calendarDiv.style.display=='block') {
            closeCalendar();
            return false;
        }
        writeCalendarContent();
    }

    returnFormat = format;
    returnDateTo = inputField;
    positionCalendar(buttonObj);
    calendarDiv.style.visibility = 'visible';
    calendarDiv.style.display = 'block';
    if(iframeObj) { // fix for IE frame problem on dropdowns
        iframeObj.style.display = '';
        iframeObj.style.height = '140px';
        iframeObj.style.width = '195px';
        iframeObj2.style.display = '';
        iframeObj2.style.height = '140px';
        iframeObj2.style.width = '195px';
    }

    updateYearDiv();
    updateMonthDiv();
}

