/* <![CDATA[ */

/**
 * AJAX engine for benchit website
 * created for requests of benchit files to get a better performance
 * the webpage doesn't need a complete reloading. that saves a lot of request time.  
 * @author Martin Johannemann
 * @since 23-07-2008
 * @package ajax
 * @filename ajax.js
 * @filepath ./lib/
 * @see ajax_requestFiles.php
 * @see mod_files.php
 * @see listview_v2.tpl.html
 */


/**
 * @name http
 * @var http
 * var for xml request object
 */
var http = null;

/**
 * @name gid
 * @var gid
 * array for group id collection
 */
var gid = new Array();

/**
 * @name t_mod
 * @var t_mod 
 * global var for current module
 */
var t_mod = "";

/**
 * @name t_action
 * @var t_action
 * global var for current action
 */
var t_action = "";

/**
 * @name t_userid
 * @var t_userid
 * global var for current userid
 */
var t_userid = "";

/**
 * @name fileTempLoaded
 * @var fileTempLoaded
 * the number of files, which will be loaded by one ajax request
 */
var fileLoadNumber = 10;

var xmlRequestHolder = "";

if (window.XMLHttpRequest) 
  http = new XMLHttpRequest();
else if (window.ActiveXObject) 
  http = new ActiveXObject("Microsoft.XMLHTTP");

/**
 * set the xml (html) request in the right id tag (e.g. div - tag).
 * this function will be called while a request is running.
 */
function setContent()
{ 
  if (http.readyState == 4) {

	xmlRequestHolder = xmlRequestHolder + http.responseText;	
	document.getElementById(gid[gid.length-1]).innerHTML = xmlRequestHolder;

	var divBox = null;
	var divBox = document.getElementById(gid[gid.length-1]);
	var cur_file_num = null;
	
	var cur_file_num = divBox.getElementsByTagName("tr").length -1;  
	var filenumber = null;

	if(navigator.appName == "Microsoft Internet Explorer"){
		try{
			filenumber = divBox.document.getElementById("filenumber");
		}
		catch(e){
			filenumber = null;
		}
	}
	else{
		try{
			filenumber = divBox.getElementById("filenumber");
		}
		catch(e){
			filenumber = null;
		}
	}

	if ((filenumber != null) && (filenumber != false)){
		var max_file_num = parseInt(filenumber.value);	
		
		if (cur_file_num < max_file_num){
			setTimeout(function(){get_FilePartContent(gid[gid.length-1],t_mod,t_action,t_userid,cur_file_num);}, 500);
		}
		else{
			setDefault();
		}
	}
	else{
		setDefault();
	}
  }
}


function setDefault()
{
	xmlRequestHolder = "";
	window.document.body.style.cursor = 'default';

	//refresh mootools tooltip windows
	loadMootoolsToolTips();
	
	//this is only for mybenchit new file statistic
	if(!(window.document.getElementById('div_lv_stat') == null))
		window.document.getElementById('div_lv_stat').style.height = window.document.getElementById('lv_stat').offsetHeight + "px";	
}

/**
 * send a request and get needed xml data from webserver
 * will be called when the client need a new data part
 * @param {Object} groupid id which stand for the actually group of kernel files which should be loaded
 * @param {Object} mod definded the module and filelist  
 * @param {Object} userid id of actually user
 */
function get_FilePartContent(groupid, mod, action, userid)
{

  var close = false;
  var vdouble = false;
  var groupby = document.getElementsByName("groupby")[0];
  var hostname = window.location.hostname;
  var protocol = window.location.protocol;

  t_mod = mod;
  t_action = action;
  t_userid = userid;


  //use a regexp to find the directory part of the path
  //needed for refering to the correct location lateron

  var re_dirs = new RegExp("^((/.*/)|/).*\.((php)|(html))","i");
  var path = re_dirs.exec(window.location.pathname)[1];	
	

  window.document.body.style.cursor = 'progress';

  if (mod == '') {
	window.document.body.style.cursor = 'default';
  	return false;
  }
  else 
  {
	for (var i = 0; i <= gid.length; i++) 
		if (gid[i] == groupid) {
			vdouble = true;
			
			if (!(get_FilePartContent.arguments.length == 5) || action == 'mess1' || action == 'arch3') {
				document.getElementById(gid[i]).innerHTML = "";
				xmlRequestHolder = "";
				gid.splice(i, 1);
				close = true;
				window.document.body.style.cursor = 'default';
			}
		}
			
	if ((http != null) && (close == false)) {
	  if(!vdouble)
	    gid.push(groupid); 												
	  http.open("POST", protocol+"//"+hostname+path+"ajaxEngine.php", true);
	  http.onreadystatechange = setContent; 							 
	  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	  switch(action)
	  {
	  	case 'files': 
		case 'commitshare':
		case 'usrmembers':
			if(get_FilePartContent.arguments.length == 5)
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&userid=" + userid + "&groupby=" + groupby[groupby.selectedIndex].value + "&fileLoadNumber=" + fileLoadNumber + "&reloaded=" + arguments[4]);
			else
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&userid=" + userid + "&groupby=" + groupby[groupby.selectedIndex].value + "&fileLoadNumber=" + fileLoadNumber);
			break;
		case 'resource':
			if(get_FilePartContent.arguments.length == 5)
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&userid=" + userid + "&fileLoadNumber=" + fileLoadNumber + "&reloaded=" + arguments[4]);
			else
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&userid=" + userid + "&fileLoadNumber=" + fileLoadNumber);
			break;
		case 'mess1':
		case 'arch3':
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&sess_id=" + userid + "&maingroup=" + arguments[4]);
			break;
		case 'newfiles':
				http.send("id=" + groupid + "&module=" + mod + "&action=" + action + "&userid=" + userid);
			break;
	  }
	}
  } 
}
/* ]]> */

