/**
* (c) InfoProjects bv. - www.infoprojects.nl
*
* $Revision: 1.83 $
* $Date: 2011/08/23 08:58:09 $
*/


var refererWindow = parent.window ? parent.window : window.top; //window.dialogArguments?window.dialogArguments:window.opener;
var CurrentTarget;
var inDialog = true;
var inSubwindow = false;
var nestedDialog = false;
var currentDialogWin = null;
var parentDialogWin = null;
var dialogTitleLength = 56; // lengte van de titel van het dialoogje

function startDialog(forcedHeight) {
  storeOrgValues();

  top.parentDialogWin = top.currentDialogWin;
  top.currentDialogWin = window;

  if (top.getElt("DialogTitleText_" + top.currentDialogID) && document.getElementsByTagName("title")[0]) {
    var dialogTitleBar = top.getElt("DialogTitleText_" + top.currentDialogID);
    if (document.getElementsByTagName("title")[0].innerHTML.length >= dialogTitleLength) {
      dialogTitleBar.innerHTML = document.getElementsByTagName("title")[0].innerHTML.substring(0, dialogTitleLength) + '[...]';
      top.setAttr(dialogTitleBar, 'title', document.getElementsByTagName("title")[0].innerHTML);
    } else {
      dialogTitleBar.innerHTML = document.getElementsByTagName("title")[0].innerHTML;
    }
  }
  top.setTrafficStatus(false);
  top.showDialog();
  startForm();
  checkForm();
  resizeDialog(forcedHeight);
  try {
    if (focusFirstInput) {
      window.setTimeout('focusFirstInput()', 300);
    }
  } catch (e) { } //ivm. compatibiliteit met plugs
}

var noDialogResize = false;

function resizeDialog(forcedHeight) {
  if (noDialogResize && !forcedHeight) {
    return;
  } else {
    curId = top.currentDialogID;
    if (curId != null) {
      dialogHeight = forcedHeight ? forcedHeight : document.body.scrollHeight;
      setEltHeight(top.getElt("DialogFrame_" + curId), dialogHeight + 25);
      setEltHeight(top.getElt("Dialog_" + curId), dialogHeight + 51);
      scroll(0, 0);

      top.dialogFitInside = top.getWindowInnerHeight() > (dialogHeight + top.rightDialogMargin + 25);

      if (top.dialogFitInside && !is.ie6) {
        setEltTop(top.getElt("Dialog_" + curId), 35);
        setEltPosition(top.getElt("Dialog_" + curId), 'fixed');
      } else {
        setEltPosition(top.getElt("Dialog_" + curId), 'absolute');
        setEltTop(top.getElt("Dialog_" + curId),top.getWinScrollTop());
      }
    }
  }
}


/**
*
* Read dialog form and pass dirty elements and their clusters to Form
*
* @param arguments Name-value pairs for elements
*
*/

function doDialogAction() {
  var returnArray = new Array();
  var returnObject = new Object();
  var isAction = false;
  var isSilent = false;
  var showInBigScreen = false;
  var target = "self";

  var clusterDirty = checkClusterDirty(document.forms[0]);

  checkForm();
  if (!formValid) return;

  for (var i = 0; i < doDialogAction.arguments.length - 1; i += 2) {
    // add forced arguments to array
    n = doDialogAction.arguments[i];
    v = doDialogAction.arguments[i + 1];

    returnObject[n] = true;
    returnArray[returnArray.length] = n;
    returnArray[returnArray.length] = v;

    if (document.forms[0].elements[n] && (document.forms[0].elements[n].type == "text" || document.forms[0].elements[n].type == "password" || document.forms[0].elements[n].type == "hidden")) {
      document.forms[0].elements[n].value = v;
    }

    if (n.indexOf(".$action") != -1) {
      clusterDirty[n.substring(0, n.indexOf(".$action"))] = true;
      if (n == "Dummy.$action") showInBigScreen = true;
      isAction = true;
    }
    else if (n == "mode" && (v == "silent" || v == "change")) isSilent = true;
    if (n == "target") target = v;
  }

  tagsAllowed = new Array(".*"); attributesAllowed = new Array(".*:.*");
  returnArray = addFormToArray(document.forms[0], returnObject, clusterDirty, returnArray);

  for (var i = 0; i < returnArray.length - 1; i += 2) {
    if (returnArray[i].indexOf(".$action") != -1) isAction = true;
  }

  if (nestedDialog) isAction = false;

  try {
    if (nestedDialog && top.parentDialogID != null) {
      var iDoc = top.getFrameDocument("DialogFrame_" + top.parentDialogID);
      top.returnDialogResults(returnArray, !isAction, isSilent, target, iDoc.forms[0], iDoc);
    }
    else if (showInBigScreen && refererWindow && refererWindow.inSearchResults && refererWindow.parent.refererWindow && refererWindow.parent.refererWindow.returnDialogResults) {
      refererWindow.parent.refererWindow.returnDialogResults(returnArray, !isAction, isSilent, target);
    }
    else if (refererWindow.inExternalWindow && refererWindow.opener && refererWindow.opener.returnDialogResults) {
      refererWindow.opener.returnDialogResults(returnArray, !isAction, isSilent, target);
    }
    else if (refererWindow && refererWindow.returnDialogResults) {
      refererWindow.returnDialogResults(returnArray, !isAction, isSilent, target);
    }
    else {
      setStatus("..");
    }
  }
  catch (e) {
    setStatus(e);
    try {
      window.setTimeout('doDialogAction()', 500);
    }
    catch (e) {
      setStatus(e);
    }
  }
}

function switchTabs(tabNum, totalTabs) {
  var tabTotal = totalTabs ? totalTabs : 10;
  for (var tabCount = 0; tabCount < tabTotal; tabCount++) {
    if (getElt("Tab_td_" + tabCount)) {
      getElt("Tab_td_" + tabCount).className = (tabCount == tabNum) ? "seltabTD" : "";
    }
    if (getElt("Tab_" + tabCount)) {
      getElt("Tab_" + tabCount).className = (tabCount == tabNum) ? "seltab" : "tab";
    }
  }
}

