/*

 #
 # Javascript file for journal.php
 #

*/

 var isLivePreview = false;

 var Tags = new Array();

 Tags["bold"] = "[b]replace this text with the text you want to bold[/b]";
 Tags["italic"] = "[i]replace this text with the text you want to italicize[/i]";
 Tags["underline"] = "[u]replace this text with the text you want to underline[/u]";
 Tags["createlink"] = "[url:http://your.link.here]link text[/url]";
 Tags["email"] = "[mailto:your@email.here]email text[/mailto]";
 Tags["code"] = "[code]line of code[/code]";
 Tags["codesnippet"] = "[codesnippet]\nsnippet of code\n[/codesnippet]\n\n";
 Tags["quote"] = "[quote]\ntext to be quoted\n[/quote]\n\n";
 Tags["dialogue"] = "[dialogue]\nFirst Person: Says something\nSecond Person: Says Something\n[/dialogue]\n\n";
 Tags["color"] = "[color:@@#COLOR#@@]replace this text with the text you want to color[/color]";

 function init_journal()
 {
          isLivePreview = document.getElementById("comment_form_livepreview").checked; //set the flag on load because the person might have
                                                                                       //reloaded the page after setting it, in which case the flag
                                                                                       //will not be set
          document.getElementById("img_bold").onclick = doButton;
          document.getElementById("img_italic").onclick = doButton;
          document.getElementById("img_underline").onclick = doButton;
          document.getElementById("img_createlink").onclick = doButton;
          document.getElementById("img_email").onclick = doButton;
          document.getElementById("img_code").onclick = doButton;
          document.getElementById("img_codesnippet").onclick = doButton;
          document.getElementById("img_quote").onclick = doButton;
          document.getElementById("img_dialogue").onclick = doButton;
          document.getElementById("sel_color").onchange = doButton;

          if(!Firefox)
          {
             document.getElementById("sel_color").focus(); //stupid ie hides the select button... WHY?
          }

          document.getElementById("img_bold").onmouseover = highlight;
          document.getElementById("img_italic").onmouseover = highlight;
          document.getElementById("img_underline").onmouseover = highlight;
          document.getElementById("img_createlink").onmouseover = highlight;
          document.getElementById("img_email").onmouseover = highlight;
          document.getElementById("img_code").onmouseover = highlight;
          document.getElementById("img_codesnippet").onmouseover = highlight;
          document.getElementById("img_quote").onmouseover = highlight;
          document.getElementById("img_dialogue").onmouseover = highlight;

          document.getElementById("img_bold").onmouseout = unHighlight;
          document.getElementById("img_italic").onmouseout = unHighlight;
          document.getElementById("img_underline").onmouseout = unHighlight;
          document.getElementById("img_createlink").onmouseout = unHighlight;
          document.getElementById("img_email").onmouseout = unHighlight;
          document.getElementById("img_code").onmouseout = unHighlight;
          document.getElementById("img_codesnippet").onmouseout = unHighlight;
          document.getElementById("img_quote").onmouseout = unHighlight;
          document.getElementById("img_dialogue").onmouseout = unHighlight;
              
          generateCaptcha();
 }

 function verify()
 {
	  var captcha = document.getElementById("comment_form_captcha").value;
          verifyCaptcha(captcha, aux_verify);
 }

 function aux_verify(CaptchaVerification)
 {
	  eval(CaptchaVerification);

	  if(CaptchaVerified)
	  {
	     validate();
	  }

	  else
	  {
             alert("I'm sorry, I cannot submit your comment. The text you entered does not match the text in the image. If you are unable to read the text, please click the image to get a new one.");
	  }
 }

 function validate()
 {
          var comment = document.getElementById("comment_form_comment").value;
          var name = document.getElementById("comment_form_author").value;
          var email = document.getElementById("comment_form_email").value;
          var website = document.getElementById("comment_form_website").value;
          var mlflag = document.getElementById("comment_form_mlflag").checked;

          name = name.replace(/^\s+/, "").replace(/\s+$/, "");
          name = (name == "") ? "anonymous" : name;
          document.getElementById("comment_form_author").value = name;

          email = email.replace(/^\s+/, "").replace(/\s+$/, "");
          
          website = website.replace(/^\s+/, "").replace(/\s+$/, "");

          comment = comment.replace(/^\s+/, "").replace(/\s+$/, "");
          document.getElementById("comment_form_comment").value = comment;

          if(document.getElementById("comment_form_p_id"))
          {
             document.getElementById("comment_form_parent_id").value = document.getElementById("comment_form_p_id").options[document.getElementById("comment_form_p_id").selectedIndex].value;
          }

          if(comment == "")
          {
             alert("You need to make some sort of comment.");
          }

          else if(email == "" && mlflag)
          {
             alert("You have indicated that you would like to receive emails, but you have not provided an email address.");
          }

          else if(!email.match(/.+@[a-z0-9]+(\.[a-z0-9]+)+$/) && email != "")
          {
             alert("You have provided an invalid email address");
          }

          else if(website != "" && !website.match(/[-\w\.]+:\/\/([-\w\.]+)+(:\d+)?(:\w+)?(@\d+)?(@\w+)?([-\w\.]+)(\/([\w\/_\.]*(\?\S+)?)?)?/))
          {
             alert("You have provided an invalid website address");
          }

          else
          {
             document.getElementById("comment_form").submit();
          }


 }

 function toggleLivePreviewFlag()
 {
          isLivePreview = document.getElementById("comment_form_livepreview").checked;
 }

 function LivePreview()
 { 
          if(isLivePreview)
          {
             preview();
          }
 }

 function preview()
 {
          var comment = document.getElementById("comment_form_comment").value;
          var name = document.getElementById("comment_form_author").value;

          name = name.replace(/^\s+/, "").replace(/\s+$/, "");
          name = (name == "") ? "anonymous" : name;
          document.getElementById("comment_form_author").value = name;

          comment = comment.replace(/^\s+/, "").replace(/\s+$/, "");
          //document.getElementById("comment_form_comment").value = comment;

          if(comment == "" && !isLivePreview)
          {
             alert("You need to make some sort of comment.");
          }
 
          else
          {
             var Request = getXMLHttpRequestObject();

             comment = comment.replace(/&/g, "@@##AMP##@@");
             comment = comment.replace(/\+/g, "@@##PLUS##@@");
             comment = comment.replace(/\?/g, "@@##QUESTION##@@");

             var FormData = "action=previewComment&name=" + name + "&comment=" + comment;

             sendXMLHttpRequest(Request, "http://vivin.net/action.php", FormData, showCommentPreview);
          }
 }

 function showCommentPreview(comment)
 {
          var comment_form = document.getElementById("comment_form");
          var div_preview = document.getElementById("div_preview");
          var div_sidebar = document.getElementById("div_sidebar");

          div_preview.style.color = "#000000";
          div_preview.style.backgroundColor = "#f7f8bc";
          div_preview.style.borderStyle = "solid";
          div_preview.style.borderWidth = "1px";
          div_preview.style.borderColor = "#000000";
          div_preview.style.padding = "5px";
          div_preview.style.marginLeft = "30px";
          div_preview.style.marginRight = "30px";
          div_preview.style.marginBottom = "10px";

          comment = comment.replace(/@@##AMP##@@/g, "&");
          comment = comment.replace(/@@##PLUS##@@/g, "+");
          comment = comment.replace(/@@##QUESTION##@@/g, "?");

          comment = (isLivePreview) ? comment.replace(/>Preview Comment:</, "><span style = \"color:#005D00;\">Live!</span> Preview Comment:<") : comment;

          if(!Firefox)
          {
             div_preview.innerHTML = comment;
          }

          else
          {
             var parser = new DOMParser();
             var XMLdoc = parser.parseFromString("<div xmlns = \"http://www.w3.org/1999/xhtml\">" + comment + "</div>", "application/xhtml+xml");

             var i = div_preview.childNodes.length - 1;

             while(i != 0)
             {
                   div_preview.removeChild(div_preview.childNodes[i]);
                   i--;
             }

             var root = XMLdoc.documentElement;

             for(i = 0; i < root.childNodes.length; i++)
             {
                 div_preview.appendChild(document.importNode(root.childNodes[i], true));
             }  
          }

          div_sidebar.style.height = parseInt(OriginalSidebarHeight) + parseInt(div_preview.scrollHeight) + "px";
 }

 function highlight(anevent)
 {
          var element;

          anevent = (anevent) ? anevent : event;

          if(anevent)
          {
             element = (anevent.target) ? anevent.target : anevent.srcElement;

             element.style.borderStyle = "solid";
             element.style.borderWidth = "1px";
             element.style.borderColor = "#001e82";
             element.style.backgroundColor = "#7c8fcf";
          }
 }

 function unHighlight(anevent)
 {
          var element;

          anevent = (anevent) ? anevent : event;

          if(anevent)
          {
             element = (anevent.target) ? anevent.target : anevent.srcElement;

             element.style.borderStyle = "solid";
             element.style.borderWidth = "1px";
             element.style.borderColor = "#eeeeee";
             element.style.backgroundColor = "#eeeeee";
          }
 }


 function doButton(anevent)
 {
          var element;

          anevent = (anevent) ? anevent : event;

          if(anevent)
          {
             element = (anevent.target) ? anevent.target : anevent.srcElement;

             
                  var comment = document.getElementById("comment_form_comment");
                  var startPos = comment.selectionStart;
                  var endPos = comment.selectionEnd;

                  if(startPos == endPos)
                  {
                     if(element.id != "sel_color")
                     {
                        comment.value = comment.value.substring(0, startPos)
                                        + Tags[element.id.replace(/img_/, "")] 
                                        + comment.value.substring(endPos, comment.value.length);
                     }

                     else
                     {
                        var color = element.options[element.selectedIndex].value;
                        comment.value = comment.value.substring(0, startPos)
                                        + Tags[element.id.replace(/sel_/, "")].replace(/@@#COLOR#@@/, color) 
                                        + comment.value.substring(endPos, comment.value.length);
                     }
                  }

                  else
                  {
                     var selection = comment.value.substring(startPos, endPos);

                     if(element.id == "sel_color")
                     {
                        if(element.selectedIndex != 0)
                        {
                           var color = element.options[element.selectedIndex].value;
                           selection = Tags[element.id.replace(/sel_/, "")].replace(/@@#COLOR#@@/, color).replace(/\][^\[]+\[/, "]" + selection + "[");
                        }
                     }

                     else if(element.id != "img_email" && element.id != "img_createlink")
                     {
                        selection = Tags[element.id.replace(/img_/, "")].replace(/\][^\[]+\[/, "]" + selection + "[");
                     }

                     else
                     {
                        selection = Tags[element.id.replace(/img_/, "")].replace(/(mailto|url):[^\]]+/, "$1:" + selection);
                     }

                     comment.value = comment.value.substring(0, startPos)
                                     + selection
                                     + comment.value.substring(endPos, comment.value.length);
                  }

                           }

          LivePreview();
 }
