﻿// JScript File

function get_XMLHttpReqObj(){
  var retXMLHttp;
  try
    {
    // For Firefox, Opera 8.0+, Safari
    retXMLHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // For Internet Explorer
    try
      {
      retXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e){            
            return null;
        }
      }
    }
    
    return retXMLHttp;
}

function ajax_get(url, str_type) {
	var ajx = new ajax;
	var o;

	ajx.url_get_synchronous(url);
	switch(str_type.toLowerCase()) {
		case "xml":
			o = ajx._response_xml ;
			break;
		case "text":
			o = ajx._response_text;
			break;
		default:
			alert("Error: Invalid type: " + str_type);
	}
	return o;
}

function ajax_request(strRequestType, strURI, strData, hndFunc) {
	ajx = new ajax;

	ajx.setResponseFunction(hndFunc);
	ajx.open(strRequestType, strURI, true);
	if (strRequestType.toUpperCase() == "POST") {
		ajx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	ajx.send(strData);
}

function ajax() {
	var _ok = false;
	var _request = null;

	var _handler = null;

	this._http_status = 0;
	this._http_status_text = "";
	this._response_text = "";
	this._response_xml = "";

	if (window.XMLHttpRequest) {
		try {
			_request = new XMLHttpRequest();
			this._ok = true;
		}
		catch (e) {
		}
	}
	else if (window.ActiveXObject) {
		try {
			_request = new ActiveXObject("Microsoft.XMLHTTP");
			this._ok = true;
		}
		catch (e) {
			try {
				_request = new ActiveXObject("Msxml2.XMLHTTP");
				this._ok = true;
			}
			catch (e) {
			}
		}
	}

	this.ready_state_change = function() {
		switch(_request.readyState){
		    case 4: // The request is complete
                this._http_status = _request.status;
			    this._http_status_text = _request.statusText;
			    if (this._http_status == 200) {
				    this._response_text = _request.responseText;
				    this._response_xml = _request.responseXML;
				    if (typeof(_handler) == "function") {
					    _handler(this);
				    }
			    }
			    else if (this._http_status == 404) {
				    alert("URL not found:\n" + this._http_status_text);
			    }
			    else {
                    alert(_request.responseText);
			    }
			        
                break;                
		}		
	}

	if (this._ok) {
		_request.onreadystatechange = this.ready_state_change;
	}
	this.setResponseFunction = function(f) {
		_handler = f;
	}

	this.setRequestHeader = function(type, data) {
		if (this._ok) {
			_request.setRequestHeader(type, data);
		}
		else {
			alert("Error: no object.\nCannot execute setRequestHeader");
		}
	}

	this.open = function(method, address, asynch) {
		if (this._ok) {
			this._http_status = 0;
			this._http_status_text = "";
			this._response_text = "";
			this._response_xml = "";
			_request.open(method, address, asynch);
		}
		else {
			alert("Error: no object.\nCannot execute open");
		}
	}
   
	this.send = function(data) {
		if (this._ok) {
			_request.send(data);
		}
		else {
			alert("Error: no object.\nCannot execute send");
		}
	}

	this.abort = function() {
		if (this._ok) {
			_request.abort();
		}
		else {
			alert("Error: no object.\nCannot execute abort");
		}
	}

	this.url_post = function(address, data) {
		this.open("POST", address, true);
		this.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.send(data);
	}
	this.url_get = function(address) {
		this.open("GET", address, true);
		this.send();
	}
	this.url_get_synchronous = function(address) {
		this.open("GET", address, false);
		this.send();
		this.ready_state_change();
	}
};

