// Copyright (C) 2010 Cognos ULC, an IBM Company. All rights reserved.
// Cognos (R) is a trademark of Cognos ULC,(formerly Cognos Incorporated).

var AdminHost = "";
var TM1ServerName = "";
var UserName = "";
var m_txtAdminHost = "";
var ctrl = document.getElementById('lstTM1Servers');
var UserNameField;
var UserNameLabel;
var PasswordField;
var PasswordLabel;
	
function init(errStr)
{
	if ( errStr != null && errStr != '' )
		alert(errStr);

	getLoginCookie();
	var AdminHostField = document.getElementById('txtAdminHost');
	if ( AdminHostField != null ) {
		m_txtAdminHost = AdminHostField.value;
		if ( m_txtAdminHost == "" && AdminHost != "" ) {
			AdminHostField.value = AdminHost;
			document.all['Submit1'].disabled = true;
			document.Form1.submit();
			return;
		}
	}
	
	var ctrl = document.getElementById('lstTM1Servers');
	UserNameField = document.getElementById('txtUserName');
	UserNameLabel = document.getElementById('lblUserName');
	PasswordField = document.getElementById('txtPassword');
	PasswordLabel = document.getElementById('lblPassword');
	var bCamLogin = false;
	
	if ( ctrl != null ) {
		var numopts = ctrl.options.length;
		var selOpt = null;
		for (var ox = 0; ox < numopts; ox++) {
		    if ( ox == 0 )
		        selOpt = ctrl.options[ox];
		        
			if ( ctrl.options[ox].text == TM1ServerName ) {
				ctrl.selectedIndex = ox;
				selOpt = ctrl.options[ox];
				break;
			}
		}
		if ( selOpt != null ) {
		    var camweburl = selOpt.value;
		     var iPos = camweburl.indexOf(':');
			if ( iPos > 0 )
			{
			    bCamLogin = true;
			    UserNameField.style.visibility = 'hidden';
			    UserNameLabel.style.visibility = 'hidden';
			    PasswordField.style.visibility = 'hidden';
			    PasswordLabel.style.visibility = 'hidden';
			}
		}
	}
	
	if ( !bCamLogin && UserNameField != null ) {
		if ( UserNameField.value == "" )
			UserNameField.value = UserName;
	}
		
	if ( AdminHostField != null && AdminHostField.value == "" )
		AdminHostField.focus();
	else if ( !bCamLogin && UserNameField != null && UserNameField.value == "" )
		UserNameField.focus();
	else if ( !bCamLogin && PasswordField != null )
		PasswordField.focus();
		
	setUserLocaleInfo();
	if ( top && top.document )
		top.document.title = document.title;
}

function OnServerChange(ctrl)
{
    if ( ctrl != null ) 
    {
		var selOpt;
		selOpt = ctrl.options[ctrl.selectedIndex];
		
		if ( selOpt != null ) 
		{
		    var camweburl = selOpt.value;
		    var iPos = camweburl.indexOf(':');
			if ( iPos > 0 )
			{
			    UserNameField.style.visibility = 'hidden';
			    UserNameLabel.style.visibility = 'hidden';
			    PasswordField.style.visibility = 'hidden';
			    PasswordLabel.style.visibility = 'hidden';
			}
			else
			{
			    UserNameField.style.visibility = 'visible';
			    UserNameLabel.style.visibility = 'visible';
			    PasswordField.style.visibility = 'visible';
			    PasswordLabel.style.visibility = 'visible';
			}
		}
	}
}

function SetAdminHost()
{
	var AdminHostField = document.getElementById('txtAdminHost');
	if ( AdminHostField != null && m_txtAdminHost != AdminHostField.value ) {
		setLoginCookie();
		document.all['Submit1'].disabled = true;
		document.Form1.submit();
	}
}

function setUserLocaleInfo()
{
	var str = Number(1222.33).toLocaleString();
	var pos1 = str.indexOf('222');
	var pos2 = str.lastIndexOf('33');

	var thousandSep = null;
	if ( pos1 > 1 ) 
	{
		thousandSep = str.substring(1, pos1);
	}

	pos1 = pos1 + 3;
	var decimalSep = null;
	if ( pos1 > 0 && pos2 > pos1 )  
	{
		decimalSep = str.substring(pos1, pos2);	
	}
	
	if ( decimalSep == null || decimalSep.length <= 0 || decimalSep.length > 3 )
		return;
		
	if ( thousandSep == null || thousandSep.length <= 0 || thousandSep.length > 3 )
		return;
	
	if ( decimalSep == thousandSep )
		return;
		
	document.getElementById('DecimalSeparator').value = Replace(decimalSep, String.fromCharCode(160), String.fromCharCode(32));
	document.getElementById('ThousandSeparator').value = Replace(thousandSep, String.fromCharCode(160), String.fromCharCode(32));
}

function do_login()
{
	setLoginCookie();
}
function setCookie(name, value)
{
	var expires = new Date();
	expires.setFullYear(expires.getFullYear()+10);
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString();
}
function getCookie(name)
{
	if ( document.cookie.length <= 0 )
		return("");  

	var search = name + "=";
	offset = document.cookie.indexOf(search);
	if ( offset == -1 )
		return("");  

	offset += search.length;
	end = document.cookie.indexOf(";", offset);
	if ( end == -1 )
		end = document.cookie.length;
	return(unescape(document.cookie.substring(offset, end)));
}

function getLoginCookie()
{
	var str = getCookie("LoginInfo");
	if ( str == null )
		return;
	var sepstr = "###";
	while ( str != "" ) {
		offset = str.indexOf(sepstr);
		if ( offset < 0 )
			break;  
		name = str.substring(0, offset);
		str = str.substring(offset+sepstr.length);
		offset = str.indexOf(sepstr);
		if ( offset < 0 ) {
			value = str;
		}
		else {
			value = str.substring(0, offset);
			str = str.substring(offset+sepstr.length);
		}
		if ( name == "adminhost" )
			AdminHost = value;
		else if ( name == "tm1server" )
			TM1ServerName = value;
		else if ( name == "username" )
			UserName = value;
	}
}

function setLoginCookie()
{
	var str;
	var elem = document.getElementById('txtUserName');
	if ( elem != null )
		str = "username###"+elem.value;
	else
		str = "username###";
		
	var elem = document.getElementById('txtAdminHost');
	if ( elem != null )
		str += "###adminhost###"+elem.value;
	
	elem = document.getElementById('lstTM1Servers');
	if ( elem != null && elem.selectedIndex >= 0 )
		str += "###tm1server###"+elem.options[elem.selectedIndex].text;
	
	setCookie("LoginInfo", str);
}

function LoginSuccess()
{
	if ( window.opener && window.opener.reloadFrames )
	{
		try 
		{
			window.opener.location.reload();
		}
		catch (e)
		{
		}
	}		
	window.close();
}


