
// $Id$

// Required values
var swekey_loginname_path = null;

// Optional values
var swekey_promo_url = "http://www.swekey.com";
var swekey_brands = null;
var swekey_loginname_resolve_url = "";
var swekey_authframe_url = null;
var swekey_show_unplugged = true;
var swekey_image_xoffset = '0px';
var swekey_image_yoffset = '0px';
var swekey_loginname_width_offset = 0;
var swekey_artwork_path = 'http://artwork.swekey.com/';

var swekey_str_unplugged = 'No swekey plugged';
var swekey_str_plugged = 'A swekey is plugged';


var swekey_id = "undefined";
var swekey_status;
var swekey_loginname_input = null;
var swekey_frame_started = false;


function swekey_refresh_login()
{
	if (! swekey_frame_started)
	{
		if (Swekey_ListKeyIds() != '')
		{
    		var frame = document.getElementById("swekey_auth_frame");
    		if (frame != null)
    		{
                swekey_frame_started = true;
                frame.setAttribute('src', swekey_authframe_url);
			}
		}
	}

	var id = Swekey_ListBrandedKeyIds(swekey_brands).substring(0, 32);
	if (id != swekey_id)
	{
		swekey_id = id;
		if (swekey_id.length == 32)
		{		
			swekey_status.setAttribute('src', swekey_artwork_path + 'plugged-8x16.png');
			swekey_status.setAttribute('title', swekey_str_plugged);
			swekey_status.style.display = '';
			swekey_show_unplugged = true;
			if (swekey_loginname_input != null && swekey_loginname_resolve_url != null && swekey_loginname_resolve_url != "")
			{
			    var xhr; 
			    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
			    catch (e) 
			    {
			        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
			        catch (e2) 
			        {
			          try {  xhr = new XMLHttpRequest();     }
			          catch (e3) {  xhr = false;   }
			        }
			     }
			 
				xhr.onreadystatechange  = function()
				{ 
					if(xhr.readyState  == 4)
					{
						if (xhr.status  == 200 && xhr.responseText != null && xhr.responseText != "")
							swekey_loginname_input.value = xhr.responseText; 
					}
				}; 
				
				//alert(swekey_loginname_resolve_url.replace(/\$swekey_id/,swekey_id));
				var url = swekey_loginname_resolve_url.replace(/\$swekey_id/,swekey_id);
				url = url.replace(/\$cookie/,encodeURIComponent(document.cookie));
				xhr.open("GET", url, true); 
				xhr.send(null); 
			}
		}
		else
		{
			if (swekey_show_unplugged)
			{
				swekey_status.setAttribute('src', swekey_artwork_path + 'unplugged-8x16.png');
				swekey_status.setAttribute('title', swekey_str_unplugged);
			}
			else
				swekey_status.style.display = 'none';
		}	
	}
	
	setTimeout("swekey_refresh_login()", 1000);
}

function insert_key_logo()
{
	if (swekey_loginname_input.offsetWidth != 0) // the page is loaded (IE)
	{
		var isIE = (navigator.userAgent.toLowerCase().indexOf('msie') >= 0);
    
    	if (swekey_loginname_width_offset != 0)
			swekey_loginname_input.style.width = (swekey_loginname_input.offsetWidth - swekey_loginname_width_offset) + 'px';

    	if (swekey_loginname_input.parentNode != null) 
    	{
        	if (swekey_loginname_input.nextSibling == null)
        		swekey_loginname_input.parentNode.appendChild(swekey_status);
        	else
        		swekey_loginname_input.parentNode.insertBefore(swekey_status, swekey_loginname_input.nextSibling);
        }
 
    	if (isIE) // do it again after the element is attached
    	{
			swekey_status.style.verticalAlign = 'middle';
			swekey_status.style.position = 'relative';
			swekey_status.style.left = swekey_image_xoffset;
			swekey_status.style.top = swekey_image_yoffset;
    	}
	}
	else
		setTimeout("insert_key_logo()", 10);
}

function swekey_login_integrate()
{
    swekey_loginname_input = null;
    
	for (var i = 0; i < swekey_loginname_path.length && swekey_loginname_input == null; i++)
	{
		if (swekey_loginname_path[i] != "")
		{
	        swekey_loginname_input = document.getElementById(swekey_loginname_path[i]);
	        if (swekey_loginname_input != null && swekey_loginname_input.tagName.toLowerCase() != 'input')
	        	swekey_loginname_input = null;
	        	
	        if (swekey_loginname_input == null)
	        {
	            objects = document.getElementsByName(swekey_loginname_path[i]);
	            if (objects != null && objects.length == 1 && objects[0].tagName.toLowerCase() == 'input')
	                swekey_loginname_input = objects[0];
	        }
	    }
    }
    
    if (swekey_loginname_input != null)
    {
    	swekey_status = document.createElement('img');
    	swekey_status.setAttribute('id', 'swekey_status');
    	swekey_status.setAttribute('onClick', 'window.open("' + swekey_promo_url + '")');
    	
    	swekey_status.setAttribute('style', 'width:8px; height:16px; padding:0px; border-spacing:0px; margin:0px; vspace:0px; hspace:0px; frameborder:no; vertical-align:middle;'
		+ 'position:relative;  left:' + swekey_image_xoffset + '; top:' + swekey_image_yoffset + ';');
		
		swekey_status.setAttribute('src', swekey_artwork_path + 'unplugged-8x16.png');
		swekey_status.setAttribute('title', swekey_str_unplugged);
		if (!swekey_show_unplugged)
			swekey_status.style.display = 'none';

		insert_key_logo();

    	if (swekey_authframe_url != null)
    	{
    		document.write('<iframe id="swekey_auth_frame" style="width:0px; height:0px; border: 0px" src = ""></iframe>');	
   
  			// We have to cleanup the cookie
			var date = new Date();
			date.setTime(date.getTime() - (24*60*60*1000)); // one day before
			document.cookie = 'swekey_authframe_session_id=; expires=' + date.toGMTString() + ';path=/;';
		}
    			
    	document.cookie = "swekey_proposed=''; path=/;";   //  reset the cookie
    	setTimeout("swekey_refresh_login()", 1000);
    }
}



var swekey_str_attach_ask = "A swekey authentication key has been detected.\nDo you want to associate it with your account ?";
var swekey_str_attach_success = "The plugged swekey is now attached to your account";
var swekey_str_attach_failed = "Failed to attach the plugged swekey to your account";

var swekey_attach_url = "";
var swekey_session_id = null;


function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function isTemporaryPage() 
{
	try
	{
		var metaElements = document.getElementsByTagName ('META');
		for (var i = 0; i < metaElements.length; i++)
		{
			var attrs = metaElements[i].attributes;
			for (var j = 0; j < attrs.length; j++)
				if (attrs[j].name == 'http-equiv' && attrs[j].value == 'refresh')
					return true;
		}
	}
	catch (e)
	{
	}
	return false;
}

function swekey_propose_to_attach()
{
    if (swekey_session_id == null || swekey_session_id == "" || get_cookie('swekey_proposed') == swekey_session_id)
        return;       
		
	if (isTemporaryPage()) 
        return;       

	var id = Swekey_ListBrandedKeyIds(swekey_brands).substring(0, 32);
	if (id != "")
	{
    	document.cookie = "swekey_proposed=" + swekey_session_id + "; path=/;";   // call it only once
        if (confirm(swekey_str_attach_ask))
        {
		    var xhr; 
		    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
		    catch (e) 
		    {
		        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		        catch (e2) 
		        {
		          try {  xhr = new XMLHttpRequest();     }
		          catch (e3) {  xhr = false;   }
		        }
		     }
		 
			xhr.onreadystatechange  = function()
			{ 
				if(xhr.readyState  == 4)
				{
					if (xhr.status  == 200 && xhr.responseText == "OK")
						alert(swekey_str_attach_success);
					else
						alert(swekey_str_attach_failed);
				}
			}; 
			
			xhr.open("GET", swekey_attach_url.replace(/\$swekey_id/,id), true); 
			xhr.send(null); 
        }
    }
    else
        setTimeout("swekey_propose_to_attach()", 1000);
}


//////////////////////////////////////////////////////////////
// This part is called when a user is logged with a swekey.
// We check that the swekey is still plugged otherwise we
// logout the user
//////////////////////////////////////////////////////////////


var swekey_max_tries = 3;
var swekey_logout_url = null;
var swekey_to_check = null;

function check_swekey_presence()
{
	if (swekey_logout_url == null || swekey_to_check == null)
		return;
		 
	if (Swekey_ListKeyIds().indexOf(swekey_to_check) < 0)
	{
		if (Swekey_Loaded())
		swekey_max_tries --;
	}
	else
		swekey_max_tries = 2; // The plugin is loaded we give 2 tries

	if (swekey_max_tries < 0)
		window.location = swekey_logout_url;
	else
		setTimeout("check_swekey_presence()", 1000);
}



