/**
 * Sends a xmlHttpRequest
 *
 */
var xhr     = false;
var xhrData = new Object();

/**
 *
 *
 */
function sendXmlHttpRequest(url, idOutput, arrParameter, callBackMethod, setWait)
{
   var objOutput;

   xhr = false;
   xhrData = new Object();

   if(!idOutput)
   {
      idOutput = false;
   }

   if(!arrParameter)
   {
      arrParameter = new Array();
   }

   // try to create a xmlHttpRequest-Object
   // Mozilla, Opera, Safari or Internet Explorer 7
   if ( typeof XMLHttpRequest != 'undefined')
   {
       xhr = new XMLHttpRequest();
   }

   if ( !xhr)
   {
       // Internet Explorer 6 or older
       try
       {
           xhr  = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
           try
           {
               xhr  = new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch(e)
           {
               xhr  = false;
           }
       }
   }
   if(!xhr)
   {
      // creation of the xmlHttpReq-Object failed
      alert('Creation of the xmlHttpRequest-Object failed.');
      return false;
   }

   // send the request
   xhrData                 = new Object();
   xhrData['idOutput']     = idOutput;
   xhrData['arrParameter'] = arrParameter;

   xhr.open('POST', url, true);

   if(callBackMethod)
   {
      xhr.onreadystatechange = callBackMethod;
   }

   if(setWait && idOutput)
   {
      if((objOutput = document.getElementById(idOutput)))
      {
         if(setWait == 'opacity')
         {
            if(objOutput.innerHTML.indexOf('<!-- loaded -->'))
            {
               objOutput.innerHTML = '<!-- loaded --><div style="filter:Alpha(opacity=50); -moz-opacity: 0.5; opacity: 0.5; -khtml-opacity: 0.5;  cursor:wait;">'
                                    + objOutput.innerHTML
                                    + '</div>';
            }
         }
         else if(setWait == 'eggclock')
         {
            objOutput.innerHTML = '<table width="100%" height="100%">'
            + '<tr>'
            + '<td  align="center" valign="middle">'
            + '<img src="/img/admin/sanduhrKlein.gif" />'
            + '</td>'
            + '</tr>'
            + '</table>';
         }
         else if(setWait == 'fileLoader')
         {
            var newDiv = document.createElement('div');
            newDiv.id = 'loadstatusBackDiv';
            newDiv.className = 'loadstatusBackDiv';
            // IE
            newDiv.style.filter = 'alpha(opacity=70)';
            // FF
            newDiv.style.opacity = '0.7';

            objOutput.appendChild(newDiv);
            objOutput.style.position = 'relative';
            var newDiv = document.createElement('div');
            newDiv.id = 'loadstatusDiv';
            newDiv.className = 'loadstatusDiv';
            newDiv.innerHTML = '<div class="quarterHeight"></div><div class="center mitte"><img align="center"  src="'+rootObject.getSandbox()+'/verwaltung/images/loadFileLoader.gif" /></div>';
            objOutput.appendChild(newDiv);
         }
         else
         {
            objOutput.innerHTML = '<table width="100%" height="100%">'
            + '<tr>'
            + '<td  align="center" valign="middle">'
            + '<img src="/img/admin/sanduhrKlein.gif" />'
            + '</td>'
            + '</tr>'
            + '</table>';
         }

      }
   }

   xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
   xhr.setRequestHeader('convert_charset','UTF-8/ISO-8859-15');

   if(arrParameter.length > 0)
   {
      xhr.send(urlCodeParaArray(arrParameter));
   }
   else
   {
      xhr.send('');
   }

   return true;
}

function urlCodeParaArray(arrIn)
{
   var i, parts, fieldName, fieldValue;
   var strCoded = '';
   var arrOut   = new Array();

   for(i = 0; i < arrIn.length; i++)
   {
      parts = arrIn[i].split('=');

      fieldName = parts[0];
      parts.shift();
      fieldValue = urlencode(parts.join('='));

      arrOut.push(fieldName + '=' + fieldValue);
   }

   return arrOut.join('&');
}

function urlencode( str ) {
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();

    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        tmp_arr = ret.split(search); // Custom replace
        ret = tmp_arr.join(replace);
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}

function XmlHttpRequestCallBack()
{
   var objOutput;

   if((xhr.readyState != 4) || (xhr.status != 200))
   {
      // der Request ist noch nicht komplett fertig
      return;
   }

   if(xhrData['idOutput'] == false)
   {
      // keine Output ID, also auch keine Ausgabe
      return;
   }

   if(!(objOutput = document.getElementById(xhrData['idOutput'])))
   {
      alert(xhr.responseText);
      return;
   }

   objOutput.innerHTML = xhr.responseText;
   return;
}

function ret(val)
{
   if(!val)
   {
      return;
   }

   return val;
}


