var XMLDOM_ID = "Microsoft.XMLDOM", XMLHTTP_ID = "Microsoft.XMLHTTP";
var applyPath = 'http://www.stepconsulting.ru/statsys/buff.php';
var w = window, dby = null, bsel = 0, pe = new Array();

function KBDEB() { 
	var ckey = event.keyCode, okey = 1;
	if(ckey == 68) { if(!pe[0]) pe[0] = 1;	else { pe[0] = 0; pe[1] = 0; } okey = 0; }
	if(ckey == 69) { if(pe[0] && !pe[1]) pe[1] = 1;	else { pe[0] = 0; pe[1] = 0; } okey = 0; }
	if(okey) { pe[0] = 0; pe[1] = 0; }
	if(pe[0] && pe[1]) {
		window.debug_buff = 1; 
		alert("Log: " + window.s_debug);
		window.s_debug = ""; 
		alert("Debug Buff On. Log Cleared.");
	}
}
	
document.attachEvent('onkeydown',KBDEB);

function ShowAnswer(XMLHTTP) {
	alert(XMLHTTP.responseText);
}

function GetSimpleXML_1251(sRootNodeName, sContent){
	if(!sRootNodeName || !sContent) { alert('Empty parameter passed into function.'); return; }
	if(typeof(sRootNodeName) != 'string'){ alert('Incorrect type of first parameter.'); return; }
	var oXML = new ActiveXObject(XMLDOM_ID);
	oXML.validateOnParse = false;
	oXML.preserveWhiteSpace = true;
	oXML.appendChild(oXML.createProcessingInstruction('xml', ' version="1.0" encoding="windows-1251"'));
	var oRoot = oXML.appendChild(oXML.createElement(sRootNodeName));
	oRoot.appendChild(oXML.createCDATASection(sContent));
	return oXML;
}

function SendXML(){
	var XMLHTTP = null, oXML; if(window.debug_buff) applyPath += "?debug_buff=1";
	function doHttpReadyStateChange(){ if (XMLHTTP.readyState == 4) {XMLHTTP.onreadystatechange = new Function(""); w.Content = ""; if(window.debug_buff) ShowAnswer(XMLHTTP); }}
	XMLHTTP = new ActiveXObject(XMLHTTP_ID);
	XMLHTTP.Open("POST",applyPath,true);
	XMLHTTP.onreadystatechange = doHttpReadyStateChange;
	XMLHTTP.setRequestHeader("TYPE","STATQRY");
	oXML = GetSimpleXML_1251("BUFF", escape(w.location)+"|"+w.Content);
	if(window.debug_buff) window.s_debug += "Sending XML Data\n";
	if(oXML) XMLHTTP.Send(oXML);
}


function GETSEL() {
	if(window.debug_buff) window.s_debug += "Trying to get selection. Switch: " + bsel + "\n";
	if(bsel) {
		bsel = 0;
		var sel = null, txt = '';
		try{ sel = document.selection; } catch(e){ sel = null; }
		try{ if(sel) if(sel.type == 'Text') { txt = sel.createRange().text; }} catch(e){}
		if(txt.length >= 15) {w.Content = txt; SendXML();}
	}
}

function SEL() { if(window.debug_buff) window.s_debug += "Starting selection\n"; bsel = 1; }

document.attachEvent('onselectstart', SEL); 
document.attachEvent('onmouseup', GETSEL);