//function getCookie(name){
//	var cname = name + "=";
//	var dc = document.cookie;
//	if (dc.length > 0) {
//		begin = dc.indexOf(cname);
//		if (begin != -1) {
//			begin += cname.length;
//			end = dc.indexOf(";", begin);
//			if (end == -1) end = dc.length;
//			return unescape(dc.substring(begin, end));        }
//	}
//	return null;
//}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

var domain = location.href.substring(location.href.indexOf("//") + 2, location.href.length).substring(0, location.href.substring(location.href.indexOf("//") + 2, location.href.length).indexOf("/"));

theme = (getCookie("theme")) ? getCookie("theme") : "default";
if (theme == "default" || theme == "/cwmb/themes/big") {
	themeurl =  "/cwmb/default"
	document.write("<link rel=stylesheet type=text/css href=/cwmb/" + theme + ".css>")
} else {
	themeurl =  theme
	document.write("<link rel=stylesheet type=text/css href=" + theme + ".css>")
}
npost = "";

an=navigator.appName;
d=document;
srb="na";
srw="na";
s=screen;
srw=s.width;
an!="Netscape"?srb=s.colorDepth:srb=s.pixelDepth;
d.write("<img name=im src=http://u1.extreme-dm.com/i.gif height=1 width=1>");
d.write("<img src=\"http://u0.extreme-dm.com/0.gif?tag=csjoint&j=y&srw="+srw+"&srb="+srb+"&rs=41&l="+escape(d.referrer)+"\" height=1 width=1>");

function showAllCookies() {
	var i = 0;
	//window.open('cookies.php?' + document.cookie.toString(),'_blank');
	//alert("Number of cookies in your browser: " + document.cookie.length.toString() +  ".  The  delimited _list of cookies you are allowed  to access from this page is : " + document.cookie.toString());
	//deleteCookie("logged")
}
//document.write("<a href='javascript:showAllCookies()'>Show all cookies</a><br>")

function clearCookies() {
	deleteCookie("logged", "/cwmb/", domain)
	top.location = top.location
}

var logged = getCookie("logged")
var lasttime

if (!logged) {
	lasttime = (getCookie("time")) ? getCookie("time") : 0;
	setCookie("lasttime", lasttime)
	logged = 1
	setCookie("logged", 1)
}

var time = new Date();
time.setTime(time.getTime() + 365 * 24 * 60 * 60 * 1000);
var curtime = new Date();
setCookie("time", curtime, time);

var offset

function changeCSS(css) {
	setCookie("theme", css, time);
}

function setCookie(name, value, expires, path, domain, secure) {
  path="/cwmb/";
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
	if (name == "offset" && offset) {
		return offset
	}
	if (name == "lasttime" && lasttime) {
		return lasttime
	}
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(fixCookie(dc.substring(begin + prefix.length, end)));
}

function fixCookie(temp) {
	while (temp.indexOf("+")>-1) {
		pos= temp.indexOf("+");
		temp = "" + (temp.substring(0, pos) + " " +
		temp.substring((pos + 1), temp.length));
	}
	return temp;
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


function RomanNumeral(number) {
	if (!number) { number = 0; }
	var Romans = new Array()
	Romans[0]="";Romans[1]="I";Romans[2]="II";Romans[3]="III";Romans[4]="IV";Romans[5]="V";Romans[6]="VI"
	Romans[7]="VII";Romans[8]="VIII";Romans[9]="IX";Romans[10]="X";Romans[20]="XX";Romans[30]="XXX";Romans[40]="XL"
	Romans[49]="IL";Romans[50]="L";Romans[60]="LX";Romans[70]="LXX";Romans[80]="LXXX";Romans[90]="XC";
	Romans[99]="IC";Romans[100]="C";Romans[200]="CC";Romans[300]="CCC";Romans[400]="CD";Romans[490]="XD";
	Romans[499]="ID";Romans[500]="D";Romans[600]="DC";Romans[700]="DCC";Romans[800]="DCCC";Romans[900]="CM"
	Romans[990]="XM";Romans[999]="IM"
	var RomanNumber = ""

	number = parseInt(number)

	while (number != 0) {
		count = number.toString().length
		if (count >= 4) {
			RomanNumber = number.toString().substr(0,count - 3)
			RomanNumber = (RomanNumber=="1") ? "M" : RomanNumber + "M"
			number = parseFloat(number.toString().substr(count - 3))
		} else {
			if (Romans[number]) {
				RomanNumber += Romans[number]
				number = 0
			}
			temp = parseFloat(number.toString().substr(0,1)) * Math.pow(10,count - 1)
			RomanNumber += Romans[temp]
			number = number - temp
		}
	}

	return RomanNumber
}

var bgcolor = "1"

function adminThread(forum,thread,thread_status) {
	user_status = (getCookie("user_status")) ? getCookie("user_status") : "N";
	if (user_status == "A" || user_status == "S" || user_status == forum) {
		var statuses = new Array("", "Open", "Closed", "Sticky", "Keeper", "Deleted", "Closed/Sticky", "Closed/Keeper", "Sticky/Keeper", "Sticky/Keeper/Closed");
		var selected = new Array("", "", "", "", "", "", "", "", "", "");
		selected[thread_status] = " selected";
		options = "<select name=new_status onchange=\"document.status.submit();\">";
		for (var i = 1; i < statuses.length; i++) {
			options = options + "<option value=" + i + selected[i] + ">" + statuses[i] + "</option>";
		}
		
		adminReply = "";
		if (thread_status == 2 || thread_status == 5 || thread_status == 6 || thread_status == 7 || thread_status == 9) {
			adminReply = "<a href=/cwmb/post.php?threadid=" + thread + "&forumid=" + forum + "&action=post>Reply</a>";
		}
		
		document.write("<tr><form action=/cwmb/post.php name=status method=post>"
		+ "<input type=hidden name=action "
		+ "value=change_status><input type=hidden name=forumid value="
		+ forum + "><input type=hidden name=threadid value=" + thread
		+ "><td class=innertop colspan=2 align=right><input type=text name=name>"
		+ "<input type=password name=password> Thread Status: "
		+ options + "</select> " + adminReply + "</td></form></tr>");
		
		document.status.name.value = getCookie("yname");
		if (getCookie("ypassword")) document.status.password.value = getCookie("ypassword");
	}
}

function quickReply(forum,thread,thread_status) {
		if (thread_status == 2 || thread_status == 6 || thread_status == 7 || thread_status == 9) {
			user_status = (getCookie("user_status")) ? getCookie("user_status") : "N";
			if (user_status != "A" || user_status != "S" || user_status != forum) {
				return;
			}
		}

		document.write("<tr><form action=/cwmb/post.php method=post name=post><input "
		+ "type=hidden name=action "
		+ "value=makepost><input type=hidden name=forumid value=" + forum + "><input type=hidden "
		+ "name=threadid value=" + thread + "><td class=innertop>Name:<br><input "
		+ "type=text name=name><br>"
		+ "Password:<br><input type=password name=password></td><td class=innertop "
		+ "valign=top>Message:<br><textarea rows=3 cols=45 "
		+ "wrap=virtual name=posttext></textarea> "
		+ "<input type=submit value=submit accesskey=s></td></form></tr>");

		if (getCookie("yname")) document.post.name.value = getCookie("yname");
		if (getCookie("ypassword")) document.post.password.value = getCookie("ypassword");
}

function Post(postid,name,title,status,icon,count,date,message,forumid,threadid,editor,editdate,sig) {
	bgcolor = (bgcolor=="1") ? "2" : "1";

	if (icon == "") { icon = "/ubb/newbie.gif" }

	lasttime = (getCookie("lasttime")) ? new Date(getCookie("lasttime")) : 0;
	//offset = (getCookie("offset")) ? parseInt(getCookie("offset")) : 0;
	adate = adjustTime(date)
	//parseInt(date) - parseInt(offset)
	if (lasttime >= adate) {
		npost = "<br>Old"
		newanch = ""
	} else {
		if (npost == "<br>Old") {
			newanch = "<a name=new></a>"
		} else {
			newanch = ""
		}
			npost = "<br>New"
	}

	date = cwmDate(adjustTime(date))

	//var n = new Date();
	//n.setTime(n.getTime() + 365 * 24 * 60 * 60 * 1000);
	//date = Date(date.substr(0,3),date.substr(4,5),date.substr(6,7),date.substr(8,9),date.substr(10,11),date.substr(12,13))

	post = "postid=" + postid + "&threadid=" + threadid + "&forumid=" + forumid
	document.write("<tr><td class=post" + bgcolor + " valign=top><a name=" + postid
	+ "></a><table width=100% cellspacing=0><td width=8 class=posthead><img src=\"" + themeurl
	+ "/name1.gif\"></td><td background=\"" + themeurl
	+ "/name2.gif\" class=posthead>" + newanch + "<nobr><a class=username href=\"/cwmb/profile.php?user="
	+ name + "\">" + name
	+ "</a></nobr></td><td class=posthead><img src=\"" + themeurl
	+ "/name3.gif\"></td><td class=posthead width=100%>&nbsp;</td></table><table><td class=post><small class="
	+ status + ">"
	+ title + "<br><br><img src=\"" + icon + "\"><br><a href=" + threadid + ".php?gotopost=" + postid
	+ "#" + postid + ">" + RomanNumeral(count)
	+ "</a>" + npost + "</small></td></table></td>" + "<td valign=top class=post" + bgcolor
	+ "><table cellspacing=0 width=100%><td width=145 background=\"" + themeurl
	+ "/date.gif\" class=posthead><small>&nbsp; &nbsp; "
	+ date + "</small><br><img src=\"" + themeurl
	+ "/spacer.gif\" width=145 height=1></td><td class=posthead><a href=/cwmb/showip.php?" + post
	+ "><img src=\"" + themeurl + "/ip.gif\" border=0></a></td><td class=posthead><a href=/cwmb/post.php?action=edit&"
	+ post + "><img src=\"" + themeurl
	+ "/edit.gif\" border=0></a></td><td class=posthead><a href=/cwmb/post.php?action=post&quote="
	+ postid + "&threadid=" + threadid + "&forumid=" + forumid
	+ "><img src=\"" + themeurl
	+ "/quote.gif\" border=0></a></td><td class=posthead width=100%>&nbsp;</td></table>"
	+ "<table><td class=post>" + message + sig + "</td></table>")
}

//displayorder
function Main(forumname,level,forumid,moderators,forumdescription,lastdate,lastname,threadid,threadname,threads,posts,name) {
	if (level == "0") {
		document.write("<tr><td colspan=6 class=inner2>" + forumname + "</td></tr>")
	} else {
		if (lastdate) {
			//lastdate = new Date(lastdate)
			lasttime = (getCookie("lasttime")) ? new Date(getCookie("lasttime")) : 0;
			//offset = (getCookie("offset")) ? parseInt(getCookie("offset")) : 0;
			////lasttime = adjustTime(lasttime)
			////adate = adjustTime(lastdate)
			adate = adjustTime(lastdate) //lastdate - offset
			//parseInt(lastdate) - parseInt(offset)
			if (lasttime >= adate) {
				npost = "<img src=\"" + themeurl + "/old.gif\">"
			} else {
				npost = "<img src=\"" + themeurl + "/new.gif\">"
			}
			lastdate = cwmDate(adjustTime(lastdate))

			document.write("<tr><td class=inner2> " + npost + " </td><td class=inner1><a href=forum.php?forumid="
			+ forumid + ">" + forumname
			+ "</a><br><small>" + forumdescription + "</small></td><td class=inner2><small>" + threads
			+ "</small></td><td class=inner1><small>" + posts + "</small></td><td class=inner2><small>" + name
			+ ": <a href=/cwmb/threads/" + forumid + "/" + threadid + ".php>"	+ threadname + "</a><br>"
			+ lastname + " (" + lastdate	+ ")</small></td><td class=inner1>" + moderators + "</td></tr>")
		} else {
			document.write("<tr><td></td><td class=inner1><a href=forum.php?forumid=" + forumid + ">" + forumname
			+ "</a><br><small>" + forumdescription + "</small></td><td class=inner2><small>" + threads
			+ "</small></td><td class=inner1><small>" + posts + "</small></td><td class=inner2>"
			+ "&nbsp;</td><td class=inner1>" + moderators + "</td></tr>")
		}
	}
}

function Thread(threadid,forumid,lastdate,name,threadname,threadstatus,lastname,threadposts,threadposts,posted) {
	posts_per_page = (getCookie("posts_per_page")) ? parseFloat(getCookie("posts_per_page")) : 40;
	lasttime = (getCookie("lasttime")) ? new Date(getCookie("lasttime")) : 0;
	//offset = (getCookie("offset")) ? parseInt(getCookie("offset")) : 0;
	adate = adjustTime(lastdate)
	
	var statuses = new Array("", "", "closed_", "sticky_", "keeper_", "deleted_", "closedsticky_", "closedkeeper_", "stickykeeper_", "closedstickykeeper_");
	
	if (lasttime >= adate) {
		npost = "<img src=\"" + themeurl + "/" + statuses[threadstatus] + "old_folder.gif\" border=0>"
	} else {
		npost = "<img src=\"" + themeurl + "/" + statuses[threadstatus] + "new_folder.gif\" border=0>"
	}

	tposts = parseFloat(threadposts) + 1;
	multipage = '';
	tmaxpost = posts_per_page + 1;
	if (tposts >= tmaxpost) {
		pages = Math.ceil(tposts/posts_per_page);

		endr = "";

		if (pages > 9) {
			endr = 9;
		}

		finish = "";

		if (endr) {
			finish = "&nbsp; <a href=threads/" + forumid + "/" + threadid + ".php?page="
			+ pages + ">Last Page</a>";
		} else {
			endr = pages;
		}

		for (var i = 2; i <= endr; i = i + 1) {
			tpage = i + "";
			tpage = " <small><a href=threads/" + forumid + "/" + threadid + ".php?page="
			+ tpage + ">Page" + tpage + "</a>&nbsp; ";
			multipage = multipage + tpage;
		}
		multipage = multipage + finish + "</small>";
	}

	lastdate = cwmDate(adjustTime(lastdate))
	posted = cwmDate(adjustTime(posted))
	document.write("<tr><td class=inner2><a href=threads/" + forumid
	+ "/" +	threadid + ".php>" + npost + "</a></td><td class=inner1><a href=threads/" + forumid
	+ "/" +	threadid + ".php>" + threadname + "</a>" + multipage + "</td><td class=inner2>" + name
	+ "</td><td class=inner1>" + threadposts + "</td><td class=inner2><small>" + lastname + "<br><nobr>("
	+ lastdate + ")</nobr></small></td></tr>")
}

function writeCell() {
	args=writeCell.arguments;

	if (args[0] == "main") {
		cl = "innertop"
	} else if (args[0] == "header") {
		cl = "inner1"
	} else if (args[0] == "sub") {
		cl = "inner2"
	} else if (args[0] == "sub1") {
		cl = "inner1"
	} else if (args[0] == "sub2") {
		cl = "inner2"
	} else {
		cl = "inner1"
	}

	document.write("<tr>");
	//alert(args.length)
	for (i=1; i<=(args.length-2); i+=2) {
		cSpan = "";
		if (args[i]) {
			cSpan = " colspan=" + args[i]
		}
		document.write("<td class=" + cl + cSpan + ">" + args[i+1] + "</td>\n")
	}
	document.write("</tr>\n");
}

function cwmDate(date) {

	date = new Date(date)
	h = date.getHours()
		if (h == 0) {
			h = "12"
			ap = "AM"
		} else if (h < 12) {
			h = h
			ap = "AM"
		} else if (h == 12) {
			h = "12"
			ap = "PM"
		} else {
			h = h - 12
			ap = "PM"
		}
	var mname=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	date = mname[date.getMonth()]  + " " + date.getDate()  + ", " + h + ":" + date.getMinutes() + ":" + date.getSeconds() + " " + ap
	return date
}

function adjustTime(date) {
	date = new Date(date)
	offset = (getCookie("offset")) ? parseInt(getCookie("offset")) : 0;
	date = date - offset
	return date
}