﻿

// Object to send Ajax requests

var  ajaxdebug="Not Set" ;
var http ;

// Do some checking for browser, return 
// appropriate XMLHttpRequest object

function createRequestObject() 
{

	var oa;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
	{
		oa = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		oa = new XMLHttpRequest();
	}
	return oa;
}





// Load categories from Northwind databases
// (This occurs on page load)

function loadCats() 
{
	if(http == null)
		http = createRequestObject() ;
		
	http.open("get","http://rdore.homeip.net/whercc/mowingjson.aspx?cmd=nextmowing", true, "richard", "Fabian01" );
	http.onreadystatechange = outputMowingCallback; // (See following function)
	http.send(null);
	return false;
}




function outputMowingCallback() 
{
	ajaxdebug = "DEBUG not set" ;
	if(http.readyState == 4)
	{
		// expected response style
		// { "rows" : [ { "firstname" : "Robert ", "lastname" : "Dietzel ", "date" : "6/6/2011 12:00:00 AM" } ], "totalRows" : 1, "error" : "null", "status" : "null" }
		//	var vDebug = "{ \"rows\" : [ { \"firstname\" : \"Robert \", \"lastname\" : \"Dietzel \", \"date\" : \"6/6/2011 12:00:00 AM\" } ], \"totalRows\" : 1, \"error\" : \"null\", \"status\" : \"null\" }\r\n\r\n" ;
		var vDebug1 = http.responseText ;
		var iIndex = vDebug1.lastIndexOf("}") ;
		vDebug1 = vDebug1.substring(0,iIndex+1) ;
		vDebug1 = "(" + vDebug1 + ");" ;
		try
		{
			jsonObject = eval(vDebug1) ;
			document.getElementById('ajaxdebugtxt1').innerHTML = jsonObject.rows[0].firstname + " " +  jsonObject.rows[0].lastname ;
//			document.getElementById('ajaxdebugtxt2').innerHTML = jsonObject.rows[0].date.toLocaleDateString() ;
			var d = new Date(jsonObject.rows[0].date) ;
			document.getElementById('ajaxdebugtxt2').innerHTML = d.toDateString() ;
			
		}
		catch(error)
		{
//			document.write("Exception - name: " + error.name + " - msg: " + error.message + " - source<BR>" + vDebug1 + " - <BR> Index " + iIndex ) ;
		}
		
//		document.write("AND HERE'S THE CALLBACK1.<BR>error " + jsonObject.error + "<BR> Rows =" +jsonObject.totalRows + "<BR> Firstname = " + jsonObject.rows[0].firstname) ;

		// Check that there wasn't an error	
		if (jsonObject.error == null)
		{
			// Loop through records, build <table> rows
			for (var i=0; i < jsonObject.totalRows; i++)
			{
//				document.write("<BR>AND HERE'S THE CALLBACK2..." + jsonObject.rows[i].firstname) ;
			}
			
			
			// Replace the content for the <div> with the following ID
			
		}
		else
			alert("Error occured: " + jsonObject.error);
			
			
		// Output debug information
//		document.getElementById('debugOutput').innerHTML = "outputProductsCallback(): " + http.responseText;
		
	}
	
	
} // loadCatsCallback()



// Helps against IE caching of data and not
// showing updates

function genSeconds()
{
	var now = new Date();
	return now.getSeconds();
}

// loadCats() ;


