/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/user.js
	
	Simpele user class, met als interessante methode writeToCookie
	
  ------------------------------------------------------------------------------
*/

function User(_id) {

	// methods
	this.logOut = User_logOut;
	this.writeToCookie = User_writeToCookie;
	this.getCookie = User_getCookie;
	
	// standard properties
	var userinfo = this.getCookie("UserInfo");
	if (userinfo) {
		var aCookie = this.getCookie("UserInfo").split("|");
		if (aCookie.length == 5) {
			this.id = aCookie[0];	
			this.name = aCookie[1];
			this.fullName = aCookie[2];
			this.token = aCookie[3];
			this.webroot = aCookie[4];
		}
		else {
			this.id = 1;	
			this.name = "guest";
			this.fullName = "Guest Access";
			this.token = "";
			this.webroot = "";
		}
	}
	else {
		this.id = 1;	
		this.name = "guest";
		this.fullName = "Guest Access";
		this.token = "";
		this.webroot = "";
	}
	
	//alert("Welkom " + this.name);
	
	
	
	
	
}
function User_logOut() {
	var userinfo = "";
	
	//alert("Writing following information to cookie: " + userinfo);
	var cookiePath;
	if (webroot == "")
		cookiePath = "/";
	else
		cookiePath = webroot;
		
	document.cookie = "UserInfo=" + escape(userinfo) + ";path=" + cookiePath;
	
	//window.location.reload();
}

function User_getCookie(whichCookie) {
	
    var dc = document.cookie;
    var prefix = whichCookie + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
	
}


function User_writeToCookie() {
	var cookiePath;
	if (webroot == "")
		cookiePath = "/";
	else
		cookiePath = webroot;
	
	var userinfo = this.id + "|" + this.name + "|" + this.fullName + "|" + this.token + "|" + cookiePath;
	
	//alert("Writing following information to cookie: " + userinfo);
	
	document.cookie = "UserInfo=" + escape(userinfo) + ";path=" + cookiePath;
	//alert(document.cookie);
}

