
var lexkey_loginname_path = null;

var lexkey_promo_url = "";
var lexkey_brands = null;
var lexkey_loginname_resolve_url = "";
var lexkey_authframe_url = null;
var lexkey_show_unplugged = true;
var lexkey_image_xoffset = '0px';
var lexkey_image_yoffset = '0px';
var lexkey_loginname_width_offset = 0;
var lexkey_artwork_path = '';

var lexkey_str_unplugged = 'No lexkey plugged';
var lexkey_str_plugged = 'A lexkey is plugged';


var lexkey_id = "undefined";
var lexkey_status = [];
var lexkey_loginnames_input = [];
var lexkey_mutltiple_loginnames_input = false;
var lexkey_frame_started = false;
var lexkey_frame_checked = 0;


function lexkey_check_authframe_validity()
{
    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 != lexkey_frame_checked)
			{
				var pos = lexkey_authframe_url.indexOf('?');
				if (pos == -1)
					pos = lexkey_authframe_url.length;
		
				alert
				(
					"lexkey Error: Url '" + lexkey_authframe_url.substr(0, pos) + "' is not accessible." +
					"\n\nYou should check your .htaccess file" +
					"\n\n!!! DO NOT ATTACH A lexkey TO YOUR ACCOUNT UNTIL THIS ERROR IS FIXED !!!" +
					"\n\nStatus: " + xhr.status + 
					"\nReply: " + xhr.responseText.substr(0, 100)
				);				
			} 
		}
	};   	

	var pos = lexkey_authframe_url.indexOf('?');
	if (pos == -1)
		pos = lexkey_authframe_url.length;

	xhr.open("GET", lexkey_authframe_url.substr(0, pos) + "?verify=" + lexkey_frame_checked, true); 
	xhr.send(null); 
}

function lexkey_refresh_login()
{
	if (! lexkey_frame_started)
	{
		if (lexkey_ListKeyIds() != '')
		{
    		var frame = document.getElementById("lexkey_auth_frame");
    		if (frame != null)
    		{
                lexkey_frame_started = true;
                frame.setAttribute('src', lexkey_authframe_url);
			}
		}
	}

	var id = lexkey_ListBrandedKeyIds(lexkey_brands).substring(0, 11);

	if (id != lexkey_id)
	{
		lexkey_id = id;
		

		if (lexkey_id.length >= 11 && lexkey_frame_checked == 0)
		{
			var now = new Date();
	    	lexkey_frame_checked = now.getTime();
	    	lexkey_check_authframe_validity();
		}

		if (lexkey_id.length == 11)
		{
			for (var i = 0; i < lexkey_status.length; i ++)
			{		
				lexkey_status[i].setAttribute('src', lexkey_artwork_path + 'plugged-8x16.png');
				lexkey_status[i].setAttribute('title', lexkey_str_plugged);
				lexkey_status[i].style.display = '';
			}
			
			lexkey_show_unplugged = true;
			if (lexkey_loginnames_input.length != 0 && lexkey_loginname_resolve_url != null && lexkey_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 != "")
							for (var i = 0; i < lexkey_loginnames_input.length; i ++)
								lexkey_loginnames_input[i].value = xhr.responseText; 
					}
				}; 
				
			
				var url = lexkey_loginname_resolve_url.replace(/\$lexkey_id/,lexkey_id);
				url = url.replace(/\$cookie/,encodeURIComponent(document.cookie));
				xhr.open("GET", url, true); 
				xhr.send(null); 
			}
		}
		else
		{
			if (lexkey_show_unplugged)
			{
				for (var i = 0; i < lexkey_status.length; i ++)
				{		
					lexkey_status[i].setAttribute('src', lexkey_artwork_path + 'unplugged-8x16.png');
					lexkey_status[i].setAttribute('title', lexkey_str_unplugged);
				}
			}
			else
			{
				for (var i = 0; i < lexkey_status.length; i ++)
				{		
					lexkey_status[i].style.display = 'none';
				}
			}
		}	
	}
	
	setTimeout("lexkey_refresh_login()", 1000);
}

function insert_key_logo()
{
	if (lexkey_loginnames_input[0].offsetWidth != 0) 
	{
		for (var i = 0; i < lexkey_loginnames_input.length; i ++)
		{
			var isIE = (navigator.userAgent.toLowerCase().indexOf('msie') >= 0);
	    
	    	if (lexkey_loginname_width_offset != 0)
				lexkey_loginnames_input[i].style.width = (lexkey_loginnames_input[i].offsetWidth - lexkey_loginname_width_offset) + 'px';
	
	    	if (lexkey_loginnames_input[i].parentNode != null) 
	    	{
	        	if (lexkey_loginnames_input[i].nextSibling == null)
	        		lexkey_loginnames_input[i].parentNode.appendChild(lexkey_status[i]);
	        	else
	        		lexkey_loginnames_input[i].parentNode.insertBefore(lexkey_status[i], lexkey_loginnames_input[i].nextSibling);
	        }
	 
	    	if (isIE) 
	    	{
				lexkey_status[i].style.verticalAlign = 'middle';
				lexkey_status[i].style.position = 'relative';
				lexkey_status[i].style.left = lexkey_image_xoffset;
				lexkey_status[i].style.top = lexkey_image_yoffset;
				lexkey_status[i].style.width = '16px';
	    	}
		}
	}
	else
		setTimeout("insert_key_logo()", 10);
}

function lexkey_login_onload()
{
    lexkey_loginnames_input = [];
    
	for (var i = 0; i < lexkey_loginname_path.length; i++)
	{
		if (lexkey_loginname_path[i] != "")
		{
            objects = document.getElementsByName(lexkey_loginname_path[i]);
            if (objects != null)
				for (var j = 0; j < objects.length; j++)
           	 		if (objects[j].tagName.toLowerCase() == 'input')
						lexkey_loginnames_input[lexkey_loginnames_input.length] = objects[j];
	    }
    }
    
    if (! lexkey_mutltiple_loginnames_input && lexkey_loginnames_input.length > 0)
 	   lexkey_loginnames_input = [lexkey_loginnames_input[0]];
    
    lexkey_status == [];

    if (lexkey_loginnames_input.length > 0)
    {
		for (var i = 0; i < lexkey_loginnames_input.length; i++)
	    {
	    	lexkey_status[i] = document.createElement('img');
	    	lexkey_status[i].setAttribute('id', 'lexkey_status');
	    	lexkey_status[i].setAttribute('name', 'lexkey_status');
	    
	    	
	    	lexkey_status[i].setAttribute('style', 'width:16px; height:16px; padding:0px; border-spacing:0px; margin:0px; vspace:0px; hspace:0px; frameborder:no; vertical-align:middle;'
			+ 'position:relative;  left:' + lexkey_image_xoffset + '; top:' + lexkey_image_yoffset + ';');
			
			lexkey_status[i].setAttribute('src', lexkey_artwork_path + 'unplugged-8x16.png');
			lexkey_status[i].setAttribute('title', lexkey_str_unplugged);
			if (!lexkey_show_unplugged)
				lexkey_status[i].style.display = 'none';
	
		}

		insert_key_logo();
	   	setTimeout("lexkey_refresh_login()", 1000);
    }
}

function lexkey_login_integrate()
{
	if (lexkey_authframe_url != null)
	{
		document.write('<iframe id="lexkey_auth_frame" style="width:0px; height:0px; border: 0px" src = ""></iframe>');	

	
		var date = new Date();
		date.setTime(date.getTime() - (24*60*60*1000)); 
		document.cookie = 'lexkey_authframe_session_id=; expires=' + date.toGMTString() + ';path=/;';
	}
			
	document.cookie = "lexkey_proposed=''; path=/;";  
	
	lexkey_add_load_event(lexkey_login_onload);
}



var lexkey_str_attach_ask = "A lexkey authentication key has been detected.\nDo you want to associate it with your account ?";
var lexkey_str_attach_success = "The plugged lexkey is now attached to your account";
var lexkey_str_attach_failed = "Failed to attach the plugged lexkey to your account";

var lexkey_attach_url = "";
var lexkey_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.toLowerCase() == 'http-equiv' && attrs[j].value.toLowerCase() == 'refresh')
					return true;
		}
	}
	catch (e)
	{
	}
	return false;
}

function lexkey_propose_to_attach()
{
    if (lexkey_session_id == null || lexkey_session_id == "" || get_cookie('lexkey_proposed') == lexkey_session_id)
        return;       
		
	if (isTemporaryPage()) 
        return;       

	var id = lexkey_ListBrandedKeyIds(lexkey_brands).substring(0, 11);
	if (id != "")
	{
    	document.cookie = "lexkey_proposed=" + lexkey_session_id + "; path=/;";  
        if (confirm(lexkey_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(lexkey_str_attach_success);
					else
						alert(lexkey_str_attach_failed);
				}
			}; 
			
			xhr.open("GET", lexkey_attach_url.replace(/\$lexkey_id/,id), true); 
			xhr.send(null); 
        }
    }
    else
        setTimeout("lexkey_propose_to_attach()", 1000);
}



var lexkey_max_tries = 3;
var lexkey_logout_url = null;
var lexkey_to_check = null;

function check_lexkey_presence()
{
	if (lexkey_logout_url == null || lexkey_to_check == null)
		return;
		 
	if (lexkey_ListKeyIds().indexOf(lexkey_to_check) < 0)
	{
		if (lexkey_Loaded())
		lexkey_max_tries --;
	}
	else
		lexkey_max_tries = 2; 

	if (lexkey_max_tries < 0)
		top.location = lexkey_logout_url;
	else
		setTimeout("check_lexkey_presence()", 1000);
}


function lexkey_add_load_event(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
  	else 
	{
		window.onload = function() 
		{
			if (oldonload) 
			{
				oldonload();
			}
			func();
	    }
	}
}


