
var enableDebugging = false;
var isNonBlocking = true;
var ajaxDebugging = false;

function getHTTPObject()

{

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
      xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

function ajaxCall(func, param, handler, isASync, withForm)
{
    if( ajaxDebugging ) showDebug( );

    ajaxDebug( 'Getting HTTP Object' );
    var ajaxHTTP = getHTTPObject( );
    ajaxDebug( 'Opening connection to the server' );
    ajaxDebug( 'Invoking AJAX__' + escape(func) + ', with params ' + escape(param) );

    if( withForm )
    {
        ajaxHTTP.open( 'POST', 'page-ajaxform.aspx', isASync );
        ajaxHTTP.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' );
    }
    else
    {
        var finalurl = '/page-ajax.aspx?func=' + escape(func) + '&params=' + escape(param)
        ajaxDebug( finalurl );
        ajaxHTTP.open( 'GET', finalurl, isASync );
    }

    if( isASync )
    {
        ajaxHTTP.onreadystatechange = function( )
        {

            switch( ajaxHTTP.readyState )
            {
                case 0:
                    ajaxDebug( 'HTTP Uni-Initialized' );
                    break;
                case 1:
                    ajaxDebug( 'HTTP Loading' );
                    break;
                case 2:
                    ajaxDebug( 'HTTP Loaded' );
                    break;
                case 3:
                    ajaxDebug( 'HTTP Interactive' );
                    break
                case 4:
                    ajaxDebug( 'HTTP Complete' );
                    handleHTTPResponse( ajaxHTTP, handler );
                    break;
            }
        }
    }
    ajaxDebug( 'Sending HTTP' );
    if( withForm ) {
        ajaxHTTP.send( 'func=' + escape(func) + '&params=' + escape(param) );
    } else {
        ajaxHTTP.send( null );
    }
    if( !isASync )
    {
        switch( ajaxHTTP.status )
        {
            case 200:
                handleHTTPResponse( ajaxHTTP, handler );
                break;
            default:
                ajaxDebug( "Response: " + ajaxHTTP.status );
                handleHTTPResponse( ajaxHTTP, handler );
        }
    }
}

function handleHTTPResponse( HTTPObj, handler )
{
    var responseText = '';
    ajaxDebug( 'Response Status is ' + HTTPObj.status );
    if( HTTPObj.status == 200 ) responseText = HTTPObj.responseText;

    ajaxDebug( 'Response Received: ' + responseText );

    // Call the handler function
    if( handler ) handler( responseText );
}


// Used for debugging...

function ajaxDebug( info )
{
    if( ajaxDebugging ) debug( '<b>AJAX</b>: ' + info + '...' );
}

// Show the debug window
function showDebug( )
{
  window.top.debugWindow =
      window.open( "",
                   "Debug",
                   "left=0,top=0,width=600,height=700,scrollbars=yes,"
                   +"status=yes,resizable=yes" );
  window.top.debugWindow.opener = self;
  // open the document for writing
  window.top.debugWindow.document.open( );
  window.top.debugWindow.document.write( "<HTML><HEAD><TITLE>JS Debug Window</TITLE></HEAD><BODY><PRE>\n" );
}


function serializeDataForTransport(form)
{
    if( !form ) return '';

    var content = '';
    for(i=0; i < form.elements.length; i++ )
    {
        var id = '';
        if( form.elements[i].id )
            id = form.elements[i].id;
        else if( form.elements[i].name )
            id = form.elements[i].name;
        else if( form.elements[i].ID )
            id = form.elements[i].ID;

        if( id != '' ) content += (content==''?'':'&') + escape(id) + '=' + escape(form.elements[i].value);
    }
    return content;
}

// If the debug window exists, then write to it
function debug( text )
{
  if( window.top.debugWindow && !window.top.debugWindow.closed )
  {
    window.top.debugWindow.document.write( text + "\n" );
  }
}

// If the debug window exists, then close it
function hideDebug( )
{
  if( window.top.debugWindow && !window.top.debugWindow.closed )
  {
    window.top.debugWindow.close( );
    window.top.debugWindow = null;
  }
}

function registerForFunction( funcName, params, handler ){
    var js;
    ajaxDebugging = enableDebugging;
    //alert(handler);
    ajaxCall( funcName, params, handler, isNonBlocking ? true : false, false);
    //ajaxCall( funcName, '', handler, isNonBlocking ? true : false, false);
}