var xmlhttp;
var showuserstate=true
document.getElementById("etwchatcontent").onclick=function(){showuserstate=false}
function showalluserstate()
{
	showuserstate=true
	document.getElementById("etwchatmessage").innerHTML=document.getElementById("hiddenalluser").innerHTML
	document.getElementById("etwchatuserstate").innerHTML="<b>Please choose one of above users and click to talk with</b>"
	}
showalluserstate()
function reinfo(ob,user)
{
document.getElementById("userid").value=ob;
document.getElementById("etwchatmessage").style.cssText="font-weight:bold; color:green;padding:0px!important;padding:5px;";
document.getElementById("etwchatmessage").innerHTML="Chat with "+user;
document.getElementById("etwchatusername").value=user;
document.getElementById("etwchatemail").style.cssText="background:#efefef;";
document.getElementById("etwchatemail").value="Enter Your E-mail Address";
}
function noinfo(ob)
{
if(document.getElementById("etwchatemail").value=="" || document.getElementById("etwchatemail").value=="Enter Your E-mail Address")
{
ob.value="";
}
}
function checkemail()
{
	var emailre=/^[a-zA-Z0-9]*[-_\.]?[a-zA-Z0-9]*@[a-zA-Z0-9]*[-]?[a-zA-Z0-9]*\.[a-zA-Z0-9]{2,}$/
	if(!(emailre.test(document.getElementById("etwchatemail").value)))
	{
		document.getElementById("etwchatemail").style.cssText="border:1px solid #ff0000;";
		document.getElementById("etwchatemail").focus();
		document.getElementById("etwchatemail").value="Invalid email address:"+document.getElementById("etwchatemail").value;
		}
		else
		{
			document.getElementById("etwchatemail").style.cssText="border:1px solid #00ff00; color:green;";
			}
	}
function createxmlhttp()
{
	if(window.ActiveXObject)
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
	xmlhttp=new XMLHttpRequest();
	}
}
function postdata()
{
	var userid=document.getElementById("userid").value;
	var etwchatcontent=document.getElementById("etwchatcontent").value;
	var etwchatuser=document.getElementById("etwchatusername").value;
	var etwchatemail=document.getElementById("etwchatemail").value;
	if(etwchatemail=="Enter Your E-mail Address" || etwchatemail=="")
	{
		alert("Email is empty");
		document.getElementById("etwchatemail").value="Enter Your E-mail Address";
		document.getElementById("etwchatemail").focus();
		return false;
		}
		if(etwchatcontent=="")
		{
			alert("Message is empty");
			return false;
			}
	if(top.location.href.indexOf("admin")>1)
	{
		var url="../postdata.php";
		var etwchataddress="";
		if(etwchatuser=="")
		{
			alert("Please click email address,first");
			return false;
			}
			if(etwchatcontent=="")
			{
				alert("Enter message");
				return false;
				}
		}
		else
		{
			var url="postdata.php";
			var etwchataddress=document.getElementById("etwchataddress").value;			
			}
	var sendquery="userid="+userid+"&etwchataddress="+etwchataddress+"&etwchatcontent="+etwchatcontent+"&etwchatemail="+etwchatemail+"&etwchatuser="+etwchatuser;
	createxmlhttp();
	//alert(sendquery)
	xmlhttp.open("POST",url,true);
	xmlhttp.onreadystatechange=handlestatechange;
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendquery);
}
function handlestatechange()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			document.getElementById("etwchatcontent").value="";
			document.getElementById("etwchatcontent").focus();
		}
	}
}
function showdetail()
{
	if(showuserstate==false)
	{
		document.getElementById("etwchatuserstate").innerHTML="<b><a href='#' onclick='showalluserstate()' style='color:#444;'>Click here to see more cooperators...</a></b><b>"
	createxmlhttp();
	var url="showguestinfo.php";
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=showresultinfo
	xmlhttp.send(null);
	}
}

function showdetailAdmin()
{
	createxmlhttp();
	var url="../showguestinfo-admin.php";
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=showresultinfo
	xmlhttp.send(null);
}
function showresultinfo()
{
if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			document.getElementById("etwchatmessage").innerHTML=xmlhttp.responseText;
		}
	}
}
function getuserinfo(userid,username)
{
	document.getElementById("userid").value=userid;
	document.getElementById("etwchatusername").value=username;
	}
function checkuser()
{
	createxmlhttp();
	var url="checkuser.php?username="+document.getElementById("username").value;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=checkstate;
	xmlhttp.send(null);
	}
function checkstate()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			document.getElementById("showstate").innerHTML=xmlhttp.responseText;
		}
	}
	}

function checkcompany()
{
	createxmlhttp();
	var url="checkcompany.php?companyname="+document.getElementById("companyname").value;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=checkcompanystate;
	xmlhttp.send(null);
	}
function checkcompanystate()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			if(xmlhttp.responseText!="")
			{
				document.getElementById("userid").value=xmlhttp.responseText;
			}
		}
	}
	}
function setemail()
{
	createxmlhttp();
	var url="checkemail.php?address="+document.getElementById("etwchataddress").value+"&mytime="+new Date().getTime()
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=checkemailaddress;
	xmlhttp.send(null);
	}
function checkemailaddress()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
			document.getElementById("setemailselect").innerHTML=xmlhttp.responseText;
		}
	}
}
function checkmymailstate()
{
	if(document.getElementById("etwchatemail").value=="no email")
	{
		document.getElementById("setemailselect").innerHTML="<input type=\"text\"  id=\"etwchatemail\" value=\"Enter Your E-mail Address\" onClick=\"noinfo(this)\" onChange=\"checkemail()\" />"
		}
	}
function setchatusername(ob)
{
	document.getElementById("etwchatusername").value=ob
	document.getElementById("etwchatmessage").innerHTML="<font color='green'><b>Now,you are talking with "+ob+"<br /><a href='#' onclick='showalluserstate()'>Click here to see more cooperators...</a></b></font>"
	document.getElementById("etwchatuserstate").innerHTML="<b><a href='#' onclick='showalluserstate()' style='color:#444;'>Click here to see more cooperators...</a></b><b>"
	}
