var PostRequest;	
var useAjax = true;

function HttpRequest() {
this.http_request = null;
this.form = null;
this.failed = false;
this.processResponse = function() {};
this.errorResponse = function(status) {};
this.loadingResponse = function(state) {};

this.getFormId = function(){
var iLen = String(this.form.name).length;
return String(this.form.name).substring(iLen, iLen - 1);
};

this.GetRequest = function() {
 if (this.http_request) { return true}
 if (window.XMLHttpRequest) { // Mozilla, Safari,...
    this.http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
   try { this.http_request = new ActiveXObject("Msxml2.XMLHTTP"); } 
   catch (e) {
     try { this.http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
	 catch (e) {return false}
}} return (this.http_request)};


this.makeRequest = function (method, url, parameters) {
if (this.failed) {return false};
var self = this;
if (this.http_request.overrideMimeType) {
//	if (method == 'POST') {this.http_request.overrideMimeType('text/html'); } else {
	this.http_request.overrideMimeType('text/xml'); //}
}
this.http_request.open(method, url, true);
this.http_request.onreadystatechange = function() {
	if (self.http_request.readyState == 4) {
	if (self.http_request.status == 200) {self.processResponse()} else {self.errorResponse(self.http_request.status)}}
	self.loadingResponse(self.http_request.readyState)};
if (method == 'POST') {
  this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.http_request.setRequestHeader("Content-length", parameters.length);
//  this.http_request.setRequestHeader("Connection", "close");
}
  this.http_request.send(parameters);
  return true;
};

this.makePOSTRequest = function (url, parameters) {return this.makeRequest ('POST', url, parameters)};

this.loadXMLDoc = function(url) {return this.makeRequest ('GET', url, null)};

this.onResponse = function() {
if (self.http_request.readyState == 4) {
  if (self.http_request.status == 200) {self.processResponse()} else {self.errorResponse(this.http_request.status)}
}
self.loadingResponse(self.http_request.readyState);
};

this.postForm = function (f) {
var poststr = 'ajax_based=1&' + this.encodeForm(f);
this.form = f;
return this.makePOSTRequest(f.action, poststr);
};

this.encodeForm = function(form) {
  if(!form||!form.elements) throw "encodeForm: error, argument is not a FORM";
  var ret=[], el;
  for(var i=0; i<form.elements.length; i++) {
    el=form.elements[i];
    if("checkboxradio".indexOf(el.type)>=0) {if(el.checked) ret[ret.length]=this.getInputValue(el);}
    else if(el.type!="button"&&el.type!="submit") ret[ret.length]=this.getInputValue(el);
  }
  return ret.join("&");
};

this.getInputValue = function(inp) {
  if(typeof(inp.nodeName)=="undefined") { //collection of radio
    for(var i=0; i<inp.length; i++)
      if(inp[i].checked) return (inp[i].name? encodeURIComponent(inp[i]).name+"=":"") + encodeURIComponent(inp[i].value);
    return "";
  }
  if(inp.type=="select-multiple") {
    var ret=[];
    for(var i=0; i<inp.options.length; i++)
      if(inp.options[i].selected) ret[ret.length]=(inp.options[i].name? encodeURIComponent(inp.options[i].name)+"[]=":"")+encodeURIComponent(inp.options[i].value);
    return ret.join("&");
  } else if(inp.type=="select-one")
     return (inp.selectedIndex>=0)?
       ((inp.name? encodeURIComponent(inp.name)+"=":"")+encodeURIComponent(inp.options[inp.selectedIndex].value)) : "";
  if(inp.type=="image") return (inp.name? encodeURIComponent(inp.name)+"=":"")+encodeURIComponent(inp.src);
  else return (inp.name? encodeURIComponent(inp.name)+"=":"")+encodeURIComponent(inp.value);
};
this.failed = (!this.GetRequest());
};

function cancelMessage() {
if (PostRequest) {PostRequest.http_request.abort();}
closeMessage();
};

function closeMessage() {};
function checkForm(form) {return true};

function postForm(form) {
if (!checkForm(form)){return false};
if (!PostRequest) {PostRequest = new HttpRequest()};	
if (PostRequest.failed) { // use html form submit
	
window.open("submit.html","SubmitWin", "width=300,height=150,toolbar=0,scrollbars=1");
form.target = "SubmitWin";
form.submit();

} else {	// use ajax form submit

PostRequest.processResponse = function() {
closeMessage();
displayStaticMessage(PostRequest.http_request.responseText, false)};

PostRequest.errorResponse = function(status) {
closeMessage();
displayStaticMessage('<table cellpadding="1" cellspacing="1" width="100%" height="100%"><tr valign="middle"><td width="45" align="center"><img src="img/cancel.gif" alt="error" align="center"></td><td class="statusText_Ok">Ошибка при передаче данных</td><tr><td></td><td class="statusError_Descr"><span class="statusError_Remark">Ошибка '+status+':</span><br>'+PostRequest.http_request.statusText+'</td></tr><tr><td align="center" colspan="2"><a class="statusClose_Win" href="#" onclick="closeMessage();">Закрыть окно</a></td></tr></table>')};

PostRequest.loadingResponse = function(state) {
if (state==1) {
displayStaticMessage('<table cellpadding="4" cellspacing="1" width="100%" height="100%"><tr valign="middle"><td width="40"><img src="img/info.gif" alt="Инфо" align="left"></td><td class="statusText_Ok">Передача данных на сервер</td></tr><tr><td align="center" colspan="2"><a class="statusClose_Win" href="#" onclick="cancelMessage();">Отменить запрос</a></td></tr></table>', false);
} if (state==4) {}};
return PostRequest.postForm(form);
}
};


