function get_http(){
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new
                ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
        xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function send_message() {
	var message = document.forms.form_chat.message.value;
	var user_to = document.forms.form_chat.user_to.value;
	var id = document.forms.form_chat.id.value;
	if (message != '') {
		document.forms.form_chat.message.value = '';
		this.http = get_http();
	    this.working = false;
		var url = '/inc/chat_insert.php';
		var select_id = 'chat_room';
		if (!this.working && this.http) {
	    	var http = this.http;
	        url = url + "?text=" + encodeURIComponent(message) + '&user_to=' + encodeURIComponent(user_to) + '&id=' + encodeURIComponent(id);
	        this.http.open("GET", url, true);
	        this.http.onreadystatechange = function() {
				if (http.readyState == 4) {
        	    	fill_chat(select_id, http.responseText);
            	    this.working = false;
	            }
    	    }
        	this.working = true;
	        this.http.send(null);
		}
	    if(!this.http) {
    		alert('Ошибка при создании XMLHTTP объекта!')
	    }
	}
}

function refresh_messages() {
	this.http = get_http();
	this.working = false;
	var url = '/inc/chat.php';
	var select_id = 'chat_room';
	if (!this.working && this.http) {
	   	var http = this.http;
	    this.http.open("GET", url, true);
	    this.http.onreadystatechange = function() {
			if (http.readyState == 4) {
            	fill_chat(select_id, http.responseText);
           	    this.working = false;
	        }
    	}
        this.working = true;
	    this.http.send(null);
	}
	if(!this.http) {
    	alert('Ошибка при создании XMLHTTP объекта!')
	}
}

function fill_chat (select_id, data) {
    var insert = document.getElementById(select_id);
    if(data.length == 0) return;
    insert.innerHTML = data;
    document.getElementById('chat_room').scrollTop=50000;
}

function cookiee(type) {
	if (type == 1) {
		document.getElementById('close').style.display='none';
		document.getElementById('bc').style.display='none';
		document.getElementById('bo').style.display='';
	}
	else if (type == 2) {
		document.getElementById('close').style.display='';
		document.getElementById('bc').style.display='';
		document.getElementById('bo').style.display='none';
	}
	this.http = get_http();
	this.working = false;
	var url = '/inc/cookie.php?type='+encodeURIComponent(type);
	if (!this.working && this.http) {
	   	var http = this.http;
	    this.http.open("GET", url, true);
	    this.http.onreadystatechange = function() {
			if (http.readyState == 4) {
            	this.working = false;
	        }
    	}
        this.working = true;
	    this.http.send(null);
	}
}