﻿function check_empty(post,page,user) {
	if (post=="")
		document.getElementById("server_update").innerHTML="אנא כתוב תגובה";
	if (page=="")
		document.getElementById("server_update").innerHTML="שגיאה. page emty";
	if (user=="")
		document.getElementById("server_update").innerHTML="אנא כתוב את שמך";
	if (post=="" || page=="" || user=="")
		return true;
}
function responseFunction(url,page) {
  var xmlHttp;
  try {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("שגיאה! הדפדפן אינו תומך ב-AJAX!");
        return false;
      }
    }
  }
    
	var post_params="new="+document.getElementById("user_post").value+"&id="+page+"&user="+document.getElementById("name_res").value+"&last_response="+document.getElementById("last_response").value;
    var url="ajax/"+url+".php";
	var post=document.getElementById("user_post").value;
	document.getElementById("user_post").value="";
	xmlHttp.open("POST", url, true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post_params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=process;
	xmlHttp.send(post_params);
    
	function process() {
		if (check_empty(post,page,document.getElementById("name_res").value))
			return false;
		if(xmlHttp.readyState==4) {
			if(xmlHttp.status==200) {
					document.getElementById("server_response").innerHTML=xmlHttp.responseText;
					document.getElementById("server_update").innerHTML="";
			}
			else
			document.getElementById("server_update").innerHTML="קיימת שגיאה בעדכון הנתונים <br /> status!=200";
		}
		else if(xmlHttp.readyState==1 || xmlHttp.readyState==2 || xmlHttp.readyState==3)
			document.getElementById("server_update").innerHTML="מעדכן, אנא המתן";
		else
			document.getElementById("server_update").innerHTML="קיימת שגיאה בעדכון הנתונים <br /> readyState!=4";
	}
}
