/* Xela utils JavaScript */
/* Copyright 2004 Psycle Interactive Ltd. */
/* $Revision: 10524 $ */

// Change the css class of an object
function changeClass(obj, className) {
  obj.setAttribute('class', className, 0);
}

// Next two functions are for general layer (div) management
function showLayer(layerID) {
  document.getElementById(layerID).style.display="block";
}

function hideLayer(layerID) {
  document.getElementById(layerID).style.display="none";
}

// Next two functions are for handling popup message div objects
function showPopupMessage(popupMessageID) {
  document.getElementById(popupMessageID).style.display="block";
}

function hidePopupMessage(popupMessageID) {
  document.getElementById(popupMessageID).style.display="none";
}

// Add an OPTION to a SELECT list
function addOptionToSelect(formName, listName, newValue, newLabel) {
  // Append new value to end of visible list and end of hidden field
  if ((newValue != "") && (newLabel != "")) {
    itemCount = listName.options.length;
    listName.options.length = itemCount + 1;
    listName.options[itemCount].value = newValue;
    listName.options[itemCount].text = newLabel;
  }
}

// Remove an OPTION from a SELECT list
function removeOptionFromSelect(listName) {
  // Remove the selected item from the list.
  listName.options[listName.selectedIndex] = null;
}

// Set the VALUE of a TEXT INPUT
function setTextValue(fieldName, value) {
  if (fieldName) fieldName.value = value;
}

// Get the VALUE of a TEXT INPUT
function getTextValue(fieldName) {
  if (fieldName) return fieldName.value;
  else return '';
}

function setDateRangeValue(id, value, targetField) {
  currentValue = targetField.value;
  if (id == "start") {
    startValue = value;
    endValue = currentValue.split(" - ")[1];
    if (endValue == null || endValue == "" || endValue == "undefined") endValue = "none";
  } else {
    endValue = value;
    startValue = currentValue.split(" - ")[0];
    if (startValue == null || startValue == "" || startValue == "undefined") startValue = "none";
  }
  targetField.value = startValue + " - " + endValue;
}

function setFieldValue(formName, value) {
  // Sets the value of a text entry box in a given form.
  // The boxes that can be set must all be named 'field'.
  // The one that is set depends on the selected item in
  // a radio group named radioGroup.

  for (i = 0; i < document.forms[formName].radioGroup.length; i++)
    if (document.forms[formName].radioGroup[i].checked)
      break;

  document.forms[formName].field[i].value = value;
}

function setDateFieldValue(formName, value) {
  // Sets the value of a text entry box in a given form.
  // The boxes that can be set must all be named 'field'.
  // The one that is set depends on the selected item in
  // a radio group named range_option.

  if (document.forms[formName].range_option[0].checked) {
    document.forms[formName].start_date.value = value;
  } else if (document.forms[formName].range_option[1].checked) {
    document.forms[formName].end_date.value = value;
  } else {
    document.forms[formName].start_date.value = value;
  }
}

function clearStatus() {
  window.status="";
  return true;
}

function confirmDocumentLoad(message, URL) {
  if (confirm(message)) {
    location.replace(URL);
  }
}

function uploadFile(formName, fieldName) {
  if (fieldName.value != "") {
    // Ask the top window to open the please wait box so we are allowed to close
    // it again without a warning

    top.openFileUploadWindow();
    formName.submit();
  } else {
    alert("Please click the browse button and select a local file,\nthen click this link again.");
  }
}

function commitEntity(formName) {
    top.openCommitPleaseWaitWindow();
    formName.submit();
}

function createFolder(formName, fieldName) {
  if (fieldName.value != "") {
    formName.submit();
  } else {
    alert("Please type a folder name,\nthen click this link again.");
  }
}

function renameFileFolder(formName, fieldName) {
  if (fieldName.value != "") {
    formName.submit();
  } else {
    alert("Please enter a new name,\nthen click this link again.");
  }
}

var win_please_wait;

function openFileUploadWindow() {
  var popupWidth = 150, popupHeight = 100;
  var screenHeight = 300, screenWidth = 790;

  if (window.screen) {
    screenHeight = screen.availHeight / 2;
    screenWidth = screen.availWidth - 10;
  }

  var popupLocX = (screenWidth - popupWidth) / 2;
  var popupLocY = (screenHeight - popupHeight) / 2;

  win_please_wait = MM_openBrWindow("", "win_please_wait", "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=150,height=100,left=" + popupLocX + ",top=" + popupLocY);
  win_please_wait.document.writeln("<html><head><title>Please Wait</title></head><body><table bgcolor=\"#FFCC00\" width=\"100%\" height=\"100%\"><tr><td style=\"text-align:center; vertical-align:middle; font-size:11pt; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif\">Please wait while file is uploaded</td></tr></table></body></html>");
}

function openCommitPleaseWaitWindow() {
  var popupWidth = 200, popupHeight = 150;
  var screenHeight = 300, screenWidth = 790;

  if (window.screen) {
    screenHeight = screen.availHeight / 2;
    screenWidth = screen.availWidth - 10;
  }

  var popupLocX = (screenWidth - popupWidth) / 2;
  var popupLocY = (screenHeight - popupHeight) / 2;

  win_please_wait = MM_openBrWindow("", "win_please_wait", "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=150,height=100,left=" + popupLocX + ",top=" + popupLocY);
  win_please_wait.document.writeln("<html><head><title>Please Wait</title></head><body><table bgcolor=\"#FFCC00\" width=\"100%\" height=\"100%\"><tr><td style=\"text-align:center; vertical-align:middle; font-size:11pt; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif\">Please wait while your changes are saved</td></tr></table></body></html>");
}

function closePleaseWaitWindow() {
  if (win_please_wait != null) {
    win_please_wait.close();
    win_please_wait = null;
  }
}

function viewFile(url) {
  // For viewing, the server is setting the HTTP header to have
  // 'Content-Disposition: inline' which will usually show the file in the
  // browser window.
  MM_openBrWindow(url, '', 'toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=700,height=500');
}

function downloadFile(url) {
  // For downloading, the server is setting the HTTP header to have
  // 'Content-Disposition: attachment' which will usually force the browser to
  // download to disk.
  document.location = url;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  win = window.open(theURL,winName,features);
  return win;
}

function MM_openBrWindowNoReturn(theURL,winName,features) { //v2.0
  win = window.open(theURL,winName,features);
}

function storeValuesInHidden(form) {
  for (i = 0; i < form.length; i++) {
    element = form.elements[i];
    if ((element.type == "select-one") && (element.name.indexOf("relation_") > -1)) {
      for (j = 0; j < element.length; j++) {
        if (j == 0)
          form.elements["hidden_" + element.name].value = element.options[j].value;
        else
          form.elements["hidden_" + element.name].value = form.elements["hidden_" + element.name].value + "," + element.options[j].value;
      }
    }
  }
}


function stringEndsWith(string, ending) {
  var regex = new RegExp(ending + "$");
  return regex.test(string);
}

function refreshWidgetFrame() {
  if ((top.widget != null) && (top.widget.history != null) && (top.widget.history.length > 0))
    top.widget.history.go(0);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

var dayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var now = new Date;
var dayDate = now.getDate();
var dateSuffix = "th";

if ((dayDate=="1")||(dayDate=="21")||(dayDate=="31")) {dateSuffix="st";}
if ((dayDate=="2")||(dayDate=="22")) {dateSuffix="nd";}
if ((dayDate=="3")||(dayDate=="23")) {dateSuffix="rd";}

