Connect={
_isFormSubmit:false,
_isFileUpload:false,
_formNode:null,
_sFormData:'',
_transaction_id:0,
_interval:50,
_tpoll:{},
	
createXhrObject:function(transactionId){
	var obj, http;
	// For Safari, Firefox, and other non-MS browsers
   if (window.XMLHttpRequest) {
     try {
       http = new XMLHttpRequest();
     } catch (e) {
       http = false;
     } 
   } else if (window.ActiveXObject) {
     // For Internet Explorer on Windows
     try {
       http = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
       try {
         http = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (e) {
         http = false;
       }
     }
   }
	obj={conn:http,tId:transactionId};
	return obj;
},

getNewConnect:function(){
	try{
		var o=this.createXhrObject(this._transaction_id);
		if(o){this._transaction_id++;}
	}
	catch(e){}
	finally{
		return o;
	}
},

send:function (url, dt) {
	var o=this.getNewConnect();
	if(!o){return null;}
	o.conn.open("GET", url+'?hash='+Math.random(), true);
	o.conn.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	o.conn.send('dt='+dt);
},

ReceiveProc:function (o) {
  switch (o.readyState) {
    case 0: // "не инициализирован"
      return ;
    break;
    case 1: // "загрузка..."
      return ;
    break;
    case 2: // "загружено"
      return ;
    break;
    case 3: // "в процессе..."
      return ;
    break;
    case 4: // "готово"
      if (o.status == 200) {
		ParseXML(o.responseXML);
  	  }
      else { // ошибка
		window.location.reload();
	  }
      return ;
    break;
    default: // "неизвестное состояние"
      return ;  
  }
},

receive:function (url) {
 	var o=this.getNewConnect();
 	if(!o){return null;}
    var h=Math.random().toString();
    h=h.substr(2,h.length);
    o.conn.open("GET", url+'?hash='+h+"&"+this._sFormData, true);
	this.handleReadyState(o);
    o.conn.send(null);
   return h;
},

handleReadyState:function(o){
	var oConn=this;
	this._tpoll[o.tId]=window.setInterval(
		function(){
			if(o.conn&&o.conn.readyState==4){
				window.clearInterval(oConn._tpoll[o.tId]);
				delete oConn._tpoll[o.Id];
				oConn.ReceiveProc(o.conn);
			}
		},this._interval);
},

setForm:function(formId,isUpload,secureUri) {
  this.resetFormState();
  var oForm;
  if(typeof formId=='string')
  {
    oForm=(document.getElementById(formId)||document.forms[formId]);
  }
  else 
    if(typeof formId=='object')
    {
      oForm=formId;
    }
    else{return;}
    if(isUpload)
    {
      this.createFrame(secureUri?secureUri:null);
      this._isFormSubmit=true;
      this._isFileUpload=true;
      this._formNode=oForm;
      return;
    }
    var oElement,oName,oValue,oDisabled;
    var hasSubmit=false;
    for(var i=0;i<oForm.elements.length;i++)
    {
      oElement=oForm.elements[i];
      oDisabled=oForm.elements[i].disabled;
      oName=oForm.elements[i].name;
      oValue=oForm.elements[i].value;
      if(!oDisabled&&oName)
      {
		switch(oElement.type)
        {
			case'select-one':
			case'select-multiple':
				for(var j=0;j<oElement.options.length;j++){
					if(oElement.options[j].selected){
						if(window.ActiveXObject){
							this._sFormData+=encodeURIComponent(oName)+'='+encodeURIComponent(oElement.options[j].attributes['value'].specified?oElement.options[j].value:oElement.options[j].text)+'&';
						}
						else{
						this._sFormData+=encodeURIComponent(oName)+'='+encodeURIComponent(oElement.options[j].hasAttribute('value')?oElement.options[j].value:oElement.options[j].text)+'&';
						}
					}
				}
			break;
			case'radio':
			case'checkbox':
				if(oElement.checked){
					this._sFormData+=encodeURIComponent(oName)+'='+encodeURIComponent(oValue)+'&';
				}
			break;
			case'file':
			case undefined:
			case'reset':
			case'button':
			break;
			case'submit':
				if(hasSubmit==false){
					this._sFormData+=encodeURIComponent(oName)+'='+encodeURIComponent(oValue)+'&';hasSubmit=true;
				}
			break;
			default:
				this._sFormData+=encodeURIComponent(oName)+'='+encodeURIComponent(oValue)+'&';break;

		}
		}
	}
	this._isFormSubmit=true;
	this._sFormData=this._sFormData.substr(0,this._sFormData.length-1);
	return this._sFormData;
},

resetFormState:function() {
	this._isFormSubmit=false;
	this._isFileUpload=false;
	this._formNode=null;
	this._sFormData="";
},

createFrame:function(secureUri) {
	var frameId='p'+this._transaction_id;
	if(window.ActiveXObject){
		var io=document.createElement('<iframe id="'+frameId+'" name="'+frameId+'" />');
		if(typeof secureUri=='boolean'){io.src='javascript:false';}
		else if(typeof secureURI=='string'){io.src=secureUri;}
	}
	else {
		var io=document.createElement('iframe');
		io.id=frameId;
		io.name=frameId;
	}
	io.style.width='0';
	io.style.height='0';
	io.style.border='0';
	document.body.appendChild(io);
},

upload:function (url) {
	var frameId='p'+this._transaction_id;
	var io=document.getElementById(frameId);
	this._formNode.action=url;
	this._formNode.method='POST';
	this._formNode.target=frameId;	
	if(this._formNode.encoding){
		this._formNode.encoding='multipart/form-data'; 
	}else{
		this._formNode.enctype='multipart/form-data';
	}
	var uploadCallback=function(){
		if (/opera/i.test(navigator.userAgent)) {
			if (!/loaded|complete/.test(document.readyState)) {
				setTimeout(function(){uploadCallback();},100);
				return;
			}else{
				var rx='';
				try{
					rx=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
				}catch(e){}
		
				ParseXML(rx);
				return;
			}
		}
		var rx='';
		try{
			rx=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
		}catch(e){}
		
		ParseXML(rx);

		if(window.detachEvent){io.detachEvent('onload',uploadCallback);}
		else{io.removeEventListener('load',uploadCallback,false);}	

		setTimeout(function(){document.body.removeChild(io);},100);
	};
	
	if(window.attachEvent){io.attachEvent('onload',uploadCallback);}
	else{io.addEventListener('load',uploadCallback,false);}

	this._formNode.submit();
	this.resetFormState();
}
};

function GetSpanByClass(cls) {
	var clses = [];
	var childs = document.getElementsByTagName('SPAN');
	for (var i=0; i<childs.length; i++) { if (childs[i].className==cls) clses[clses.length] = childs[i]; }
	return clses;
};


function GetDivByClass(cls) {
	var clses = [];
	var childs = document.getElementsByTagName('DIV');
	for (var i=0; i<childs.length; i++) { if (childs[i].className==cls) clses[clses.length] = childs[i]; }
	return clses;
};

function ParseXML(xml) {
	xml = xml.documentElement; 
	if (xml == null) { alert("Server connection Error. Sorry."); } 
	for (var i = 0; i < xml.childNodes.length; i++) { 
		if (xml.childNodes[i].nodeName == "cmd"){ 
			var cmd; 
			var target; 
			var data; 
			for (var j=0; j<xml.childNodes[i].attributes.length; j++) { 
				if (xml.childNodes[i].attributes[j].name == "p") { cmd = xml.childNodes[i].attributes[j].value;} 
				if (xml.childNodes[i].attributes[j].name == "t") { target = xml.childNodes[i].attributes[j].value; }
			}
			if (xml.childNodes[i].firstChild.data ) { data = xml.childNodes[i].firstChild.data; } else { data = ''; } 
			if (cmd=='innerHTML'){try{document.getElementById(target).innerHTML = data;}catch(e){} } 
			else if (cmd=='+innerHTML'){ try{document.getElementById(target).innerHTML=data+document.getElementById(target).innerHTML;}catch(e){} } 
			else if (cmd=='innerHTML+'){ try{document.getElementById(target).innerHTML+=data;}catch(e){} } 
			else if (cmd=='innerHTMLspanAll'){ var tt=GetSpanByClass(target);for (var oo=0;oo<tt.length;oo++){try{tt[oo].innerHTML=data;}catch(e){} } }
			else if (cmd=='innerHTMLdivAll'){ var tt=GetDivByClass(target);for (var oo=0;oo<tt.length;oo++){try{tt[oo].innerHTML=data;}catch(e){} } }
			else if (cmd=='value'){ try{document.getElementById(target).value=data;}catch(e){}} 
			else if (cmd=='js'){ eval(data); } 
		}
	}
	try{if(_resID){_chLctSize(_resID)}}catch(e){}
};
//==============================

function msgwin(n,t,w,h,align,resize,grid,cont){
	var o;
	if (!(o=document.getElementById('layerContent'+n))){
		o=document.createElement('div');
		o.id='layerContent'+n;
		o.title=t;
		document.body.appendChild(o);
	};
	if (!cont){ cont='<img border="0" src="img/ajax/ajax.gif" />';}
	document.getElementById('layerContent'+n).innerHTML=cont;
	var p=eval('({width:'+w+', height:'+h+', close: function(event, ui) { $("#layerContent'+n+'").dialog("destroy") }})');
	$('#layerContent'+n).dialog(p);
}	


function _showOnTop(n,f){
	$('#layerContent'+n).dialog('moveToTop');
}

function _closeMyWin(n){
	$('#layerContent'+n).dialog('destroy');
}

function _chLctSize(n){
	$('#layerContent'+n).dialog( "option", "height", 'auto' );
}

function _waitwin(n){
	document.getElementById('layerContent'+n).innerHTML='<img border="0" src="img/ajax/ajax.gif" />';
}

