// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

var ajax_get_error = false;

function ajax_do (url,el,temp) {
	// Does URL begin with http?
//	alert(url);
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}

	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	
	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);

	el.innerHTML = temp;
	return true;
}

function ajax_get (url, el) {
	// Has element been passed as object or id-string?
	if (typeof(el) == 'string') {
		el = document.getElementById(el);
	}

//	el.style.display = "";

	// Valid el?
	if (el == null) { return false; }

	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}

	//el.innerHTML = 'جاري تحميل الشجرة ...';
	var temp = el.innerHTML;
	el.innerHTML = '<span style="align: center"><img src="images/10-0.gif"></span>';
	// Create getfile URL
	getfile_url = base_url + 'getfilev2.php?url=' + escape(url) + '&el=' + escape(el.id);

	// Do Ajax
	ajax_do (getfile_url,el,temp);
	return true;
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function ajaxopen(div, url)
  {

  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	
	xmlHttp.onreadystatechange=function()
	  {
	  if(xmlHttp.readyState==4)
		{
			document.getElementById(div).innerHTML = xmlHttp.responseText;
			document.getElementById('loading').style.display = 'none';
		}
		else
		{ 
			var arrayPageScroll = getPageScroll();
			var arrayPageSize = getPageSize();
			
			document.getElementById('loading').style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 189) / 2) + 'px');
			document.getElementById('loading').style.left = (((arrayPageSize[0] - 20 - 295) / 2) + 'px');

			document.getElementById('loading').style.display = '';
		}
	  }

	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);
  }


function ajaxopen2(div, url,parameters)
  {

  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
		alert(YES);
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	
	xmlHttp.onreadystatechange=function()
	  {
	  if(xmlHttp.readyState==4)
		{
			document.getElementById(div).innerHTML = xmlHttp.responseText;
		//	document.getElementById('loading').style.display = 'none';
		}
		else
		{ 
			var arrayPageScroll = getPageScroll();
			var arrayPageSize = getPageSize();
			
		//	document.getElementById('loading').style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 189) / 2) + 'px');
		//	document.getElementById('loading').style.left = (((arrayPageSize[0] - 20 - 295) / 2) + 'px');

		//	document.getElementById('loading').style.display = '';
		}
	  }

	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 addComment(r)
{
	var ttt = document.getElementById('commentBox').value;
	document.getElementById('COMMENT_FORM').innerHTML = '<img alt="" src="images/loading.gif" width="48" height="48" />';
	ajaxopen2('COMMENT_FORM', '_commentBox.php','ajax=1&id=' + encodeURI(r) + '&CommentBox=' + encodeURI(ttt));

	document.getElementById('PRV_COMMENTS').innerHTML = '<img alt="" src="images/loading.gif" width="48" height="48" />';

	setTimeout("showPrvComments(" + r + ",0)", 1000); // 1 second
}

function showPrvComments(r,s)
{
	document.getElementById('PRV_COMMENTS').innerHTML = '<img alt="" src="images/loading.gif" width="48" height="48" />';
	ajaxopen2('PRV_COMMENTS', 'users_comments.php','start=' + s + '&id=' + encodeURI(r) + '&grp_id=3&lang=A');
}

