// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.3/standard/wwwroot/js/standard/multiselect.js $ 
// $Date: 27-04-07 10:53 $
// $Revision: 1 $ 
// $Author: Nsm $
// =============================================================== 

// Author PDL checkin 2003/03/17

var updateFlag = false;

function fetch(url,top)
{
    var frame;
    if (top)
    {
        parent.showWaitingBox();
        frames['redirectiframe'].location.href = url;
    }
    else
    {
        frame = self;
        frame.location = url;
    }
}

function getSearchText(maxNo)
{
    var result = "";
    for (i=0;i<maxNo;i++)
    {
        var listBox = getElement("kode"+i);
        var idx = listBox.selectedIndex;
        var text = listBox.options[idx].text;
        result+= ((result.length>0) ? " " : "") +text;
    }
    return result;
}

function selfields(maxNo,loc)
{
    if (updateFlag)
        return;
    for (i=0;i<maxNo;i++)
    {
       var listBox = getElement("kode"+i);
       var nameAttribute = listBox.name;
       var name;
       idx = listBox.selectedIndex;
       if (idx>=0)
       {
           name = encodeURIComponent(listBox.options[idx].value);
           var inputBox = getElement("input"+i);
           if (inputBox&&(idx>=0))
               inputBox.value = listBox.options[idx].text;
           loc += "&"+nameAttribute+"="+name;
       }   
    }
    loc += "&sessionid="+getFormParam("sessionid");
    document.location = loc;
}

function showChosen(elemName,box)
{
    updateFlag = true;
    var inputBox = getElement(elemName);
    var idx = box.selectedIndex;
    if (idx>=0)
    {
       var value = box.options[idx].text;
       /*var value_idx = value.split("#");
       value = value_idx[0];
       */

       inputBox.value = value;
    }
    updateFlag = false;
}

function listFind(event, textBox, listBox)
{
    var startOf = textBox.value;
    if (startOf == "")
        return;

    startOf = startOf.toUpperCase();

    var list = getElement(listBox);
    var nOptions = list.options.length;

    if (nOptions == 0)
        return;

    var i;
    var options = list.options;

    for (i=0;i<nOptions;i++)
    {
      if (options[i].text.toUpperCase().indexOf(startOf) == 0)
      {
         list.selectedIndex = i;
         return;
      }
    }
}

function doitright(strurl, listBox, colIdx, top, maxNo)
{
    var e = getElement(listBox);
    var idx = e.selectedIndex;
    if (idx<0)
        return;

    var values = e[idx].value;
    setCookie ("searchtext",getSearchText(maxNo));
    var value_idx = values.split("#");
    var value = value_idx[colIdx];
		strurl = completeURL(strurl, value);
    //url += "&sq_shapeindex="+value;
    //url += "&shapeindex="+value;
    //url += "&queryname="+getUrlParam("queryname");
    //url += "&spatialquery="+getUrlParam("spatialquery");
    //url += "&sessionid="+getFormParam("sessionid");

    if(top)
       fetch( strurl, true ); // virker til find
    else
       fetch( strurl, false ); // virker til hgfm
}

function getrighturl(strurl, listBox, colIdx, maxNo)
{
    var e = getElement(listBox);
    var idx = e.selectedIndex;
    if (idx<0)
        return;

    var values = e[idx].value;
    setCookie ("searchtext",getSearchText(maxNo));
    var value_idx = values.split("#");
    var value = value_idx[colIdx];
		return completeURL(strurl, value);
}

function completeURL(strurl, value){
		var newURL;
		newURL = strurl;
    newURL += "&sq_shapeindex="+value;
    newURL += "&shapeindex="+value;
    newURL += "&queryname="+getUrlParam("queryname");
    newURL += "&spatialquery="+getUrlParam("spatialquery");
    newURL += "&sessionid="+getFormParam("sessionid");
    return  newURL;
}

function listlink(url)
{
  var listurl = document.location.href;
  url += "&listurl="+listurl;
  self.location = url;
}

function doitNoRightMenu(url, listBox, colIdx, loadMenu, maxNo)
{
    var e = getElement(listBox);
    var idx = e.selectedIndex;

    if (idx<0)
        return;
    var values = e[idx].value;
    var searchtext = getSearchText(maxNo);

    var str;
    var value_idx = values.split("#");
    var value = value_idx[colIdx];
    url += "&sq_shapeindex="+value;
    url += "&shapeindex="+value;
    url += "&searchtext="+searchtext;
    //url += "&queryname="+getUrlParam("queryname");
    url += "&spatialquery="+getUrlParam("spatialquery");
    url += "&sessionid="+getFormParam("sessionid");
    fetch(url);

}



function getUrlParam(name)
{

    var str = location.search.substring(1, location.search.length);
    var params = str.split("&");

    for (i=0; i<params.length; i++)
    {
        var varval = params[i].split("=");
        if (varval[0] == name) return varval[1];
    }
    return "";
}

function getFormParam(name)
{

    var elem = document.forms[0].elements[name];
    return elem.value;
}

function loadPage(w,h,focusNo,maxNo)
{
    //if (w > 0)
    //    window.resizeTo( w,h );

    updateFlag = true;
    for (var i=0;i<maxNo;i++)
    {
       var inputBox = getElement("input"+i);
       inputBox.disabled = i>focusNo;
    }
    for (var i=0;i<maxNo;i++)
    {
       var listBox = getElement("kode"+i);
       var field = getUrlParam(listBox.name);
       var inputBox = getElement("input"+i);
       if (!field)
           break;
       idx = listBox.selectedIndex;
       if (inputBox&&(idx>=0))
       {
           inputBox.value = listBox.options[idx].text;
       }
    }
    updateFlag = false;

    var e = getElement("input"+focusNo);
    if (e)
        e.focus();
}

function cancelSearch()
{
    var cookie = getCookie("beforesearch");
    fetch(cookie);
}

function submitenter(myfield,e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13)
    {
        myfield.form.submit();
        return false;
    }
    else
        return true;
}

