//sets messages to be displayed in text boxes
var FormTxt = Array();
FormTxt["search"] = "Enter search Term...";
FormTxt["author"] = "Your Name";
FormTxt["email"] = "Email Address (won't be made public)";
FormTxt["RssEmail"] = "Enter Your Email Address";
FormTxt["url"] = "Add a URL to this comment";
FormTxt["comment-text"] = "Type Your Comment Here...";

function setPrimarySection(sectionName) {
  var navElements = document.getElementById("siteNavMain").getElementsByTagName("li");

  for (var tmpEl = 0; tmpEl < navElements.length; tmpEl++) {
    if (sectionName != "" && navElements[tmpEl].innerHTML.toLowerCase().indexOf(">" + sectionName.toLowerCase()) > -1) {
      BCLink = navElements[tmpEl].getElementsByTagName("a")[0].href;
      navElements[tmpEl].className += " activeNav";
    } 
  }
}
  
//creates cookies
function createCookie(name,value,days)
{
 if (!days)
   days = 14;
    
	     var date = new Date();
 date.setTime(date.getTime()+(days*24*60*60*1000));
 var expires = "; expires="+date.toGMTString();
	
 document.cookie = name+"="+value+expires+"; path=/";
}//end createCookie

//reads a cookie
function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(";");

  for(var i=0;i < ca.length;i++)
  {
	   var c = ca[i];
    while (c.charAt(0)==' ')
      c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0)
    return c.substring(nameEQ.length,c.length);
	 }
	 return null;
}//end readCookie

//reads stored values into post fields
function readPostValues()
{
  if (readCookie("authorName") != null)
    document.getElementById("author").value = readCookie("authorName");

  if (readCookie("authorEmail") != null)
    document.getElementById("email").value = readCookie("authorEmail");
  
  if (readCookie("ResetCAPTCHA") == "1" && readCookie("commentSave") != null)
  {
    document.getElementById("comment-text").value = decodeURIComponent(readCookie("commentSave"));
    createCookie("ResetCAPTCHA","0",1);
    createCookie("comment-text","",1);
  }
}

//saves data for later use
function saveCommentData()
{
  //saves the user information in cookies
  if (document.getElementById("author").value != "")
    createCookie("authorName",document.getElementById("author").value);

  if (document.getElementById("email").value != "")
    createCookie("authorEmail",document.getElementById("email").value);
    
  if (document.getElementById("comment-text").value != "")
    createCookie("commentSave",encodeURIComponent(document.getElementById("comment-text").value));
}


//run when a user posts a comment
function formSubmit()
{
  if (document.getElementById("author").value == "" && document.getElementById("author").value != "Your Name")
  {
    alert("You must enter a name\nin order to comment.");
    return false;
  }

  if (document.getElementById("RequireEmail") != null && document.getElementById("email").value.search(/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/) < 0)
  {
    alert("You must enter a valid Email\nin order to comment.");
    return false;
  }

  
  if (document.getElementById("comment-text").value == "")
  {
    alert("You must enter a Comment\nin order to comment.");
    return false;
  }
  
  if (document.getElementById("captcha_code") != null && document.getElementById("captcha_code").value == "")
  {
    alert("You must enter the Security Code\nin order to comment.");
    return false;
  }

  //saves data for later use
  saveCommentData();
  
  //disables posting so we get fewer duplicate posts
  document.getElementById("comment-post").value = "Posting..";
  document.getElementById("comment-post").disabled = true;
  
  return true;
}

function initForms()
{
  AllTags = document.getElementsByTagName("input");
  for (var tmpT=0;tmpT<AllTags.length;tmpT++)
    if (FormTxt[AllTags[tmpT].id])
      setupInpArea(AllTags[tmpT]);
}
  
function setupInpArea(inpEle)
{
  inpEle.onfocus = function() {inpFocus(this)};
  inpEle.onblur = function() {inpBlur(this)};
  inpEle.defaultValue = FormTxt[inpEle.id];
  inpBlur(inpEle);
}
  
function inpBlur(inpEle)
{
  if (inpEle.value == "" || inpEle.value == FormTxt[inpEle.id])
  {
//    inpEle.className += " formUnsel";
    inpEle.value = FormTxt[inpEle.id];
  }
}

function inpFocus(inpEle)
{
  if (inpEle.value == FormTxt[inpEle.id])
  {
    inpEle.value = "";
//    inpEle.className = inpEle.className.replace("formUnsel","");
  }
}

function setDomain()
{
  var curDom = document.domain;
  document.domain = curDom.substr(curDom.indexOf(".")+1);
}

function calendarLoad(rspTxt, rspStatus)
{
  if (rspStatus == "success")
    document.getElementById("CalendarDisplay").innerHTML = rspTxt;
 
 if (location.href.indexOf("test=1") > 0)
  {
    alert(p2);
  }
}

function getCalendar(calURL)
{
  $("#CalendarDisplay").load(calURL,calendarLoad);
}
$.getQueryString = function () {};

function nav(sel) {
   if (sel.selectedIndex == -1) return;
   var opt = sel.options[sel.selectedIndex];
   if (opt && opt.value)
      location.href = opt.value;
}

function NumberComments(TotPosts, DspPosts)
{
  if (TotPosts == "")
    TotPosts = DspPosts;
  
  if (!isNaN(TotPosts) && !isNaN(DspPosts))
  {
    var StrPost = Number(TotPosts) - Number(DspPosts);
    
    if (StrPost < 0)
      StrPost = 0;
  
    $(".CommentNumber").each(function() {this.innerHTML = ++StrPost;});
  }
}

function CommentResetCAPTCHA()
{
  createCookie("ResetCAPTCHA","1",1);
}

function ShowCaptchaReset()
{
  $("<div class='CaptchaOutMsg'><span>This CAPTCHA has expired!</span><br /><button class='CAPTCHAResetBtn' onclick='return refreshCaptcha()'>&nbsp;Refresh Text&nbsp;</button><br /><small>(Your comment will not be lost.)</small></div>").appendTo("#captchaArea");

  document.getElementById("captcha_code").disabled = true;
  document.getElementById("comment-post").disabled = true;
}

function refreshCaptcha()
{
  saveCommentData();
  CommentResetCAPTCHA();
  location.href = location.protocol + "//"+location.host + location.pathname + "#post";
  location.reload();
  return false;
}


$.getQueryString = function () {};



