
function read_questions(key){
	loading();
	var xmlHttp;
	xmlHttp = xmlhttpRequest();
	var url = "./ajax/ajax_func.php";
	var queryString = key;
	queryString += '&action=read_questions';
	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('questions_list').innerHTML = j.list;
			  getById('questions_page').innerHTML = j.page;
			}
		}
	}
	}
}

function ask_question(){
	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('question').value==''){
		alert('please input question!');
		return false;
	}else{
		if(getById('question').value.length>500){
			alert('character limit 500!');
		    return false;
		}
	}
	loading();
	var xmlHttp;
	xmlHttp = xmlhttpRequest();
	var url = "./ajax/ajax_func.php";
	var queryString = 'action=ask_question&name='+getById('name').value+'&email='+getById('email').value+'&question='+getById('question').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();//alert(xmlHttp.responseText);
			if(xmlHttp.responseText=='success'){
			getById('name').value=getById('email').value=getById('question').value='';
			alert("Thank you for asking question.");
			read_questions('');
			}else{
			    //alert(xmlHttp.responseText);
			    alert('Server is busy now, please try later');
			}
		}
	}
	}
}
