﻿var logonLink = document.getElementById('itemSDConnectLogon');
var divLogon = document.getElementById('divLogon');

function getInnerWidth()
{
	var w = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
	}
	
	return w;
}


// hides the SDConnect login area
function HideSDConnectLink() {

    var horizontalOffSet = (getInnerWidth() - 700) / 2;
    var logonLink = document.getElementById('itemSDConnectLogon');
    var divLogon = document.getElementById('divLogon');
    var iFrameLogon = document.getElementById('iFrameLogon');

    iFrameLogon.src = '';
    
    divLogon.style.visibility = 'hidden';
    logonLink.innerText = 'client logon';
}

// shows the SDConnect login area
function ShowSDConnectLink() {
    
    var divLogon = document.getElementById('divLogon');
    if (divLogon.style.visibility == 'visible') { return; }
    
    var iFrameLogon = document.getElementById('iFrameLogon');
    var logonLink = document.getElementById('itemSDConnectLogon');
    var publicLoginPageUrl = 'http://www.sdlconnect2.com/SDParticipant/Logon/p_logonPublic.aspx';
    // var publicLoginPageUrl = 'http://publicqa.sdlconnect2.com/SDParticipant/Logon/p_logonPublic.aspx';
    // var publicLoginPageUrl = 'http://127.0.0.1/SDParticipant/Logon/p_logonPublic.aspx';
    // var publicLoginPageUrl = 'http://qa3web.sdlconnect.sdlcg.com/SDParticipant/Logon/p_logonPublic.aspx';
    
    // get parameter values
    var token = GetUrlParameter('token');
    var activityID = GetUrlParameter('a');
    var participantID = GetUrlParameter('p');
    var respondentID = GetUrlParameter('r');
    var viewID = GetUrlParameter('v');
    var returnUrl = GetUrlParameter('ReturnUrl');
    
    // build public login url
    iFrameSource = publicLoginPageUrl + '?x=1'
    
    if (token != '') {
        iFrameSource = iFrameSource + '&token=' + token;
    }
    if (activityID != '') {
        iFrameSource = iFrameSource + '&a=' + activityID;
    }
    if (participantID != '') {
        iFrameSource = iFrameSource + '&p=' + participantID;
    }
    if (respondentID != '') {
        iFrameSource = iFrameSource + '&r=' + respondentID;
    }
    if (viewID != '') {
        iFrameSource = iFrameSource + '&v=' + viewID;
    }
    if (returnUrl != '') {
        iFrameSource = iFrameSource + '&ReturnUrl=' + returnUrl;
    }
    
    
    iFrameLogon.src = iFrameSource;
    
    var offSetX = logonLink.offsetWidth;
    var finalX = 550 - offSetX;
    var leftPosition = finalX.toString() + "px";
    MoveObject(divLogon, "30px", leftPosition);
    divLogon.style.visibility = 'visible';
    
    // logonLink.innerText = 'close';
}

// gets the value of a given url parameter
function GetUrlParameter(name) {

    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

// start up script
if (GetUrlParameter("autologon") == "1") {
    self.setTimeout("ShowSDConnectLink()", 2000);
}

function MoveObject(object, topPosition, leftPosition) {
    object.style.position = "absolute";
    object.style.top = topPosition;
    object.style.left = leftPosition;
}