// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
//var UseAjaxActionResult="the_AjaxActionResult";
var UseAjaxActionResult=false;

var nothing;
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}
function getXhr(){
	return createXmlHttpRequestObject();
	// or
	// return  xmlHttp;
}


function parseEventParams(paramArray){
	//var ParamArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ]; ==> transfer to string
	var paramStr="";
	var p="";
	for (var i=0; i<paramArray.length; i++) {
		p = paramArray[i];
		p = encodeURIComponent(p);
		paramStr=paramStr+"&event_params["+i+"]=" + p;
	}
	return paramStr;
}

function setAjaxActionResult(res,id){
	if (UseAjaxActionResult){
		
		if (typeof res == "undefined") {
			res = "";
		}
		if (typeof id == "undefined") {
			id = UseAjaxActionResult;
		}
		htmlEl = document.getElementById(id); 
		htmlEl.innerHTML = res ;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// call to server
function ajaxEvent(aEvent, paramArray, paramStr){
  
  //var  xmlHttp=createXhr();
  setAjaxActionResult('');
  
  var eventParams="";
  
  if (typeof aEvent == "undefined") {
    aEvent = "";
  }

  if (typeof paramArray == "undefined") {
    eventParams = "";
  }else{
	eventParams = parseEventParams(paramArray);
  }  

  if (!(typeof paramStr == "undefined")) {
    eventParams=eventParams+paramStr;
  }
  
  
  var params="event="+aEvent+eventParams;
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading the async.txt file from the server
      xmlHttp.open("GET", "ajax_event_handler.php"+"?"+params, true);
	  
	  // DEBUG: Cash disabling
	  xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
      
	  xmlHttp.onreadystatechange = handleAjaxEvent;
      xmlHttp.send(null);
	  
	  //DEBUG
	  //document.getElementById("myDivElement").innerHTML=params;
	  //DEBUG end 

	}
    // display the error in case of failure
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
	
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function that handles the HTTP response (JS)
function handleAjaxEvent() {
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // read the message from the server
        response = xmlHttp.responseText;
	  //DEBUG
	  //	 document.getElementById("myDivElement").innerHTML=response;
	  //DEBUG end 
		eval(response);
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function getSelText()
{
    var txt = '';
    if (window.getSelection){
        txt = window.getSelection();
    }
    else if (document.getSelection){
        txt = document.getSelection();
    }
    else if (document.selection){
        txt = document.selection.createRange().text;
     }
   return txt;
}

function parseFormParams(obj) {
  //var getstr = "?";
  var getstr = "&";
  for (i=0; i<obj.childNodes.length; i++) {
	 if (obj.childNodes[i].tagName == "INPUT") {
		if (obj.childNodes[i].type == "text") {
		   getstr += obj.childNodes[i].name + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
		}

		if (obj.childNodes[i].type == "hidden") {
		   getstr += obj.childNodes[i].name + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
		}
		
		if (obj.childNodes[i].type == "checkbox") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		   } else {
			  getstr += obj.childNodes[i].name + "=&";
		   }
		}
		if (obj.childNodes[i].type == "radio") {
		   if (obj.childNodes[i].checked) {
			  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
		   }
		}
	 }   
	 if (obj.childNodes[i].tagName == "SELECT") {
		var sel = obj.childNodes[i];
		getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
	 }
	 
	 if (obj.childNodes[i].tagName == "TEXTAREA") {
		var sel = obj.childNodes[i];
		getstr += sel.name + "=" + encodeURIComponent(sel.value) + "&";
	 }	 
	 
  }
  //makeRequest('get.php', getstr);
  return getstr;
}
function ajaxFormSubmit(formId,formHandler){
	obj=document.getElementById(formId);
	var params;
	params=parseFormParams(obj);
	ajaxEvent(formHandler,nothing,params);
}