var xmlHttp;
//

function sendMail()
{
	if(!validateForm()) return false;
	var name=document.getElementById("cname").value;
	var email=document.getElementById("cemail").value;
	var tel=document.getElementById("ctel").value;
	var website=document.getElementById("cwebsite").value;
	var message=encodeURI(document.getElementById("cmessages").value);
	var url="email.php";
	var parameters="sid="+(parseInt(Math.random()*99999999))+"&cname="+name+"&cemail="+email+"&ctel="+tel+"&cwebsite="+website+"&cmessages="+message;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				alert(xmlHttp.responseText);
			}
			else 
			{
				alert("Not success !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 xmlHttp.overrideMimeType('text/xml');
                // See note below about this line
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
//begin clients processing
function getClients()
{
	if(!validateForm()) return false;
	var username=document.getElementById("username").value;
	var password=document.getElementById("password").value;
	var url="accessdata/clients.php";
	var parameters="sid="+(parseInt(Math.random()*99999999))+"&username="+username+"&password="+password;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				alert(xmlHttp.responseText);
				document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Not success !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}
function getManageUser(projectid)
{
	var funcname="get_clients_manageuser";
	var url="accessdata/accessdata.php";
	var parameters="sid="+(parseInt(Math.random()*99999999))+"&func="+funcname+"&projectid="+projectid;
	//alert(parameters);
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Server not response !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";
}
function updProjects()
{
	if(!validateProject()) return false;
	var userid="0";
	if(document.getElementById("user").options.length>0) userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
	
	var username=document.getElementById("newusername").value;
	if(username=="") username=document.getElementById("username").value;
	var password=document.getElementById("password").value;
	var firstname=document.getElementById("firstname").value;
	var lastname=document.getElementById("lastname").value;
	var email=document.getElementById("email").value;
	var category="";
	if(document.getElementById("category1").checked)category=document.getElementById("category1").value;
	else if(document.getElementById("category2").checked)category=document.getElementById("category2").value;
	else category="2";
	var projectid="0";
	if(document.getElementById("project").options.length>0) 
		projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;
	var projectname=document.getElementById("newproject").value;
	if(projectname=="") projectname=document.getElementById("project").options[document.getElementById("project").selectedIndex].text;
	var finished="";
	if(document.getElementById("finished0").checked) finished=document.getElementById("finished0").value;
	else if(document.getElementById("finished0").checked) finished=document.getElementById("finished0").value;
	var projectlink=document.getElementById("link").value;
	
	if(document.getElementById("new").disabled)
	{
		projectid="0";
		if(document.getElementById("newusername").value!="") userid="0";
	}
	
	var url="accessdata/accessdata.php";
	var parameters="func=upd_project&sid="+(parseInt(Math.random()*99999999))+"&username="+username+"&password="+password+"&userid="+userid+"&firstname="+firstname+"&lastname="+lastname+"&email="+email+"&category="+category+"&projectid="+projectid+"&projectname="+projectname+"&finished="+finished+"&link="+projectlink;
	//alert(parameters);return;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText!="")alert(xmlHttp.responseText);
				else getManageUser(projectid);
			}
			else 
			{
				alert("Not success !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}
function delProject()
{
	if(!confirm("Are you sure delete this project and user?")) return;
	var userid="0";
	if(document.getElementById("user").options.length>0) userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
	var projectid="0";
	if(document.getElementById("project").options.length>0)projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;
	var url="accessdata/accessdata.php";
	var parameters="func=del_project&sid="+(parseInt(Math.random()*99999999))+"&userid="+userid+"&projectid="+projectid;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText=="")
				{
					alert("Delete success !");
					getManageUser();
				}
				else alert(xmlHttp.responseText);
			}
			else 
			{
				alert("Server not responding !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}
//login
function authenticate_user()
{
	if(!validateLogin())return;
	var username=document.getElementById("username").value;
	var password=document.getElementById("password").value;
	var url="accessdata/accessdata.php";
	var parameters="func=authenticate_user&sid="+(parseInt(Math.random()*99999999))+"&username="+username+"&password="+password;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText=="")
				{
					alert("Username and Password does not exists !");
				}
				else
				document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Server not responding !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";
}
function getLogin()
{
	var funcname="get_clients_login";
	var url="accessdata/accessdata.php";
	var parameters="sid="+(parseInt(Math.random()*99999999))+"&func="+funcname;
	//alert(parameters);
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Server not response !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";
}
//client info
function getClientsInfo(userid,projectid,messageid,messagetype)
{
	var funcname="get_clients_info";
	var url="accessdata/accessdata.php";
	var parameters="sid="+(parseInt(Math.random()*99999999))+"&func="+funcname+"&projectid="+projectid+"&userid="+userid+"&messageid="+messageid+"&messagetype="+messagetype;
	//alert(parameters);
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Server not response !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";
}

function updMessage()
{
	if(!validateMessage()) return false;
	var userid="0";
	if(document.getElementById("user").options.length>0) userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
	var projectid="0";
	if(document.getElementById("project").options.length>0) 
		projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;
	
	var title=document.getElementById("mtitle").value;
	var message=document.getElementById("mmessage").value;
	var messageid=document.getElementById("mid").value;
	if(document.getElementById("newmessage").checked) messageid=0;
	
	var url="accessdata/accessdata.php";
	var parameters="func=upd_message&sid="+(parseInt(Math.random()*99999999))+"&userid="+userid+"&projectid="+projectid+"&title="+title+"&message="+message+"&messageid="+messageid;
	//alert(parameters);return;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText=="")alert("Post message not success");
				else document.getElementById("manageuser").innerHTML=xmlHttp.responseText;
			}
			else 
			{
				alert("Not success !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";
}
function projectSelectedIndexChanged()
{
	var userid="0";
	if(document.getElementById("user").options.length>0) userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
	var projectid="0";
	if(document.getElementById("project").options.length>0) 
		projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;
	getClientsInfo(userid,projectid,0,1);
}

function uploadFile() {
	if(!validateDocument()) return false;
	var file1=document.getElementById("file1");
	var file2=document.getElementById("file2");	
	var file1title=document.getElementById("file1title").value;
	var file2title=document.getElementById("file2title").value;	
	var userid="0";
	var username="";
	if(document.getElementById("user").options.length>0) 
	{
		userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
		username=document.getElementById("user").options[document.getElementById("user").selectedIndex].text;
	}
	var projectid="0";
	if(document.getElementById("project").options.length>0) 
	projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;

// Create new JsHttpRequest object.
    var req = new JsHttpRequest();
    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            // Write result to page element (_RESULT becomes responseJS). 
			if(req.responseText=="")alert("Server not responding !");
			else  document.getElementById("manageuser").innerHTML=req.responseText;
			
        }
    }
    // Prepare request object (automatically choose GET or POST).
    req.open(null, 'accessdata/accessdata.php', true);
    // Send data to backend.
    req.send( { file1: file1,file2:file2,func:"upload_file",file1title:file1title,file2title:file2title,userid:userid,projectid:projectid,username:username } );
//document.getElementById("manageuser").innerHTML="<div align=\"center\" style=\"height:300px;vertical-align:middle;\"><br><br><br><br><br><br><br><b>Loading...</b></div>";

}

function delMessage(messageid,type) //type=1 message, 2 document
{
	if(type==1){if(!confirm("Are you sure to delete this message ?")) return;}
	else {if(!confirm("Are you sure to delete this file ?")) return;}

	var userid="0";
	if(document.getElementById("user").options.length>0) userid=document.getElementById("user").options[document.getElementById("user").selectedIndex].value;
	var projectid="0";
	if(document.getElementById("project").options.length>0) 
		projectid=document.getElementById("project").options[document.getElementById("project").selectedIndex].value;	
	var messageparentid=document.getElementById("mid").value;

	var url="accessdata/accessdata.php";
	var parameters="func=del_message&sid="+(parseInt(Math.random()*99999999))+"&messageid="+messageid+"&type="+type+"&messageparentid="+messageparentid+"&projectid="+projectid+"&userid="+userid;
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText=="1")
					alert("Unable to delete source message !");
				else if(xmlHttp.responseText=="")
					alert("Delete not success !");
				else
				{
					alert("Delete success !");
					document.getElementById("manageuser").innerHTML=xmlHttp.responseText;				
				}
			}
			else 
			{
				alert("Server not responding !");
			}
		}
	};
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", parameters.length);
    xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}
