

var oCalStyle //calendar style
var oMonthStyle //month style
var oYearStyle //year style 
var ctlToPlaceValue //Control to put user selection into
var monthSelected //Month (as integer) that was passed in
var yearSelected //Year (as integer) that was passed in
var dateSelected //day part of date (as integer) that was passed in
var omonthSelected
var oyearSelected
var odateSelected
var crossYearObj, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlNow, nStartingYear
var oCal;
var bPageLoaded = false
var ie = document.all;
var dom = document.getElementById;

var today = new Date()
var dateNow = today.getDate()
var monthNow = today.getMonth()
var yearNow = today.getFullYear()
var _emsDPBuildingId = -1;

var firstAvailableDate;
var lastAvailableDate;

var bShow = false;

//Will be called first.  popUpCal checks if it has been called if it hasn't it calls it.
function init() {
    oCalStyle = document.getElementById("calendar").style;
    oCal = document.getElementById("calendar");
    hideCalendar() //hide existing calendar going to rebuild

    oMonthStyle = document.getElementById("selectMonth").style;

    oYearStyle = document.getElementById("selectYear").style;

    monthConstructed = false;
    yearConstructed = false;

    if (showToday == 1) {
        document.getElementById("lblToday").innerHTML = todayString + " <a class='CalendarToday' onmousemove='window.status=\"" + gotoString + "\"' onmouseout='window.status=\"\"' title='" + gotoString + "' href='javascript:monthSelected=monthNow;yearSelected=yearNow;constructCalendar();'>" + dayName[(today.getDay() - startAt == -1) ? 6 : (today.getDay() - startAt)] + " " + dateNow + " " + monthAbbr[monthNow] + "	" + yearNow + "</a>"
    }

    sHTML1 = "<a id='spanLeft'  class='CalendarTitleControlNormal' onmouseover='swapImage(\"changeLeft\", 3);this.className=\"CalendarTitleControlActive\";window.status=\"" + scrollLeftMessage + "\"' href='javascript:decMonth()' onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",2);this.className=\"CalendarTitleControlNormal\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'><img id='changeLeft' src='" + img[2].src + "' width='10' height='11'></a>&nbsp;"
    sHTML1 += "<a id='spanRight' class='CalendarTitleControlNormal' onmouseover='swapImage(\"changeRight\", 5);this.className=\"CalendarTitleControlActive\";window.status=\"" + scrollRightMessage + "\"' onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",4);this.className=\"CalendarTitleControlNormal\";window.status=\"\"' href='javascript:incMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'><img id='changeRight' src='" + img[4].src + "' width='10' height='11'></a>&nbsp;"
    sHTML1 += "<span id='spanMonth'  class='CalendarTitleControlNormal' onmouseover='swapImage(\"changeMonth\",1);this.className=\"CalendarTitleControlActive\";window.status=\"" + selectMonthMessage + "\"' onmouseout='swapImage(\"changeMonth\",0);this.className=\"CalendarTitleControlNormal\";window.status=\"\"' onclick='popUpMonth()'></span>&nbsp;"
    sHTML1 += "<span id='spanYear'  class='CalendarTitleControlNormal' onmouseover='swapImage(\"changeYear\",1);this.className=\"CalendarTitleControlActive\";window.status=\"" + selectYearMessage + "\"'	onmouseout='swapImage(\"changeYear\",0);this.className=\"CalendarTitleControlNormal\";window.status=\"\"'	onclick='popUpYear()'></span>&nbsp;"

    document.getElementById("caption").innerHTML = sHTML1

    bPageLoaded = true
}

//Hides calendar
function hideCalendar() {
    if (oCalStyle)
        oCalStyle.visibility = "hidden"

    if (oMonthStyle != null) { oMonthStyle.visibility = "hidden" }
    if (oYearStyle != null) { oYearStyle.visibility = "hidden" }

    showElement('SELECT');
}


//Main entry point...this is the call from page
function popUpCalendar(ctl, ctl2, format) {
    if (ctl.disabled || ctl2.disabled) {
        return;
    }

    var leftpos = 0
    var toppos = 0

    //Check if init has been called	
    if (bPageLoaded) {
        if (oCalStyle.visibility == "hidden") {
            ctlToPlaceValue = ctl2

            tokensChanged = 0
            //gave us a valid date format....use the date the gave us

            // use user's date
            aData = ctl2.value.split(localDateSeparator)
            aFormat = localDateFormat.split(localDateSeparator);
            for (var i = 0; i < 3; i++) {
                if ((aFormat[i] == "d") || (aFormat[i] == "dd")) {
                    dateSelected = parseInt(aData[i], 10)
                    tokensChanged++
                }
                else if ((aFormat[i] == "m") || (aFormat[i] == "mm")) {
                    monthSelected = parseInt(aData[i], 10) - 1
                    tokensChanged++
                }
                else if (aFormat[i] == "yyyy" || aFormat[i] == "yy" || aFormat[i] == "y") {
                    yearSelected = parseInt(aData[i], 10)
                    tokensChanged++
                }
                else if (aFormat[i] == "mmm") {
                    for (var j = 0; j < 12; j++) {
                        if (aData[i] == monthName[j]) {
                            monthSelected = j
                            tokensChanged++
                        }
                    }
                }
            }

            //Make sure that we got a valid date. if not, set to today
            if ((tokensChanged != 3) || isNaN(dateSelected) || isNaN(monthSelected) || isNaN(yearSelected)) {
                dateSelected = dateNow
                monthSelected = monthNow
                yearSelected = yearNow
            }

            odateSelected = dateSelected
            omonthSelected = monthSelected
            oyearSelected = yearSelected

            //Set position of Cal
            setObjectPosition(oCal, true);
            constructCalendar(1, monthSelected, yearSelected);
            oCalStyle.visibility = (dom || ie) ? "visible" : "show"

            hideElement('SELECT', document.getElementById("calendar"));
            bShow = true;
            if (document.getElementById("spanLeft")) {
                document.getElementById("spanLeft").focus();
            }
        }
    }
    else {
        init()
        popUpCalendar(ctl, ctl2, format)
    }
}



/* hides <select> and <applet> objects (for IE only) */
function hideElement(elmID, overDiv) {
    if (ie) {
        for (var i = 0; i < document.all.tags(elmID).length; i++) {
            obj = document.all.tags(elmID)[i];
            if (!obj || !obj.offsetParent) {
                continue;
            }

            // Find the element's offsetTop and offsetLeft relative to the BODY tag.
            objLeft = obj.offsetLeft;
            objTop = obj.offsetTop;
            objParent = obj.offsetParent;

            while (objParent && objParent.tagName.toUpperCase() != "BODY") {
                objLeft += objParent.offsetLeft;
                objTop += objParent.offsetTop;
                objParent = objParent.offsetParent;
            }

            objHeight = obj.offsetHeight;
            objWidth = obj.offsetWidth;

            if ((overDiv.offsetLeft + overDiv.offsetWidth) <= objLeft);
            else if ((overDiv.offsetTop + overDiv.offsetHeight + 20) <= objTop);
            else if (overDiv.offsetTop >= (objTop + objHeight + obj.height));
            else if (overDiv.offsetLeft >= (objLeft + objWidth));
            else {
                obj.style.visibility = "hidden";
            }
        }
    }
}

/*
* unhides <select> and <applet> objects (for IE only)
*/
function showElement(elmID) {
    if (ie) {
        for (var i = 0; i < document.all.tags(elmID).length; i++) {
            obj = document.all.tags(elmID)[i];

            if (!obj || !obj.offsetParent) {
                continue;
            }

            obj.style.visibility = "";
        }
    }
}

function swapImage(srcImg, destImg) {
    //document.getElementById(srcImg).setAttribute("src", imgDir + destImg) 
    document.getElementById(srcImg).setAttribute("src", img[destImg].src)
}

function padZero(num) {
    return (num < 10) ? '0' + num : num;
}

/*** Month Pulldown	***/

function StartDecMonth() {
    intervalID1 = setInterval("decMonth()", 80);
}

function StartIncMonth() {
    intervalID1 = setInterval("incMonth()", 80)
}

function incMonth() {
    monthSelected++
    if (monthSelected > 11) {
        monthSelected = 0
        yearSelected++
    }
    constructCalendar()
}

function decMonth() {
    monthSelected--
    if (monthSelected < 0) {
        monthSelected = 11
        yearSelected--
    }
    constructCalendar()
}

function constructMonth() {
    popDownYear()
    if (!monthConstructed) {
        sHTML = ""
        for (var i = 0; i < 12; i++) {
            sName = monthName[i];
            if (i == monthSelected) {
                sName = "<B>" + sName + "</B>"
            }
            sHTML += "<tr><td id='m" + i + "' onmouseover='this.className=\"CalendarDropDownItemActive\"' onmouseout='this.className=\"CalendarDropDownItemNormal\"' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
        }

        document.getElementById("selectMonth").innerHTML = "<table summary='Format Only' width='70' class='CalendarDropdownNormal' cellspacing='0' onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" + sHTML + "</table>"

        monthConstructed = true
    }
}

function popUpMonth() {
    constructMonth()
    oMonthStyle.visibility = "visible";
    oMonthStyle.left = parseInt(oCalStyle.left) + 39 + "px";
    oMonthStyle.top = parseInt(oCalStyle.top) + 26 + "px"

    hideElement('SELECT', document.getElementById("selectMonth"));
    hideElement('APPLET', document.getElementById("selectMonth"));
}

function popDownMonth() {
    oMonthStyle.visibility = "hidden"
}

/*** Year Pulldown ***/

function incYear() {
    for (var i = 0; i < 7; i++) {
        newYear = (i + nStartingYear) + 1
        if (newYear == yearSelected)
        { txtYear = "&nbsp;<B>" + newYear + "</B>&nbsp;" }
        else
        { txtYear = "&nbsp;" + newYear + "&nbsp;" }
        document.getElementById("y" + i).innerHTML = txtYear
    }
    nStartingYear++;
    bShow = true
}

function decYear() {
    for (var i = 0; i < 7; i++) {
        newYear = (i + nStartingYear) - 1
        if (newYear == yearSelected)
        { txtYear = "&nbsp;<B>" + newYear + "</B>&nbsp;" }
        else
        { txtYear = "&nbsp;" + newYear + "&nbsp;" }
        document.getElementById("y" + i).innerHTML = txtYear
    }
    nStartingYear--;
    bShow = true
}

function selectYear(nYear) {
    yearSelected = parseInt(nYear + nStartingYear);
    yearConstructed = false;
    constructCalendar();
    popDownYear();
}

function constructYear() {
    popDownMonth()
    sHTML = ""
    if (!yearConstructed) {

        sHTML = "<tr><td align='center'	onmouseover='this.className=\"CalendarDropDownItemActive\"' onmouseout='clearInterval(intervalID1);this.className=\"CalendarDropDownItemNormal\"' onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>"
        var j = 0
        nStartingYear = yearSelected - 3
        for (var i = (yearSelected - 3); i <= (yearSelected + 3); i++) {
            sName = i;
            if (i == yearSelected) {
                sName = "<B>" + sName + "</B>"
            }

            sHTML += "<tr><td id='y" + j + "' onmouseover='this.className=\"CalendarDropDownItemActive\"' onmouseout='this.className=\"CalendarDropDownItemNormal\"' onclick='selectYear(" + j + ");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
            j++;
        }

        sHTML += "<tr><td align='center' onmouseover='this.className=\"CalendarDropDownItemActive\"' onmouseout='clearInterval(intervalID2);this.className=\"CalendarDropDownItemNormal\"' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>"

        document.getElementById("selectYear").innerHTML = "<table summary='Format Only' width='44' class='CalendarDropdownNormal' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>" + sHTML + "</table>"

        yearConstructed = true
    }
}

function popDownYear() {
    clearInterval(intervalID1)
    clearTimeout(timeoutID1)
    clearInterval(intervalID2)
    clearTimeout(timeoutID2)
    oYearStyle.visibility = "hidden"
}

function popUpYear() {
    var leftOffset

    constructYear()
    oYearStyle.visibility = (dom || ie) ? "visible" : "show"
    leftOffset = parseInt(oCalStyle.left) + document.getElementById("spanYear").offsetLeft
    oYearStyle.left = leftOffset + 8 + "px";
    oYearStyle.top = parseInt(oCalStyle.top) + 26 + "px"
}

/*** calendar ***/

function takeYear(theDate) {
    x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
}

function constructCalendar() {
    bShow = true;
    var dateMessage
    var startDate = new Date(yearSelected, monthSelected, 1)
    var endDate = new Date(yearSelected, monthSelected + 1, 1);
    endDate = new Date(endDate - (24 * 60 * 60 * 1000));
    numDaysInMonth = endDate.getDate()

    datePointer = 0
    dayPointer = startDate.getDay() - startAt

    if (dayPointer < 0) {
        dayPointer = 6
    }
    sHTML = "<table	border='0' class='CalendarBody' summary='Format Only'><tr>"

    for (var i = 0; i < 7; i++) {
        sHTML += "<td width='27' align='right'><B>" + dayName[i] + "</B></td>"
    }
    sHTML += "</tr><tr>"

    for (var i = 1; i <= dayPointer; i++) {
        sHTML += "<td>&nbsp;</td>"
    }

    for (datePointer = 1; datePointer <= numDaysInMonth; datePointer++) {
        dayPointer++;
        sHTML += "<td align='right'>"

        var sStyle = "CalendarNormalDay"; //regular day

        if ((datePointer == dateNow) && (monthSelected == monthNow) && (yearSelected == yearNow)) //today
        { sStyle = "CalendarCurrentDay"; }
        else if (dayPointer % 7 == (startAt * -1) + 1) //end-of-the-week day
        { sStyle = "CalendarEndWeekday"; }

        //selected day
        if ((datePointer == odateSelected) && (monthSelected == omonthSelected) && (yearSelected == oyearSelected))
        { sStyle += " CalendarSelectedDay"; }

        sHint = ""
        for (var k = 0; k < HolidaysCounter; k++) {
            if ((parseInt(Holidays[k].d) == datePointer) && (parseInt(Holidays[k].m) == (monthSelected + 1))) {
                if ((parseInt(Holidays[k].y) == 0) || ((parseInt(Holidays[k].y) == yearSelected) && (parseInt(Holidays[k].y) != 0))) {
                    if (parseInt(Holidays[k].b) === -1 || parseInt(Holidays[k].b) === _emsDPBuildingId) {
                        sStyle += " CalendarHoliday";
                        sHint += sHint == "" ? Holidays[k].desc : "\n" + Holidays[k].desc
                    }
                }
            }
        }

        var regexp = /\"/g
        sHint = sHint.replace(regexp, "&quot;")

        dateMessage = "onmousemove='window.status=\"" + selectDateMessage.replace("[date]", makeLocalDate(datePointer, monthSelected + 1, yearSelected)) + "\"' onmouseout='window.status=\"\"' "

        sHTML += "<a class='" + sStyle + "' " + dateMessage + " title=\"" + sHint + "\" href='javascript:dateSelected=" + datePointer + ";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</a>"

        sHTML += ""
        if ((dayPointer + startAt) % 7 == startAt) {
            sHTML += "</tr><tr>"
        }
    }

    document.getElementById("calContent").innerHTML = sHTML
    document.getElementById("spanMonth").innerHTML = "&nbsp;" + monthName[monthSelected] + "&nbsp;<IMG id='changeMonth' SRC='" + img[0].src + "' WIDTH='12' HEIGHT='10' BORDER='0'>"
    document.getElementById("spanYear").innerHTML = "&nbsp;" + yearSelected + "&nbsp;<IMG id='changeYear' SRC='" + img[0].src + "' WIDTH='12' HEIGHT='10' BORDER='0'>"
}

if (dom) {
    //		for	(i = 0; i < imgsrc.length; i++)
    //		{
    //			img[i] = new Image
    //			img[i].src= imgDir + imgsrc[i]
    //		}
    document.write("<div onclick='bShow=true' id='calendar'	 class='CalendarDiv'>");
    document.write("<table width='220' class='CalendarContainer' summary='Format Only'><tr><td>");
    document.write("<table width='218'  class='CalendarHeaderTable' summary='Format Only'><tr><td class='CalendarTitle'>");
    document.write("<B><span id='caption'></span></B></td>");
    document.write("<td align='right'><a href='javascript:hideCalendar()'><IMG SRC='" + img[6].src + "' WIDTH='15' HEIGHT='13' BORDER='0' ALT='" + ems_CloseTheCalendar + "'></a></td></tr>");
    document.write("</table></td></tr><tr><td class='CalendarBody'><div id='calContent'></div></td></tr>")

    if (showToday == 1) {
        document.write("<tr class='CalendarToday'><td><span id='lblToday'></span></td></tr>")
    }

    document.write("</table></div><div id='selectMonth' class='CalendarDiv'></div><div id='selectYear' class='CalendarDiv'></div>");
}

if (ie) {
    init()
}
else {
    window.onload = init
}

function makeLocalDate(d, m, y) {
    sTmp = localDateFormat;
    sTmp = sTmp.replace("dd", "<e>")
    sTmp = sTmp.replace("d", "<d>")
    sTmp = sTmp.replace("<e>", padZero(d))
    sTmp = sTmp.replace("<d>", d)
    sTmp = sTmp.replace("mmm", "<o>")
    sTmp = sTmp.replace("mm", "<n>")
    sTmp = sTmp.replace("m", "<m>")
    sTmp = sTmp.replace("<m>", m)
    sTmp = sTmp.replace("<n>", padZero(m))
    sTmp = sTmp.replace("<o>", monthName[m])
    sTmp = sTmp.replace("yyyy", y)
    sTmp = sTmp.replace("yy", padZero(y % 100))
    return sTmp;
}

function selectText(box) {
    if (box) {
        checkDateFired = false;
        try {
            box.select();
        }
        catch (cantSelect) {
        }
        if (typeof (clearResults) != 'undefined')
            clearResults();
    }

}

function isLeapYear(year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

var daysofmonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var keyDownFired = false;
function checkKeyDownDate(event, box) {
    var key = getKey(event);

    if (key == 100) //hit d set to today
    {
        keyDownFired = true;
        monthSelected = monthNow + 1;
        dateSelected = dateNow;
        yearSelected = yearNow;

        populateBoxWithDate(box, monthSelected, dateSelected, yearSelected);
        box.onchange(event);
        return false;
    }

    var dateIs = getXFromValue(box.value, "d");
    var monthIs = getXFromValue(box.value, "m");
    var yearIs = getXFromValue(box.value, "y");

    if (dateIs == -1 || monthIs == -1 || yearIs == -1) //don't have a good date.
        return true;

    keyDownFired = false;
    switch (key) {
        case 61: //+				
        case 43:
            dateIs = dateIs + 1;
            if (isLeapYear(yearIs)) {
                if (dateIs > daysofmonthLY[monthIs - 1]) {
                    dateIs = 1;

                    if (monthIs == 12) {
                        monthIs = 1;
                        yearIs = yearIs + 1;
                    }
                    else {
                        monthIs = monthIs + 1;
                    }
                }
            }
            else {
                if (dateIs > daysofmonth[monthIs - 1]) {
                    dateIs = 1;

                    if (monthIs == 12) {
                        monthIs = 1;
                        yearIs = yearIs + 1;
                    }
                    else {
                        monthIs = monthIs + 1;
                    }
                }
            }

            keyDownFired = true;
            break;

        case 45: // -
            dateIs = dateIs - 1;
            if (dateIs < 1) {
                if (isLeapYear(yearIs)) {
                    if (monthIs == 1) {
                        monthIs = 12;
                        yearIs = yearIs - 1;
                    }
                    else
                        monthIs = monthIs - 1;

                    dateIs = daysofmonthLY[monthIs - 1];
                }
                else {
                    if (monthIs == 1) {
                        monthIs = 12;
                        yearIs = yearIs - 1;
                    }
                    else
                        monthIs = monthIs - 1;

                    dateIs = daysofmonth[monthIs - 1];
                }
            }

            keyDownFired = true;
            break;
    }

    if (keyDownFired) {
        monthSelected = monthIs;
        dateSelected = dateIs;
        yearSelected = yearIs;
        populateBoxWithDate(box, monthIs, dateIs, yearIs);
        box.onchange(event);
        return false;
    }
    else {
        return true;
    }
}


function dateBoxKeyPressed(event, box) {
    var key = getKey(event);
    if (key == 13) //suppress retrun
        return false;

    checkKeyDownDate(event, box);

    if (!keyDownFired) //The did NOT push +, -, or d
    {
        if (key < 32 || (key >= 33 && key < 46) || (key >= 112 && key <= 123))
            return true;
    }
    else {
        checkDateFired = false;        
        return false;
    }
}

function getXFromValue(boxValue, x) {
    try {
        var formatArray = new Array();
        formatArray = localDateFormat.split(localDateSeparator);
        var dateParts = new Array();
        dateParts = boxValue.split(localDateSeparator);

        if (dateParts.length == 3 && dateParts[2].indexOf(" ") !== -1) {
            try {
                dateParts[2] = dateParts[2].substring(0, dateParts[2].indexOf(" "));
            }
            catch (badThird) {
            }
        }
        var dateIndex = -1;
        for (var i = 0; i < dateParts.length; i++) {
            if (formatArray[i].indexOf(x) >= 0) {
                return Number(dateParts[i]);
            }
        }

        return dateIndex;
    }
    catch (badDate) {
    }
}

function isDateValid(d) {
    
    if (d === "") {
        return true;
    }
    var dateIs = checkNum(getXFromValue(d, "d"));
    var monthIs = checkNum(getXFromValue(d, "m"));
    var yearIs = checkNum(getXFromValue(d, "y"));
    if (!monthIs || monthIs == -1) {
        monthIs = monthNow + 1;
    }

    if (yearIs == -1) {
        yearIs = yearNow;
    }
    else {
        if (yearIs < 38) {
            yearIs += 2000;
        }
        else {
            if (yearIs < 1900) {
                yearIs += 1900;
            }
        }
        if (yearIs > 9999) {
            yearIs = -1;
        }
    }

    if (monthIs > 12) {
        monthIs = -1;
    }

    if (isLeapYear(yearIs) && monthIs != -1) {
        if (dateIs > daysofmonthLY[monthIs - 1]) {
            dateIs = -1;
        }
    }
    else {
        if (dateIs > daysofmonth[monthIs - 1]) {
            dateIs = -1;
        }
    }

    if (dateIs == -1 || monthIs == -1 || yearIs == -1) {
        return false;
    }
    else {
        return true;
    }
}

var checkDateFired = false;
function checkDate(box) {
    if (box.value == "")
        return;

    if (!checkDateFired) {
        var dateIs = checkNum(getXFromValue(box.value, "d"));
        var monthIs = checkNum(getXFromValue(box.value, "m"));
        var yearIs = checkNum(getXFromValue(box.value, "y"));

        if (!monthIs || monthIs == -1) {
            monthIs = monthNow + 1;
        }

        if (yearIs == -1) {
            yearIs = yearNow;
        }
        else {
            if (yearIs < 38) {
                yearIs += 2000;
            }
            else {
                if (yearIs < 1900) {
                    yearIs += 1900;
                }
            }

            if (yearIs > 9999) {
                yearIs = -1;
            }
        }

        if (monthIs > 12) {
            monthIs = -1;
        }

        if (isLeapYear(yearIs) && monthIs != -1) {
            if (dateIs > daysofmonthLY[monthIs - 1]) {
                dateIs = -1;
            }
        }
        else {
            if (dateIs > daysofmonth[monthIs - 1]) {
                dateIs = -1;
            }
        }


        if (isDateValid(box.value) === true) {
            populateBoxWithDate(box, monthIs, dateIs, yearIs)
        }
        else {
            alert(ems_InvalidDate);
            populateBoxWithDate(box, monthNow + 1, dateNow, yearNow)
            selectText(box)
        }
        checkDateFired = true;
    }
}

function checkNum(x) {
    if (!x)
        return -1;
    if (isNaN(x))
        return -1;

    return Number(x);
}

function populateBoxWithDate(box, monthIs, dateIs, yearIs) {
    var sSelectedDateForDayName = new Date(monthIs + '/' + dateIs + '/' + yearIs);

    if (firstAvailableDate > sSelectedDateForDayName) {
        alert(ems_FirstDateViolation);
        monthIs = firstAvailableDate.getMonth() + 1;
        dateIs = firstAvailableDate.getDate();
        yearIs = firstAvailableDate.getFullYear();
    }

    if (lastAvailableDate < sSelectedDateForDayName) {
        alert(ems_LastDateViolation);
        monthIs = lastAvailableDate.getMonth() + 1;
        dateIs = lastAvailableDate.getDate();
        yearIs = lastAvailableDate.getFullYear();
    }

    sSelectedDateForDayName = new Date(monthIs + '/' + dateIs + '/' + yearIs);

    box.value = makeLocalDate(dateIs, monthIs, yearIs) + ' ' + dayName[(sSelectedDateForDayName.getDay() - startAt == -1) ? 6 : (sSelectedDateForDayName.getDay() - startAt)]

    if (document.getElementById('hdlastAttendeeBookDate'))
        document.getElementById('hdlastAttendeeBookDate').value = makeLocalDate(dateIs, monthIs, yearIs);

    if (typeof (attemptClear) != 'undefined')
        attemptClear();
    return;
}
	

