function get_cookie(name) {
	with(document.cookie) {
		var regexp=new RegExp("(^|;\\s+)"+name+"=(.*?)(;|$)");
		var hit=regexp.exec(document.cookie);
		if(hit&&hit.length>2) return unescape(hit[2]);
		else return '';
	}
};

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

function get_password(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 insert(text) {
	var textarea=document.forms.postform.comment;
	if(textarea) {
		if(textarea.createTextRange && textarea.caretPos) { // IE
			var caretPos=textarea.caretPos;
			caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==" "?text+" ":text;
		}
		else if(textarea.setSelectionRange) // Firefox
		{
			var start=textarea.selectionStart;
			var end=textarea.selectionEnd;
			textarea.value=textarea.value.substr(0,start)+text+textarea.value.substr(end);
			textarea.setSelectionRange(start+text.length,start+text.length);
		}
		else
		{
			textarea.value+=text+" ";
		}
		textarea.focus();
	}
}

function highlight(post)
{
	var cells=document.getElementsByTagName("td");
	for(var i=0;i<cells.length;i++) if(cells[i].className=="highlight") cells[i].className="reply";

	var reply=document.getElementById("reply"+post);
	if(reply)
	{
		reply.className="highlight";
/*		var match=/^([^#]*)/.exec(document.location.toString());
		document.location=match[1]+"#"+post;*/
		return false;
	}

	return true;
}



function set_stylesheet(styletitle,norefresh)
{
	set_cookie("wakabastyle",styletitle,365);

	var links=document.getElementsByTagName("link");
	var found=false;
	for(var i=0;i<links.length;i++)
	{
		var rel=links[i].getAttribute("rel");
		var title=links[i].getAttribute("title");
		if(rel.indexOf("style")!=-1&&title)
		{
			links[i].disabled=true; // IE needs this to work. IE needs to die.
			if(styletitle==title) { links[i].disabled=false; found=true; }
		}
	}
	if(!found) set_preferred_stylesheet();
}

function set_preferred_stylesheet()
{
	var links=document.getElementsByTagName("link");
	for(var i=0;i<links.length;i++)
	{
		var rel=links[i].getAttribute("rel");
		var title=links[i].getAttribute("title");
		if(rel.indexOf("style")!=-1&&title) links[i].disabled=(rel.indexOf("alt")!=-1);
	}
}

function get_active_stylesheet()
{
	var links=document.getElementsByTagName("link");
	for(var i=0;i<links.length;i++)
	{
		var rel=links[i].getAttribute("rel");
		var title=links[i].getAttribute("title");
		if(rel.indexOf("style")!=-1&&title&&!links[i].disabled) return title;
	}
	return null;
}

function get_preferred_stylesheet()
{
	var links=document.getElementsByTagName("link");
	for(var i=0;i<links.length;i++)
	{
		var rel=links[i].getAttribute("rel");
		var title=links[i].getAttribute("title");
		if(rel.indexOf("style")!=-1&&rel.indexOf("alt")==-1&&title) return title;
	}
	return null;
}

function set_inputs(id) {
  with(document.getElementById(id)) {
    var bump_val = get_cookie("bump");
    if(!name.value) name.value=get_cookie("name"); 
    if(!email.value) email.value=get_cookie("email"); 
    if(!password.value) password.value=get_password("password"); 
    if(bump_val=='on') bump.checked=true;
    if(bump_val=='off') bump.checked=false;
  } 
}

function set_delpass(id) { with(document.getElementById(id)) password.value=get_cookie("password"); }

window.onunload=function(e)
{
	if(style_cookie)
	{
		var title=get_active_stylesheet();
		set_cookie(style_cookie,title,365);
	}
}

window.onload=function(e)
{
	var match;

	if(match=/#i([0-9]+)/.exec(document.location.toString()))
	if(!document.forms.postform.comment.value)
	insert(">>"+match[1]);

	if(match=/#([0-9]+)/.exec(document.location.toString()))
	highlight(match[1]);
}

if(style_cookie)
{
	var cookie=get_cookie(style_cookie);
	var title=cookie?cookie:get_preferred_stylesheet();
	set_stylesheet(title);
}

var expDays = 10000;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=")) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function cookieForms(mode) {

  if(mode == 'open') {
    cookieValue = GetCookie('saved_postform');
    if(cookieValue != null) {
      document.getElementById("postform").agree.checked = true;
    }
  }

  if(mode == 'save') {
    passValue = 'cb'+document.getElementById("postform").agree.checked;
    set_cookie('saved_postform', passValue, exp);
  }
}


function checkCheckBox(f) {
  if (f.agree.checked == false ) {
    alert("Please check if the artist of this picture is on the DNP list and agree with the rules by clicking on the box above.");
    return false;
  } else {
    cookieForms('save');
    return true;
  }
}

function popUp(URL) { window.open(URL, '_blank' , 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=500,height=455,left = 262,top = 150'); }

function bpopUp(URL) { window.open(URL, '_blank' , 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1'); }
function bpopUp2(CAT,BANNER) { window.open('/bannerscripts/jump.cgi?f='+CAT+'&ref='+BANNER, '_blank' , 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1'); }

function qpopUp(URL) { window.open(URL, '_blank' , 'toolbar=1, scrollbars=1,  location=1, statusbar=1,menubar=1, resizable=1,width=730,height=280,left = 200,top = 200'); }

function report(url,pnum,ppar,board) {
  var type = prompt("Are you sure you wish report this post? If so, please enter below the number of the rule that was broken:\n0: Behavior: Insults, flaming, aggressiveness, stirring drama (including legal discussions on the image boards).\n1: Non-Furry / Illegal\n2: Wrong board\n3: Flood/spam\n4: DNP/Commercial material (if no signature is visible, please post the artist in the thread)\n5: Photomorphs (edits of photographs of real photos)\n\n8: None of the above, please enter a small description");
  
  if (type == "4") {
    var artist = prompt("Please enter the name of the artist:");
    document.getElementById('repcomment').value = artist;    
  }
  if (type == "8") {
    var artist = prompt("You want to report something that does not fit the standard categories, please enter a small description:");
    document.getElementById('repcomment').value = artist;    
  }

  if (((type > 0) && (type < 6)) || (type=="8") || (type=="0")){
    document.getElementById('frmreporturl').value = url;
    document.getElementById('pnum').value = pnum;
    document.getElementById('ppar').value = ppar;
    document.getElementById('type').value = type;
    document.getElementById('board').value = board;
    document.frmreport.submit();
  } else {
    alert("You must enter a number corresponding to the type of report you wish to enter and nothing else. Valid entry values: 0, 1, 2, 3, 4, 5, 8. If the report window does not show you the full list of report types, refer to the rules page and enter the number of the rule that you believe was violated.");
  }
}

function qcheckCheckBox(f) {
	if (f.agree.checked == false ) {
		alert("Please check if the artist of this picture is on the DNP list and agree with the rules by clicking on the box above.");
		return false;
	} else {
		cookieForms('save');
    document.getElementById('postform').submit();    
    window.opener.location.reload();
    //window.close();
		return true;
	}
}

function getXmlHttpRequest() {
    var httpRequest = null;
    try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) { httpRequest = null; }
    }
    if (!httpRequest && typeof XMLHttpRequest != "undefined") {
        httpRequest = new XMLHttpRequest();
    }
    return httpRequest;
}

function replaceDivContents(xmlHttpRequest, dstDivId) {
    var dstDiv = document.getElementById(dstDivId);
    dstDiv.innerHTML = xmlHttpRequest.responseText;
}

function execOnSuccess(stateChangeCallback) {
    return function(xmlHttpReq) {
        if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) stateChangeCallback(xmlHttpReq);
        // alert(xmlHttpReq + " " + xmlHttpReq.readyState + " " + xmlHttpReq.status);
    };
}

function postUrl(url, data, stateChangeCallback) {
    var xmlHttpReq = getXmlHttpRequest();
    if (!xmlHttpReq) return;
    
    if(!data) data = "$_=";
    
    xmlHttpReq.open("POST", url, true);
    xmlHttpReq.onreadystatechange = function() {
        stateChangeCallback(xmlHttpReq);
    };
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.send(data);
    // alert ('url: ' + url + '\ndata: ' + data);
}


function size_field(obj,rows) { obj.setAttribute("rows",rows); }