var gUserInfo=null;
var gBaseUrl=null;

function userInfo() {
makeSyncRequest("getUserInfo.php", "", function(data) {
setUserInfo(data);
});
}

function checksession() {
	return; // remove until a common connect is implemented
	if (!gUserInfo) { return; } // dont check if user isnt logged in
	makePOSTRequest("checkSession.php", "", function(data, responseCode) {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				var result = http_request.responseText;
				if (result != "1") {
					window.location.reload(true);
				}
			} else {
				// ignore;
			}
		}
	});
}

function UserInfo(type, userid, userstatus, username, displayname, homelat, homelng) {
	this.type = type;
	this.userid = userid;
	this.username = username;
	this.displayname = displayname;
	this.userstatus = userstatus;
	this.homelat = homelat;
	this.homelng = homelng;
	this.next = null;
}

function setUserInfo(data) {
	if (data == "") { return; }
	var lines = data.split("\n");
	gUserInfo = null;
	var currUserInfo = null;
	for (i=0; i<lines.length; ++i) {
		var idx=lines[i].indexOf("=");
		var name = lines[i].substr(0,idx);
		var value = lines[i].substr(idx+1);
		if (name == "type") {
			type = value;
			if (!currUserInfo) {
				gUserInfo = new UserInfo(type, null, null, null, null, null, null);
				currUserInfo = gUserInfo;
			} else {
				currUserInfo.next = new UserInfo(type, null, null, null, null, null, null);
				currUserInfo = currUserInfo.next;
			}
		}

		if (name == "baseurl") { gBaseUrl = value; }
		if (name == "userid") { currUserInfo.userid = value; }
		if (name == "userstatus") { currUserInfo.userstatus = value; }
		if (name == "username") { currUserInfo.username = value; }
		if (name == "displayname") { currUserInfo.displayname = value; }
		if (name == "homelat") { currUserInfo.homelat = value; }
		if (name == "homelng") { currUserInfo.homelng = value; }
	}
}

/**
* Function : var_dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function var_dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += var_dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 
