var CurrentNotices = "";
var OtherNotices = "";
var today = new Date();
var vFrom = new Date();
var vTo = new Date();
var vYear = today.getFullYear();
var vYearFrom = vYear;
var vYearTo = vYear;

function load()
{
    var jsonObj;
	var url = "notices.json";
	jsonObj = null;
	if(window.XMLHttpRequest)
	{
	   	jsonObj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
	    jsonObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(jsonObj != null)
	{
	    jsonObj.onreadystatechange = state_Change;
		jsonObj.open("GET", url, true);
        jsonObj.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		jsonObj.send(null);
	}
	else
	{
	    alert("Your browser does not support XMLHTTP.");
	}
	function state_Change()
	{
		var currNotArray = new Array();
        var otherNotArray = new Array();
		if(jsonObj.readyState == 4)
		{
            if(jsonObj.status == 200)
			{
                var jsonData = eval("(" + jsonObj.responseText  + ")");
				var j = 0;
                var k = 0;
				for (var i = 0; i < jsonData.notices.length; i++)
				{
                    var current = false;
				    if(jsonData.notices[i].enable == "Yes")
					{
                        var vFromDate = (jsonData.notices[i].validfrom.split("/")[0])
                        var vFromMonth = (jsonData.notices[i].validfrom.split("/")[1] - 1)
                        var vToDate = (jsonData.notices[i].validto.split("/")[0])
                        var vToMonth = (jsonData.notices[i].validto.split("/")[1] - 1)
                        if(jsonData.notices[i].validfrom != "" && jsonData.notices[i].validto != "")
                        {
                            if(vFromMonth > vToMonth)
                            {
                                if((today.getMonth() < vFromMonth && today.getMonth() < vToMonth) || (today.getMonth() > vFromMonth && today.getMonth() > vToMonth))
                                {
                                    currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
    	                            j++;
                                    current = true;
                                }
                                else if((today.getMonth() == vFromMonth && today.getMonth() >= vToMonth) || (today.getMonth() <= vFromMonth && today.getMonth() == vToMonth))
                                {
                                    if((today.getMonth() == vFromMonth && today.getDate() >= vFromDate) || (today.getMonth() == vToMonth && today.getDate() <= vToDate))
                                    {
                                        currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
    	                                j++;
                                        current = true;
                                    }
                                }
                                else
                                {
                                    current = false;
                                }
                            }
                            else if(vFromMonth < vToMonth)
                            {
                                if(today.getMonth() > vFromMonth && today.getMonth() < vToMonth)
                                {
                                    currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
    	                            j++;
                                    current = true;
                                }
                                else if((today.getMonth() == vFromMonth && today.getMonth() <= vToMonth) || (today.getMonth() >= vFromMonth && today.getMonth() == vToMonth))
                                {
                                    if((today.getMonth() == vFromMonth && today.getDate() >= vFromDate) || (today.getMonth() == vToMonth && today.getDate() <= vToDate))
                                    {
                                        currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
        	                            j++;
                                        current = true;
                                    }
                                }
                                else
                                {
                                    current = false;
                                }
                            }
                            else
                            {
                                if(vFromDate == vToDate)
                                {
                                    currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
	                                j++;
                                    current = true;
                                }
                                else if(vFromDate > vToDate)
                                {
                                    if((today.getDate() <= vFromDate && today.getDate() <= vToDate) || (today.getDate() >= vFromDate && today.getDate() >= vToDate))
                                    {
                                        currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
	                                    j++;
                                        current = true;
                                    }
                                    else
                                    {
                                        current = false;
                                    }
                                }
                                else
                                {
                                    if(today.getDate() >= vFromDate && today.getDate() <= vToDate)
                                    {
                                        currNotArray[j] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
	                                    j++;
                                        current = true;
                                    }
                                    else
                                    {
                                        current = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            current = false;
                        }
                        if(current == false)
                        {
                            otherNotArray[k] = jsonData.notices[i].order + "#" + jsonData.notices[i].title + "#" + jsonData.notices[i].desc + "#" + jsonData.notices[i].updated;
                            k++;
                        }
					}
                }
                if((sendPage.search(/index.html/) > 0 || sendPage.search(/notices.html/) > 0) || sendURL == "Home")
                {
				    if(currNotArray.length > 0)
				    {
                        currNotArray.sort();
                        CurrentNotices += '<div id="Heading1">Current Notices</div>';
				        for (var i = 0; i < currNotArray.length; i++)
				        {
					        CurrentNotices += '<h1>' + currNotArray[i].split("#")[1] + '</h1>';
					        CurrentNotices += '<p>' + currNotArray[i].split("#")[2] + '</p>';
					        CurrentNotices += '<p id="Updated">Updated: ' + currNotArray[i].split("#")[3] + '</p>';
                            document.getElementById('CurrentNotices').innerHTML = CurrentNotices;
				        }
				    }
                    else
                    {
                        if(sendPage.search(/notices.html/) > 0)
                        {
                            CurrentNotices += '<div id="Heading1">Current Notices</div>';
                            CurrentNotices += '<h1>There are no ' + "'" + 'Current Notices' + "'" + ' to display.</h1>';
                            document.getElementById('CurrentNotices').innerHTML = CurrentNotices;
                        }
                    }
                }
                if(sendPage.search(/notices.html/) > 0)
                {
				    if(otherNotArray.length > 0)
				    {
                        otherNotArray.sort();
                        OtherNotices += '<div id="Heading1">Other Notices</div>';
				        for (var i = 0; i < otherNotArray.length; i++)
				        {
					        OtherNotices += '<h1>' + otherNotArray[i].split("#")[1] + '</h1>';
					        OtherNotices += '<p>' + otherNotArray[i].split("#")[2] + '</p>';
					        OtherNotices += '<p id="Updated">Updated: ' + otherNotArray[i].split("#")[3] + '</p>';
				        }
				    }
                    else
                    {
                        OtherNotices += '<div id="Heading1">Other Notices</div>';
                        OtherNotices += '<h1>There are no ' + "'" + 'Other Notices' + "'" + ' to display.</h1>';
                    }
                    document.getElementById('OtherNotices').innerHTML = OtherNotices;
                }
			}
			else
			{
				alert("Problem retrieving JSON data: " + jsonObj.statusText);
			}
		}
	}
}


