//
//
// The date routines in this library were developed by Qualitech Solutions, Inc (www.QualitechSolutions.com)
// Modifying within the application they are currently being used is allowed. However copying and re-using
// in another application is not permitted unless written authorization is obtained from Qualitech Solutions, Inc.
//
//
var today = new Date();
var day;
var month;
var year;
var thisT;
var thisF;
var thisDays;

function makeArray0() 
{
    for (i = 0; i<makeArray0.arguments.length; i++)
        this[i] = makeArray0.arguments[i];
}

var names     = new makeArray0('January','February','March','April','May','June','July','August','September','October','November','December');
var days      = new makeArray0(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var dow       = new makeArray0('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

function copyDate() 
{
    myTextBox = '' + padout(month - 0 + 1)  + '/' + padout(day)+ '/' + year;

    eval("document." + thisF +"." + thisT + ".value = '" + myTextBox + "'");
}

function y2k(number)
{
    return (number < 1000) ? number + 1900 : number; 
}

function padout(number) 
{ 
    return (number < 10) ? '0' + number : number; 
}

function newWindow(frmName, txtName, NoDays)
{
    if (window.calFrame)
    {
        setTimeout('newWindow(\'' + frmName + '\', \'' + txtName + '\', \'' + NoDays + '\');', 10);

        return;
    }

    thisF = frmName;
    thisT = txtName;
    thisDays = NoDays;

    eval('thisText = document.' + thisF + '.' + thisT);

    if (validateDate(thisText.value))
    {
        var aDate = thisText.value.split('/');

        day = aDate[1] - 0;   
        month = aDate[0] - 1;
        year = aDate[2] - 0;

        if (year < 50) 
            year += 2000; 

        if (year > 49 && year < 100) 
            year += 1900;
    }
    else
    {
        day   = today.getDate();
        month = today.getMonth();
        year  = today.getFullYear();
    }

    isRecentIE = false;

    if (document.all)
    {
        IEVersion = parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf('MSIE') + 5, navigator.appVersion.indexOf(';', navigator.appVersion.indexOf('MSIE'))));
        isRecentIE = (IEVersion >= 5.5);
    }
  
    if (isRecentIE)
    {
        thisIFrame = document.createElement('<iframe name="calFrame" frameborder="no" scrolling="no" style="position:absolute; width:284px; height:180px;">');
        thisIFrame.src = 'javascript:parent.Calendar();';

        var left=0; var top=0;

        for(var e=thisText; e && e.tagName!='BODY'; e=e.offsetParent)
        {
            left += e.offsetLeft;
            top += e.offsetTop;
        }

        var textHeight = thisText.offsetHeight;
        var frameHeight = thisIFrame.style.pixelHeight;
        var scrollTop = document.body.scrollTop;
        var scrollLeft = document.body.scrollLeft;
 
        thisIFrame.style.left = Math.max(Math.min(left, document.body.clientWidth + scrollLeft - thisIFrame.style.pixelWidth), scrollLeft);
	
        if(top-frameHeight >= scrollTop && (top+textHeight+frameHeight) > document.body.clientHeight + scrollTop)
            thisIFrame.style.top = top - frameHeight + 1;
        else
            thisIFrame.style.top = top + textHeight;

	document.body.appendChild(thisIFrame);

        // If in a modal dialog, the IFrame disappears w/o the setTimeout

	setTimeout('document.onclick = hideCal;', 0);
    }
    else	// NS, old IE
    {
        windowOptions = "toolbar=no,location=no,directories=no,resize=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=280,height=193";

        if (!window.mywindow) 
        {
            mywindow=open('../cal.htm','myname',windowOptions);
            mywindow.opener = window;
        }
        else
        { 
            if (!mywindow.closed)
                mywindow.focus();
            else
                mywindow=open('../cal.htm','myname',windowOptions);
        }
    }
}

function hideCal()
{
    document.body.removeChild(thisIFrame);
    document.onclick = null;
}

function Calendar()
{
    var output = '';

    output += '<HTML>';
    output += '<HEAD>';
    output += '<link rel="stylesheet" type="text/css" href="ResumeFit.css">';
    output += '<Title>Calendar</title>';
    output += '</HEAD>';

    output += '<BODY class="calendar">';

    output += '<FORM NAME="Cal"><TABLE align="center" border="0"><TR>';
    output += '<td onClick="parent.decrementMonth();" class="CalenderSwitch">&nbsp;&lt;&lt;&nbsp;&nbsp;<\/td>';
    output += '<TD>';
    output += '<SELECT NAME="cboMonth" onChange="parent.changeMonth(this.selectedIndex);" class="Calendar">';

    for (var i=0; i<12; i++) 
    {
        if (i == month)
            output += '<OPTION VALUE="' + i + '" SELECTED>' + names[i] + '<\/OPTION>';
        else
            output += '<OPTION VALUE="' + i + '">'          + names[i] + '<\/OPTION>';
    }

    output += '<\/SELECT><SELECT NAME="cboYear" onChange="parent.changeYear(this.options[this.selectedIndex].value);" class="Calendar">';

    for (var i=1900; i<2100; i++) 
    {
        if (i == year)
            output += '<OPTION VALUE="' + i + '" SELECTED>' + i + '<\/OPTION>';
        else
            output += '<OPTION VALUE="' + i + '">'          + i + '<\/OPTION>';
    }

    output += '<\/SELECT><\/TD>'; 
    output += '<td  onClick="parent.incrementMonth();" class="CalenderSwitch">&nbsp;&nbsp;&gt;&gt;&nbsp;<\/td>';
    output += '<\/TR><TR><TD ALIGN=CENTER COLSPAN=2>';
    
    output += '<\/td><\/tr><\/table>';

    firstDay = new Date(year,month,1);
    startDay = firstDay.getDay();

    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
        days[1] = 29; 
    else
        days[1] = 28;

    output += '<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1 BORDERCOLORDARK="#FFFFFF" BORDERCOLORLIGHT="#C0C0C0" align="center" id="days"><TR>';

    for (i=0; i<7; i++)
        output += '<TD width="32" ALIGN=CENTER class="CalendarHdr">' + dow[i] +'<\/TD>';

    output += '<\/TR><TR>';

    var rowCount = 0;
    var column = 0;
    var lastMonth = month - 1;

    if (lastMonth == -1)
        lastMonth = 11;

    for (i=0; i<startDay; i++, column++)
        output += '<TD width="32" class="CalendarOff">' + (days[lastMonth]-startDay+i+1) + '<\/TD>';

    for (i=1; i<=days[month]; i++, column++)
    {
        if (column == 7)
        {
            output += '<\/tr><tr>';
            column = 0;
            rowCount++;
        }

        if (isTooEarly(year, month, i))
            output += '<TD width="32" align="left" class="CalendarOff" valign="top" style="background-image:url(\'../images/padlock_cal.gif\');background-repeat:no-repeat;background-color:#EEEEEE;">' + i + '<\/TD>';
	else
            if (thisDays == 5 && (column == 0 || column == 6))
	        output += '<TD width="32" class="CalendarOff">' + (isToday(year, month, i) ? '<span style="color:red; font-family:Courier New, Arial; font-size:15px;">*</span>&nbsp;' : '') + i + '<\/TD>';
            else
            {
                output += '<TD width="32" class="CalendarOn" onclick="parent.changeDay(' + i + ');" onmouseout="this.style.backgroundColor=\'#EEEEEE\'; this.childNodes[0].style.color=\'\';" onmouseover="this.style.backgroundColor=\'#597099\'; this.childNodes[0].style.color=\'#FAF3CD\';">';
                output += '<A class="Calendar" HREF="javascript:void(0);">' + (isToday(year, month, i) ? '<span style="color:red; font-family:Courier New, Arial; font-size:15px;">*</span>&nbsp;' : '') + i + '<\/A><\/TD>';
            }
    }

    if (column > 0) 
    {
        for (i=1; column<7; i++, column++)
            output +=  '<TD width="32" class="CalendarOff">' + i + '<\/TD>';
    }

    output += '<\/TR><\/TABLE><\/FORM>';
    output += '</BODY>';
    output += '</HTML>';

    thisIFrame.style.height = (rowCount * 22) + 91;

    return output;
}

function isTooEarly(thisYear, thisMonth, thisDay)
{
    if (window.earliestDate)
        if (parseInt('' + thisYear + padout(parseInt(thisMonth) + 1) + padout(thisDay)) < earliestDate)
            return true;

    return false;
}

function isToday(thisYear, thisMonth, thisDay)
{
  return (thisDay == today.getDate() && thisMonth == today.getMonth() && thisYear == today.getFullYear());
}

function changeDay(thisDay) 
{
    day = thisDay + '';

    copyDate();

    hideCal();
}

function decrementMonth() 
{
    if (month == 0)
    {
        month = '11';

        year--;
    }
    else
        month--;

    thisIFrame.src = 'javascript:parent.Calendar();';
}

function incrementMonth() 
{
    if (month == 11)
    {
        month = '0';

        year++;
    }
    else
        month++;
	
    thisIFrame.src = 'javascript:parent.Calendar();';
}

function changeMonth(monthIn)
{
    month = monthIn;

    thisIFrame.src = 'javascript:parent.Calendar();';
}

function changeYear(yearIn)
{
    year = yearIn;

    thisIFrame.src = 'javascript:parent.Calendar();';
}


function IsDatePartNumeric(testMeIn, allowDecimal)
{
    var decimalCount = 0; var numberCount = 0;
    
    testMe = testMeIn + '';
    
    for (var i=0, len=testMe.length, valid='0123456789.';  i<len ; i++)
    {
        if (valid.indexOf(testMe.substring(i,i+1)) == -1)
            return false;

        if (valid.indexOf(testMe.substring(i,i+1)) == 10)
            decimalCount++;
        else
            numberCount++;
    }

    if (!allowDecimal && decimalCount > 0)
        return false;

    if (decimalCount > 1) 
        return false;

    if (numberCount == 0) 
        return false; 

    return true;
}
        
function isDate (day,month,year) 
{
    // checks if date passed is valid
    // will accept dates in following format:
    // isDate(dd,mm,ccyy), or
    // isDate(dd,mm) - which defaults to the current year, or
    // isDate(dd) - which defaults to the current month and year.
    // Note, if passed the month must be between 1 and 12, and the
    // year in ccyy format.

    var today = new Date();

    year  = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);

    if (!day) 
        return false;

    var test = new Date(year,month,day);

    if ( (y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate()) )
        return true;
    else
        return false
}

function validateDate(date) 
{
    var iSlash1 = 0;
    var iSlash2 = 0;
    var iSlash3 = 0;
    var err = 0;
    var aDate;
    var d;
    var m;
    var y;

    iSlash1 = date.indexOf('/')
    iSlash2 = date.indexOf('/', iSlash1 + 1)
    iSlash3 = date.indexOf('/', iSlash2 + 1)
 
    if (iSlash1 < 1 || iSlash2 < 1 || iSlash3 > 0)
        return false;
    else
    {   
        if (Date.parse(date) != Date.parse(date))
            return false;
        else
        {
            aDate = date.split('/');
            d = aDate[1];           
            m = aDate[0];
            y = aDate[2];

            if (d.length > 2 || m.length > 2 || y.length > 4) 
                return false;

            d = aDate[1] - 0;           
            m = aDate[0] - 0;
            y = aDate[2] - 0;

            if (!(IsDatePartNumeric(d, false) && IsDatePartNumeric(m, false) && IsDatePartNumeric(y, false))) 
                return false;

            if (y > 99 && y < 1900) 
                return false;

            if (y > 2099) 
                return false;

            if (d == 0 || m == 0) 
                return false;

            if (y < 50)
                y = 2000 + y; 

            if (y > 49 && y < 100) 
                y = 1900 + y; 

            if (isDate(d, m, y)) 
                return true;
            else
                return false;
        }
    }
}

function setCookie(key, value) 
{
    document.cookie = key + "=" + escape(value);
}

function getCookie(key) 
{
    if (document.cookie.length) 
    {
        var cookies = ' ' + document.cookie;
        var start = cookies.indexOf(' ' + key + '=');

        if (start == -1) 
            return null;

        var end = cookies.indexOf(";", start);

        if (end == -1)
            end = cookies.length;

        end -= start;

        var cookie = cookies.substr(start,end);

        return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
    }
    else
        return null;
}

function validateTime(thisTime) 
{
    if (isNaN(Date.parse('01/01/2003 ' + thisTime)))
        return false;
    else
        return true;
}
