
function read_messages(key){
	loading();
	var xmlHttp;
	xmlHttp = xmlhttpRequest();
	var url = "./ajax/ajax_func.php";
	var queryString = key;
	queryString += '&action=read_messages';
	xmlHttp.open("post", url, true);
	xmlHttp.setRequestHeader('Content-Type',  "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
	xmlHttp.onreadystatechange = function(){
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			unloading();
			var j = xmlHttp.responseText.parseJSON();
			if(j!=null){
			  getById('messages_list').innerHTML = j.list;
			  getById('messages_page').innerHTML = j.page;
			}
		}
	}
	}
}

function leave_message(){
	if(getById('name').value==''){
		alert('please input name!');
		return false;
	}
	if(getById('email').value==''){
		alert('please input email!');
		return false;
	}else{
	    var exp1 = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		if(!exp1.test(getById('email').value)){
			alert('please input email with correct format!');
		    return false;
		}	
	}
	if(getById('message').value==''){
		alert('please input message!');
		return false;
	}else{
		if(getById('message').value.length>500){
			alert('character limit 500!');
		    return false;
		}
	}
	loading();
	var xmlHttp;
	xmlHttp = xmlhttpRequest();
	var url = "./ajax/ajax_func.php";
	var queryString = 'action=leave_message&name='+getById('name').value+'&email='+getById('email').value+'&message='+getById('message').value;
	xmlHttp.open("post", url, true);
	xmlHttp.setRequestHeader('Content-Type',  "application/x-www-form-urlencoded");
	xmlHttp.send(queryString);
	xmlHttp.onreadystatechange = function(){
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			unloading();
			if(xmlHttp.responseText=='success'){
			getById('name').value=getById('email').value=getById('message').value='';
			alert("Thank you for leaving message.");
			read_messages('');
			//read_reviews('');
			}else{
			    //alert(xmlHttp.responseText);
			    alert('Server is busy now, please try later');
			}
		}
	}
	}
}