var MS_LAYER_POINT = 0;
var MS_LAYER_LINE = 1;
var MS_LAYER_POLYGON = 2;
var MS_LAYER_RASTER = 3;
var MS_LAYER_ANNOTATION = 4;
var MS_LAYER_QUERY = 5;
var MS_LAYER_CIRCLE = 6;
var MS_LAYER_TILEINDEX = 7;

var w=null;

function lc_swapit(id,on) {
    ob=find_handle(id);
    on?ob.visibility="visible":ob.visibility="hidden"; 
}
function div_swapdisplay(id, force) {
    if (ob=find_handle(id)) {
        if (force == undefined) {
            on = ob.display == "block" ? "none" : "block";
        } else {
            on = force;
        }
        ob.display = on;
    }
}
                        
function find_object(id) {
    if (document.getElementById) { if (!(document.getElementById(id))) { return false; } return document.getElementById(id); } else return false; 
}
function find_handle(id) { 
    if (document.getElementById) { var fh=document.getElementById(id); if (!fh) { return false; } return fh.style; }
    else if (document.all) return document.all[id].style; return false; 
}
function popw(page,same) {
    winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=675,height=400";
    ShowFrame = window.open(page, 'pw'+same, winopts); 
}
function ww() {
    return document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent!=null) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) curtop += obj.y;
    return curtop;
}
// xmlreq ------------------------------------------------------------
gis_request=new Array();
gis_request_c=0;
function xmlreq(url,fresp) { 
    r_c=gis_request_c++; gis_request[r_c] = new xmlhttp_request (url, fresp, r_c);return r_c; }
function xmlhttp_request (url, e, id) {
    this.target = document; this.url = url; this.id = id; this.event = e; this.xmlhttp = false; this.method = "GET"; this.doRequest(); }
xmlhttp_request.prototype.init_xmlhttp = function () {
    try { this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { this.xmlhttp = false; } } if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') { this.xmlhttp = new XMLHttpRequest(); }
}
xmlhttp_request.prototype.doRequest = function () {
    if (!this.xmlhttp) { this.init_xmlhttp(); }
    //this.xmlhttp=false;
    if (this.xmlhttp) {
        var o = this;
        o.xmlhttp.open(this.method, this.url, true);
        o.xmlhttp.onreadystatechange = function() {
            if (o.xmlhttp.readyState==4) { 
              if (o.xmlhttp.status == 200) {
                o.response = o.xmlhttp.responseText; 
                if (o.response) { o.event(o.id,o); }
                else { var iid=(o.id && o.id=='sgy')?'sgy':o.id+1; lbf.location=baseurl+o.url+'&iframe='+iid; }
                } } }
        o.xmlhttp.send(null);
    }
    else { var iid=(this.id && this.id=='sgy')?'sgy':this.id+1; lbf.location=baseurl+this.url+'&iframe='+iid; }
}
// background sessions -----------------------------------------------
function gis_bsess(bsvar,bsval) {
    xmlreq('bsess.php?bsvar='+bsvar+'&bsval='+bsval,gis_noresp);
}
function gis_noresp() { }

function showLoginBox() {
    
    var o;
    if (o = find_handle("loginBox")) {
        o.display = 'block';
    }
    if (o = find_handle("loginLink")) {
        o.display = 'none';
    }
}
function showRegisterBox() {
    var o;
    var t = 100;
    if (o = find_handle('layoutFrame')) {
        t = o.top == '' ? '100px' : o.top;
    }
    if (o = find_object("registerBox")) {
        var ww = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
        var l = parseInt((ww-400)/2);
        o.style.left = l+'px';
        o.style.top = t;
        o.style.display = 'block';
    }
}
function registerSubmit() {
    var fields = ['username', 'password', 'email', 'fullname'];
    var e = false;
    var params = '';
    var o;
    if (o = find_object("regerr")) {
        o.style.display = 'none';
    }

    for(var i in document.registerform.elements) {
        if((document.registerform.elements[i].type == 'text')) {
            if (document.registerform.elements[i].value.match(/^\s*$/)) {
                e = true;
            } else {
                params += '&'+document.registerform.elements[i].name+'='+document.registerform.elements[i].value;
            }
        }
    }
    if (e) {
        if (o) {
            o.innerHTML = lrs['regerr_empty'];
            o.style.display = 'block';
        }
    } else {
        if (document.registerform.password.value != document.registerform.password2.value || document.registerform.password.value.match(/^\s*$/)) {
            e = true;
        } else {
            params += '&password='+document.registerform.password.value+'&password2='+document.registerform.password2.value;
        }
        if (e) {
            if (o) {
                o.innerHTML = lrs['regerr_password'];
                o.style.display = 'block';
            }
        } else {
            call('admin/x_register.php?action=register'+params, this, registerSubmit_resp);
        }
    }
}
function showMessage(code) {
    alert(lrs[code]);
}
function registerSubmit_resp(resp) {
    var aresp = resp.split('|');
    var o;
    if (aresp[0] == 'ok') {
        if (o = find_object('regerr')) {
            o.innerHTML = lrs['regerr_success'];
            o.style.display = 'block';
        }
        if (o = find_object('registerForm')) {
            o.style.display = 'none';
        }
    } else {
        if (o = find_object('regerr')) {
            o.innerHTML = lrs['regerr_'+aresp[0]];
            o.style.display = 'block';
        }
    }
}

function in_array (item, a) {
    for (var i=0;i<a.length;i++) {
        if (a[i] == item) return true;
    }
    return false;
}
function popwindow(page,same, width, height) {
  if (!width) {width=500;}
  if (!height) {height=600}
  winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height;
  return window.open(baseurl+page, 'pw'+same, winopts); 
}
function popwindowdirect(page,same, width, height) {
  if (!width) {width=500;}
  if (!height) {height=600}
  winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height;
  return window.open(page, 'pw'+same, winopts); }

function onAddressClick(x,y,id)
{
    w=window.open("","ka_map");
    var timeout=1;
    if (w.location.href == "about:blank")
    {
        w.location.href= "../index.php";
        timeout=15000;
    }

    w.focus();
    
    w.setTimeout("myAjaxExecute('manufaktura/message.php?x="+x+"&y="+y+"&object_id="+id+"&action=edit',myMessageCallback);",timeout);
    w.focus();
    
    return false;
}
var mapHandle = null;
function openMap(o) {
    if (mapHandle != null) {
        try {
            var map = mapHandle.myKaMap;
            if (map == undefined) mapHandle = null;
        } catch(e) {
            mapHandle = null;
        }
    }
    if (!mapHandle) {
        mapHandle = popwindowdirect(o.href, 'mapwindow',900, 600);
    } else {
        if (o.href.search(new RegExp("\\?(.+)$"))) {
            var sp = RegExp.$1;
            var p = sp.split('&');
            var qp = new Array();
            qp['map'] = '';
            qp['layers'] = '';
            qp['cps'] = '';
            for (var i=0;i<p.length;i++) {
                var q=p[i].split('=');
                qp[q[0]]=q[1];
            }
            mapHandle.setMapPosition(qp['map'], qp['layers'], qp['cps']);
        }
    }
    return false;

}
function setCookie( name, value, expires, path, domain, secure ) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    //expires: days
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
        ( ( path ) ? ";path=" + path : "" ) + 
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}
function getCookie( check_name ) {
    //first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ ) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name ) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 ) {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found ) {
        return null;
    }
}
function deleteCookie( name, path, domain ) {
    if ( getCookie( name ) ) {
        document.cookie = name + "=" +
            ( ( path ) ? ";path=" + path : "") +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}
var jsToLoad = 0;
var loaded = '';
function dhtmlLoadScripts(jsurls, cssurls, altcssurls) {
    if (cssurls == undefined) cssurls = "";
    if (altcssurls == undefined) altcssurls = "";
    var aurls = jsurls.split('::');
    for (var i in aurls) {
        var url = aurls[i];
        if (url != '') {
            var e = document.createElement("script");
            e.src = url;
            e.type="text/javascript";
            document.getElementsByTagName("head")[0].appendChild(e);
            if (e.addEventListener)
                e.addEventListener('load', function() {jsToLoad--;}, false);
            else
                e.onreadystatechange = function() {
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    jsToLoad--;
                }
            }
            jsToLoad++;
        }
    }
    aurls = cssurls.split('::');
    for (var i in aurls) {
        var url = aurls[i];
        if (url != '') {
            var e = document.createElement("link");
            e.setAttribute("rel", "stylesheet");
            e.setAttribute("type", "text/css");
            e.setAttribute("href", url);
            document.getElementsByTagName("head")[0].appendChild(e);
        }
    }
    aurls = altcssurls.split('::');
    for (var i in aurls) {
        var url = aurls[i];
        if (url != '') {
            var e = document.createElement("link");
            e.setAttribute("rel", "alternate stylesheet");
            e.setAttribute("type", "text/css");
            e.setAttribute("media", "screen");
            e.setAttribute("title", url);
            e.setAttribute("href", url);
            document.getElementsByTagName("head")[0].appendChild(e);
        }
    }
    initDHTMLAPI();
    drawPage();
    checkJsCssLoaded();
}
function checkJsCssLoaded() {
    if (jsToLoad) {
        setTimeout('checkJsCssLoaded()', 1000);
    } else {
        myOnLoad();
    }
}
