var _is_map_auto_size = false;
var _is_new_session = false;
var _tab_index = 1;

function init_variables(is_map_auto_size, is_new_session, tab_index) {
     _is_map_auto_size = is_map_auto_size;
     _is_new_session = is_new_session;
     _tab_index = tab_index;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<a.length-2;i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function onResize() {
   if (_is_map_auto_size && _tab_index == 1) {
         submit();
   }
}

function onLoadFit(what,setting) {
   if (_is_new_session == true) {
      //TODO: kims.setIsNewSession(false);
      change(what,setting);
   }
}

function change(what,setting) {
   document.forms["view"].what.value = what;
   document.forms["view"].setting.value = setting;
   submit("view");
}

function submit(whichForm) {
   document.forms[whichForm].submit();
}
//function openLogIn() {
//    openDialog('login.jsp?init=true', 320, 170);
//}

var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))
var dialogWin = new Object()
// Generate a modal dialog.
function openDialog(url, width, height, scrolls) {
    delay();
    if (! dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
        dialogWin.url = url
        dialogWin.width = width
        dialogWin.height = height
        // Keep name unique so Navigator doesn't overwrite an existing dialog.
        dialogWin.name = ( new Date()).getSeconds().toString()

        // Assemble window attributes and try to center the dialog.
        if (Nav4) {
            dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width) / 2)
            dialogWin.top = window.screenY + ((window.outerHeight - dialogWin.height) / 2)
            var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",resizable=no,scrollbar=yes,width="
                    + dialogWin.width + ",height=" + dialogWin.height
        } else {
            dialogWin.left = (screen.width - dialogWin.width) / 2
            dialogWin.top = (screen.height - dialogWin.height) / 2
            var attr = "left=" + dialogWin.left + ",top=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width
                    + ",height=" + dialogWin.height;

            if (scrolls != null)
                attr += ",scrollbars,resizable=yes"
            else
                attr += ",resizable=no"
        }

        //     if(attrs!=null)
        //dialogWin.win=window.open(dialogWin.url,dialogWin.name,attrs)
        // ',resizable,scrollbars'
        //else
        dialogWin.win = window.open(dialogWin.url, dialogWin.name, attr)
        dialogWin.win.focus();
    }

}

// Event handler to inhibit Navigator form element and IE link activity when dialog window is active.
function deadend() {
    if (dialogWin.win && ! dialogWin.win.closed) {
        dialogWin.win.focus()
        return false
    }
}

// Since links in IE4 cannot be disabled, preserve IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links for IE.
function disableForms() {
    IELinkClicks = new Array()

    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = true
            }
        }

        IELinkClicks[h] = new Array()

        for (i = 0; i < frames[h].document.links.length; i++) {
            IELinkClicks[h][i] = frames[h].document.links[i].onclick
            frames[h].document.links[i].onclick = deadend
        }
    }
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = false
            }
        }

        for (i = 0; i < frames[h].document.links.length; i++) {
            frames[h].document.links[i].onclick = IELinkClicks[h][i]
        }
    }
}

// Grab all Navigator events that might get through to form elements while dialog is open. For IE, disable form elements.
function blockEvents() {
    if (Nav4) {
        window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onclick = deadend
    } else {
        disableForms()
    }

    window.onfocus = checkModal
}

// As dialog closes, restore the main window's original event mechanisms.
function unblockEvents() {
    if (Nav4) {
        window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onclick = null
        window.onfocus = null
    } else {
        enableForms()
    }
}

function checkModal() {
    setTimeout("finishChecking()", 50)
    return true
}

function finishChecking() {
    if (dialogWin.win && ! dialogWin.win.closed) {
        dialogWin.win.focus()
    }
}

function delay() {
   setTimeout("nothing();", 100);
}
function nothing() {
   var i=0;
   i++;
}
function openLayerBrowser(cnt) {
    initsize = 110;

    if (cnt > 3)
        initsize += ((cnt - 3) * 13.5);

    openDialog('LayerBrowser.jsp', 435, initsize, 'scroll');
}

function openLayerDisplayControl(cnt) {

   initsize = 110;
   if (cnt > 3) initsize += ((cnt - 3) * 16.5);
   openDialog('layer_display_control.jspx', 375, initsize, 'scroll');
}

function changeChartDef() {
    str = document.view.CHART_DEFINITION.options[document.view.CHART_DEFINITION.selectedIndex].value;
				//document.view.CHART_DEFINITION.selectedIndex;// + ","
              //+ document.view.CHART_DEFINITION.options[document.view.CHART_DEFINITION.selectedIndex].value;
    change('LOAD_CHART', str);
}
function openRegister() {
    //alert();
    if (dialogWin.win != null)
        dialogWin.win.close();

    dialogWin = new Object();
    //document.view.submit();
    openRegPage();
}

function openRegPage() {
    openDialog('register.jsp', 500, 290);
}
function pointIdentify() {
    openDialog('point_identify.jspx', 500, 290, 'scroll');
}function changeLayerVisible(what, setting) {
  if(setting!="")
     change(what, setting);

}
function openMapSetSize() {
    openDialog('set_map_size.jspx?init=true', 300, 150);
}

function openDatasetExtentMenu(sSectionID)
{
	var eHeader = document.getElementById(sSectionID);
	var eSection = document.getElementById(sSectionID + "_body");
    eHeader.className = "datasetExtentOpen";
    eSection.style.display = "";
//    saveCookie(sSectionID, "on", 365);
}

function closeDatasetExtentMenu(sSectionID)
{
	var eHeader = document.getElementById(sSectionID);
	var eSection = document.getElementById(sSectionID + "_body");

    eHeader.className = "datasetExtentClosed";
    eSection.style.display = "none";
//    saveCookie(sSectionID, "off", 365);

}

function toggleDatasetExtentMenu(sSectionID)
{
	var eHeader = document.getElementById(sSectionID);
	if(eHeader.className == "datasetExtentOpen")
        closeDatasetExtentMenu(sSectionID);
	else
        openDatasetExtentMenu(sSectionID);

/*if (readCookie(sSectionID, 'on') == "on")
	{
	    closeDatasetExtentMenu(sSectionID);
	}
	else
	{
        openDatasetExtentMenu(sSectionID);
	}*/
}

function openLayerDisplay(cnt) {
   initsize = 110;
   if (cnt > 3) initsize += ((cnt - 3) * 16.5);
   openDialog('layer_display_control.jspx', 375, initsize, 'scroll');
}
function editMapLegend() {
   openDialog('layer_display_control.jspx', 375, 500, 'scroll');
}
function changeAxis(what, setting) {
   if(setting != "-1")
      change(what, setting);
}
function openClientMap() {
   openDialog('clientmap.jspx', 475, 500, 'scroll');
}
function changeLanguage(what, setting) {
  if(setting != "fr") {
     change(what, setting);
  } else {
     alert("Under Construction!");
  }
}
function toggleNotesMenu(sSectionID) {
    idx = document.forms["view"].metadata_editor_index.value;
    if(idx != "1") {
        if (readCookie(sSectionID) != 'headerOpened')
            change("SET_METADATA_INDEX","1");
    }
    toggleMenu(sSectionID);
    hideAllExcept(sSectionID);
}
function toggleSourcesMenu(sSectionID) {
    idx = document.forms["view"].metadata_editor_index.value;
    if(idx != "2") {
        if (readCookie(sSectionID) != 'headerOpened')
            change("SET_METADATA_INDEX","2");
    }
    toggleMenu(sSectionID);
    hideAllExcept(sSectionID);
}
function toggleThematicMapMenu(sSectionID) {
    var obj = eval(document.forms["view"].metadata_editor_index);
    if(obj != null) {
        idx = document.forms["view"].metadata_editor_index.value;
        if(idx != "0") {
            if (readCookie(sSectionID) != 'headerOpened')
                change("SET_METADATA_INDEX","0");
        }
    }
    toggleMenu(sSectionID);
    hideAllExcept(sSectionID);

}