//-----------------------------------------------------------------------------
// google map app
var gapp;
var req;
var defLat;
var defLng;
var defZom;
if (defLat == '') {defLat = 0; }
if (defLng == '') {defLng = 0; }
if (defZom == '') {defZom = 2; }
function load() {
    gapp = new MyGmap();
}
function MyGmap() {
    if (GBrowserIsCompatible()) {
        this.recton = false;
        this.rectfx = 0;
        this.rectfy = 0;
        this.objRect = [];
        this.objLine = new GPolyline(this.objRect, "#FF0000");
        this.gmap = new GMap2(document.getElementById("gmap"));
        this.gmap.setCenter(new GLatLng(defLat, defLng), defZom);
        this.gmap.addControl(new GLargeMapControl());
        this.gmap.addControl(new GMapTypeControl());
        GEvent.bind(this.gmap, "click", this, this.onMapClick);
    }
}

MyGmap.prototype.drawRect = function(px1, px2, py1, py2) {
    var width = px2 - px1;
    var pxc = px1;
    if (width < 0) {width = width + 360; }
    if (width > 180) {pxc = pxc + width / 2; }
    this.objRect = [new GPoint(px1, py1), new GPoint(px1, py2),
                    new GPoint(pxc, py2),
                    new GPoint(px2, py2), new GPoint(px2, py1),
                    new GPoint(pxc, py1),
                    new GPoint(px1, py1)];
//    this.gmap.removeOverlay(this.objLine);
    this.objLine = new GPolyline(this.objRect, "#FF0000");
    this.gmap.addOverlay(this.objLine);

//// This Code brings user into confusion if he/she clicks right edge first.
//    var lng = (px1 + px2) / 2;
//    var lat = (py1 + py2) / 2;
//    if (px1 > px2) {lng = lng + 180; }
//    this.gmap.setCenter(new GLatLng(lat, lng));
    if (document.getElementById("searchmap")) {
        if (document.getElementById("searchmap").value == '0') {
            document.getElementById("searchmap").value = '1';
        }
    }
    setShowPos();
}
MyGmap.prototype.onMapClick = function(marker, point) {
    if(point == undefined) { // click on marker
        return;
    }

    var recttext = '';
    var rectx = point.x;
    var recty = point.y;
    if (this.recton) { // second click
        setRectxy(this.rectfx, this.rectfy, rectx, recty);
        this.drawRect(this.rectfx, rectx, this.rectfy, recty);
        this.recton = false;
    } else {
        setRectxy(point.x, point.y, 1000, 1000);
        this.rectfx = point.x;
        this.rectfy = point.y;
        this.recton = true;
	this.gmap.removeOverlay(this.objLine);
	clearShowPos();
	setShowPos1();
    }
}
function setShowPos() {
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    if (x1 < 0) {document.getElementById("xp1").innerHTML = "W"; }
    else        {document.getElementById("xp1").innerHTML = "E"; }
    if (x2 < 0) {document.getElementById("xp2").innerHTML = "W"; }
    else        {document.getElementById("xp2").innerHTML = "E"; }
    if (y1 < 0) {document.getElementById("yp1").innerHTML = "S"; }
    else        {document.getElementById("yp1").innerHTML = "N"; }
    if (y2 < 0) {document.getElementById("yp2").innerHTML = "S"; }
    else        {document.getElementById("yp2").innerHTML = "N"; }
    document.getElementById("xv1").innerHTML = getRounded(Math.abs(x1));
    document.getElementById("xv2").innerHTML = getRounded(Math.abs(x2));
    document.getElementById("yv1").innerHTML = getRounded(Math.abs(y1));
    document.getElementById("yv2").innerHTML = getRounded(Math.abs(y2));
}
function setShowPos1() {
    var x1 = document.getElementById("x1").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    if (x1 < 0) {document.getElementById("xp1").innerHTML = "W"; }
    else        {document.getElementById("xp1").innerHTML = "E"; }
    if (y1 < 0) {document.getElementById("yp1").innerHTML = "S"; }
    else        {document.getElementById("yp1").innerHTML = "N"; }
    document.getElementById("xv1").innerHTML = getRounded(Math.abs(x1));
    document.getElementById("yv1").innerHTML = getRounded(Math.abs(y1));
}
function clearShowPos() {
    document.getElementById("xv1").innerHTML = "";
    document.getElementById("xv2").innerHTML = "";
    document.getElementById("yv1").innerHTML = "";
    document.getElementById("yv2").innerHTML = "";
}
function getRounded(value) {
    return Math.round(value * 1000) / 1000;
}
function setRectForm() {
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    if ((x1 != "") && (x2 != "") && (y1 != "") && (y2 != "")) {
        gapp.drawRect(x1, x2, y1, y2);
    }
}
function getMoveOnLat() {
    var size = gapp.gmap.getBounds();
    var sw = size.getSouthWest();
    var ne = size.getNorthEast();
    return (ne.lat() - sw.lat()) / 20.0;
}
function getMoveOnLng() {
    var size = gapp.gmap.getBounds();
    var sw = size.getSouthWest();
    var ne = size.getNorthEast();
    var move = ne.lng() - sw.lng();
    if (move < 0) {move = (move + 360) / 20.0; }
    else {move = move / 20.0; }
    return move;
}
function checkRectSet(x1, y1, x2, y2) {
    var tmpVal;
    var xo1 = document.getElementById("x1").value - 0;
    var xo2 = document.getElementById("x2").value - 0;
    if (xo1 > xo2) {x2 = x2 + 360; }
    if ((x2 - x1) > 360) {return; }
    if (y1 < y2) {return; }
    if (y1 > 90.0) {y1 = 90.0; }
    if (y1 < -90.0) {return; }
    if (y2 < -90.0) {y2 = -90.0; }
    if (y2 > 90.0) {return; }
    if (x1 > 180) {x1 = x1 - 360; }
    if (x2 > 180) {x2 = x2 - 360; }
    if (x1 < -180) {x1 = x1 + 360; }
    if (x2 < -180) {x2 = x2 + 360; }
    gapp.gmap.removeOverlay(gapp.objLine);
    setRectxy(x1, y1, x2, y2);
    setRectForm();
}
function moveRectNorth() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y1 += move;
    y2 += move;
    checkRectSet(x1, y1, x2, y2);
}
function moveRectSouth() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y1 -= move;
    y2 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function moveRectWest() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x1 -= move;
    x2 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function moveRectEast() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x1 += move;
    x2 += move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeEastPlus() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x2 += move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeEastMins() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x2 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeWestPlus() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x1 += move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeWestMins() {
    var move = getMoveOnLng();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    x1 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeNorthPlus() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y1 += move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeNorthMins() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y1 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeSouthPlus() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y2 += move;
    checkRectSet(x1, y1, x2, y2);
}
function resizeSouthMins() {
    var move = getMoveOnLat();
    var x1 = document.getElementById("x1").value - 0;
    var x2 = document.getElementById("x2").value - 0;
    var y1 = document.getElementById("y1").value - 0;
    var y2 = document.getElementById("y2").value - 0;
    y2 -= move;
    checkRectSet(x1, y1, x2, y2);
}
function clearDat() {
    document.getElementById("x1").value = "";
    document.getElementById("y1").value = "";
    document.getElementById("x2").value = "";
    document.getElementById("y2").value = "";
    gapp.gmap.removeOverlay(gapp.objLine);
    gapp.recton = false;
}
function setRectxy(x1, y1, x2, y2) {
    document.getElementById("x1").value = Math.floor(x1*1000)/1000;
    document.getElementById("y1").value = Math.floor(y1*1000)/1000;
    if (x2 < 360) {
        document.getElementById("x2").value = Math.floor(x2*1000)/1000;
        document.getElementById("y2").value = Math.floor(y2*1000)/1000;
    } else {
        document.getElementById("x2").value = "";
        document.getElementById("y2").value = "";
    }
}
function setRectdat(rectdat) {
    document.getElementById("rectdat").value = rectdat;
    if (window.clipboardData) {
        clipboardData.setData("Text", rectdat);
    }
}
function addPoints () {
    if (req.readyState != 4) {
        return true;
    }
    var xmlDoc = req.responseXML;
    var markers = xmlDoc.documentElement.getElementsByTagName("marker");
    for (var cnt = 0; cnt < markers.length; cnt++) {
        createMarker(markers[cnt]);
    }
}
function createMarker(marker) {
    var gmarkopt = new Object;
    var gpoint = new GPoint(marker.getAttribute("lng"),
                        marker.getAttribute("lat"));
    gmarkopt.title = marker.getAttribute("name");
    var gmarker = new GMarker(gpoint, gmarkopt);
    GEvent.addListener(gmarker, "click", function () {
        gmarker.openInfoWindowHtml(createInfo(marker)); });
    gapp.gmap.addOverlay(gmarker);
}
function clearMarker () {
    gapp.gmap.clearOverlays();
}
function createInfo(marker) {
    var posId = marker.getAttribute("id");
    var posLng = marker.getAttribute("lng");
    var posLat = marker.getAttribute("lat");
    var posName = marker.getAttribute("name");
    var posDesc = GXml.value(marker);
    return '<div class="info">'+posDesc+'</div>';
}
function loadXml () {
    clearMarker();
    req = GXmlHttp.create();
    req.open("GET", "allslide.php", true);
    req.onreadystatechange = function () {addPoints(); }
    req.send(null);
}



//-----------------------------------------------------------------------------
// publ search result update
var reqPub;
function pubUpdateResult (id) {
    id = id - 1;
    reqPub = GXmlHttp.create();
    var opt = '';
    if (document.getElementById('figuremode').value == 'on') {
        opt = '&figuremode=on';
    }
    reqPub.open('GET', 'dispres.php?id=' + id + opt);
    reqPub.onreadystatechange = function () {pubUpdate(); }
    reqPub.send(null);
}
function pubUpdate() {
    if (reqPub.readyState != 4) {
        return true;
    }
    var xmlDoc = reqPub.responseXML;
    var datas = xmlDoc.documentElement.getElementsByTagName('res');
    var header = xmlDoc.documentElement.getElementsByTagName('head');
    var figs = xmlDoc.documentElement.getElementsByTagName('fig');
    var result = '<table>' + GXml.value(header[0]);
    var hash;
    for (var cnt = 0; cnt < datas.length; cnt++) {
        hash = datas[cnt].getAttribute('hash');
        result += GXml.value(datas[cnt]);
    }
    result += '</table>';
    document.getElementById('result').innerHTML = result;
    var id = '';
    for (var cnt = 0; cnt < figs.length; cnt++) {
        id += GXml.value(figs[cnt]) + ';';
    }
    pubUpdateDisp(id);
}
function pubUpdateClearOne(id) {
    if (reqSiteDisped.indexOf(id) != -1) {
        reqSiteDisped.replace(id + ',', '');
        document.getElementById('images' + id).innerHTML = '';
        document.getElementById('hide' + id).style.display = 'none';
        document.getElementById('show' + id).style.display = 'block';
    }
}
function pubUpdateClear(id) {
    var clearIds = reqPubDisped.split(',');
    for (var cnt in clearIds) {
        if (clearIds[cnt]) {
            document.getElementById('images' + clearIds[cnt]).innerHTML = '';
            document.getElementById('show' + clearIds[cnt]).style.display = 'block';
            document.getElementById('hide' + clearIds[cnt]).style.display = 'none';
        }
    }
    reqSiteDisped = '';
    document.getElementById('hover').style.display = 'none';
}
var reqPubDisp;
var reqPubDispId;
var reqPubDisped = '';
var reqPubDispQuery = '';
function pubUpdateDisp(id) {
    if (id.indexOf(';') != -1) {
        reqPubDispQuery = id.substr(id.indexOf(';') + 1);
        id = id.substr(0, id.indexOf(';'));
    }
    reqPubDispId = id;
    reqPubDisped += id + ',';
    reqPubDisp = GXmlHttp.create();
    reqPubDisp.open('GET', 'imglist.php?jhash=' + id);
    reqPubDisp.onreadystatechange = function () {pubUpdateDispQuery(); }
    reqPubDisp.send(null);
}
function pubUpdateDispQuery () {
    if (reqPubDisp.readyState != 4) {return true; }
    var xmlDoc = reqPubDisp.responseXML;
    var datas = xmlDoc.documentElement.getElementsByTagName('res');
    var result = '';
    var cid;
    for (var cnt = 0; cnt < datas.length; cnt++) {
        result += GXml.value(datas[cnt]);
    }
    document.getElementById('images' + reqPubDispId).innerHTML = result;
    document.getElementById('show' + reqPubDispId).style.display = 'none';
    document.getElementById('hide' + reqPubDispId).style.display = 'block';
    if (reqPubDispQuery != '') {pubUpdateDisp(reqPubDispQuery); }
}

//-----------------------------------------------------------------------------
// landslide search result update
var reqLs;
function lsUpdateResult (id) {
    id = id - 1;
    reqLs = GXmlHttp.create();
    reqLs.open('GET', 'dispres.php?id=' + id);
    reqLs.onreadystatechange = function () {lsUpdate(); }
    reqLs.send(null);
}
function lsUpdate() {
    if (reqLs.readyState != 4) {
        return true;
    }
    var xmlDoc = reqLs.responseXML;
    if (!xmlDoc || !xmlDoc.documentElement){
	return true;
    }
    var datas = xmlDoc.documentElement.getElementsByTagName('res');
    var header = xmlDoc.documentElement.getElementsByTagName('head');
    var result = '<table>' + GXml.value(header[0]);
    var hash;
    for (var cnt = 0; cnt < datas.length; cnt++) {
        hash = datas[cnt].getAttribute('hash');
        result += GXml.value(datas[cnt]);
    }
    result += '</table>';
    document.getElementById('result').innerHTML = result;
}
function siteUpdateClearOne(id) {
    if (reqSiteDisped.indexOf(id) != -1) {
        reqSiteDisped.replace(id + ',', '');
        document.getElementById('images' + id).innerHTML = '';
        document.getElementById('hide' + id).style.display = 'none';
        document.getElementById('show' + id).style.display = 'block';
    }
}
function siteUpdateClear() {
    var clearIds = reqSiteDisped.split(',');
    for (var cnt in clearIds) {
        if (clearIds[cnt]) {
            document.getElementById('images' + clearIds[cnt]).innerHTML = '';
            document.getElementById('show' + clearIds[cnt]).style.display = 'block';
            document.getElementById('hide' + clearIds[cnt]).style.display = 'none';
        }
    }
    reqSiteDisped = '';
    document.getElementById('hover').style.display = 'none';
}
var reqSiteDisp;
var reqSiteDispId;
var reqSiteDisped = '';
function siteUpdateDisp(id) {
    reqSiteDispId = id;
    reqSiteDisped += id + ',';
    reqSiteDisp = GXmlHttp.create();
    reqSiteDisp.open('GET', 'imglist.php?jhash=' + id);
    reqSiteDisp.onreadystatechange = function () {siteUpdateDispQuery(); }
    reqSiteDisp.send(null);
}
function siteUpdateDispQuery () {
    if (reqSiteDisp.readyState != 4) {return true; }
    var xmlDoc = reqSiteDisp.responseXML;
    var datas = xmlDoc.documentElement.getElementsByTagName('res');
    var result = '';
    var cid;
    result += '<table>';
    result += '<tr>';
    for (var cnt = 0; cnt < datas.length; cnt++) {
        if (cnt % 5 == 0){
           result += '</tr><tr>';
        }
        result += '<td valign="bottom">';
        result += GXml.value(datas[cnt]);
        result += '</td>';
    }
    result += '</tr>';
    result += '</table>';
    document.getElementById('images' + reqSiteDispId).innerHTML = result;
    document.getElementById('show' + reqSiteDispId).style.display = 'none';
    document.getElementById('hide' + reqSiteDispId).style.display = 'block';
}

//-----------------------------------------------------------------------------
// text search update
var selectList = '';
var selectEqu = '';
var selectAndOr = '<option value="and">and</option><option value="or">or</option>';
var reqText;
function loadForm() {
    document.getElementById('secnd').value = '';
    reqText = GXmlHttp.create();
    reqText.open("GET", "allsecond.php", true);
    reqText.onreadystatechange = function () {loadFormInit(); }
    reqText.send(null);
}
function loadFormInit() {
    if (reqText.readyState != 4) {
        return true;
    }
    var xmlDoc = reqText.responseXML;
    var conds = xmlDoc.documentElement.getElementsByTagName("cond");
    for (var cnt = 0; cnt < conds.length; cnt++) {
        selectList += '<option value="' + conds[cnt].getAttribute('id') + '">';
        selectList += conds[cnt].getAttribute('name') + '</option>';
    }
    var equ = xmlDoc.documentElement.getElementsByTagName("equ");
    selectEqu = GXml.value(equ[0]);
    addNewScn();
    searchRestoreFromString('new');
}
function addSelAndOr(name) {
    return '<select name="' + name + '" id="' + name + '">' + selectAndOr + '</select>';
}
function addNewScn() {
    var curAdd = '';
    var curAdvId = Math.floor(document.getElementById("advmax").value);
    if (curAdvId != 0) {curAdd += '<hr />'; }

    curAdd += '<div>';

    curAdd += '<div style="float: right;">';
    curAdd += '<span id ="add' + curAdvId + '">';
    curAdd += '<input type="button" onclick="searchStoreToString(); addNewScn(); searchRestoreFromString(\'up\');" value="Add new condition" />';
    curAdd += '</span>';
    curAdd += '<input type="button" onclick="searchStoreToStringDeleteAdv(' + curAdvId + ');" value="Delete this condition" />';
    curAdd += '</div>';
    var ordinal_sfx = 'th';
    if ( (curAdvId+1) % 10 ==1) {
        ordinal_sfx = 'st';
    }else if ( (curAdvId + 1) % 10 == 2) {
        ordinal_sfx = 'nd';
    }else if ( (curAdvId + 1) % 10 == 3) {
        ordinal_sfx = 'rd';
    }

    curAdd += '<span class="bold">' + (curAdvId + 1) + ordinal_sfx + " condition" + '</span>';
    if (curAdvId != 0) {
        curAdd += " / to prev condition";
        curAdd += addSelAndOr('priv' + curAdvId);
    }
    curAdd += '</div>';

    curAdd += '<br />';
    curAdd += '<div id="scn' + curAdvId + '"></div>';
    document.getElementById('secnd').innerHTML += curAdd;
    if (curAdvId != 0) {
        id = 'add' + (curAdvId - 1);
        document.getElementById(id).innerHTML = '';
    }
    curAdd = '<input type="hidden" name="advsec' + curAdvId + '" id="advsec';
    curAdd += curAdvId + '" value="0" />';
    document.getElementById("advsec").innerHTML += curAdd;
    addFormSecnd(curAdvId);
    curAdvId += 1;
    document.getElementById("advmax").value = curAdvId;
}
function retNewSelectNID (id) {
    return '<select name="' + id + '" id="' + id + '">';
}
function addFormSecndKeylist(id) {
    window.open('getlist.php?id=' + document.getElementById('cond' + id).value + '&pid=val' + id);
}
function addFormSecnd (id) {
    var curAdd = '';

    curAdd += '<div style="clear: right;">';
    curAdd += '<div style="float: left;">';
    var curSelectId = Math.floor(document.getElementById("advsec" + id).value);
    var curSId = id + '-' + curSelectId;
    if (curSelectId != 0) {curAdd += addSelAndOr('priv' + curSId); }
    curAdd += retNewSelectNID('cond' + curSId) + selectList + '</select>';
    curAdd += retNewSelectNID('equ' + curSId) + selectEqu + '</select>';
    curAdd += '<input type="text" size="30" name="val' + curSId + '" id="val' + curSId + '" />';
    curAdd += '</div>';

    curAdd += '<a class="click " href="#" onclick="addFormSecndKeylist(\'' + curSId + '\')">list</a>';
    curAdd += '<div style="float: right;">';
    curAdd += '<span id="add' + curSId + '">';
    curAdd += '<input type="button" onclick="searchStoreToString(); addFormSecnd(' + id + '); searchRestoreFromString(\'up\'); " value="Add"/>';
    curAdd += '</span>';
    curAdd += '<input type="button" onclick="searchStoreToStringDeleteSec(\'' + curSId + '\');" value="Delete"/>';
    curAdd += '</div>';
    curAdd += '</div>';
    curAdd += '<br />';
    document.getElementById('scn' + id).innerHTML += curAdd;
    if (curSelectId != 0) {
        var nid = curSelectId - 1;
        nid = 'add' + id + '-' + nid;
        document.getElementById(nid).innerHTML = '';
    }
    curSelectId += 1;
    document.getElementById("advsec" + id).value = curSelectId;
}
// store into strConds
function searchStoreToString () {
    var maxId = document.getElementById('advmax').value;
    strConds = searchSTSVals('advmax');
    for (var cid = 0; cid < maxId; cid++) {
        var curMax = document.getElementById('advsec' + cid).value;
        strConds += searchSTSVals('advsec' + cid);
        for (var ccid = 0; ccid < curMax; ccid++) {
            curCnd = cid + '-' + ccid;
            strConds += searchSTSVals('cond' + curCnd);
            strConds += searchSTSVals('equ' + curCnd);
            strConds += searchSTSVals('val' + curCnd);
            if (ccid > 0) {strConds += searchSTSVals('priv' + curCnd); }
        }
        if (cid > 0) {strConds += searchSTSVals('priv' + cid); }
    }
}
function searchStoreToStringDeleteAdv(id) {
    var maxId = document.getElementById('advmax').value;
    strConds = 'advmax=' + (maxId - 1) + ';';
    var cid = 0;
    for (var sid = 0; sid < maxId; sid++) {
        if (sid != id) {
            var curMax = document.getElementById('advsec' + sid).value;
            strConds += searchSTSValMul('advsec', cid, sid);
            for (var ccid = 0; ccid < curMax; ccid++) {
                curCnd = cid + '-' + ccid;
                surCnd = sid + '-' + ccid;
                strConds += searchSTSValMul('cond', curCnd, surCnd);
                strConds += searchSTSValMul('equ', curCnd, surCnd);
                strConds += searchSTSValMul('val', curCnd, surCnd);
                if (ccid > 0) {strConds += searchSTSValMul('priv', curCnd, surCnd); }
            }
            if (cid > 0) {strConds += searchSTSValMul('priv', cid, sid); }
            cid += 1;
        }
    }
    document.getElementById("secnd").innerHTML = "";
    document.getElementById("advsec").innerHTML = "";
    document.getElementById("advmax").value = 0;
    loadForm();
}
function searchStoreToStringDeleteSec(id) {
    var maxId = document.getElementById('advmax').value;
    strConds = searchSTSVals('advmax');
    var sid;
    var surCnd;
    var curAdv = id.substr(0, id.indexOf('-'));
    for (var cid = 0; cid < maxId; cid++) {
        var curMax = document.getElementById('advsec' + cid).value;
        if (curAdv == cid) {
            strConds += 'advsec' + cid + '=' + (curMax - 1) + ';';
        } else {
            strConds += 'advsec' + cid + '=' + curMax + ';';
        }
        sid = 0;
        for (var ccid = 0; ccid < curMax; ccid++) {
            curCnd = cid + '-' + ccid;
            surCnd = cid + '-' + sid;
            if (curCnd != id) {
                strConds += searchSTSValMul('cond', surCnd, curCnd);
                strConds += searchSTSValMul('equ', surCnd, curCnd);
                strConds += searchSTSValMul('val', surCnd, curCnd);
                if (ccid > 0) {strConds += searchSTSValMul('priv', surCnd, curCnd); }
                sid += 1;
            }
        }
        if (cid > 0) {strConds += searchSTSVals('priv' + cid); }
    }
    document.getElementById("secnd").innerHTML = "";
    document.getElementById("advsec").innerHTML = "";
    document.getElementById("advmax").value = 0;
    loadForm();
}
function searchSTSVals (id) {
    return id + '=' + document.getElementById(id).value + ';';
}
function searchSTSValMul (id, cid, sid) {
    return id + cid + '=' + document.getElementById(id + sid).value + ';';
}
// restore from strConds
function searchRestoreFromString (ccnd) {
    if(!strConds) return;
    while (strConds.length != 0) {
        strId = strConds.substr(0, strConds.indexOf('='));
        strConds = strConds.substr(strConds.indexOf('=') + 1);
        strKey = strConds.substr(0, strConds.indexOf(';'));
        strConds = strConds.substr(strConds.indexOf(';') + 1);
        cval = Math.floor(strKey);
        if (strId.substr(0, 6) == 'advmax') {
            if (ccnd == 'new') {
                for (var cid = 1; cid < cval; cid++) {addNewScn(); }
            }
        } else if (strId.substr(0, 6) == 'advsec') {
            if (ccnd == 'new') {
                cv = Math.floor(strId.substr(6));
                for (var cid = 1; cid < cval; cid++) {addFormSecnd(cv); }
            }
        } else {
            if (! document.getElementById(strId)) {
//                alert('restore error : no such ' + strId);
            } else {
                document.getElementById(strId).value = strKey;
            }
        }
    }
}
function searchRestoreFromStringGet (id) {
    // key=xxx;
    var strPos = strConds.indexOf(id);
    if (strPos == -1) {return ''; }
    strPos = strPos + id.length + 1;
    strCur = strConds.substr(strPos);
    strCur = strCur.substr(0, strCur.indexOf(';'));
    return strCur;
}


//-----------------------------------------------------------------------------
// google map app for getting sq. positions
function loadPos() {
    gapp = new MyGmapPos();
}
function MyGmapPos() {
    if (GBrowserIsCompatible()) {
        this.recton = false;
        this.rectfx = 0;
        this.rectfy = 0;
        this.objRect = [];
        this.objLine = new GPolyline(this.objRect, "#FF0000");
        this.gmap = new GMap2(document.getElementById("gmap"));
        this.gmap.setCenter(new GLatLng(defLat, defLng), defZom);
        this.gmap.addControl(new GLargeMapControl());
        this.gmap.addControl(new GMapTypeControl());
        GEvent.bind(this.gmap, "click", this, this.onMapClick);
    }
}
MyGmapPos.prototype.onMapClick = function(marker, point) {
    document.getElementById("x1").value = point.x;
    document.getElementById("y1").value = point.y;
}


function newSearch() {
    window.location.assign(window.location.href + '?actionnew=yes');
}

//-----------------------------------------------------------------------------
// switch search mode
function switchFigure() {
    var mode = document.getElementById("figuremode").value;
    if (mode == "off") {
        document.getElementById("figuremode").value = 'on';
        document.getElementById("figuresw").value = strModeSwToNormal;
    } else {
        document.getElementById("figuremode").value = 'off';
        document.getElementById("figuresw").value = strModeSwToFigure;
    }
}

function setFigure() {
    var mode = document.getElementById("figuremode").value;
    if (mode == "on") {
        document.getElementById("figuresw").value = strModeSwToNormal;
    } else {
        document.getElementById("figuresw").value = strModeSwToFigure;
    }
}

