var strMonthStart;
var strMonthEnd;
var iRecCount =0;

var strConsultStart = new Array(); 
var strConsultEnd	= new Array(); 
var strConsultTitle = new Array(); 

function Prev()
{
	iMonth = MonthInt(document.getElementById("lblMonth").innerHTML) - 2;
	iYear = parseInt(document.getElementById("lblYear").innerHTML);
	if (iMonth == -1)
	{
		iMonth = 11;
		iYear = iYear - 1;
		
	}	
	
	document.getElementById("lblMonth").innerHTML = MonthString(iMonth + 1);
	document.getElementById("lblYear").innerHTML = iYear;
	setDateRows(iMonth, iYear);	
}

function Next()
{
	iMonth = MonthInt(document.getElementById("lblMonth").innerHTML);
	iYear = parseInt(document.getElementById("lblYear").innerHTML);
	if (iMonth == 12)
	{
		iMonth = 0;
		iYear = iYear + 1;
		
	}
	
	document.getElementById("lblMonth").innerHTML = MonthString(iMonth + 1);
	document.getElementById("lblYear").innerHTML = iYear;
	setDateRows(iMonth, iYear);	
}

function MonthString(iMonth)
{
	switch(iMonth)
	{
		case 1:
			return "January";
			break;
		case 2:
			return "Febuary";
			break;
		case 3:
			return "March";
			break;
		case 4:
			return "April";
			break;
		case 5:
			return "May";
			break;
		case 6:
			return "June";
			break;
		case 7:
			return "July";
			break;
		case 8:
			return "August";
			break;
		case 9:
			return "September";
			break;
		case 10:
			return "October";
			break;
		case 11:
			return "November";
			break;																														
		case 12:
			return "December";
			break;			
	}
}
function MonthInt(strMonth)
{
	switch(strMonth)
	{
		case "January":
			return 1;
			break;
		case "Febuary":
			return 2;
			break;
		case "March":
			return 3;
			break;
		case "April":
			return 4;
			break;
		case "May":
			return 5;
			break;
		case "June":
			return 6;
			break;
		case "July":
			return 7;
			break;
		case "August":
			return 8;
			break;
		case "September":
			return 9;
			break;
		case "October":
			return 10;
			break;
		case "November":
			return 11;
			break;																														
		case "December":
			return 12;
			break;			
	}
}

function InitialisePage()
{
	var d=new Date();	
	document.getElementById("lblMonth").innerHTML = MonthString(d.getMonth() + 1);
	document.getElementById("lblYear").innerHTML = d.getYear();
	setDateRows(d.getMonth(), d.getYear());	
}

function daysInMonth(iMonth, iYear)
{
  return 32 - new Date(iYear, iMonth, 32).getDate();
}

function startDayOfMonth(iMonth, iYear)
{
  return new Date(iYear, iMonth, 1).getDay(); 
}

function setDateRows(iMonth, iYear)
{
	iDisplayMonth = iMonth + 1
	iDaysInMonth = daysInMonth(iMonth, iYear);
	iStartDay = startDayOfMonth(iMonth, iYear);			
	strMonthStart = "01" + "/" + iMonth.toString() + "/" + iYear.toString();
	strMonthEnd = iDaysInMonth + "/" + iMonth.toString() + "/" + iYear.toString();
	
	if (iStartDay == 0)
	{
		iStartDay = 7;
	}
	
	iDayCount = 0;		
	DiaryConsult();	
	
	strInnerHTML = "<div class='DayDivide'></div>";			
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		if (iStartDay <= iPosCount)
		{
			iDayCount++;
			strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
			
		}
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}	
	document.getElementById("dateRow1").innerHTML = strInnerHTML;	
	
		
	strInnerHTML = "<div class='DayDivide'></div>";				
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		iDayCount++;
		strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}
	document.getElementById("dateRow2").innerHTML = strInnerHTML;	

	strInnerHTML = "<div class='DayDivide'></div>";					
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		iDayCount++;
		if (iDayCount <= iDaysInMonth)
		{
			strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
		}
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}
	document.getElementById("dateRow3").innerHTML = strInnerHTML;	
	
	strInnerHTML = "<div class='DayDivide'></div>";				
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		iDayCount++;
		if (iDayCount <= iDaysInMonth)
		{
			strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
		}
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}
	document.getElementById("dateRow4").innerHTML = strInnerHTML;	
			
	strInnerHTML = "<div class='DayDivide'></div>";				
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		iDayCount++;
		if (iDayCount <= iDaysInMonth)
		{
			strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
		}
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}
	document.getElementById("dateRow5").innerHTML = strInnerHTML;		
	
	strInnerHTML = "<div class='DayDivide'></div>";				
	for (iPosCount =1; iPosCount < 8; iPosCount++)
	{
		strInnerHTML += "<div class='DayDate'>";	
		iDayCount++;
		if (iDayCount <= iDaysInMonth)
		{
			strInnerHTML += iDayCount.toString() + "/" + iDisplayMonth.toString() + "/" + iYear.toString();
		}
		strInnerHTML += "</div>";
		strInnerHTML += "<div class='DayDivide'></div>";
	}
	document.getElementById("dateRow6").innerHTML = strInnerHTML;	
	
	strInnerHTML = "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	strInnerHTML += "<div class='DayDivide'></div>";

	var iWeekStart = 8 - iStartDay;
	var iBlock;
	var iLine = 1;
	var iStyle = 1;
	for (iPosCount =0; iPosCount < iRecCount; iPosCount++)
	{		
		if (formatDate(strConsultStart[iPosCount]) <= formatDate(iWeekStart + "/" + iMonth + "/" + iYear))
		{
			iBlock = 0;
			for (iPosCount2 = 1; iPosCount2 <= iWeekStart ; iPosCount2++)
			{
				if (formatDate(strConsultEnd[iPosCount]) >= formatDate(iPosCount2 + "/" + iMonth + "/" + iYear))
				{
					iBlock++;				
				}
			}
			buildRow(iStartDay,iBlock,strConsultTitle[iPosCount],iStyle,1,iLine);				
			if (iStyle == 1)
			{
				iStyle = 2;
			}
			else
			{
				iStyle = 1;
			}
			if (iLine == 12) {break;}			
			iLine++;
		}	
	}
	for (iPosCount = iLine; iPosCount <= 12; iPosCount++)
	{
		displayRow(strInnerHTML,1,iPosCount);
	}
	
	iWeekStart = iWeekStart + 1;
	
	for (iMasterLoop = 2; iMasterLoop <=6; iMasterLoop++)
	{
		
		iLine = 1;
		iStyle = 1;		
		for (iPosCount =0; iPosCount < iRecCount; iPosCount++)
		{
			if (formatDate(strConsultStart[iPosCount]) <= formatDate(iWeekStart + "/" + iMonth + "/" + iYear))
			{
				iBlock = 0;
				for (iPosCount2 = iWeekStart; iPosCount2 <= iWeekStart + 6 ; iPosCount2++)
				{
					if (formatDate(strConsultEnd[iPosCount]) >= formatDate(iWeekStart + "/" + iMonth + "/" + iYear))
					{
						if (iPosCount2 <= iDaysInMonth)
						{
							iBlock++;				
						}
					}
				}
				if (iBlock > 0)
				{
					buildRow(0,iBlock,strConsultTitle[iPosCount],iStyle,iMasterLoop,iLine);				
					if (iStyle == 1)
					{
						iStyle = 2;
					}
					else
					{
						iStyle = 1;
					}	
					if (iLine == 12) {break;}						
					iLine++;
				}
			}	

		}
		for (iPosCount = iLine; iPosCount <= 12; iPosCount++)
		{
			displayRow(strInnerHTML,iMasterLoop,iPosCount);
		}			
		iWeekStart = iWeekStart + 7;
	}

}

function displayRow(strHTML, iRow, iLine)
{
	var strControl = "dataRow" + iRow + "Line" + iLine;
	document.getElementById(strControl).innerHTML = strHTML;
}


function buildRow(iStartBlock, iBlockSize, strTitle, iStyle, iRow, iLine)
{
	strHTML = "";	
	bMouseText = false;
	strMouseText = "";
	for (iPosCount3 = 1; iPosCount3 < iStartBlock; iPosCount3++)
	{					
		strHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
	}	
	
	if (strTitle.length > iBlockSize * 7)
	{
		bMouseText = true;
		strMouseText = strTitle
		strTitle = strTitle.substr(0, iBlockSize * 7) + "...";
	}
	if (bMouseText)
	{
		strHTML += "<div class='DayDivide'></div><div class='DayBodyType" + iStyle + "_" +  iBlockSize + "' title='" + strMouseText + "'>" + strTitle + "</div>";	
	}
	else
	{
		strHTML += "<div class='DayDivide'></div><div class='DayBodyType" + iStyle + "_" +  iBlockSize + "'>" + strTitle + "</div>";	
	}
	

	
	if (iBlockSize != 6 && iStartBlock != 0)
	{
		for (iPosCount3 = iStartBlock + iBlockSize; iPosCount3 <= 7; iPosCount3++)
		{					
			strHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
		}				
	}	
	else
	{
		for (iPosCount3 = iStartBlock + iBlockSize; iPosCount3 < 7; iPosCount3++)
		{					
			strHTML += "<div class='DayDivide'></div><div class='DayBody'></div>";
		}	
	}
	
	strHTML += "<div class='DayDivide'></div>";	
	displayRow(strHTML,iRow,iLine);
	
}


function DiaryConsult()
{	
	iRecCount = 0;
	url = ConsultationURL;
	url +="?x=" + Math.floor(Math.random() * 100000);
	xmlhttp12=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp12=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp12=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp12!=null)
	{
		xmlhttp12.onreadystatechange=state_Change_DiaryConsult
		xmlhttp12.open("GET",url,false)
		xmlhttp12.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}	
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function formatDate(strDateIn)
{
	strDateBuild = "";	
	strDateIn = trim(strDateIn.substr(0,11));
		
	var strDateArray = new Array();
	strDateArray = strDateIn.split("/");
	if (strDateArray[1].length == 1)
	{
		strDateBuild = strDateArray[2] + "0" + strDateArray[1];
	}
	else
	{
		strDateBuild = strDateArray[2] + strDateArray[1];
	}
	if (strDateArray[0].length == 1)
	{
		strDateBuild = strDateBuild  + "0" + strDateArray[0];
	}
	else
	{
		strDateBuild = strDateBuild  + strDateArray[0];
	}
		
	return (strDateBuild);
}

function state_Change_DiaryConsult()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp12.readyState==4)
	{
		// if "OK"
		if (xmlhttp12.status==200)
		{		
			var iCount = 0;
			iRecCount = 0;
			var response = xmlhttp12.responseXML.documentElement;
			var x=response.getElementsByTagName("ENTRY")						
			for(i=0;i<x.length;i++)
			{
				strStartDate = "";
				
				xx = x[i].getElementsByTagName("START_DATE");
				strStartDate = xx[0].firstChild.data;
				if (formatDate(strStartDate) <= formatDate(strMonthStart)) 
				{					
					xx = x[i].getElementsByTagName("END_DATE");
					strEndDate = xx[0].firstChild.data;	
					if (formatDate(strEndDate) >= formatDate(strMonthStart)) 
					{
						xx = x[i].getElementsByTagName("TITLE");
						strConsultStart[iCount] = strStartDate;
						strConsultEnd[iCount]	= strEndDate;
						strConsultTitle[iCount] = xx[0].firstChild.data;						
						iCount++;
						iRecCount++;
					}
				}
			}
		}
		else
		{
			alert("Problem retrieving XML data:" + xmlhttp12.statusText)
		}		
	}
}

