
var Utf8 = {

     // public method for url encoding
     encode : function (string) {
         string = string.replace(/\r\n/g,"\n");
         var utftext = "";

         for (var n = 0; n < string.length; n++) {

             var c = string.charCodeAt(n);

             if (c < 128) {
                 utftext += String.fromCharCode(c);
             }
             else if((c > 127) && (c < 2048)) {
                 utftext += String.fromCharCode((c >> 6) | 192);
                 utftext += String.fromCharCode((c & 63) | 128);
             }
             else {
                 utftext += String.fromCharCode((c >> 12) | 224);
                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                 utftext += String.fromCharCode((c & 63) | 128);
             }

         }

         return utftext;
     },

     // public method for url decoding
     decode : function (utftext) {
         var string = "";
         var i = 0;
         var c = c1 = c2 = 0;

         while ( i < utftext.length ) {

             c = utftext.charCodeAt(i);

             if (c < 128) {
                 string += String.fromCharCode(c);
                 i++;
             }
             else if((c > 191) && (c < 224)) {
                 c2 = utftext.charCodeAt(i+1);
                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                 i += 2;
             }
             else {
                 c2 = utftext.charCodeAt(i+1);
                 c3 = utftext.charCodeAt(i+2);
                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                 i += 3;
             }

         }

         return string;
     }

 }

var wthreec = (document.getElementById) ? true: false;
var ie5 = (wthreec && document.all) ? true : false;
var req;

function loadXMLDoc(url, data)
{
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType)
		{
			req.overrideMimeType('text/xml');
		}
	}
	else
		if (window.ActiveXObject)
		{ // IE
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					req = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}

	if (!req)
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	req.onreadystatechange = processReqChange;
	if (debugMode)
		debugReq = 'debug&';
	else
		debugReq = '';
	url_s = url+'?'+ urlEncodeData(data)+"&hash=" + Math.random();
	if (debugMode)
		alert(url_s);
	req.open("GET", url_s, true);
	req.send();

	ab = window.setTimeout("req.abort();", 5000);
}

function processReqChange()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
				document.getElementById(tWindow+"_container").innerHTML = req.responseText;
				hideWindow(tLoadingWin);
				showWindow(tWindow);
		}
		else
		{
			alert("Failed to send request:\n" +
			req.statusText);
			hideWindow(tLoadingWin);
		}
	}
}

function urlEncodeData(data)
{
	var query = [];
	if (data instanceof Object)
	{
		for (var k in data)
		{
			query.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k]));
		}
		return query.join('&');
	}
	else
	{
		return encodeURIComponent(data);
	}
}

var cWindow = null;
var startX = 0;
var startY = 0;
var IE = document.all ? true : false;

function hideWindow(windowid)
{
	document.getElementById(windowid).style.display = 'none';
}

function showWindow(windowid,x)
{
	document.getElementById(windowid).style.display = 'block';
	if (x>0)
		document.getElementById(windowid).style.top = 300+x+'px';
}

function createTextareaWindow(title,windowid,textid,callback,x,y,contain)
{
	var code = '';
//	code += "<link href=\"http://mise.net.ua/usr/libs/attachments/frontend.css\" rel=\"stylesheet\" type=\"text/css\" />";
	code += "<div class='windowStyle' id='"+windowid+"'>";
	code += "<div class='wTitle' id='"+windowid+"_title'><img src='http://mise.net.ua/usr/libs/attachments/close.gif' align=right class='close' onClick='hideWindow(\""+windowid+"\");'>";
	code += ""+title+"</div>";
	if (contain)
		code += "<div class='wContainer' id='"+windowid+"_container'></div>";
	code += "</div>";
	document.write(code);
	eWindow = document.getElementById(windowid);
	eWindow.style.top = y;
	eWindow.style.left = x;
	wTempX = x;
	wTempY = y;
	return windowid;
}


/*	Main Part */

var selectionText;
var debugMode = false;
var is_sent = false;
var backendUrl = 'http://mise.net.ua/usr/libs/attachments/init.dls';

function showAttachment(uri_id)
{
	var postData = {'uri_id':uri_id}
	showWindow(tLoadingWin,1);
	loadXMLDoc(backendUrl, postData);
}

var tWindow = createTextareaWindow('Просмотр приложения', 'wAtt', 'tField', '', 400, 300,true);
var tLoadingWin = createTextareaWindow('Загружаю данные, ожидайте пожалуйста...', 'wLoading', 'tLF', '', 400, 300,false);
hideWindow(tWindow);
hideWindow(tLoadingWin);

document.write("<link href=\"http://mise.net.ua/usr/libs/attachments/frontend.css\" rel=\"stylesheet\" type=\"text/css\" />");