var jestem_misio=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 jestem_misio=true;
@end @*/

var general_url="/pylo.cgi";
//Zwraca instancję obiektu XMLHttpRequest
//lub odpowiednika dla niegrzecznych przeglądarek
function hrequest(){
	var xmlhttp=false;
	if(typeof XMLHttpRequest!='undefined'){
		xmlhttp=new XMLHttpRequest();
	}
	if(!xmlhttp&&jestem_misio){
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
        catch(e){
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
            catch(E){
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}
//Wykonuje proste zapytanie Ajax'em - czyli albo nie ma parametrów,
//albo już je zakodowano wcześniej w url i zwraca treść odpowiedzi
function simpleRequest(str,url){
	var xhr=hrequest();
	if(!xhr) return "";
	var m=g('monitor')&&g('monitor').checked;
    //m=true;
	try{
		if(!url) url=general_url;
		xhr.open("POST",url,false);
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		if(m) alert(str);
		xhr.send(str);
		if(m) alert(xhr.responseText);
		return xhr.responseText;
	}
	catch(e){
		alert(e);
		return "";
	}
}

//Wykonuje proste zapytanie Ajax'em - czyli albo nie ma parametrów,
//albo już je zakodowano wcześniej w url i zwraca obiekt xmlHttpRequest
function enhancedSimpleRequest(str,url){
	var xhr=hrequest();
	if(!xhr) return "";
	var m=g('monitor')&&g('monitor').checked;
    //m=true;
	try{
		if(!url) url=general_url;
		xhr.open("POST",url,false);
		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		if(m) alert(str);
		xhr.send(str);
		if(m) alert(xhr.responseText);
		return xhr;
	}
	catch(e){
		alert(e);
		return "";
	}
}


//Koduje polecenie z parametrami do postaci requestString
function RequestString(command){
	var str=""
	if(command) str="cmd="+encodeURIComponent(command);
	var i;
	for(i=1;i<arguments.length-1;i+=2){
		if(arguments[i+1] != null){
			if(str) str+='&'
			str+=arguments[i]+'='+encodeURIComponent(arguments[i+1]);
		}
	}
	return str;
}
var return_value;
//Wykonuje zapytanie Ajax'em oczekując sensownej odpowiedzi
//Odpowiedź powinna ustawiać zmienną return_value na dane w postaci json
//Odpowiedź powinna też ustawiać zmienną command_done na true,
//jeśli serwer wykonał polecenie
function enhancedPostRequest(command){
	var str="cmd="+encodeURIComponent(command);
	var i;
	for(i=1;i<arguments.length-1;i+=2){
		if(arguments[i+1]!=null) str+='&'+arguments[i]+'='+encodeURIComponent(arguments[i+1]);
	}
	var xhr=enhancedSimpleRequest(str);
    if(!xhr){
		alert("Błąd połączenia z serwerem");
		return false;
	}
    if(xhr.status==500){
        alert(xhr.responseText);
        return false;
    }
    rc=xhr.responseText;
	var command_done=false;
	try{
		eval(rc);
	}
	catch(e){
		alert(e);
		return false;
	}
	return command_done;
}
//Wykonuje zapytanie Ajax'em oczekując sensownej odpowiedzi
//Odpowiedź powinna ustawiać zmienną return_value na dane w postaci json
//Odpowiedź powinna też ustawiać zmienną command_done na true,
//jeśli serwer wykonał polecenie
function PostRequest(command){
	var str="cmd="+encodeURIComponent(command);
	var i;
	for(i=1;i<arguments.length-1;i+=2){
		if(arguments[i+1]!=null) str+='&'+arguments[i]+'='+encodeURIComponent(arguments[i+1]);
	}
	var rc=simpleRequest(str);
	if(!rc){
		alert("Błąd połączenia z serwerem");
		return false;
	}
	var command_done=false;
	try{
		eval(rc);
	}
	catch(e){
		alert(e);
		return false;
	}
	return command_done;
}
//Zamienia ciągi białych znaków w tekście na pojedyncze spacje
//i usuwa białe znaki z początku i końca tekstu
//Obowiązkowa przy przyjmowaniu danych z formularzy i prompt'ów
function cpress(a){
	return a.replace(/^\s+/g,'').replace(/\s+$/g,'').replace(/\s+/g,' ');
}
//Usuwa białe znaki z początku i końca tekstu
function trim(a){
	return a.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
//Zwraca nowy element DomNode, opcjonalnie z ustawionymi atrybutami
//Nakładka na document.createElement(typ)
function c(typ){
	var el=document.createElement(typ);
	var i;
	for(i=1;i<arguments.length-1;i+=2){
		if(arguments[i]=='class'){
			el.className=arguments[i+1];
		}
		else{
			el.setAttribute(arguments[i],arguments[i+1]);
		}
	}
	return el;
}
//Zwraca nowy element TextNode
function t(a){
	return document.createTextNode(a);
}
//Zwraca element DomNode, który posiada podane id
function g(a){
	return document.getElementById(a);
}
//Usuwa wszystkich potomków elementu
//i wstawia, jako jedynego potomka nowy element TextNode
//o wartości podanej w argumencie str
function changeInnerString(el,str){
	if(!el) return;
	clear_node(el);
	el.appendChild(t(str));
}
//Zwraca współrzędne obiektu w postaci tablicy [x,y]
function GetXY(obj){
	var x=0,y=0;
	for (;obj;obj=obj.offsetParent){
		x+=obj.offsetLeft;
		y+=obj.offsetTop;
	}
	return [x,y]
}
//Zwraca wartość elementu (input,select,textarea)
function v(id,cpr){
	if(typeof id=="string"){
		id=g(id);
	}
	if(!id) return "";
	if(id.nodeName.toLowerCase()=="input"||id.nodeName.toLowerCase()=="textarea"){
		var typ=id.type.toLowerCase();
		if(typ=="checkbox"||typ=="radio"){
			if(id.checked){
				if(id.hasAttribute('value')) return id.value;
				return "1";
			}
			return "";
		}
		if(cpr) return trim(id.value);
		return cpress(id.value);
	}
	if(id.nodeName.toLowerCase()=="select") {
		var n=id.selectedIndex;
		if(n<0) return "";
		var opt=id.options[n];
		return opt.value;
	}
	return "";
}
//Ustawia wartość elementu (input,select,textarea)
function s(id,v){
	if(typeof id=="string"){
		id=g(id);
	}
	if(!id) return;
	if(id.nodeName.toLowerCase()=="input"||id.nodeName.toLowerCase()=="textarea"){
		id.value=v;
		return;
	}
	if(id.nodeName.toLowerCase()=='select'){
		var opts=id.options,i;
		for(i=0;i<opts.length;i++) if(opts[i].value==v){
			id.selectedIndex=i;
			break;
		}
		return;
	}
}
//Zwraca true, jeżeli string zaczyna się od 'y','Y','t' lub 'T'
//false w przeciwnym wypadku
function is_yes(c){
	if (!c) return false;
	c=c.charAt(0).toLowerCase();
	if (c=='t' || c=='y') return true;
	return false;
}
//Usuwa element z listy potomków aktualnego rodzica
function orphan(id){
	var d=g(id);
	if(d) d.parentNode.removeChild(d);
}
//Usuwa wszystkich potomków elementu
function clear_node(el){
	if(typeof el=="string") el=g(el);
	while(el.firstChild) el.removeChild(el.firstChild);
}
//Zwraca dla podanego obiektu najmłodszego przodka posiadającego daną klasę
function parentOfClass(ob,n){
	var cls,i
	while((ob=ob.parentNode)){
		cls=ob.className;
		if(!cls) continue;
		cls=cpress(cls).split(' ')
		for(i=0;i<cls.length;i++){
			if(cls[i]==n) return ob;
		}
	}
	return null;
}
//Zwraca dla podanego obiektu najmłodszego przodka, który jest danego typu
function parentOfType(ob,n){
	while((ob=ob.parentNode)) if(ob.nodeName.toLowerCase()==n) return ob;
	return null;
}
//Zwraca  element td posiadający daną klasę
//należący do tego samego wiersza tabelki, co podany obiekt
function getTdByClass(ob,clas){
	var tr=parentOfType(ob,'tr');
	if(!tr) return null;
	var td;
	for(td=tr.firstChild;td;td=td.nextSibling){
		if(td.className&&td.className==clas) return td;
	}
	return null;
}
//Json
function json_encode(mixed_val){
    // http://kevin.vanzonneveld.net
    // +      original by: Public Domain (http://www.json.org/json2.js)
    // + reimplemented by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + improved by: T.J. Leahy
    // *     example 1: json_encode(['e', {pluribus: 'unum'}]);
    // *     returns 1: '[\n    "e",\n    {\n    "pluribus": "unum"\n}\n]'
    /*
        http://www.JSON.org/json2.js
        2008-11-19
        Public Domain.
        NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
        See http://www.JSON.org/js.html
    */
    var json=this.window.JSON;
    if(typeof json==='object'&&typeof json.stringify==='function'){
        return json.stringify(mixed_val);
    }
    var value=mixed_val;
    var quote=function(string){
        var escapable=/[\\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
        var meta={    // table of character substitutions
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        };
        escapable.lastIndex=0;
        return escapable.test(string) ?
        '"' + string.replace(escapable, function (a) {
            var c = meta[a];
            return typeof c === 'string' ? c :
            '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
        }) + '"' :
        '"' + string + '"';
    };
    var str = function (key, holder) {
        var gap = '';
        var indent = '    ';
        var i = 0;          // The loop counter.
        var k = '';          // The member key.
        var v = '';          // The member value.
        var length = 0;
        var mind = gap;
        var partial = [];
        var value = holder[key];
        // If the value has a toJSON method, call it to obtain a replacement value.
        if (value && typeof value === 'object' &&
            typeof value.toJSON === 'function') {
            value = value.toJSON(key);
        }
        // What happens next depends on the value's type.
        switch (typeof value) {
            case 'string':
                return quote(value);
            case 'number':
                // JSON numbers must be finite. Encode non-finite numbers as null.
                return isFinite(value) ? String(value) : 'null';
            case 'boolean':
            case 'null':
                // If the value is a boolean or null, convert it to a string. Note:
                // typeof null does not produce 'null'. The case is included here in
                // the remote chance that this gets fixed someday.
                return String(value);
            case 'object':
                // If the type is 'object', we might be dealing with an object or an array or
                // null.
                // Due to a specification blunder in ECMAScript, typeof null is 'object',
                // so watch out for that case.
                if (!value) {
                    return 'null';
                }
                // Make an array to hold the partial results of stringifying this object value.
                gap += indent;
                partial = [];
                // Is the value an array?
                if (Object.prototype.toString.apply(value) === '[object Array]') {
                    // The value is an array. Stringify every element. Use null as a placeholder
                    // for non-JSON values.
                    length = value.length;
                    for (i = 0; i < length; i += 1) {
                        partial[i] = str(i, value) || 'null';
                    }
                    // Join all of the elements together, separated with commas, and wrap them in
                    // brackets.
                    v = partial.length === 0 ? '[]' :
                    gap ? '[\n' + gap +
                    partial.join(',\n' + gap) + '\n' +
                    mind + ']' :
                    '[' + partial.join(',') + ']';
                    gap = mind;
                    return v;
                }
                // Iterate through all of the keys in the object.
                for (k in value) {
                    if (Object.hasOwnProperty.call(value, k)) {
                        v = str(k, value);
                        if (v) {
                            partial.push(quote(k) + (gap ? ': ' : ':') + v);
                        }
                    }
                }
                // Join all of the member texts together, separated with commas,
                // and wrap them in braces.
                v = partial.length === 0 ? '{}' :
                gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
                mind + '}' : '{' + partial.join(',') + '}';
                gap = mind;
                return v;
        }
    };
    // Make a fake root object containing our value under the key of ''.
    // Return the result of stringifying the value.
    return str('', {
        '': value
    });
}

