/**
 * Opens a pop up window
 *
 * @param   string  url
 * @param   integer width
 * @param   integer height
 * @param   string  title
 * @return  boolean
*/
function popup(url) {

  var width = (arguments.length > 1) ? parseInt(arguments[1]) : 620;
  var height = (arguments.length > 2) ? parseInt(arguments[2]) : 380;
  var title = (arguments.length > 3) ? arguments[3] : '';
  var props = "width=" + width + ",height=" + height + ",resizable=yes,menubar=no,locationbar=no,status=no,scrollbars=yes,depend=yes,left=10,top=10";
  var popup;

  if (popup = window.open(url, title, props)) {

    popup.focus();
    return true;
  }

  return false;
} // end function popupwindow

/**
* Ask the user, if he/she wants to delete a record.
*
* @param   string  target_url
* @access  public
*/
function confirmDelete(target_url) {

  var text = (arguments.length > 1) ? parseInt(arguments[1]) : 'Möchten Sie diesen Datensatz wirklich löschen?';
  if(confirm(text))
    window.location.href = target_url;
} // end function confirmDelete


/**
* Handles challenge/response login (CHAP)
*
* Sets the response value of login form and deletes the password entry
* Note: fields must be identified as
*  - username (text input field for username)
*  - password (text input field for password)
*  - response (hidden field for calculated string)
*  - challenge (hidden field for unique seed)
* Note: the form has to be identified as 'login_form'
*
* @access   public
*/
function doChallengeResponse() {

  response = document.getElementById('username').value + ':' +
    hex_md5(document.getElementById('password').value) + ":" +
    document.getElementById('challenge').value;
  // set response
  document.getElementById('response').value = hex_md5(response);
  document.getElementById('password').value = "";
  document.getElementById('login_form').submit();
} // end function doChallengeResponse

/**
* Set bookmark
*
* @param    string    url
* @param    string    title
* @access   public
*/
function setBookmark(url, title) {

  if (document.all)
    window.external.AddFavorite(url, title);
  else if (window.sidebar)
    window.sidebar.addPanel(title, url, '');
} // end function setBookmark


/**
* Go to url which is defined by selector
*
* @param    object    selector
* @access   public
*/
function goTo(selector) {

  if((target = selector.elements['KatOptions'].options[selector.elements['KatOptions'].selectedIndex].value) != '')
    window.location.href = target;

  return false;
}  // end function goTo

Mercator = {
	setCookie: function(c_name, value, expiredays)
	{
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
	},

	getCookie: function(c_name)
	{
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=");
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) {
					c_end=document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	},

	deleteCookie: function(cookie_name)
	{
		var cookie_date = new Date();
		cookie_date.setTime(cookie_date.getTime() - 1);
		document.cookie = cookie_name + "=; expires=" + cookie_date.toGMTString();
	},

	checkCookieEnabled: function(nodeId)
	{
		Mercator.setCookie('mcTest', '1', null);
		if (Mercator.getCookie('mcTest')) {
			Mercator.deleteCookie('mcTest');
		} else {
			document.getElementById(nodeId).style.display = 'block';
		}
	}
};

