// This file was generated by Dashcode from Apple Inc.
// You may edit this file to customize your web application.

var rfcNumber = "0";
var rowData;
var reqObj = new XMLHttpRequest();

var url = "http://www.geisterstunde.org/rfc_browser/create_rfc_xml.php"
//var url = "http://www.geisterstunde.org/rfclist.xml";
initReq(url,prepareRowData,false);


var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    // This dataSource uses fixed data for the content of the list.  Some applications may have such static data for their lists, others will want to replace this with information fetched remotely via XMLHttpRequest.
    _rowData: 
		rowData
	,
	    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return this._rowData.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitle) {
            templateElements.rowTitle.innerText = this._rowData[rowIndex];
        }

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handler = function() {
            var resort = self._rowData[rowIndex];
            detailController.setRepresentedObject(resort);
            var browser = document.getElementById('browser').object;
            // The Browser's goForward method is used to make the browser push down to a new level.  Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById('detailLevel'), resort);
        };
        rowElement.onclick = handler;
    }
};

var detailController = {
    // This object acts as a controller for the detail UI.
    
    setRepresentedObject: function(representedObject) {
        // To start, the represented object of our detail controller is simply a string, the title of the list row that the user chose.  You may want to make the represented object any kind of object when you customize the template.
        this._representedObject = representedObject;
		rfcNumber = representedObject.substring(0,4);
        
        var rfcHeaderText = document.getElementById('rfcHeaderText');
		var detailText = document.getElementById('detailText');
		rfcHeaderText.innerHTML = "RFC " + rfcNumber;
		detailText.innerHTML = "<div><blockquote>Loading...</blockquote></div>";
		
		// Request could still be null if neither ActiveXObject
		//   initialization succeeded
		if (reqObj) {
			var url = "http://www.geisterstunde.org/rfc_browser/get_rfc.php?rfc=" + rfcNumber;
			initReq(url,handleRFCGetResponse,true);
		} else {
			alert("Error in Safari.");
		}
    }
    
};

//
// This function GETs the URL and calls the handler when the fetch is done
//
function initReq(url,handler,async) {
	try {
		// Specify the function that will handle the HTTP response
		reqObj.open("GET", url, async);
		reqObj.onreadystatechange = handler;
		reqObj.send(null);
	} catch (errv) {
		alert("RFC Browser cannot contact the server at the moment.	Error detail: " + errv.message);
	}
}

//
// This function handles the response from the XmlHttpObject of getting the RFC page
//
function handleRFCGetResponse() {
	if (reqObj.readyState == 4) {
        if (reqObj.status == 200) {			
			var detailText = document.getElementById('detailText');
			var rfcHeaderText = document.getElementById('rfcHeaderText');
			detailText.innerHTML = formatRFC(reqObj.responseText);
			rfcHeaderText.innerHTML = "RFC " + rfcNumber;
        } else {
			if (reqObj.status == 404) {
				alert("The requested RFC could not be found.");
			} else {
				alert("HTTP Error: " + reqObj.status);
			}
		}
    }
}

// 
// This function formats the RFC raw text
//
function formatRFC(rfcText) {
	return "<xmp>" + rfcText + "</xmp>";
}

//
// This function prepares the row of RFCs
//
function prepareRowData() {

	rowData = new Array();

	if (reqObj.readyState == 4) {
        if (reqObj.status == 200) {		
			
			// get the XML string
			var rfcxml = reqObj.responseText;
			
			// create a parser object
			try
			{
			  parser = new DOMParser();
			  xmlDoc = parser.parseFromString(rfcxml,"text/xml");
			} catch(e) {
			  alert("Error parsing RFC list: " + e.message);
			  return;
			}
			
			var rfc_array = xmlDoc.getElementsByTagName("rfc");
			
			for (var i=0;i<rfc_array.length;i++)
			{ 
				var element = rfc_array[i].getElementsByTagName("number")[0].childNodes[0].nodeValue;
				element = element + ": " + rfc_array[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;

				rowData[i] = element;
			}
			
			
		} else {
			if (reqObj.status == 404) {
				alert("The RFC list could not be downloaded.");
			} else {
				alert("HTTP Error: " + reqObj.status);
			}
		}
    }
}

//
// Function: load()
// Called by HTML body element's onload event when the web application is ready to start
//
function load()
{
    setupParts();
}

