//
// Entriq utils.js
//
// Objects and (helper) functions to simplify integration with Entriq services
//
// Most essential objects listed below

// user object
// Usage:	
//	Access to user rights and attributes to personalize web experience.
//	The user object is extracted from a cookie, which is typically created during login
//	See Online Entriq documentation for detailed info
// Example:
// 	if (! user.isAuthenticated())
//		document.write('<a href="login.html">Login</a>');
// 	if (! user.isInRole('demo','','introduction'))
//		document.write('<a href="buy.html">Subscribe now!</a>'); 

// session object
// Usage: 
//	Access to Entriq session information
//	A session contains a token (ticket) that enables identification of the user across web servers
//	The session object is extracted from a cookie, which is typically created during login
//	See Online Entriq documentation for detailed info
// Example:
//	var url = "rtsp://media.cdn.net/acme/video1.asf";
// 	url = url + "?sessionId=" + session.SessionId + "&ticket=" + session.Ticket + "&agentHost=" + session.AgentHost;



// Helper functions

var args = new Object();
var query = location.search.substring(1);
var pairs = query.split("&");
for (var i=0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf("=");
	if (pos == -1) continue;
	args[pairs[i].substring(0,pos).toLowerCase()] = unescape(pairs[i].substring(pos+1));
}

function getArgument(argName)
{
	if (typeof(args) == "undefined") return "";
	var temp = args[argName.toLowerCase()];
	if (typeof(temp) == "undefined") return "";
	return temp;
}

function miniPopup(hostname, query)
{
	var url;
	if (typeof(query) == "undefined") {
		url = hostname;
	} else {
		url = "http://" + hostname + "/miniConsole.html?" + query;
	}
	var popupWin = open(url, 'entriq', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=no,resizable=no,copyhistory=no,width=480,height=292,left=' + ((window.screen.width - 480) / 2) + ',top=' + ((window.screen.height - 292) / 2));
	try {
		popupWin.focus();
	} catch (err) {
		alert('If you have installed a popup-blocker, turn it off, or hold down the Ctrl-key, and try again.');
	}
}

// Cookie functions
// Usage: See O'Reilly JavaScript book.  Topic Cookie management
// Example: 
//   var user = new Cookie("user");
//   if (user.load() && user.Name) alert(user.Name); else { user.Name = "me"; user.store(); }

function Cookie(name, hours, path, domain, secure)
{
	this.$name = name;
	if (hours) this.$expiration = new Date((new Date()).getTime() + hours*3600000);
	else this.$expiration = null;
	if (path) this.$path = path; else this.$path = null;
	if (domain) this.$domain = domain; else this.$domain = null;
	if (secure) this.$secure = true; else this.$secure = false;
}

function Cookie_store()
{
	var cookieval = "";
	for(var prop in this) {
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) continue;
		if (cookieval != "") cookieval += '&';
		cookieval += prop + '=' + escape(this[prop]);
	}
	var cookie = this.$name + '=' + cookieval;
	if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	if (this.$secure) cookie += '; secure';
	document.cookie = cookie;
}

function Cookie_load()
{
	var allcookies = document.cookie;
	if (allcookies == "") return false;
	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false; 
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';', start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);
	var a = cookieval.split('&'); 
	for(var i=0; i < a.length; i++) a[i] = a[i].split('=');
	this.Entitlements = new Object();
	for(var i = 0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
		// The following line is specifically added for USER cookie entitlements 
		// MAN inserts cookie value for each entitlement, leading with '_'
		if (a[i][0].charAt(0) == "_") {
			this.Entitlements[a[i][0].substr(1).toLowerCase()] = this[a[i][0]];
		}
	}
	return true;
}

function Cookie_remove()
{
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	document.cookie = cookie;
}

new Cookie();

Cookie.prototype.store = Cookie_store;
Cookie.prototype.load = Cookie_load;
Cookie.prototype.remove = Cookie_remove;

var session = new Cookie("MAN");
session.load();

var user = new Cookie("MANUser");
user.load();

function _user_isAuthenticated()
{
	session.load();
	return (typeof(session.SessionId) != "undefined");
}

function _user_isAuthorized(aid, cid, iid)
{
	user.load();
	if (typeof(cid) == "undefined") cid = "";
	return ((typeof(aid) != "undefined") && (typeof(iid) != "undefined") && (user.isAuthenticated()) && (typeof(user.Entitlements[aid.toLowerCase() + "_" + cid.toLowerCase() + "_" + iid.toLowerCase()]) != "undefined"));
}

user.isAuthorized = _user_isAuthorized;
user.isAuthenticated = _user_isAuthenticated;
user.isInRole = _user_isAuthorized;