// JavaScript Document
//保留一个XMLHttpRequest请求
var xmlHttp;
xmlHttp = createXmlHttpRequestObject();

//创建一个XMLHttpRequest请求
function createXmlHttpRequestObject()
{ 
  //为XMLHttpRequest 对象存储索引
  var xmlHttp;
  //用于除了IE6以及更早版本以外的所有浏览器
  try
  {
	  xmlHttp = new XMLHttpRequest();
  }
	catch(e)
	{ 
	  //用于IE6及早期版本
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		
		//尝试每个程序直到其中一个能够运行
		for(var i=0;i<XmlHttpVersions.length && !xmlHttp;i++ )
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	//返回被创建的对象或显示一个出错信息
	if(!xmlHttp)
	  document.getElementById("divMessage").innerHTML = '无法创建XMLHttpRequest对象';
	else
	  return xmlHttp;
}

/*------------------
用户动作集合
------------------*/
//顶一下
function up_thread(tid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=up"+"&tid="+tid;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('up_thread()',500);	
	}
}

//主题评分
function score(tid,inhtml,score)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=score"+"&tid="+tid+"&score="+score;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('score()',500);	
	}
}

//主题收藏
function favourites(tid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=favourites"+"&tid="+tid;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('favourites()',500);	
	}
}

//回复点评
function post_score(pid,inhtml,score)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=post_score"+"&pid="+pid+"&score="+score;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('post_score()',500);	
	}
}

//添加好友
function add_friend(uid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=add_friend"+"&uid="+uid;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('add_friend()',500);	
	}
}

//采取答案
function takeanswer(tid,aid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=takeanswer"+"&tid="+tid+"&aid="+aid;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('takeanswer()',500);	
	}
}

//加入村子
function joingroup(gid,user,action,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=joingroup"+"&gid="+gid+"&action="+action;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('joingroup()',500);	
	}
}

//加入报价单
function addbaojia(btid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=baojia"+"&btid="+btid;
		xmlHttp.open("POST","../ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('addbaojia()',500);	
	}
}

//加入对比单
function addduibi(btid,inhtml)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		content="type=duibi"+"&btid="+btid;
		xmlHttp.open("POST","../ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){handleServerResponse(inhtml)};
		xmlHttp.send(content);
	}
	else
	{
		setTimeout('addduibi()',500);	
	}
}

//改变uc头部颜色 change uc's header color
function changecolor(uid,color)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		if(uid){
		content="uid="+uid+"&color="+color;
		xmlHttp.open("POST","ajax_post.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xmlHttp.onreadystatechange = function(){setcolo(color)};
		//xmlHttp.send(content);
        xmlHttp.send(content);
		}
	}
	else
	{
		setTimeout('changecolor()',500);	
	}
}



function setcolo(color){

var d=document.getElementById("topcolor");
var b=document.getElementById("bott");
var ur="url(themes/buy37/img/uc1/uc_topcolor_"+color+".jpg) no-repeat left -46px";
//alert(ur);
d.style.background=ur;
b.style.background="url(themes/buy37/img/uc1/uc_topcolor_"+color+"_1.jpg) repeat-x left -46px";

} 





//服务器响应函数
function handleServerResponse(inhtml)
{
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
	    xmlResponse = xmlHttp.responseXML;
	    xmlDocumentElement = xmlResponse.documentElement;
	    helloMessage = xmlDocumentElement.firstChild.data;



        /*if(helloMessage.indexOf("alert")==-1)
			{ 
			
				helloM=new Array();
			   helloM=helloMessage.split(",");
			   if (helloM.length>1)
			   {document.getElementById(showhtml).innerHTML = helloM[1];
                 document.getElementById(inhtml).innerHTML = helloM[0];
			   }
			   else
				document.getElementById(inhtml).innerHTML = helloM;
			}
			else
			{
			

				artmsg = helloMessage.replace("alert","");
				alert(artmsg);
			}*/

			if(helloMessage.indexOf("alert")==-1)
			{
				document.getElementById(inhtml).innerHTML = helloMessage;
			}
			else
			{
				artmsg = helloMessage.replace("alert","");
				alert(artmsg);
			}






		}
	}
}


