
var _httpObjects = new Array();
var _httpCallbacks = new Array();

function initAjax() {
  if (typeof XMLHttpRequest != "undefined") {
      return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    var aVersions = [ "MSXML2.XMLHttp.5.0",
      "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
      "MSXML2.XMLHttp","Microsoft.XMLHttp"
    ];
    for (var i = 0; i < aVersions.length; i++) {
      try {
        return new ActiveXObject(aVersions[i]);
      } catch (oError) {}
    }
  }
  return false;
}

function ajaxRequest(url, callback, errorhandler) {
	var index = -1;
	for (var i = 0; i < _httpObjects.length; i++) {
		if (!_httpCallbacks[i]) {
			index = i;
			break;
		}
	}
	var httpReq = null;
	if (index < 0) {
		httpReq = initAjax();
		_httpObjects.push(httpReq);
		_httpCallbacks.push(callback);
		index = _httpObjects.length - 1;
	} else {
		_httpCallbacks[index] = callback;
		httpReq = _httpObjects[index];
	}
	httpReq.open("GET", url, true);
	httpReq.onreadystatechange = function() {
		var _httpReq = _httpObjects[index];
		if (_httpReq.readyState == 4) {
			var _callback = _httpCallbacks[index];
			_httpCallbacks[index] = '';
			try {
				if (_httpReq.status == 200) {
					var response = _httpReq.responseText;
					if (response.indexOf('login.do') > -1) {
						window.location = 'login.do';
					} else {
						response = response.replace(/\n/g, '\\n');
						response = response.replace(/\r/g, '\\r');
						response = response.replace(/'/g, '\\\'');
						eval(_callback + '(\'' + response + '\')');
					}
				} else {
          if (errorhandler) {
            errorhandler(_httpReq.responseText);
          } else {
            popupAlert(messages['errors_header'], _httpReq.responseText, "error");
          }
				}
			} catch (e) {
        if (errorhandler) {
          errorhandler(messages['reading_response'] + ': ' + e);
        } else {
          popupAlert(messages['errors_header'], messages['reading_response'] + ': ' + e, "error");
        }
			}
		}
	};
	httpReq.send(null);
}

function ajaxRequestXml(url, callback, errorhandler) {
	var httpReq = initAjax();
  if (!httpReq) {
    popupAlert(messages['ajax_not_supported']);
    return;
  }
	httpReq.open("GET", encodeURI(url + "&sid=" + Math.random()), true);
	httpReq.onreadystatechange = function() {
		if (httpReq.readyState == 4 || httpReq.readyState=="complete") {
			try {
				if (httpReq.status == 200) {
          var response = httpReq.responseXML;
					if (httpReq.responseText.indexOf('login.do') > -1) {
						window.location = 'login.do';
					} else {
            if (callback) {
              callback(response);
            }
					}
				} else {
          if (errorhandler) {
            errorhandler(httpReq.responseText);
          } else {
            popupAlert(messages['errors_header'], httpReq.responseText, "error");
          }
				}
			} catch (e) {
        if (errorhandler) {
          errorhandler(messages['reading_response'] + ': ' + e);
        } else {
          popupAlert(messages['errors_header'], messages['reading_response'] + ': ' + e, "error");
        }
			}
		}
	};
	httpReq.send(null);
}

/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

var AIM = {

    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            c.onStart();
        }
      f.submit();
    },

    loaded : function(id) {
      var i = document.getElementById(id);
      var d;
        if (i.contentDocument) {
            d = i.contentDocument;
        } else if (i.contentWindow) {
            d = i.contentWindow.document;
        } else {
            d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
          i.onComplete(d.documentElement ? d : d.body.innerHTML);
        }
    }

};
