var ajaxstatussid='';
var ajaxstatusref='';


function showDebug( )
{
    try{
        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" );
    }catch(e){ }
}

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;
}
var ajaxDebugging = false;

function ajaxSimpleRequest(func,param) {
    var ajaxHTTP = getHTTPObject( );
    ajaxHTTP.open( 'GET', 'ajaxfuncs.aspx?func='+func+'&'+param, false);
    ajaxHTTP.send();
    if( ajaxHTTP.status == 200 ) {
        try {
            return ajaxHTTP.responseText;
        } catch(e) {
            return 'Could not get response: ' + e.toString();
        }
    } else {
        return 'Server returned HTTP '+ajaxHTTP.status+'\n\n' +ajaxHTTP.responseText;
    }
}

function ajaxCall(func, param, handler, isASync, tag)
{
    try{
        var URL = 'ajaxfuncs.aspx?func=' + escape(func) + '&params=' + escape(param);
        return ajaxCallURL(URL, func, param, handler, isASync, tag);
    }catch(e){
        alert("AjaxCall failed " + e.message);
    }
}

function ajaxCallURL(URL, func, param, handler, isASync, tag)
{
    window.status='AJAX Posting to ' + URL + ' ['+func+']...';
    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) );
    ajaxDebug( 'POSTING to ' + URL );

    ajaxHTTP.open( 'POST', URL, isASync );
    ajaxHTTP.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' );

    if( isASync && ajaxDebug )
    {
        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, tag );
                    break;
            }
        }
    }
    ajaxDebug( 'Sending Post of ' + param );
    ajaxHTTP.send( param );

    if( !isASync )
    {
        switch( ajaxHTTP.status )
        {
            case 200:
                ajaxDebug('Handling 200 response');
                return handleHTTPResponse( ajaxHTTP, handler, tag );
                break;
            default:
                alert('Got a bad AJAX Response, code ' + ajaxHTTP.status +'\n\n'+getResponseFromHTTP(ajaxHTTP) );
                ajaxDebug( "Response: " + ajaxHTTP.status );
        }
    }
    return null;
}

function getResponseFromHTTP(HTTPObj) {
    var responseText = '';
    try {
        responseText = HTTPObj.responseText;
    } catch(e) {
        responseText = 'Could not get response: ' + e.toString();
    }
    return responseText;
}

function handleHTTPResponse( HTTPObj, handler, tag )
{
    var responseText = '';
    ajaxDebug( 'Response Status is ' + HTTPObj.status );
    responseText=getResponseFromHTTP(HTTPObj);

    if( HTTPObj.status != 200 ) {
        alert('Got a bad AJAX Response, code ' + HTTPObj.status + '\n\n' + responseText );
        //showErrMsg('Got a bad AJAX Response, code ' + HTTPObj.status + '<hr>' + responseText );
    }

    ajaxDebug( 'Response Received: ' + responseText );
    ajaxDebug( 'Handler Is: ' + handler );

    // Call the handler function
    if( handler ) handler( responseText , tag );

    ajaxDebug( 'handleHTTPResponse Returning '+responseText);
    return responseText;
}


// Used for debugging...

function ajaxDebug( info )
{
    if( ajaxDebugging ) debug( '<b>AJAX</b>: ' + info + '...' );
}

function checkForOK(responseText)
{
    if (responseText!='OK' && responseText.substring(0,2)!='KO') {
        alert(responseText+'\n\nExpected OK');
    } else if (responseText.substring(0,2)=='KO') {
        window.top.document.location.href=responseText.substring(2); ;
    }else {
        window.status += '  OK';
    }
}

function ajaxStatusObject() {

    ajaxstatusref='';
    var statusdiv=null;
    try {
        ajaxstatusref='window.top.document.getElementById(\'ajaxstatus\')';
        statusdiv=eval(ajaxstatusref);
        if (statusdiv==null) throw("NULL");
    } catch(e) {
        try {
            ajaxstatusref='window.parent.document.getElementById(\'ajaxstatus\')';
            statusdiv=eval(ajaxstatusref);
            if (statusdiv==null) throw("NULL");
        } catch(e) {
            try {
                ajaxstatusref='document.getElementById(\'ajaxstatus\')';
                statusdiv=eval(ajaxstatusref);
                if (statusdiv==null) throw("NULL");
            } catch(e) {
                statusdiv=null;
            }
        }
    }
    return statusdiv;
}

function ajaxStatusUpdate(responseText)
{
    var statusdiv=ajaxStatusObject();
    if (statusdiv!=null) {
        statusdiv.innerHTML=responseText;
        if (responseText=='Done.') {
            var hidenow=ajaxstatusref+'.style.display=\'none\'';
            try {
                setTimeout(hidenow,500);
            } catch(e) {}
        } else if (ajaxstatussid!='') {
            setTimeout(reRunStatus,250);
        }
    }
}

function reRunStatus( ){
    try{
        ajaxCallURL( 'ajaxstatus.aspx?s=' + ajaxstatussid, 'status', '', ajaxStatusUpdate, true );
    }catch(e){ }
}

function debug( text )
{
    try{
        if( window.top.debugWindow && !window.top.debugWindow.closed )
        {
            window.top.debugWindow.document.write( text + "\n" );
        }
    }catch(e){}
}
