var resContent = "";
function GetContent(ra,param) // this is for Firm
{
    InitXmlHttp();
    InvokeASHX("/LinkedIn/LinkedInService.ashx?ra="+ra+"&name="+param);
    return resContent;
}
function GetLawyerContent(ra,fname,lname,firm,em)// this is for Lawyer
{
    InitXmlHttp();
    InvokeASHX("/LinkedIn/LinkedInService.ashx?ra="+ra+"&fname="+fname+"&lname="+lname+"&name="+firm+"&em="+em);
    return resContent;
}

function InitXmlHttp() {
    // Attempt to initialize xmlhttp object
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        // Try to use different activex object
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }
    
    // If not initialized, create XMLHttpRequest object
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
      {     
            xmlhttp = new XMLHttpRequest();
      }
      // Define function call for when Request obj state has changed
      xmlhttp.onreadystatechange=XMLHttpRequestCompleted;
}
function InvokeASHX(resource)
{
    InitXmlHttp();
    xmlhttp.onreadystatechange= XMLHttpRequestCompleted;
    xmlhttp.open("GET", "" + resource+"&org"+(new Date()).getTime(), true );
    xmlhttp.send(null);
}
function XMLHttpRequestCompleted()
{
        if (xmlhttp.readyState==4)
    {
        try
        {
            resContent = xmlhttp.responseText;
            linkedinPopup();
        }
        catch (e)
        {
            resContent = "Service Not available, please try again later";
        }
    }
}

//begin linkedin popup
//common function to find the x position of the linked in image that was clicked
function findLinkedinPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
//common function to find the y position of the linked in image that was clicked
function findLinkedinPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}
var linkedinObj = "";
var linkedingDiv = "";
var linkedinName = "";
var linkedinPopupType = "";

//show linkedin firm popup
function showLinkedinFirmPopup(obj,div,firm)
{
    linkedinObj = obj;
    linkedinDiv = div;
    linkedinName = firm.replace("~","'");
    linkedinPopupType = "firm";
    document.getElementById('linkedinTitle').innerHTML = "<div class=\"t-12\">See Who You Know at <b>" + linkedinName + "</b></div>";
     
    document.getElementById('linkedinBody').innerHTML = "<br /><div align=\"center\"><img src=\"/images/ajax-loader.gif\" alt=\"\" /></div><br />";
    showLinkedinPopupControl(linkedinObj,linkedinDiv,"175"); 
    GetContent('firmsearch',escape(linkedinName));
}

//common function for adding the content to lawyer or firm popup
function linkedinPopup(){
    if (linkedinPopupType == "firm"){        
        document.getElementById('linkedinBody').innerHTML = resContent;         
    }
    if (linkedinPopupType == "lawyer"){        
        document.getElementById('linkedinLawyerBody').innerHTML = resContent;         
    }
    resContent = "";
}

//show linkedin lawyer popup
function showLinkedinLawyerPopup(obj,div,fn,ln,firm,em)
{
    linkedinObj = obj;
    linkedinDiv = div;
    linkedinPopupType = "lawyer";
    var popup = document.getElementById(div);
    document.getElementById('linkedinLawyerTitle').innerHTML = "<span class=\"linkedin-name\">How you're connected to " + fn + "</span>";
    
    document.getElementById('linkedinLawyerBody').innerHTML = "<br /><div align=\"center\"><img src=\"/images/ajax-loader.gif\" alt=\"\" /></div><br />";
    showLinkedinPopupControl(obj,div,"125");    
    GetLawyerContent('lawyerprofile',fn,ln,firm,em);
}

//common function to show login popup for lawyers and firms
function showLinkedinLoginPopup(obj,div,name,returnUrl,type)
{
    var nm = name.replace("~","'");
    var popup = document.getElementById(div);
    if (type == "firm"){
        popup.className = "linkedin-firm-info";
        document.getElementById('linkedinTitle').innerHTML = "<div class=\"t-12\">See Who You Know at <b>" + nm + "</b></div>";
    } else if (type == "lawyer"){
        popup.className = "linkedin-firm-info";
        document.getElementById('linkedinTitle').innerHTML = "<div class=\"t-12\">See How You Know <b>" + nm + "</b></div>";
    }
    document.getElementById('linkedinBody').innerHTML = "<div class=\"t-12\"><a href=\"javascript: linkedinSubmit('/LinkedIn/ProcessLinkedInAuth.aspx','" + returnUrl + "')\" class=\"b t-12\" title=\"Allow\">Allow</a> <b>LexisNexis to access your LinkedIn account.</b><br><div class=\"p-t-5\"></div>Not a LinkedIn member? <a href=\"javascript: linkedinSubmit('/LinkedIn/ProcessLinkedInAuth.aspx','" + returnUrl + "')\" class=\"b t-12\" title=\"Sign up\">Sign up</a> today!</div>"; 
    showLinkedinPopupControl(obj,div,"175");    
}

//Allow link for getting linkedin key for first time users 
function linkedinSubmit(url,returnUrl)
{
    //check to see if using form fields
    if(document.getElementById("ru")){
        document.getElementById("ru").value = returnUrl;
        document.search.action = url;
        document.search.method = "get";
	    document.body.style.cursor = 'wait';
	    document.search.submit();
	}else{
	    location.href = url + "?ra=key&ru=" + returnUrl;
	}
}

//common function that shows the popup below the image link
function showLinkedinPopupControl(obj,div,leftInset)
{    
    var popup = document.getElementById(div);
    if(popup.style.display == "none")
        popup.style.display = "block";
    var nLeft = findLinkedinPosX(obj);
    if(nLeft > leftInset)
        popup.style.left = (nLeft - leftInset) + "px"; 
    popup.style.top = (findLinkedinPosY(obj) + 17) + "px";  //Added 15 extra pixal to make the div below the trigger
}

//common function used to hide the linked in popup
function hideLinkedinPopupControl(div)
{
    if(document.getElementById(div).style.display == "block")
        document.getElementById(div).style.display = "none";
}

//function linkedinShowHide is used for lawyer results expand/collapse feature
function linkedinShowHide(showName, hideName)
{
    document.getElementById(showName).style.display = "block";
    document.getElementById(hideName).style.display = "none";
}