﻿function ReloadSessionandPage(projectname,url)
    {
    //alert("ReloadSesionandPage");
    //Reload session variables and then reload the page
	var http = false;
    var reqstring = "action=LoadSession";
    reqstring = reqstring + "&projectname="+projectname;
    //alert("parent = " + parent.document.getElementById("txtactiveusername"));
    if (parent.document.getElementById("txtactiveusername") != null)
        {
        reqstring = reqstring + "&username="+parent.document.getElementById("txtactiveusername").value;
        reqstring = reqstring + "&password="+parent.document.getElementById("txtactivepassword").value;
        }
   	var d = new Date()
    reqstring = reqstring + "&dummy="+d.getHours()+d.getMinutes()+d.getSeconds();
    //alert("reqstring = " + reqstring);

    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else  {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                //load the url into the frame
                //document.src = url;
           		window.location.href=url;
                }
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }
    http.open("POST", "../_engine/ajaxaction.aspx", true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);
    }


function ShowCommentDialog()
    {
    //Determine if the page is in a frame or not
    if (document.getElementById("tblDialogWindow") != null)
        {
        var doc=document;
        }
     else if (parent.document.getElementById("tblDialogWindow") != null)
        {
        var doc=parent.document;
        }

    if (doc != null)
        {
        var s="";
        s = s + "<table style='width:100%;height:100%' cellpadding=0 cellspacing=0 border=0>";
        s = s + "<tr><td><textarea id='txtAddComment' style='width:100%;height:100%;font-family:tahoma,arial;border:0px solid transparent'></textarea></td></tr>";
        s = s + "<tr style='height:30px'><td style='background-color:#DDDDDD;border-top:solid 1px #CCCCCC;' align='right'><input type='button' id='btnok' value='Add Comment' onclick='AddComment()'/></td></tr>";

        doc.getElementById("spanheading").innerHTML = "Add Comment";
        doc.getElementById("tblDialogWindow").style.width = "500px"
        doc.getElementById("tblDialogWindow").style.height = "250px"
        doc.getElementById("tddialog").innerHTML = s;
        doc.getElementById("tbldialog").style.display = "";
        doc.getElementById("divtransparency").style.display = "";
        doc.getElementById("txtAddComment").focus();
        }
    }
    
function AddComment()
    {
    //Determine if the page is in a frame or not
    if (document.getElementById("tblDialogWindow") != null)
        {
        var sComment=document.getElementById("txtAddComment").value;
//        //encode the comment so that html code is displayed as text
//        sComment = escape(sComment);
//        sComment = sComment.replace(/\//g,"%2F");
//        sComment = sComment.replace(/\?/g,"%3F");
//        sComment = sComment.replace(/=/g,"%3D");
//        sComment = sComment.replace(/&/g,"%26");
//        sComment = sComment.replace(/@/g,"%40");

        

        var sPageID = TreeView1.SelectedNode.ID;
        //Hide the comment dialog
        document.getElementById("tddialog").innerHTML = "";
        document.getElementById("tbldialog").style.display = "none";
        document.getElementById("divtransparency").style.display = "none";
        }
     else if (parent.document.getElementById("tblDialogWindow") != null)
        {
        var sComment=parent.document.getElementById("txtAddComment").value;
        var sPageID = parent.TreeView1.SelectedNode.ID
        //Hide the comment dialog
        parent.document.getElementById("tddialog").innerHTML = "";
        parent.document.getElementById("tbldialog").style.display = "none";
        parent.document.getElementById("divtransparency").style.display = "none";
        }

    if (sComment != null && sComment != "" && sPageID != null)
        {
      	//Add comment to page using AJAX
	    var http = false;
        var reqstring = "action=addcomment";
        reqstring = reqstring + "&projectname="+GetProjectName();
        reqstring = reqstring + "&username="+getActiveUsername();
        reqstring = reqstring + "&password="+getActivePassword();
        reqstring = reqstring + "&pageid="+sPageID;
        //alert("comment = " + sComment);
        reqstring = reqstring + "&comment="+sComment;
        if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else  {http = new XMLHttpRequest();} 
        http.onreadystatechange=function() 
            {
            if(http.readyState == 4) 
                {
                if (http.status == 200) 
                    {
                    var sresponse=http.responseText;
                    //If the default status is 'pending' then inform the user
                    if (sresponse.toLowerCase().indexOf("pending") != -1)
                        {
                        alert("Your comment has been submitted, and will be published after being reviewed.") 
                        }
                    else
                        {
                        //Reload the page so that the comments are displayed
                        loadpage(sPageID);
                        }
                    }
                else 
                    {
                    alert("Error: "+http.status+http.responseText);
                    }
                }
            }
        http.open("POST", "../_engine/ajaxaction.aspx", true);
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        http.send(reqstring);
    
        }

    }
    
function ChangeCommentStatus(commentid)
    {
    var commentstatus = document.getElementById("lstcommentstatus"+commentid).options[document.getElementById("lstcommentstatus"+commentid).selectedIndex].text;

    //alert("change status " + commentid)
    //alert("new status " + commentstatus)
    //Change comment status using AJAX
    var http = false;
    var reqstring = "action=changecommentstatus";
    reqstring = reqstring + "&projectname="+GetProjectName_Page();
    reqstring = reqstring + "&username="+getActiveUsername();
    reqstring = reqstring + "&password="+getActivePassword();
    reqstring = reqstring + "&commentid="+commentid;
    reqstring = reqstring + "&status="+commentstatus;
    
    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else  {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                //Don't need to do anything
                }
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }
    http.open("POST", "../_engine/ajaxaction.aspx", true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);
    }
    
function DeleteComment(commentid)
    {
    if (confirm("Delete Comment?") == false)
        {
        return;
        }
    
   	//Delete reader comment using AJAX
    var http = false;
    var reqstring = "action=deletecomment";
    reqstring = reqstring + "&projectname="+GetProjectName_Page();
    reqstring = reqstring + "&username="+getActiveUsername();
    reqstring = reqstring + "&password="+getActivePassword();
    reqstring = reqstring + "&commentid="+commentid;
    //alert("reqstring = " + reqstring);
    if(navigator.appName == "Microsoft Internet Explorer") {http = new ActiveXObject("Microsoft.XMLHTTP");} else  {http = new XMLHttpRequest();} 
    http.onreadystatechange=function() 
        {
        if(http.readyState == 4) 
            {
            if (http.status == 200) 
                {
                //Hide the deleted comment
                document.getElementById("trComment"+commentid).style.display="none";
                }
            else 
                {
                alert("Error: "+http.status+http.responseText);
                }
            }
        }
    http.open("POST", "../_engine/ajaxaction.aspx", true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send(reqstring);

    }
    
function getActiveUsername()
    {
    //get the active username
    if (parent.document.getElementById("txtactiveusername") != null)
        {
        //get active username from parent frame
        return parent.document.getElementById("txtactiveusername").value;
        }
    else
        {
        //get active username from this frame
        return susername = document.getElementById("txtactiveusername").value;
        }
    }
    
function getActivePassword()
    {
    //get the active password
    if (parent.document.getElementById("txtactivepassword") != null)
        {
        //get active password from parent frame
        return parent.document.getElementById("txtactivepassword").value;
        }
    else
        {
        //get active password from this frame
        return susername = document.getElementById("txtactivepassword").value;
        }
    }

/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{

  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  //if the tag divpage exists, this indicates that the format is 'flat' and we should get and set content from divpage
  if (document.getElementById("divpage") != null)
    {
    //-- DIVPAGE --
    var thispage=document.getElementById("divpage");
    }
  else
    {
    //-- ENTIRE PAGE -- 
    var thispage=document.body;
    }
  
  if (!document.body || typeof(thispage.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  //var bodyText = document.body.innerHTML;
  var bodyText = thispage.innerHTML;
  //var bodyText = document.getElementById("divpage").innerHTML;
  //alert("bodyText = "+bodyText);
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
 
  //document.body.innerHTML = bodyText;
  thispage.innerHTML = bodyText;
  //document.getElementById("divpage").innerHTML = bodyText;
  return true;
}

function getSearchText()
    {
    var SearchText = "";
    //determine if the advanced search textbox exists
    if (document.getElementById("txtAdvancedSearch") != null)
        {
        if (document.getElementById("txtAdvancedSearch").value != "")
            {
            return document.getElementById("txtAdvancedSearch").value;
            }
        }
    else if (document.getElementById("txtsearch") != null)  
        {
        if (document.getElementById("txtsearch").value != "")
            {
            return document.getElementById("txtsearch").value;
            }
        }
    }

function GetProjectName_Page()
    {
    var projectname = window.location.href;
    var ipos = projectname.lastIndexOf("/");
    projectname = projectname.substring(0,ipos);
    ipos = projectname.lastIndexOf("/");
    projectname = projectname.substring(ipos+1);
    //convert any space characters
    projectname = projectname.replace(/%20/g, " ");
    return projectname
    }
