/**
----------------------------------------------------------------------------
General functions
----------------------------------------------------------------------------
**/

/** 
 * @brief	function to find an element by its ID 
 * @param 	theObj, ID of object
 * @param	theDoc, optional, document if empty
 * @retval	object or null if not found
 * @author	macromedia
**/
function findObj(theObj, theDoc){
  var p, i, foundObj;  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}

/** 
 * @brief	searches for needle in haystack (array) 
 * @param 	needle search param
 * @param	haystack array
 * @retval	key of element if found
 * @author	stephan spies 
 * @date	2006/08/02
**/
function in_array(needle, haystack){
	for (h in haystack) {
		if (haystack[h] == needle) {
			return h;
		}
	}//for
}//function

/** 
 * @brief	get the value of a radio-box 
 * @param 	rObj, name of radio-box (group)
 * @retval	key of element if found
 * @author	stephan spies
 * @date	2006/08/02
**/
function getRadioValue(rObj) {
	if (rObj.checked) return rObj.value;
    return false;
}

/** 
 * @brief	check if obj.type exists, so its a input, select or textarea-field
 *			and we use obj.value else we use innerHTML 
 * @param 	Obj, html-element
 * @param	value to assign
 * @author	stephan spies
 * @date	2006/08/03
**/
function assignValue(obj, value){
	if (obj.type) {
		obj.value = value;
	} else {	
		obj.innerHTML = value;
	}
}
/**
----------------------------------------------------------------------------
General Ajax functions
----------------------------------------------------------------------------
**/

/**
 * @brief	create ajax-object
 * @author	stephan spies
 * @date	2006/08/02
**/
function makeObject(){
  var x;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    x = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    x = new XMLHttpRequest();
  }
  return x;
}

/** 
 * @brief	listener: the function interprets an ajax response and writes it 
 *			to 'innerHTML' of the result_id object
 * @param 	obj ajax request object
 * @param	result_id, id of an html-element where to output the result, 
 *			can be : div, textarea, span, td
 * @author	stephan spies
 * @date	2006/08/02
**/
function parseInfo(obj, result_id){
  var res_obj = findObj(result_id);
  /** diplay a "please wait..." message **/ 
  assignValue(res_obj, '<img src="images/ajax-loader.gif" align="absmiddle">&nbsp;&nbsp;&nbsp;Sending your inquiry... please wait!');
  if(obj.readyState == 4){
    var answer = obj.responseText;
    assignValue(res_obj, answer);
  }
}

/** 
 * @brief	send the ajax request
 * @param 	request_uri, url for request f.e. 'index.php'
 * @param	result_id, id of an html-element where to output the result 
 * @param	post_vars, the vars for posting to php [format: 'first=value1&second=value2&third=value3'...]
 * @author	stephan spies
 * @date	2006/08/02
**/
function sendAjaxRequest(request_uri, result_id, post_vars){
  var request = makeObject();
  if (post_vars!=null) {
    request.open('post', request_uri);
  } else {
    request.open('get', request_uri);  
  }
  /** add the listener in await of response **/
  request.onreadystatechange = function() { 
    parseInfo(request, result_id);
  }
  if (post_vars!=null) {
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    request.send(post_vars);
  } else {
    request.send(null);
  }
}
/**
----------------------------------------------------------------------------
END General Ajax functions
----------------------------------------------------------------------------
**/

/** 
 * @brief	submit a complete form as a ajax request
 * @param 	request_uri, url for request f.e. 'index.php'
 * @param 	form, the html form: if you do a "onsubmit"
 * simply us 'this' 
 * @param	result_id, id of an html-element where to 
 * output the result 
 * @author	stephan spies
 * @date	2006/08/02
**/
function submitAjax (request_uri, form, result_id) {
  var elements = form.elements;
  var accept_elements = new Array('text', 'password', 'select-one', 'textarea', 'radio', 'submit');
  /** 
   * !important! 
   * We add a post-var named 'action' and the 
   * value 'ajax'. You can use this on php-side 
   * to identify a ajax-request 
  **/
  var post_vars = 'action=ajax';
  /** collect all elements and there values in form **/
  for(i=0; i<elements.length; i++){
 	if ( in_array(form.elements[i].type, accept_elements) 
		&& form.elements[i].name!='' ){
	  if (form.elements[i].type=='radio'){
	  	if (getRadioValue(form.elements[i])){
		  post_vars+='&'+form.elements[i].name;
		  post_vars+='='+form.elements[i].value;
	  	}
	  } else {
	    post_vars+='&'+form.elements[i].name;
		post_vars+='='+form.elements[i].value;
	  }//else
    }//if ( in_array
  }//for
  /** send the request **/
  sendAjaxRequest(request_uri, result_id, post_vars);
}//function