var g_CurrentJumpPopup = null;
function KSCP_CreateJumpCalendarMenu(yearDivId, monthDivId, buttDivId, startDateId, sWPQ, year, month)
{
	g_CurrentJumpPopup = CMenu("calendarmenu");
	g_CurrentJumpPopup.setAttribute("CompactMode", "true");
	KSCP_CreateHtmlMenuItem(g_CurrentJumpPopup, document.all[yearDivId + sWPQ].innerHTML);
	KSCP_CreateHtmlMenuItem(g_CurrentJumpPopup, document.all[monthDivId + sWPQ].innerHTML);
	KSCP_CreateHtmlMenuItem(g_CurrentJumpPopup, document.all[buttDivId + sWPQ].innerHTML);
	OMenu(g_CurrentJumpPopup, event.srcElement, null, null, -1);
	
/*	var allTables = menu._arrPopup[0].all.tags("table");
	for(var i = 0; i < allTables.length; i++)
	{
		allTables[i].border = "1";
	}*/
	var allTr = g_CurrentJumpPopup._arrPopup[0].all.tags("tr");
	for(var i = 0; i < allTr.length; i++)
	{
		if(allTr[i].type == "option")
		{
			for(var ii = 0; ii < allTr[i].cells.length; ii++)
			{
				allTr[i].cells[ii].style.cursor = "auto";
			}
		}
	}
    
	g_CurrentJumpPopup._arrPopup[0].onkeydown=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmousedown=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmouseover=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmouseout=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onclick=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmouseover=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmouseout=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onmousedown=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].onclick=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].oncontextmenu=function(){event.returnValue = false; event.cancelBubble = true; return false;};
	g_CurrentJumpPopup._arrPopup[0].style.cursor="auto";
	setTimeout(KSCP_ClearTimeOutToHideMenu, 100);
}

function KSCP_JumpDate( yearDivId, monthDivId, sWPQ, view_period, day, format)
{
	var year = g_CurrentJumpPopup._arrPopup[0].all["cb_" + yearDivId + sWPQ].options[g_CurrentJumpPopup._arrPopup[0].all["cb_" + yearDivId + sWPQ].selectedIndex].value;
	var month = g_CurrentJumpPopup._arrPopup[0].all["cb_" + monthDivId + sWPQ].options[g_CurrentJumpPopup._arrPopup[0].all["cb_" + monthDivId + sWPQ].selectedIndex].value;
	var date = format.replace(/year/gi, year).replace(/month/gi, month).replace(/day/gi, day);
	kwiz_cal_MoveToViewDate(date, view_period, sWPQ);
	MenuHtc_hide();
}

function KSCP_CreateHtmlMenuItem(menu, html)
{
	var mi=CAMOpt(menu, "", "", "");
	mi.onMenuClick = null;
	mi.innerHTML = html;
}
function KSCP_ClearTimeOutToHideMenu()
{
	if (document.body.getAttribute("HideMenuTimeOut") !=null)
	{
		ClearTimeOut("HideMenuTimeOut");
	}
	else
	{
		setTimeout(KSCP_ClearTimeOutToHideMenu, 100);
	}
}
function KSCP_ShowLogin(logintype, sWPR)
{
	window.showModalDialog(sWPR + '/ModalContainer.htm', '/_layouts/KWizCom_KSCP/LoginAs.aspx?logintype=' + logintype, 'dialogHeight: 320px; dialogWidth:600px;edge:Raised;center:Yes;help:No;scroll:No;unadorned:No;resizable:No;status:No;');
}

function KSCP_ExportData(logintype, sWPR)
{
	window.showModalDialog(sWPR + '/ModalContainer.htm', '/_layouts/KWizCom_KSCP/LoginAs.aspx?logintype=' + logintype, 'dialogHeight: 320px; dialogWidth:600px;edge:Raised;center:Yes;help:No;scroll:No;unadorned:No;resizable:No;status:No;');
}

function KSCP_RenderCalendar(parent, sWPQ, url, data, hidObj,  imageUrl)
{
	parent.isasync = true;
	if(url != null)
	{
		parent.url = url;
		parent.hidobj = hidObj;
		parent.data = data;
		parent.imageurl = imageUrl;
		parent.monthviewext = "";
		parent.gotodateext = "";
	}
	else
	{
		url = parent.url;
		data = parent.data;
		imageUrl = parent.imageurl;
	}
	url = parent.url + parent.hidobj.value;
	
	this.Parent = parent;
	this.Url = url;
	this.Data = data;
	this.HttpRequest = null;
	this.LastReadyStatus = -1;
	var headObj = parent.all["navigationHeaderButonsDiv"];
	if(headObj == null)
		parent.innerHTML = "<table border='0' class='ms-cal-header' cellspacing='15' cellpadding='15' width='100%' height='100%'><tr><td nowrap valign='middle'><img src='" + imageUrl + "'></td></tr></table>";
	else
	{
		headObj.style.height = headObj.offsetHeight;
		headObj.style.width = headObj.offsetWidth;
		headObj.innerHTML = "<img src='" + imageUrl + "'>";
	}
	KSCP_SendHttpRequest(this.Url, this.Data);
	function KSCP_CreateXMLHttpRequestObject()
	{
		try
		{
			var httpRequest = null;
			if( window.ActiveXObject )  // IE
			{
				var MSXML_XMLHTTP_PROGIDS = new Array(
								'Microsoft.XMLHTTP',
								'MSXML2.XMLHTTP.5.0',
								'MSXML2.XMLHTTP.4.0',
								'MSXML2.XMLHTTP.3.0',
								'MSXML2.XMLHTTP'
								);
				for (var i=0; i < MSXML_XMLHTTP_PROGIDS.length; i++)
				{
					httpRequest = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
					if( httpRequest != null )
					{
						break;
					}
				}
			}
			else if( window.XMLHttpRequest ) // // Mozilla, Safari,...
			{
				var httpRequest = new XMLHttpRequest();
				httpRequest.overrideMimeType('text/xml');
			}
			if(!httpRequest)
			{            
				alert("Failed to create the XmlHttpRequest Object");
			}  
			return httpRequest;
		}
		catch(e)
		{
		}
	}

	function KSCP_ReadyStateChanged()
	{
		var XMLHTTPREQUEST_COMPLETE = 4;
		var XMLHTTPREQUEST_OK = 200;
		var XMLHTTPREQUEST_REDIRECT = 302;
		var XMLEXPIRATION = 288;             
		var currentState = null;        
		var httpCode = null;
	    
		try
		{
			currentState = this.HttpRequest.readyState;           
		}
		catch(e)
		{
			//Handle exception here             
		}
	                
		try
		{        
			if ((currentState == 0 || currentState == XMLHTTPREQUEST_COMPLETE) && this._lastReadyStatus != XMLHTTPREQUEST_COMPLETE) 
			{                        
				try
				{
					httpCode = this.HttpRequest.status;                
				}
				catch(e)
				{                
					httpCode = 377;                
				}
	                      
				this.LastReadyStatus = currentState;    
	            
				if (httpCode == XMLHTTPREQUEST_OK)
				{   
					parent.innerHTML =  this.HttpRequest.responseText;
				}            
				else if (currentState != 0)
				{   
					alert("The operation failed with HTTP code " + httpCode);
				}
			}
			else
			{
				this.LastReadyStatus = currentState;   
			}        
		}
		catch(e)
		{        
			alert("The operation failed: " + e.description);
		}
	}

	function KSCP_SendHttpRequest(url, data)
	{   
		try
		{		   				
			this.HttpRequest = KSCP_CreateXMLHttpRequestObject();
			this.HttpRequest.open("POST", url, true);
			this.HttpRequest.onreadystatechange = KSCP_ReadyStateChanged;
			this.HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.HttpRequest.send(data);
		}
		catch(e)   
		{
			alert("Failed to send XmlHttpRequest");
		}
	}
}

function kwiz_cal_FormatQueryString(q)
{
	var parent = document.all["WebPart" + q];
	var urlExt = "&ms-cal-startdate" + q + "=" + document.all["ms-cal-startdate" + q].value;
	urlExt += "&ms-cal-viewperiod" + q + "=" + document.all["ms-cal-viewperiod" + q].value;
	urlExt += "&ms-cal-expandedweek" + q + "=" + document.all["ms-cal-expandedweek" + q].value;
	urlExt += "&ms-cal-viewstyle" + q + "=" + document.all["ms-cal-viewstyle" + q].value;
	urlExt += "&ms-cal-hidedaysof" + q + "=" + document.all["ms-cal-hidedaysof" + q].value;
	parent.hidobj.value = urlExt;
}
function kwiz_cal_MoveToHideDaysOf(val, q)
{
	var parent = document.all["WebPart" + q];
	document.all["ms-cal-hidedaysof" + q].value = val;
	if((parent != null) && (parent.isasync == true))
	{
		kwiz_cal_FormatQueryString(q);
		var tmp = new KSCP_RenderCalendar(parent, q);
	}
	else
	{
		document.all["ms-cal-hidedaysof" + q].form.submit();
	}
}
function kwiz_cal_MoveToViewStyle(view_style, q)
{
	var parent = document.all["WebPart" + q];
	document.all["ms-cal-viewstyle" + q].value = view_style;
	if((parent != null) && (parent.isasync == true))
	{
		kwiz_cal_FormatQueryString(q);
		var tmp = new KSCP_RenderCalendar(parent, q);
	}
	else
	{
		document.all["ms-cal-viewstyle" + q].form.submit();
	}
}
function kwiz_cal_MoveToViewDate(strdate, view_type, q)
{
	if(document.all["ms-cal-intigwspscal" + q].value == 0)
	{
		var parent = document.all["WebPart" + q];
		document.all["ms-cal-startdate" + q].value = strdate;
		document.all["ms-cal-viewperiod" + q].value = view_type;
		if((parent != null) && (parent.isasync == true))
		{
			kwiz_cal_FormatQueryString(q);
			var tmp = new KSCP_RenderCalendar(parent, q);
		}
		else
		{
			document.all["ms-cal-viewperiod" + q].form.submit();
		}
	}
	else
	{
		MoveToViewDate(strdate, view_type);
	}
}


function kwiz_cal_GetMonthView(str, q)
{
	var parent = document.all["WebPart" + q];
	document.all["ms-cal-expandedweek" + q].value = str;
	if((parent != null) && (parent.isasync == true))
	{
		kwiz_cal_FormatQueryString(q);
		var tmp = new KSCP_RenderCalendar(parent, q);
	}
	else
	{
		document.all["ms-cal-expandedweek" + q].form.submit();
	}
}

function  kwiz_cal_GoToLink(elm)
{
	var href = elm.forhref;
	if(href == null)
		href = elm.href;
	if(href == null)
        return;
  	var win = window.open(href, elm.target);
}

function kwiz_cal_Print(q)
{
	var disp_setting="toolbar=no,location=no,directories=no,menubar=no,"; 
	disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
	var content_vlue = document.getElementById("KWizCom_CalendarView" + q).outerHTML; 

	var docprint=window.open("","",disp_setting); 
	docprint.document.open(); 
	docprint.document.write('<html><head><title>Calendar Printing</title>'); 
	for(var i = 0; i < document.styleSheets.length; i++)
	{
		docprint.document.write('<LINK href="' + document.styleSheets[i].href + '" rel=Stylesheet>');
	}
	docprint.document.write('</head><body onLoad="self.print();self.close();"><center>');          
	var re1 = new RegExp("onmouse", "ig");     
	var re2 = new RegExp("onclick", "ig");     
	var re3 = new RegExp("href", "ig");     
	docprint.document.write(content_vlue.replace(re1, "onmouse1").replace(re2, "onclick1").replace(re3, "href1"));        
	docprint.document.write('</center></body></html>'); 
	docprint.document.close(); 
	docprint.focus(); 
}
function  kwiz_cal_ItemMouseOver(elm)
{
	elm.className = elm.bclassname + "sel";
	if((elm != document.body.calttel) && (document.body.calttel != null))
		document.body.calttel.popup = null;
	document.body.calttel = elm;
	window.setTimeout(kwiz_cal_ShowTooltip, 500);
}
function  kwiz_cal_ItemMouseOut(elm)
{
	
	elm.className = elm.bclassname;
	elm.toElement = window.event.toElement;
	window.setTimeout(kwiz_cal_HideTooltip, 500);
}

function kwiz_cal_ShowTooltip()
{
	var e = document.body.calttel;
	if(e.className != e.bclassname + "sel")
		return;
	if(e.popup != null)
		return;
	kwiz_cal_LocateToottip(e);
}

function kwiz_cal_HideTooltip(toEl)
{
	var e = document.body.calttel;
	if(e == null)
		return;
	if(e.className != e.bclassname)
		return;
	if(e.popup == null) 
		return;
	if(e.popup.document.body.focused == true)
		return;
	e.popup.hide();
	e.popup = null;
}
var gTooltipbody = null;
var gTooltipMaintable = null;
var gTooltiptable = null;
var gTooltiptime = null;
var gTooltipfieldtitle = null;
var gTooltipfielddata = null;
function kwiz_cal_InitTooltipStyles()
{
	try
	{
		if(gTooltipbody == null)
		{
			for (var s = 0; s < document.styleSheets.length; s++) 
			{
				for (var r = 0; r < document.styleSheets[s].rules.length; r++) 
				{
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltipbody') 
						gTooltipbody = document.styleSheets[s].rules[r].style.cssText;
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltipmaintable') 
						gTooltipMaintable = document.styleSheets[s].rules[r].style.cssText;
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltiptable') 
						gTooltiptable = document.styleSheets[s].rules[r].style.cssText;
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltiptime') 
						gTooltiptime = document.styleSheets[s].rules[r].style.cssText;
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltipfieldtitle') 
						gTooltipfieldtitle = document.styleSheets[s].rules[r].style.cssText;
					if (document.styleSheets[s].rules[r].selectorText == '.ms_cal_tooltipfielddata') 
						gTooltipfielddata = document.styleSheets[s].rules[r].style.cssText;
				}
			}
		}
	}
	catch(e)
	{
		gTooltipbody = null;
	}
}
function kwiz_cal_LocateToottip(e)
{
	try
	{
		kwiz_cal_InitTooltipStyles();
		var iX = -10;
		var iY = 18;
		var oPopup = window.createPopup();
		e.popup = oPopup;
		var popupBody = oPopup.document.body;
		var popupDocument = oPopup.document;
		popupDocument.createStyleSheet();
		if(gTooltipbody.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltipbody', gTooltipbody);
		if(gTooltipMaintable.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltipmaintable', gTooltipMaintable);
		if(gTooltiptable.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltiptable', gTooltiptable);
		if(gTooltiptime.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltiptime', gTooltiptime);
		if(gTooltipfieldtitle.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltipfieldtitle', gTooltipfieldtitle);
		if(gTooltipfielddata.length > 0) popupDocument.styleSheets[0].addRule('.ms_cal_tooltipfielddata', gTooltipfielddata);
		popupBody.className = 'ms_cal_tooltipbody';
		popupBody.parentmouseout = kwiz_cal_HideTooltip;
		popupBody.gotolink = kwiz_cal_GoToLink;
		popupBody.onmouseover = function(){this.ownerDocument.body.focused = true;};
		popupBody.onmouseleave = function(){this.ownerDocument.body.focused = false;this.parentmouseout();};
		oPopup.document.body.innerHTML = e.titlep;
		oPopup.show(0, 0, 0, 0, e);
		var nRealWidth	= popupBody.scrollWidth + popupBody.offsetWidth - popupBody.clientWidth;
		var nRealHeight = popupBody.scrollHeight + popupBody.offsetHeight - popupBody.clientHeight;
		oPopup.hide();
		oPopup.show(iX, iY, nRealWidth + 30, nRealHeight, e);
	}
	catch(e)
	{
	}
}

