var rowcount = 0;
var XmlHttp;
function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined")
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function setExceptions()
{
	// URL to get states for a given client
	var requestURL = GetExceptionUrl;
	CreateXmlHttp();
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleExceptionLookup;

		//Initializes the request object with POST (METHOD of posting),
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestURL,  true);
			
		// send header for post
		XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		
		//Sends the request to server
		var querystring = "locationID="+ locationID;
		XmlHttp.send(querystring);
	}
}

function HandleExceptionLookup()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{
			if(XmlHttp.responseXML != null){
				DisplayExceptionResults(XmlHttp.responseXML.documentElement);
			}else{
				alert("There was a problem retrieving data from the server (responseXML == null)");
			}
		}
		else
		{
			alert("There was a problem retrieving data from the server (status = " + XmlHttp.status + ")");
		}
	}
}

function DisplayExceptionResults(xmlString)
{
	// set days of the week default peak/off-peak status
	var exceptionNodes = xmlString.getElementsByTagName("exception");
	var curPageCount = exceptionNodes.length;
	if (curPageCount > 0)
	{
		for(var x=0; x<curPageCount; x++)
		{
			var peak = exceptionNodes[x].getAttribute("peak");
			var startDate = exceptionNodes[x].getAttribute("startDate");
			var endDate = exceptionNodes[x].getAttribute("endDate");
			
			var exceptionStartDate = startDate.split("-");
			var startMonth = exceptionStartDate[0];
			var startDay = exceptionStartDate[1];
			var startYear = exceptionStartDate[2];
			
			var exceptionEndDate = endDate.split("-");
			var endMonth = exceptionEndDate[0];
			var endDay = exceptionEndDate[1];
			var endYear = exceptionEndDate[2];
			
			if (endYear == 1753)
			{
				if (startMonth == curDispMonth && startYear == curDispYear)
				{
					if (peak == 0)
					{
						document.getElementById("_"+startDay).className = "offpeak";
					}
					else if (peak == 1)
					{
						document.getElementById("_"+startDay).className = "onpeak";
					}
				}
			}
			else
			{
				sDate = new Date(startYear,startMonth-1,startDay);
				dateCursor = sDate;
				eDate = new Date(endYear,endMonth-1,endDay);
				
				// loop through each day in date range and mark any days in current month appropriately
				while(dateCursor <= eDate && dateCursor.getDate() <= daysInMonth(dateCursor.getMonth(), dateCursor.getYear()))
				{
					var curDate = new Date(curDispYear, curDispMonth-1, dateCursor.getDate());
					if (curDate >= sDate && curDate <= eDate)
					{
						if (peak == 0)
						{
							document.getElementById("_"+dateCursor.getDate()).className = "offpeak";
						}
						else if (peak == 1)
						{
							document.getElementById("_"+dateCursor.getDate()).className = "onpeak";
						}
					}
					dateCursor.setDate(dateCursor.getDate()+1)
					//alert(dateCursor);
				}
			}
		}
	}
}