// ********************
// Begin Popup Calendar
// ********************

//global variables
var popCalDstFld;
var popCalDstFmt;
var temp;
var popCalDiv;
var IntervalLength = 36525; //Default upper limit of enabled dates - about 100 years.
var Sel1, Sel2, Sel3;
var FirefoxSel1, FirefoxSel2, FirefoxSel3;
var calArea;

function popupCal() {
 	var tmpDate = new Date();
	var tmpString = "";
	var tmpNum = 0;
	var popCalDateVal;
	var dstWindowName = "";


	//if (popCalDiv) closeCalPopup();
//	if (calArea) closeCalPopup();
	//Initialize reference to popup calendar DIV element to an empty object.
	popCalDiv = new Object();
	
	//Check for the right number of arguments
	if (arguments.length < 2)
	{
		alert("popupCal(): Wrong number of arguments.");
		return void(0);
	}
	//Get the command line arguments -- Localization is optional
	dstWindowName = popupCal.arguments[0];
	popCalDstFld = popupCal.arguments[1];
	temp = popupCal.arguments[1];
	toHidden = popupCal.arguments[1];
	popCalDstFmt = popupCal.arguments[2];  //Localized Short Date Format String
	popCalMonths = popupCal.arguments[3];  //Localized Month Names String
	popCalDays = popupCal.arguments[4];    //Localized Day Names String
	popInterval = popupCal.arguments[5];   // negative value - enable all days
	calArea = popupCal.arguments[3]; 							// 0  - disable all days before current date
											// positive value - disable all days before current date 
											// and also popInterval working days forward
    
    Sel1 = popupCal.arguments[7];
    Sel2 = popupCal.arguments[8];
    Sel3 = popupCal.arguments[9];
    FirefoxSel1 = popupCal.arguments[7];
    FirefoxSel2 = popupCal.arguments[8];
    FirefoxSel3 = popupCal.arguments[9];

    //if IntervalLength is passed as input argument and positive - set its value.
    if ((typeof(popupCal.arguments[6]) != "undefined") && (popupCal.arguments[6] > 0))
	  IntervalLength = popupCal.arguments[6];  

	//check destination field name
	if (popCalDstFld != "")
	  popCalDstFld = document.getElementById(popCalDstFld);

	//default localized short date format if not provided
	if (popCalDstFmt == "")
	  popCalDstFmt = "M/d/yyyy";

	//default localized months string if not provided
	if (popCalMonths == "")
	  popCalMonths = "January,February,March,April,May,June,July,August,September,October,November,December";
 
	//default localized months string if not provided
	if (popCalDays == "")
	  popCalDays = "Sun,Mon,Tue,Wed,Thu,Fri,Sat";
 
	//default popInterval if not provided
	if(typeof(popInterval) == "undefined")
	  popInterval=-1;

	tmpString = new String(popCalDstFld.value);
	//If tmpString is empty (meaning that the field is empty) 
	//use todays date as the starting point
	if(tmpString == "")
		popCalDateVal = new Date()
	else
	{
		//Make sure the century is included, if it isn't, add this 
		//century to the value that was in the field
		tmpNum = tmpString.lastIndexOf( "/" );
		if ( (tmpString.length - tmpNum) == 3 )
		{
			tmpString = tmpString.substring(0,tmpNum + 1) + Math.floor(tmpDate.getFullYear()/100) + tmpString.substr(tmpNum+1);
			popCalDateVal = new Date(tmpString);
		}
		else
		{
			//If we got to this point, it means the field that was passed 
			//in had a 4 digit number after the last slash.  Try to convert 
			//it to a date
			popCalDateVal = new Date(tmpString.toString());
		}
	}
	
	//Make sure the date is a valid date.  Set it to today if it is invalid
	//"NaN" is the return value for an invalid date
	if( popCalDateVal.toString() == "NaN" )
	{
		popCalDateVal = new Date();
		popCalDstFld.value = "";
	}

	// ------------ AB 18-Dec-2004: ----------------
	// If popInterval>0 then check that the month to be shown has at least one enabled date,
	// otherwise, show calendar starting from the first enabled date
	 
	if (popInterval>0)
	{
		// set EnabledDate to 00:00:00 of current date
		var EnabledDate = new Date("" + (tmpDate.getMonth()+1) + "/" + tmpDate.getDate() + "/" + tmpDate.getFullYear());

		//scroll EnabledDate forward by popInterval working days
		var WorkingDaysCount = popInterval;
		do
		{
			if ((EnabledDate.getDay() > 0) && (EnabledDate.getDay() < 6))
			{
				WorkingDaysCount--;
			}
			EnabledDate.setTime(EnabledDate.getTime() + 24*60*60*1000);
		} while (WorkingDaysCount > 0);

		//if as a result it happened that EnabledDate is greater than popCalDateVal
		//then start calendar with the month to which EnabledDate belongs
		if (EnabledDate.getTime()>popCalDateVal.getTime())
		{
			popCalDateVal.setTime(EnabledDate.getTime());
		}
	}
	//------- End of changes AB 18-Dec-2004 -------

	//Set the base date to midnight of the first day of the specified month, 
	//this makes things easier?
	var dateString = String(popCalDateVal.getMonth()+1) + "/" + String(popCalDateVal.getDate()) + "/" + String(popCalDateVal.getFullYear());

	//Call the routine to draw the initial calendar
	reloadCalPopup(dateString, dstWindowName, popInterval);
	
	return void(0);
}
 
function closeCalPopup() {

    popCalDiv.style.visibility = "hidden";
    calArea.style.visibility = "hidden";
    split_date(Sel1, Sel2, Sel3);
  
	return void(0);
}
 
function reloadCalPopup() //[0]dateString, [1]dstWindowName, [2]popInterval
{
	var tmpDate = new Date( reloadCalPopup.arguments[0] );
	if (tmpDate.toString() == "Invalid Date")
	    tmpDate = new Date();
	tmpDate.setDate(1);
	
	//Get the calendar data
	var popCalData = calPopupSetData(tmpDate,reloadCalPopup.arguments[1],reloadCalPopup.arguments[2]);
 
	//Check to see if the window has been initialized, create it if it hasn't been
	
	if( popCalDiv.toString() == "[object Object]" )
	{
		popCalDiv=document.createElement("DIV");
		popCalDiv.style.position = "absolute";
		popCalDiv.style.borderStyle = "solid";
		popCalDiv.style.borderWidth = "1px";
		
		//Center calendar in browser window
		popCalDiv.style.left = "" + (document.body.scrollLeft + Math.floor(document.body.clientWidth/2)+75) + "px";
		popCalDiv.style.top = "" + (document.body.scrollTop + Math.floor(document.body.clientHeight/2) - 102) + "px";

		popCalDiv.style.backgroundColor = "#F1F1F1";
		popCalDiv.style.align = "center"; 
	}
	//popCalDiv.innerHTML = popCalData;
	calArea.style.visibility = "visible";
	calArea.innerHTML = popCalData;
	//document.body.appendChild(popCalDiv);
	
	return void(1);
}

function calPopupSetData(firstDay,dstWindowName,popInterval)
{
	var popCalData = "";
	var lastDate = 0;
	var fnt = new Array;
	fnt[0] = "<FONT FACE=\"Arial,Helvetica,sans-serif\" SIZE=\"1\">";
	fnt[1] = "<B><FONT FACE=\"Arial,Helvetica,sans-serif\" SIZE=\"2\">";
	fnt[2] = "<B><FONT FACE=\"Arial,Helvetica,sans-serif\" SIZE=\"2\" COLOR=\"#EF741D\">";
	var dtToday = new Date();
	var dtTodayValue = dtToday.setHours(0,0,0,0).valueOf();
	var thisMonth = firstDay.getMonth();
	var thisYear = firstDay.getFullYear();
	var nPrevMonth = (thisMonth == 0 ) ? 11 : (thisMonth - 1);
	var nNextMonth = (thisMonth == 11 ) ? 0 : (thisMonth + 1);
	var nPrevMonthYear = (nPrevMonth == 11) ? (thisYear - 1): thisYear;
	var nNextMonthYear = (nNextMonth == 0) ? (thisYear + 1): thisYear;
	var sToday = String((dtToday.getMonth()+1) + "/01/" + dtToday.getFullYear());
	var sPrevMonth = String((nPrevMonth+1) + "/01/" + nPrevMonthYear);
	var sNextMonth = String((nNextMonth+1) + "/01/" + nNextMonthYear);
	var sPrevYear1 = String((thisMonth+1) + "/01/" + (thisYear - 1));
	var sNextYear1 = String((thisMonth+1) + "/01/" + (thisYear + 1));
	var tmpDate = new Date( sNextMonth );
	
	tmpDate = new Date( tmpDate.valueOf() - 1001 );
	lastDate = tmpDate.getDate();

	if (this.popCalMonths.split) // javascript 1.1 defensive code
	{
		var monthNames = this.popCalMonths.split(",");
		var dayNames = this.popCalDays.split(",");
	}
	else  // Need to build a js 1.0 split algorithm, default English for now
	{
		var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var dayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
	}

	var cellAttribs  = "align=\"center\" BGCOLOR=\"#F1F1F1\" onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\"";
	var closeAnchor = "<CENTER><input type=button value=\"Close\" onClick=\"javascript:closeCalPopup()\">"+fnt[0]+"<BR>&nbsp;</FONT></CENTER>\n";
	var todayAnchor = "<A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:reloadCalPopup('"+sToday+"','"+dstWindowName+"','"+popInterval+"');\">Today</A>";
	var prevMonthAnchor = "<A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:reloadCalPopup('"+sPrevMonth+"','"+dstWindowName+"','"+popInterval+"');\">" + monthNames[nPrevMonth] + "</A>";
	var nextMonthAnchor = "<A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:reloadCalPopup('"+sNextMonth+"','"+dstWindowName+"','"+popInterval+"');\">" + monthNames[nNextMonth] + "</A>";
	var prevYear1Anchor = "<A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:reloadCalPopup('"+sPrevYear1+"','"+dstWindowName+"','"+popInterval+"');\">"+(thisYear-1)+"</A>";
	var nextYear1Anchor = "<A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:reloadCalPopup('"+sNextYear1+"','"+dstWindowName+"','"+popInterval+"');\">"+(thisYear+1)+"</A>";
		
	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"215\">\n<TR>\n<TD width=\"35\">&nbsp</TD>\n");
	popCalData += ("<TD width=\"40\" align=\"center\" " + cellAttribs);
	popCalData += (" >\n");
	popCalData += (fnt[0]+prevYear1Anchor+"</FONT>\n</TD>\n");
	popCalData += ("<TD width=\"65\" align=\"center\" "+cellAttribs);
	popCalData += (" >\n");
	popCalData += (fnt[0]+todayAnchor+"</FONT>\n</TD>\n");
	popCalData += ("<TD width=\"40\" align=\"center\" "+cellAttribs);
	popCalData += (" >\n");
	popCalData += (fnt[0]+nextYear1Anchor+"</FONT>\n</TD>\n<TD width=\"35\">&nbsp</TD>\n");
	popCalData += ("</TR>\n</TABLE>\n");

	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"215\">\n");          
	popCalData += ("<TR>\n<TD width=\"45\" align=\"center\" "+cellAttribs);
	popCalData += (" >\n");
	popCalData += (fnt[0] + prevMonthAnchor + "</FONT></TD>\n");
	popCalData += ("<TD width=\"125\" align=\"center\">\n");
	popCalData += ("&nbsp;&nbsp;"+fnt[1]+"<FONT COLOR=\"#000000\">" + monthNames[thisMonth] + ", " + thisYear + "&nbsp;&nbsp;</FONT></B></TD>\n");
	popCalData += ("<TD width=\"45\" align=\"center\" "+cellAttribs);
	popCalData += (" >\n");
	popCalData += (fnt[0]+nextMonthAnchor+"</FONT>\n</TD>\n</TR>\n</TABLE>\n"+fnt[0]+"<BR></FONT>\n");       

	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" cellpadding=\"0\"  width=\"215\">\n" );
	popCalData += ("<TR>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[0]+"</FONT></B></TD>\n<TD  bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">");
	popCalData += (fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[1]+"</FONT></B></TD>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[2]+"</FONT></B></TD>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">");
	popCalData += (fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[3]+"</FONT></B></TD>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[4]+"</FONT></B></TD>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">");
	popCalData += (fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[5]+"</FONT></B></TD>\n<TD bgcolor=\"#CCCCCC\" width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[6]+"</FONT></B></TD>\n</TR>\n");

	var calDay = 0;
	var monthDate = 1;
	var weekDay = firstDay.getDay();

	//calculate upper limit of enabled dates
	var UpperLimitDate = new Date(); 
	UpperLimitDate.setDate(UpperLimitDate.getDate()+IntervalLength);
	
	// "if" statement below skips Saturdays and Sundays and applies popInterval
	// to working days only. Remove or comment everything between "begin" and
	// "end" marks to apply popInterval to any days.
	// ------ begin ------
	if (popInterval>0)
	{
		var WorkingDaysCount = popInterval;
		tmpDate.setTime(dtToday.getTime());
		do
		{
			if ((tmpDate.getDay() == 0) || (tmpDate.getDay() == 6))
			{
				popInterval++;
			}
			else
			{
				WorkingDaysCount--;
			}
			tmpDate.setTime(tmpDate.getTime() + 24*60*60*1000);
			
		} while (WorkingDaysCount > 0);
	}
	//----- end ----
	
	do
	{
		popCalData += ("<TR>\n");
		for (calDay = 0; calDay < 7; calDay++ )
		{
			if((weekDay != calDay) || (monthDate > lastDate))
			{
				popCalData += ("<TD width=\"30\">"+fnt[1]+"&nbsp;</FONT></B></TD>\n");
				continue;
			}
			else
			{
				var current = new Date(thisYear,thisMonth,monthDate)
				if (((popInterval<0) || (current.valueOf()+ 1001 >= dtTodayValue + popInterval*24*60*60*1000)) && (current.valueOf() < UpperLimitDate.valueOf()))
				{
					anchorVal = "  <A STYLE=\"text-decoration:none;color:#364180\" HREF=\"javascript:calPopupSetDate(popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');closeCalPopup()\">\n    ";
					jsVal = "javascript:calPopupSetDate(popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');closeCalPopup()";

					popCalData += ("<TD width=\"30\" "+cellAttribs+" onClick=\""+jsVal+"\">\n");
				
					if ((firstDay.getMonth() == dtToday.getMonth()) && (monthDate == dtToday.getDate()) && (thisYear == dtToday.getFullYear()) )
						popCalData += (anchorVal+fnt[2]+monthDate+"</FONT></B></A></TD>\n");
					else
						popCalData += (anchorVal+fnt[1]+monthDate+"</FONT></B></A></TD>\n");

				}
				else
				{
					popCalData += ("<TD width=\"30\" align=\"center\" style=\"text-decoration:line-through;color:#777777\">"+fnt[1]+monthDate+"</FONT></B></TD>\n");
				}
				weekDay++;
				monthDate++;
			}
		}
		weekDay = 0;
		popCalData += "</TR>\n";
	} while( monthDate <= lastDate );
	
	popCalData += ("</TABLE>\n"+fnt[0]+"<BR></FONT>\n");
	popCalData += closeAnchor;

	return( popCalData );
}

function calPopupSetDate()
{
	calPopupSetDate.arguments[0].value = calPopupSetDate.arguments[1];
}

// utility function
function padZero(num)
{
	return ((num <= 9) ? ("0" + num) : num);
}

// Format short date
function constructDate(d,m,y)
{
	var fmtDate = this.popCalDstFmt
	fmtDate = fmtDate.replace ('dd', padZero(d))
	fmtDate = fmtDate.replace ('d', d)
	fmtDate = fmtDate.replace ('MM', padZero(m))
	fmtDate = fmtDate.replace ('M', m)
	fmtDate = fmtDate.replace ('yyyy', y)
	fmtDate = fmtDate.replace ('yy', padZero(y%100))
	return fmtDate;
}

function focusField(field) {
	if(field.value == "MM/DD/YYYY") {
		field.value = "";	
	}
}

function blurField(field) {
	if(field.value == "") {
		field.value = "MM/DD/YYYY";
	}
}

// ******************
// End Popup Calendar
// ******************

function split_date() {
 
var	dstSelect1 = split_date.arguments[0];
var	dstSelect2 = split_date.arguments[1];
var	dstSelect3 = split_date.arguments[2];
	if (dstSelect1!="" && dstSelect2!="" && dstSelect3!="") {

    var arrDate = document.getElementById(toHidden).value.split("/");
    var day = new Number(arrDate[1]);
    var month = new Number(arrDate[0]);
    var year = new Number(arrDate[2]);
   //FirefoxSel1
    if (month > 0) {
       
       // document.getElementById(dstSelect2).options[month - 1].selected = true;
        dstSelect2.options[month - 1].selected = true;
     //   document.getElementById('ctl00_popupScheduleAnAppointment_ddlPreferedMeetingMonth').options[month - 1].selected = true;
       
    }
    if (day > 0) {
        // document.getElementById(dstSelect1).options[day - 1].selected = true;
        dstSelect1.options[day - 1].selected = true;
    }
    var yearBox = dstSelect3; //document.getElementById(dstSelect3);
    for (i=0;i<yearBox.length;++i)
    {
   if (yearBox.options[i].text==year)
    {yearBox.options[i].selected=true; break;}
   }
   }
}

function Date1(){
   var d, s = "";           
   d = new Date();                           
   month = d.getMonth() + 1;            
   day = d.getDate();                 
   year = d.getFullYear();
   
   s=month+"/"+day+"/"+year;
                           
   document.getElementById("text1").value=s;
   document.getElementById("text2").value=s;
   document.getElementById("month").options[month-1].selected=true;
   document.getElementById("day").options[day-1].selected=true;
   var yearBox = document.getElementById("year");
    for (i=0;i<yearBox.length;++i)
    {
    if (yearBox.options[i].text==year)
    {yearBox.options[i].selected=true; break;}
    }
   
	document.getElementById("month2").options[month-1].selected=true;
	document.getElementById("day2").options[day-1].selected=true;
	var yearBox = document.getElementById("year2");
    for (i=0;i<yearBox.length;++i)
    {
    if (yearBox.options[i].text==year)
    {yearBox.options[i].selected=true; break;}
    }
   
   Date1=s;   
}

function Date2(){
   var d, s = "";           
   d = new Date();                           
   month = d.getMonth() + 1;            
   day = d.getDate();                 
   year = d.getFullYear();
   
   s=month+"/"+day+"/"+year;
                           
   document.getElementById("text1").value=s;
   document.getElementById("month").options[month-1].selected=true;
   document.getElementById("day").options[day-1].selected=true;
   var yearBox = document.getElementById("year");
    for (i=0;i<yearBox.length;++i)
    {
    if (yearBox.options[i].text==year)
    {yearBox.options[i].selected=true; break;}
    }
   
   Date2=s;   
}

function DateToHidden(field1Id, field2Id, field3Id, destField)
{
var s1 = document.getElementById(field1Id).value;
var s2 = document.getElementById(field2Id).value;
var s3 = document.getElementById(field3Id).value;

var s = s2+"/"+s1+"/"+s3;

document.getElementById(destField).value=s;
}



function Date3(){
   var d1, s1 = "";           
   d1 = new Date();                           
   month1 = d1.getMonth() + 1;            
   day1 = d1.getDate();                 
   year1 = d1.getFullYear();
   
   s1=month1+"/"+day1+"/"+year1;  
 
   document.getElementById("text1").value=s1;
   document.getElementById("text2").value=s1;
   document.getElementById("month").options[month1-1].selected=true;
   document.getElementById("day").options[day1-1].selected=true;
 
   var yearBox1 = document.getElementById("year");
 
    for (i1=0;i1<yearBox1.length;++i1)
    {
    if (yearBox1.options[i1].text==year1)
    {yearBox1.options[i1].selected=true; break;}
    } 
    
  
  Date3 = s1;
}