function getCookie(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 "";
}

function setCookie(c_name,value) {
  document.cookie=c_name+ "=" +escape(value)+ ";path=/";
  checkCookie();
}

function checkCookie() {
  fontsize = getCookie('nff_fontsize');
  document.getElementById('mainColumn').style.fontSize = fontsize + 'em';
}
function isValidEmail(str) {
  // These comments use the following terms from RFC2822:
  // local-part, domain, domain-literal and dot-atom.
  // Does the address contain a local-part followed an @ followed by a domain?
  // Note the use of lastIndexOf to find the last @ in the address
  // since a valid email address may have a quoted @ in the local-part.
  // Does the domain name have at least two parts, i.e. at least one dot,
  // after the @? If not, is it a domain-literal?
  // This will accept some invalid email addresses
  // BUT it doesn't reject valid ones. 
  var atSym = str.lastIndexOf("@");
  if (atSym < 1) { return false; } // no local-part
  if (atSym == str.length - 1) { return false; } // no domain
  if (atSym > 64) { return false; } // there may only be 64 octets in the local-part
  if (str.length - atSym > 255) { return false; } // there may only be 255 octets in the domain

  // Is the domain plausible?
  var lastDot = str.lastIndexOf(".");
  // Check if it is a dot-atom such as example.com
  if (lastDot > atSym + 1 && lastDot < str.length - 1) { return true; }
  //  Check if could be a domain-literal.
  if (str.charAt(atSym + 1) == '[' &&  str.charAt(str.length - 1) == ']') { return true; }
  return false;
}
jQuery(document).ready(function() {

   $("body").delegate(".input.button_submit_add_thread", "click", function(){
      $(".addedtread").css("display", "block");
    });


jQuery(".replyPost a").click(function(e) {
  e.preventDefault();
  if (!jQuery(this).hasClass("clickedOnce")) {
    jQuery(this).addClass("clickedOnce");
    var ajaxURL = jQuery(this).attr("href").replace("template=forum","template=ajaxNewPost");
    jQuery.get(ajaxURL,function(data) {
      jQuery("#forum > p").after(data).next().find(".forumTitle").remove();
      jQuery("#forum > p").next().find(".newPostBack").remove();
    });
   }
});
jQuery("#postForm").live("submit",function(e) {
  e.preventDefault();
  var email = jQuery(this).find("input[name*='email']").val();
  signatureLength = jQuery(this).find("input[name*='signature']").val().length;
  if (!(isValidEmail(email) && signatureLength > 0)) {
    alert("En feil oppstod. Se etter at du har fylt ut alle felter og at du har skrevet korrekt e-post.");
  }
  else {
    jQuery.post(jQuery(this).attr("action"),jQuery(this).serialize(),function(data) {
      if(!(data.length > 500)) { // post successfully posted
        jQuery("#forum > p").next().remove();
        jQuery("#forum > p").after("<strong>Innlegget ble sendt!</strong>")<
        setTimeout("window.location.reload();",2000);
      }
      else {
        jQuery("#forum > p").next().remove();
        jQuery("#forum > p").after(data).next().find(".forumTitle").remove();
        jQuery("#forum > p").next().find(".newPostBack").remove();
      }
    });
  }
});
}); // end of things happening on document ready
