function setCookies() {
 with(document.post) {
  if (typeof(UserName)!="undefined") createCookie("k4_name", UserName.value);
  if (typeof(UserEmail)!="undefined") createCookie("k4_email", UserEmail.value);
  if (typeof(UserPassword)!="undefined") createCookie("k4_pass", UserPassword.value);
 }
}

function createCookie(name,value) {
 var date = new Date();
 date.setTime(date.getTime()+(365*24*60*60*1000));
 var expires = "; expires="+date.toGMTString();
 document.cookie = name+"="+value+expires+"; path=/";
}

window.onload = function(e) {
 with(document.post) {
  if (typeof(UserName)!="undefined") UserName.value=get_cookie("k4_name");
  if (typeof(UserEmail)!="undefined") UserEmail.value=get_cookie("k4_email");
  if (typeof(UserPassword)!="undefined") UserPassword.value=get_pass("k4_pass");
  if (typeof(StayInThread)!="undefined") {
   var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
   for(var i = 0; i < hashes.length; i++) {
    var hash = hashes[i].split('=');
    if (hash[0] == 'stay' && hash[1] == '1')
      StayInThread.checked = true;
   }
  }
 }
 document.delform.DeletePassword.value=get_pass("k4_pass");
 document.post.Attachment.removeAttribute("disabled");
 init();
}

function get_cookie(name){
 with(document.cookie){
  var index=indexOf(name+"=");
  if(index==-1) return '';
  index=indexOf("=",index)+1;
  var endstr=indexOf(";",index);
  if(endstr==-1)
   endstr=length;
  return decodeURIComponent(substring(index,endstr));
}}

function get_pass(name){
 var pass=get_cookie(name);
 if(pass) return pass;
 var chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 var pass='';
 for(var i=0;i<8;i++){
  var rnd=Math.floor(Math.random()*chars.length);
  pass+=chars.substring(rnd,rnd+1);
 }
 return(pass);
}

function quote(text) {
  if (document.selection) {
    document.post.PostMessage.focus();
    sel = document.selection.createRange();
    sel.text = ">>" + text + "\n";
  } else if (
      document.post.PostMessage.selectionStart ||
      document.post.PostMessage.selectionStart == "0") {
    var startPos = document.post.PostMessage.selectionStart;
    var endPos = document.post.PostMessage.selectionEnd;
    document.post.PostMessage.value =
	  document.post.PostMessage.value.substring(0, startPos) + 
	  ">>" + text + "\n" +
	  document.post.PostMessage.value.substring(endPos, document.post.PostMessage.value.length);
  } else {
    document.post.PostMessage.value += ">>" + text + "\n";
  }
}

function repquote(rep) {
  if (rep.match(/q([0-9]+)/)) {
    rep=rep.replace(/q/,"");
    if(document.post.PostMessage.value=="") {
      quote(rep);
    }
  }
}

var currentReply = null;
function replyhl(id) {
  if (currentReply != null) currentReply.className = "reply"
  currentReply = document.getElementById(id);
  if (currentReply) {currentReply.className = "replyhl";
  }
  
}

function init() {
  arr=location.href.split(/#/);
  if (arr[1]) {
  if (arr[1].match(/(q)?([0-9]+)/)) {
    rep=arr[1];
    re=arr[1].replace(/q/,"");
    replyhl(re);
    repquote(rep);
  }
  }
}	

function switchUploadPane()
{
	if(document.getElementById('UploadUrl').style.display == "none")
	{
		document.getElementById('UploadUrl').style.display = "block";
		document.getElementById('UploadFile').style.display = "none";
		document.post.Attachment.setAttribute("disabled", "disabled");
		document.post.AttachmentUrl.removeAttribute("disabled");
		document.post.AttachmentUrl.setAttribute("value", "http://");
	}
	else
	{
		document.getElementById('UploadUrl').style.display = "none";
		document.getElementById('UploadFile').style.display = "block";
		document.post.Attachment.removeAttribute("disabled");
		document.post.AttachmentUrl.setAttribute("disabled", "disabled");
	}
}

