var MyRequest;
var MyRequestComments;
var MyRequestSubmitComment;
var MyRequest2;
var MyRequest3;
var MyRequest4;
var MyRequestWordCount;

var SubmitHash = "c4c5004274d3b83f8c68ebe6726a010c";
var AuthorHash = "ae3aa0c0d32ad8d52c1e7c68cdb2dce7";

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}


function CreateRequest(){
	try{
		request = new XMLHttpRequest();
	}
	catch (trymicrosoft){
		try{
			request = new ActiveXObject("Msxm12.XMLHTTP");
		}
		catch (othermicrosoft){
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed){
				request = null;
			}
		}
	}
	if (request == null)
		alert("error creating request object!");
	else
		return(request);

}

function GetStory(){
	var Paused = document.getElementById("Pause").value;

	if (Paused != "Yes"){



		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();


		var Url = "GetStory.php?Random=" + hours + minutes + seconds;

		if (MyRequest.readyState != 0 && MyRequest.readyState != 4){
			//alert ("Reqeust not readY!");
		}
		else{



			MyRequest.open("GET", Url, true);

			MyRequest.onreadystatechange = PopulateStory;//(NoteId, BoxIsChecked, AlertMessage);
			MyRequest.send(null);
		}
	}
	setTimeout("GetStory()", 2000);
}



function PopulateStory(){

    if(MyRequest.readyState == 4 && MyRequest.status == 200){
		var Info = MyRequest.responseText;

		if (Info != false && Info != "" && Info != "false"){

			var jsonData = Info.parseJSON();

			var MyString = "";

			MyString = ReplaceNewLines(jsonData["Text"]);

			document.getElementById("StoryText").innerHTML = MyString;


			var Scroll = document.getElementById("Scroll").value;

			if (Scroll == "Yes"){

				document.getElementById("StoryText").scrollTop=100000000;
			}

			var now = new Date();
			var hours = now.getHours();
			var minutes = now.getMinutes();
			var seconds = now.getSeconds();

			document.getElementById("ReaderNotice").innerHTML ="<span style='color:gray;'>Story Last Load: " + hours + ":" + minutes + ":" + seconds + " </span>";



		}
		else{
			document.getElementById("StoryText").innerHTML = "No entries found.  Perhaps the author has not yet begun.";
		
					var now = new Date();
			var hours = now.getHours();
			var minutes = now.getMinutes();
			var seconds = now.getSeconds();

			document.getElementById("ReaderNotice").innerHTML ="<span style='color:gray;'>Last Load: " + hours + ":" + minutes + ":" + seconds + " </span>";

		}


	}
}



function GetComments(){

	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();


	var Url = "GetComments.php?Random=" + hours + minutes + seconds;

	MyRequestComments.open("GET", Url, true);

	MyRequestComments.onreadystatechange = PopulateComments;//(NoteId, BoxIsChecked, AlertMessage);
	MyRequestComments.send(null);


	setTimeout("GetComments()", 2000);
}


function PopulateComments(){

	var CommentTemplate2 = "<div class='CommentContainer'>"
			+ "<table>"
			+ "<tr><td>{Date}</td><td>{Name}</td></tr>"
			+ "<tr><td colspan=2>{Text}</td></tr>"
			+ "</table>";


    if(MyRequestComments.readyState == 4 && MyRequestComments.status == 200){

		var Info = MyRequestComments.responseText;


		if (Info != "\"none\""){

			var jsonData = Info.parseJSON();

			var CurrentData = "";


			for (i = 0; i < jsonData.length ;i++){

				var ThisClass= GetCommenterClass(jsonData[i].Name);

				var NameClass = ThisClass;
				var CommentClass = ThisClass;
				var DateClass = ThisClass;

				var NewData = "<div class='CommentContainer'>"
				+ "<table>"
				+ "<tr><td class='" + DateClass +"'>" + jsonData[i].DateAdded + "</td><td class='" + NameClass +"'>" + jsonData[i].Name + "</td></tr>"
				+ "<tr><td colspan=2 class='" + CommentClass + "'>" + jsonData[i].Comment + "</td></tr>"
				+ "</table>";

				CurrentData = CurrentData + NewData;
			}

			document.getElementById("CommentList").innerHTML = CurrentData;




		}
		else{
			document.getElementById("CommentList").innerHTML = "No Comments Found.";

		}

		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();

		document.getElementById("CommentNotice").innerHTML ="<span style='color:gray;'>Comments Last Load: " + hours + ":" + minutes + ":" + seconds + " </span>";

	}
}

function AddComment(){
	var Url = "SubmitComment.php";

	var Name = document.getElementById("Name").value;
	var Comment= document.getElementById("Comment").value;

	if (Comment == ""){
		alert("Please enter a comment first!");
		return(false);
	}
	else{
		var Data = "Name=" + Name + "&Comment=" + Comment + "&SubmitHash=" + SubmitHash;

		MyRequestSubmitComment.open("POST", Url, true);

		MyRequestSubmitComment.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		MyRequestSubmitComment.onreadystatechange = AddCommentStatus;//(NoteId, BoxIsChecked, AlertMessage);

		MyRequestSubmitComment.send(Data);
	}
}

function AddCommentStatus(){

    if(MyRequestSubmitComment.readyState == 4 && MyRequestSubmitComment.status == 200){
		var Info = MyRequestSubmitComment.responseText;

		document.getElementById("Comment").value="";
		document.getElementById("Comment").blur();
	}

}


function GetCommenterClass(Name){

	if (Name == "Christy"){
		return("Author");
	}
	else if (Name == "Ron"){
		return("Ron");
	}
	else if (Name == "Toby"){
		return("Toby");
	}
	else if (Name == "Anne"){
		return("Anne");
	}
	else if (Name == "Alison"){
		return("Alison");
	}
	else{
		return("normal");
	}

}

function SubmitStory(){
	var Url = "SubmitStory.php";

	var Contents = document.getElementById("StoryText").value;
	var EntryId = document.getElementById("EntryId").value;

	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();

	document.getElementById("notice").innerHTML ="<span style='color:black;'>Attempting to save document (" + hours + ":" + minutes + ":" + seconds + ") </span>";

	if (Contents != "" && EntryId != ""){


		
		if (MyRequestSubmitStory.readyState != 0 && MyRequestSubmitStory.readyState != 4){
			//alert ("Reqeust not readY!");
		}
		else{
			var Data = "Contents=" + Contents + "&EntryId=" + EntryId + "&SubmitHash=" + AuthorHash;

			MyRequestSubmitStory.open("POST", Url, true);

			MyRequestSubmitStory.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');


			MyRequestSubmitStory.onreadystatechange = SubmitStoryStatus;


			MyRequestSubmitStory.send(Data);
		}
	}
	else{
		document.getElementById("notice").innerHTML ="<span style='color:black;'>Nothing to save (empty window?)(" + hours + ":" + minutes + ":" + seconds + ") </span>";
	}


		setTimeout("SubmitStory()", 2000);
}




function SubmitStoryStatus(){
//alert("doing SubmitStoryStatus() - state = " + MyRequestSubmitStory.readyState + ", status = " + MyRequestSubmitStory.status);
//alert("SubmitStoryStatus()"); 

    if(MyRequestSubmitStory.readyState == 4 && MyRequestSubmitStory.status == 200){
		var Info = MyRequestSubmitStory.responseText;
		if (Info == "1"){
			var now = new Date();
			var hours = now.getHours();
			var minutes = now.getMinutes();
			var seconds = now.getSeconds();

			document.getElementById("notice").innerHTML ="<span style='color:green;'>Document last saved: " + hours + ":" + minutes + ":" + seconds + " </span>";
		}
		else{

			document.getElementById("notice").innerHTML ="<span style='color:red;'>error saving document! <BR> " + Info + "</span>";

		}

	}
	else{
		var TempHTML = document.getElementById("notice").innerHTML;
		document.getElementById("notice").innerHTML = TempHTML;// + " (RS: " + MyRequestSubmitStory.readyState + "/" + MyRequestSubmitStory.status;
	}


}



function ReplaceNewLines(MyString){

	
	MyString = escape(MyString);

	var Found = 0;

	if (MyString.indexOf('%0D%0A') > -1){
		Regex = /%0D%0A/g;
		Found = 1;
	}
	if (MyString.indexOf('%0A') > -1){
		Regex = /%0A/g;
		Found = 1;
	}
	if (MyString.indexOf('%0D') > -1){
		Regex = /%0D/g;
		Found = 1;
	}

	if (Found == 1){

		return unescape( MyString.replace(Regex, '<br />') );
	}
	else{
		return(unescape(MyString));
	}
}


function CheckForSession(){
	var Url = "CheckForSession.php";

	var MyVisitorId = document.getElementById("VisitorId").value;

		var Data = "VisitorId=" + MyVisitorId + "&SubmitHash=" + SubmitHash;

		MyRequest2.open("POST", Url, true);

		MyRequest2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		//MyRequest2.onreadystatechange = AddCommentStatus;

		MyRequest2.send(Data);
		setTimeout("CheckForSession()", 10000);

}


function GetSessionCount(){

		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();

		var Url = "GetSessionCount.php?Random=" + hours + minutes + seconds;

		MyRequest4.open("GET", Url, true);

		MyRequest4.onreadystatechange = PopulateSessionCount;
		MyRequest4.send(null);

		setTimeout("GetSessionCount()", 10000);
}



function PopulateSessionCount(){

    if(MyRequest4.readyState == 4 && MyRequest4.status == 200){
		var Info = MyRequest4.responseText;

		var jsonData = Info.parseJSON();

		if (jsonData == "1"){
			document.getElementById("SessionCount").innerHTML = jsonData + " person watching.";
		}
		else{
			document.getElementById("SessionCount").innerHTML = jsonData + " people watching.";
		}


		
	}
}


function GetAuthorLoggedIn(){
	var Paused = document.getElementById("Pause").value;

		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();

		var Url = "GetAuthorLoggedIn.php?Random=" + hours + minutes + seconds;

		MyRequest3.open("GET", Url, true);

		MyRequest3.onreadystatechange = PopulateGetAuthorLoggedIn;
		MyRequest3.send(null);

		setTimeout("GetAuthorLoggedIn()", 10000);
}



function PopulateGetAuthorLoggedIn(){

    if(MyRequest3.readyState == 4 && MyRequest3.status == 200){
		var Info = MyRequest3.responseText;

		var jsonData = Info.parseJSON();

		
		if (jsonData == "1"){
			document.getElementById("AuthorInSession").innerHTML = "<span style='color:green;'>The Author Is Logged In</span>";
		}
		else{
			document.getElementById("AuthorInSession").innerHTML = "<span>The Author Is Not Logged In</span>";
		}


	}
}


function GetWordCount(){

		var now = new Date();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();

		var Url = "GetWordCount.php?Random=" + hours + minutes + seconds;

		MyRequestWordCount.open("GET", Url, true);

		MyRequestWordCount.onreadystatechange = PopulateWordCount;
		MyRequestWordCount.send(null);

		setTimeout("GetWordCount()", 5000);
}



function PopulateWordCount(){

    if(MyRequestWordCount.readyState == 4 && MyRequestWordCount.status == 200){
		var Info = MyRequestWordCount.responseText;

		var jsonData = Info.parseJSON();

		if (jsonData != "0"){
			document.getElementById("count").innerHTML = jsonData + " words written.";
		}
		else{
			document.getElementById("count").innerHTML = "Error loading count.";
		}


		
	}
}







MyRequest = CreateRequest();
MyRequestComments = CreateRequest();
MyRequestSubmitComment = CreateRequest();
MyRequest2 = CreateRequest();
MyRequest3 = CreateRequest();
MyRequest4 = CreateRequest();
MyRequestWordCount = CreateRequest();
