/*
*	 Author: Andrew Rogozov
 */
(function(){
	
	var ajax = {};	

	ajax.send = function(url,params,loaded,preload,isPost,synchro){		
		
		var request     = getHttpRequest();
		var contentType = 'application/x-www-form-urlencoded';
		var method      = 'POST';		
		if(!isPost){
			method      = 'GET';
			contentType = "text/xml";	
			url         = url+params;
			params      = null;
		}
		if(loaded && !synchro){			
			var callBack = function(){
				if(request.readyState == 4)
				if (request.status == 200)
					loaded(request);
			};
			request.onreadystatechange = callBack;
		}		
		request.open(method, url, synchro?false:true);
		request.setRequestHeader("Content-Type", contentType+"; charset=utf-8");
		
		
		request.send(params);
		if(synchro)return request;
		
	};
	
	function getHttpRequest(){	
		/*@cc_on  return (new ActiveXObject("Microsoft.XMLHTTP")) || (new ActiveXObject("Msxml2.XMLHTTP")) @*/	
		return new XMLHttpRequest();
	};
	
//---------------------| IFRAME |----------------------- //

	// TODO: ????????? ??: http://web-tec.info/2007/09/09/ajax_fundamentals_iframe/
	
	ajax.uploadFile = function(form, url, callBack){
		var iframe = makeIframe();
		if (typeof form == "string") form=document.getElementById(form);

		url+= '&callBackFun='+callBack;
		
		form.target  = iframe.name;
		form.action  = url;
//		form.method  = 'POST';
//		form.enctype = 'multipart/form-data';

		form.submit();
	};
	
	function makeIframe(url){
		 var doc = document;
		 var id = 'iframe_upload';
		 
		 var frm = doc.getElementById(id);
		 if(frm) return frm;		 
		 
		 var div = doc.createElement('div');
		 div.innerHTML = '<iframe style="display:none" src="" id="'+id+'" name="'+id+'"></iframe>';		 
		 div.style.position = 'absolute';		     
		 doc.body.appendChild(div);		 
		 return doc.getElementById(id);
	};
	

//	function getIFrameXML(iframe) {
//		  var doc=iframe.contentDocument;
//		  if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
//		  if (!doc) doc=window.frames[iframe.id].document;
//		  if (!doc) return null;
//		  if (doc.location=="about:blank") return null;
//		 // if (doc.XMLDocument) doc=doc.XMLDocument;
//		  return doc;
//		}	
	
	if(window.js){ 
		window.js.ajax = ajax;
	}else{
		window.js	   = {};
		window.js.ajax = ajax;
	}
	
})();
