/*
This file is part of pDAF.
Copyright (c) 2005-2010 by Gregorcic & Helger IT systems OEG.
All rights reserved.
http://www.phloc.com
 */

/**
 * Specialized function for doing a server-side logging
 * 
 * @param sDesiredURI
 *          AJAX URI to use. Pass <code>null</code> if unsure.
 * @param sMessage
 *          The message to be logged.
 * @param sLogLevel
 *          The log level to be used. May be <code>null</code>.
 * @param sLoggerName
 *          The name of the logger to use. May be <code>null</code>.
 */
function phloc_server_log(sDesiredURI, sMessage, sLogLevel, sLoggerName) {
  sajax_do_call(sDesiredURI, "log", null, [ sMessage, sLogLevel, sLoggerName ]);
}

function phloc_server_error(sMessage) {
  phloc_server_log(null, sMessage, "error", null);
}

// ========================================================
function pDAFAjaxClass()
// ========================================================
{}

// ========================================================
pDAFAjaxClass.prototype =
// ========================================================
{
  /** function to trigger an AJAX call for keeping the session alive */
  pdafSessionStartKeepAlive : function() {
    sajax_do_call(null, "keepSessionAlive",
        pDAFAjax.pdafCallbackSessionKeepAlive);
  },

  pdafCallbackSessionKeepAlive : function(sResultXHTMLSniplet) {
    window.setTimeout(pDAFAjax.pdafSessionStartKeepAlive, 60000);
  }
}

var pDAFAjax = window.pDAFAjax = new pDAFAjaxClass();

function pdaf_jump_on_select(o) {
  if (o)
    jphloc.href(o.options[o.selectedIndex].value);
}

var g_nPopupCounter = 0;
var g_bAnyModifications = false;
var g_sRequestedHref = false;

// Show the help window
function pdaf_show_help(sURL) {
  window.open(sURL, '', 'dependent=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbars=no,width=600,height=400');
}

// Create a new popup every time by appending a unique number
function pdaf_default_popup(sURL, sTitle, nWidth, nHeight, bResizable, bStatus) {
  pdaf_default_popup_static(sURL, sTitle + g_nPopupCounter, nWidth, nHeight, bResizable, bStatus);
  g_nPopupCounter++;
}

// Simply open a popup window and focus it
function pdaf_default_popup_static(sURL, sTitle, nWidth, nHeight, bResizable, bStatus) {
  var oNewWin = window.open(sURL, sTitle, "width=" + nWidth
      + ",height=" + nHeight + ",resizable=" + (bResizable ? "yes" : "no")
      + ",status=" + (bStatus ? "yes" : "no"));
  oNewWin.focus();
}

function pDAF_formSubmitWithHiddenField(sFormID, sFieldName, sValue) {
  var aForm = jphloc.ge(sFormID);
  if (aForm) {
    var aHidden = jphloc.ce("input");
    aHidden.setAttribute("type", "hidden");
    aHidden.setAttribute("name", sFieldName);
    aHidden.setAttribute("value", sValue);
    aForm.appendChild(aHidden);
    aForm.submit();
  }
}

// this cache can be used to store values between windows
// (must always be executed in the parent window to work in IE
// is currently used by the file browser...

var oPDAFPropertyCache = new Object();

oPDAFPropertyCache.setProperty = function(key, value) {
  if (!this.props)
    this.props = new Array();
  this.props[key] = value;
}

oPDAFPropertyCache.getProperty = function(key) {
  if (!this.props)
    this.props = new Array();
  return this.props[key];
}

