/*
 * 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
  recalcTextHeight();
  //recalcHeight();

}

// Set visibility of an object by object ID.
function setVisibilityInline(targetId, visible) {

  var target = document.getElementById(targetId);
  
  if(visible == true) {
    target.style.display = "inline-block";
  } else {
    target.style.display = "none";
  }
  
  // Defined in pageLoad.js
  recalcTextHeight();
  //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);

}

// Apply checked state to visibility of an object
function applyCheckedVisibleInline(checkBox, targetId) {

  setVisibilityInline(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();

}

//Select 4 links to the healthbox and set the id/ids in the hidden textbox
function selectHealthBox(checkbox, checkboxId , idboxId, countboxId) {
  var idbox     = document.getElementById(idboxId);
  var countbox  = document.getElementById(countboxId);
  var count     = parseInt(countbox.value);
  var ids;
  
  if (checkbox.checked == true) {
    if (count == 4) {
      alert("Det er kun muligt at tilføje 4 links.");
      checkbox.checked = false;
    } else {
      idbox.value += checkboxId +",";
      count += 1;  
      countbox.value = count;
    }
  } else {
    var selected = idbox.value;
    var newNums = ""; 
    
    if (selected != "") {
      ids = selected.split(","); 
      for(var i = 0; i < ids.length -1; i++) {
        if (parseInt(ids[i]) != parseInt(checkboxId)) { 
          newNums += ids[i] + ",";
        } 
      }
      idbox.value = newNums;
    } 
    count -= 1; 
    countbox.value = count;
  }

}

// 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("/CmsSundhed/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("/CmsSundhed/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(logoSrc) {

  if(window.event) {
  
    // IE
    for(var i = 0; i < document.styleSheets.length; i++) {
      if(document.styleSheets[i] && document.styleSheets[i].title == ("print_" + logoSrc)) {
        document.styleSheets[i].disabled = false;
      }
    }

    window.onafterprint = printContentAfter;
    window.print();

  } else {

    // Mozilla
    window.print();

  }
  
}

// Printing finished/aborted
function printContentAfter(logoSrc) {

  for(var i = 0; i < document.styleSheets.length; i++) {
    if(document.styleSheets[i] && document.styleSheets[i].title == ("print_" + logoSrc)) {
      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("/CmsSundhed/Dialogs/Vejviser/vejviser.html#" + offset, 838, 630);

}

// Popup email query
function popupEmailQuery() {

  popup("/CmsSundhed/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];

  }
  
}

// Show the div from the TopLinks this can be used to display content on a onmouseover
function show(str)
{
  if (document.all)
  {
	var e = document.getElementById("picpreview");
    e.style.left = window.event.clientX + 10 + document.body.scrollLeft;
    e.style.top = window.event.clientY + document.body.scrollTop;
    e.innerHTML = "<table bgcolor='#444444' cellpadding='0' cellspacing='1'><tr><td><table cellpadding='0' cellspacing='0' bgcolor='#cccccc'><tr><td><img src='/CmsSundhed/Images/HealthBox/" + str + "'></td></tr></table></td></tr></table>";
    e.style.visibility = "visible";
  }
}

// Hide the div from the TopLinks this can be used with a onmouseout
function hide()
{
  if (document.all)
  {
    document.getElementById("picpreview").style.visibility = "hidden";
  }
}

function letterBoxPass(passId, passRepeatId)
{
  var pass   = document.getElementById(passId);
  var passRepeat = document.getElementById(passRepeatId);
  
  if ( pass.value != "" )
  {  
    if ( passRepeat.value != "" )
    {
      if ( pass.value != passRepeat.value )
      {  
        alert("Du skal angive det samme password to gange.")
      }
    }
    else
    {
    alert("Du skal udfylde begge password felter.")
    }
  }
  else
  {
    alert("Du skal udfylde begge password felter.")
  }
}

function openDiv(openId, textId, hiddenId)
{
  var div = document.getElementById(openId);
  var textDiv = document.getElementById(textId);
  var showAdvHidden = document.getElementById(hiddenId)
  
  if (div.style.display == 'none')
  {
    div.style.display = 'block';
    textDiv.innerText = 'Tryk her for at vende tilbage til standards\u00F8gning'
    showAdvHidden.value = '1'
  }
  else
  { 
    div.style.display = 'none';
    textDiv.innerText = 'Tryk her for avanceret s\u00F8gning'
    showAdvHidden.value = '0'
  }  
  recalcTextHeight();
} 

function checkHidden(openId, textId, hiddenId)
{
  var div = document.getElementById(openId);
  var textDiv = document.getElementById(textId);
  var showAdvHidden = document.getElementById(hiddenId)
  if (showAdvHidden.value == '1')
  {
    div.style.display = 'block';
    textDiv.innerText = 'Tryk her for at vende tilbage til standards\u00F8gning'
  }

}
function SelectAllContains(string)
{
  var cb = document.getElementsByTagName("input");
  var i;
  for (i = 0; i < cb.length; i++)
  {
    if (cb(i).type == "checkbox")
    {
      if (cb(i).id.match(string) == string)
      {
        cb(i).checked = true;
      }
    }
  }
}

function ClearAllContains(string)
{
  var cb = document.getElementsByTagName("input");
  var i;
  for (i = 0; i < cb.length; i++)
  {
    if (cb(i).type == "checkbox")
    {
      if (cb(i).id.match(string) == string)
      {
        cb(i).checked = false;
      }
    }
  }
}

function insertTopLink(id) 
{
  var dlgPath   = null;
  var dlgArgs   = null;
  
  var textBox = document.getElementById(id);
  var result 
  
  // Open internal link browser
  dlgPath   = IDS_FRAMEWORK_NEW_VIRTUAL_PATH + "/Dialogs/InternalLinks/InternalLinks.aspx?NRMODE=Unpublished&wbc_caller=IEModal";
  dlgArgs   = "";
  result = window.showModalDialog(dlgPath, dlgArgs, IDS_IEWIN_INTLINKS_FEATURES);
  
  if (result == "http://Channels/")
    textBox.innerText = "";
  else if (result == "Cancel")
    return;
  else 
    textBox.innerText = result;
}

function openClose(divId)
{
  var div = document.getElementById(divId);

  if (div.style.display == "none")
    div.style.display = "block";
  else if (div.style.display == "block")
    div.style.display = "none";
}

function openCloseInIframe(divId, iframeId)
{
  var div = document.getElementById(divId);

  if (div.style.display == "none")
    div.style.display = "block";
  else if (div.style.display == "block")
    div.style.display = "none";

  iframeHeight(iframeId);
}


function test()
{
  alert("test");
}

function newWindow(link, specs)
{
  window.open(link,'',specs)


}


/*
// 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);

}
*/

