var xml_request = false;

function makeXMLRequest(url, parameters) {
	xml_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xml_request = new XMLHttpRequest();
		if (xml_request.overrideMimeType) {
			xml_request.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xml_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xml_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!xml_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	xml_request.onreadystatechange = xml_handler;
	xml_request.open('GET', url + parameters);
	xml_request.send(null);
}

function xml_handler() {
	if(xml_request.readyState != 4){
		return false;
	}
	if(xml_request.status != 200){
		completed_user_lookup = true;
		return false;
	}
	//alert("Got text: "+xml_request.responseText);
	var text = xml_request.responseText;
	if(!text) return false;
	var lines = text.split("\n");
	if(!lines) return false;
	for(var i=0; i<lines.length; i++){
		if(!lines[i].match(/^<(field|group) ([^>]+)>(.+)/)) continue;
		var type = RegExp.$1;
		var type_selector = RegExp.$2;
		var value = RegExp.$3;
		if(type == 'field'){
			fields[type_selector] = value;
		}
		else if(typeof(groups) == 'object'){
			groups[type_selector] = value;
		}
	}
	completed_user_lookup = true;
	//alert('completed xml retrieval; email is '+fields['email']);
}

var current_url = ((''+window.document.location).split('?'))[0];
makeXMLRequest('/user_information?requestor='+current_url+'&ignore_cache=1', '');
//alert(xml_request.fields);
//setTimeout( "alert('email: '+fields['email']);", 5000);

function try_ajax() {
	this.attempt++;
	if(this.attempt > this.max_attempt){
		clearInterval(this.tid);
		if(typeof(this.no_user_detected_f) == 'function') this.no_user_detected_f();
	}
	if(completed_user_lookup){
		clearInterval(this.tid);
		if(fields.email){
			if(typeof(this.user_detected_f) == 'function') this.user_detected_f();
			//alert("completed lookup\n\nuser_detected_f (executing):\n"+this.user_detected_f+"\n\nno_user_detected_f:\n"+this.no_user_detected_f);
		}
		else{
			if(typeof(this.no_user_detected_f) == 'function') this.no_user_detected_f();
			//alert("completed lookup\n\nuser_detected_f:\n"+this.user_detected_f+"\n\nno_user_detected_f (executing):\n"+this.no_user_detected_f);
		}
		//setTimeout(this.try_ajax(user_detected_f),500);
	}
}

function wait(){
	var myself = this;
	function callMethod() {
		myself.try_ajax();
	}
	this.tid = setInterval(callMethod, 500);
}

function Ajax(user_detected_f, no_user_detected_f){
	//alert("instantiated\n\nuser_detected_f:\n"+user_detected_f+"\n\nno_user_detected_f:\n"+no_user_detected_f);
	if(logged_out){
		if(typeof(no_user_detected_f) == 'function') no_user_detected_f();
		//alert("completed lookup\n\nuser_detected_f:\n"+user_detected_f+"\n\nno_user_detected_f (executing):\n"+no_user_detected_f);
		return false;
	}

	if( (typeof(fields) == 'object') && fields['id']){
		// if we aren't cached, we already have the user information, 
		//	so immediately do user_detected_f()
		//	instead of starting an ajax call
		// alert('non-cached call to user_detected_f()');
		if(typeof(user_detected_f) == 'function') user_detected_f();
		//alert("completed lookup\n\nuser_detected_f (executing):\n"+user_detected_f+"\n\nno_user_detected_f:\n"+no_user_detected_f);
		return false;
	}

	this.user_detected_f = user_detected_f;
	this.no_user_detected_f = no_user_detected_f;
	this.tid;
	this.attempt = 0;
	this.max_attempt = 20;
	this.try_ajax = try_ajax;
	this.wait = wait;
	this.wait();
}

