function showpopup (ihtml, event, caption) {
	if (document.getElementById('mypopup').style.display == 'none') {
		document.getElementById("mypopup").style.left = event.clientX + 20 + "px";
		var scrolled = (window.pageYOffset == undefined) ? document.body.scrollTop : window.pageYOffset;
		document.getElementById("mypopup").style.top = event.clientY + scrolled + "px";
		if (caption == undefined) {caption = "Message";}
		document.getElementById("captionhere").innerHTML = caption;
		document.getElementById("texthere").innerHTML = ihtml;
		document.getElementById("mypopup").style.display = "block";
	}
}

function hidepopup() {
	document.getElementById('mypopup').style.display = 'none';
}

function getxmlhttp() {
	var xmlhttp = false;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		if (!xmlhttp) {
			xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlhttp;
}

function talkform(event) {
	var html = "<form name='frmtalk'><table cellpadding='3'>";
	html += "<tr><td style='font-weight: bold; color: red;' id='result'></td></tr>";
	html += "<input type='hidden' name='tid' value='' />";
	html += "<input type='hidden' name='mid' value='' />";
	html += "<tr><td><b>Subject:</b><br><input name='subj' size='50' maxlength='255' value=''></td></tr>";
	html += "<tr><td><b>Message:<br><textarea name='mess' cols='80' rows='10'></textarea></td></tr>";
	html += "<tr><td align='center'><input onclick='with (document.frmtalk) {talksend(subj.value, mess.value, document.frmtalk);}' type='button' value='Preview &raquo;' id='smt' style='font-weight: bold;'></td></tr></table></form>";
	showpopup(html,event,"New Topic");

}

function talksend(subj,mess,frm,tid) {
	frm.smt.disabled = 1;
	frm.smt.style.cursor = "wait";
	if (tid == undefined) {
		frm.subj.style.cursor = "wait";
	}
	frm.mess.style.cursor = "wait";
	document.body.style.cursor = "wait";
	xmlhttp = getxmlhttp();
	if (xmlhttp) {
		xmlhttp.open("POST", "xmlhttp.php", false);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		if (tid == undefined) {
			params = "subj=" + subj + "&mess=" + mess;
		} else {
			params = "&mess=" + mess + "&rtid=" + tid;
		}
		xmlhttp.setRequestHeader("Content-length", 2);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
		var res = xmlhttp.responseText;
		document.getElementById("result").style.backgroundColor = "yellow";
		if (res.substr(0, 4) == "<img") {
			document.getElementById("result").style.color = "red";
			document.getElementById("result").innerHTML = res;
		} else {
			document.location.href = "talk.php?tid=" + res;
			window.scrollTo(0, document.body.scrollHeight);
		}
    } else {
        alert("Your browser does not support this feature");
    }
	frm.smt.disabled = 0;
	frm.smt.style.cursor = "default";
	if (tid == undefined) {
		frm.subj.style.cursor = "default";
	}
	frm.mess.style.cursor = "default";
	document.body.style.cursor = "default";
}

function iseverythingok(answer,tid,event) {
	if (answer == "Yes") {
		xmlhttp = getxmlhttp();
		xmlhttp.open("GET", "xmlhttp.php?tid=" + tid, false);
		xmlhttp.send(null);
		var resp = xmlhttp.responseText;
		showpopup(resp, event, "New Topic");
		setTimeout("document.location.href = 'talk.php'", 5000);
	}
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

function editform(td,event) {
	html = "<form style='margin: 0;'>";
	html += "<input type='hidden' name='dataid' value='" + td.getAttribute("data-id") + "'>";
	html += "<input size='5' name='datasid' value='" + td.getAttribute("data-sid") + "'>";
	html += "<input type='hidden' name='wk' value='" + td.getAttribute("data-wk") + "'>";
	html += "<select name='lwpos'><option value='0'>---";
	for (var pos = 1; pos <= td.getAttribute("data-maxpos"); pos++) {
		html += "<option value='" + pos + "'>" + pos;
	}
	html += "</select>";
	html += "<input type='hidden' name='type' value='" + td.getAttribute("data-type") + "'>";
	html += "<input type='submit' value='&raquo;'></form>";
	showpopup(html,event);
}
