function iresize(image) {
	var iheight=image.height;
	var iwidth=image.width;
	if (iwidth>700) { image.width=700; }
	else if (iheight>400) { image.height=400; }
}
function iopen(image) {
	var iname=image.src;
	window.open(iname,'_blank');
}
function bbstyle(bbn) {
	var textobj=document.getElementById("bodyinput");
	if (textobj) {
		var bbcodes=["[b]","[/b]", "[i]","[/i]", "[u]","[/u]", "[quote]","[/quote]", "[code]","[/code]", "[list]","[/list]", "[list=]","[/list]", "[img]","[/img]", "[url]","[/url]"];
		wrapSelection(textobj,bbcodes[bbn],bbcodes[bbn+1]);
	}
}
function mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
	txtarea.selectionStart = selEnd + lft.length + rgt.length;
	txtarea.selectionEnd = selEnd + lft.length;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;
}
function IEWrap(lft, rgt) {
	strSelection = document.selection.createRange().text;
	if (strSelection!="") {
		document.selection.createRange().text = lft + strSelection + rgt;
	}
}
function wrapSelection(txtarea, lft, rgt) {
	if (document.all) {IEWrap(lft, rgt);}
	else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
}
function wrapSelectionWithLink(txtarea) {
	var my_link = prompt("Enter URL:","http://");
	if (my_link != null) {
		lft="<a href=\"" + my_link + "\">";
		rgt="</a>";
		wrapSelection(txtarea, lft, rgt);
	}
	return;
}
function updateScore() {
	if (httpsend.readyState==4 && httpsend.status==200) {
		out=parsexml(httpsend.responseXML);
		if (out.error=="0") {
			var rating=out.rating-128;
			var msgid=out.msgid;
			var pos=0;
			var neg=0;
			var gid=document.getElementById("g"+msgid);
			var rid=document.getElementById("r"+msgid);
			if (rating>0) { pos=rating; } else { neg=Math.abs(rating); }
			if (gid) { gid.style.width=pos+"px"; }
			if (rid) { rid.style.width=neg+"px"; }
			if (out.score==1) {
				document.images["ratepos"+msgid].src=document.images["ratepos"+msgid].getAttribute("srcon");
				document.images["rateneu"+msgid].src=document.images["rateneu"+msgid].getAttribute("srcoff");
				document.images["rateneg"+msgid].src=document.images["rateneg"+msgid].getAttribute("srcoff");
			} else if (out.score==-1) {
				document.images["ratepos"+msgid].src=document.images["ratepos"+msgid].getAttribute("srcoff");
				document.images["rateneu"+msgid].src=document.images["rateneu"+msgid].getAttribute("srcoff");
				document.images["rateneg"+msgid].src=document.images["rateneg"+msgid].getAttribute("srcon");
			} else if (out.score==0) {
				document.images["ratepos"+msgid].src=document.images["ratepos"+msgid].getAttribute("srcoff");
				document.images["rateneu"+msgid].src=document.images["rateneu"+msgid].getAttribute("srcon");
				document.images["rateneg"+msgid].src=document.images["rateneg"+msgid].getAttribute("srcoff");
			}
		} else if (out.error=="12") {
			alert("You can not rate your own message");
		}
	}
	return true;
}
function sendHttpRequest(url) {
	httpsend=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	if(httpsend) {
		httpsend.open("GET", url);
		httpsend.onreadystatechange=updateScore;
		httpsend.send(null);
	}
}
function rateThis(msgid,score) {
	var url="../../../ratemsg/"+msgid+"/&amp;score="+score;
	sendHttpRequest(url);
	return true;
}
function parsexml(xml) {
	var out=new Object();
	out.msg="";
	out.rating="0";
	out.error="-1";
	out.msgid="0";
	out.score="0";
	var xmldoc=xml.getElementsByTagName("ratemsg");
	if (xmldoc && xmldoc.length>0) {
		out.msg=xmlget(xmldoc[0],"message",out.msg);
		out.rating=xmlget(xmldoc[0],"rating",out.rating);
		out.msgid=xmlget(xmldoc[0],"msgid",out.msgid);
		out.error=xmlget(xmldoc[0],"error",out.error);
		out.score=xmlget(xmldoc[0],"score",out.score);
	}
	return out;
}
function xmlget(xmldoc,xmlname,xmldefault) {
	out=xmldefault;
	msg=xmldoc.getElementsByTagName(xmlname);
	if (msg.length>0 && msg[0].childNodes.length>0) {
		out=msg[0].firstChild.nodeValue;
	}
	return out;
}
