// ------------------------- Copyright notice ----------------------------
//
// Copyright (C) 2008 - Learning Access Institute, Seattle
//                      All Rights Reserved
//
// See Software License for further details which can be found online
// in the root of the application directory in a file LICENSE
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.  IN NO EVENT SHALL THE LEARNING ACCESS INSTITUTE OR
// ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// REDISTRIBBUTION OF SETFORTH SOFTWARE, INLCUDING ANY SPECIAL
// LIBRARIES, GRAPHICS OR OTHER MATERIAL PART OF THIS DISTRIBUTION
// IS PROHIBITED.  MODIFICATIONS TO THE SOURCE CODE FOR INTERNAL
// USE BY THE LICENSEE IS GRANTED WITH LIMITATIONS AS SET FORTH IN
// THE LICENSE AGREEMENT.
//
// PORTIONS OF THIS SOFTWARE AND ITS UNDERLYING PROCESSES AND ALGORITHMS
// ARE PROTECTED BY PENDING PATENT APPLICATIONS WORLD WIDE.
//
//
// Authors:             Willem Scholten
// Original date:       October 2008
// Contact:             wscholten@learningaccess.org
//
// Module name:         dpexplorer.js
// Application name:    DataExplorer Main Javascript functions
//
// Version:             1.0
//
// $HeadURL$
// $Id$
// $LastChangedBy$
// ----------------------------------------------------------------------

        var availCnt = document.getElementById('availListCount');
        //availCnt.innerHTML = document.DDPQQInputForm.availableList.options.length;
        //refreshSelectedCount();

	function cancelForm(type) {
	        if(type == 'noLogin') { alert('User is not logged in -- action canceled.'); }
	        if(type == 'noSession') { alert('Session was unexpectedly terminated -- action canceled.'); }
        }

	function adminLogin() {
                document.DDPQQInputForm.submit();
	}

	function adminLoginCancel() {
                document.DDPQQInputForm.username.value = '';
                document.DDPQQInputForm.password.value = '';
                document.DDPQQInputForm.submit();
	}

	function adminLogout() {
		document.DDPQQInputForm.doLogout.value = '1';
		document.DDPQQInputForm.submit();
        }

	function adminRefresh(uiTab) {
		document.DDPQQInputForm.submit();
        }


        function goToNewTab(tabValue) {
                document.DDPQQInputForm.tabSelect.value = tabValue;
                document.DDPQQInputForm.page.value = '';
                document.DDPQQInputForm.selListVal.value = '';
                document.DDPQQInputForm.selListGeoVal.value = '';
                document.DDPQQInputForm.selListYearVal.value = '';
                document.DDPQQInputForm.resetForm.value = '1';
                document.DDPQQInputForm.submit();
        }                                


        function changeDimHK(tc,stc) {
                var targetCombo = document.DDPQQInputForm.selectedList;
                var selListVal = new Array();

                for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                }
                                                                                                                    
                document.DDPQQInputForm.selListVal.value = selListVal;
                document.DDPQQInputForm.tc.value = tc;
                document.DDPQQInputForm.stc.value = stc;
                document.DDPQQInputForm.submit();
        }


        function changeAdminDimHK(tc,stc) {
                            
	        //alert ('Admin menu change: ' + tc);                // For debugging purpose
		                                                                                        
                document.DDPQQInputForm.adminTc.value = tc;
                document.DDPQQInputForm.adminStc.value = stc;
                document.DDPQQInputForm.submit();
        }

        function changeGeoState(area,region) {


                //var targetCombo = document.DDPQQInputForm.selectedList;
                var selListGeoVal = new Array();

                for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListGeoVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                }

                document.DDPQQInputForm.selListGeoVal.value = selListGeoVal;
                document.DDPQQInputForm.area.value = area;
                document.DDPQQInputForm.region.value = region;
                //document.DDPQQInputForm.stc.value = stc;
                document.DDPQQInputForm.submit();
        }

        function selectOption() {
                var sourceCombo = document.DDPQQInputForm.availableList;
                var targetCombo = document.DDPQQInputForm.selectedList;

                for (idx=0; idx<sourceCombo.options.length; idx++) {
                        if (sourceCombo.options[idx].selected) {
                                var toBeAdd = true;
                                for (ctr=0; ctr<targetCombo.options.length; ctr++) {
                                        if (sourceCombo.options[idx].value == targetCombo.options[ctr].value) {
                                                toBeAdd = false;
                                                break;
                                        }
                                }
                                if (toBeAdd){
                                        var targetLength = targetCombo.options.length
                                        targetCombo.options.length = targetLength + 1
                                        targetCombo.options[targetLength].value = sourceCombo.options[idx].value
                                        targetCombo.options[targetLength].text = sourceCombo.options[idx].text

                                }
                        }

                }
                refreshSelectedCount();
        }


        function selectAdvancedOption() {
                // We double clicked on one of the indicators which has advanced options
                // this should trigger a popup to
                var sourceCombo = document.DDPQQInputForm.availableMoreOptionsList;
                for (idx=0; idx<sourceCombo.options.length; idx++) {
                        if (sourceCombo.options[idx].selected) {
                           //alert ('Advanced option for indicator: ' + sourceCombo.options[idx].value);                // For debugging purpose
                           openOptionWindowPopup(sourceCombo.options[idx].value);
                        }
                }
        }

        function openOptionWindowPopup(indicator) {
                // Now open the popup window for the incoming indicator
                // We check if we can open it, if not -- we return an alert!

                //     X        = box width
                //     Y        = box height
                //     link     = the contents
                //     screenX  = upper corner of window is at X
                //     screenY  = upper corner of window is at Y
                //     link     = the app to open (url)
                //     name     = windwo name
                var name = 'IndicatorAdvancedOptions';
                var link = 'indicatorAdvancedOptions.php?indicator=' + indicator;
                var X = 500;
                var Y = 620;
                screenX = 150;
                screenY = 50;

                var mine = window.open(link,name,"height="+Y+",width="+X+",left=150,top=50,screenX=150,screenY=50,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no");
                if(mine) { var popUpsBlocked = false } else { var popUpsBlocked = true }

                if(popUpsBlocked) {
                        alert('We have detected that you are using popup blocking software.\nPlease allow macg5.learningaccess.org to open popups.....');
                }


        }

        function openInfoWindowPopup(info) {
                // Now open the popup window for the incoming indicator
                // We check if we can open it, if not -- we return an alert!

                //     X        = box width
                //     Y        = box height
                //     link     = the contents
                //     screenX  = upper corner of window is at X
                //     screenY  = upper corner of window is at Y
                //     link     = the app to open (url)
                //     name     = windwo name
                var name = 'InfoWindow';
                var link = 'info.php?option=' + info;
                var X = 500;
                var Y = 620;
                screenX = 150;
                screenY = 50;

                var mine = window.open(link,name,"height="+Y+",width="+X+",left=150,top=50,screenX=150,screenY=50,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no");
                if(mine) { var popUpsBlocked = false } else { var popUpsBlocked = true }

                if(popUpsBlocked) {
                        alert('We have detected that you are using popup blocking software.\nPlease allow macsrv01.learningaccess.org to open popups.....');
                }
        }


        function openHelpWindowPopup(info) {
                // Now open the popup window for the incoming indicator
                // We check if we can open it, if not -- we return an alert!

                //     X        = box width
                //     Y        = box height
                //     link     = the contents
                //     screenX  = upper corner of window is at X
                //     screenY  = upper corner of window is at Y
                //     link     = the app to open (url)
                //     name     = windwo name
                var name = 'HelpWindow';
                var link = 'help.php?option=' + info;
                var X = 500;
                var Y = 620;
                screenX = 150;
                screenY = 50;

                var mine = window.open(link,name,"height="+Y+",width="+X+",left=150,top=50,screenX=150,screenY=50,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no");
                if(mine) { var popUpsBlocked = false } else { var popUpsBlocked = true }

                if(popUpsBlocked) {
                        alert('We have detected that you are using popup blocking software.\nPlease allow macsrv01.learningaccess.org to open popups.....');
                }
        }

        function openInfoMapDisplay(state) {
                // Now open the popup window for the incoming indicator
                // We check if we can open it, if not -- we return an alert!

                //     X        = box width
                //     Y        = box height
                //     link     = the contents
                //     screenX  = upper corner of window is at X
                //     screenY  = upper corner of window is at Y
                //     link     = the app to open (url)
                //     name     = windwo name
                var name = 'InfoWindowShowMap';
                var link = 'infoShowMap.php?state=' + state;
                var X = 925;
                var Y = 620;
                screenX = 150;
                screenY = 150;

                var mine = window.open(link,name,"height="+Y+",width="+X+",left=150,top=150,screenX=150,screenY=150,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no");
                if(mine) { var popUpsBlocked = false } else { var popUpsBlocked = true }

                if(popUpsBlocked) {
                        alert('We have detected that you are using popup blocking software.\nPlease allow macsrv01.learningaccess.org to open popups.....');
                }


        }

        function selectAllValues(){
                var sourceCombo = document.DDPQQInputForm.availableList;
                var targetCombo = document.DDPQQInputForm.selectedList;

                for (idx=0; idx<sourceCombo.options.length; idx++) {
                                var toBeAdd = true;
                                for (ctr=0; ctr<targetCombo.options.length; ctr++) {
                                        if (sourceCombo.options[idx].value == targetCombo.options[ctr].value) {
                                                toBeAdd = false;
                                                break;
                                        }
                               }
                                if (toBeAdd){
                                        var targetLength = targetCombo.options.length
                                        targetCombo.options.length = targetLength + 1
                                        targetCombo.options[targetLength].value = sourceCombo.options[idx].value
                                        targetCombo.options[targetLength].text = sourceCombo.options[idx].text

                                }


                }

                refreshSelectedCount();
        }

        function removeOption() {
                var targetCombo = document.DDPQQInputForm.selectedList;
                for (idx=0; idx<targetCombo.options.length; idx++) {
                                if (targetCombo.options[idx].selected) {
                                //targetCombo.options.remove(idx);
                                targetCombo.remove(idx);
                                idx--;
                        }

                }
                refreshSelectedCount();
        }

        function removeAll() {
                document.DDPQQInputForm.selectedList.options.length = 0;
                refreshSelectedCount();
        }

        function refreshSelectedCount() {
		//alert('Called RefreshSelectedCount');
                var selectedCnt = document.getElementById('selectedListCount');
                selectedCnt.innerHTML = document.DDPQQInputForm.selectedList.options.length;
		if(document.DDPQQInputForm.selectedList.options.length > 650) {
		   // Show WARNING
		   document.getElementById('geoErrorMsg').innerHTML = '&nbsp; Geographical Selections exceed system maximum of 650';
		}

        }

        function refreshAvailableCount() {
		//alert('Called RefreshSelectedCount');
                var availableCnt = document.getElementById('availListCount');
                availableCnt.innerHTML = document.DDPQQInputForm.availableList.options.length;
        }

        function goToResetPage() {
                document.DDPQQInputForm.tabSelect.value = '';
                document.DDPQQInputForm.page.value = 'indic';
                document.DDPQQInputForm.selListVal.value = '';
                document.DDPQQInputForm.selListGeoVal.value = '';
                document.DDPQQInputForm.selListYearVal.value = '';
                document.DDPQQInputForm.resetForm.value = '1';
                document.DDPQQInputForm.submit();
        }

        function goHelpAdminFormReset() {
		document.helpPageAdminForm.save.value = 'n';
		document.helpPageAdminForm.submit();
	}

	function goTextEditFormReset() {
		document.helpPageAdminForm.save.value = 'n';
		document.helpPageAdminForm.submit();
	}

	function goTextEditFormDelete() {
                document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.type.value = 'delete';
                document.helpPageAdminForm.submit();
        }

	function goTextEditFormDeleteReset() {
                document.helpPageAdminForm.deleteConfirm.value = '';
                document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.submit();
        }

	function goUserAdminFormReset() {
		document.helpPageAdminForm.username.value = '';
		document.helpPageAdminForm.fullname.value = '';
		document.helpPageAdminForm.password.value = '';
		document.helpPageAdminForm.email.value = '';
		document.helpPageAdminForm.disabled.value = '0';
		document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.submit();
        }

	function goNewIndicatorFormReset() {
		document.helpPageAdminForm.IndicatorName.value = '';
		document.helpPageAdminForm.AdvancedNote.value = '';
		document.helpPageAdminForm.IndicatorNumber.value = '0';
		document.helpPageAdminForm.AdvancedOptions.value = '0';
		document.helpPageAdminForm.Active.value = '0';
		document.helpPageAdminForm.WebActive.value = '0';
		document.helpPageAdminForm.canCPIindex.value = '0';
		document.helpPageAdminForm.noDataYet.value = '0';
		document.helpPageAdminForm.CategoryCode.value = '';
		document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.submit();
        }
  
	function goNewDatasetFormReset() {
		document.helpPageAdminForm.DatasetID.value = '';
		document.helpPageAdminForm.dataSrc.value = '';
		document.helpPageAdminForm.DatasetName.value = '';
		document.helpPageAdminForm.GeographicCoverage.value = '';
		document.helpPageAdminForm.DateRange.value = '';
		document.helpPageAdminForm.Active.value = '0';
		document.helpPageAdminForm.Website.value = '';
		document.helpPageAdminForm.ReleaseFrequency.value = '';
		document.helpPageAdminForm.ReleaseMonth.value = '';
		document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.submit();
	}

	function goHelpAdminFormDeleteReset() {
                document.helpPageAdminForm.deleteConfirm.value = '';
                document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.submit();
        }
		
	function goHelpAdminFormDelete() {
                document.helpPageAdminForm.save.value = 'n';
                document.helpPageAdminForm.type.value = 'delete';
                document.helpPageAdminForm.submit();
        }

        function goBackToPage(page) {
                document.DDPQQInputForm.page.value = page;
                document.DDPQQInputForm.submit();
        }

        function goToNextPage(page) {

                if(page == 'indic') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'indic';           // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }
                if(page == 'geo') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'indic';           // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }


                if(page == 'series') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListGeoVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'geo';             // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }

                if(page == 'extract') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListYearVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'series';          // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }


                if(page == 'download') {
                   //var selListVal = new Array();
                   //for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                   //           selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   //}
                   //document.DDPQQInputForm.selListYearVal.value = selListVal;
                   //if(selListVal.length == 0) {
                   //   document.DDPQQInputForm.page.value = 'series';          // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    //} else {
                        document.DDPQQInputForm.page.value = page;
                    //}
                }


                document.DDPQQInputForm.submit();
        }


        function goToNextPageGeoOrder(page) {
		// We need to process the order slightly different hence two version ofthe 'next' button function
		// could be smarter using the interface as a parameter but this is a dirty quick fix :)
                if(page == 'indic') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListGeoVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'geo';           // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }
                if(page == 'geo') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListGeoVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'geo';           // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }


                if(page == 'series') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'indic';             // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }

                if(page == 'extract') {
                   var selListVal = new Array();
                   for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                                selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   }
                   document.DDPQQInputForm.selListYearVal.value = selListVal;
                   if(selListVal.length == 0) {
                        document.DDPQQInputForm.page.value = 'series';          // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    } else {
                        document.DDPQQInputForm.page.value = page;
                    }
                }


                if(page == 'download') {
                   //var selListVal = new Array();
                   //for (idx=0; idx<document.DDPQQInputForm.selectedList.options.length; idx++) {
                   //           selListVal[idx] = document.DDPQQInputForm.selectedList.options[idx].value;
                   //}
                   //document.DDPQQInputForm.selListYearVal.value = selListVal;
                   //if(selListVal.length == 0) {
                   //   document.DDPQQInputForm.page.value = 'series';          // Can not move on UNLESS we got indicators selected
                        // should write error notice here why we can not move on
                    //} else {
                        document.DDPQQInputForm.page.value = page;
                    //}
                }


                document.DDPQQInputForm.submit();
        }

