// ===================================================================
// Author: Matt Kruse <mkruse@netexpress.net>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================
//  John Scott added passing in a list of divs 02/2003
//  put items you want to hide when calendar is visible in divs
//  then put div names in an array to pass to showCalendar
//  usage:
// 	<script>
//  	var HideDivsFrom = new Array('return_time', 'baggage');
//  </script>
//  <a name="calposition0"></a><a href="javascript:showCalendar('calendar','calposition0','showdate', 'HideDivsFrom','formName','date0');">
// 



var use_css=false;
var use_layers=false;

if (document.all)    { use_css    = true; }
if (document.layers) { use_layers = true; }

var CALWINDOW;

// Write out default styles to document or return string
function writestyles(doc) {
alert("in function");
	var result = "";
	result += "<STYLE>\n";
	result += "TD.cal { font-family:arial; font-size: 8pt; background-color:#EEEEEE;}\n";
	result += "TD.calmonth { font-family:arial; font-size: 8pt; text-align: center; background-color:#FFFFFF;}\n";
	result += "TD.caltoday { font-family:arial; background-color:#EEEEEE; font-size: 8pt; font-weight:bold; text-align: right; }\n";
	result += "INPUT.caltoday { font-family:arial; font-size: 8pt; width:47px; height: 20px; }\n";
	result += "A:link.cal, A:visited.cal { text-decoration:none; color:#0000F0; }\n";
	result += "A:link.calthismonth, A:visited.calthismonth { text-decoration:none; color:#000000; }\n";
	result += "A:link.calothermonth, A:visited.calothermonth { text-decoration:none; color:#CCCCCC; }\n";
	result += "</STYLE>\n";
	alert("string has been built");
	if (doc != "") {
		doc.write(result);
	} else {
		return result;
	}
}


//writestyles(this.document);

function getOffsetLeft (el) {
	var scrollamount = document.body.scrollLeft;
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
//	ol = ol - scrollamount;
	return ol;
}

function getOffsetTop (el) {
	var scrollamount = document.body.scrollTop;
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null) { ot += el.offsetTop; }
//	ot = ot - scrollamount;
//	alert(ot);
	return ot;
}
//

function showHideDivs(divList){
	for(var i=0;i<divList.length;i++) {
		if(divList[i] != undefined){
			var thisdiv = document.all[divList[i]].style;
			if (thisdiv.visibility == "visible" || !thisdiv.visibility) {
				thisdiv.visibility = "hidden";
			}
			else{
				thisdiv.visibility = "visible";
			}
		}
	}
	return;
}
//
function showHideDivsS(divList){
	for(var i=0;i<divList.length;i++) {
		var thisdiv = document.all[divList[i]].style;
		if (thisdiv.visibility == "visible" || !thisdiv.visibility) {
		    // hack jws 013103
		    if(!(divList[i] == "return_time" && document.all["return_date"].style.visibility == "hidden")){
			   thisdiv.visibility = "hidden";
			}
		}
		else{
			thisdiv.visibility = "visible";
		}
	}
	return;
}
function showCalendar(divname, anchorname, functionname, divListName, formName, form_elem) {
	if (arguments.length>4) {
		showHideDivs(eval(divListName));
	}
	// if ns4 make it a popup win
	if (document.layers){divname = '';}
	// Find position relative to anchor
	if (use_css) {
		var x = getOffsetLeft(document.all[anchorname]);
		var y = getOffsetTop(document.all[anchorname]);
		//alert("anchor="+anchorname+", x,y="+x+","+y);
	} else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name == anchorname) {
				found=1;
				break;
				}
			}
		if (found == 0) {
			return;
		}
		var x = document.anchors[i].x;
		var y = document.anchors[i].y;
		x=x-window.pageXOffset;
		y=y-window.pageYOffset;
	} else {
		return;
	}
	x = x;
	y = y+25;
	// WRITE CALENDAR TO DIV
	if (divname != "" && use_css) {
		// Position the calendar DIV
		var calendardiv = document.all[divname].style;
		if (calendardiv.visibility == "visible") {
			calendardiv.visibility = "hidden";
			return;
		}
		calendardiv.left = x;
		calendardiv.top  = y;
		// Write output to calendar DIV
		if (arguments.length>5) {
			outputCalendar(divname,functionname,divListName,arguments[4],arguments[5]);
		} else {
			outputCalendar(divname,functionname,divListName);
		}
		// Show the calendar DIV
				//calendardiv.bgcolor="#000000";

		calendardiv.visibility = "visible";
		calendardiv.position  = "absolute";
		calendardiv.left=x
		calendardiv.top=y
	} else {  // WRITE CALENDAR TO POPUP WINDOW
		if (use_layers) {
			var windowx = window.screenX;
			var windowy = window.screenY + (window.outerHeight-24-window.innerHeight);
		}
		if (use_css) {
			var windowx = window.screenLeft;
			var windowy = window.screenTop;
		}
		x = x + windowx;
		y = y + windowy;
				if(CALWINDOW && CALWINDOW.opened){alert ('about to close Calendar popup');CALWINDOW.close();}
		if (!CALWINDOW || CALWINDOW.closed) {
			CALWINDOW = window.open("about:blank","calwindow","status,width=148,height=150,screenX="+x+",left="+x+",screenY="+y+",top="+y+",resizable");
			CALWINDOW.focus();
		}
		// Write output to popup window
		if (arguments.length>5) {
			outputCalendar(divname,functionname,divListName,arguments[4], arguments[5]);
		} else {
			outputCalendar(divname,functionname,divListName);
		}
	}
}

function outputCalendar(divname, functionname, divListName, formName, form_elem) {
	var now = new Date();
	myDate = new Date();
	var dateElem='document.'+formName+'.'+form_elem+'.value';
	var dateString = eval(dateElem);
	if(dateString){
		if(myDate.setTime(Date.parse(dateString))){
			if(myDate.getFullYear()<1970){
				myDate.setFullYear(myDate.getFullYear()+100);
			}
			now = myDate;
		}
	}



	if (arguments.length > 5) { var
		month = arguments[5];
	} else {
		var month = now.getMonth()+1;
	}
	if (arguments.length > 6) {
		var year = arguments[6];
	} else {
		var year = now.getFullYear();
	}
	var monthnames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) { // leap year
		daysinmonth[2] = 29;
	}
	var current_month = new Date(year,month-1,1);

	var display_year = year;
	var display_month = month;
	var display_date = 1;

	var offset = 0;
	var weekday= current_month.getDay();
	if (weekday > 0) {
		display_month--;
		if (display_month < 1) { display_month = 12; display_year--; }
		display_date = daysinmonth[display_month]-weekday+1;
	}
	var next_month = month+1;
	var next_month_year = year;
	if (next_month > 12) { next_month=1; next_month_year++; }
	var last_month = month-1;
	var last_month_year = year;
	if (last_month < 1) { last_month=12; last_month_year--; }

	var date_class;
	var result = "";
	if (divname == "" ) {
		var windowref = "window.opener.";
	} else {
		var windowref = "";
	}
	// If POPUP, write entire HTML document
	if (divname == "") {
		result += "<HTML><HEAD>"+writestyles('')+"<SCRIPT LANGUAGE=\"Javascript 1.2\">";
		result += 'alert(\'hmm?\')';
		result += 'function changeWidth(newWidth) {';
		result += 'var orig_width = window.outerwidth;';
		result += 'window.outerwidth = newWidth;';
		result += 'return true;';
		result += '}';
		result += 'changeWidth(400);';
		result += '</SCRIPT>';
		result += "<BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0 onBlur=\"window.close();\">";
	}
	result += '<FORM>';
	if (divname != "") {
		result += '<TABLE WIDTH=144 bgcolor="#FFFFFF" BORDER=1 BORDERWIDTH=1 BORDERCOLOR="#001133" CELLSPACING=0 CELLPADDING=1>';
		result += '<TR><TD ALIGN=CENTER>';
		result += '<CENTER>';
		result += '<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>';
	} else {
		result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>';
	}

	result += '<TR BGCOLOR="#C0C0C0">';
	result += '	<TD CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\',\''+divListName+'\',\''+formName+'\',\''+form_elem+'\','+last_month+','+last_month_year+')">&lt;&lt;</A></B></TD>';
	result += '	<TD CLASS="cal" WIDTH=100 ALIGN=CENTER>'+monthnames[month-1]+' '+year+'</TD>';
	result += '	<TD CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\',\''+divListName+'\',\''+formName+'\',\''+form_elem+'\','+next_month+','+next_month_year+')">&gt;&gt;</A></B></TD>';
	result += '</TR>';
	result += '</TABLE>';
	result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>';
	result += '<TR>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>S</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>M</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>T</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>W</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>T</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>F</TD>';
	result += '	<TD CLASS="calmonth" ALIGN=CENTER WIDTH=14%>S</TD>';
	result += '</TR>';
	result += '<TR><TD COLSPAN=7 ALIGN=CENTER BGCOLOR="#CCCCCC"><IMG SRC="s.gif" WIDTH=120 HEIGHT=1></TD></TR>';
	for (var row=1; row<=6; row++) {
		result += '<TR>';
		for (var col=1; col<=7; col++) {
			if (display_month == month) {
				date_class = "calthismonth";
				}
			else {
				date_class = "calothermonth";
				}
			if ((display_month == now.getMonth()+1) && (display_date==now.getDate()) && (display_year==now.getFullYear())) {
				td_class="caltoday";
				}
			else {
				td_class="calmonth";
				}
//NOT***
			result += '	<TD CLASS="'+td_class+'"><A HREF="javascript:'+windowref+functionname+'('+display_year+','+display_month+','+display_date+',\''+formName+'\',\''+form_elem+'\');'+windowref+'hideCalendar(\''+divname+'\');'+windowref+'showHideDivs('+divListName+');" CLASS="'+date_class+'">'+display_date+'</A></TD>';
			display_date++;
			if (display_date > daysinmonth[display_month]) {
				display_date=1;
				display_month++;
				}
			if (display_month > 12) {
				display_month=1;
				display_year++;
				}
			}
		result += '</TR>';
		}
	result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="s.gif" WIDTH=120 HEIGHT=1></TD></TR>';
	result += '<TR>';
	result += '	<TD COLSPAN=7 ALIGN=CENTER>';
	
	result += '		<A HREF="javascript:'+windowref+'hideCalendar(\''+divname+'\');'+windowref+'showHideDivs('+divListName+');">Close</A>';
	//result += '		<A HREF="javascript:'+windowref+'hideCalendar(\''+divname+'\');'+windowref+'showHideDivs('+divListName+');"';
	//result += ' onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'closewin\',\'\',\'/AgentPlus/templates/eCharterConnect/images/Close_window_B.gif\',1)"><img src="/AgentPlus/templates/eCharterConnect/images/Close_window_O.gif" alt="Close Window" name="closewin" border="0"></A>';
	
	result += '	</TD>';
	result += '</TR>';
	result += '</TABLE>';
	result += '</CENTER>';
	result += '</TD></TR>';
	result += '</TABLE>';
	result += '</FORM>';
	if (divname == "") {
		result += "</BODY></HTML>";
		}

	if (divname != "") {
		if (use_css) {
			document.all[divname].innerHTML = result;
		}
		if (use_layers) {
			var thedoc = document.layers[divname].document;
			thedoc.open;
			thedoc.write(result);
			thedoc.close();
						document.layers[divname].bgColor = "#FFFCCF";
		}
	} else {
		CALWINDOW.document.open();
		CALWINDOW.document.write(result);
		CALWINDOW.document.close();
//        CALWINDOW.focus();
	}
}
 
 

 function hideCalendar(divname) {
	if (divname != "") {
		if (use_layers) { var calendardiv = document.layers[divname]; }
		if (use_css)    { var calendardiv = document.all[divname].style;}
		calendardiv.visibility = "hidden";
	} else {
		CALWINDOW.close();
	}
}

// image rollover functions


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

