var LinkedInXmlHttp = null;



function LinkedIn_VerifyLogin(functionInCB, functionOutCB)
{
	//------store loggedin callback function------
	g_SocialNet.LinkedIn.LoggedInCallback = functionInCB;
	g_SocialNet.LinkedIn.LoggedOutCallback = functionOutCB;
	//--------------------------------------------
	var token = GetCookie("Lin_oauth_token");
	var tokensecret = GetCookie("Lin_oauth_token_secret");
	if (IsNullOrEmpty(tokensecret) || IsNullOrEmpty(token)){
		LinkedIn_DrawLoginButton();
		LinkedIn_ClearUserInfo();
		return;
	} 
	//----user logged in, show connected img------- 	
	LinkedIn_DrawConnectedButton();
	//---------------------------------------------
	LinkedIn_SetSocialNetworkVars(); 	
	DisplayLoginFields();
	//---------------------------------------------
	if (IsNullOrEmpty(g_SocialNet.LinkedIn.UserID)){
		//--- get info from LinkedIn----------
		LinkedIn_GetUserProfile();
	//--------------------------------------------- 	
	}else{  	
		LinkedIn_DrawUserInfo( g_SocialNet.LinkedIn.UserName, g_SocialNet.LinkedIn.PhotoUrl, g_SocialNet.LinkedIn.ProfileUrl );
		if (functionInCB != null){
			functionInCB(); 
		}
	}
	//---------------------------------------------
}

function LinkedIn_Login()
{
	var sFeatures = "height=400,width=650,resizable=1,menubar=0,toolbar=0,location=1,directories=0,scrollbars=0,status=0"
	var LoginWindow = window.open( ScriptSourceNsf() + '/LinkedInLogin?OpenForm', '',sFeatures);
}

function LinkedIn_Logout()
{
	var tokensecret = GetCookie("Lin_oauth_token_secret");
 	var token = GetCookie("Lin_oauth_token");
	if (IsNullOrEmpty(tokensecret) || IsNullOrEmpty(token)){
		return;
	}  	
	var data = "<Request><oauthtoken>" + token + "</oauthtoken>" + 
		"<oauthtokensecret>" + tokensecret + "</oauthtokensecret>" +
		"<AppEnvironment>" + CurrentEnvironment() + "</AppEnvironment>" +  
		"</Request>" ;
	LinkedIn_PostWebRequest( ScriptSourceNsf() + "/LinkedInLogout?OpenAgent" , data, function() {
		if (LinkedInXmlHttp.readyState==4 && LinkedInXmlHttp.status==200){
			DeleteCookie( "Lin_oauth_token" );
			DeleteCookie( "Lin_oauth_token_secret" );
			DeleteCookie( "Lin_userid" );
			DeleteCookie( "Lin_firstname" );
			DeleteCookie( "Lin_lastname" );
			DeleteCookie( "Lin_username" );
			DeleteCookie( "Lin_photourl" );
			DeleteCookie( "Lin_profileurl" );
			LinkedIn_SetSocialNetworkVars("", "", "", "", ""); 	
			LinkedIn_DrawLoginButton();	
			LinkedIn_ClearUserInfo();
			DisplayLoginFields();
			//------run loggedin callback------------------
			var functionCB = g_SocialNet.LinkedIn.LoggedOutCallback;
			if (functionCB != null){
				functionCB(); 
			}
    	}
    });	
}
	
function ReceiveLinkedInInfo( token, tokensecret )
{
	SetCookie( "Lin_oauth_token", token, 1 );
	SetCookie( "Lin_oauth_token_secret", tokensecret, 1 );
	LinkedIn_DrawConnectedButton()
	LinkedIn_GetUserProfile();
}

function LinkedIn_GetUserProfile(functionCB)
{
	var tokensecret = GetCookie( "Lin_oauth_token_secret");
 	var token = GetCookie( "Lin_oauth_token");
	if (IsNullOrEmpty(tokensecret) || IsNullOrEmpty(token)){
		return;
	} 
	var data = "<Request><oauthtoken>" + token + "</oauthtoken>" + 
		"<oauthtokensecret>" + tokensecret + "</oauthtokensecret>" +
		"<AppEnvironment>" + CurrentEnvironment() + "</AppEnvironment>" +   
		"<userfields></userfields></Request>" ;
	//----------------------------------------------------------
	LinkedIn_PostWebRequest( ScriptSourceNsf() + "/LinkedInGetUserProfile?OpenAgent", data, function() {
		if (LinkedInXmlHttp.readyState==4){
			if (LinkedInXmlHttp.status==200){
				var xmlDoc = LinkedInXmlHttp.responseXML;		
				var root = xmlDoc.documentElement;
				var userid = GetNodeValue( root, "id" );
				var firstname = GetNodeValue( root, "first-name" );
				var lastname = GetNodeValue( root, "last-name" );
				var photourl = GetNodeValue( root, "picture-url" );
				var profileurl = GetNodeValue( root, "public-profile-url" );
				var username = firstname + " " + lastname;
				//---------------------------------------
				SetCookie("Lin_userid", userid, 1);
				SetCookie("Lin_firstname", firstname, 1);
				SetCookie("Lin_lastname", lastname, 1);
				SetCookie("Lin_username", username, 1);
				SetCookie("Lin_photourl", photourl, 1);
				SetCookie("Lin_profileurl", profileurl, 1);
				LinkedIn_SetSocialNetworkVars(userid, firstname, lastname, photourl, profileurl); 	
				DisplayLoginFields();
				//---------------------------------------
				LinkedIn_DrawUserInfo( username, photourl, profileurl );
				//------run loggedin callback------------------
				var functionCB = g_SocialNet.LinkedIn.LoggedInCallback;
				if (functionCB != null){
					functionCB(); 
				}
			}else{
				// failed - do ???
			}		
    	}
    });
}

function LinkedIn_DrawUserInfo( Name, PhotoUrl, ProfileUrl )
{
	var item = document.getElementById("LinUser");
	if (item != null){
		item.innerHTML = "<table><tr><td style='width: 80px;'>" +
			"<a target='_blank' href='"  + ProfileUrl + "' alt='Link to " + Name + "' >" +
			"<img src='" + PhotoUrl + "' style='border: 0px'></a></td>" +
			"<td><div style='font: 9pt Verdana; padding-left: 4px;'>  Welcome LinkedIn User:<br>&nbsp;" + 
			Name +
			"<br><br><button type='button' style='font: 8pt Verdana' onclick='LinkedIn_Logout();'>Sign out of LinkedIn</button>" +
			"</div>" +
			"</td></tr></table>";
		item.style.display = "";
	}
}

function LinkedIn_ClearUserInfo()
{
	var item = document.getElementById("LinUser");
	if (item != null){
		item.innerHTML = "";
		item.style.display = "none";
	}
}

function LinkedIn_DrawLoginButton()
{
	var item = document.getElementById("LinLoginButton");
	if (item != null){
		item.innerHTML = "<img src='" + ScriptSourceNsf() + "/log-in-linkedin-small.png' onclick='LinkedIn_Login()' style='cursor: hand;'>";
	}
}

function LinkedIn_DrawConnectedButton()
{
	var item = document.getElementById("LinLoginButton");
	if (item != null){
		item.innerHTML = "<img src='" + ScriptSourceNsf() + "/linkedin-small.png' alt='You are logged into LinkedIn'>";
	}
}

function LinkedIn_SetSocialNetworkVars()
{
	if (arguments.length == 5){
		g_SocialNet.LinkedIn.UserID = arguments[0];
		g_SocialNet.LinkedIn.FirstName = arguments[1];
		g_SocialNet.LinkedIn.LastName = arguments[2];
		g_SocialNet.LinkedIn.PhotoUrl = arguments[3];
		g_SocialNet.LinkedIn.ProfileUrl = arguments[4];		
		g_SocialNet.LinkedIn.UserName = arguments[1] + " " + arguments[2];
	}else{
		g_SocialNet.LinkedIn.UserID = GetCookie("Lin_userid");
		g_SocialNet.LinkedIn.FirstName = GetCookie("Lin_firstname");
		g_SocialNet.LinkedIn.LastName = GetCookie("Lin_lastname");
		g_SocialNet.LinkedIn.PhotoUrl = GetCookie("Lin_photourl");
		g_SocialNet.LinkedIn.ProfileUrl = GetCookie("Lin_profileurl");		
		g_SocialNet.LinkedIn.UserName = GetCookie("Lin_username");
	}		
}

function LinkedIn_PostWebRequest( url, data, funcptr )
{
	if (window.XMLHttpRequest){		// code for IE7+, Firefox, Chrome, Opera, Safari
		LinkedInXmlHttp = new XMLHttpRequest();
  	}else{										// code for IE6, IE5
 		LinkedInXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  	}
	LinkedInXmlHttp.onreadystatechange = funcptr;
	LinkedInXmlHttp.open( "POST", url, true);
  	LinkedInXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//LinkedInXmlHttp.setRequestHeader("Content-length", data.length);
	//LinkedInXmlHttp.setRequestHeader("Connection", "close");
	LinkedInXmlHttp.send( data );
}


function CurrentEnvironment()
{
	if (location.hostname.toLowerCase().indexOf("maysoft.org") != -1){
		return("Development");
	}
	return("Production");
}

function ScriptSourceNsf()
{
	var item = document.getElementById("LinkedInCode");
	if (item == null){
		return("");
	}
	var url = item.src;
	var index = url.indexOf(".nsf" );
	return( index == -1 ? "" : url.substring(0, index+4) );
}



