/**
 * @author Szymon
 */

var callReq = function(){

	var ajaxCore = {
	url:null,
	target:null,
	formId:null,
	addInner:null,
	isRespond:false,
	http_request_ob:false,
	
		
		initAjax:function(){
			this.httpRequest();
			if (arguments[0]) {
				this.hash = arguments[0];
				var method = this.hash.method||'g';
				this.url = this.hash.url||'';
				this.target = this.hash.target || null;
				this.formId = this.hash.fID||null;
				this.addOnInner = this.hash.inner||'';
				this.HP = this.hash.HP||{};
				if(this.hash.onRespond){this.onRespond(this.hash.onRespond);}
				switch (method) {
					case ('g'):
						this.getRespond();
						return true;
						break;
					case ('p'):
						this.postRespond();
						break;
					default:
						break;
				}
			}else{
				return;
			}
		},
		
		httpRequest:function(){
			if (window.XMLHttpRequest) {
		         this.http_request_ob = new XMLHttpRequest();
			}else{
			
				try {
					this.http_request_ob = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						this.http_request_ob = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (E) {
						this.http_request_ob = false;
					}
				}
			}
					
			if (!this.http_request_ob && typeof XMLHttpRequest != 'undefined') {
					this.http_request_ob = new XMLHttpRequest();
			}
			
			if (this.http_request_ob.overrideMimeType) {
		        this.http_request_ob.overrideMimeType('text/html; charset=utf-8');
		    }
		},
		
		status:function(){
				this.http_request_ob.onreadystatechange = function(){
						if (ajaxCore.http_request_ob.readyState == 4) {
							if (ajaxCore.http_request_ob.status == 200) {
								this.isRespond = true;									
								if (ajaxCore.target != null) {
									ajaxCore.target.innerHTML = ajaxCore.addOnInner + ajaxCore.http_request_ob.responseText;
																var scripts ='';
								var text = ajaxCore.http_request_ob.responseText.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
															scripts += arguments[1] + '\n';
															return ;
														});
							    eval(scripts);	
								}else{
									var scripts ='';
									var text = ajaxCore.http_request_ob.responseText.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
															scripts += arguments[1] + '\n';
															return ;
														});
							    eval(scripts);	
									return ajaxCore.http_request_ob.responseText;
								}																
								
							}else {
								//alert('Bład odpowiedzi');
								this.isRespond = false;
							}
						}
						else {						
							if (ajaxCore.target != null)
								ajaxCore.target.innerHTML = '<div style="background-color:#ffe5a6; padding:1px; border:1px solid black; font-weight:bold; width:85px; height:18px;">Loading...<img src="./exec/images/ajax/loader-1.gif" border="0" /></div>';
						}
				}
		},
	
		getHP:function(){
			if(codexd.defined(this.HP)){
				this.prms = '';
				this.ob2url(this.HP);
				return this.prms;
			}
		},
	
		ob2url:function(){
			if (codexd.defined(arguments[0])) {
			var arr = arguments[0];
				for(var i in arr){
					if(typeof(arr[i])=='object'){
						this.ob2url(arr[i]);
					}else{
						this.prms +=i+"="+encodeURI(arr[i])+'&';
					}
				}
			}
		},
	
		FormParams:function(){
			var form = codexd.getOb(codexd.getArg(arguments[0]));
			var params = '';
			if(form != null){
				for (var i=0;i<form.length;i++) {
					params += ((i==0)?form.elements[i].name+"="+encodeURI(form.elements[i].value):"&"+form.elements[i].name+"="+encodeURI(form.elements[i].value));
		  		}	
			}
		  	 paramas;
		},
			
		getRespond:function(){
			this.status();
			this.http_request_ob.open('GET', this.url, true);
		    this.http_request_ob.send(null);
		},
		
		postRespond:function(){
			this.status();		
			var params = (this.formId!= null)?this.FormParams(this.formId):this.getHP();
			
			this.http_request_ob.open('POST',this.url, true);
			this.http_request_ob.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.http_request_ob.setRequestHeader("Content-length", params.length);
			this.http_request_ob.setRequestHeader("Connection", "close");		
		    this.http_request_ob.send(params);		
		},
		
		onRespond:function(fn){
			this.rID = setInterval(function(fn){
										if(ajaxCore.isRespond){
											eval(''+fn+'');
											clearInterval(ajaxCore.rID);
										}						
					   			   },500
					   );
		}
		
	};
	this.req = ajaxCore;	
	this.req.initAjax(arguments[0]||{});	
}