var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
} 

function AjaxPost(url,p,uid) {
  http.open("POST", url, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.setRequestHeader('Encoding', 'windows-1255');
  http.setRequestHeader('Content-Encoding', 'windows-1255');
  showLoadingDiv();
//  body.onload = new Function("showLoadingDiv()")
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
	AjaxFinish(http.responseText,uid);
	document.getElementById("loadingDiv").style.display = 'none';
    }
  }
  http.send(p);
}

function AjaxPostFunc(url,p,fName,sParms) {
  http.open("POST", url, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.setRequestHeader('Encoding', 'windows-1255');
  http.setRequestHeader('Content-Encoding', 'windows-1255');

  showLoadingDiv();

  http.onreadystatechange=function() {
    if(http.readyState == 4) {
	eval(fName + "(" + sParms + "http.responseText" + ");");
	document.getElementById("loadingDiv").style.display = 'none';
    }
  }
  http.send(p);
}

 function showLoadingDiv() {

	if (window.innerHeight)
		var itop = window.pageYOffset
	else if (document.documentElement && document.documentElement.scrollTop)
		var itop = document.documentElement.scrollTop
	else if (document.body)
		var itop = document.body.scrollTop


	if (document.compatMode && document.compatMode != "BackCompat")
	   itop += Math.round(document.documentElement.clientHeight/2);
	else
	   itop += Math.round(document.body.clientHeight/2);

	itop-=27;

	document.getElementById("loadingDiv").style.top = itop + 'px';
	document.getElementById("loadingDiv").style.display = 'block';
 }

 function loginPost() {
	AjaxPost("index.php?act=login","posted=1"+
		"&userName="+document.getElementById("loginuserName").value+
		"&userPassword="+document.getElementById("loginuserPassword").value,1);
 }

 function disConnect() {
	AjaxPost("index.php?act=logout","posted=1",2);
 }

 function AjaxFinish(t,uid) {
  if(uid==1) {
	if(t=="") {
	document.getElementById("loginErr").innerHTML = 'הפרטים שהוזנו שגויים. אנא נסה\\י שנית.';
	} else {
		document.getElementById("loginBox").innerHTML = t;
	}
   } else if(uid==2) {
	document.getElementById("loginBox").innerHTML = t;
   } else if(uid==3) {
	document.getElementById("phptext").innerHTML = t;
	document.getElementById("phptext").style.color = 'red';
	if(t=="") {
		alert("ההרשמה בוצעה בהצלחה.\nהינך מוזמן\\ת להתחבר לחשבונך תחת \"כניסה לרשומים\" בצד ימין.");
		window.location=('index.php');
	}
   } else if(uid==4) {
	document.getElementById("phptext").innerHTML = t;
   } else if(uid==5) {
	if(t=="error2") {
		alert("הנושא לא פורסם מכיוון שאינך מחובר למערכת.\nאנא התחבר ונסה שנית.");
	} else if(t=="error3") {
		alert("אנא וודא כי מילאת כותרת ותוכן לנושא.");
	} else {
		document.getElementById("topics").innerHTML = t;
	}
   }
 }

function securePost(sText) {
	sText = sText.replace(/&/g,"|mys001|");
	sText = sText.replace(/=/g,"|mys002|");
	sText = sText.replace(/\+/g,"|mys003|");

	return sText;
}

function votePoll() {
	myItem = document.pollForm.pollOption;
	var v=0;
	for(i=0; i<myItem.length; i++) {
		if(myItem[i].checked)
			v=myItem[i].value;
	}
	AjaxPostFunc("index.php?act=votepoll","posted=1&op="+v,"showPoll","");
}

function loadPoll() {
	AjaxPostFunc("index.php?act=showpoll","posted=1","showPoll","");
}

function showPoll(xml) {
	document.getElementById("pollDiv").innerHTML = xml;
}