/* ********************************************************************
Softricks Utilities: Next Working Date
Author : Kedar R. Bhave

Modification History :-
v1.0    18th Sep 2000.
v1.01	29th Sep 2000.	Bugfix

# COPYRIGHT NOTICE
# Copyright (c) 2000 Softricks.com, All rights reserved.
# This script may be used and modified free of charge by anyone as long as
# this copyright notice and the comments above are kept in their original 
# form. By using this script, you agree to the disclaimer notices as on the
# softricks.com site.
#
# Selling the code for this script, without prior written consent from the
# author, is not allowed. Redistributing this script over the internet or 
# in any medium should be done only with author's written permission.
#
# IN ALL CASES COPYRIGHT AND HEADERS MUST REMAIN INTACT.
#
# If you plan to use the script on a commercial site, we suggest that you
# provide a link or a reference to Softricks.com somewhere on your site.
#
# Distributed under the GNU General Public License.
# For more information visit: http://www.gnu.org/copyleft/lgpl.html
#
# Visit the website for more information on Softricks.com's
# Copyright, Privacy, Disclaimer and Terms of use policies.
******************************************************************** */
var td = new Date();
var today = format_date(td.getMonth(), td.getDate(), td.getFullYear(), "MM/DD/YYYY");

function format_date(p_month, p_day, p_year, p_format) {
	Months = ["January", "February", "March", "April", "May", "June",
			  "July", "August", "September", "October", "November", "December"];
	var vData;
	var vMonth = 1 + p_month;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Months[p_month].substr(0,3).toUpperCase();
	var vFMon = Months[p_month].toUpperCase();
	var vY4 = new String(p_year);
	var vY2 = new String(vY4.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (p_format) {
			case "MM\/DD\/YYYY" :
					vData = vMonth + "\/" + vDD + "\/" + vY4;
					break;
			case "MM\/DD\/YY" :
					vData = vMonth + "\/" + vDD + "\/" + vY2;
					break;
			case "MM-DD-YYYY" :
					vData = vMonth + "-" + vDD + "-" + vY4;
					break;
			case "MM-DD-YY" :
					vData = vMonth + "-" + vDD + "-" + vY2;
					break;

			case "DD\/MON\/YYYY" :
					vData = vDD + "\/" + vMon + "\/" + vY4;
					break;
			case "DD\/MON\/YY" :
					vData = vDD + "\/" + vMon + "\/" + vY2;
					break;
			case "DD-MON-YYYY" :
					vData = vDD + "-" + vMon + "-" + vY4;
					break;
			case "DD-MON-YY" :
					vData = vDD + "-" + vMon + "-" + vY2;
					break;

			case "DD\/MONTH\/YYYY" :
					vData = vDD + "\/" + vFMon + "\/" + vY4;
					break;
			case "DD\/MONTH\/YY" :
					vData = vDD + "\/" + vFMon + "\/" + vY2;
					break;
			case "DD-MONTH-YYYY" :
					vData = vDD + "-" + vFMon + "-" + vY4;
					break;
			case "DD-MONTH-YY" :
					vData = vDD + "-" + vFMon + "-" + vY2;
					break;
			case "MONTH DD, YYYY" :
					vData = vFMon + " " + vDD + ", " + vY4;
					break;
			case "DD\/MM\/YYYY" :
					vData = vDD + "\/" + vMonth + "\/" + vY4;
					break;
			case "DD\/MM\/YY" :
					vData = vDD + "\/" + vMonth + "\/" + vY2;
					break;
			case "DD-MM-YYYY" :
					vData = vDD + "-" + vMonth + "-" + vY4;
					break;
			case "DD-MM-YY" :
					vData = vDD + "-" + vMonth + "-" + vY2;
					break;

			default :
					vData = vMonth + "\/" + vDD + "\/" + vY4;
	}

	return vData;
}

function getFirstWorkingDay() {
	/* 
		Arguments:
		1. Date String in a valid date format
		2. Weekend string (multiple days can be specified by separating them with commas
		   default: 6,7 (Sat & Sun)
		3. Date format for the return date (15 formats supported)
		4. Advance/Stay flag - NextWorkingDate may be configured in 2 ways -
		   a> to advance to the next working day always
		   b> to stay on the given date if it is a working day already. 
		      but advance if it is one of the weekend days.
	*/

	if (arguments.length <= 0)
		return null;

	var ds = arguments[0];

	var DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	var lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

	var dt = new Date(ds);
	// I don't like 0-to-6 numbering. I use Mon-Sun as 1-7.
	var dy = dt.getDay() == 0 ? 7 : dt.getDay();
	var d = dt.getDate();
	var m = dt.getMonth();
	var y = dt.getFullYear();
	var ld = (y % 4) == 0 ? (((y % 100) == 0 && (y % 400) != 0) ? DOMonth[m] : lDOMonth[m]) : DOMonth[m];
	var advstay;

	// alert(d + "-" + m + "-" + y + "-" + dy + "-" + ld);

	// Temporary next variables..
	ndy = dy;
	nd = d;
	nm = m;
	ny = y;

	if (arguments.length > 1 && arguments[1] != null)
		wd = arguments[1];
	else
		wd = "6,7";
		
	var we = wd.split(",");

	// Do you want the NEXT working day or IMMEDIATE working day?
	if (arguments.length > 3 && arguments[3] != null)
		advstay = (typeof(arguments[3] == "string")) ?
						parseInt(arguments[3]) :
						arguments[3];
	else
		advstay = 1;		// Default is get NEXT working day

	tnd = d + advstay;			// Temporary next day
	tndy = (dy + advstay) % 7;

	do {
		weekend = false;
		
		// Apply some basic validations first..
		// can't be more than the last day of month!
		if (tnd > ld) {
			// if it went over the last day of month,
			// its a day in the next month
			nd = tnd - ld;

			// Month Validations..
			tnm = m + 1;
			// can't be more than the last month of year!
			if (tnm > 11) {
				// if it went over the last month of year,
				// its a day in the 1st month of next year
				nm = 0;
				ny = y + 1;
			} else nm = tnm;
		} else nd = tnd;

		// Whats the day?
		ndy = (tndy == 0) ? 7 : tndy;

		// Now this day should not be a weekend..
		for (i=0; i<we.length; i++)
			if (we[i] == ndy) {weekend = true; break;}

		if (weekend) {
			tnd = nd + 1;
			tndy = (ndy + 1) % 7;
		}
	} while (weekend);

	return format_date(nm, nd, ny, arguments[2] == null ? "MM/DD/YYYY" : arguments[2])
}
