var jweb="www.wheyonline.net";
var jowner="";
var jsch = querySt('sch');
var xmlHTTP;
var txtmsg,url;

function rq_post(jowner){
	showMSGPopup('div_w');
	var objdoc = document.getElementById? document.getElementById('owner'):document.all.owner;
	objdoc.value = jowner;
}

function rq_okedit(){
	rq_editweb();
	rq_view('webboard');
}

function cls_web(){	
	rq_view('webboard');
	var jdiv = "div_editweb";
	var objdiv = document.getElementById? document.getElementById(jdiv):document.all.jdiv;
	objdiv.innerHTML = "";
}

function rq_imgadmin(jtxt){
	var objimgall = document.getElementById? document.getElementById('imgadmin'):document.all.imgall;
	objimgall.style.display = jtxt;
}

function rq_editweb(){
	txtmsg = "div_editweb";
	url = "http://"+jweb+"/webboard/editwebboardadmin.asp?id=";
	ajax(txtmsg,url,'');
}

function rq_pakmood(jmsg,jid,jpage,jroom){
	var url="http://"+jweb+"/webboard/pakmood.asp?id="+jid;
	ajax('',url,'');
	rq_view(jmsg,jpage,jsch,jroom);
}

function rq_del(jmsg,jid,jpage,jroom){
	var url="http://"+jweb+"/webboard/del"+jmsg+".asp?id="+jid;
	ajax('',url,'');
	rq_view(jmsg,jpage,jsch,jroom);
}

function rq_logout(){
	var txtmsg = "div_spanlogin";
	var url="http://"+jweb+"/webboard/logout.asp?id=";
	ajax(txtmsg,url,'');
}

function rq_postwb(jmsg){
	objwb = document.getElementById(jmsg);
	showMSGPopup(jmsg);
}

function rq_imgall(){
	var objimgall = document.getElementById('imgall');
	if(objimgall.style.display != "none"){
		objimgall.style.display = "none";
	}
}

function rq_waitgb(jdiv){
	document.getElementById(jdiv).style.display = "";
}

function rq_edit(pdiv,jmsg,jid,jpage){
	document.getElementById('imgall').style.display = "";
	showMSGPopup('div_'+pdiv);
	var txtmsg = "divmemo"+jmsg;
	var url="http://"+jweb+"/webboard/edit"+jmsg+".asp?msg="+jmsg+"&id="+jid+"&page="+jpage;
	ajax(txtmsg,url,'');
}

function rq_memo(jmsg,jsch,jroom){
	document.getElementById('imgall').style.display = "";
	var txtmsg = "divmemo"+jmsg;
	var url="http://"+jweb+"/webboard/memo"+jmsg+".asp?own="+jroom+"&sch="+jsch;
	ajax(txtmsg,url,'');
}

function rq_search(jtext,jroom){
	document.getElementById('imgsch').style.display = "";
	var txtmsg = "divview_"+jtext;
	var jsch = formsearch.menu1[formsearch.menu1.selectedIndex].text;
	var jtxt = formsearch.textsearch.value;
	var url="http://"+jweb+"/webboard/view"+jtext+".asp?room="+jroom;
	var postpack="own="+jowner+"&sch="+jsch+"&txt="+jtxt;
	ajax(txtmsg,url,'POST',postpack);
	document.getElementById('imgsch').style.display = "none";
}

function rq_view(jtext,jpage,jsch,jroom){
	var txtmsg = "divview_"+jtext;
	var url="http://"+jweb+"/webboard/view"+jtext+".asp?own="+jowner+"&page="+jpage+"&sch="+jsch+"&room="+jroom;
	ajax(txtmsg,url,'');
}

function rq_okwb(jmsg,jdiv,jimg,jpage,jroom){
	rq_memo(jmsg,jsch,jroom);
	rq_view(jmsg,jpage,jsch,jroom);
	document.getElementById(jimg).style.display = "none";
	showMSGPopup(jdiv);
	rq_imgall();
}

function createXMLHttpRequest(){
	try{
		xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			xmlHTTP = false;
		}
	}

	if(!xmlHTTP && document.createElement){
		xmlHTTP = new XMLHttpRequest();
	}

	return xmlHTTP;
}

function ajax(txtmsg,url,txtMT,pac){
	var objdoc = document.getElementById? document.getElementById(txtmsg):document.all.txtmsg;
	var today = new Date();
	today.setTime( today.getTime() );
	url = url+"&today="+today;
	xmlHTTP = createXMLHttpRequest();
	if(txtMT == 'POST'){
		xmlHTTP.open("POST", url, false);
		xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHTTP.send(pac);
	}else{
		xmlHTTP.open("GET", url, false);
		xmlHTTP.send('');
	}	
	if(txtmsg != ''){
		objdoc.innerHTML = xmlHTTP.responseText;
	}
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
		return ft[1];
		}
	}
}

// ฟังก์ชันเขียนค่า คุกกี้
function saveCookie(name,text){
// กำหนดวันหมดอายุของคุกกี้
var ExpDate = new Date();
ExpDate.setTime (ExpDate.getTime() + (24 * 60 * 60 * 1000 * 365)); 

// บันทึกคุกกี้
document.cookie = name + "=" +text+ "; expires=" + ExpDate.toGMTString() + "; path=/";
}

// ฟังก์ชันอ่านค่า คุกกี้
function getCookie(name){
var YourCookie = document.cookie; 
var cname = name + "=";
var clen = YourCookie.length;
var cbegin = 0;

while(cbegin < clen){
var vbegin = cbegin + cname.length;
    if (YourCookie.substring(cbegin, vbegin) == cname) { 
    var vend = YourCookie.indexOf (";", vbegin);
    if (vend == -1) vend = clen;
    return unescape(YourCookie.substring(vbegin, vend));
    }
cbegin = YourCookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}

// ฟังก์ชันลบ คุกกี้
function delCookie(name){
var expireNow = new Date();
document.cookie = name + "=" +"; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";

ReaYourCookie (); //ปรับปรุงค่าในฟอร์มใหม่
}

//msgPopup
iens6=document.all||document.getElementById
var thename;
var objPopup;
var objWidth;
var thetext;
var winHeight;
var winPositionFromTop;
var winScrollTop;
var winWidth;
var winTop;
var winBottom;
var winNow;
var startH = 0;
var step = 5;
var time = 10;
var openTimer;

function showMSGPopup(text,theswitch,inwidth,inheight){
	thename = text;
	objPopup = document.getElementById? document.getElementById(thename):document.all.thename;
	objWidth = parseInt(objPopup.style.width);
	winHeight = parseInt(objPopup.style.height);
	if(iens6&&document.all) {
		winPositionFromTop=document.body.clientHeight;
		winWidth=(document.body.clientWidth-document.body.leftMargin);
		winScrollTop=document.body.scrollTop;
	}
	if(iens6&&!document.all) {
		winPositionFromTop=window.innerHeight;
		winWidth=(window.innerWidth-(document.body.offsetLeft+30));
		winScrollTop=window.pageYOffset;
	}
	if(theswitch=="override") {
		winWidth=inwidth;
		winHeight=inheight;
	}
	var toshow = objPopup.style.display;
	if(toshow == "none"){
		startH = 0;
		objPopup.style.display = "";
		objPopup.style.top = winPositionFromTop+winScrollTop+"px";
		objPopup.style.left = ((winWidth/2)-(objWidth/2)+50)+"px";
		setTimeout("slide(true)", time);
	}else{
		winTop = parseInt(objPopup.style.top)+winHeight;
		setTimeout("slide(false)", time);
	}
}

function slide(isUp){
	if(isUp) {
		if(startH >= winHeight){
			return;
		}
	objPopup.style.top = (winScrollTop+winPositionFromTop)-startH+"px";
	startH+=step;
	}else if(!isUp){
		winNow = parseInt(objPopup.style.top);
		if(iens6&&document.all){
			winBottom = document.body.scrollTop+document.body.clientHeight;
		}
		if(iens6&&!document.all){
			winBottom = window.pageYOffset+window.innerHeight;
		}
		if(winNow >= winBottom){
			objPopup.style.top = winScrollTop+winPositionFromTop+"px";
			objPopup.style.display = "none";
			rq_imgall();
			return;
		}
	objPopup.style.top = winTop-startH+"px";
	startH-=step;
	}
	setTimeout("slide("+isUp+")", time);
}

//drag
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};