if (document.getElementById("tfc:params"))
{
   (function() {
      function BEV_tfc(domain, category, title, surl, rurl, turl, uli, notog)
      {
         this.enabled = false;
         this.comDivShowing = false;
         this.ntShowing = true;
         this.topics = [ "" ];;
         this.topicsLoaded = false;
         this.ajaxinit = false;
         this.xmlhttp = null;
         this.domain = domain;
         this.category = category;
         this.title = title;
         this.subURL = domain+surl;
         this.refreshURL = domain+rurl;
         this.topicsURL = domain+turl;
         this.useLocInfo = uli;
		 this.noToggle = notog;
         this.month = "";

         this.commentSaved = function()
         {
             alert("Your comment was saved - the list will be updated momentarily");
             this.refreshFeedbackList();
         }

         this.gotTopics = function()
         {
		    var rt = G__BEV_tfc.ajax.responseText.replace("__tfc", "this");
            // Get data from the server's response
            eval(rt);
            var topsel = document.getElementById("tfc:topsel");
            for (var i = 0; i < G__BEV_tfc.topics.length; i++)
            {
               var o = document.createElement("OPTION");
               o.value = "t"+i;
               o.innerHTML = G__BEV_tfc.topics[i];
               try {
                  topsel.add(o, null); // standards compliant; doesn't work with all IE
               }
               catch(ex) {
                  topsel.appendChild(o);
               }
            }
         }

         this.refreshFeedbackList = function()
         {
            var rt = document.getElementById("tfc:radtopic");
            var timesort = true;
            if (rt.checked)
               timesort = false;
            var url = this.refreshURL;
            url += timesort? "&sort=time": "&sort=topic";
            var if0 = document.getElementById('tfc:currentcomments');
            if0.src = "";
            if0.src = url;
         }

         this.getCookie = function(c_name)
         {
            if (document.cookie.length > 0)
            {
               c_start=document.cookie.indexOf(c_name + "=");
               if (c_start != -1)
               {
                  c_start=c_start + c_name.length+1;
                  c_end = document.cookie.indexOf(";", c_start);
                  if (c_end == -1)
                   c_end = document.cookie.length;
                  return unescape(document.cookie.substring(c_start, c_end));
               }
            }
            return "";
         }

         this.getXferObj = function()
         {
            if (window.XMLHttpRequest)
            {
               // code for IE7+, Firefox, Chrome, Opera, Safari
               this.ajax = new XMLHttpRequest();
            }
            else if (window.ActiveXObject)
            {
            // code for IE6, IE5
               this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else
            {
               alert("Ajax is not supported by this browser - can't post your message");
               return false;
            }

            this.ajaxinit = true;
            return true;
         }

         this.submitComment = function()
         {
            var url = this.subURL;
            var moniker = document.getElementById("tfc:moniker").value;
            if (moniker == "")
            {
               alert("Please enter a reader name or nickname - it does not need to be real, but please be consistent.");
               return;
            }
            var topic = document.getElementById("tfc:topic").value;
            if (topic == "")
            {
               alert("Please choose a topic (click the existing topics button), or start a new one (click the new topic button).");
               return;
            }
            var text = document.getElementById("tfc:comment").value;
            if (text == "")
            {
               alert("You have not entered any feedback or comment.");
               return;
            }
            url += "?category="+this.category;
            moniker = encodeURIComponent(moniker);
            url += "&moniker="+moniker;
            topic = encodeURIComponent(topic);
            url += "&topic="+topic;
            text = encodeURIComponent(text);
            url += "&blurb="+text;
            if (this.useLocInfo)
            {
               var tc = (typeof( window['city'] ) == "undefined")? "N/A": city;
               var tr = (typeof( window['region'] ) == "undefined")? "N/A": region;
               var tcn = (typeof( window['country'] ) == "undefined")? "N/A": country;
               tc = encodeURIComponent(tc);
               url += "&city="+tc;
               tr = encodeURIComponent(tr);
               url += "&region="+tr;
               tcn = encodeURIComponent(tcn);
               url += "&country="+tcn;
            }
            if (!this.ajaxinit)
            {
               if (!this.getXferObj())
                  return;
            }
            this.ajax.open("GET", url, true);
            this.ajax.onreadystatechange = function() { if (G__BEV_tfc.ajax.readyState == 4) G__BEV_tfc.commentSaved(); };
            this.ajax.send(null);
         }

         this.toggleCommentDiv = function()
         {
            if (!this.enabled)
            {
               alert("Please wait until the page is fully loaded");
               return;
            }
            var comdiv = document.getElementById("tfc:sendcomment");
            if (this.comDivShowing)
               comdiv.style.display = "none";
            else
               comdiv.style.display = "block";
            this.comDivShowing = !this.comDivShowing;
            var moniker = document.getElementById("tfc:moniker");
            var last = this.getCookie("moniker");
            moniker.value = last;
            if (!this.topicsLoaded)
            {
               var iframe = document.getElementById("tfc:currentcomments");
               iframe.src = this.refreshURL+"&sort=time";
               if (!this.ajaxinit)
               {
                  if (!this.getXferObj())
                     return;
               }
               this.ajax.open("GET", this.topicsURL+"&sort=time", true);
               this.ajax.onreadystatechange = function() { if (G__BEV_tfc.ajax.readyState == 4) G__BEV_tfc.gotTopics(); };
               this.ajax.send(null);
               this.topicsLoaded = true;
            }
         }

         this.toggleTopic = function()
         {
            var ntdiv = document.getElementById("tfc:ntdiv");
            var etdiv = document.getElementById("tfc:etdiv");
            if (this.ntShowing)
            {
               ntdiv.style.display = "none";
               etdiv.style.display = "block";
            }
            else
            {
               ntdiv.style.display = "block";
               etdiv.style.display = "none";
            }
            this.ntShowing = !this.ntShowing;
         }

         this.updateTopic = function()
         {
            var topic = document.getElementById("tfc:topic");
            var topsel = document.getElementById("tfc:topsel");
            topic.value = topsel.options[topsel.selectedIndex].innerHTML;
         }

         this.toggleSort = function()
         {
             this.refreshFeedbackList();
         }


         this.emitHTML = function()
         {
            var outerdiv = document.getElementById("tfc:outerdiv");
            outerdiv.innerHTML = this.html;
            var title = document.getElementById("tfc:title");
            title.innerHTML = this.title;
            this.enabled = true;
			if (this.noToggle)
			   this.toggleCommentDiv();
         }

         var now = new Date();
         var y = now.getFullYear();
         var m = now.getMonth();
         var ms = ""+(m+1);
         if (ms.length < 2) ms = "0"+ms;
         this.month = ""+y+ms;

         this.refreshURL += "?category="+category+"&month="+this.month;
         this.topicsURL += "?category="+category+"&month="+this.month;

         var t = "<div style=\"display:none; width:800px; height:430px;\" onload=\"onDivLoad()\" id=\"tfc:sendcomment\">";
         t += "<center>\n<br><span id=\"tfc:title\" class=\"bighead\">TFC Title:</span>\n<table width=\"700\">\n<tr>";
         t += "<td width=\"50%\">Comments on the current month:</td>\n";
         t += "<td>Sort by: Topic <sub><input type=\"radio\" onclick=\"G__BEV_tfc.toggleSort()\" id=\"tfc:radtopic\" name=\"sortorder\" value=\"topic\"></sub>\n";
         t += "Date/time <sub><input type=\"radio\" onclick=\"G__BEV_tfc.toggleSort()\" id=\"tfc:radtime\" name=\"sortorder\" value=\"time\" checked=\"true\"></sub></td>\n";
         t += "</tr>\n</table>\n<iframe src=\"\" id=\"tfc:currentcomments\" width=\"700\" height=\"200\" border=\"1\"></iframe>\n<p>";
         t += "<table width=\"700\">\n<tr><td width=\"50%\"><b><i>Add your feedback on the current month:</i></b></td><td valign=\"bottom\">Existing topic\n";
         t += "<sub><input id=\"tfc:extop\" name=\"topicrad\" type=\"radio\" onclick=\"G__BEV_tfc.toggleTopic()\"></sub>\nNew topic <sub><input id=\"tfc:newtop\" name=\"topicrad\" type=\"radio\" onclick=\"G__BEV_tfc.toggleTopic()\" checked></td></sub>\n";
         t += "<tr valign=\"top\"><td width=\"50%\">Reader name/moniker/alias: <input style=\"width:100px\" type=\"text\"\nid=\"tfc:moniker\" value=\"\">\n<img src=\"trans.gif\" width=\"40\" height=\"2\">Topic:</td>";
         t += "<td><div style=\"position:relative; display:block; border:none; width:300px; height:25px\" id=\"tfc:ntdiv\">\n<input style=\"width:280px\" type=\"text\" id=\"tfc:topic\" value=\"\">";
         t += "</div>\n<div style=\"position:relative; display:none; border:none; width:300px; height:25px\" id=\"tfc:etdiv\">";
         t += "<select onchange=\"G__BEV_tfc.updateTopic()\" style=\"width:280px\" id=\"tfc:topsel\"><option value=\"0\">(unspecified)</option></select></div>";
         t += "</td></tr></table><textarea id=\"tfc:comment\" name=\"comment\" style=\"width:700px; height:80px; border:1px solid black;\"></textarea><p>";
         t += "<button type=\"button\" onclick=\"G__BEV_tfc.submitComment()\" id=\"tfc:submitcomment\">Submit Comment</button>&nbsp;";
		 if (!this.noToggle)
            t += "<button type=\"button\" onclick=\"G__BEV_tfc.toggleCommentDiv()\" id=\"tfc:togglecomment\">Close Feedback Section</button>";
         t += "</center></div>";

         this.html = t;
      }
      var t = document.getElementById("tfc:params");
	  //alert(t.value);
      var po = eval("({"+t.value+"})");

      G__BEV_tfc = new BEV_tfc(po.site, po.category, po.title, po.surl, po.rurl, po.turl, po.useLocInfo, po.noToggle);
      G__BEV_tfc.emitHTML();
   })();
}

