var xmlhttp;
var targetId;
var requestUrl;
var isWorking;
var selectedPage;

/**
 * Function initializes a xmlhttp object which has already been constructed.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynchronous   - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 */
function initRequest( requestType, url, asynchronous, responseHandle ) {
  try {
      // Set the callback function
      xmlhttp.onreadystatechange = responseHandle;

      // Try to open connection
      xmlhttp.open( requestType, url, asynchronous );

      if( requestType.toLowerCase() == "post" ) {
        xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8" );
        xmlhttp.send( arguments[4] );
      } else {
        xmlhttp.send( null );
      }

  } catch (errv) {
    isWorking = false;
    alert(  "The application cannot contact the server at the moment.\n"+
            "Please try again in a few seconds.\n" );
  }
}

/**
 * Function constructs a Request object. Handles both Mozilla-based
 * browsers and Microsoft-browsers.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynch         - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 *  Note:
 *  Any fifth parameters represented as arguments[4] are the data a
 *  POST request is designed to send.
 */
function httpRequest( requestType, url, asynch, responseHandle ) {
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlhttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  // Test for a null xmlhttp if neither ActiveXObject was initialized
  if ( xmlhttp ) {
    if( requestType.toLowerCase() != "post" ) {
      initRequest( requestType, url, asynch, responseHandle );
    } else {
      //the POSTed data
      var args = arguments[4];
      if( args != null && args.length > 0 ){
        initRequest( requestType, url, asynch, responseHandle, args );
      }
    }
  }
}

/**
 * Function initializes the handler. This must be called first as
 * the page using these functions loads up.
 * Parameters:
 *  urlStr - The URL of the handler script. If this string doesn't
 *           include the question mark it is added to its end.
 */
function initHandler( urlStr, targetField ) {
  if ( urlStr.indexOf("?") == -1 )
    urlStr = urlStr + "?";

  requestUrl    = urlStr;
  targetId      = targetField;
}

function loadPage( pageId, subPageId ) {
  if( !isWorking )
  {
    if ( requestUrl != "" && ! isNaN(pageId) )
    {
      var url = requestUrl + "&action=view&id="+pageId;
      
      if ( ! isNaN(subPageId) )
        url += "&sid="+ subPageId;

      selectedPage = pageId;

      isWorking = true;
      httpRequest( "get", url, true, loadPage_cb );
    }
    else
      alert( "Not initialized." );
  }
}

function loadPage_cb() {
  if( xmlhttp && xmlhttp.readyState == 4 )
  {
    var naviItemCounter = 0;
    var naviItemImage = document.getElementById('naviItem_0');

    while (  naviItemImage != null )
    {
      var parentId = naviItemImage.parentNode.id;
      parentId = parentId.substr( (parentId.indexOf("_") + 1) );
      parentId = parseInt(parentId);

      if ( parentId == selectedPage )
        naviItemImage.src = '/images/nuoli_alas.gif';
      else
        naviItemImage.src = '/images/nuoli.gif';

      naviItemCounter++;
      naviItemImage = document.getElementById('naviItem_'+naviItemCounter);
    }
  
    document.getElementById( targetId ).innerHTML = xmlhttp.responseText;
    isWorking = false;
  }
}
