function updateCal()
{
    // do not process
    //return;

    var sNewDate;
    var y, m, d;
    y = document.forms["frmMain"].elements["income_nextpayday1_year"].value;
    m = document.forms["frmMain"].elements["income_nextpayday1_month"].value;
    d = document.forms["frmMain"].elements["income_nextpayday1_day"].value;

    if ( !(m=='' || d=='') )
    {
        var i_y, i_m, i_d;
        i_y = parseInt(y);
        i_m = parseInt(m);
        i_d = parseInt(d);

        var correctedDate;
        correctedDate = correctPayday(i_y, i_m, i_d);

        //alert( "Changed to --> " + correctedDate);
        //alert(correctedDate.getYear()<1000)?1900+correctedDate.getYear():correctedDate.getYear());


        var new_y, new_m, new_d
        new_y = (correctedDate.getYear()<1000)?1900+correctedDate.getYear():correctedDate.getYear();
        new_m = correctedDate.getMonth()+1;
        new_d = correctedDate.getDate()

        document.forms["frmMain"].elements["income_nextpayday1_month"].value = new_m;
        document.forms["frmMain"].elements["income_nextpayday1_day"].value = new_d;
        document.forms["frmMain"].elements["income_nextpayday1_year"].value = new_y;


        sNewDate = new_m + "/" + new_d + "/" + new_y;
        document.forms["frmMain"].elements["cal_date"].value = sNewDate;
    }
}


function getDayofWeek(y, m, d)
{
    var thedate2 = new Date();

    thedate2.setYear(y);
    thedate2.setDate(d);
    thedate2.setMonth(m-1);
    thedate2.setHours(0);
    thedate2.setMinutes(0);
    thedate2.setSeconds(0);

    var dayofweek = thedate2.getDay();

    return dayofweek;
}

function isWeekend(y, m, d)
{
    var dayofweek = getDayofWeek(y, m, d);

    if (dayofweek == 0 || dayofweek == 6)
        return true;
    else
        return false;
}

function isHoliday(y, m, d)
{
    switch(y) {
    case 2008:
        var holidays = {
          1 :  [1, 21],
          2 :  [18],
          5 :  [26],
          7 :  [4],
          9 :  [1],
          10 : [13],
          11 : [11, 27],
          12 : [25]
        }
        break;
    case 2009:
        var holidays = {
          1 :  [1, 19],
          2 :  [16],
          5 :  [25],
          7 :  [4],
          9 :  [7],
          10 : [12],
          11 : [11, 26],
          12 : [25]
        }
        break;
    }

    // disable holidays
    holidaysthismonth = holidays[m];

    if (!holidaysthismonth)
        return false;

    for (var i in holidaysthismonth)
        if (holidaysthismonth[i] == d)
            return true;

    return false;
}

function dateAdd(intval, numb, base){
	/*intval is YYYY, M, D, H, N, S as in VBscript; numb is amount +/-; base is javascript date object*/
	switch(intval){
		case "M":
            //alert( "payday1: "+base.getMonth()+"\n" );
			base.setMonth(base.getMonth() + numb);
            //alert( "payday2: "+base.getMonth()+"\n" );
			break;
		case "YYYY":
            //alert( "payday1: "+base.getFullYear()+"\n" );
			base.setFullYear(base.getFullYear() + numb);
            //alert( "payday2: "+base.getFullYear()+"\n" );
			break;
		case "D":
            //alert( "payday1: "+base.getDate()+"\n" );
			base.setDate(base.getDate() + numb);
            //alert( "payday2: "+base.getDate()+"\n" );
			break;
		case "H":
			base.setHours(base.getHours() + numb);
			break;
		case "N":
			base.setMinutes(base.getMinutes() + numb);
			break;
		case "S":
			base.setSeconds(base.getSeconds() + numb);
			break;
		default:
	}
	return base
}

function correctPayday(y, m, d)
{
    var thedate = new Date();
    thedate.setYear(y);
    thedate.setDate(d);
    thedate.setMonth(m-1);
    thedate.setHours(0);
    thedate.setMinutes(0);
    thedate.setSeconds(0);

    // Step 1 - if weekend, then move back 1 or 2 days
    if (isWeekend(y,m,d))
    {
        var dayofweek = getDayofWeek(y, m, d);
        if (dayofweek==0)
            dateAdd("D", -2, thedate);
        if (dayofweek==6)
            dateAdd("D", -1, thedate);

        correctPayday((thedate.getYear()<1000)?1900+thedate.getYear():thedate.getYear(), thedate.getMonth()+1, thedate.getDate());
    }

    // Step 2 - if holiday, then move back 1 day, unless the holiday is a Monday, then go to Tuesday
    if (isHoliday(y,m,d))
    {
        var dayofweek = getDayofWeek(y, m, d);

        if (dayofweek==1)
            dateAdd("D", +1, thedate);
        else
            dateAdd("D", -1, thedate);
    }

    return thedate;
}
