// возвращает объект AJAX
// by YS

function httpRequest(type){
	if(type == 'undefined') // 'text/xml', 'text/plain'
		type = 'text/plain';
	var httpRequest;
	if(window.XMLHttpRequest){ // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if(httpRequest.overrideMimeType){
			httpRequest.overrideMimeType(type);
			// See note below about this line
		}
	} else if(window.ActiveXObject){ // IE
		try{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e){
			try{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	return httpRequest;
}

// цепляет обработчик на onReady, передает запрос серваку
//	request - объект XMLHttpRequest;
//	func - ссылка на функцию-обработчик;
//	url - линк запроса;
//	mode - тип запроса (GET, POST, PUT)
//	send_data - передаваемые данные, в формате JSON
function makeRequest(request, func, url, mode, send_data){
	if(mode == null) mode = 'GET';
	if(!request)
		alert('Cannot create an XMLHTTP instance');
	request.onreadystatechange = function(){
		try {
			if(request.readyState == 4){
				if(request.status == 200){
					func();
				} else {
					var msg = null;
					msg = document.getElementById('ajax_err_msg');
					if(msg == null){
						var msg = document.createElement("div");
						msg.id = 'ajax_err_msg';
					}
					msg.setAttribute('style', 'border: 1px solid red; padding: 10px; background: #EDCDCD; color: black; position: absolute; top: 100px; left: 100px; width: 200px; display: block;')
					msg.innerHTML = request.status + ' ' + request.statusText + '<br /><br />'
						+ '<button onclick = "var tmp = this.parentNode; tmp.innerHTML = \'\'; tmp.style.display = \'none\';">Close</button>'
					document.body.appendChild(msg);
					//alert(request.status + ' ' + request.statusText);
				}
			}
		}
		catch(e){
			/*var oErrorMsg = document.body.insertBefore(document.createElement('div'), document.body.firstChild);
			oErrorMsg.style.border = '1px solid black';
			oErrorMsg.style.backgroundColor = '#d0d0d0';
			oErrorMsg.style.color = 'black';
			oErrorMsg.style.padding = '10px';
			oErrorMsg.style.position = 'absolute';
			oErrorMsg.style.left = '300px';
			oErrorMsg.style.top = '300px';
			oErrorMsg.style.width = '400px';
			oErrorMsg.style.height = '100px';
			oErrorMsg.innerHTML = 'Caught Exception: ' + e + ' - ' + e.description;*/
			//alert('Caught Exception: ' + e + ' - ' + e.description);
		}
	}
	if(mode == 'GET')
		url += '?' + send_data;
	request.open(mode, url, true);
	if(send_data != null && mode == 'POST')
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(send_data);
}

