// Create a function to ramp up the image opacity in Mozilla
var fadein_opacity = 0.04, 
		fadeout_opacity = 1.00;

function fadein(opacity, div_id) {
	var fadein_img = document.getElementById(div_id), 
			local_fadein_opacity = opacity;
			
	fadein_img.style.display = '';

  if (typeof(opacity) != 'undefined') { local_fadein_opacity = opacity; }

  if (local_fadein_opacity < 0.99 && fadein_img && fadein_img.style &&
      typeof(fadein_img.style.MozOpacity) != 'undefined') 
	{
    local_fadein_opacity += .05;
    fadein_img.style.MozOpacity = local_fadein_opacity;
    setTimeout(function(){fadein(local_fadein_opacity, div_id)}, 50);
  }
}

function fadeout(opacity, div_id) {
	var fadeout_img = document.getElementById(div_id), 
			local_fadeout_opacity = opacity;

  if (typeof(opacity) != 'undefined') { local_fadeout_opacity = opacity; }

  if (local_fadeout_opacity > 0.05 && fadeout_img && fadeout_img.style &&
      typeof(fadeout_img.style.MozOpacity) != 'undefined') 
	{
    local_fadeout_opacity -= .05;
    fadeout_img.style.MozOpacity = local_fadeout_opacity;
    setTimeout(function(){fadeout(local_fadeout_opacity, div_id)}, 50);
  }
	else {
		fadeout_img.style.display = 'none';
	}
}

function ShowStatusID(id, status)
{
  var row = document.getElementById(id);

  if (row)
  {
		row.innerHTML = status;
		row.style.display = '';
  }
  else
	{
    alert(id + ' not found!');
	}
}

function ShowHideDescriptionID(url, id, status_id, status)
{
	ShowHideElementID(url, id, status_id, status);
}

function ShowHideElementID(url, id, status_id, status)
{
  var row = document.getElementById(id);

  if (row.innerHTML)
  {
    if (row.style.display == '')
		{
			fadeout(fadeout_opacity, id);
		}
    else
		{
			fadein(fadein_opacity, id);
		}
    return;
  }
  else
	{
    row.style.display = '';
	}
  Post.Send('', url, id, status_id, status);
}

function ReplaceElementID(url, id, status_id, status)
{
  Post.Send('', url, id, status_id, status);
} 

/***** Begin:  Ajax section *****************/
var Ajax = new Object();
Ajax.isUpdating = true;

Ajax.Request = function(method, query, callback, url, id, status_id, status)
{
  this.isUpdating = true;
  this.callbackMethod = callback;
	
  if (window.XMLHttpRequest)
  {
    try { this.request = new XMLHttpRequest(); } catch(e) {}
  }
  else if (window.ActiveXObject)
  {
    try { this.request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e)
    {
      try { this.request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
    }
  }
    
  if (!this.request)
  {
    alert('\n\nGiving up :(\n\nCannot create an AJAX connection.');
    return false;
  }

  if (method.toLowerCase() == 'get' && query != '') url = url + "?" + query;
  if (method == '') method = 'post';

  this.request.open(method, url, true);
  this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

//	var timeout = setTimeout(function () {
//		document.getElementById(status_id).innerHTML = 'Request Cancelled';
//		Ajax.isUpdating = false;
//		this.request.abort(); alert('here');}, 300);
																		 
  this.request.onreadystatechange = function() {
    Ajax.checkReadyState(id, status_id, status);
  };

  this.request.send(query);
}
	
Ajax.checkReadyState = function(id, status_id, status)
{
  try {
    if (this.request.readyState == 4) 
		{
// 0  Uninitialized - open() has not been called yet.
// 1 	Loading - send() has not been called yet.
// 2 	Loaded - send() has been called, headers and status are available.
// 3 	Interactive - Downloading, responseText holds the partial data.
// 4 	Completed - Finished with all operations.
      if (this.request.status == 200) 
			{
// 1. If the state is UNSENT or OPENED return 0 and terminate these steps.
// 2. If the error flag is true return 0 and terminate these steps.
// 3. Return the HTTP status code, success = 200
        this.isUpdating = false;
      if (id != '')
        if (document.getElementById(id))
          this.callbackMethod(this.request.responseText, id, this.request.readyState);
      }
      else {
        alert('\n\nThere was a problem with the request.\n\nStatus code: '+this.request.status+'\n'+this.request.statusText);
      }
	  }
    else
    {
			// display the updating status message when processing has not completed yet
      if (status_id != '')
        if (document.getElementById(status_id))
          this.callbackMethod(status, status_id, this.request.readyState);
    }
  }
  catch(error) {
    alert('Error: '+error.name+' -- '+error.message);
  }
}
/***** End:  Ajax section *******************/

/***** Begin:  Ajax Post section ************/
var Post = new Object();
Post.Send = function(form, url, id, status_id, status)
{
  if (form) {
    var query = Post.buildQuery(form);
    Ajax.Request(form.method, query, Post.OnResponse, url, id, status_id, status);
  }
  else {
    Ajax.Request('get',       '',    Post.OnResponse, url, id, status_id, status);
  }
}

Post.OnResponse = function(htmltext, div_id, readyState)
{
	if (readyState == 4)
	{
		if (div_id != '') {
  	  document.getElementById(div_id).innerHTML = htmltext;
//	  	fadein(fadein_opacity, div_id);
		}
	}
	else
	{
		if (div_id != '')
  	  document.getElementById(div_id).innerHTML = htmltext;		
	}
}

Post.buildQuery = function(form)
{
  var query = "";
  for(var i=0; i < form.elements.length; i++)
  {
    var key = form.elements[i].name;
    var value = encodeURIComponent(Post.getElementValue(form.elements[i]));
		
    if (value == '') value = ' ';
		
    if (key && value)
    {
      query += key + "=" + value + "&";
    }
  }

  return query;
}

Post.getElementValue = function(formElement)
{
  if (formElement.length != null) var type = formElement[0].type;
  if ((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;

  switch(type)
  {
    case 'undefined': return;

    case 'radio':
      for (var x=0; x < formElement.length; x++)
        if (formElement[x].checked == true)
          return formElement[x].value;

    case 'select-multiple':
      var myArray = new Array();
      for (var x=0; x < formElement.length; x++)
        if(formElement[x].selected == true)
          myArray[myArray.length] = formElement[x].value;
      return myArray;

    case 'checkbox': return formElement.checked;
	
    default: return formElement.value;
  }
}
/***** End:  Ajax Post section ************/
