﻿
//started by the comment button, this opens the login panel if not logged in.

var commentPanel, userHash;
var leftPos = 550;
function viewCommentTools() {

	var tooltipCommentTools = document.getElementById('tooltip-comment-tools');
	if (!tooltipCommentTools) {
		strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
			'<p><strong>' + translate('Comment tool') + '</strong></p>' +
			'<p><a href="#" onclick="startDoComment(); return false;">' + translate('Create new comment') + '</a></p>' +
			'<p><a href="#" onclick="viewAllComments(this); return false;">' + translate('Show all my comments') + '</a></p>' +
			'<p><a href="#" onclick="sendToAFriend(this); return false;">' + translate('Send my comments to a friend') + '</a></p>' +
			'<span class="button"><input type="button" class="logout" value="' + translate('Logout') + '" onclick="logout(); closebox(this);" /></span>' +
			'</div>';
		strhtml += '<a id="commentCloseButton" class="close" href="#" onclick="closebox(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		div = document.createElement('div');
		divId = document.createAttribute('id');
		divId.nodeValue = 'tooltip-comment-tools';
		div.setAttributeNode(divId);
		div.className = 'tooltip-large';
		div.innerHTML = strhtml;
			//commentPanel.insertBefore(div, commentPanel.firstChild);
		document.body.appendChild(div);
		
			var tooltipComments = document.getElementById('tooltip-comments');
				if (tooltipComments) tooltipComments.style.display = 'none';
				
			var toolbar = document.getElementById('toolbar');
				
				div.style.left = leftPos + 'px';
				div.style.top = elmPos.Y(toolbar) + 75 + 'px';
				
		tooltip_visible = true;
	}
	else {
		tooltipCommentTools.style.display = 'block';
	
		var logoutBtn = tooltipCommentTools.getElementsByTagName('span')[0];
		if (logoutBtn.style.display == 'none') logoutBtn.style.display = 'block';
	}
}

function startDoComment() {
	docomment = true; 
	var tooltipCommentTools = document.getElementById('tooltip-comment-tools');
	tooltipCommentTools.style.display = 'none';
	tooltip_visible = false;
}

function handleStartComments(a)
{
	/*if (tooltip_visible)
	{
		return;
	}*/
		
	if ( typeof(hhLogEvent) == "function" )
	    hhLogEvent("Comments");

	a.className = 'active';	
	tooltip = document.getElementById(a.id + '_tooltip');
	
	
	//tooltip.className = 'tooltip-large';
	//tooltip.getElementsByTagName('span')[0].style.display = 'none';
	
	commentPanel = tooltip;
	
	//check logged in.
    //get hash from cookie
    var hash = loadHash();
    if(hash == null)
    {
    
		var tooltipComments = document.getElementById('tooltip-comments');
			
			if (!tooltipComments) {
				//show login controls
				strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
					'<p><strong>' + translate('Login') + '</strong><br />' +
					translate('You must be logged in to use the comment tools')  + '.</p>' +
					'<input type="text" onfocus="usrFocus(this);" onblur="usrBlur(this);" class="text" id="usr" value="(' + translate('Your e-mail address') + ')">' +
					'<input onfocus="passwordFocus(this, \'\');" type="text" class="text" id="fakepass" value="(' + translate('Password') + ')">' +
					'<input onblur="passwordBlur(this, \'\');" type="password" class="text active" style="display: none;" id="pass">' +
					'<input id="btnLogin" onclick="doLogin();" type="submit" class="button" value="' + translate('Login') + '">' +
					'<br /><a href="#" onclick="newUser(this); return false;">' + translate('New user?') + '</a>' +
					'<a href="#" onclick="reminder(this); return false;" style="margin-left: 10px;">' + translate('Forgot password?') + '</a>' +
					'</div>';
				strhtml += '<a id="commentCloseButton" class="close" href="#" onclick="closebox(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
				
				div = document.createElement('div');
				divId = document.createAttribute('id');
					divId.nodeValue = 'tooltip-comments';
				div.setAttributeNode(divId);
				div.className = 'tooltip-large';
				div.innerHTML = strhtml;
				//tooltip.insertBefore(span, commentPanel.firstChild);
				document.body.appendChild(div);
					
					div.style.left = elmPos.X(a) - 45 + 'px';
					div.style.top = elmPos.Y(a) + 20 + 'px';
				}
			else {
				tooltipComments.style.display = 'block' }
    }
    else //logged in.
    {
        //init function
        checkLogin(true);
        
    }    
    tooltip_visible = true;
}

// general function to close the comment series of panels
function closeCommentPanels() {
	document.getElementById('commentCloseButton').click();
}


// this function creates an additional panel to view the reminder email button
var panelReminder = false;
function reminder(a) {
	if (panelReminder)
		return false;
		
	div = document.createElement('div');
	
	strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
		'<p><strong>' + translate('Lost your password?') + '</strong><br />' +
		translate('Type your e-mail address and we will send the password to you.')  + '</p>' +
		'<input type="text" onfocus="usrFocus(this);" onblur="usrBlur(this);" class="text" id="reminderemail" value="(' + translate('Your e-mail address') + ')">' +
		'<input onclick="sendpassword();" type="submit" class="button" value="' + translate('Send') + '">' +
		'</div>';
	strhtml += '<a class="close" href="#" onclick="closetooltip(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		
	div.innerHTML = '<span class="tooltip-body">' + strhtml + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
	div.className = 'tooltip-large';
	div.style.display = 'block';
	div.style.top = elmPos.Y(a) + 16 + 'px';
	div.style.left = elmPos.X(a) -100 + 'px';
	document.body.appendChild(div)
	panelReminder = true;
}

// function to send password to an e-mailaddress
function sendpassword() {
	var script = document.createElement('script');
        script.src = 'http://cybercom.commentserver.ar.halvarsson.se/remoteSave/sendPassReminder.aspx?recoverPass=' + document.getElementById('reminderemail').value;
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
}

function sendPassReminder(succeed) {
	if (succeed) {
		alert(translate('Your password has been sent to your e-mail address.'));
		closeCommentPanels();
	}
	else {
		alert(translate('Please check your e-mail address. The password could not be sent.'));
	}
}

// this function creates an additional panel to view the registration form
var panelNewUser = false;
function newUser(a) {
	if (panelNewUser)
		return;
		
	document.getElementById('btnLogin').disabled = 'disabled';
	
	div = document.createElement('div');
	
	strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
		'<p><strong>' + translate('New user') + '</strong><br />' +
		translate('Enter a valid e-mail address and choose a password')  + '.</p>' +
		'<input type="text" onfocus="usrFocus(this);" onblur="usrBlur(this);" class="text" id="newusr" value="(' + translate('Your e-mail address') + ')">' +
		'<input onfocus="passwordFocus(this, \'new\');" type="text" class="text" id="newfakepass" value="(' + translate('Password') + ')">' +
		'<input onblur="passwordBlur(this, \'new\');" type="password" class="text active" style="display: none;" id="newpass">' +
		'<input onclick="createNewLogin();" type="submit" class="button" value="' + translate('Register') + '">' +
		'<input type="checkbox" class="small" id="chkAccept" /> <label for="chkAccept" class="small">' + translate('I have accepted the <a href="#" onclick="termsOfUse(this); return false;">terms of use</a>.') + '</label>' +
		'</div>';
	strhtml += '<a class="close" href="#" onclick="closetooltip(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		
	div.innerHTML = '<span class="tooltip-body">' + strhtml + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
	div.className = 'tooltip-large';
	div.style.display = 'block';
	div.style.top = elmPos.Y(a) + 16 + 'px';
	div.style.left = elmPos.X(a) - 4 + 'px';
	document.body.appendChild(div);
	panelNewUser = true;
}

// this function creates an additional panel to send to a friend
var panelFriend = false;
function sendToAFriend(a) {
	if (panelFriend)
		return false;
		
	div = document.createElement('div');
	
	strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
		'<p><strong>' + translate('Send my comments to a friend') + '</strong><br />' +
		translate('Type your friends e-mail address and we will him or her the annual report as a link, including your personal comments.')  + '</p>' +
		'<input type="text" onfocus="usrFocus(this);" onblur="usrBlur(this);" class="text" id="friendsemail" value="(' + translate('Your friends e-mail address') + ')">' +
		'<input onclick="doSendToAFriend();" type="submit" class="button" value="' + translate('Send') + '">' +
		'</div>';
	strhtml += '<a class="close" href="#" onclick="closetooltip(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		
	div.innerHTML = '<span class="tooltip-body">' + strhtml + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
	div.className = 'tooltip-large';
	div.style.display = 'block';
	div.style.top = elmPos.Y(a) + 26 + 'px';
	div.style.left = elmPos.X(a) + 'px';
	document.body.appendChild(div)
	panelFriend = true;
}

function doSendToAFriend() {
	var friendsemail = document.getElementById('friendsemail').value;
	var hash = loadHash();
	var script = document.createElement('script');
    
    script.src = 'http://cybercom.commentserver.ar.halvarsson.se/remotesave/sendToFriend.aspx?hash=' + hash + '&lang=' + lang + '&email=' + friendsemail;
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
} 

function sendToFriendResponse(succeed)
{
	if (succeed) {
		alert(translate('A link to the annual report including your comments has been sent to your friend!'));
		closeCommentPanels();
	}
	else {
		alert(translate('Please check your friends e-mail address. The link could not be sent.'));
	}
}

// this function creates an additional panel to view all comments
var panelAllcomments = false;
function viewAllComments(a) {
	if (panelAllcomments)
		return false;
		
	div = document.createElement('div');	
	//commentManager.parseCommentaArray();
	var aComments = commentManager.getComments();	
	strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
		'<p><strong>' + translate('All comments') + '</strong></p><p><div class="scrolltext"><ul>';
	for (var i = 0; i < aComments.length; i++) {
		strhtml += '<li><a href="' + aComments[i].pl + '.html" title="' + aComments[i].Comment + '">' + aComments[i].Comment + '</a></li>';
	}
	strhtml += '</ul></div></p></div><a href="#" class="close" onclick="closetooltip(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		
	div.innerHTML = '<span class="tooltip-body">' + strhtml + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
	div.className = 'tooltip-large scroll';
	div.style.display = 'block';
	div.style.top = elmPos.Y(a) + 26 + 'px';
	div.style.left = elmPos.X(a) + 'px';
	document.body.appendChild(div)
	panelAllcomments = true;
}

// this function creates an additional panel to view the terms of use
var panelTermsOfUse = false;
function termsOfUse(a) {
	if (panelTermsOfUse)
		return false;
		
	div = document.createElement('div');
	
	strhtml = '<div class="framesmall-top"></div><div class="tooltip-body">' +
		'<p><strong>' + translate('Terms of use') + '</strong></p><p>' +
		translate('TeliaSonera does not forward personal information collected through the annual report. </p><p>Information collected from visitors is not used to track individuals who visited/displayed the annual report.')  + '.</p>' +
		'</div>';
	strhtml += '<a href="#" class="close" onclick="closetooltip(this); return false;"><img src="../../images/popup/close.png" alt="[X]" /></a><div class="framesmall-bottom"></div>';
		
	div.innerHTML = '<span class="tooltip-body">' + strhtml + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
	div.className = 'tooltip-large';
	div.style.display = 'block';
	div.style.top = '360px';
	div.style.left = '670px';
	document.body.appendChild(div)
	panelTermsOfUse = true;
}

// this function removes the default text in the user-name textfield
function usrFocus(input) {
	if (input.value == '(' + translate('Your e-mail address') + ')') {
		input.value = '';
	}
	input.className = 'text active';
}

// this function restores the default text in the user-name textfield
function usrBlur(input) {
	if (input.value == '') {
		input.value = '(' + translate('Your e-mail address') + ')';
		input.className = 'text';
	}
}

// this function hides the textfield with the label "password", and shows the real password input field
function passwordFocus(input, prefix) {
	field = document.getElementById(prefix + 'pass');
	field.style.display = 'block';
	input.style.display = 'none';
	field.focus();
}

// this function hides the textfield with the label "password", and shows the real password input field
function passwordBlur(input, prefix) {
	if (input.value == '') {
		field = document.getElementById(prefix + 'fakepass');
		field.style.display = 'block';
		input.style.display = 'none';
	}
}

function logout() {	
	var name = "ARTSLoginHash";
	var expires = ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	document.cookie = name+"=."+expires+"; path=/";
	
	sp = document.getElementById('main').getElementsByTagName('span');
	for (var i = 0; i < sp.length; i++) {
		if (sp[i].className == 'comment')
			sp[i].className = '';
	}
	
	var tooltipCommentTools = document.getElementById('tooltip-comment-tools');
		if (!tooltipCommentTools) return;
		tooltipCommentTools.style.display = 'none';
		
		//window.location.reload();
}


//click event from loginbutton
function doLogin()
{
    var user = document.getElementById("usr");
    var pass = document.getElementById("pass");
    login(user.value, pass.value);
}

//click event from create user button
function createNewLogin()
{
	if (!document.getElementById('chkAccept').checked) {
		alert(translate('Before you register, you must check the box to accept the terms of use.'));
	}
	else {
		var user = document.getElementById("newusr");
		var pass = document.getElementById("newpass");
		createLogin(user.value, pass.value);
	}
}

//check server if valid login
function login(user, pass)
{
	if(user != "" && pass != "" && user != '(' + translate('Your e-mail address') + ')')
    {
		//create user hash
        var hash= hex_md5(user + pass);
        
        //save hash to global variable
        userHash = hash;
        
        //request from server
        var script = document.createElement('script');
        script.src = 'http://cybercom.commentserver.ar.halvarsson.se/remoteSave/checkLogin.aspx?hash=' + hash;
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
        
        var tooltipComments = document.getElementById('tooltip-comments');
			if (!tooltipComments) return;
			tooltipComments.style.display = 'none';
    }
    else {
		alert(translate('Login failed'));
    }
}

//response with success or fail from server
function checkLogin(loginStatus)
{
    if(loginStatus)
    {
        if(loginStatus == true)
        {
            //login success
            if (commentPanel.getElementsByTagName('input').length > 0)
            	commentPanel.removeChild(commentPanel.getElementsByTagName('span')[0]);
				
            viewCommentTools();
			
            //save hash in cookie
			if (userHash)
				saveHash(userHash);
        
            //load user hash
            var hash = loadHash();
            if(hash == null) return;
            
            //load the scripts to get old comments and CommentManager class.
            loadCommentScripts(hash);
        }
        else
        {
			alert(translate('Login failed'));
        }
    }
    else
    {
		alert(translate('Login failed'));
    }
}


/*****************<  Create new user   >*************/
function createLogin(user, pass)
{
    if(user != "" && pass != "" && user != '(' + translate('Your e-mail address') + ')')
    {
        var hash= hex_md5(user + pass);
        var script = document.createElement('script');
        script.src = 'http://cybercom.commentserver.ar.halvarsson.se/remotesave/createlogin.aspx?hash=' + hash + '&email=' + user + '&pass=' + pass;
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    else
    {
		alert(translate('The registration failed. Please try another e-mail address and/or password'));
    }
}

function setLoginStatus(text)
{
    document.getElementById("status").innerHTML = text;
}

//response from server
function handleCreatedLogin(loginCreationStatus)
{
    if(loginCreationStatus)
    {
        if(loginCreationStatus == true)
        {
            alert(translate('Thank you for registering!\n\nYou now have access to the comment tools.'));
            var user = document.getElementById("newusr").value;
			var pass = document.getElementById("newpass").value;
			login(user, pass);
        }
        else
        {
            alert(translate('The registration failed. Please try another e-mail address and/or password'));
        }
    }
    else
    {
        alert(translate('The registration failed. Please try another e-mail address and/or password'));
    }
}

function saveHash(hash)
{
    createCookie("ARTSLoginHash",hash,30);
}

function loadHash()
{
    return readCookie("ARTSLoginHash");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

