//\//////////////////////////////////////////////////////////////////////////////////
//\  standard.js  --  This notice must remain untouched at all times.
//\  Copyright Pentos AG 2004, 
//\
//\ 
//\
//\//////////////////////////////////////////////////////////////////////////////////
var ie6 = false;
var ie5 = false;

function shProp(obj) {
  var text = '';
  for (elem in obj) {
    text += ', ' + elem;
  }
  alert(text);
}

var errorMessages = '';

function addErrorMessage(newMessage) {
	if (errorMessages != '' && newMessage != '') {
		errorMessages += '#~#';
	}
	errorMessages += newMessage;
}

function writeErrorMessage(currentFrame) {
	var doc = currentFrame.document;
	var errMessage = doc.getElementById('idErrorMessage');
	errorText = errorMessages.split('#~#');
	var errText = '<div class="errormessage-title"><img src="js/%27%0A%09%09%2B%20%27/v1.0/img/error_sign_yellow.gif" align="absbottom" /> <a name="ErrorMessage">ATTENTION</a></div>'
		+ '<div class="errormessage-text">' + errorText.join('<br />') + '</div>';
	errMessage.innerHTML = errText;
	currentFrame.location.href = '#ErrorMessage';
}

function checkForErrorMessage(currentFrame) {
	errorMessages = currentFrame.document.forms[0].errormessage.value;
	checkErrors(currentFrame);
}

function checkErrors(currentFrame) {
	if (errorMessages == '') {
		return false;
	}
	else {
		writeErrorMessage(currentFrame);
		return true;
	}
}

function changeAHREF(obj, validationMethodName) {
	/*
	 * This function can be used to extend the domino generated _doClick Method.
	 * I had needed a javascript confirmation and that's why i use the function confirmSaveAndClose.
	 * the return of obj.onclick.toString(); is something like:
	 *
	 * function anonymous()
	 * {
	 * return _doClick('.......
	 * }
	 *
	 * you can also replace the brakets ({}) if you like but for me this was not neccessary...
	 *
	 * the new Method that i build is something like this:
	 *
	 * if (confirmSaveAndClose())
	 * {
	 * return _doClick('.......
	 * }
	 *
	 * Because i do not need the onClick Event and the onMouseDown Event any more, let's set it to null.
	 * At the end, i put the new Function into the href that the Domino Server left empty...
	 */
 	var _onClick = obj.onclick.toString();
  var _newMethod = _onClick.replace(/function.+\s*\{/, 'if (' + validationMethodName + '()) {')
 	_newMethod = _newMethod.replace(/return /, 'var ignoreThisThing = ');
 	obj.onclick = null;
 	obj.onmousedown = null;
 	obj.href = 'javascript:' + _newMethod + ';';
}

function selectAllDocs(obj, cFrame) {
  var iTeUNID = cFrame.document.forms[0].iTeUNID;
  if (obj.checked) {
    for (i = 0; i < (iTeUNID.length - 1); i++) {
      iTeUNID[i].checked = true;
    }
  }
  else {
    for (i = 0; i < (iTeUNID.length - 1); i++) {
      iTeUNID[i].checked = false;
    }
  }
}

function isSthSelected(cFrame) {
	var iTeUNID = cFrame.document.forms[0].iTeUNID;
	if (iTeUNID.length) {
		for (i = 0; i < iTeUNID.length; i++) {
			if (iTeUNID[i].checked && iTeUNID[i].value.length == 32) {
				return true;
			}
		}
	}
	else {
		if (iTeUNID.value.length == 32) {
			return true;
		}
	}
	return false;
}

function getSelectedOnlyOne(cFrame) {
	var iTeUNID = cFrame.document.forms[0].iTeUNID;
	var retValue = "0";
	if (iTeUNID.length) {
		for (i = 0; i < iTeUNID.length; i++) {
			if (iTeUNID[i].checked  && iTeUNID[i].value.length == 32) {
				if (retValue == "0") {
					retValue = iTeUNID[i].value;
				}
				else {
					return "-1";
				}
			}
		}
	}
	else {
		if (iTeUNID.value.length == 32) {
			retValue = iTeUNID.value;
		}
	}
	return retValue;
}


function showInfo(cFrame, infoID, infoTitle, infoWidth, noFix) {
  var doFix = true;
  if (noFix != null && typeof noFix != 'undefined') {
    doFix = noFix;
  }
  var message = 'Currently there is no help available.'
  var fieldTitle = ' Field help';
  var closeText =
    '<span style="text-decoration:none; font-weight:bold; font-size:12px; color:#FFF; padding-left: 15px; display: block;">x</span>';
  var divWidth = '';
  if (infoTitle != null && infoTitle != '') {
    fieldTitle = ' ' + infoTitle;
  }
  if (infoWidth != null && infoWidth != '') {
    if (parseInt(infoWidth) > 162 && doFix) {
      infoWidth = 162;
    }
    divWidth = infoWidth;
  }
  else {
    divWidth = 162;
  }
  if (typeof cFrame.infoArray != 'undefined') {
    var test = cFrame.infoArray[infoID];
    if (typeof test != 'undefined') {
      message = test;
    }
  }
  if (doFix) {
    return cFrame.overlib(message, STICKY, CAPTION, fieldTitle, CLOSETEXT, closeText,
      CLOSECLICK, WIDTH, divWidth, FIXX, '654');
  }
  else {
    return cFrame.overlib(message, STICKY, CAPTION, fieldTitle, CLOSETEXT, closeText,
      CLOSECLICK, WIDTH, divWidth);
  }
}

function expandViewEntry(cFrame, docNumber) {
  var docNum = docNumber;
  if (typeof cFrame.singleCategory != 'undefined' &&
    cFrame.singleCategory) {
    var docNumArray = docNumber.split('.');
    if (docNumArray.shift != null) {
      docNumArray.shift();
    }
    else {
      docNumArray = psShift(docNumArray);
    }
    docNum = docNumArray.join('.');
  }
  cFrame.location.href =
    cFrame.viewName + '?OpenView&Expand=' + docNum + "#viewCategory"
    + docNumber;
}

function expandAll(cFrame) {
  cFrame.location.href =
    cFrame.viewName + '?OpenView&ExpandView';
}

function collapseAll(cFrame) {
  cFrame.location.href =
    cFrame.viewName + '?OpenView&CollapseView';
}

function startsWith(t1, t2) {
  var t1Part = t1.substr(0, t2.length);
  if (t1Part == t2) {
    return true;
  }
  else {
    return false;
  }
}

function getSingleCategoryNumber(cFrame) {
  for (var i = 0; i < cFrame.document.links.length; i++) {
    var curID = cFrame.document.links[i].id;
    if (startsWith(curID, 'idExpander')) {
      if (curID.indexOf('.') > 0) {
        return curID.substring(10, curID.indexOf('.') + 1);
      }
      else {
        alert('An Error has occured. Please contact your Application Developer!');
        return '';
      }
    }
  }
}

function changeViewTwistie(cFrame) {
  if (cFrame.currentExpanded == 'All') {
    for (var i = 0; i < cFrame.document.links.length; i++) {
      var curID = cFrame.document.links[i].id;
      if (startsWith(curID, 'idExpander')) {
        if (cFrame.document.links[i].className == 'view-category-collapsed') {
          cFrame.document.links[i].className = 'view-category-expanded';
        }
        else {
          cFrame.document.links[i].className = 'v-cat-exp';
        }
      }
    }
  }
  else if (cFrame.currentExpanded != '') {
    var curExpanded = cFrame.currentExpanded;
    if (typeof cFrame.singleCategory != 'undefined' &&
      cFrame.singleCategory) {
      curExpanded = getSingleCategoryNumber(cFrame) + curExpanded;
    }
    var currentExpandedArray = curExpanded.split('.');
    var currentIDName = '';
    for (var i = 0; i < currentExpandedArray.length; i++) {
      if (currentIDName != '') {
        currentIDName += '.';
      }
      currentIDName += currentExpandedArray[i];
      var currentID = cFrame.document.getElementById('idExpander'
        + currentIDName);
      if (currentID != null && typeof currentID != 'undefined') {
        if (currentID.className == 'view-category-collapsed') {
          currentID.className = 'view-category-expanded';
        }
        else {
          currentID.className = 'v-cat-exp';
        }
      }
    }
  }
}

var myWindow = null;

function windowClose() {
  if (myWindow != null && typeof myWindow != 'undefined'
    && !myWindow.closed) {
    myWindow.close();
  }
}

function windowOpener (cFrame, url, title, w, h, t, l, st, mb, sb, rz) {
	if (url == null || url == '') {
		alert('Wrong URL!');
		return false;
	}
	if (title == null || title == '')
		title ='window1';
	title = title.replace(/\W/g, '_');
	var breite = '671';
	if (w != null && w != '' && !isNaN(w))
		breite = w;
	var hoehe = '600';
	if (h != null && h != '' && !isNaN(h))
		hoehe = h;
	var oben = '224';
	if (t != null && t != '' && !isNaN(t))
		oben = t;
	var links = '168';
	if (l != null && l != '' && !isNaN(l))
		links = l;
	var status = '';
	if (st != null && st)
		status = ', status=yes';
	if (st != null && !st)
		status = ', status=no';
	var menubar = '';
	if (mb != null && mb)
		menubar = ', menubar=yes';
	if (mb != null && !mb)
		menubar = ', menubar=no';
	var scrollbars = '';
	if (sb != null && sb)
		scrollbars = ', scrollbars=yes';
	if (sb != null && !sb)
		scrollbars = ', scrollbars=no';
	var resizable = '';
	if (rz != null && rz)
		resizable = ', resizable=yes';
	if (rz != null && !rz)
		resizable = ', resizable=no';
	myWindow = cFrame.window.open(url, title, 'width=' + breite + ', height=' + hoehe + ', top=' + oben + ', left=' + links + status + menubar + scrollbars + resizable);
	myWindow.focus();
}

function getCookie(cFrame, cookieName) {
	var search = cookieName + '=';
	if (cFrame.document.cookie.length > 0) {
		var offset = cFrame.document.cookie.indexOf(search);
		if (offset > -1) {
			offset += search.length;
			var ende = cFrame.document.cookie.indexOf(';', offset);
			if (ende == -1) {
				ende = cFrame.document.cookie.length;
			}
			return unescape(cFrame.document.cookie.substring(offset, ende));
		}
		else {
			return '';
		}
	}
}

function setCookie(cFrame, cookieName, cookieValue, cookieTime, cookiePath, cookieDomain) {
	var expires = '';
	if (cookieTime != null && cookieTime !='' && !isNaN(cookieTime)) {
		var expiresDate = new Date();
		var today = new Date();
		expiresDate.setTime(today.getTime() + cookieTime);
		expires = '; expires=' + expiresDate.toGMTString();
	}
	var path = '';
	if (cookiePath != null && cookiePath != '') {
		path = '; path=' + cookiePath;
	}
	var domain = '';
	if (cookieDomain != null && cookieDomain != '') {
		domain = '; domain=' + cookieDomain;
	}
	cFrame.document.cookie = cookieName + '=' + escape(cookieValue) + path + domain + expires;
}

function rememberURL(cFrame) {
	setCookie(cFrame, 'TotalRecall', cFrame.location.href, '', cFrame.cookiePath, cFrame.cookieDomain);
}

function returnToView(cFrame, defaultURL) {
	var retURL = getCookie(cFrame, 'TotalRecall');
	if (retURL != '' && typeof retURL != 'undefined') {
		cFrame.location.href = retURL;
	}
	else {
		if (defaultURL != null && defaultURL != '') {
			cFrame.location.href = defaultURL;
		}
		else {
			alert('No URL to return to!');
		}
	}
}

function psErrorHandler(nachricht,datei,zeile) {
  var fehler = 'Errormessage:\n' + nachricht + '\n' + datei
    + '\n' + zeile;
  showErrorMessage(fehler);
  return true;
}

function showErrorMessage(fehler) {
  alert(fehler)
}

function psShift(arr1) {
  if(typeof arr1[0] == 'undefined') {
    return arr1;
  }
  var newArr = new Array();
  for (var i = 1; i < arr1.length; i++) {
    newArr[i-1] = arr1[i];
  }
  return newArr;
}

function pickpersons(returnToField, multi, cFrame){
  var conFrame = null;
  if (cFrame != null && typeof cFrame != 'undefined') {
    conFrame = cFrame;
  }
  else {
    conFrame = top.content;
  }
	if(multi){
	  windowOpener (conFrame, '/gin/svcs/dir.nsf/pickpersons?OpenPage&returntofield=' +
      returnToField + '&fields=returntofield', 'SelectPerson', '762', '315')
	} else {
	  windowOpener (conFrame, '/servlet/PersonPicker?action=pickperson&returntofield=' +
      returnToField + '&fields=returntofield', 'SelectPerson', '462', '340')
	}
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
