/*function buildCal(m, y, cM, cH, cDW, cD, brdr)
{
	initTip();
	var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

	var todaydate=new Date() //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	var t='<div class="'+cM+'"><table class="'+cM+'" cols="7" cellpadding="2" border="'+brdr+'" cellspacing="0"><tr align="center">';
	t+='<td colspan="7" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td></tr><tr align="center">';
	
	for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
		t+='</tr><tr align="center">';
	for(i=1;i<=42;i++)
	{
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		
		if(x==" " || x== "&nbsp;")
		{
			day="&nbsp;";
		}
		else
		{
			var dayevent_= m+''+x+''+y ;
			
			var pos=in_array(dayevent_ , dayevent);
				if (x==scanfortoday) //DD added
				{
					
					if(pos>-1)
					{
					      //alert('ok');
						day='<span id="today"><a  class="day1" style="cursor:pointer"  onmouseover="doTooltip(event,\''+formatJS(content_evt[pos])+'\','+m+','+x+','+y+')"  onclick="chose_add_or_view('+m+','+x+','+y+')"  onmouseout="hideTip()" ><font color="red">'+x+'</font></a></span>'; //DD added
					
					}
					else day='<span id="today"><a style="cursor:pointer"  href="create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' "  title="Add Event" ><font color="red">'+x+'</font></a></span>'; //DD added
				}
				else if(pos>-1)
					//day='<a class="day1" style="cursor:pointer"  onmouseover="doTooltip(event,'+m+','+x+','+y+')" onmouseout="hideTip()" href="create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' " >'+x+'</a>'; 
					day='<a class="day1" style="cursor:pointer"  onmouseover="doTooltip(event,\''+formatJS(content_evt[pos])+'\','+m+','+x+','+y+')"   onclick="chose_add_or_view('+m+','+x+','+y+')"  onmouseout="hideTip()" >'+x+'</a>'; 
				else
					day='<a style="cursor:pointer" href="create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' " title="Add Event" >'+x+'</a>'; 
			
		}
		t+='<td class="'+cD+'">'+day+'</td>';

		if(((i)%7==0)&&(i<36))
			t+='</tr><tr align="center">';
	}
	return t+='</tr></table></div>';
}

*/ 
function buildCal(m, y, cM, cH, cDW, cD, brdr)
{
	initTip();

	var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

	var todaydate=new Date() //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	var t='<div class="'+cM+'"><table class="'+cM+'" cols="7" cellpadding="2" border="'+brdr+'" cellspacing="0"><tr align="center">';
	t+='<td colspan="7" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td></tr><tr align="center">';
	
	for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
		t+='</tr><tr align="center" class="'+cD+'">';
	
	for(i=1;i<=42;i++)
	{
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		
		if(x==" " || x== "&nbsp;")
		{		
			if(i=="36") break;
			day="<td>&nbsp;</td>";
		}
		else
		{
			var dayevent_= m+''+x+''+y ;
			
			var pos=in_array(dayevent_ , dayevent);
				if (x==scanfortoday) //DD added
				{
					
					if(pos>-1)
					{
					      //alert('ok');
						day='<td  class="day1"  style="cursor:pointer"  onmouseover="doTooltip(event,\''+formatJS(content_evt[pos])+'\','+m+','+x+','+y+')"  onclick="chose_add_or_view('+m+','+x+','+y+')"  onmouseout="hideTip()" ><span id="today"><font color="red">'+x+'</font></span></td>'; //DD added
					
					}
					else day='<td style="cursor:pointer" onclick="window.location.href=\'create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' \' "  title="Add Event" > <span id="today"><font color="red">'+x+'</font></span></td>'; //DD added
				}
				else if(pos>-1)
					//day='<a class="day1" style="cursor:pointer"  onmouseover="doTooltip(event,'+m+','+x+','+y+')" onmouseout="hideTip()" href="create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' " >'+x+'</a>'; 
					day='<td  class="day1"  style="cursor:pointer"  onmouseover="doTooltip(event,\''+formatJS(content_evt[pos])+'\','+m+','+x+','+y+')"   onclick="chose_add_or_view('+m+','+x+','+y+')"  onmouseout="hideTip()" >'+x+'</td>'; 
				else
					day='<td style="cursor:pointer" onclick="window.location.href=\'create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' \' " title="Add Event"> '+x+'</td>'; 
			
		}
		//t+='<td class="'+cD+'">'+day+'</td>';
		t+=day;	
		if(((i)%7==0)&&(i<36))
			t+='</tr><tr align="center" class="'+cD+'">';
				
	}
	return t+='</tr></table></div>';
}


function in_array( what, where )
{
	var a=-1;
	
	for(var i=0;i<where.length;i++)
	{
	    //alert(what+':'+where[i]);
		if(what+'' == ''+(where[i]))
		{
		 //alert("OK");
		  a=i;
		  break;
		}
	}
     return a;
}




//========================================================================
//show tip



/***********************************************
* Image w/ description tooltip- By Dynamic Web Coding (www.dyn-web.com)
* Copyright 2002-2007 by Sharon Paine
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* IMPORTANT: Put script after tooltip div or 
	 put tooltip div just before </BODY>. */





var dom = (document.getElementById) ? true : false;
var ns5 = (!document.all && dom || window.opera) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ie4 && !ie5 && !dom) ? true : false;

var origWidth, origHeight;

// avoid error of passing event object in older browsers
if (nodyn) { event = "nope" }

///////////////////////  CUSTOMIZE HERE   ////////////////////
// settings for tooltip 
// Do you want tip to move when mouse moves over link?
var tipFollowMouse= true;	
// Be sure to set tipWidth wide enough for widest image
var tipWidth= 160;
var offX= 10;	// how far from mouse to show tip
var offY= 5; 
var tipFontFamily= "Verdana, arial, helvetica, sans-serif";
var tipFontSize= "8pt";
// set default text color and background color for tooltip here
// but don't have to
var tipFontColor= "#000000";
var tipBgColor= "#EFFFFF"; 
var tipBorderColor= "#000080";
var tipBorderWidth= 1;
var tipBorderStyle= "ridge";
var tipPadding= 0;
var clicked=false;
////////////////////  END OF CUSTOMIZATION AREA  ///////////////////
function  chose_add_or_view(m,x,y)
{	
	clicked=true;	
	tipOn = true;
	document.onmousemove = null;
	var DateID=m+''+x+''+y ;
	var string='<table  class="Grey11Bold" width="160" border="0" cellpadding="0" cellspacing="2">  <tr>   <td width="63" align="center"> <input type="button" name="addevent" id="addevent" value="Add" style="width:63px; height:18px"  class="Button6_rollover" onMouseOver="goLite(this)"  onMouseOut="goDim(this)" onClick="javascript: window.location.href= \' create_event_calendar.php?m='+m+'&d='+x+'&y='+y+' \' " />  </td>   <td width="63" align="center"> <input type="button" name="viewevent" id="viewevent" value="View" style="width:63px; height:18px"  class="Button6_rollover" onMouseOver="goLite(this)"  onMouseOut="goDim(this)"  onClick="javascript: window.location.href= \' event_calendar_many.php?DateID='+DateID+' \' "  /></td>   <td width="18" align="center"> <input type="button" name="close" id="close" value="X" style="width:18px; height:18px"  class="pagelist_arrow_forum" onMouseOver="goLite(this)"  onMouseOut="goDim(this)" onClick="hidechose() " /> </td>  </tr> </table> ';
	//var string='<table class="Grey11Bold" ><tr ><td > <a style="cursor:pointer" onclick="alert(\'add\');">add </a></td> <td >|</td><td >  <a style="cursor:pointer" onclick="alert(\'view\');">view </a></td><td >  <a style="cursor:pointer" onclick="hidechose();">close </a></td>  </tr></table> ';
	document.getElementById("tipDiv").innerHTML=string;
}

function hidechose() {
	if(clicked)
	{	if (!tooltip) return;
		t2=setTimeout("tipcss.visibility='hidden'",100);
		tipOn = false;
		clicked=false;
	}
}
////////////////////////////////////////////////////////////
//  initTip	- initialization for tooltip.
//	Global variables for tooltip. 
//		Set styles
//		Set up mousemove capture if tipFollowMouse set true.
////////////////////////////////////////////////////////////
var tooltip, tipcss;
function initTip() {
	if (nodyn) return;
	tooltip = (ie4)? document.all['tipDiv']: (ie5||ns5)? document.getElementById('tipDiv'): null;
	tipcss = tooltip.style;
	if (ie4||ie5||ns5) {	// ns4 would lose all this on rewrites
		tipcss.width = tipWidth+"px";
		tipcss.fontFamily = tipFontFamily;
		tipcss.fontSize = tipFontSize;
		tipcss.color = tipFontColor;
		tipcss.backgroundColor = tipBgColor;
		tipcss.borderColor = tipBorderColor;
		tipcss.borderWidth = tipBorderWidth+"px";
		tipcss.padding = tipPadding+"px";
		tipcss.borderStyle = tipBorderStyle;
	}
	if (tooltip&&tipFollowMouse) {
		document.onmousemove = trackMouse;
	}
}

//window.onload = initTip;

/////////////////////////////////////////////////
//  doTooltip function
//			Assembles content for tooltip and writes 
//			it to tipDiv
/////////////////////////////////////////////////
var t1,t2;	// for setTimeouts
var tipOn = false;	// check if over tooltip link


	
	/*	//------------------------------------
	// to layout image and text, 2-row table, image centered in top cell
	// these go in var tip in doTooltip function
	// startStr goes before image, midStr goes between image and text
	var startStr = '<table width="' + tipWidth + '"><tr><td  background="/images/icons/checkered_3x3_bg_dark.gif" class="Grey11Bold">Event </td></tr><tr><td><span style="font-family:' + tipFontFamily + '; font-size:' + tipFontSize + '; color:' + tipFontColor + ';"><div id="event_content"></div></span></td></tr><tr><td align="center" width="100%">';
	var midStr = '</td></tr><tr><td valign="top">';
	var endStr = '</td></tr></table>';
	//------------------------------------*/
function doTooltip(evt,evt_content,m,x,y)
{
	clicked=false;
	
	document.onmousemove = trackMouse;
	//------------------------------------
	// to layout image and text, 2-row table, image centered in top cell
	// these go in var tip in doTooltip function
	// startStr goes before image, midStr goes between image and text
	var startStr = '<table width="' + tipWidth + '"><tr><td  background="/images/icons/checkered_3x3_bg_dark.gif" class="Grey11Bold" align="right">'+m+'/'+x+'/'+y+' </td></tr><tr><td><span style="font-family:' + tipFontFamily + '; font-size:' + tipFontSize + '; color:' + tipFontColor + ';"><div id="event_content"></div></span></td></tr><tr><td align="center" width="100%">';
	var midStr = '</td></tr><tr><td valign="top">';
	var endStr = '</td></tr></table>';
	//------------------------------------
	
	if (!tooltip) return;
	if (t1) clearTimeout(t1);	if (t2) clearTimeout(t2);
	tipOn = true;
	
	curBgColor = tipBgColor;
	curFontColor = tipFontColor;
	if (ie4||ie5||ns5) {
		var tip = startStr  + midStr  + endStr;
		tipcss.backgroundColor = curBgColor;
	 	tooltip.innerHTML = tip;
		show_event(evt_content);
	

	}
	if (!tipFollowMouse) positionTip(evt);
	else t1=setTimeout("tipcss.visibility='visible'",100);
}

var mouseX, mouseY;
function trackMouse(evt) {
	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
	mouseX = (ns5)? evt.pageX: window.event.clientX + standardbody.scrollLeft;
	mouseY = (ns5)? evt.pageY: window.event.clientY + standardbody.scrollTop;
	if (tipOn) positionTip(evt);
}

/////////////////////////////////////////////////////////////
//  positionTip function
//		If tipFollowMouse set false, so trackMouse function
//		not being used, get position of mouseover event.
//		Calculations use mouseover event position, 
//		offset amounts and tooltip width to position
//		tooltip within window.
/////////////////////////////////////////////////////////////
function positionTip(evt) {
	if (!tipFollowMouse) {
		standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
		mouseX = (ns5)? evt.pageX: window.event.clientX + standardbody.scrollLeft;
		mouseY = (ns5)? evt.pageY: window.event.clientY + standardbody.scrollTop;
	}
	// tooltip width and height
	var tpWd = (ie4||ie5)? tooltip.clientWidth: tooltip.offsetWidth;
	var tpHt = (ie4||ie5)? tooltip.clientHeight: tooltip.offsetHeight;
	// document area in view (subtract scrollbar width for ns)
	var winWd = (ns5)? window.innerWidth-20+window.pageXOffset: standardbody.clientWidth+standardbody.scrollLeft;
	var winHt = (ns5)? window.innerHeight-20+window.pageYOffset: standardbody.clientHeight+standardbody.scrollTop;
	// check mouse position against tip and window dimensions
	// and position the tooltip 
	if ((mouseX+offX+tpWd)>winWd) 
		tipcss.left = mouseX-(tpWd+offX)+"px";
	else tipcss.left = mouseX+offX+"px";
	if ((mouseY+offY+tpHt)>winHt) 
		tipcss.top = winHt-(tpHt+offY)+"px";
	else tipcss.top = mouseY+offY+"px";
	if (!tipFollowMouse) t1=setTimeout("tipcss.visibility='visible'",100);
}

function hideTip() {
	if(!clicked)
	{	if (!tooltip) return;
		t2=setTimeout("tipcss.visibility='hidden'",100);
		tipOn = false;
	}
}

function show_event(evt_content)
{/*
	var pars = 'someParameter=ABC';
	var myAjax = new Ajax.Updater(
			'event_content', //containerid
			'getevent_calendar.php?m='+m+'&x='+x+'&y='+y, //url
			{
				method: 'get', 
				parameters: pars,
				evalScripts: true
			});
	*/
	document.getElementById("event_content").innerHTML="<center><br>Loading...</center>";
	document.getElementById("event_content").innerHTML=evt_content;
	
}

document.write('<div id="tipDiv" style="position:absolute; visibility:hidden; z-index:100"></div>')

		
//=======================================================================

var themonths=['January','February','March','April','May','June',
'July','August','September','October','November','December']
					
var todaydate=new Date()
var curmonth=todaydate.getMonth()+1 //get current month (1-12)
var curyear=todaydate.getFullYear() //get current year
var	temp_curmonth=curmonth;
var	temp_curyear=curyear;
var _curyear=curyear;

function updatecalendar(theselection)
{

	if(theselection>12)
	{	temp_curyear=_curyear+1;
		theselection=1;
	}
	if(theselection<1)
	{	temp_curyear=_curyear-1;
		theselection=12;
	}
	
	temp_curmonth=theselection;
	_curyear=temp_curyear;

	var themonth=theselection;
	var theyear=temp_curyear;
	var calendarstr=buildCal(themonth, theyear, "main", "month", "daysofweek", "days", 1)
	if (document.getElementById)
		document.getElementById("calendarspace").innerHTML=calendarstr

}
function update_current()
{	
	temp_curyear=curyear; //get default current year when next or prev out of current 
	temp_curmonth=curmonth;
	var calendarstr=buildCal(curmonth, curyear, "main", "month", "daysofweek", "days", 1)
	if (document.getElementById)
	document.getElementById("calendarspace").innerHTML=calendarstr
}
//=======================================================================					
					
function formatJS(str)
{
	str = str.replace(/&/g, "&amp;");
   	str = str.replace(/</g, "&lt;");
   	str = str.replace(/>/g, "&gt;");
    str = str.replace(/\"/g, "\\\"");
    str = str.replace(/\'/g, "\\'");
	str = str.replace(/\n/g, "\\n");
	str = str.replace(/\r/g, "\\r");
	str = str.replace(/\//g, "\\/");

	return str;
}
	
