function resultInit(){
    //there is a bug in IE that caches form values when using the back button
    if (document.all){
        setFields();
    }
    doShowHide();
}
function setFields(){
    //set form values to default values for a bug in IE browsers
    if (document.search.n)
        document.search.n.value = document.search.n.defaultValue;
    if (document.search.ns)
        document.search.ns.value = document.search.ns.defaultValue;
    if (document.search.dv)
        document.search.dv.value = document.search.dv.defaultValue;
}
function doPage(num){
	document.search.p.value = num;
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//pagination for articles
function doArticlePage(url,num){
	document.search.p.value = num;
	document.search.action = url;
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//sort for articles
function doArticleSort(url,val){
    document.search.ns.value = val;
	document.search.action = url;
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//number of results to display
function doNumResults(url, val) {
    document.search.p.value = "0";
    document.search.rpp.value = val;
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//number of Article results to display
function doArticleNumResults(url,val){
    document.search.rpp.value = val;
	document.search.action = url;
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//sort for lawyer locator from form
function doSort(val){
    document.search.p.value = "0";
	document.search.ns.value = val;
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
function removeSelection(num,dim){
    var str = document.search.n.value;
    var arVal = str.split("+");
    var val = "";
    
    //default Peer Review and Client Review to corresponding sort
    //if((dim.indexOf("Peer Review")!=-1)||(dim.indexOf("Client Review")!=-1)){
    //    document.getElementById("ns").value = "";
    //}
    //end Peer Review and Client Review
    for (i = 0; i < arVal.length; i++){
        if (num != arVal[i]){
            val += arVal[i] + "+";
        }
    }
    if (val != ""){
		val = val.substring(0,val.length-1);
	}else{
	    val = "0";
	}
	//set c to "D" to capture search stats
	document.search.p.value = "";
	document.search.c.value = "D";
    document.search.n.value = val;
    document.search.dv.value = dim;
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
function addSelection(num,dim){
    var val = document.getElementById("n").value;
    //added to prevent duplicate adds in Firefox CR 2398 - PC 8/13/08
    var notdup = "true";
    var arVal = val.split("+");
    //default Peer Review and Client Review to corresponding sort
    if(dim.indexOf("Peer Review")!=-1){
        if(document.getElementById("ft").value == "1" || document.search.ft.defaultValue == "1")        {
            document.getElementById("ns").value = "A_SoPeerRating|0";
        }else if(document.getElementById("ft").value == "2" || document.search.ft.defaultValue == "2"){
            document.getElementById("ns").value = "F_SoPeerRating|0";
        }
    }
    if(dim.indexOf("Client Review")!=-1){
        if(document.getElementById("ft").value == "1" || document.search.ft.defaultValue == "1")        {
            document.getElementById("ns").value = "A_SoClientRating|0";
        }else if(document.getElementById("ft").value == "2" || document.search.ft.defaultValue == "2"){
            document.getElementById("ns").value = "F_SoClientRating|0";
        }
    }
    //end Peer Review and Client Review
    for (i = 0; i < arVal.length; i++){
        if (num == arVal[i]){
            notdup = "false";
            break;
        }
    }
    if (notdup == "true"){
        if ((val == "0")||(val == "")){
            document.search.n.value = num;
        }else{
            document.search.n.value = val + "+" + num;
        }
        document.search.p.value = "";
        //set c to "D" to capture search stats
        document.search.dv.value = dim;
        document.search.c.value = "D";
	    document.search.action = "/Results.aspx";
	    document.body.style.cursor = 'wait';
	    document.search.submit();
	}
}
function hideDim(num){
    var str = document.search.ne.value;
    var arVal = str.split("+");
    var val = "";
    for (i = 0; i < arVal.length; i++){
        if (num != arVal[i]){
            val += arVal[i] + "+";
        }
    }
    if (val != ""){
		val = val.substring(0,val.length-1);
	}
	
    document.search.ne.value = val;
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
function showDim(num){
    var val = document.search.ne.value;
    if ((val == "0")||(val == "")){
        document.search.ne.value = num;
    }else{
        document.search.ne.value = val + "+" + num;
    }
	document.search.c.value = "X";
	document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
// hide/show guided nav from form and browse
function toggleNav(id) {
	var topicStyle  = document.getElementById("div" + id).style;
	if (topicStyle.display == "none") {
		topicStyle.display = "block";
		document.getElementById("class" + id).className = "show";
		removeShowHide(id);
	} else {
		topicStyle.display = "none";
		document.getElementById("class" + id).className = "hide";
		addShowHide(id);
	}
}
function addShowHide(id){
    var obj = "";
    if (document.search.sh){ //from form search
        obj = document.search.sh;
    } else { //from browse search 
        obj = document.search.s;
    }
    
    if ((obj.value == "0")||(obj.value == "")){
        obj.value = id;
    }else{
        obj.value = obj.value + "+" + id;
    }
}
function doBrowseLink(ur){
    var url = ur;
    //default Peer Review and Client Review to corresponding sort
    if(url.indexOf("Peer Review")!=-1){
        if(url.indexOf("add|")!=-1){
            if(url.indexOf("lawyers")!=-1){
                url += "&ns=A_SoPeerRating|0";
            }else if(url.indexOf("firms")!=-1){
                url += "&ns=F_SoPeerRating|0";
            }
        }
    }
    if(url.indexOf("Client Review")!=-1){
        if(url.indexOf("add|")!=-1){
            if(url.indexOf("lawyers")!=-1){
                url += "&ns=A_SoClientRating|0";
            }else if(url.indexOf("firms")!=-1){
                url += "&ns=F_SoClientRating|0";
            }
        }
    }
    //end Peer Review and Client Review
    url = replaceAll(url,"& "," ",true);
    if (document.search.s.value != ""){
        var args = url.split("?");
        var str = document.search.s.value;
        str = replaceAll(str," ","+",true);
        document.search.s.value = str;
        var val = "sh=" + str;
        if (args.length > 1){
            url += "&" + val;
        }else{
            url += "?" + val;
        }
    }
    if (document.search.z.value != "")
        url += "&z=" + escape(document.search.z.value);
    location.href = url;
}
//sort for lawyer locator from browse
function doBrowseSort(ur, val){
    var url = ur;
	if ((val != "")&&(val!="SortKey")){
        var args = url.split("?");
        var parm = "ns=" + val;
        if (args.length > 1){
            url += "&" + parm;
        }else{
            url += "?" + parm;
        }
    }
    if (document.search.s.value != ""){
        var str = document.search.s.value;
        str = replaceAll(str," ","+",true);
        document.search.s.value = str;
        var args = url.split("?");
        if (args.length > 1){
            url += "&sh=" + str;
        }else{
            url += "?sh=" + str;
        }
    }
    if (document.search.z.value != "")
        url += "&z=" + escape(document.search.z.value);
    location.href = url;
}
function removeShowHide(id){
    var obj = "";
    if (document.search.sh){ //from form search
        obj = document.search.sh;
    }else{ //from browse search
        obj = document.search.s;
    }
    var str = obj.value;
    var arVal = str.split("+");
    var val = "";
    for (i = 0; i < arVal.length; i++){
        if (id != arVal[i]){
            val += arVal[i] + "+";
        }
    }
    if (val != ""){
		val = val.substring(0,val.length-1);
	}	
    obj.value = val;
}
function replaceAll( str, searchTerm, replaceWith, ignoreCase )   {
   var regex = "/"+searchTerm+"/g";
   if( ignoreCase ) regex += "i";
   return str.replace( eval(regex), replaceWith );
}
function doShowHide(){

    var str = "";
    if (document.search.sh){ //from form search
        str = document.search.sh.value;
    }else{ //from browse search
        str = document.search.s.value;
        str = replaceAll(str," ","+",true);
        document.search.s.value = str;
    }
    if (str != ""){
        var arVal = str.split("+");        
        for (i = 0; i < arVal.length; i++){
            var id = arVal[i];
            if (document.getElementById("div" + id)){
                document.getElementById("div" + id).style.display = "none";
                document.getElementById("class" + id).className = "hide";
            }
        }
    }
}
function searchSwitch(num){
    //set c to "S" to capture search stats
    document.search.p.value = "";
	document.search.c.value = "S";
    document.search.ft.value = num;
    document.search.z.value = "";
    //set form fields to default values
    document.search.ns.value = "";
    document.search.n.value = "0";
    document.search.ne.value = "";
    document.search.sh.value = "";
    document.search.nr.value = "";
    document.search.dv.value = "";
    document.search.action = "/Results.aspx";
    document.body.style.cursor = 'wait';
	document.search.submit();
}
function alumShow(){
    document.getElementById("ash").value = "show";
    document.search.c.value = "X";
    document.search.action = "/Results.aspx";
	document.body.style.cursor = 'wait';
	document.search.submit();
}
//Invite to connect link code
var resContent = "";
function InitInviteXmlHttp() {
    // 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=XMLInviteHttpRequestCompleted;
}
function InvokeInviteASHX(resource)
{
    InitInviteXmlHttp();
    xmlhttp.onreadystatechange= XMLInviteHttpRequestCompleted;
    xmlhttp.open("GET", "" + resource+"&org"+(new Date()).getTime(), true );
    xmlhttp.send(null);
}
function XMLInviteHttpRequestCompleted()
{
        if (xmlhttp.readyState==4)
    {
        try
        {
            resContent = xmlhttp.responseText;
            validateInviteConnect();
        }
        catch (e)
        {
            resContent = "Service Not available, please try again later";
        }
    }
}
var isln = "";
var eflag = "";
var firstName = "";
var lastName = "";
var invite = "";
var name = "";
function checkInviteConnect(_isln, _eflag, _firstName, _lastName, _invite, _name)
{
    isln = _isln;
    eflag = _eflag;
    firstName = _firstName;
    lastName = _lastName;
    invite = _invite;
    name = _name;
   // conLink = "<a href=\"/network/invite.aspx?isln=" + isln + "&eflag=" + eflag + "&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite + "\" class=\"go-b\" title=\"Invite to Connect\">Invite to Connect</a>";
    InitXmlHttp();
    InvokeInviteASHX("/ResultsService.ashx?isln="+isln);
    return resContent;
}
function validateInviteConnect(){    
    ///0 - If no invites have been sent to the email from that user
    ///7 - Invites have been sent within past 3 days
    ///8 - Invite sent to the email but before 3 days
    ///13 - Already connected
    ///14 - Exceeded maximum number of invites, 3 times for now
    ///15 - Allow invitations from people who know my email
    ///100 - Not logged in user
    
    switch (resContent)
    {
        case "0":
          location.href="/network/invite.aspx?isln=" + isln + "&eflag=" + eflag + "&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite;
          break;
        case "7":
            var header = "<div class=\"b r t-12\">Invite to Connect Error</span>";       
            var wdth = 500;
            var text = "<div class=\"t-12\">You have recently sent an invitation to <span class=\"t-12 b\">" + name + "</span>. You must wait at least three days before you can resend an invitation. Please try again after three days have passed.</div>" ;    
            var links = "<div><a href=\"javascript: closeIt();\" title=\"Ok\"><img src=\"/images/buttons/mhc-ok.gif\" alt=\"Ok\" border=\"0\" /></a></div>";    
            callAlert(header,text,links,wdth); 
          break
        case "8":
          location.href="/network/invite.aspx?isln=" + isln + "&eflag=N&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite;
          break;
        case "13":
            //Already connected, this should not happen in search result page, but still handle that just in case.
            var header = "<div class=\"b r t-12\">Unable to Send Invitations</span>";       
            var wdth = 500;
            var text = "<div class=\"t-12\">You may not send an invitation to <span class=\"t-12 b\">" + name + "</span> as you are already connected to <span class=\"t-12 b\">" + name + "</span>.</div>" ;    
            var links = "<div><a href=\"javascript: closeIt();\" title=\"Ok\"><img src=\"/images/buttons/mhc-ok.gif\" alt=\"Ok\" border=\"0\" /></a></div>";    
            callAlert(header,text,links,wdth); 
            break;
        case "14":
           //Integrated the new rule for maximum 3 times invitation
            var header = "<div class=\"b r t-12\">Unable to Send Invitations</span>";       
            var wdth = 500;
            var text = "<div class=\"t-12\">Looks like you have already invited <span class=\"t-12 b\">" + name + "</span> to connect. You may not send an invitation to <span class=\"t-12 b\">" + name + "</span> more than 3 times.</div>" ;    
            var links = "<div><a href=\"javascript: closeIt();\" title=\"Ok\"><img src=\"/images/buttons/mhc-ok.gif\" alt=\"Ok\" border=\"0\" /></a></div>";    
            callAlert(header,text,links,wdth); 
            break;
        case "15":
            location.href="/network/invite.aspx?isln=" + isln + "&eflag=N&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite;
            break;
        case "100":
            //For not signed in user, redirect to invite page which requires login.
            location.href="/network/invite.aspx?isln=" + isln + "&eflag=" + eflag + "&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite;
            break;
        default:
           //Error or unhandled return codes, send to invite page and handle there
          location.href="/network/invite.aspx?isln=" + isln + "&eflag=" + eflag + "&rfn=" + firstName + "&rln=" + lastName + "&invtype=" + invite;
          break;
    }
}
//added for Alumni export issue - link does not work after dimension filtering caused by address in window changing
function updateAction() {
    var ft = document.search.ft.value;
    var org = document.search.org.value;
    var act = "/Results.aspx?ft=" + ft + "&org=" + org;
    document.exp.action = act;
    return true;
}

/**
*Added by Gene on July 07,2009
*Description: Build shared connections section on search result page for MHC members
**/
var profileOwnerName = "";
function buildConnectionPath(currentUserID, profileOwnerUserID, name)
{
    profileOwnerName = name;
    UserAuthService.GetSharedConnections(currentUserID,profileOwnerUserID,onSuccess, onError);
}

function onSuccess(result)
{
    var strSharedConnections = "";
    for(var n=0; n < result.length; n++)
    {
        strSharedConnections += result[n].FirstName + " " + result[n].LastName + ",";
    }
    
    strSharedConnections = strSharedConnections.substr(0,strSharedConnections.length - 1);
    
    toolTip("<span class='b'>" + profileOwnerName + "</span> is connected to <span class='b'>" + strSharedConnections + ".</span>", 320, false);
}

function onError(result)
{
    //Do nothing for now
    //alert("Failed to get shared connections: " + result.get_message());
}

