/* iChat forums javascript */
function ichat_redirect(Server, Topic)
{
        Topic=Topic.replace(/&rsquot;/gi,"'");
        if ( document.cookie.search( /ichat_cookie/i ) != -1 )
        {
            if (Topic == "")
            {
               window.location = Server + "/mb/user/topics.html?NoFrames=1";
            }
            else
            {
            window.location = Server + "/mb/user/msglist.html?NoFrames=1&" + Topic ;
            }
        }
        else
        {
           window.location = Server + "/mb/+login_user?username=guest&password=guest&" 
                             + Topic + "&uri=/user/msglist.html?NoFrames=1";
        }
}

/* Auto-login user as a guest if user did not already log in as a member */
  var url_location = "" + window.location;

  if (url_location.indexOf("//") >= 0)
    url_location = url_location.substring(url_location.indexOf("//")+2,url_location.length);

  var server = url_location.substring(0,url_location.indexOf("/"));
  var uri = url_location.substring(url_location.indexOf("/"),url_location.length);

  if (uri.indexOf("/mb") >= 0)
  {
    uri = uri.substring(uri.indexOf("/mb")+3,uri.length);
    if (document.cookie.search(/ichat_cookie/i)==-1)
    {
      var new_location = "http://" + server +
                         "/mb/+login_user?username=guest&password=guest&uri=" + uri;
      window.location = new_location;
    }
  }

function replacePlus( str ) {
      while ( str.indexOf( "+" ) > 0 ) {
    str = str.substring( 0, str.indexOf( "+" ) ) + "%2b" + str.substr( str.indexOf( "+" ) + 1 );
      }
      return str;
}

/* 
Pulls the DCOL_USER cookie, if they have logged in through My Discovery.
Returns their First Name. value[1] is their screen name.  Abigail Sherr van Roode 20010430 
*/
function getCookie(name) {
    var nameStr = name + "=";
    var maxLen = document.cookie.length
    var i = 0
            while (i < maxLen) {
                    var j = i + nameStr.length
                    if (document.cookie.substring(i,j) == nameStr) {
                    var cookieEnd = document.cookie.indexOf(";",j);
                    if (cookieEnd == -1) {
                    cookieEnd = document.cookie.length;
                    }
                    return unescape(document.cookie.substring(j,cookieEnd));
                    }
            i++
            }
            return "";
}

function userName() {
    var name = "";
    var cookie = getCookie('dcol_user');
    values = cookie.split ("|");
    name = values[2];
    return(name);
}


function loggedin(net,dest) {
//alert("net="+net+"\ndest="+dest)
    if (userName()) {
	strURL = '<a href="http://my.'+net+'.discovery.com/MyDiscovery/logout.html?';
	if (net == "discovery") strURL = '<a href="http://my.discovery.com/MyDiscovery/logout.html?';
    strMsg = '<span class="text"><b>You are currently logged in as ';
    strMsg += '<span class="primeColor">' + userName() + '</span>. Go to ';
	strMsg += strURL + 'dest=login.html%3fdest=' + dest + '%26loginmsg=ichat">';
	strMsg += 'My Discovery</a> to login as another user.</b></span>';
    document.write(strMsg);
    }
}

function randomGenerator()
{
    var rndNumber = Math.random();
    var secondsObj = new Date();
    var seconds = secondsObj.getSeconds();
    var number = rndNumber * 100000 * seconds;
    var roundedNumber = Math.round(number);

    return roundedNumber;
   
}


function confirmRemove(path,url,groupName)
{
    var gotoThis = path+url+"action=RemoveGroup&ProcessGroup=" + groupName;
    var NoGotoThis = path+url;
    if ( confirm("Removing this Topic will delete all messages related to this topic. Are you sure you want to remove it?") )
    {
        location.href=gotoThis;
    }

    else
    {
        location.href=NoGotoThis;
    }
}


function confirmDelete(path,url,msgId)
{
    var gotoThis = path+url+"action=DeleteMessage&ProcessMessage=" + msgId +
                  "&ParentMsg=$Topic.ParentMsg&StartMessage=$Args.StartMessage&Direction=$Args.Direction&MessageIndex=$Args.MessageIndex";
        var NoGotoThis = path+url+
                  "ParentMsg=$Topic.ParentMsg&StartMessage=$Args.StartMessage&Direction=$Args.Direction&MessageIndex=$Args.MessageIndex";

    if ( confirm("Are you sure you want to delete this message?") )
    {
      location.href=gotoThis;
    }
    else
    {
      location.href=NoGotoThis;
        }
}

function verifySearch(button, isPublicSearchable)
{
    var submit = true;

    //  I commented this out because  the searchField has been commented
    //  out below.  cws 21 Apr 98
    // if the advanced settings do not apply to the selected search field
    //if (button.form.Searchfield.selectedIndex != 0)
    //{
        // submit the form
    //    button.form.submit();
    //    return;
    //}

    // if the search scope is "current topic"
    if (button.form.Scope.selectedIndex == 0)
    {
        // if the current topic is not public searchable, the search field "subject and body" does not apply
        if (!isPublicSearchable)
        {
            alert('The current topic is not searchable.  Only public topics may be searched.');

            return;
        }
    }

    // check the advanced settings
    /* Commented out because advanced settings aren't used - Abigail Sherr 20000830
    var maxDoc = document.forms[0].MaxDoc.value
    var threshold = document.forms[0].ScoreThreshold.value;
    if (maxDoc <= 0)
    {
        alert('"Max Number Retrieved" can not be negative or zero.');
        submit = false;
    }
    if (maxDoc > $System.NumSearchMessagesToDisplay)
    {
        alert('"Max Number Retrieved" must be less than or equal to $System.NumSearchMessagesToDisplay which is set by the administrator.');
        submit = false;
    }
    if (threshold < 0 || threshold > 100)
    {
        alert('"Score Threshold" must have value between 0 and 100.');
        submit = false;
    }
    */

    if (submit == true)
    {
        button.form.submit();
    }
}

function openWindow()
{
    window.open("$System.httppath/user/matchmessage.html?Pattern=$Args.Pattern^&SearchHandle=$Args.SearchHandle",
                "_blank",
                "scrollbars,resizable,height=400,width=400");
}


