var openPrintWin;
var originalMapWidth, originalMapHeight;
var printMode = false;
var printModeHeight, printModeWidth;
openPrintWin = 0;

var pnlMap = 'ctl00_cph1_pnlMap';
var SVGmap = 'objSVGmap';
var MapTabID = 'ctl00_cph1_v3MapTab_';
var ddlMapClass;
var ddlMapClass2;
var CurrentddlMapClassSelectedIndex;
var CurrentddlMapClassSelectedIndex2;

function DimensionPopUp(indicator) {
    var ddlIndicator2Type = MapTabID + 'ddlIndicator2Type';
    var ddlIndicator2Type2 = MapTabID + 'ddlIndicator2Type2';
    var ddlTemplate = MapTabID + 'ddlMapTemplate';
    var ddlDimension;
    var timeddl;
    var all = '0';
    var ddlmap = document.getElementById(MapTabID + 'ddlMaps');
    var rtype = ddlmap.options[ddlmap.options.selectedIndex].value;
    var rtype2 = '';

    if (indicator == 1) {
        ddlDimension = document.getElementById(MapTabID + 'ddlMapDimensions');
        timeddl = document.getElementById(MapTabID + 'ddlMapTime');
    }
    else {
        ddlDimension = document.getElementById(MapTabID + 'ddlMapDimensions2');
        timeddl = document.getElementById(MapTabID + 'ddlMapTime2');
        var v = document.getElementById(ddlTemplate).options[document.getElementById(ddlTemplate).selectedIndex].value;
        if (v.toLowerCase().indexOf('compare') != -1) {
            rtype2 = document.getElementById(ddlIndicator2Type2).options[document.getElementById(ddlIndicator2Type2).options.selectedIndex].value;
            if (document.getElementById(ddlIndicator2Type).options.selectedIndex > 0) {
                all = '1';
            }
        }
    }



    var ddlDimensionValue = ddlDimension.options[ddlDimension.options.selectedIndex].value;
    var dType = ddlDimensionValue.substring(1, ddlDimensionValue.indexOf(":"));
    var dimension = ddlDimensionValue.substring(ddlDimensionValue.indexOf(":") + 1, (ddlDimensionValue.indexOf("-")));
    var time = timeddl.options[timeddl.options.selectedIndex].value;


    var dialogargs = "dialogHeight: 600px; dialogWidth: 600px; center: Yes; help: No; resizable: No; status: No;";
    var url = "DataSetPopUpInfo.aspx?rtype=" + rtype.toString() + "&rtype2=" + rtype2.toString() + "&dType=" + dType.toString() + "&dim=" + dimension.toString() + "&time=" + time.toString() + "&all=" + all.toString();

    if (window.radopen) {
        var wnd = window.radopen(url, null);
    }
    else {
        if (window.showModalDialog) {
            window.showModalDialog(url, window, dialogargs);
        }
        else {
            window.open(url, null, config = "height=600,width=600,resizable=no,status=no");
        }
    }
}

function ChangeDiscColour(sender, eventArgs) {
    var dgClassifications2 = MapTabID + 'dgClassifications2';
    var oldColour = eventArgs.get_oldColor();
    var newColour = sender.get_selectedColor()
    if ((oldColour != newColour) && (document.getElementById(dgClassifications2).style.display != 'none')) {
        document.getElementById(dgClassifications2).style.display = 'none';
        document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
        HideMapTools2();
    }

}

function HideMapTools2() {
    //document.getElementById(MapTabID+'MapTools2').style.display='none';
}

function ChangeMapClass(mapclass) {
    var el = document.getElementById(MapTabID + mapclass);
    var dgClassifications = MapTabID + 'Migrated_dgClassifications';
    var dgClassifications2 = MapTabID + 'dgClassifications2';
    if ((el == ddlMapClass) && (document.getElementById(dgClassifications)) && (document.getElementById(dgClassifications).style.display != 'none')) {
        if (confirm('Are you sure you want to change the number of classifications or colours?\nDoing so will reset any classification labels you have manually applied.')) {
            document.getElementById(dgClassifications).style.display = 'none';
            document.getElementById(MapTabID + 'hiddenClearMapClass1').value = 'true';
            HideMapTools2();
        }
        else
        { document.getElementById(MapTabID + 'ddlMapClass').selectedIndex = CurrentddlMapClassSelectedIndex; }
    }
    if ((el == ddlMapClass2) && (document.getElementById(dgClassifications2)) && (document.getElementById(dgClassifications2).style.display != 'none')) {
        if (confirm('Are you sure you want to change the number of classifications?\nDoing so will reset any classification labels you have manually applied')) {
            document.getElementById(dgClassifications2).style.display = 'none';
            document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
            HideMapTools2();
        }
        else
        { document.getElementById(MapTabID + 'ddlMapClass2').selectedIndex = CurrentddlMapClassSelectedIndex2; }
    }
}

function ChangeMap() {
    forceRegenration();
    SetMapTitles();
}

function ChangeMapDimensions(el) {
    //alert(document.getElementById(MapTabID+'hiddenClearMapClass2').value);
    var dgClassifications = document.getElementById(MapTabID + 'Migrated_dgClassifications');
    var dgClassifications2 = document.getElementById(MapTabID + 'dgClassifications2');
    var ddlTemplate = document.getElementById(MapTabID + 'ddlMapTemplate');
    var ttype = 'single';
    if (ddlTemplate.options[ddlTemplate.options.selectedIndex].value.toLowerCase().indexOf('double') != -1) {
        ttype = 'double';
    }

    //alert((el.id==MapTabID+'ddlMapDimensions'));
    if (el.id == MapTabID + 'ddlMapDimensions2') {
        document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
        if (dgClassifications2 != null) {
            dgClassifications2.style.display = 'none';
            HideMapTools2();
        }
    }
    if (el.id == MapTabID + 'ddlMapDimensions') {
        //alert(el.id);
        document.getElementById(MapTabID + 'hiddenClearMapClass1').value = 'true';
        if (dgClassifications != null) {
            dgClassifications.style.display = 'none';
            HideMapTools2();
        }
    }
    //alert(document.getElementById(MapTabID+'hiddenClearMapClass1').value);

    CheckCategories();
    SetMapTitles();

}

function CheckCategories() {
    if (document.getElementById('objSVGmap') != null)
    { return; }
    if (cats) {
        var ddlMapDimensions = document.getElementById(MapTabID + 'ddlMapDimensions');
        ddlMapClass = document.getElementById(MapTabID + 'ddlMapClass');
        //ddlMapClass2 = document.getElementById(MapTabID+'ddlMapClass2');
        var ddlRangeScope = document.getElementById(MapTabID + 'ddlRangeScope');
        //var ddlRangeScope2 = document.getElementById(MapTabID+'ddlRangeScope2');
        var ddlColours = document.getElementById(MapTabID + 'ddlColours');
        var test = ddlMapDimensions.options[ddlMapDimensions.selectedIndex].value;
        //alert(test);
        if (ddlMapClass) { ddlMapClass.disabled = false };
        //if (ddlMapClass2) {ddlMapClass2.disabled=false};
        if (ddlColours) { ddlColours.selectedIndex = 0; ddlColours.options[0].disabled = false; }
        for (i = 0; i < cats.length; i++) {
            if (test.indexOf(cats[i]) != -1) {
                if (ddlMapClass) { ddlMapClass.disabled = true };
                //if (ddlRangeScope) { ddlRangeScope.selectedIndex = 1; ddlRangeScope.disabled = true };
                if (ddlColours) {
                    ddlColours.selectedIndex = 7;
                    ddlColours.options[0].disabled = true;
                }
            }

        }
    }
}

function SetMapTitles() {
    var ddlMapDimensions = document.getElementById(MapTabID + 'ddlMapDimensions');
    var ddlMapDimensions2 = document.getElementById(MapTabID + 'ddlMapDimensions2');
    var ddlIndicator2Type2 = document.getElementById(MapTabID + 'ddlIndicator2Type2');
    var ddlTemplate = document.getElementById(MapTabID + 'ddlMapTemplate');
    var ddlMaps = document.getElementById(MapTabID + 'ddlMaps');
    var ddlMapTime = document.getElementById(MapTabID + 'ddlMapTime');
    var ddlMapTime2 = document.getElementById(MapTabID + 'ddlMapTime2');
    var txt1 = document.getElementById(MapTabID + 'Migrated_txtMapTitle1');
    var txt2 = document.getElementById(MapTabID + 'Migrated_txtMapTitle2');
    var txt3 = document.getElementById(MapTabID + 'Migrated_txtMapTitle3');
    var compare = false;
    var ttype = 'single';
    if (ddlTemplate.options[ddlTemplate.options.selectedIndex].value.toLowerCase().indexOf('double') != -1) {
        ttype = 'double';
    }
    if (ddlTemplate.options[ddlTemplate.options.selectedIndex].value.toLowerCase().indexOf('compare') != -1) {
        compare = true;
    }
    //return;

    if (txt1) {
        if (ttype == 'double') {
            var t1 = ddlMapDimensions.options[ddlMapDimensions.selectedIndex].text;
            var t2 = '';
            if (ddlMapDimensions2)
            { t2 = ddlMapDimensions2.options[ddlMapDimensions2.selectedIndex].text; }
            var start = t1.lastIndexOf(' - ');
            if (start > 0) { start += 3 };
            var title1 = t1.substring(start, t1.length);
            start = t2.lastIndexOf(' - ');
            if (start > 0) { start += 3 };
            var title2 = t2.substring(start, t2.length);

            txt1.value = title1 + ' (' + ddlMapTime.options[ddlMapTime.selectedIndex].text + ')';
            if (ddlMapTime2) {
                txt2.value = title2 + ' (' + ddlMapTime2.options[ddlMapTime2.selectedIndex].text + ')';
            } else {
                txt2.value = title2;
            }

            if (compare == true) {
                txt3.value = ddlMaps.options[ddlMaps.selectedIndex].text + ' boundaries and ' + ddlIndicator2Type2.options[ddlIndicator2Type2.selectedIndex].text + ' discs';
            } else {
                txt3.value = ddlMaps.options[ddlMaps.selectedIndex].text + ' Map';
            }

        }
        else {
            var t1 = ddlMapDimensions.options[ddlMapDimensions.selectedIndex].text;

            if (t1.length > 60) {
                var start = t1.lastIndexOf(' - ');
                if (start > 0) { start += 3 };
                t1 = t1.substring(start, t1.length);
            }
            else {
                t1 = t1.substring(0, 60)
            }
            txt1.value = t1;
            txt2.value = ddlMaps.options[ddlMaps.selectedIndex].text;
            txt3.value = ddlMapTime.options[ddlMapTime.selectedIndex].text;
        }
    }
}

function ChangeMapRangeScope(el) {
    var dgClassifications = document.getElementById(MapTabID + 'Migrated_dgClassifications');
    var dgClassifications2 = document.getElementById(MapTabID + 'dgClassifications2');
    if (el.id == MapTabID + 'ddlRangeScope2') {
        if (dgClassifications2) {
            dgClassifications2.style.display = 'none';
            HideMapTools2();
        }
        document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
    }
    if (el.id == MapTabID + 'ddlRangeScope') {
        if (dgClassifications) {
            dgClassifications.style.display = 'none';
            HideMapTools2();
        }
        document.getElementById(MapTabID + 'hiddenClearMapClass1').value = 'true';
    }
}

function ChangeMapTime(el) {
    var dgClassifications = document.getElementById(MapTabID + 'Migrated_dgClassifications');
    var dgClassifications2 = document.getElementById(MapTabID + 'dgClassifications2');
    if (el.id == MapTabID + 'ddlMapTime2') {
        if (dgClassifications2) {
            dgClassifications2.style.display = 'none';
            HideMapTools2();
        }
        document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
    }
    if (el.id == MapTabID + 'ddlMapTime') {
        if (dgClassifications) {
            dgClassifications.style.display = 'none';
            HideMapTools2();
        }
        document.getElementById(MapTabID + 'hiddenClearMapClass1').value = 'true';
    }
    SetMapTitles();
}

function ChangeTemplate(doSetTitles) {
    if (doSetTitles === undefined) {
        doSetTitles = true;
    }

    var ddlTemplate = MapTabID + 'ddlMapTemplate';
    var ddlSideBar = MapTabID + 'Migrated_ddlMapSideBar';
    var ddlSideBarLabel = MapTabID + 'lblMapSideBar';
    var ddlMapDimensions2 = MapTabID + 'ddlMapDimensions2';
    var ddlIndicator2Type2 = MapTabID + 'ddlIndicator2Type2';
    var ddlIndicator2Type = MapTabID + 'ddlIndicator2Type';

    if (document.getElementById(ddlTemplate)) {
        var v = document.getElementById(ddlTemplate).options[document.getElementById(ddlTemplate).selectedIndex].value;
        if (v.toLowerCase().indexOf('printtemplate.svg') != -1) {
            //document.getElementById(ddlSideBar).selectedIndex=2;
            document.getElementById('divIndicator2').style.display = 'none';
            if (document.getElementById('tdIndicator2')) { document.getElementById('tdIndicator2').style.display = 'none'; }
            document.getElementById(ddlSideBar).style.display = 'none';
            document.getElementById(ddlSideBarLabel).style.display = 'none';
        }
        else {
            if (v.toLowerCase().indexOf('double') >= 0) {
                if (document.getElementById('divIndicator2')) { document.getElementById('divIndicator2').style.display = '' };
                if (document.getElementById('tdIndicator2')) { document.getElementById('tdIndicator2').style.display = '' };
                document.getElementById(ddlSideBar).style.display = 'none';
                document.getElementById(ddlSideBarLabel).style.display = 'none';
                if (v.toLowerCase().indexOf('compare') >= 0) {
                    document.getElementById(ddlIndicator2Type2).style.display = 'inline';
                } else {
                    document.getElementById(ddlIndicator2Type2).style.display = 'none';
                    //if (document.getElementById(ddlIndicator2Type)) {
                    //    if (document.getElementById(ddlIndicator2Type).options[document.getElementById(ddlIndicator2Type).selectedIndex].value != 'Same') {
                    //        document.getElementById(ddlIndicator2Type).selectedIndex = 0;
                    //        setTimeout('__doPostBack(\'ctl00$cph1$v3MapTab$ddlIndicator2Type\',\'\')', 0);
                    //    }
                    //}
                }
                if (document.getElementById(ddlIndicator2Type)) {
                    if (document.getElementById(ddlIndicator2Type).options[document.getElementById(ddlIndicator2Type).selectedIndex].value == 'Same') {
                        var ddlSelectDataSet = document.getElementById(MapTabID + 'ddlSelectDataSet');
                        if (ddlSelectDataSet) { ddlSelectDataSet.style.display = 'none'; }
                    }
                }
            }
            else {
                if (document.getElementById('divIndicator2')) { document.getElementById('divIndicator2').style.display = 'none' };
                if (document.getElementById('tdIndicator2')) { document.getElementById('tdIndicator2').style.display = 'none' };
                document.getElementById(ddlSideBar).style.display = '';
                document.getElementById(ddlSideBarLabel).style.display = '';
            } 
        }
    }
    if (doSetTitles)
    { SetMapTitles(); }
}



function doPrint()
{ openPrintWin = 1; }

function checkSubmit() {
    if (openPrintWin == 1) {

        var winX = 0; var winY = 0; var w = 700; var h = 500;
        if (parseInt(navigator.appVersion) >= 4) {
            w = screen.availWidth - 200; h = screen.availHeight - 200;
        }
        // only set new values if 4.0 browser
        if (parseInt(navigator.appVersion) >= 4) {
            winX = (screen.availWidth - w) * .5;
            winY = (screen.availHeight - h) * .5;
        }
        var winOpts = "toolbar=no,location=0,directories=no,status=yes,menubar=0,scrollbars=1,resizable=0," + "width=" + w + ",height=" + h + ",left=" + winX + ",top=" + winY
        simPrint = window.open("", "simPrint", winOpts);
        aspnetForm.target = 'simPrint';
        openPrintWin = 0;
        return true;
    }
    else {
        aspnetForm.target = '';
        return true;
    }
}

function instructionsMap() {
    var h = screen.availHeight - 100;
    var w = screen.availWidth - 200;
    var dialogargs = "dialogHeight: " + h.toString() + "px; dialogWidth: " + w.toString() + "px; center: Yes; help: No; resizable: No; status: No;";
    //alert(dialogargs);
    window.showModalDialog("MapInstructions.htm", window, dialogargs);
}

function openMapWindow() {
    var wind;
    var e = document.getElementById(SVGmap);
    var mapsrc;
    if (e.data)
    { mapsrc = e.data; }
    else
    { mapsrc = e.src; }
    wind = window.open(mapsrc, '_blank', "height=407,width=550,resizable=1,status=0,menubar=0");

}

function V2fullScreen(map) {
    var e;
    if (map == undefined)
    { e = document.getElementById(SVGmap); }
    else
    { e = document.getElementById(map); }

    var _loc;
    if (e.data)
    { _loc = e.data; }
    else
    { _loc = e.src; }

    var _name = "gisWin";
    var h = screen.availHeight - 20;
    var w = screen.availWidth - 10;

    var _features = "toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=0,resizable=0," + "width=" + w + ",height=" + h + ",left=" + 0 + ",top=" + 0
    var _replace = true;

    gisWin = window.open(_loc, _name, _features, _replace);
    gisWin.focus();

    return;
}



function forceRegenration() {
    document.getElementById(MapTabID + 'hiddenRegenerateMap').value = 'true';
    var dgClassifications = MapTabID + 'Migrated_dgClassifications';
    var dgClassifications2 = MapTabID + 'dgClassifications2';
    if (document.getElementById(dgClassifications)) {
        document.getElementById(dgClassifications).style.display = 'none';
        document.getElementById(MapTabID + 'hiddenClearMapClass1').value = 'true';
    }

    if (document.getElementById(dgClassifications2)) {
        document.getElementById(dgClassifications2).style.display = 'none';
        document.getElementById(MapTabID + 'hiddenClearMapClass2').value = 'true';
    }
}