/*
 * Event Logging Code
 v 1.0
 Author:  Eric Walker 
 
 Written for the Museum of Science, Boston.

 AJAX code based on Apple's XMLHttpRequestExample sample project.
 
 */

window.onLoad = loadSession();

function loadSession() {
	var url = "http://www.mos.org/weatherwise/event_logger.php?method=getAnonymousSession";
	loadXMLDoc(url);
}

function getSession() {
	return session_token;
}

// note, sessionKey is no longer required
// the browser can track this for you.
function logEvent(eventTypeId, message) {
	//alert('log event');
	var url = "http://wxserver2.mos.org/weatherwise/event_logger.php?method=logEvent";
	//url = "https://wxserver2.mos.org/internal/staging/" + url;
	url = url + "&obs_id=" + escape(eventTypeId);
	url = url + "&session_token=" + escape(session_token);
	url = url + "&notes=" + escape(message);
	var path = location.href.split("/index.html")[0];
	//alert(path);
	loadXMLDoc2(path+"/"+url);
}

// global flag
var isIE = false;

// global request and XML document objects
var req;
var req2;

// session id
var session_token = "-1";
var sessionLoaded = false;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function loadXMLDoc2(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req2 = new XMLHttpRequest();
        req2.onreadystatechange = proc2;
        req2.open("GET", url, true);
        req2.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req2) {
            req2.onreadystatechange = proc2;
            req2.open("GET", url, true);
            req2.send();
        }
    }
}

function proc2() {
	if (req2.readyState == 4) {
    	    // only if req shows "loaded"
        // only if "OK"
        if (req.status == 200) {
         } else {
         	/*
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
            */
         }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {

    if (req.readyState == 4) {
    	    // only if req shows "loaded"
        // only if "OK"
        if (req.status == 200) {
        	sessionTokenHandler();
         } else {
         	/*
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
            */
         }
    }
}

function sessionTokenHandler()  {
	session_token = req.responseText;
	sessionLoaded = true;
}

// waiting function
// javascript does not have a built in wait command, so this 
// will be a substitute

function sleep(millis) {
var start = new Date();
var date = new Date();
while (date - start < millis) {
	date = new Date();
}

}
