/*
для вызова в коде использовать
<script>Statistics.Add(type, param)</script>
*/


if(!Statistics){
	var Statistics = {};
	
	Statistics.CreateRequest = function(){
		var Request = false;

		if (window.XMLHttpRequest)
		{
			//Gecko-совместимые браузеры, Safari, Konqueror
			Request = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			//Internet explorer
			try
			{
				Request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (CatchException)
			{
				Request = new ActiveXObject("Msxml2.XMLHTTP");
			}
		}

		if (!Request)
		{
			//alert("Вы используете устаревший броузер!");
		}

		return Request;
	}
	
	
	Statistics.st_array = new Array();
	Statistics.AJAXRequest = Statistics.CreateRequest();
	
	
	Statistics.Add = function (type, param){
			
    	Statistics.st_array[Statistics.st_array.length] = new Array(type, param);
    	return 0;
	}
	
	Statistics.Save = function(){
			
			if(Statistics.st_array.length && Statistics.AJAXRequest){
				
					var post_str = '';
					for(var i = 0; i < Statistics.st_array.length; i++){
						post_str += Statistics.st_array[i][0] + '[]=' + Statistics.st_array[i][1] + '&';
					}
					
					try{
						if(Statistics.AJAXRequest.readyState == 4 || Statistics.AJAXRequest.readyState == 0){
							Statistics.AJAXRequest.open("POST", '/ajax/statistics_save.php', true);
						  Statistics.AJAXRequest.onreadystatechange = Statistics.handleRequest;
							Statistics.AJAXRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
							Statistics.AJAXRequest.send( post_str );
							
							Statistics.st_array = new Array(); //обнуляем массив
						}
					}
					catch(e) {
						;
					}
			}
	}
	
	Statistics.handleRequest = function(){
		if(Statistics.AJAXRequest.readyState == 4 && Statistics.AJAXRequest.status == '200'){
			if(Statistics.AJAXRequest.responseText.lastIndexOf("<response>")==-1) {
				return false;
			}
		}
	}
	
	
	
	
}