var YAjax=(function(){
	 
		var createXhrObject=function() { 
			var methods = [
			  function() { return new XMLHttpRequest(); },
			  function() { return new ActiveXObject('Msxml2.XMLHTTP'); },
			  function() { return new ActiveXObject('Microsoft.XMLHTTP'); }
			];
			for(var i = 0, len = methods.length; i < len; i++) {
				  try {
					methods[i]();
				  }catch(e) {
					continue;
				  }
				  //createXhrObject = methods[i]; 
				  return methods[i];
			}
			throw new Error('SimpleHandler: Could not create an XHR object.');
		} ;
       var dataParams=function(arr){
		    var s=[];
			for(var key in arr){
				s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(arr[key]);
			}
			return s.join("&");
		}
		return {request:function(method, url, data, success, failure){
				
				var xhr = createXhrObject()();
				
				var senddata=dataParams(data);
				if(method.toUpperCase() !== 'POST'){
				   if(senddata){
					 url+='?'+senddata;
				   }
				   senddata=null;
				   

				}

			 xhr.open(method, url, true);
			//alert(method.toUpperCase());
			if(method.toUpperCase() === 'POST'){
				
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			
			xhr.onreadystatechange = function() {
				  if(xhr.readyState !== 4) return;
				  //alert('asdfasd');
				  (xhr.status === 200) ? 
					success(xhr.responseText, xhr.responseXML) : 
					failure(xhr.status);
					//alert(xhr.responseText);
			};
			xhr.send(encodeURI(senddata));
			
		}
	};
})();