var b=0,i=0,u=0,q=0,s=0,cm=0,cc=0,contactlist;
var tu="";
var tn="";
var ti="";
var cur_cm="cm";
var cur_sm="smb";
var post_p="";
var post_s="";
var post_n="";
var userid=0;
var login=0;
var sm_start=0;
var smiliesb=0;
var smilies_l=0;
var sm_pages=0;
var admin=0;
var edit_nr=0;
var ori_edit="";
var form_width=0;
var sendmsgto=1;
var contactlist_i;

function fl(){
login=getcookie('login');
if(!login)
{
	login=getcookie('ssllogin');
}

if(document.getElementById("rf"))
{
createform(document.getElementById("rf"));
window.onresize=resize;

if(login)
{
	var end=login.indexOf('+');
	userid=login.substring(0,end);
	admin = isadmin();
	userlinks();
	mr();
}
}
if(window!=top)top.location.href=location.href;
}
window.onload=fl;

function isadmin()
{
	if(userid==1 || userid==2)
	{
		return 1;
	}
	return 0;
}

function userlinks(){
if (!document.getElementsByTagName) return;

var url=("http://www.team-fonzie.net/user/"+userid);
var urls=("https://www.team-fonzie.net/user/"+userid);

var an=document.getElementsByTagName("a");
for (var i=0;i<an.length;i++){
var ar=an[i];
var th=ar.href;

if(th==url || th==urls || (admin && th.substring(0,32)=="http://www.team-fonzie.net/user/") || (admin && th.substring(0,33)=="https://www.team-fonzie.net/user/")){
if(ar.id!="")
{
	var nr=ar.id.substring(1,10);
	if(admin)
	{
		document.getElementById("p"+nr).innerHTML+="<div style=\"margin:0;border:1px dashed #333;background:#eed;text-align:right\"><a style=\"margin-right:40px\" href=\"/member/edit/"+nr+"\" onClick=\"return edit("+nr+")\">edit</a> <a style=\"margin-right:40px\" href=\"/member/delete/"+nr+"\">delete</a></div>";
	}
	else
	{
	document.getElementById("p"+nr).innerHTML+="<div style=\"margin:0;border:1px dashed #333;background:#eed;text-align:right\"><a style=\"margin-right:40px\" href=\"/member/edit/"+nr+"\">edit</a> <a style=\"margin-right:40px\" href=\"/member/delete/"+nr+"\">delete</a></div>";
	}
}
}
}
}

function edit(nr)
{
	if(edit_nr)
	{
		alert("Already editing a message");
		return false;
	}

	edit_nr = nr;
	ori_edit = document.getElementById("p"+nr).innerHTML;

	document.getElementById("p"+nr).innerHTML ="<div><p style=\"background:url(http://download.team-fonzie.net/~hautis/fonzie/loading.gif) top left no-repeat;height:30px;padding:10px 0 0 50px\">Loading...</p></div><div class=\"p\"><p><br/><br/><br/><br/></p></div>";

	document.getElementById("p"+nr).innerHTML+="<div style=\"margin:0;border:1px dashed #333;background:#eed;text-align:right\"> &nbsp; </div>";
 
	f_loadXMLDoc("/quickedit/fetch/"+nr);
	return false;
}

function loadurl(url)
{
	var req;
        if (window.XMLHttpRequest)
        {
                req = new XMLHttpRequest();
                req.open("GET", url, true);
                req.send(null);
        }
        else if (window.ActiveXObject)
        {
                isIE = true;
                req = new ActiveXObject("Microsoft.XMLHTTP");
                if (req)
                {
                        req.open("GET", url, true);
                        req.send();
                }
        }
}                

function mr(){
if(cm || cc){return;}
var tmp=location.href.split("/");
var tmpc=tmp[4].split("#");
tmp[4]=tmpc[0];
if(tmp.length>5)
{
	var tmpb=tmp[5].split("#");
	tmpc=tmpb[0];
}
else
{
	tmpc=0;
}
if(tmp[3]=="" || tmp[4]=="")
{
	return;
}
loadurl("/mr?a="+tmp[3]+"&b="+tmp[4]+"&c="+tmpc);
}


function createform(f_object, f_text){
var tmp="";
var login=getcookie('login');

var aft_id = "";
if(f_object != document.getElementById("rf"))
{
	aft_id = "2";
	cur_cm="cm2";
	cur_sm="smb2";
}
/*
else
{
	aft_id = "";
	cur_cm="cm";
	cur_sm="smb";
}
*/

var width=document.body.clientWidth;
if(width==0){width=document.documentElement.clientWidth;}
var content_width=Math.floor((width*0.9)-30);
form_width=Math.floor(content_width-60-80);
var form_addcss="";
if(cc || cm){form_width=Math.floor(form_width*0.7)-70;form_addcss=" style=\"margin-left:0\"";}
else{form_width=Math.floor(form_width*0.7);}

tmp+="<form id=\"cm"+aft_id+"\" onsubmit=\"return vf('"+cur_cm+"')\" action=\"/post"+aft_id+"\" method=\"post\">";
tmp+="<div id=\"cf"+aft_id+"\""+form_addcss+">";

if(cc)
{
	tmp+=create_contactlist();
}

if(cm)
{
        tmp+=create_topiclist();
        tmp+="<label><input class=\"tp\" name=\"title\" type=\"text\" maxlength=\"64\" alt=\"title\"/>&nbsp;Title</label>";
        tmp+="<input name=\"newtopic\" value=\"yes\" type=\"hidden\"/>";
}else{
	if(!login)
	{
        	tmp+="<label><input class=\"tp\" name=\"name\" type=\"text\" maxlength=\"64\" alt=\"name\"/>&nbsp;Name</label>";
		tmp+="<label><input class=\"tp\" name=\"website\" type=\"text\" maxlength=\"60\" alt=\"website\"/>&nbsp;Website</label>";
	}
	if(post_s)
	{
		tmp+="<input name=\"s\" value=\""+post_s+"\" type=\"hidden\"/>";//news_sub_id
	}
	if(f_object != document.getElementById("rf"))
	{
		tmp+="<input name=\"edit_nr\" value=\""+edit_nr+"\" type=\"hidden\"/>";
	}
	tmp+="<input name=\"p\" value=\""+post_p+"\" type=\"hidden\"/>";
	tmp+="<input name=\"n\" value=\""+post_n+"\" type=\"hidden\"/>";//page
}

tmp+=ib();

tmp+="<div id=\"ps"+aft_id+"\" class=\"ps\" style=\"width:"+(form_width+4)+"px\"><textarea id=\"ps_post"+aft_id+"\" name=\"post\" rows=\"10\" cols=\"40\" style=\"width:"+form_width+"px\">";

if(f_text!=undefined)
{
	tmp+=f_text;
}
tmp+="</textarea></div>";

tmp+=ss();

tmp+="<input class=\"sb\" type=\"submit\" value=\"";
if(cm){
	tmp+="Create this topic";
}else if(cc){
	tmp+="Send this message";
}else{
	if(f_object != document.getElementById("rf"))
	{
		tmp+="Edit this comment";
	}
	else
	{
		tmp+="Post this comment";
	}
}
tmp+="\">";

if(aft_id)
{
        tmp+="<input value=\"Cancel\" class=\"sb\" type=\"button\" style=\"clear:none;margin-left:20px\" onClick=\"edit_cancel()\">";
}

tmp+="<div id=\""+cur_sm+"\"></div>";

tmp+="</div>";
tmp+="</form>";

f_object.innerHTML=tmp;
if(cm)
{
	ct();
}
}

function resize()
{
var width=document.body.clientWidth;
if(width==0){width=document.documentElement.clientWidth;}
var content_width=Math.floor((width*0.9)-30);
form_width=Math.floor(content_width-60-80);
if(cc || cm){form_width=Math.floor(form_width*0.7)-70;}
else{form_width=Math.floor(form_width*0.7);}
document.getElementById("ps").style.width=(form_width+4)+"px";
document.getElementById("ps_post").style.width=(form_width)+"px";
//if(document.getElementById("edit_area"))
//{
//	document.getElementById("edit_area").style.width=(form_width+4)+"px";
//}
}

function create_contactlist()
{
var tmp="<label><select style=\"margin-bottom:5px\" name=\"s\" alt=\"to\">";
var l=contactlist.length;
for(var i=0;i<l;i++)
{
	tmp+="<option value=\""+contactlist_i[i]+"\"";
	if(contactlist_i[i]==sendmsgto)
	{
		tmp+=" selected=\"selected\"";
	}
	tmp+=">send to "+contactlist[i]+"</option>";
}
tmp+="</select></label>";
return tmp;
}


function create_topiclist()
{
tmp="<label>";
tmp+="<select name=\"p\" id=\"first\" alt=\"select a topic type\" onchange=\"ct()\">";
tmp+="<option value=\"topic\">Forum post</option>";
tmp+="<option value=\"blog\">Blog entry</option>";
if(userid==1 || userid==2)
{
tmp+="<option value=\"news\">News post</option>";
}
tmp+="</select>";
tmp+="</label>";
tmp+="<label>";
tmp+="<select name=\"s\" id=\"second\" alt=\"select a topic type\">";
tmp+="<option value=\"\">no subtopic available</option>";
tmp+="</select>";
tmp+="</label>";
return tmp;
}

function ct()
{
var box = document.getElementById("first");
var number = box.options[box.selectedIndex].value;
if(number=="topic"){
document.getElementById("second").options.length = 0;
document.getElementById("second").options[0]=new Option('Fonzie - General','3');
document.getElementById("second").options[1]=new Option('Fonzie - News','11');
document.getElementById("second").options[2]=new Option('Fonzie - School','1');
document.getElementById("second").options[3]=new Option('Fonzie - Fun','20');
document.getElementById("second").options[4]=new Option('Fonzie - Movies & TV','6');
document.getElementById("second").options[5]=new Option('Fonzie - IRC','25');
document.getElementById("second").options[6]=new Option('Fonzie - Fonzie forum','7');
document.getElementById("second").options[7]=new Option('Projects - Machinima','26');
document.getElementById("second").options[8]=new Option('Projects - GotRank','27');
document.getElementById("second").options[9]=new Option('Games - General','2');
document.getElementById("second").options[10]=new Option('Games - Counter-Strike','21');
document.getElementById("second").options[11]=new Option('Games - Private Forum','23');
}else{
document.getElementById("second").options.length = 0;
document.getElementById("second").options[0]=new Option('no subtopic available','');
}
}


function sm(v,smile)
{
smb(cur_sm);
document.forms[v].post.value+=smile+" ";
document.forms[v].post.focus();
}

function smq(v,smile)
{
document.forms[v].post.value+=smile+" ";
document.forms[v].post.focus();
}

function vf(this_cm)
{
s=document.forms[this_cm].post.value.length;
if(s<2){
alert("Please insert a comment before posting.");
return false;
}
if(cm){
s=document.forms[this_cm].title.value.length;
if(s<2){
alert("Please enter a title before posting.");
return false;}
}

document.forms[this_cm].submit.disabled=true;
return true;
}

function ibb(this_cm) {
var ta;
if(b==0){
ta="[B]";
document.forms[this_cm].b.value=" B*";
b=1;
} else {
ta="[/B]";
document.forms[this_cm].b.value=" B ";
b=0;
}
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ibu(this_cm) {
var ta;
if(u==0){
ta="[U]";
document.forms[this_cm].u.value=" U*";
u=1;
} else {
ta="[/U]";
document.forms[this_cm].u.value=" U ";
u=0;
}
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ibi(this_cm) {
var ta;
if(i==0){
ta="[I]";
document.forms[this_cm].i.value=" I*";
i=1;
} else {
ta="[/I]";
document.forms[this_cm].i.value=" I ";
i=0;
}
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ibh(this_cm){
var url=prompt(tu, "http://");
var urt=prompt(tn, "My Webpage");
var ta="[URL="+url+"]"+urt+"[/URL]";
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ibq(this_cm){
var ta;
if(q==0){
ta="[QUOTE]";
document.forms[this_cm].q.value=" Quote*";
q=1;
}else{
ta="[/QUOTE]";
document.forms[this_cm].q.value=" Quote ";
q=0;
}
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ibg(this_cm) {
var url=prompt(ti, "http://");
var ta="[IMG]"+url+"[/IMG]";
document.forms[this_cm].post.value+=ta;
document.forms[this_cm].post.focus();
}

function ss(){
var tmp="<div class=\"sm\"><p>Smilies:</p><br/>";
var smilies=new Array(
":)*smile",
":(*sad",
":D*biggrin",
":p*tounge",
";)*wink",
"???*oh",
":O:*wow",
":angry:*mad",
":cool:*cool",
":laugh:*laugh"
//":blues:*blues"
);

var ar=new Array();
for(var i=0;i<smilies.length;i++){
ar=stringsplit(smilies[i]);
tmp+="<a href=\"javascript:smq('"+cur_cm+"','"+ar[0]+"')\"><img src=\"http://download.team-fonzie.net/~hautis/fonzie/smilies/"+ar[1]+".gif\" alt=\""+ar[1]+"\"/></a> ";
if((i%2)==1){tmp+="<br/>";}
}
if(cur_sm=="smb")
{
	tmp+="<a href=\"javascript:smb(\'"+cur_sm+"\')\">More</a>";
}
tmp+="</div>";
return tmp;
}


function smb(this_sm){
var a="";

if(s==1){
s=0;
document.getElementById(this_sm).style.display="none";
}else{
s=1;
document.getElementById(this_sm).style.display="inline";
document.getElementById(this_sm).style.border="1px dashed #333";
document.getElementById(this_sm).style.margin="20px 0";
document.getElementById(this_sm).style.background="#f4f4e4";
if(cm!=1 && cc!=1){document.getElementById("pg").style.display="none";}
//a="<br/><a href=\"javascript:smb(\'"+this_sm+"\')\">hide smilies</a><br/><br/>";

if(!smiliesb)
{
smiliesb=new Array("angel2","laugh","icon_rendeer","fok_hahaha","RTFM3","icon_cat","frust","icon_cherry","devil","reddisgust","tongue","icon_joker","beer_smiley","kungfu","fok_loveit","azzangel","icon_geek","pinkie","redcap","division","blush","UNCLESAM","yellow","sauer","wiggle","smilygifs436","fok_grin","red","fok_puh","\@","fok_drool","fok_bye","upside","chainsaw","confused","fok_pukey","cwmddd","eek2","eek7","icon_cheese","icon_cheers","icon_rr","party","icon_porc","thefinger","tounge","pain10","argue","stupid","fok_eerie","sd3","fok_lovers","impactwhore","messer","Ghost_anim","puke","punkdude","sad","fok_devil","frown","Oo","drunken_smilie","biggrin2","fok_yes","eek","down","dancing","female","dohslap","fok_blush","plasma","spock","ZZZsleep","punk","britain","fok_yawnee","icon_jokercolor","rock2","icon_cool","icon_salut","pissed","sex","suspect","overlord","blackey","blah","headshot","fart2","camper","ukliam2","shy","ukliam3","Wow","fart","sleep","grinning","madani","menacegrin","rocketwhore","twofingers","icon_sad","fok_frown","beerbang","lol","shaft","SHOCKED","rocket","ghostface","bluegrab","sleepy","icon_farao","cool2","king","male","Hydrogen","biggrin","rock","popblood","sl-undecided","beerfriends","love2","slotje","evilbunny","rail","fork_off","mgwhore","fok_sleep","deal","holy","angel","kiss","anger","icon_smurfin","shakin","fok_wink","icon_spiderman","sconf","xyxthumbs","cool","smash","wavey","Flip_anim","humping","icon_santa","_sure","bat_angel","fok_heart","crazy3","wallbash","ride","sound","bounce","fok_yummie","kane34","luxhello","ripper","milk","alien","smile","icon_flower","icon_thumright","wow2","toilet","egypt1","wave","scared","icon_bounce","Peace!","jigga","icon_thumleft","enforcer","oh","aeh","po","fok_shutup","icon_albino","silly","rukker","ghost","color_","fok_thumbsup","headscratch","ices_blume","icon_smurf","instagib","smokin","oops","bluecap","gun","fok_smile","007","qrocket","fok_bonk","spidereekA","fok_sadcheer","plasmawhore","bluescreen","fok_snif","icon_shaking2","thrasher","fok_shiny","evilimu","fok_worshippy","icon_study","ignore","fok_michel","11ztongue","fok_hypocrite","unclesam","lokai","rocket2","rocket3","biggrinjester","slant","ar15","fok_confused","roller","icon_bigsmurf","wazzup","fok_cheer","grenade","blues","bom","fok_nooo","thumb","fok_puh2","bash","fok_nosmile","megarofl","prost","icon_profileleft","icon_pirat","poorpet","fok_frusty","love","fok_clown","mfight","fok_wedding","wow","glare","icon_compress","icon_shaking","scatter","gauntlet","spammer","sun_smiley","wink","mad");
}
smilies_l=smiliesb.length;
sm_pages=Math.ceil(smilies_l/25);

document.getElementById(this_sm).style.border="1px dashed #333";
document.getElementById(this_sm).style.margin="20px 0";
document.getElementById(this_sm).style.background="#f4f4e4";

sm_page(this_sm,sm_start);
}
}

function sm_page(this_sm,newstart)
{
sm_start=newstart;
var a="";
for(var i=0;i<sm_pages;i++)
{
a+="<a href=\"javascript:sm_page('"+this_sm+"',"+(i*25)+")\">["+(i+1)+"]</a>";
}
a+="<br/>";
for(i=sm_start;(i<(sm_start+25) && i<smilies_l);i++){
a+="<a href=\"javascript:sm('"+cur_cm+"','["+smiliesb[i]+"]')\"><img src=\"http://download.team-fonzie.net/~hautis/fonzie/smilies/"+smiliesb[i]+".gif\" alt=\""+smiliesb[i]+"\"/></a>";
}
a+="</div>";
document.getElementById(this_sm).innerHTML=a;
}


function ib(){
var tmp="";
tmp+="<div class=\"ib\">";
tmp+="<input type=\"button\" accesskey=\"b\" onclick=\"ibb('"+cur_cm+"')\" name=\"b\" value=\"B\"/> ";
tmp+="<input type=\"button\" accesskey=\"i\" onclick=\"ibi('"+cur_cm+"')\" name=\"i\" value=\"I\"/> ";
tmp+="<input type=\"button\" accesskey=\"u\" onclick=\"ibu('"+cur_cm+"')\" name=\"u\" value=\"U\"/> ";
tmp+="<input type=\"button\" accesskey=\"h\" onclick=\"ibh('"+cur_cm+"')\" value=\" http:// \"/>";
tmp+="<input type=\"button\" accesskey=\"q\" onclick=\"ibq('"+cur_cm+"')\" name=\"q\" value=\" Quote \"/>";
tmp+="<input type=\"button\" accesskey=\"p\" onclick=\"ibg('"+cur_cm+"')\" value=\" Image \"/>";
tmp+="</div>";
return tmp;
}

function getcookie(cookiename) {
var cookiestring=""+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1)return 0;
var index2=cookiestring.indexOf(';',index1);
if (index2==-1) index2=cookiestring.length;
return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function stringsplit(string)
{ 
var ar=new Array();
var i=0; 
var start=0;
while(start>=0 && start<string.length){
var end=string.indexOf('*',start);
if(end >= 0){ 
ar[i++]=string.substring(start,end);
start=end+1; 
}else{ 
ar[i++]=string.substring(start,string.length); 
start=-1;
}
} 
return ar;
}

/* New xml request model */

// fetch xml doc
var rq;

function f_loadXMLDoc(url)
{
	if (window.XMLHttpRequest)
	{
		rq = new XMLHttpRequest();
		rq.onreadystatechange = f_processReqChange;
		rq.open("GET", url, true);
		rq.send(null);
	}
	else if (window.ActiveXObject)
	{
		rq = new ActiveXObject("Microsoft.XMLHTTP");
		if (rq)
		{
			rq.onreadystatechange = f_processReqChange;
			rq.open("GET", url, true);
			rq.send();
		}
	}
}

// on xml doc loaded
function f_processReqChange()
{
	if (rq.readyState == 4)
	{
		if (rq.status == 200)
		{
			if(rq.responseText.charAt(0)=='s')
			{
				// show fetch success
				//document.getElementById("p"+edit_nr).innerHTML = rq.responseText.substring(1);
				
				//document.getElementById("p"+edit_nr).innerHTML ="<form method=\"post\" action=\"/quickedit/update/"+edit_nr+"\"><div><p style=\"height:30px;padding:10px 0 0 50px\">Editing mode</p></div><div class=\"p\"><p><textarea id=\"edit_area\" style=\"width:"+(form_width+4)+"px;height:200px\">"+rq.responseText.substring(1)+"</textarea></p></div>";
				//document.getElementById("p"+edit_nr).innerHTML+="<div style=\"margin:0;border:1px dashed #333;background:#eed;text-align:right\"><input name=\"cancel\" value=\"Cancel\" type=\"button\" style=\"margin:4px 0\" onClick=\"edit_cancel()\"><input style=\"margin:4px 10px\" name=\"submit\" value=\"Submit\" type=\"submit\"></div></form>";
				document.getElementById("p"+edit_nr).className = "none";
				createform(document.getElementById("p"+edit_nr), rq.responseText.substring(1));
			}
			//else if(rq.responseText.charAt(0)=='u')
			//{
				//show update success
			//}			
			else
			{
				// show error
				edit_cancel();
				alert(rq.responseText.substring(1));
				
			}
		}
		else
		{
			edit_cancel();
			// couldn't retrieve xml
		}
	}
}

function edit_cancel()
{
	document.getElementById("p"+edit_nr).innerHTML = ori_edit;
	document.getElementById("p"+edit_nr).className = "";
	edit_nr=0;
}
