var changed = false;
var looseDataMessage = "Really?";
var deleteDataMessage = "Really?";
var keyCode = 0;

if (self.name == "") self.name = "multibox_main";

var submittedActions = new Array()
var actionElementName = "actionMethod";
var indexElementName = "actionIndex";

var inputform = null;
var nextAction = null;
var lastAction = null;
var lastHref = null;

var childWindows = new Array();

var lastBoxId = null;

function showObject(id) {

  if (document.getElementById) {
    if (lastBoxId && lastBoxId != id) {
      hideObject(lastBoxId);
    }
    cinfo = document.getElementById(id);
    if (cinfo) {
      cinfo.style.display = "inline";
      lastBoxId = id;
    }
  }

}
function hideObject(id) {
  if (document.getElementById) {
    cinfo = document.getElementById(id);
    if (cinfo) {
      cinfo.style.display = "none";
      lastBoxId = null;
    }
  }

}

function enterEvent(e) {
  if (!e) e = window.event;
  if (e.keyCode == 13){
    document.forms[0].submit();
    return false;
  } else {
    return true;
  }

}

function addChildWindow (childw) {
    childWindows[childWindows.length] = childw;
}

function activateOpenChilds () {
    var i = 0;
    for (i=0;i<childWindows.length;i++) {
        if (childWindows[i].closed == false) {
            childWindows[i].focus();
        }
    }
}

function submitAction (formName, action, index) {
    if (formName+action+String(index) != lastAction) {
        var actionElement = null;
        var indexElement = null;
        if (formName.length > 0) {
            inputform = getFormByName(formName);
            if (inputform) {
                if (action != null && action != "") actionElement = getElementOfFormByName(actionElementName, inputform);
                if (index != null && index != "-1") indexElement = getElementOfFormByName(indexElementName, inputform);
            }
        } else {
            actionElement = getElementByName(actionElementName, null);
            indexElement = getElementOfFormByName(actionIndex, inputform);
        }
        if (actionElement) {
            actionElement.value = action;
        } else if (action != null && action != "") {
            alert ("Inputfield '"+actionElementName+"' not found!");
        }
        if (indexElement) {
            indexElement.value = index;
        } else if (index != null && index != "-1") {
            alert ("Inputfield '"+indexElementName+"' not found!");
        }
        if (inputform) {
            // if (submitAllowed(inputform)) inputform.submit();
            lastAction = formName+action+String(index);
            showWaitCursor();

            var actionString;
            if (arguments[3] != null) {
              actionString = "inputform.method='"+arguments[3]+"';inputform.submit()";
            } else {
              actionString = "inputform.submit()";
            }
            window.setTimeout(actionString,100);
            window.setTimeout("removeLastHref()",1);
        } else {
            alert ("Form with name '"+formName+"' not found!");
        }
    } else {
            alert ("Bitte nur einmal klicken!");
    }
}

var lastButtonPressed;

function removeLastHref () {
    if (lastHref) {
      lastHref.removeAttribute("href");
      if (lastHref.blur) lastHref.blur();
    }
}

function pressButton (button) {
    if (button != lastButtonPressed && buttonHrefMemo[button] == "") {
        releaseButton();

        var bobj = eval("document.img_"+button);
        bobj.src = buttonList[button]["pressed"].src;

        lastButtonPressed = button;
    }
}

function enableButton (button) {
    if (buttonHrefMemo[button] != "") {
    	var bobj = eval("document.img_"+button);
      	bobj.src = buttonList[button]["released"].src;

	bobj.parentNode.setAttribute("href",buttonHrefMemo[button]);
	buttonHrefMemo[button] = "";
    }
}

function disableButton (button) {
    if (buttonHrefMemo[button] == "") {
      	var bobj = eval("document.img_"+button);
      	bobj.src = buttonList[button]["disabled"].src;

      	buttonHrefMemo[button] = bobj.parentNode.getAttribute("href");
      	bobj.parentNode.removeAttribute("href");
    }
}

function releaseButton () {
    if (lastButtonPressed && !lastAction) {
        var bobj = eval("document.img_"+lastButtonPressed);
        bobj.src = buttonList[lastButtonPressed]["released"].src;
        lastButtonPressed = null;
    }
}

function doAction (action) {
    if (action != lastAction) {
        lastAction = action;
        nextAction = action;
        showWaitCursor()
        window.setTimeout("document.location.href = nextAction.replace('&amp;','&')",100);
        window.setTimeout("removeLastHref()",0);
    } else {
        alert ("Bitte nur einmal klicken!");
    }
}

var enterAction = null;
var actInputfield = null;

function enterEvent(Ereignis) {
  var key;
  if ( navigator.appName.toLowerCase() == 'netscape' )
    key = Ereignis.which;
  else
    key = window.event.keyCode;

  if (key == 13) {
    if (enterAction) {
        actInputfield.blur();
        eval(enterAction);
    }
    return false;
  } else {
    return true;
  }
}

function registerEnterEvent() {
  enterAction = arguments[1];
  actInputfield = arguments[0];
  actInputfield.onkeydown=enterEvent;
}

function submitAllowed(formObject) {
    for (var i=0; i<submittedActions.length; i++)
        if (submittedActions[i] == formObject.elements[actionElementName].value) return false;

    submittedActions[submittedActions.length] = formObject.elements[actionElementName].value;
    return true;
}

function getElementByName (elementName, formName) {
  if (formName == null || formName.length==0) {
    for (var y=0;y<document.forms.length;y++) {
      var inputform = getFormByName(formName);
      if (inputform) return getElementByName(elementName,inputform);
    }
    return null;
  } else {
      return getElementOfFormByName(elementName, getFormByName(formName));
  }
}

function getElementOfFormByName (elementName, inputform) {
  for (var i=0;i<inputform.elements.length;i++) {
    if (inputform.elements[i].name == elementName) return inputform.elements[i];
  }
  return null;
}

function getFormByName (formName) {
  for (var y=0;y<document.forms.length;y++) {
    if (document.forms[y].name == formName) return document.forms[y];
  }
  return null;
    }


var buttonList = new Array();
var buttonHrefMemo = new Array();

function addButton(id,imgSrc,pImgSrc, dImgSrc) {
    var img = new Image();
    img.src = imgSrc;

    if (pImgSrc) {
        var pImg = new Image();
        pImg.src = pImgSrc;
    }

    if (dImgSrc) {
        var dImg = new Image();
        dImg.src = dImgSrc;
    }

    var btn = new Array(2);
    btn["released"] = img;

    if (pImgSrc)
        btn["pressed"] = pImg;
    else
        btn["pressed"] = img;

    if (dImgSrc)
        btn["disabled"] = dImg;
    else
        btn["disabled"] = img;

    buttonList[id] = btn;
    buttonHrefMemo[id] = "";
}

function submitToDelete(doAction) {
  var jump = confirm(deleteDataMessage);
  if (jump) submitForced(doAction);
}

function submit(doAction) {
  var jump = true;
  if (changed) jump = confirm(looseDataMessage);
  if (jump) submitForced(doAction);
}

function noKeyCode(ereignis) {
  keyCode = 0;
}

function getKeyCode(ereignis) {
  keyCode = 0;
  if (navigator.appName == "Netscape") {
    keyCode = ereignis.which;
  } else {
    keyCode = window.event.keyCode;
  }
}

function looseData() {
  if (keyCode == 13) return false;

  var jump = true;
  if (changed) jump = confirm(looseDataMessage);
  return jump;
}

function popUp(action) {
  popUpWindow = window.open(action,'pop_Hilfe','width=500,height=545,dependent=yes,left=100,top=100');
  popUpWindow.name = "pop."+action;
  popUpWindow.focus();
}

function showWaitCursor() {
        if (document.getElementsByTagName) {
          document.getElementsByTagName("body")[0].style.cursor = "wait";
        }
        for (formNr = 0; formNr < document.forms.length; formNr++) {
          for (elementNr = 0; elementNr < document.forms[formNr].elements.length; elementNr++) {
            if (document.forms[formNr].elements[elementNr].style) document.forms[formNr].elements[elementNr].style.cursor = "wait";
          }
        }
        /*
        for(linkNr = 0; linkNr < document.links.length; linkNr++) {
          if (document.links[linkNr].style) document.links[linkNr].style.cursor = "wait";
        }
        */
}

function submitForced(doAction) {
        showWaitCursor();
  document.form.doAction.value = doAction;
        if (self.name.indexOf("pop") == 0 && document.form.target == "multibox_main") self.close(); else document.form.submit();
}

function writeActionForm() {

    var i=0;
    windowObject = arguments[i++];
    doActionParam = arguments[i++];

    windowObject.document.write('<form method=\"post\" name=\"form\" action=\"ServiceFC.jsp\">');
    windowObject.document.write('<input type=\"hidden\" name=\"doAction\" value=\"'+doActionParam+'\">');

    while(arguments[i]) {
      windowObject.document.write('<input type=\"hidden\" name=\"'+arguments[i++]+'\" value=\"'+arguments[i++]+'\">');
    }

    windowObject.document.write('<\/form>');
}

function writeHead(windowObject) {
    windowObject.document.write('<head><meta http-equiv=\"pragma\" content=\"no-cache\">');
    windowObject.document.write('<meta http-equiv=\"Cache-Control\" content=\"no-cache,must-revalidate\">');
    windowObject.document.write('<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\">');
    windowObject.document.write('<link rel=\"stylesheet\" href=\"\/shared\/includes\/gst.css\" type=\"text\/css\"><\/head>');
}

function writeWaitMessage(windowObject,message) {
    windowObject.document.write('<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"100%" align=\"center\">');
    windowObject.document.write('<tr>');
    windowObject.document.write('<td align=\"center\" valign=\"middle\" class=\"wait\">');
    windowObject.document.write(message);
    windowObject.document.write('<\/td><\/tr><\/table>');
}

function writeBeginOfHTML(windowObject,bgcolor,background) {
    var bgColorStr = "";
    var backgroundStr = "";
    if (arguments[0] != null) bgColorStr = " bgcolor='"+bgcolor+"'";
    if (arguments[1] != null) backgroundStr = " background='"+background+"'";
    windowObject.document.write('<html><body style=\"cursor : wait\" '+bgColorStr+backgroundStr+' >');
}

function writeEndOfHTML(windowObject) {
    windowObject.document.write('<\/body><\/html>');
}


function showHelp() {
  helpWindow = window.open('','pop_Hilfe','width=500,height=545,dependent=yes,left=100,top=100');
        helpWindow.focus();
        writeHead(helpWindow);
        writeBeginOfHTML(helpWindow,"#DFD0C0","/shared/pic/bckgrnd_1.gif");
        writeActionForm(helpWindow,'showHelp');
        writeEndOfHTML(helpWindow);
        helpWindow.document.close();
        helpWindow.document.form.submit();
}

function showProduktInfo(pNr) {
    produktWindow = window.open('','pop_ProduktInfo','width=500,height=460,dependent=yes,scrollbars=yes,left=60,top=100');
    produktWindow.focus();
    writeHead(produktWindow);
    writeBeginOfHTML(produktWindow,"#DFD0C0","/shared/pic/bckgrnd_1.gif");
    writeActionForm(produktWindow,'showProduktInfo','produktnr_info',pNr);
    writeEndOfHTML(produktWindow);
    produktWindow.document.close();
    produktWindow.document.form.submit();
}

function showLieferschein(aNr,locale) {
    lieferschein = window.open('','pop_Lieferschein','width=690,height=550,dependent=yes,menubar=yes,scrollbars=yes,left=60,top=100');
    lieferschein.focus();
    writeHead(lieferschein);
    writeBeginOfHTML(lieferschein,"#FFFFFF");
    writeActionForm(lieferschein,'showLieferschein','auftragnr_auswahl',aNr);

    if (locale.toUpperCase() == "DE") {
      writeWaitMessage(lieferschein,'<p>Bitte warten!!!<\/p><p>Formulardaten werden generiert...<\/p>');
    } else {
      writeWaitMessage(lieferschein,'<p>Please wait!!!<\/p><p>generating formular...<\/p>');
    }

    writeEndOfHTML(lieferschein);

    lieferschein.document.close();
    lieferschein.document.form.submit();
}

function showDeliveryPrint(startDatum,endDatum,locale) {
    deliveryPrint = window.open('','pop_Liefernachweis','width=690,height=550,dependent=yes,menubar=yes,scrollbars=yes,left=60,top=100');
    deliveryPrint.focus();
    writeHead(deliveryPrint);
    writeBeginOfHTML(deliveryPrint,"#FFFFFF");
    writeActionForm(deliveryPrint,'submitPrintDelivery','startdatum',startDatum,'enddatum',endDatum);

    if (locale.toUpperCase() == "DE") {
      writeWaitMessage(deliveryPrint,'<p>Bitte warten!!!<\/p><p>Formulardaten werden generiert...<\/p>');
    } else {
      writeWaitMessage(deliveryPrint,'<p>Please wait!!!<\/p><p>generating formular...<\/p>');
    }

    writeEndOfHTML(deliveryPrint);

    deliveryPrint.document.close();
    deliveryPrint.document.form.submit();
}

function renderColorBox(color, id) {
  var width = 12, height = 12;
  document.write('<table border="0" align="left" ><tr><td id='+id+' bgcolor="#'+color+'" ><img src="include/pixel.gif" align="middle" width="'+width+'" height="'+height+'" border="0" alt=""/></td></tr></table>&nbsp;');
}




