/*

  (c) 2006 Claudiu Cismaru (claudiu AT cnixs DOT com)

  This software is relased under GNU GPL v2 licence.

*/

/* httpReq object */
function newHTTPReq () {
  var hRq = false;

  if (window.XMLHttpRequest) {
    try {
      hRq = new XMLHttpRequest ();
    }
    catch (e) {
      hRq = false;
    }
  }
  else if (window.ActiveXObject) {
    try {
      hRq = new ActiveXObject ("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        hRq = new ActiveXObject ("Microsoft.XMLHTTP");
      }
      catch (e) {
        hRq = false;
      }
    }
  }
  return hRq;
}

/* Object for AJAX chat communication */
function AJAXCommunication () {

  var resultHandler = null;

  var httpReq;

  function requestHandler () {
	var xmlStr = null, xmlObj = null;


    if (httpReq.readyState == 4) {
      if ((httpReq.status == 200) || (httpReq.status == 304)) {
        xmlObj = httpReq.responseXML;
        xmlStr = httpReq.responseText;
      }
      else {
        alert (httpReq.status + ": " + httpReq.statusText);
        return;
      }

      if (resultHandler)
        resultHandler (xmlObj, xmlStr);

    } // end if (readyState)
	
  } // end function

  this.setResultHandler = function (handleFunction) {
    resultHandler = handleFunction;
  }

  this.getAsyncPOSTURL = function (url, data) {	
    if (config["enableSocketServer"]==true){
    	composeRequestXML(data);
    	//printElement(data);
	}else{
		httpReq = new newHTTPReq ();

	    if (httpReq == null)
	      return null;
	
	    httpReq.onreadystatechange = requestHandler;
	
	    httpReq.open ("POST", url, true);
	    httpReq.setRequestHeader ("Content-Type",
	      "application/x-www-form-urlencoded; charset=utf-8");
	
	    var urlString = "";
		
	    for (var key in data)
		{
			if(key=="t")
			{
	      		var str = data[key]+'';
				
				
				str = str.replace(/<b>/ig,"-oppb-");
				str = str.replace(/<i>/ig,"-oppi-");
				//alert(str);
				
				str = str.replace(/<\/b>/ig,"-clpb-");
				str = str.replace(/<\/i>/ig,"-clpi-");	
				
				str = str.replace(/</g,"&#60;");
				str = str.replace(/>/g,"&#62;");
				
				
				str = str.replace(/-oppb-/g,"<b>");
				str = str.replace(/-oppi-/g,"<i>");
	
				str = str.replace(/-clpb-/g,"</b>");
				str = str.replace(/-clpi-/g,"</i>");
				
				
				urlString += key + "=" + encodeURIComponent(str) + "&";
			}
			else
				urlString += key + "=" + encodeURIComponent(data[key]) + "&";
		  
	    }
		
	    httpReq.send (urlString);	    
	}
  }
  this.commId = 0;

  this.msgId = 0;
}

