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

//var general_url=site_url+"/admin.php";

var general_url="/index.cgi"

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;
}

function simpleRequest(str)
{
	var xhr=hrequest();
	if (!xhr) return "";
	var m=g('monitor') && g('monitor').checked;
	try {
		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 "";
	}
}

var return_value;

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]);
	}
	//alert(str);
	var rc=simpleRequest(str);
	//alert(rc);
	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;
}

function cpress(a)
{
	return a.replace(/^\s+/g,'').replace(/\s+$/g,'').replace(/\s+/g,' ');
}

function trim(a)
{
	return a.replace(/^\s+/g,'').replace(/\s+$/g,'')
}



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;
}

function t(a)
{
	return document.createTextNode(a);
}

function g(a)
{
	return document.getElementById(a);
}

function changeInnerString(el,str)
{
	while (el.firstChild) el.removeChild(el.firstChild);
	el.appendChild(t(str));
}

/* wartosc */

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 (typ.checked) {
				if (typ.value) return typ.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 "";
}

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;
	}
	
}

function is_yes(c)
{
	if (!c) return false;
	c=c.charAt(0).toLowerCase();
	if (c=='t' || c=='y') return true;
	return false;
}


function orphan(id)
{
	var d=g(id);
	if (d) d.parentNode.removeChild(d);
}

function clear_node(el)
{
	if (typeof el == "string") el=g(el);
	while (el.firstChild) el.removeChild(el.firstChild);
}

var popup_window;
var popup_data;
var popup_return_value;

function open_dialog(url,w,h)
{
	if (popup_window) try {
		popup_window.close();
	} catch(e) {};
	if (window.showModalDialog) return window.showModalDialog(
		url,
		null,(jestem_misio?"dialogWidth: ":"dialogwidth: ")+w+
			(jestem_misio?"dialogHeight: ":"; dialogheight: ")+h+
			"; scroll: yes; center: yes; resizable: yes");
	else popup_window=window.open(url+"?x="+Math.random(),
		"_blank",
		"width="+w+",height="+h+",scrollbars=yes");
}

function icon_url(id,w,h,m,b)
{
	var str="/obrazek.php?id="+id;
	if (w) str+='&w='+w;
	if (h) str+='&h='+h;
	if (m) str+='&mode='+m;
	if (b) str+='&bg='+m;
	return str;
}

function parentOfType(ob,n)
{
	while (ob=ob.parentNode) if (ob.nodeName.toLowerCase() == n) return ob;
	return null;
}

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;
}


function focus_keynext(ev,id)
{
	if (ev.keyCode ==10 || ev.keyCode==13) {
		var el=g(id);
		if (el) el.focus();
		return false;
	}
	return true;
}

function button_keynext(ev,id)
{
	if (ev.keyCode ==10 || ev.keyCode==13) {
		var el=g(id);
		if (el && el.onclick) el.onclick(ev);
		return false;
	}
	return true;
	
}


function node_string(node)
{
	if (node.firstChild) {
		return node.firstChild.nodeValue;
	}
	return '';
}

function create_row(id)
{
	var tr=c('tr','id','row_'+id);
	var i,td;
	for (i=1;i<arguments.length;i+=2) {
		td=c('td','class',arguments[i]);
		if (arguments[i+1]) {
			td.appendChild(t(arguments[i+1]));
		}
		tr.appendChild(td);
	}
	return tr;
}

function create_button_thid(label,id,fun,ob)
{
	var but=c('input','type','button','value',label);
	but.onclick=function()
	{
		fun(but,id);
	}
	if (ob) ob.appendChild(but);
	return but;
}
function create_button_thid2(label,id,id_2,fun,ob)
{
	var but=c('input','type','button','value',label);
	but.onclick=function()
	{
		fun(but,id,id_2);		
	}
	if (ob) ob.appendChild(but);
	return but;
}
function create_button_thid3(label,id,id_2,id_3,fun,ob)
{
	var but=c('input','type','button','value',label);
	but.onclick=function()
	{
		fun(but,id,id_2,id_3);		
	}
	if (ob) ob.appendChild(but);
	return but;
}

var editor_params;
function open_editor(ob,id,klasa,input_id)
{
	var td=getTdByClass(ob,klasa);
	editor_params={text_id:input_id,text_td:td};
	window.open("admin.php?page=editor&id="+id,"_blank","width=800,scrollbars=yes,location=no");
}

function open_editor2(ob,id,klasa,input_id)
{
	var span=document.getElementById(klasa+'_'+id);
	editor_params={text_id:input_id,text_td:span};
	window.open("admin.php?page=editor&id="+id,"_blank","width=800,scrollbars=yes,location=no");
}

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
    });
}


function prompt_i18n(msg,values,allowempty)
{
	var w=400;
	var h=150;
	return window.showModalDialog(
		'multidialog.html',
		{msg:msg,val:values,allowempty:allowempty},
		(jestem_misio?"dialogWidth: ":"dialogwidth: ")+w+
			(jestem_misio?"dialogHeight: ":"; dialogheight: ")+h+
			"; scroll: yes; center: yes; resizable: yes");
}

function get_pl(values)
{
	if (values['pl']) return values['pl'];
	var i;
	for (i in values) {
		if (values[i]) return values[i];
	}
	return '';
}

