// Global variables
// t is a global time set to clear the rating's message window
var timerPage;

// timerAjaxComments is a global timer set to keep track of how long it is
// taking for the Geogad server to respond. If it is longer than some
// prefered value, the program redirects the user input to another operation.
var timerAjaxPage;

function pageWaitMessage(id) {
	pageClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="pageMessageBox"><div style="text-align: center" ><span class="blueText">Sending....</span></div></div>';
}

function pageProblemMessage(id) {
	pageClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="pageMessageBox"><div style="text-align: center" ><span class="yellowBackground redfont">Problem getting new page. Please try again later.</span></div></div>';
	timerPage = window.setTimeout('pageClearMessage("'+id+'")', 7000); 
}

function pageClearMessage(id) {
	pageClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '';
}

function pageClearTimers() {
// free callback timer here
   window.clearTimeout(timerPage);
   window.clearTimeout(timerAjaxPage);
}


 function isAjaxSupportedPage() {
// Check if request can be created by is not used
   var request = null;
   if(typeof window.XMLHttpRequest != 'undefined')
   {
     request = new XMLHttpRequest();
   }
   else if(typeof window.ActiveXObject != 'undefined')
   {
     try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
     catch(err) { request = null; }
   }
   if (request != null)
   	return(true); 
   else 
   	return(false); 
}

function handleCommentPageReturn(data) {

//var ce = document.getElementById("check_error");
//ce.innerHTML += " In handleCommentPageReturn ";
//ce.innerHTML += " data= ["+data+"]";

// rewrite displayComments

   if ((data == null) || (data == "")) {
	// Big problem
		pageProblemMessage("displayComments"); 
   } else {  // Success; print data
   pageClearTimers();
   pageClearMessage("displayComments");

//   var str = eval( "(" + data + ")" );
   var str = data;

//ce.innerHTML += " data evaled ";

// data comes in as a JSON object
   var thisCount = str.Count;
   var thisLimit = str.Limit;
   var thisOffset = str.Offset;
   var thisNickname = str.Nickname;
   var thisRatingLimit = str.RatingLimit;
   var thisTID = str.tID;
   var thisCTID = str.ctID;
   var thisTSID = str.tsID;
   var thisPriv = str.Priv;
   var thisActive = str.Active;

   var commentCurrentPage = (thisOffset/thisLimit) + 1;

   if (str.Results.length > 0) {
      var tempStr = '';
// Show count box      
    var commentPageSelectCode = displayCountBox(thisCount, thisNickname, thisLimit, thisTID, thisCTID, thisTSID, thisRatingLimit, thisPriv, thisActive, commentCurrentPage, "displayComments");

    if (commentPageSelectCode.length > 0)
     	tempStr += commentPageSelectCode;

    tempStr += "<br/>";

	var i;
	for (i in str.Results) {
  		var output = printComments(str.Results[i], thisNickname, thisTID, thisCTID, thisTSID);
    		if (output.length > 0)
     			tempStr += output;
   	}

    tempStr += "<br/>";
    if (commentPageSelectCode.length > 0)
     	tempStr += commentPageSelectCode;

    var rt = document.getElementById("displayComments");
    if (tempStr.length > 0)
    	rt.innerHTML = tempStr;   
   } 
  } 
}

function showCommentsPage(nickname, pageSize, tID, ctID, tsID, ratingLimit, priv, active, pageNum, box) {

//var ce = document.getElementById("check_error");
//ce.innerHTML = " In showCommentsPage ";
//ce.innerHTML += " nickname= "+nickname+" tID= "+tID+" ctID= "+ctID+" tsID= "+tsID+" ratingLimit= "+ratingLimit+" priv= "+priv+" active= "+active+" pageNum= "+pageNum+" box= "+box;


// old browsers technique - seems to work fine
// get value from the ratingSelect select box
   var selectBox = document.getElementById("commentRatingSelect");
   var user_input_oldformat = selectBox.options[selectBox.selectedIndex].value;
//ce.innerHTML += " user_input_oldformat= "+user_input_oldformat;

   var userSelectedRatingLimit = parseInt(user_input_oldformat, 10);
//ce.innerHTML += " userSelectedRatingLimit= "+userSelectedRatingLimit;
   if (!isNaN(userSelectedRatingLimit)) 
	ratingLimit = userSelectedRatingLimit;

//ce.innerHTML += " ratingLimit= "+ratingLimit;


// Calculate offset for this page
   var offset = (pageNum - 1) * pageSize;
//ce.innerHTML += " offset= "+offset;

   if (isAjaxSupportedPage()) {
//ce.innerHTML += " In isAjaxSupportedPage ";
   	JGetCommentInfo.getCommentsJSON(nickname, pageSize, offset, tID, ctID, tsID, ratingLimit, priv, active, handleCommentPageReturn);
	pageClearTimers();
	commentClearTimers();
	pageWaitMessage(box);
	timerAjaxPage = window.setTimeout('pageProblemMessage("'+box+'")', 60000); 
   } else { 
//ce.innerHTML += " In NOT isAjaxSupportedPage ";
        var fullURL = window.location.href;
        var lastPos = fullURL.lastIndexOf("/");
        var baseURL = fullURL.substring(0, lastPos);
        if (tID > 0)
            baseURL += "/tour?tid="+tID;
        else if (ctID > 0)
            baseURL += "/customtour?ctid="+ctID;
        else if (tsID > 0)
            baseURL += "/tourstop?tsid="+tsID;
        baseURL += "&cp="+pageNum+"&rl="+ratingLimit;
        document.location.href=baseURL;

   } 
//ce.innerHTML += " Out showCommentsPage ";

}

function displayCountBox(count, nickname, commentPageSize, tID, ctID, tsID, ratingLimit, priv, active, commentCurrentPage, box) {  
    var comment = '';

// Figure out how many pages exist
    var totalCommentPages = Math.ceil(count/commentPageSize);

    if ((count > 0) && (totalCommentPages >= 2)) {        

        comment = "<div class='pageSelector'><div class='leftPage'>";
        comment += "<span class='dots'>Page:</span>";                        
        if (commentCurrentPage > 3) {
            // Print out
            comment += "<span class='go2Page' title='Newest Page' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", 1, \""+box+"\");' "; 
            comment += ">Newest</span>";
            comment += "<span class='dots'>...</span>";
        }
        if ((commentCurrentPage-2) > 0) {
            comment += "<span class='go2Page' title='Page "+(commentCurrentPage-2)+"'";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage-2)+", \""+box+"\");' "; 
            comment += ">"+(commentCurrentPage-2)+"</span>";
        }
        if ((commentCurrentPage-1) > 0) {
            comment += "<span class='go2Page' title='Page "+(commentCurrentPage-1)+"' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage-1)+", \""+box+"\");' "; 
            comment += ">"+(commentCurrentPage-1)+"</span>";
        }        
            comment += "<span class='currentPage' title='Current Page'>"+commentCurrentPage+"</span>";
        if ((commentCurrentPage+1) <= totalCommentPages) {
            comment += "<span class='go2Page' title='Page "+(commentCurrentPage+1)+"' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage+1)+", \""+box+"\");' "; 
            comment += ">"+(commentCurrentPage+1)+"</span>"; 
               } 
        if ((commentCurrentPage+2) <= totalCommentPages) {
            comment += "<span class='go2Page' title='Page "+(commentCurrentPage+2)+"' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage+2)+", \""+box+"\");' "; 
            comment += ">"+(commentCurrentPage+2)+"</span>";            
        } 
        if ((commentCurrentPage+3) <= totalCommentPages) {
            comment += "<span class='dots'>...</span>";                
            comment += "<span class='go2Page' title='Oldest Page' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+totalCommentPages+", \""+box+"\");' "; 
            comment += ">Oldest</span>";
        } 
        comment += "</div><div class='rightPage'>";    
        if ((commentCurrentPage-1) > 0) {
            comment += "<span class='go2Page' title='Previous Page' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage-1)+", \""+box+"\");' "; 
            comment += ">Prev</span>";
        }        
        if ((commentCurrentPage+1) <= totalCommentPages) {
            comment += "<span class='go2Page' title='Next Page' ";
            comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
            comment += "onclick='showCommentsPage(\""+nickname+"\", "+commentPageSize+", "+tID+", "+ctID+", "+tsID+", "+ratingLimit+", "+priv+", "+active+", "+(commentCurrentPage+1)+", \""+box+"\");' "; 
            comment += ">Next</span>";                
        }  
            comment += "</div>";     
            comment += "</div>";     
    } 
    return comment;
}

function printComments(data, thisNickname, tID, ctID, tsID) {
//var ce = document.getElementById("check_error");
//ce.innerHTML += " In printComments ";

//ce.innerHTML += " thisNickname= "+thisNickname;
   if (thisNickname == 'null')
        thisNickname = null;
//ce.innerHTML += " After thisNickname= "+thisNickname;

       var comment;
        
//    var str = eval( "(" + data + ")" );
//ce.innerHTML += " data evaled ";
 
// Pull data out of JSON string
    var commentID = data.CommentID;
    var reply2 = data.Reply2;
    var title = data.Title;
    var commentInfo = data.Comment;
    var authorComment = data.Author;
    var date = data.Date;
    var banned = data.Banned;
    var up = data.Up;
    var down = data.Down;
    var spam = data.Spam;
    var ratingComment = data.Rating;
//ce.innerHTML += " Got Basics ";
    if (reply2 <= 0) {
                comment = "<div id='comment"+commentID+"' name='comment"+commentID+"' class='commentBox";
                if (banned || spam) { comment += " spammed"; } 
                comment += "'";
                comment += ">";
    } else {
                comment = "<div id='comment"+commentID+"' name='comment"+commentID+"' class='replyBox";
                if (banned || spam) { comment += " spammed"; } 
                comment += "'>";
    }
//ce.innerHTML += " OK1 comment.length= "+comment.length;
    if (banned || spam) {  
                var bannedCommentTitle;
                if (reply2 <= 0)
                    bannedCommentTitle = "Comment marked as spam";
                else 
                    bannedCommentTitle = "Reply marked as spam";
                comment += "<div id='bannedBlockShow"+commentID+"' name='bannedBlockShow"+commentID+"' class='bannedBoxShow' style='display:inline; visibility: visible' >";
                comment += "<div class='bannedTitleShow'>"+bannedCommentTitle+"</div>";
                comment += "<div id='showCommentLink"+commentID+"' name='showCommentLink"+commentID+"' class='showCommentLink' style='display:inline; visibility: visible' ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #aaaaaa\"; this.style.cursor=\"auto\";' "; 
                comment += "onclick='showBannedComment("+commentID+");'>Show</div>";
                comment += "</div>";
                comment += "<div id='bannedBlockHide"+commentID+"' name='bannedBlockHide"+commentID+"' class='bannedBoxHide' style='display:none; visibility: hidden' >";
                comment += "<div class='bannedTitleHide'>"+bannedCommentTitle+"</div>";
                comment += "<div id='hideCommentLink"+commentID+"' name='hideCommentLink"+commentID+"' class='hideCommentLink' style='display:inline; visibility: visible' ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000ff\"; this.style.cursor=\"auto\";' "; 
                comment += "onclick='hideBannedComment("+commentID+");'>Hide</div>";
                comment += "</div>";
                comment += "<div id='commentInfo"+commentID+"' style='display:none; visibility: hidden'>"; 
    }    
//ce.innerHTML += " OK2 comment.length= "+comment.length;
    comment += "<div class='commentTitleBlock'>";
    comment += "<div style='clear:both'></div>";  
    comment += "<div class='commentTitleLeft'>";
    comment += "<a class='authorLink' href='user/"+authorComment+"' >"+authorComment+"</a>";
    comment += "<span class='timePeriod'>("+date+")</span>";
    comment += "<span class='spammedTitleMessage' id='spammedTitleMessage"+commentID+"' name='spammedTitleMessage"+commentID+"' style='display:none; visibility: hidden'>Marked as spam</span>";
    comment += "</div>";
    
    comment += "<div class='commentTitleRight'>";
    comment += "<span id='titleMessage"+commentID+"' name='titleMessage"+commentID+"' class='commentTitleMessage' ></span>";
//ce.innerHTML += " OK3 comment.length= "+comment.length;
    if (ratingComment == 0)
                comment += "<span id='comment"+commentID+"RatingCount' name='comment"+commentID+"RatingCount' style='color: #333333;'>0&nbsp;</span>";
    else if (ratingComment > 0)
                if (ratingComment == 99999) {
                // Problem getting data; skip
                } else {
                    comment += "<span id='comment"+commentID+"RatingCount' name='comment"+commentID+"RatingCount' style='color: #00dd00;'>"+ratingComment+"&nbsp;</span>";
                } else if (ratingComment < 0)
                    comment += "<span id='comment"+commentID+"RatingCount' name='comment"+commentID+"RatingCount' style='color: #dd0000;'>"+ratingComment+"&nbsp;</span>";
            
            // If the user has already rated or if this comment has already been banned as spam,
            // only show the gray pointer jpgs. Right now, all users can rate comments.
            if (banned || spam){
                comment += "<img id='poorComment"+commentID+"' name='poorComment"+commentID+"' alt='Comment rating unavailable' src='images/downgray.gif' class='arrowImg' /><img id='goodComment"+commentID+"' name='goodComment"+commentID+"' alt='Comment rating unavailable' src='images/upgray.gif' class='arrowImg' />";
            } else if (up || down) {
                comment += "<img id='poorComment"+commentID+"' name='poorComment"+commentID+"' alt='You have already rated' src='images/downgray.gif' class='arrowImg' onclick='alreadyRatedCommentMessage(\"titleMessage"+commentID+"\");' /><img id='goodComment"+commentID+"' name='goodComment"+commentID+"' alt='You have already rated' src='images/upgray.gif' class='arrowImg' onclick='alreadyRatedCommentMessage(\"titleMessage"+commentID+"\");' />";
            } else if (thisNickname != null) {
                    comment += "<img id='poorComment"+commentID+"' name='poorComment"+commentID+"' alt='Poor comment' src='images/downgray.gif' class='arrowImg' onmouseover='this.src = \"images/downred.gif\";' onmouseout ='this.src = \"images/downgray.gif\";' onclick='CommentPoorRating(\""+thisNickname+"\", "+commentID+");' /><img id='goodComment"+commentID+"' name='goodComment"+commentID+"' alt='Good comment' src='images/upgray.gif' class='arrowImg' onmouseover='this.src = \"images/upgreen.gif\";' onmouseout ='this.src = \"images/upgray.gif\";' onclick='CommentGoodRating(\""+thisNickname+"\", "+commentID+");'/ >";
            } else {
                comment += "<img id='poorComment"+commentID+"' name='poorComment"+commentID+"' alt='Poor comment' src='images/downgray.gif' class='arrowImg' onclick='login2RateCommentMessage(\"titleMessage"+commentID+"\");' /><img id='goodComment"+commentID+"' name='goodComment"+commentID+"' alt='Good comment' src='images/upgray.gif' class='arrowImg' onclick='login2RateCommentMessage(\"titleMessage"+commentID+"\");' />";
            }
//ce.innerHTML += " OK4 comment.length= "+comment.length;
    comment += "</div>";  
    comment += "<div style='clear:both'></div>";  
    comment += "</div>";

    
    comment += "<div id='commentBlock"+commentID+"' name='commentBlock"+commentID+"' class='commentBlock' >";        
    comment += "<div class='commentTextRight' >";
    comment += "<div style='clear:both'></div>";  
// If the user has already rated or if this comment has already been banned as spam,
// only show the gray pointer jpgs. Right now, all users can rate comments.

    if (banned || spam){
// Show nothing
                  comment += "Marked as spam.";
    } else if (thisNickname == null) {
                comment += "(<span id='replyComment"+commentID+"' name='replyComment"+commentID+"' class='linkReplySpam' title='Reply to comment' onclick='login2ReplyMessage(\"titleMessage"+commentID+"\");'  ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000dd\"; this.style.cursor=\"auto\";' >Reply</span>"; 
                comment += ")&nbsp;(";
                comment += "<span id='spamComment"+commentID+"' name='spamComment"+commentID+"' class='linkReplySpam' title='Mark as spam' onclick='login2MarkSpamMessage(\"titleMessage"+commentID+"\");'  ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000dd\"; this.style.cursor=\"auto\";' >Spam</span>)";
    } else {
                comment += "(<span id='replyComment"+commentID+"' name='replyComment"+commentID+"' class='linkReplySpam' title='Reply to comment' onclick='reply2Comment(\""+thisNickname+"\", "+tID+", "+ctID+", "+tsID+", "+commentID+", \"newReplyMessageBox"+commentID+"\");' ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000dd\"; this.style.cursor=\"auto\";' >Reply</span>";    
                comment += ")&nbsp;(";
                comment += "<span id='spamComment"+commentID+"' name='spamComment"+commentID+"' class='linkReplySpam' title='Mark as spam' onclick='CommentSpam(\""+thisNickname+"\", "+commentID+");'  ";
                comment += "onmouseover='this.style.borderBottom=\"none\"; this.style.cursor=\"pointer\";' onmouseout='this.style.borderBottom=\"1px solid #0000dd\"; this.style.cursor=\"auto\";' >Spam</span>)";
    }          


//ce.innerHTML += " OK5 comment.length= "+comment.length;
    comment += "</div>";         
    
    
    comment += "<div style='clear:both'></div>";           
    comment += "<div class='commentText' >";
    comment += commentInfo;
    comment += "</div>";                
    comment += "<div id='newReplyMessageBox"+commentID+"' name='newReplyMessageBox"+commentID+"' ></div>";
    comment += "<div id='newReplyBox"+commentID+"' name='newReplyBox"+commentID+"' ></div>";                        
    comment += "</div>";
    if (banned || spam) { comment += "</div>"; }
    comment += "</div>";
    comment += "<div style='clear:both' ></div>";           
//ce.innerHTML += " OK6 comment.length= "+comment.length;

    return(comment);    
}