// **************
// * Navigation *
// **************

// *** Core menu code (BEGIN) **********************************************

// Shows a container
function myshow(id) {
  if (id != undefined && id != '') {
     $(id).style.display = "inline";
  };
}

// Hides a container
function myhide(id) {
  if (id != undefined && id != '') {
     $(id).style.display = "none";
  };
}

// Hides or shows the menu by clicking the page title container
function activateMenusByPageTitle() {
  if (menuActiveByPageTitel) {  	 
     menuActiveByPageTitel = false;
     $('menu').hide();
     $('menu_reduced').hide();
  } else {
     menuActiveByPageTitel = true;
     $('menu').show();
  }
}


// Switch activation status of the menu
function activateMenu(activateId, deactivateId) {
	// if (deactivateId != '' && document.getElementById(deactivateId)) {
	if (deactivateId != '' && $(deactivateId)) {
 	   $(deactivateId).hide();
 	}; 	   
	// if (activateId != '' && document.getElementById(activateId)) {
	if (activateId != '' && $(activateId)) {
 	   $(activateId).show();
 	};
}

// Call for loading one image
function changeContent(objectName,mode,navLevel) {
  if (navLevel == "") {navLevel = 0};
  navLevel = parseInt(navLevel);
  
  useCascade = true;
  if (navLevel < 0) {
  	 useCascade = false;
  	 navLevel = 0;
  };
// alert(navLevel + ' ' + objectName + ' ' + mode );
	tmpIndex = 0;    // "off"
 	if (mode == "over") {tmpIndex = 1;};
 	if (mode == "active") {tmpIndex = 2;};
 	if (mode == "deactive") {tmpIndex = 0;};

  if (objectName != activeElement[navLevel] || mode == "active" || mode == "deactive") {
     $(objectName).style.background = 'url(' + navBgImg[navLevel][tmpIndex] + ')';
  };
  
  if (useCascade) {
     if (objectName != activeElement[navLevel]) {
        if (mode == "off") {
           if (activeElement[navLevel] == "") {
              // Hide elements of sub-navigation
              for (i = navLevel; i <= menuNavigationDeep; i++) {
                  len = menuStruct[i].length;
                  for (j = 0; j < len; j++) {
                      if (subMenu[menuStruct[i][j]]) {
                         len2 = subMenu[menuStruct[i][j]].length;
                         for (k = 0; k < len2; k++) {
                             myhide(subMenu[menuStruct[i][j]][k]);
                         };
                      };
                  };
              };
           };
        };
        if (mode == "over") {
           if (activeElement[navLevel] == "") {
              // Show element of sub-navigation
              if (subMenu[objectName]) {
                 len = subMenu[objectName].length;
                 for (i = 0; i < len; i++) {
                     myshow(subMenu[objectName][i]);
                 };
              };
           };
        };
     };
  };
}

// Call for enable a sub menu as active and disable the old active element
function setMenu(objectName,navLevel) {
  if (navLevel == "") {navLevel = "0"};

  // Disable current displayed sub menus
  for (i = navLevel; i <= menuNavigationDeep; i++) {
      // Reset menu
      len = menuStruct[i].length;
      for (j = 0; j < len; j++) {
     	   if (document.getElementById(menuStruct[i][j])) {
     	      changeContent(menuStruct[i][j],'deactive',i);
     	   };      	
      	
      	  // Hide sub menus if exist
          if (subMenu[menuStruct[i][j]]) {         	
             len2 = subMenu[menuStruct[i][j]].length;
             for (k = 0; k < len2; k++) {
                 myhide(subMenu[menuStruct[i][j]][k]);
             };
          };
      };
  };

  if (objectName != activeElement[navLevel] || activeElement[navLevel] == "") {
     // Enable new submenu
     if (subMenu[objectName]) {
        len = subMenu[objectName].length;
        for (i = 0; i < len; i++) {
            myshow(subMenu[objectName][i]);
        };
     };

     // Define active element and delete cascade
     for (i = (navLevel + 1); i <= menuNavigationDeep; i++) {
         activeElement[i] = "";
     };
     activeElement[navLevel] = objectName;
     changeContent(activeElement[navLevel],'active',navLevel);
  } else { 
     for (i = navLevel; i <= menuNavigationDeep; i++) {
         activeElement[i] = "";
     };
  };
}

// Gets the id of the parent element
// if parentLevel==0 then take parent, if parentLevel==1 then take parent of the parent, etc.
function getParentId(objectId, parentLevel) {
	if (parentLevel == '') {parentLevel = 0};
	parentLevel = parseInt(parentLevel);
  tmp = $(objectId).ancestors();
  tmp = tmp[parentLevel];
  parentId = tmp.id;
  return parentId;
}

// Displays (hides and shows) sub menus (if does exist) according to an item
// E.g. it shows all elements (menu containers) like subMenu["preise"] => "menu_01_02_01
function displaySubmenu(itemId, mode) {
if (mode == '') {mode = 'show'};
  if (itemId) {         	
     len = subMenu[itemId].length;
     for (i = 0; i < len; i++) {
     	   if (mode == 'show') {
            myshow(subMenu[itemId][i]);
         } else {
         	  myhide(subMenu[itemId][i]);
         };
     };
  };
}

// Initiates the menu
function setMenuOnLoad(objectName,navLevel) {
  if ($(objectName)) {
     navLevel = parseInt(navLevel);
// alert('objectName=' + objectName + ' navLevel=' + navLevel + ' activeElement[' + navLevel + ']=' + activeElement[navLevel]);

     // Activate current item
     changeContent(objectName,'active',navLevel);
     activeElement[navLevel] = objectName;

     // Get id of the parent element  
     // E.g. objectName == "servicepreise" => parentId = "menu_01_02_01"
     parentId = getParentId(objectName,0);

     // Lookup for item of next parent navigation level having "parentId" in associated Array
     // E.g. "menu_01_02_01" is element of subMenu["preise"] => Get all elements of subMenu["preise"]
     for (tmpNavLevel = (navLevel - 1); tmpNavLevel >= 0; tmpNavLevel--) {

         // Iterate all items of the navigation level in menuStruct[tmpNavLevel]
         len = menuStruct[tmpNavLevel].length;
         for (j = 0; j < len; j++) {

             if (subMenu[menuStruct[tmpNavLevel][j]]) {

                len2 = subMenu[menuStruct[tmpNavLevel][j]].length;
                tmpActivateSubmenu = false;
                for (k = 0; k < len2; k++) {
                	   if (subMenu[menuStruct[tmpNavLevel][j]][k] == parentId) {
                       tmpActivateSubmenu = true;
                    };
                };
                if (tmpActivateSubmenu) {
                	  // E.g. show all containers of subMenu["preise"]
                	  displaySubmenu(menuStruct[tmpNavLevel][j], 'show');
                	
                	  // Activate parent item e.g. "preise" (= menuStruct[tmpNavLevel][j])
                	  changeContent(menuStruct[tmpNavLevel][j],'active',tmpNavLevel);
                	  activeElement[tmpNavLevel] = menuStruct[tmpNavLevel][j];

                	  parentId = getParentId(menuStruct[tmpNavLevel][j],0);
                };
             };
         };
     };
  };
}

// Sets all links of a special link class to "hidefocus"
function hideFocusOfLinkClass() {
  $$('#a_menu').each(function(i) {i.hideFocus = "true";}); 
}


// C9E9FE   hellgrün
// 47B868   grün
// 2091C9   blau

var menuActive = true;
// var menuActiveByPageTitel = true;
<?php if ($menuActiveByPageTitel) : ?>
   var menuActiveByPageTitel = true;
<?php else : ?>
   var menuActiveByPageTitel = false;
<?php endif; ?>
var menuNavigationDeep = <?php echo $jsMenuNavigationDeep ?>;

// Init parameter for current displayed submenus
var activeElement = new Array();
<?php
for ($j = 0; $j <= $jsMenuNavigationDeep; $j++) :
    echo "activeElement[" . $j . "] = '';";
endfor;
?>
var activeElementLen = activeElement.length;

var navBgColor = new Array();
navBgColor[0] = "#1b12b9";   // menu_01 blau  (27, 18, 185)
navBgColor[1] = "#4e45ec";   // menu_02 blau  (78, 69, 236)
navBgColor[2] = "#97bcFF";   // menu_03 blau  (151, 188, 255)
navBgColor[3] = "#97bcFF";

var navBgImg = new Array();
navBgImg[0] = new Array("../images/navItemBgOffMenu01.jpg", "../images/navItemBgOverMenu01.jpg", "../images/navItemBgActiveMenu01.jpg");
navBgImg[1] = new Array("../images/navItemBgOffMenu02.jpg", "../images/navItemBgOverMenu02.jpg", "../images/navItemBgActiveMenu02.jpg");
navBgImg[2] = new Array("../images/navItemBgOffMenu03.jpg", "../images/navItemBgOverMenu03.jpg", "../images/navItemBgActiveMenu03.jpg");
navBgImg[3] = new Array("../images/navItemBgOffMenu03.jpg", "../images/navItemBgOverMenu03.jpg", "../images/navItemBgActiveMenu03.jpg");

// Array of the menu structure
var menuStruct = new Array();
// menuStruct[0] = new Array("verwaltung","auftraege", ...);
// menuStruct[1] = new Array("kunden","transporteure",... ,"uebersicht","pdf_ausgabe");
// ...
<?php
for ($j = 1; $j < $menuLevels; $j++) :
    echo "menuStruct[" . ($j - 1) . "] = new Array(" . $jsMenuStruct[$j] . ");\n";
endfor;
?>

// Array of containers be visible by hovering and click
var subMenu = new Array();
// subMenu["verwaltung"] = new Array("menu_01_01","menu_01_02","menu_01_03");
// subMenu["rechnungen"] = new Array("menu_02_01");
// ...
<?php
$jsMenuOut = "";
$tmpLen = count($jsSubMenu);  
for ($j = 0; $j < $tmpLen; $j++) :
    $jsMenuOut .=  "subMenu[\"" . $jsSubMenu[$j][0] . "\"] = new Array(";
    $tmpLen2 = count($jsSubMenu[$j][1]);
    for ($k = 0; $k < $tmpLen2; $k++) :
        $jsMenuOut .= "\"" . $jsSubMenu[$j][1][$k] . "\"";
        if ($k < ($tmpLen2 - 1)) : 
           $jsMenuOut .= ",";
        endif;          
    endfor;
    $jsMenuOut .= ");\n";
endfor;
echo $jsMenuOut;
?>


// *** Core menu code (END) **********************************************



// *** Framework Wrapper (BEGIN) ***************************************

function vShow(selector) {
	myshow(selector);
};

function vHide(selector) {
  myhide(selector);
};

function vSetCss(selector, cssStyles) {
  $(selector).setStyle(cssStyles);
};

function vSetElementContent(selector, content) {
  $(selector).innerHTML = content;
};

function vGetElementContent(selector) {
  return $(selector).innerHTML;
};

function vConfirmFunc(msg, fn, height, selector) {
  if (confirm(msg)) { fn(); };
};

function vConfirmFuncPar(msg, fn, height, selector) {
  if (confirm(msg)) { fn(); };
};

// *** Framework Wrapper (END) ******************************************



// *** DHTML (BEGIN) ****************************************************

var activeButton = '';
var disabledButtons = new Array();   // E.g. for export to disable all buttons after selecting one of them

function checkButtonIsDisabled(buttonName) {
  notDisabled = true;
  disabledButtonsLen = disabledButtons.length;
  for (i = 0; i < disabledButtonsLen; i++) {
      if (buttonName == disabledButtons[i]) {notDisabled = false;};
  };
  return notDisabled;
}

function changeButton(mode,buttonName,content,paddingLeft,paddingTop) {

  elem = $(buttonName);
  notDisabled = checkButtonIsDisabled(buttonName);
  
  if (buttonName != activeButton && notDisabled) {
     if (mode == "off") {
     	  elem.setStyle({color: '#FFFFFF'});
        // elem.innerHTML = '<div style="margin-left:' + paddingLeft + '; margin-top:' + paddingTop + ';">' + content + '</div>\n';
     };
     if (mode == "over") {
     	  buttonWidth = elem.getStyle('width');
     	  buttonWidth = parseInt(buttonWidth.substring(0, (buttonWidth.length - 2))) - 2;
     	  buttonHeight = elem.getStyle('height');
     	  buttonHeight = parseInt(buttonHeight.substring(0, (buttonHeight.length - 2))) - 2;
     	  elem.setStyle({color: '#97bcFF'});

     	  // elem.innerHTML = '<div style="margin-left:' + paddingLeft + '; margin-top:' + paddingTop + '; color:#FFFFFF;">' + 'x' + content + '</div>\n';
/*
     	  tmpInnerHTLM  = eval('<div style="height:1px; background:#FFFFFF;"></div>\n' +
     	                  '<div style="height:' + buttonHeight + 'px;">\n' +
     	                  '  <div style="width:1px; height:100%; background:#FFFFFF;"></div>\n' +
     	                  '  <div style="width:' + buttonWidth + 'px; height:' + buttonHeight + 'px;">' + content + '</div>\n' +
     	                  '  <div style="width:1px; height:100%; background:#000000;"></div>\n' +
     	                  '</div>\n' +
                        '<div style="height:1px; background:#000000;"></div>\n');
        elem.innerHTML = tmpInnerHTLM;
*/        
     };
  };
}

// *** DHTML (END) ****************************************************


// Prototype.Request (POST)
function ajax_request(url, data) {
    var myAjax = new Ajax.Request(
        url,
        {method: 'post', parameters: data, onComplete: ajax_response}
    );
}

// Prototype.Response (POST)
function ajax_response(originalRequest) {
    if(!bHasRedirect) {
        //process originalRequest.responseText;
        // e.g.:
        $('maincontent').innerHTML = responseText;
    }
    else {
        bHasRedirect = false;
        ajax_request(originalRequest.responseText, "");
    }
}

// Prototype.Request (GET)
function ajaxRequestGet(specificURL, parms) {
    var myAjax = new Ajax.Request(
        specificURL,        
        {method: 'get', asynchronous: false, parameters: parms, cache: false}
    );
    eval(myAjax.transport.responseText);
}

var statusMessage = "<?php echo $statusMessage ?>";

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
};

function hqCheckAll(numOfHq) {
  for (i = 0; i < numOfHq; i++) {
	  document.getElementsByName('f_hq_id[]')[i].checked = true;
	}
}

function hqUncheckAll(numOfHq) {
  for (i = 0; i < numOfHq; i++) {
	  document.getElementsByName('f_hq_id[]')[i].checked = false;
  }
}

function mkImpressumPopupWin() {

  var widthPopupWin = 250;
  var heightPopupWin = 310;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin=window.open("../admin/impressum.html", "","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin);
};

function mkLanguagePopupWin() {

  var widthPopupWin = 250;
  var heightPopupWin = 310;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin=window.open("../admin/language.php", "","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin);
};

function submitPage(navItem) {
  if (document.forms[0].currentNavigationItem) {
     document.forms[0].currentNavigationItem.value = navItem;
  }
  if (document.forms[0].menuActiveByPageTitel) {
     if (menuActiveByPageTitel) {
        document.forms[0].menuActiveByPageTitel.value = '0';
     } else {
        document.forms[0].menuActiveByPageTitel.value = '1';
     }
  }
  document.forms[0].submit();
};

function logout() {
  if (confirm('<?php echo getLngt("Möchten Sie sich wirklich abmelden?") ?>')) {
     document.location.href = "../admin/logout.php";
  };
};

function genAlert (text) {
  alert(text);
};

function genConfirm (text) {
  if (confirm(text)) {
     return true;
  };
  return false;
};

function openMfHistory() {
  var widthPopupWin = 900;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/mf_history.php","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openJobInvoiceAssoc() {
  var widthPopupWin = 1200;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../jobs/jb_inv_assoc.php","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openVehicleDisposition() {
  var widthPopupWin = 1800;
  var heightPopupWin = 1024;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/vehicle_disposition.php","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};