/******************************************************
	Function: openTwoCalendars
	Purpose:  open a new calendar window
	
	Inputs:   none
	Returns:  none
*******************************************************/
function openTwoCalendars() {
	calWindow=open('/en_US/OWE/Common_Utilities/html/cal.html','myname','resizable=no,width=700,height=235,top=300,left=300');
	calWindow.location.href = '/en_US/OWE/Common_Utilities/html/cal.html';

    if (calWindow.opener == null) {
    	calWindow.opener = self;
    }
    calWindow.focus();
    
    return calWindow;
}

/******************************************************
	Function: addDays
	Purpose:  add the number of days to the date that passed in.
	
	Inputs:   date to be added to
			  the number of days
	Returns:  date result
*******************************************************/
function addDays(myDate, days)
{
	return new Date(myDate.getTime() + days*24*60*60*1000);
}

/******************************************************
	Function: addBusinessDays
	Purpose:  add the number of bussiness days to existing date
	
	Inputs:   1. date to be added to
			  2. the number of days
	Returns:  date result
*******************************************************/
function addBusinessDays(yourDate, days)
{
	myDate = yourDate;	
	var countNum = 0;
	var i;
	var dayNotInBusinessDays = true;
	if (isSaturday(yourDate)) 
	{
		yourDate = addDays(myDate, 2);
	}
	if (isSunday(yourDate))	
	{
		yourDate = addDays(myDate, 1);
	}
	myDate = yourDate;
	for (i = 0; i < days; i++)
	{	
		yourDate = addDays(myDate, 1);
		dayNotInBusinessDays = true;		
		while (dayNotInBusinessDays)
		{
			dayNotInBusinessDays = false;
			if (isSaturday(yourDate))
			{
				myDate = yourDate;
				yourDate = addDays(myDate, 2);
				dayNotInBusinessDays = true;
			}
			if (isSunday(yourDate))
			{
				myDate = yourDate;
				yourDate = addDays(myDate, 1);
				dayNotInBusinessDays = true;
			}
				
			while(isHoliday(yourDate)) {
				myDate = yourDate;
				yourDate = addDays(myDate, 1);
				dayNotInBusinessDays = true;
				//alert("holiday in while");
			}
			myDate = yourDate;
		}

	}
	return yourDate;
}

/******************************************************
	Function: addWeekendBuffer
	Purpose:  move date to weekday
	
	Inputs:   date
	Returns:  date result
*******************************************************/
function addWeekendBuffer(yourDate)
{
	var myDate = yourDate;	
    
    if (isSaturday(yourDate))
    { // if Saturday, add two days which is Monday
    	myDate = addDays(yourDate, 2);
    }
    else if (isSunday(yourDate))
    { // if Sunday, add one day which is Monday
    	myDate = addDays(yourDate, 1);
    }
    return myDate;
}

/******************************************************
	Function: isSaturday
	Purpose:  to determine if date falls on a Saturday
	
	Inputs:   date
	Returns:  true / false
*******************************************************/
function isSaturday(yourDate)
{	
	// first day of the month
	var firstDayOfMonth = new Date(yourDate.getFullYear(),yourDate.getMonth(),1);
	// day of the week based on the first day of the month.  day is in 0-6, 0 is Sunday
    var dayOfWeekInNum = firstDayOfMonth.getDay();
    
    return (((yourDate.getDate() + dayOfWeekInNum) %7) == 0) ? true : false;
}

/******************************************************
	Function: isSunday
	Purpose:  to determine if date falls on a Sunday
	
	Inputs:   date
	Returns:  true / false
*******************************************************/
function isSunday(yourDate)
{
	var firstDayOfMonth = new Date(yourDate.getFullYear(),yourDate.getMonth(),1);
    var dayOfWeekInNum = firstDayOfMonth.getDay();
 	return (((yourDate.getDate() + dayOfWeekInNum) %7) == 1) ? true : false;
}

function getMonth(strDate)
{	
	var sep = strDate.indexOf('/');	
	return strDate.substring(0, sep);
	
}
function getDay(strDate)
{
	var sep1 = strDate.indexOf('/') + 1;	
	var sep2 = strDate.lastIndexOf('/');
	return strDate.substring(sep1, sep2);
}

function getYear(strDate)
{
	var sep = strDate.lastIndexOf('/') + 1;
	return strDate.substring(sep, strDate.length);
}

function isWeekend(intDay, startDay)
{
	if ( (((intDay + startDay) %7) == 0) || (((intDay + startDay) %7) == 1) )
		return true;
	else
		return false;
		
}

function isThisMonthHoliday(intDay, thisMonthHolidayArray)
{
	var bHoliday = false;
	var hDay;
	
	if (thisMonthHolidayArray.length > 0)
    {
    	var j;
    	for (j = 0; j < thisMonthHolidayArray.length; j++)
		{
			hDay = thisMonthHolidayArray[j];
			if (intDay == hDay) 
			{
				bHoliday = true;
				break;
			}
		}
    }
    
    return bHoliday;	
}

/******************************************************
	Function: isHoliday
	Purpose:  determine if the date is in one of the holidays
	
	Inputs:   date
	Returns:  array of days
*******************************************************/
function isHoliday(yourDate)
{
	//alert("holiday=" + yourDate);
	
	var bHoliday = false;
	var hDay;
	var i;
	for (i = 0; i < HolArray.length; i++)
	{
        hdt = new Date(HolArray[i]);
		hDay   = hdt.getDate();
		hMonth = hdt.getMonth();  // month in 0-11
		hYear  = hdt.getFullYear();

		//alert("hDay=" + hDay + ", hMonth=" + hMonth + ", hYear=" + hYear + "...yourDate.getDate()=" + yourDate.getDate() + ", yourDate.getMonth()=" + yourDate.getMonth() + "yourDate.getFullYear()=" + yourDate.getFullYear())
		if (yourDate.getDate() == hDay && yourDate.getMonth() == hMonth  && yourDate.getFullYear() == hYear) {
			bHoliday = true;
			break;
		}
    }
    
    return bHoliday;	
}

/******************************************************
	Function: getThisMonthHoliday
	Purpose:  get the holiday days from the default date
	
	Inputs:   date
	Returns:  array of days
*******************************************************/
function getThisMonthHoliday(defaultSignupDate)
{
	// this month holidays
	var thisMonthHolidayArray = new Array();
	var iHoliday = 0;
	var numHolidays = 0;
	var todayDate = new Date();
	var i;
	for (i = 0; i < HolArray.length; i++)
	{
        hdt = new Date(HolArray[i]);
		hMonth = hdt.getMonth();
		hDay   = hdt.getDate();
		hYear  = hdt.getFullYear();
		//alert("Holiday=" + hMonth+"/"+hDay+"/"+hYear + " ----" + defaultSignupDate.getMonth());
		
		if (hMonth == defaultSignupDate.getMonth() && hYear == defaultSignupDate.getFullYear())
		{	
			thisMonthHolidayArray[iHoliday] = hDay;
			iHoliday++;			
		}		
    }
    
    return thisMonthHolidayArray;
}

/******************************************************
	Function: getNumHolidayForThisMonth
	Purpose:  get the number of holidays from the default date
	
	Inputs:   date
	Returns:  num of days
*******************************************************/
function getNumHolidayForThisMonth(defaultSignupDate)
{
	numHolidays = 0;
	var todayDate = new Date();
	for (i = 0; i < HolArray.length; i++)
	{
        hdt = new Date(HolArray[i]);
		hMonth = hdt.getMonth();
		hDay   = hdt.getDate();
		hYear  = hdt.getFullYear();
		//alert("Holiday=" + hMonth+"/"+hDay+"/"+hYear + " ----" + defaultSignupDate.getMonth());
		
		if (hMonth == defaultSignupDate.getMonth() && hYear == defaultSignupDate.getFullYear())
		{	
			// make sure the holiday doesn't fall between today date and the default date.
			// if it does, we need to add the number of holiday days to the default date
			if (todayDate.getDate() <= hDay && hDay <= defaultSignupDate.getDate())
			{
				numHolidays++;
			}
		}		
    }
    
    return numHolidays;
}

/******************************************************
	Function: verifyDate
	Purpose:  to check for the number of days must be at least 5 days from the current date
	
	Inputs:   date
	Returns:  true / false
*******************************************************/
function verifyDate(testDate)
{
	var revisedDate = new Date(testDate); 
	var minStartDate = parseInt(g_revisedStartDate)-parseInt(g_minStartDate);//Changed because server date and time needs to be considered. 
	revisedDate = addBusinessDays(new Date(testDate), minStartDate);	
	revisedDate = addBusinessDays(revisedDate, getNumHolidayForThisMonth(revisedDate));
	currentSelectedDate = new Date(testDate); 
	return ((Date.parse(currentSelectedDate.toDateString()) < Date.parse(revisedDate.toDateString())) ? true : false);

}
function makeArray0() {
    for (i = 0; i < makeArray0.arguments.length; i++)
        this[i] = makeArray0.arguments[i];
}

function testDate() {
	var todayDate = new Date();
	var i;
	
	var msg = "";
	
	for (i = 0; i < 365; i++) {
		defDate = addBusinessDays(todayDate, 2);
		busDate = addBusinessDays(todayDate, 5);
		msg = msg + "today: " + todayDate + "; signup date: " + defDate + "; revised date: " + busDate + "\n";
		todayDate = addDays(todayDate, 1);
	}
	
	return msg;
}

function testDate2 () {
	var signupDate = new Date();	
	alert("today is : " + signupDate);
	
	// set date as July 31, 2007
	// month in 0-11 with 0 being Jan.
		
	signupDate.setDate(31);		
    signupDate.setMonth(6);
    signupDate.setFullYear(2007);
	
	alert("July 31, 2007 in Javascript is: " + signupDate);
	
	// set date as July 31, 2007
	// month in 0-11 with 0 being Jan.
		
	signupDate.setDate(31);		
    signupDate.setMonth(6);
    signupDate.setFullYear(2007);
	
	alert("July 31, 2007 in Javascript is: " + signupDate);
	
}

var names     = new makeArray0('January','February','March','April','May','June','July','August','September','October','November','December');
var es_names  = new makeArray0('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
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');
var es_dow    = new makeArray0('Dom','Lun','Mar','Mier','Jue','Vie','Sab');
// make changes to the holiday array for ENTCR 8974.  The change is to temporary add June 26, 2007 as a holiday.
var HolArray = new Array("01/01/2007","05/28/2007","06/26/2007","07/04/2007","09/03/2007","11/22/2007","11/23/2007", "11/24/2007","12/24/2007","12/25/2007","01/01/2008","01/21/2008","03/21/2008","05/26/2008","07/04/2008","09/01/2008","11/27/2008","11/28/2008","12/24/2008","12/25/2008","12/26/2008")
//test 365 days var HolArray = new Array("01/01/2007","05/28/2007","06/26/2007","07/04/2007","09/03/2007","11/22/2007","11/23/2007", "11/24/2007","12/24/2007","12/25/2007","01/01/2008","01/21/2008","03/21/2008","05/26/2008","07/04/2008","09/01/2008","11/27/2008","11/28/2008","12/24/2008","12/25/2008","12/26/2008")
var g_minStartDate = 2;  // require minimum of 2 days before start service
var g_revisedStartDate = 5; // require minimum of 5 days before start service
var cutoffTime   = "14"; // cutoff time is 2 pm.  if this ever need to be changed, change should be updated as in military hours.