var popWin = 0;

function popWindow(e,url,w,h,x,y,scr) {
	window.status = '';
	hidePop();
	popWin = window.open(url,"","width=" + w + ",height=" + h + ",screenX=" + x + ",left=" + x + ",screenY=" + y + ",top=" + y + ",scrollbars=" + scr);
}

function aniWindow(url,w,h,x,y) { // not close
	window.status = '';
	hidePop();
	popWin1 = window.open(url,"","width=" + w + ",height=" + h + ",screenX=" + x + ",left=" + x + ",screenY=" + y + ",top=" + y);
}
function openWindow(url,h,w) {
	window.status = '';
	hidePop();
	popWin =  window.open("", "","width=" + w + ",height=" + h + ",screenX=60,left=60,screenY=60,top=60");
	popWin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');
	popWin.document.writeln('<img src="'+ url + '" border=0>');
	popWin.document.writeln('</body>');
}
function hidePop(){
	if(popWin != 0 && !popWin.closed) {
		popWin.close();
	}
}
function icon(i,e){
	window.status = i;
	if (document[e].MESSAGE.value=='Bài viết tiếng Việt không bỏ dấu sẽ bị xóa') {
		document[e].MESSAGE.value = '';
	}
	document[e].MESSAGE.value = document[e].MESSAGE.value + " " + i + " ";
	document[e].MESSAGE.focus();
}
function gotourl(e){
	window.opener.location.href = e;
	window.opener.focus();
}
function cleartext(e,val) {
	if (e.value == val) e.value = '';
}
function selectPop() {
	e = document.ckmail.POP.options[document.ckmail.POP.selectedIndex].value;
	document.ckmail.POPSERVER.value = e;
}
function jump(list) {
	location.href = list.options[list.selectedIndex].value;
}
function goTo1(list) {
	location.href = '?title='+list.options[list.selectedIndex].value.split(' ')[0]+'&page='+list.options[list.selectedIndex].value.split(' ')[1];
}
function addzero(e){
	if (e < 10) e = "0" + e;
	return e;
}
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
today = new Date();
day = days[today.getDay()];
mo = addzero(today.getMonth() + 1);
date = addzero(today.getDate());
year = today.getYear();
if(navigator.appName=="Netscape") {
year = year + 1900;
}
today = "";
today += day + " ";
today += mo + "-";
today += date + "-";
today += year;

var ie=document.all;
var ns=document.layers;
var ns6=document.getElementById&&!document.all;

function typechange(n) {
	if (n == 2) {
		document.images['a1'].src = "../images/1.gif";
		document.images['a2'].src = "../images/2.gif";
		document.images['a3'].src = "../images/3.gif";
		document.images['a4'].src = "../images/4.gif";
		document.images['a5'].src = "../images/5.gif";
		document.images['a6'].src = "../images/6.gif";
		document.images['a7'].src = "../images/7.gif";
		document.images['a8'].src = "../images/8.gif";
		document.images['a9'].src = "../images/9.gif";
	}
	else if (n == 1) {
		document.images['a1'].src = "../images/a1a.gif";
		document.images['a2'].src = "../images/a2a.gif";
		document.images['a3'].src = "../images/a3a.gif";
		document.images['a4'].src = "../images/a4a.gif";
		document.images['a5'].src = "../images/a5a.gif";
		document.images['a6'].src = "../images/a7a.gif";
		document.images['a7'].src = "../images/oa.gif";
		document.images['a8'].src = "../images/a6a.gif";
		document.images['a9'].src = "../images/dd.gif";
	}
}

function closeBox(box) {
	if (document.getElementById(box))document.getElementById(box).style.visibility = "hidden";
	if (box=='phbox') document.getElementById('cont').style.visibility = "hidden";
}

function openBox(box) {
	document.getElementById(box).style.visibility = "visible";
}

function changebg(bg,cl) {
	document.getElementById(bg).style.background = cl;
}

var time;
var ch;
function timeout() {
	time = setTimeout("popdown()",500);
}

function printEbook() {
	calcPo('pr');

}
function popup(n,im,t) {
	popdown()
	closeBox('signin')
	ch = n;
	if (ch==1) imgChange(im,'../images/tttab.gif');
	calcPo(im);
	document.getElementById(t).style.visibility = "visible";
	document.getElementById(t).style.left = dx;
	document.getElementById(t).style.top = dy;
	clearTimeout(time);
}

function popdown() {
	if (ch==1) imgChange('tt','../images/tttab1.gif');
	ch = 0;
	if (document.getElementById('tab')) document.getElementById('tab').style.visibility = "hidden";
	if (document.getElementById('pr'))document.getElementById('pr').style.visibility = "hidden";
	document.getElementById('ma').style.visibility = "hidden";
}

function cursoron() {
clearTimeout(time)
}

function signbox(e) {
	if (document.getElementById('signin'))document.getElementById('signin').style.visibility = e
	if (e == "visible") {
		document.signin.MEMBERID.select();
		closeBox('msbox')
	}
}

var dx = 0;
var dy = 0;

function calcPo(im){
	var img = document.images[im];
	if(document.layers) {
		dx = (img.x) + 1;
		dy = (img.y) + 14;
	}
	else {
		dx = getX(img) + 1;
		dy = getY(img) + 14;
	}
}

function getX(obj){
	return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );
}

function getY(obj){
	return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );
}

var banner = new Array(4);

banner[0]= "<img src=http://vietmessenger.com/banners/vmbanner.gif border=0 height=60 width=468 alt='Chat & Email in Vietnamese'>";
banner[1]= "<img src=http://vietmessenger.com/banners/vmbanner.gif border=0 height=60 width=468 alt='Chat & Email in Vietnamese'>";
banner[2]= "<img src=http://vietmessenger.com/banners/vmbanner.gif border=0 height=60 width=468 alt='Chat & Email in Vietnamese'>";
banner[3]= "<img src=http://vietmessenger.com/banners/vmbanner.gif border=0 height=60 width=468 alt='Chat & Email in Vietnamese'>";

function showBanner() {
r_num = (Math.round((Math.random()*3)))
if (document.layers) {
document.write(banner[r_num]);
}
else {
document.getElementById("banner").innerHTML =  banner[r_num];
setTimeout("showBanner()",20000)
}
}

function showAds(n) {
}

var xmlhttp;

var tloader='<img src="../images/tloading.gif" border="0">';

function getThumbs() {
document.getElementById("pthumbs").innerHTML=tloader;
xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="http://vietmessenger.com/cgi-bin/getthumbs.cgi";
	url=url+"?sid="+Math.random();
	xmlhttp.onreadystatechange=genThumbs;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function genThumbs()
{
if (xmlhttp.readyState==4)
{
document.getElementById("pthumbs").innerHTML=xmlhttp.responseText;
}
}

function getDomainRoot()
{
var uri = location.href;
var startIndex;
if (null != uri.match("file:///") )
startIndex = 8;
if (null != uri.match("http://") )
startIndex = 7;
if (null != uri.match("https://") )
startIndex = 8;
var endIndex = uri.indexOf("/", startIndex);
var domainRoot = uri.substring(0, endIndex);
return domainRoot;
}
var loader='<table border="0" cellpadding="0" cellspacing="0" bgcolor="#EFEFED"><tr valign="middle"><td width="4" bgcolor="#AAACAA">&nbsp;</td><td height="340" width="416" align="center"><img src="../images/loading.gif" border="0"></td></tr></table>';
var q;
var wi;
var he;
var box_height;

var dsoctop=0
var box_heigh 
var top_y=82
var bottom_y=52
var left_x=100

function showMember(cm,id,lmemid,lpass,mode){
	var domainRoot=getDomainRoot()
	
	closeBox('msbox')
	signbox('hidden')
	q=cm
	document.getElementById("abox").style.width = 436;
	document.getElementById("bbox").style.width = 436;
	document.getElementById("cbox").style.width = 420;
	document.getElementById("dbox").style.width = 420;
	document.getElementById("vi").innerHTML='';
	document.getElementById("meminfo").innerHTML=loader;
	document.getElementById("icons").innerHTML='';
	
	setBox(436)
	
	document.getElementById("phbox").style.visibility="visible";
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var url="http://vietmessenger.com/getmember/";
	url=url+"?"+cm+"="+id+"&lmemid="+lmemid+"&lpass="+lpass+"&mode="+mode; 
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); 
	xmlhttp.send(null);
}

function stateChanged() {
	if (xmlhttp.readyState==4){
		wi = xmlhttp.responseText.split("||")[4]
		he = xmlhttp.responseText.split("||")[5]
		if (wi !="") {
			wi=Number(wi)
			he=Number(he)
			if (wi>400) {
				document.getElementById("abox").style.width = wi+36;
				document.getElementById("bbox").style.width = wi+36;
				document.getElementById("cbox").style.width = wi+20;
				document.getElementById("dbox").style.width = wi+20;
				setBox(wi+36,he)
			}
		}
		document.getElementById("vi").innerHTML=xmlhttp.responseText.split("||")[0];
		document.getElementById("meminfo").innerHTML=xmlhttp.responseText.split("||")[1];
		document.getElementById("icons").innerHTML=xmlhttp.responseText.split("||")[2];
		document.getElementById("sendmess").innerHTML=xmlhttp.responseText.split("||")[3];
		if (q =='login' && xmlhttp.responseText.split("||")[6] != "") {
			var dom = 'vietmessenger.com';
			document.cookie = 'ADMINID='+ xmlhttp.responseText.split("||")[6]+';path=/;domain='+dom;
			document.cookie = 'STAT='+ xmlhttp.responseText.split("||")[7]+';path=/;domain='+dom;
			document.cookie = 'MEMBERID='+ xmlhttp.responseText.split("||")[6]+';path=/;domain='+dom;
			document.cookie = 'ONLINE='+ xmlhttp.responseText.split("||")[6]+';path=/;domain='+dom;
			document.cookie = 'GENDER='+ xmlhttp.responseText.split("||")[8]+';path=/;domain='+dom;
			document.cookie = 'AGE='+ xmlhttp.responseText.split("||")[9]+';path=/;domain='+dom;
			document.cookie = 'COUNTRY='+ xmlhttp.responseText.split("||")[10]+';path=/;domain='+dom;
			if (document.getElementById("io")) {
				document.getElementById("io").innerHTML='<a href="http://vietmessenger.com/cgi-bin/signin.cgi?action=signout" onmouseout="imgChange(\'im2\',\'images/tab_signout.gif\');" onmouseover="imgChange(\'im2\',\'images/tab_signout1.gif\');"><img name="im2" src="images/tab_signout.gif" alt="sign out" title="sign out" border="0" width="73" height="16"></a>'
			}
		}
	}
}

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){ // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function setBox(w,h) {
	var page_height = getPageSizeWithScroll()[1];
	//alert('page_height: '+page_height)
	var msbox_obj = document.getElementById("phbox")
	dsoctop = getScrollY()
	//alert('dsoctop: '+dsoctop)
	var py = parseInt(dsoctop)+top_y
	//alert('py: '+py)
	//box_height = parseInt(document.getElementById("phbox").clientHeight)+bottom_y
	//alert('box_height: '+box_height)
	if (h && py>(page_height-h-top_y)) {
		py = page_height-h-top_y
	}
	msbox_obj.style.top = py
	msbox_obj.style.left=document.body.offsetWidth/2-(w/2);
	
	if (document.getElementById("iframe")) {
		document.getElementById("iframe").style.width=document.getElementById("phbox").clientWidth
		document.getElementById("iframe").style.height=document.getElementById("phbox").clientHeight
	}
	document.getElementById("cont").style.height = page_height
	document.getElementById("cont").style.width = document.body.offsetWidth
	document.getElementById("cont").style.visibility = 'visible'
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}



function dragbox(e){
	box_width = parseInt(crossobj.clientWidth)
	var page_width = 1002;
	var px =parseInt(crossobj.style.left.replace("px",""))
	
	dsoctop = getScrollY()
	box_height = parseInt(crossobj.clientHeight)+bottom_y 
	var page_height = getPageSizeWithScroll()[1];
	var py =parseInt(crossobj.style.top.replace("px",""))
	var box_top=0
	var box_left=0
	
	if (ie&&dragit){
		box_left=tempx+event.clientX-offsetx
		box_top=tempy+event.clientY-offsety
		
		if (px>=left_x && px<=(page_width-box_width)) {
			if (box_left<left_x) box_left=left_x
			else if (box_left>(page_width-box_width)) box_left=page_width-box_width
			crossobj.style.left=box_left
		}
		
		if (py>=top_y && py<=(page_height-box_height)) {
			if (box_top<top_y) box_top=top_y
			else if (box_top>(page_height-box_height)) box_top=page_height-box_height
			crossobj.style.top=box_top
		}
		return false
	}
	
	else if (ns6&dragit){
		box_left=tempx+e.clientX-offsetx
		box_top=tempy+e.clientY-offsety
		if (px>=left_x && px<=(page_width-box_width)) {
			if (box_left<left_x) box_left=left_x
			else if (box_left>(page_width-box_width)) box_left=page_width-box_width
			box_left += "px"
			crossobj.style.left=box_left
		}
		
		if (py>=top_y && py<=(page_height-box_height)) {
			if (box_top<top_y) box_top=top_y
			else if (box_top>(page_height-box_height)) box_top=page_height-box_height
			box_top += "px"
			crossobj.style.top=box_top
		}
		return false
	}
}

function initializedrag(e,id){
	crossobj= document.getElementById("phbox")
	var firedobj=ns6? e.target : event.srcElement
	var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body"
	while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!=id){
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement
	}

	if (firedobj.id==id){
		offsetx=ie? event.clientX : e.clientX
		offsety=ie? event.clientY : e.clientY
		
		tempx=parseInt(crossobj.style.left)
		tempy=parseInt(crossobj.style.top)
		
		dragit=true
		//document.onmousemove=dragbox // disable drag
	}
	//alert(tempx)
	//alert(tempy)
}
document.onmouseup=new Function("dragit=false")


function Validate8(e) {
	if (idpass(e)) return true;
	else return false;
	return true;
}

function Validate4(e) {
	if (Blank(document[e].MESSAGE.value)) {
		document[e].MESSAGE.focus();
		document[e].MESSAGE.select();
		alert("Message field is empty.");
		return false;
	}
	if (document[e].MESSAGE.value.length > 1024) {
		document[e].MESSAGE.focus();
		document[e].MESSAGE.select();
		alert("Message is limited to 1024 characters.");
		return false;
	}
	return true;
}

function idpass(e) {
	if (Blank(document[e].MEMBERID.value)) {
		document[e].MEMBERID.focus();
		document[e].MEMBERID.select();
		alert("Type in Member ID");
		return false;
	}
	if (Blank(document[e].PASSWORD.value)) {
		document[e].PASSWORD.focus();
		document[e].PASSWORD.select();
		alert("Type in Password");
		return false;
	}
	return true;
}

function rateit(rating,source) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	var url="http://vietmessenger.com/cgi-bin/rateit.cgi";
	url=url+"?rating="+rating+"&source="+source; 
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged1;
	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); 
	xmlhttp.send(null);

		//rating = (rating * 25) - 8;
		//document.getElementById('current-rating').style.width = rating+'px';
}

function stateChanged1() {
	if (xmlhttp.readyState==4){
		rating = Math.round((xmlhttp.responseText.split("|")[1]/xmlhttp.responseText.split("|")[0]) * 16)
		document.getElementById('current-rating').style.width = rating+'px';
		document.getElementById("rating").innerHTML =  'thank you for voting';
	}
	for (i=1;i<=7;i++) {
		document.getElementById('star'+i).style.visibility = 'hidden';
	}
}

rien = " \t\n\r";
function Nothing(s){  
	return ((s == null) || (s.length == 0))
}
function Blank(s) {
	var i;
    if (Nothing(s)) return true;
    for (i = 0; i < s.length; i++){
	var c = s.charAt(i);
	if (rien.indexOf(c) == -1) 
	return false;
    }
    return true;
}

function getin(e) {
	if (Validate8(e)) {
		showMember('login',document[e].vmem.value,document[e].MEMBERID.value,document[e].PASSWORD.value,document[e].mode.value)
	}
}

function sendmess(e) {
	if (Validate4(e)) {
		showMember('sendmess',document[e].vmem.value,escape(document[e].MESSAGE.value))
	}
}

function sendReview(t,m,c){
	var url="http://vietmessenger.com/bookreview/";
	url=url+"?title="+t+"&message="+m;  
	url=url+"&sid="+Math.random();
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	xmlhttp.onreadystatechange=bookReview;
	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); 
	xmlhttp.send(null);
}

function bookReview() {
	if (xmlhttp.readyState==4){
		var mess = xmlhttp.responseText
		var pby = document.vmboard.PBY.value
		var postby = document.vmboard.POSTBY.value
		var pda = document.vmboard.PDA.value
		var avatar = ''
		if (document.vmboard.AVATAR.value !='') {
			avatar = "<a href=\"javascript:showMember('member','"+postby+"');\"><img src=\"http://vietmessenger.com/avatars/mb/"+document.vmboard.AVATAR.value+"\" border=\"0\" class=\"avatar\" alt=\""+pby+"\" title=\""+pby+"\"></a>"
		}
		
		var review = '<table cellpadding="4" cellspacing="0" border="0" width="800" class="font">'
		review += "<tr valign=\"top\"><td valign=\"top\" width=\"110\" align=\"center\"  style=\"padding-top:8px\">"+avatar+"<br /><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"68\" class=\"font\"><tr><td align=\"center\"><a href=\"javascript:showMember('member','"+postby+"');\" class=\'mem\'>"+pby+"</a></td></tr></table></td><td>"
		review += '<table cellpadding="0" cellspacing="0" border="0">'
		review += '<tr><td valign="top"rowspan="3"><img height="17" src="../images/rvpoint.gif" width="16" vspace="23"></td><td><img height="5" src="../images/rvtl.gif" width="5"></td><td width="600" background="../images/rvt.gif"></td><td><img height="5" src="../images/rvtr.gif" width="5"></td></tr>'
		review += '<tr valign="top"><td background="../images/rvl.gif" width="5" height="84"></td><td width="640" bgcolor="#FFFFFF" class="bfont" style="padding:8px"><font class="font"><i>'+pda+'</i></font><br /><br />'+mess+'</td><td background="../images/rvr.gif" width="5" height="32"></td></tr>'
		review += '<tr><td><img height="5" src="../images/rvbl.gif" width="5"></td><td width="600" background="../images/rvb.gif"></td><td><img height="5" src="../images/rvbr.gif" width="5"></td></tr>'
		review += '</table>'
		review += '</td></tr>'
		review += '</table>'
		document.getElementById("reviews").innerHTML=review+document.getElementById("reviews").innerHTML
		//document.vmboard.MESSAGE.value=''
		location.href = '#vcomment'
	}
}


