/*
 * pageContent.js
 *
 * IT-Sundhed
 * Lasse F. Pedersen
 * 25/Mar/2004
 *
 * Updates page content on different events
 */

// Set visibility of an object by object ID.
function setVisibility(targetId, visible) {

  var target = document.getElementById(targetId);
  
  if(visible == true) {
    target.style.display = "block";
  } else {
    target.style.display = "none";
  }
  
  // Defined in pageLoad.js
  recalcHeight();

}

// Apply checked state to visibility of an object
function applyCheckedVisible(checkBox, targetId) {

  setVisibility(targetId, checkBox.checked);

}

// Apply checked state to visibility of an object
function applyCheckedInvisible(checkBox, targetId) {

  setVisibility(targetId, !checkBox.checked);

}

// Minimize/maximize window
function resizeWindow(myWindow) {

  myWindow.className = (myWindow.className == "window_open" ? "window_closed" : (myWindow.className == "window_closed" ? "window_open" : myWindow.className));
  recalcHeight();

}

// Update export fields with data from ExportDialog
function updateExportFields(path, idFieldId, pathFieldId) {

  var idField   = document.getElementById(idFieldId);
  var pathField = document.getElementById(pathFieldId);

  if(pathField.value != "" && pathField.value.substring(0, 1) == "/") {
    path = encodeURI(pathField.value);
  }

  var result = window.showModalDialog("/CmsPraksis/Dialogs/ExportFolder.aspx?path=" + path + "&amp;selected=" + idField.value, "", "status:no");
  var values;
  
  if(result == null || result == "") {
    return;
  }

  values = result.split(",");

  idField.value   = values[0];
  pathField.value = values[1];  

}

// Update meta field with data from MetaDialog
function updateMetaField(type, fieldId) {
  
  var field   = document.getElementById(fieldId);
  var content = field.value.replace("/^\s*|\s*$/g","");
  var current;

  if(content == "") {
    current = new Array();
  } else { 
    current = content.split(",");
    for(var i = 0; i < current.length; i++) {
      current[i] = current[i].replace("/^\s*|\s*$/g","");
    }
  }
  
  //alert("current: " + current.join(","));
  var result = window.showModalDialog("/CmsPraksis/Dialogs/MetaDialog.aspx?type=" + type + "&checked=" + field.value, "", "status:no");
  var checked;
  
  if(result == null) {
    return;
  }
  
  if(result == "") {
    checked = new Array();
  } else {
    checked = result.split(",");
  }
  
  //alert("checked: " + checked.join(","));

  for(var i = 0; i < checked.length; i++) {
    var found = false;
    for(var j = 0; j < current.length; j++) {
      if(checked[i] == current[j]) {
        found = true;
      }
    }
    if(found == false) {
      current.push(checked[i]);
    }
  }      

  //alert("combined: " + current.join(","));

  field.value = current.join(",");
  
}

// Printing
function printContent() {

  if(window.event) {

    // IE
    for(var i = 0; i < document.styleSheets.length; i++) {
      if(document.styleSheets[i] && document.styleSheets[i].title == "print") {
        document.styleSheets[i].disabled = false;
      }
    }

    window.onafterprint = printContentAfter;
    window.print();

  } else {

    // Mozilla
    window.print();

  }
  
}

// Printing finished/aborted
function printContentAfter() {

  for(var i = 0; i < document.styleSheets.length; i++) {
    if(document.styleSheets[i] && document.styleSheets[i].title == "print") {
      document.styleSheets[i].disabled = true;
    }
  }

}

// Popup
function popup(url, popupW, popupH) {

  var windowW = window.document.documentElement.clientWidth;
  var windowH = window.document.documentElement.clientHeight;
  var windowT = 0;
  var windowL = 0;

  if(windowW == null || windowW == 0) {
    windowW = screen.width;
  }
  if(windowH == null || windowH == 0) {
    windowH = screen.height;
  }

  if(window.screenTop != null) {
    windowT = window.screenTop;
    windowL = window.screenLeft;
  } else if(window.screenY != null) {
    windowT = window.screenY;
    windowL = window.screenX;
  }

  var popupT = windowT + (windowH - popupH) / 2;
  var popupL = windowL + (windowW - popupW) / 2;

  if(popupT < 0) {
    popupT = 0;
  }
  if(popupL < 0) {
    popupL = 0;
  }

  window.open(url, "", "location=0,menubar=0,resizable=0,scrollbars=1,toolbar=0,width=" + popupW + ",height=" + popupH+ ",top=" + popupT + ",left=" + popupL);

}

// Popup vejviser
function popupVejviser(offset) {

  popup("/CmsPraksis/Dialogs/Vejviser/vejviser.html#" + offset, 780, 585);

}

// Popup email query
function popupEmailQuery() {

  popup("/CmsPraksis/Dialogs/IndtastEmail.aspx", 400, 180);

}

/*
 * Select data (dynamic select forms)
 */

// Select data instances
var selectData = new Array();

// Add pair to select data instances
function addSelectPair(dataKey, key, value) {
  
  var pairs = selectData[dataKey];
  
  if(!pairs) {
    selectData[dataKey] = pairs = new Array();
  }
  
  pairs[key] = value;
  
}

// Fill select options from data instance
function fillSelect(dataKey, selectId) {
  
  var select = document.getElementById(selectId);
  var pairs  = selectData[dataKey];
  
  while(select.options.length > 0) {
    select.options.remove(0); 
  }
        
  for(key in pairs) {

    var option = document.createElement("option");
    
    select.options.add(option);

    option.innerText = key;
    option.value = pairs[key];

  }
  
}

/*
// Colour iframe contents
function colourIframe(iframeId, doneWaiting) {

  if(!doneWaiting) {
    alert("before wait");
    setTimeout("colourIframe('" + iframeId + "', true)", 200);
  }

  alert("after wait");
  alert(document.getElementById(iframeId).contentWindow);

}
*/

