﻿

/* page nav */
var url,Currpage,re,Currentpage,iurl,FirstpageUrl,PrevpageUrl,NextpageUrl,LastpageUrl,pageCount,prevpage,nextpage,pageStart,pageEnd,i,ipage;
	url=""+document.location;
	if (url.indexOf("page=")==-1) {
		Currpage=1;
	}
	else {
		re=/(\S.*)(page=\d*)(\S.*|\S*)/g;
		Currentpage=url.replace(re,"$2");
		Currentpage=Currentpage.replace("page=","");
		//re=/(?:\S{1,}page=|\D.*)/g;
		//Currentpage=url.replace(re,"");
		url=url.replace("&page="+Currentpage,""); 
		url=url.replace("page="+Currentpage+"&","");
		url=url.replace("page="+Currentpage,"");
	}
	url+=(url.indexOf("?")==-1)?"?":"&";
	url=url.replace("?&","?");
	url=url.replace("&&","&");
	url=url.replace("###","");
	url=url.replace("##","");
	url=url.replace("#","");
function Fromatpage(str,StartNum,EndNum) {
	str+="";
	if (str.length>=1) {
		mynum=parseInt(str,10);
		if (isNaN(mynum)) {
			mynum=StartNum;
		}
		else {
			if (EndNum>-1) {
				mynum=(mynum<StartNum)?StartNum:mynum;
				mynum=(mynum>EndNum)?EndNum:mynum;
			}
			else {
				mynum=(mynum<StartNum)?StartNum:mynum;
			}
		}
	}
	else {
		mynum=StartNum;
	}
	return (mynum);
}

function Showopage(Tabstart,Tabend,CurrpageFont1,CurrpageFont2,pageCountFont1,pageCountFont2,PrepageFont1,PrepageFont2,RecCountFont1,RecCountFont2,FirstFont,PrevFont,NextFont,LastFont,Jump,pageNumFont1,pageNumFont2,pageNumFont3,pageNumFont4,LinkFont1,LinkFont2,LinkFont3,LinkFont4,iRecCount,iRecPerpage,ipageNum) {
	RecCount=Fromatpage(iRecCount,0,-1);
	RecPerpage=Fromatpage(iRecPerpage,1,-1);
	pageNum=Fromatpage(ipageNum,0,-1);
	pageCount=(RecCount%RecPerpage==0)?(RecCount/RecPerpage):(Fromatpage((RecCount/RecPerpage),0,RecCount)+1);
	Currpage=(pageCount>0)?(Fromatpage(Currentpage,1,pageCount)):(Fromatpage(Currentpage,0,pageCount));
	prevpage=Fromatpage((Currpage-1),1,pageCount);  
	nextpage=Fromatpage((Currpage+1),1,pageCount);    
	if (Currpage<=1&&pageCount<=1) {
		//FirstpageUrl="&nbsp;"+LinkFont3+FirstFont+LinkFont4+"&nbsp;";
		//PrevpageUrl="&nbsp;"+LinkFont3+PrevFont+LinkFont4+"&nbsp;";
		//NextpageUrl="&nbsp;"+LinkFont3+NextFont+LinkFont4+"&nbsp;";
		//LastpageUrl="&nbsp;"+LinkFont3+LastFont+LinkFont4+"&nbsp;";
		FirstpageUrl="";
		PrevpageUrl="";
		NextpageUrl="";
		LastpageUrl="";
	}
	else if (Currpage==1&&pageCount>1) {
		FirstpageUrl="&nbsp;"+LinkFont3+FirstFont+LinkFont4+"&nbsp;";
		PrevpageUrl="&nbsp;"+LinkFont3+PrevFont+LinkFont4+"&nbsp;";
		NextpageUrl="&nbsp;<a href=\""+url+"page="+nextpage+"\">"+LinkFont1+NextFont+LinkFont2+"</a>&nbsp;";
		LastpageUrl="&nbsp;<a href=\""+url+"page="+pageCount+"\">"+LinkFont1+LastFont+LinkFont2+"</a>&nbsp;";
	}
	else if (Currpage==pageCount) {
		FirstpageUrl="&nbsp;<a href=\""+url+"page=1\">"+LinkFont1+FirstFont+LinkFont2+"</a>&nbsp;";
		PrevpageUrl="&nbsp;<a href=\""+url+"page="+prevpage+"\">"+LinkFont1+PrevFont+LinkFont2+"</a>&nbsp;";
		NextpageUrl="&nbsp;"+LinkFont3+NextFont+LinkFont4+"&nbsp;";
		LastpageUrl="&nbsp;"+LinkFont3+LastFont+LinkFont4+"&nbsp;";
	}
	else {
	FirstpageUrl="&nbsp;<a href=\""+url+"page=1\">"+LinkFont1+FirstFont+LinkFont2+"</a>&nbsp;";
		PrevpageUrl="&nbsp;<a href=\""+url+"page="+prevpage+"\">"+LinkFont1+PrevFont+LinkFont2+"</a>&nbsp;";
		NextpageUrl="&nbsp;<a href=\""+url+"page="+nextpage+"\">"+LinkFont1+NextFont+LinkFont2+"</a>&nbsp;";
		LastpageUrl="&nbsp;<a href=\""+url+"page="+pageCount+"\">"+LinkFont1+LastFont+LinkFont2+"</a>&nbsp;";
	}
	pageStart=Fromatpage((Currpage-pageNum),1,pageCount);  
	pageEnd=Fromatpage((Currpage+pageNum),1,pageCount);  
	document.write (CurrpageFont1+Currpage+CurrpageFont2+pageCountFont1+pageCount+pageCountFont2+PrepageFont1);
	document.write (RecPerpage+PrepageFont2+RecCountFont1+RecCount+RecCountFont2+FirstpageUrl+PrevpageUrl);
	if (Currpage>=1) {
		for (i=pageStart;i<=pageEnd;i++) {
			if (i!=Currpage) {
				document.write ("&nbsp;<a href=\""+url+"page="+i+"\">"+pageNumFont1+i+pageNumFont2+"</a>&nbsp;");
			}
			else {
				document.write ("&nbsp;"+pageNumFont3+i+pageNumFont4+"&nbsp;");
			}
		}
	}
	document.write (NextpageUrl+LastpageUrl+Jump);
}

function getEID()
{
	var zb = document.getElementsByName("zb");
	var EventID;
	for(var i = 0; i < zb.length; i ++)
	{
		if (zb[i].checked)
		{
			EventID = zb[i].value;
		}
	}
	return EventID;
}


var eid = document.getElementById("eid").value;

//alert(eid);
var GetChaturl = "/zt/08meeting/chatroom/getChatData3.aspx?islive=1&EventID="+eid;
var GetChaturl3 = "/zt/08meeting/chatroom/getChatDataLy.aspx?islive=1&EventID="+eid;
//var GetChaturl2 = "/zt/08meeting/chatroom/new.aspx";
var SendChaturl = "/zt/08meeting/chatroom/sendChatData.aspx";
var lastID=-1; //initial value will be replaced by the latest known id
var lastID2=-1; //initial value will be replaced by the latest known id



var page=1; 

url2=""+document.location;
re2=/(\S.*)(page=\d*)(\S.*|\S*)/g;
url2=url2.replace(re2,"$2");
url2=url2.replace(/(page=)/g,"");   
//alert(url2);

if (url2==""+document.location) {
	page=1;
}else{
	page = url2;
}

//window.onload = initJavaScript;

function initJavaScript() {
	document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
	checkStatus(''); //sets the initial value and state of the input comment
	checkName(); //checks the initial value of the input name
	receiveChatText(); //initiates the first data query
	//receiveChatText2(); //initiates the first data query
	receiveChatText3();
}

//initiates the first data query
function receiveChatText() {
	if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
  	httpReceiveChat.open("GET",GetChaturl+'&lastID='+lastID+'&page='+page+'&rand='+Math.floor(Math.random() * 1000000), true);
    httpReceiveChat.onreadystatechange = handlehHttpReceiveChat; 
  	httpReceiveChat.send(null);
	}
}

/*function receiveChatText2() {
	if (httpReceiveChat2.readyState == 4 || httpReceiveChat2.readyState == 0) {
  	httpReceiveChat2.open("GET",GetChaturl2+'?lastID='+lastID2+'&rand='+Math.floor(Math.random() * 1000000), true);
    httpReceiveChat2.onreadystatechange = handlehHttpReceiveChat2; 
  	httpReceiveChat2.send(null);
	}
}*/

function receiveChatText3() {
	if (httpReceiveChat3.readyState == 4 || httpReceiveChat3.readyState == 0) {
  	httpReceiveChat3.open("GET",GetChaturl3+'&lastID='+lastID2+'&rand='+Math.floor(Math.random() * 1000000), true);
    httpReceiveChat3.onreadystatechange = handlehHttpReceiveChat3; 
  	httpReceiveChat3.send(null);
	}
}

function     deleteElement(id){   
		var     d     =     document.getElementById(id);   
		var     p   
		if     (p     =     d.parentNode)   
			{   
				p.removeChild(d);   
			}   
}   


//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat() {
  if (httpReceiveChat.readyState == 4) {

	//document.getElementById("loading2").style.display = "none";
	totalCmt = httpReceiveChat.responseText.split('==='); 
	document.getElementById("TotalPost").innerHTML = "共 <strong>"+ totalCmt[1] +"</strong> 条";

	deleteElement('outputList');
	oUl = document.createElement('ul');
		oUl.setAttribute('id','outputList');
	insert1 = document.getElementById("chatoutput");
	insert1.insertBefore(oUl, insert1.firstChild);
	//alert("del");




	results = httpReceiveChat.responseText.split('---'); //the fields are seperated by ---

	
	for(i=results.length-6;i>=0;i=i-5) { //goes through the result one message at a time
		insertNewContent(results[i],results[i+1],results[i+2],results[i+3],results[i+4]); //inserts the new content into the page
	}

    setTimeout('receiveChatText();',4000); //executes the next data query in 4 seconds
  }
}


/*function handlehHttpReceiveChat2() {
  if (httpReceiveChat2.readyState == 4) {
	//document.getElementById("loading3").style.display = "none";
	
	deleteElement('outputList3');
	oUl = document.createElement('div');
		oUl.setAttribute('id','outputList3');
	insert1 = document.getElementById("ask_cnt");
	insert1.insertBefore(oUl, insert1.firstChild);
	//alert("del");


	results = httpReceiveChat2.responseText; //the fields are seperated by ---
	insertNewContent2(results); //inserts the new content into the page		
    setTimeout('receiveChatText2();',6000); //executes the next data query in 4 seconds
  }
}*/

function handlehHttpReceiveChat3() {
  if (httpReceiveChat3.readyState == 4) {
	//document.getElementById("loading3").style.display = "none";
	
	deleteElement('outputList1');
	oUl = document.createElement('ul');
	oUl.setAttribute('id','outputList1');
	insert1 = document.getElementById("chatoutput1");
	insert1.insertBefore(oUl, insert1.firstChild);
	//alert("del");


	results = httpReceiveChat3.responseText; //the fields are seperated by ---
	insertNewContent3(results); //inserts the new content into the page		
    setTimeout('receiveChatText3();',6000); //executes the next data query in 4 seconds
  }
}

//inserts the new content into the page
function insertNewContent(id,liUrl,liName,liText,liTime) {
	
	var Element = {}; 
	Element.CreateNode = function(tag) { 
		return document.createElement(tag);
	} 
	Element.CreateHtmlNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.innerHTML = text; 
		return n; 
	} 
	Element.CreateTextNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.appendChild = document.createTextNode(text); 
		return n; 
	} 

	insertO = document.getElementById("outputList");
	oLi = document.createElement('li');
		oLi.setAttribute('class','clear');
		oLi.setAttribute('className','clear');//for IE's sake
		oLi.setAttribute('id','li_'+id);
	oSpan = document.createElement('h5');
		oSpan.setAttribute('className','name'); //for IE's sake
		oSpan.setAttribute('class','name');
		oSpan.setAttribute('id','uname');

	oTime = Element.CreateHtmlNode('span',liTime);
		oTime.setAttribute('id','m_time_'+id);
		oTime.setAttribute('class','c_time');
		oTime.setAttribute('className','c_time');//for IE's sake
		//oTime.setAttribute('href','javascript:SetReplyAuhor("[b]'+liName+'：[/b]' +liText+'")');



	liName = '<a href="http://bulo.hjenglish.com/'+ liUrl +'" target="_blank"><strong>'+ liName +'</strong></a>';
	
	oName = Element.CreateHtmlNode('span',liName+'： ');

	oText = Element.CreateHtmlNode('div',liText);
		oText.setAttribute('class','chat_item');
		oText.setAttribute('className','chat_item');
	
	oSpan.appendChild(oName);
	oLi.appendChild(oSpan);
	oLi.appendChild(oTime);
	oLi.appendChild(oText);
	insertO.insertBefore(oLi, insertO.firstChild);
}

/*function insertNewContent2(liText) {
	
	var Element = {}; 
	Element.CreateNode = function(tag) { 
		return document.createElement(tag);
	} 
	Element.CreateHtmlNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.innerHTML = text; 
		return n; 
	} 
	Element.CreateTextNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.appendChild = document.createTextNode(text); 
		return n; 
	} 


	insert1 = document.getElementById("outputList3");


	oText = Element.CreateHtmlNode('span',liText);
		oText.setAttribute('class','new_all');
		oText.setAttribute('className','new_all');
	
	insert1.insertBefore(oText, insert1.firstChild);

	
}*/
function insertNewContent3(liText) {
	
	var Element = {}; 
	Element.CreateNode = function(tag) { 
		return document.createElement(tag);
	} 
	Element.CreateHtmlNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.innerHTML = text; 
		return n; 
	} 
	Element.CreateTextNode = function(tag, text) {  
		var n = Element.CreateNode(tag); 
		if (text) n.appendChild = document.createTextNode(text); 
		return n; 
	} 


	insert1 = document.getElementById("outputList1");


	oText = Element.CreateHtmlNode('span',liText);
		oText.setAttribute('class','new_all');
		oText.setAttribute('className','new_all');
	
	insert1.insertBefore(oText, insert1.firstChild);

	
}


//stores a new comment on the server
function sendComment() {
	currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
	if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0)) {
		currentName = document.forms['chatForm'].elements['name'].value;
		currentID = document.forms['chatForm'].elements['id'].value;
		param = 'n='+ currentName+'&c='+ currentChatText+'&id='+ currentID;
		var u = SendChaturl + "?EventID="+getEID();
		httpSendChat.open("POST", u, true);
		httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	httpSendChat.onreadystatechange = handlehHttpSendChat;
  	httpSendChat.send(param);
  	document.forms['chatForm'].elements['chatbarText'].value = '';
	document.getElementById('chatbarText').focus();
	} else {
		setTimeout('sendComment();',1000);
	}
	//document.getElementById("msg_sucess").innerHTML = "<span style=color:red>留言成功！</span>";
	alert("发布成功！");
}

//deals with the servers' reply to sending a comment
function handlehHttpSendChat() {
  if (httpSendChat.readyState == 4) {
  	receiveChatText(); //refreshes the chat after a new comment has been added (this makes it more responsive)
  }
}


//does celver things to the input and submit
function checkStatus(focusState) {
	currentChatText = document.forms['chatForm'].elements['chatbarText'];
	oSubmit = document.forms['chatForm'].elements['submit'];
	if (currentChatText.value != '' || focusState == 'active') {
		oSubmit.disabled = false;
	} else {
		oSubmit.disabled = true;
	}
}

var _uid = 0;

function SetUID(id)
{
	var uid = 0;
	uid = id;
	_uid = id;

	if (uid != 0){
		document.forms['chatForm'].elements['id'].value = uid;
	}
	else{
		document.forms['chatForm'].elements['id'].value = '1376385';
	}

}

function SetUName(name)
{
	var uname = "";
	uname = name;
//alert(uname);

	currentName = document.forms['chatForm'].elements['name'];

	if (uname != ""){
		document.getElementById("logout_msg").style.display = "";
		document.getElementById("login_msg").style.display = "none";
		document.forms['chatForm'].elements['name'].value = uname;
		document.forms['chatForm'].elements['name'].disabled=true;
	}
	else{
		document.getElementById("logout_msg").style.display = "none";
		document.getElementById("login_msg").style.display = "";
		document.forms['chatForm'].elements['name'].value = uname;
		document.forms['chatForm'].elements['name'].disabled=false;
	}

}

//autoasigns a random name to a new user
function checkName() {
	currentName = document.forms['chatForm'].elements['name'];
	if (currentName.value == '') {
		currentName.value = '沪友'+ Math.floor(Math.random() * 10000);
	}
}



//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
//var httpReceiveChat2 = getHTTPObject();
var httpReceiveChat3 = getHTTPObject();
var httpSendChat = getHTTPObject();

//document.getElementById("loading2").style.display = "none";
