/*
WGPAjax - AJAX/JavaScript библиотека.
Интерфейс AJAX GET/POST запросов и отправки форм в iframe.
Copyright © VACion, 2009
*/
function WGPAjax()
{
	this.xmlhttp={"Type":0,"S":0,"Opened":0};
	if(window.XMLHttpRequest){this.xmlhttp.Type=1;this.xmlhttp.S=new XMLHttpRequest();}
	else if(window.ActiveXObject){this.xmlhttp.Type=2;this.xmlhttp.S=(new ActiveXObject("Microsoft.XMLHTTP"))||(new ActiveXObject("Msxml2.XMLHTTP"));}
	this.xmlhttpconnect=function(url,method,vars,callback)
	{
		if(this.xmlhttp.Type==0) return false;
		if(typeof vars=='object') vars=oElems(vars);
		if(method=="GET"){this.xmlhttp.S.open(method,url+(vars?"?"+vars:""),true);this.xmlhttp.Opened=1;
		}else if(method=="POST"){
			this.xmlhttp.S.open(method,url,true);
			this.xmlhttp.S.setRequestHeader("Method","POST "+url+" HTTP/1.1");
			this.xmlhttp.S.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.xmlhttp.Opened=1;}
		if(this.xmlhttp.Opened){
			this.xmlhttp.S.onreadystatechange=function(){
				if(wgpaobj.xmlhttp.S.readyState==4&&wgpaobj.xmlhttp.Opened){wgpaobj.xmlhttp.Opened=0;callback(wgpaobj.xmlhttp.S);}};
			if(vars) this.xmlhttp.S.send(vars);
		}
	}

	this.ajfsubm=function(f,callback,multipart,su)
	{
		if(typeof f=='string') f=document.getElementById(f);if(!f) return false;
		if(f.method.toLowerCase()=="post"&&multipart)
		{
			var io=document.createElement("iframe"),ioid="aj_"+new Date().getTime().toString();
			io.setAttribute("id",ioid);
			io.setAttribute("name",ioid);
			io.setAttribute("width","0");
			io.setAttribute("height","0");
			io.setAttribute("border","0");
			eV(io,function(){
				var a,b,h=(a=io.contentWindow.document.body)?a.innerHTML:false,x=(b=io.contentWindow.document)?b.XMLDocument:b;
				if(callback){callback({"responseText":h,"responseXML":x});callback=null;setTimeout(function(){io.parentNode.removeChild(io);},10);}
			},"load");
			f.parentNode.appendChild(io);
			window.frames[ioid].name=ioid;
			f.setAttribute("target",ioid);
			f.setAttribute("method","post");
			f.setAttribute("enctype","multipart/form-data");
			f.setAttribute("encoding","multipart/form-data");
			f.submit();
		}
		else
		{
			vars=fElems(f.elements);
			this.xmlhttpconnect(f.action,f.method,vars,callback);		
		}
	}

	eV=function(o,f,ev){if(o.addEventListener){o.addEventListener(ev,f,true);}else{if(o.attachEvent)o.attachEvent("on"+ev,f);}}

	fElEUC=function(n,v){return(encodeURIComponent(n)+"="+encodeURIComponent(v)+"&");}

	fElems=function(a)
	{
		var b="",c,j,i,s,n,v;for(i=0;i<a.length;i++){
			c=a[i];n=c.name;v=c.value;if(!c.disabled&&c.name)switch(c.type){
				case'select-one':case'select-multiple':for(j=0;j<c.options.length;j++)if(c.options[j].selected){b+=fElEUC(n,(c.options[j].value||c.options[j].text));}break;
				case'radio':case'checkbox':if(c.checked)b+=fElEUC(n,v);break;
				case'file':case undefined:case'reset':case'button':break;
				case'submit':if(!s){b+=fElEUC(n,v);s=true;}break;
				default:b+=fElEUC(n,v);break;
			}
		}
	}

	oElems=function(a){var b="",i;for(i in a) b+=fElEUC(i,a[i]);return b;}
}

var wgpaobj=new WGPAjax();