//user data
var cid = "";
var isRegistered = false;
var sentFeedback = false;
var reportUrl = "report.aspx?";
var mediaUrl = null;
var mediaType = null;
var usePlayerPopup = false;

//webcast data
var isLive = false;
var rpcScript = null;
var rpcUrl = "./js/data.js";
var rpcTimer = null;
var rpcTimeStamp = "";
var data = {};
//data.state = "registration";
data.state = "presentation";

checkRegistration();

function checkRegistration()
{
	getCookieData();
	if(data != null)
	{
		dataChanged(data);
	}
}

window.onload = function()
{
	if(isLive)
	{
		updateLiveData();
	}
	//registration
	if(window.location.href.indexOf("register.htm") > -1)
	{
		EventManager.Add('register-btn', 'click', register_click);
	}
	
	//feedback
	if(window.location.href.indexOf("index.htm") > -1 || window.location.href.indexOf("feedback.htm") > -1)
	{
		EventManager.Add('feedback-btn', 'click', feedback_click);
		//clear feedback
		var o = findObj('comments');
		if(o != null)
		{
			o.value = "";
		}
	}
	
	//select player
	if(window.location.href.indexOf("select-player.htm") > -1)
	{
		EventManager.Add('selectplayer-btn', 'click', selectplayer_click);
	}
	
	if(window.location.href.indexOf("index.htm") > -1)
	{
		init();
	}
}

function dataChanged(o)
{
	if(data == null)
	{
		return;
	}
	
	data = o;
	if(isRegistered)
	{
		if(data.state == "registration")
		{
			if(window.location.href.indexOf("register2.htm") < 0 )
			{
				window.location = "register2.htm";
			}
		}
		else if(data.state == "presentation")
		{
			if(window.location.href.indexOf("index.htm") < 0)
			{
				if(window.location.href.indexOf("select-player.htm") < 0)
				{
					window.location = "select-player.htm";
				}
				
				/*if(mediaUrl != null)
				{
					if(cookies["cookiesenabled"] == "true")
					{
						window.location = data.page;
					}
					else
					{
						window.location = data.page + "?ir=1&cid="+cid;
					}
				}
				else
				{
					
				}
				*/
			}
		}
	}
	else
	{
		if(window.location.href.indexOf("register.htm") < 0)
		{
			window.location = "register.htm";
		}	
	}
}

function getCookieData()
{
	//get data
	if(cookies["cookiesenabled"] == "true")
	{
		if(cookies["isRegistered"])
		{
			isRegistered = true;
		}
		if(cookies["sentFeedback"])
		{
			sentFeedback = true;
		}
		if(cookies["cid"])
		{
			cid = cookies["cid"];
		}
		else
		{
			var expiration = new Date(); 
			expiration.setYear(expiration.getYear() + 1);
			cid = new Date().getTime();
			document.cookie = "cid=" + cid + "; expires=" + expiration.toGMTString();
		}
	}
	else
	{
		//no cookies
		var pq = new PageQuery();
		if(pq.getValue('ir') != null)
		{
			isRegistered = true;
		}
		if(pq.getValue('cid') != null)
		{
			cid = pq.getValue('cid')
		}
	}
}

function register_click()
{
	var o;
	var arr = new Array();
	
	o = findObj('name');
	if(o != null)
	{
		arr["v0"] = escape(o.value);
	}
	o = findObj('position');
	if(o != null)
	{
		arr["v1"] = escape(o.options[o.selectedIndex].innerHTML);
	}
	o = findObj('company');
	if(o != null)
	{
		arr["v2"] = escape(o.value);
	}
	o = findObj('country');
	if(o != null)
	{
		if(o.options != null)
		{
			arr["v3"] = escape(o.options[o.selectedIndex].innerHTML);
		}
		else
		{
			arr[""] = escape(o.value);
		}
	}
	o = findObj('email');
	if(o != null)
	{
		arr["v4"] = escape(o.value);
	}
	
	var img = new Image();
	img.src = reportUrl + "t=r&" + "cid=" + cid + "&" + getQueryString(arr);
	setCookie("isRegistered", "true");
	//document.cookie = "isRegistered=true; expires=Sun, 01-Mar-2006 08:00:00 GMT";
	isRegistered = true;
}

function selectplayer_click()
{
	if(usePlayerPopup)
	{
		document.forms[0].action = "";
		document.forms[0].method = "";
		
		var inputs = document.getElementsByName("sid");
		if(inputs != null && inputs.length > 0)
		{
			var selectedStream = "";
			var qs = "";
			
			for(var i=0; i<inputs.length; i++)
			{
				if(inputs[i].name == "sid" && inputs[i].checked)
				{
					selectedStream = inputs[i].value;
					break;
				}
			}
			
			var autostart = (document.getElementById("autostart") && document.getElementById("autostart").checked) ? false : true;
			
			if(selectedStream != "")
			{
				qs = "?sid="+selectedStream;
			}
			if(!autostart)
			{
				qs += "&autostart=false";
			}
			if(qs != "")
			{
				window.open("index.htm"+qs, "player");
			}
			else
			{
				window.open("index.htm", "player");
			}
		}
	}
}

function feedback_click()
{
	//hide tab
	if(tabPanel1 != null)
	{
		tabPanel1.hideTab(3);
	}
	sentFeedback = true;
	
	return;
	
	var o;
	var arr = new Array();
	
	for(var i=1; i<5; i++)
	{
		o = findObj('q1'+i);
		if(o != null && o.checked)
		{
			break;
		}
	}
	arr["v0"] = escape(o.value);
	
	for(var i=1; i<5; i++)
	{
		o = findObj('q2'+i);
		if(o != null && o.checked)
		{
			break;
		}
	}
	arr["v1"] = escape(o.value);
	
	for(var i=1; i<3; i++)
	{
		o = findObj('q3'+i);
		if(o != null && o.checked)
		{
			break;
		}
	}
	arr["v2"] = escape(o.value);
	
	o = findObj('comments');
	if(o != null)
	{
		arr["v3"] = escape(o.value);
	}

	var img = new Image();
	img.src = reportUrl + "t=f&" + "cid=" + cid + "&" + getQueryString(arr);
	sentFeedback = true;
	o.value = "Thank you!";
}

//RPC -- move to axlib
function sendRPC(url) 
{
	if (rpcTimer) 
	{
		setTimeout("sendRPC('" + url + "')", 100);  // Try again in 71ms
		return;
	}
	if(rpcScript == null || !isIE)
	{
		rpcScript = document.createElement('script');
		rpcScript.type = "text/javascript";
		document.body.appendChild(rpcScript);	
	}
	rpcScript.src = url;
}

function rpcLoaded(o)
{
	rpcTimer = null;
	if(o != null)
	{
		if(o.state != data.state || o.timeStamp != data.timeStamp)
		{
			dataChanged(o);
		}
	}
}

function updateLiveData() 
{
	var params = new Date().getTime() +  Math.random()*1000;
	if(rpcTimer == null)
	{
		sendRPC(rpcUrl+"?x="+params);
	}
	setTimeout(updateLiveData, 5000);
}

// LIB
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, days, path, domain, secure) {
    var expires;
    if(days != null)
    {
		expires = new Date();
		expires.setTime(expires.getTime()+(days*24*60*60*1000));
    }
	document.cookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") + 
	((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + 
	((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    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));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
