/*
nc    =  Netscape Navigator der vierten Generation
moz   =  Mozilla basierte Browser wie Netscape 7
op6   =  Opera 5/6 (kennt document.documentElement nicht)
op7   =  Opera 7
ie    =  Alle Internetexplorer
ie4   =  Internet Explorer der vierten Generation
ie5Up =  Internet Explorer an Generation 5
dom   =  Alle Dom-fähigen Browser
*/
var nc    =  (document.captureEvents  && !document.getElementById)?       true : false;
var moz   =  (document.captureEvents  && document.documentElement)?       true : false;
var op6   =  (document.getElementById && !document.documentElement)?      true : false;
var op7   =  (window.opera && /Opera( |\/)7/i.exec(navigator.userAgent))? true : false;
var ie    =  document.all?																								true : false;
var ie4   =  (document.all            && !document.documentElement)?      true : false;
var ie5up =  (document.all            && document.documentElement)?       true : false;
var dom   =  document.getElementById?																			true : false;
var DHTML = (document.getElementById || document.all || document.layers);

var popwindows = new Array();


function getObj(objid) 
{ 
	if (document.getElementById) 
		{ return document.getElementById(objid); } 
	else if (document.all) 
		{ return document.all[objid];	} 
	else if (document.layers) 
		{ return document.layers[objid]; } 
} 

function showWaitMessage(div,flag)  
{ 
	if (!DHTML) return; 
	var x = getObj(div); 
	x.style.visibility = (flag) ? 'visible':'hidden';
	if(! document.getElementById) if(document.layers) x.style.left=280/2; return true; 
} 


function ShowLoginInfo(objid, newobjid)
{
	if (!DHTML) return; 
	var x = getObj(objid);
	x.style.display = "none";
	if(!newobjid)
		newobjid = "divLoginInfo";
	var y = getObj(newobjid);
	y.style.display = "block";
	y.innerHTML = "Bitte warten...";
}

//Defaultbuttons bei mehreren Submit-Button's definieren
function UseButton4Event(e,buttonid)
{ 
	if (!DHTML) return; 

	if (!e) var e = window.event;
	if (ie && !check4InvalidKeyCode(e,true)) return;
	if (!buttonid || !getObj(buttonid)) return;
	btn = getObj(buttonid);
	var code;

	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	//window.status='yooh...' + buttonid + e + code;		
  if (code == 13 && !IsTextArea(e))
	{	
		if(e.returnValue)
		{
			e.returnValue = false;
			e.cancelBubble = true;
		}
		if(e.preventDefault)
		{
			e.cancel = true;
			e.stopPropagation();
			e.preventDefault();
		}	
		window.status='click...' + btn;					
		//ie
		if(btn.click)
		{
			window.status='ie...' + btn;					
			btn.click();
			return false;
		}
		//moz
		if(document.createEvent && btn.dispatchEvent)
		{
			window.status='moz...' + btn;					
			var evt=document.createEvent("MouseEvents");
			if(evt && evt.initMouseEvent)evt.initMouseEvent("click",true,true,document.defaultView,1,0,0,0,0,false,false,false,false,0,null);
			btn.dispatchEvent(evt);
		}
	}
}

function IsEnter(e)
{
	if (!DHTML) return;
	if (!e) var e = window.event;
	
	if (e.keyCode) 
		code = e.keyCode;
	else 
		if (e.which) 
			code = e.which;
  return (code == 13 && !IsTextArea(e));
}

//prüfen, ob Enter in Textares ausgelöst wurde
function IsTextArea(e)
{
	var targ;
	if (!e) var e = window.event;
	
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	var SourceTag = targ.tagName;
	if (!SourceTag || (SourceTag.toLowerCase() != 'textarea'))
		return false;
	else
		return true;
}

//ungültige Tasten filtern
function check4InvalidKeyCode(e,UseButton)
{
	//return true; //abgeschaltet

	var charCode = ie ? e.keyCode : e.which;
	if (ie && e.shiftKey) charCode += 1000;
	if (ie && e.altKey) charCode += 2000;
	if (ie && e.ctrlKey) charCode += 3000;
	charCode = formatNumber(charCode, '00000');
	charCode = 'c' + charCode;
	//var CurrentChar =  String.fromCharCode(charCode).toUpperCase();
	//Liste der nicht erlaubten Zeichencodes -> <,>,&,#
	var InvalidChar = 'c00226,c01226,c01054,c00191';
	//wenn kein Button definiert ist, dann auch Enter blocken
	if (!UseButton && !IsTextArea(e)) 
		InvalidChar += ',c00013,c01013';
	var index = InvalidChar.indexOf(charCode);
	//window.status = charCode + '|' + CurrentChar;
	if (index >= 0)
	{
		window.status = 'invalid Key...';
		e.returnValue=false;
		e.cancel = true;
		return false;
	}
	else
	{
		//window.status = '';
		return true;
	}
}

//Relocate Documentposition
function loadPosition()
{
	return;
	if(navigator.cookieEnabled && document.cookie)
	{
		var sCookie = GetCookie("Pos");
		if (!sCookie) return;
		
		var xPos = sCookie.split("|")[0];
		var yPos = sCookie.split("|")[1];
		
		if (ie) //IE
		{
			document.body.scrollLeft = xPos;
			document.body.scrollTop = yPos;
		} 
		else if (nc) //NS
		{
			window.pageXOffset = xPos;
			window.pageYOffset = yPos;
		}
	}
}

function savePosition()
{
	return;
	if(navigator.cookieEnabled)
	{
		var xPos;
		var yPos;
		if (ie) //IE
		{
			xPos = document.body.scrollLeft;
			yPos = document.body.scrollTop;
		} 
		else //NS
		{
			xPos = window.pageXOffset;
			yPos = window.pageYOffset;
		}
		SetCookie("Pos",xPos + "|" + yPos);
	}
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}


function callpopup(module,titledesc,query,width,height,NotModal) {	
	
	var windowWidth  = width ? width : 400;
	var windowHeight = height ? height : 200;
	
	var windowLeft   = screen.width/2 - windowWidth/2;
	var windowTop    = screen.height/2 - windowHeight/2;	
	
	var wintitle = 'GetSync-[' + (titledesc ? titledesc : 'Popup') + ']';
	
	var link;
	if (!query)
		link = 'popup.aspx?load=' + module + '&title=' + wintitle;
	else
		link = 'popup.aspx?load=' + module + '&' + query + '&title=' + wintitle;
		
	if(NotModal)
	{
		return window.open(link,'popup','width=' + windowWidth + ',height=' + windowHeight +',left=' + windowLeft + ',top=' + windowTop +',location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
	}
	else
	{
		//Fenster suchen
		if(!PopWindowCheck(wintitle)) 
			PopWindowAddNew(wintitle,window.open(link,'popup','width=' + windowWidth + ',height=' + windowHeight +',left=' + windowLeft + ',top=' + windowTop +',location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no'));
		PopWindowSetFocus(wintitle);
	}
}

function callhelp(topic, lang) {	
	
	var windowWidth  = 300;
	var windowHeight = 400;
	
	var windowLeft   = screen.width/2 - windowWidth/2;
	var windowTop    = screen.height/2 - windowHeight/2;	
	
	if(!lang) lang = 'de';
	
	var link = 'popup.aspx?load=help&title=GetSync-Hilfe&topic=' + topic + '&lang=' + lang;
		
	return window.open(link,'help','width=' + windowWidth + ',height=' + windowHeight +',left=' + windowLeft + ',top=' + windowTop +',location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
}

function PopWindowCheck(wintitle)
{	
	var rootwindow = GetRootWindow();
	var IsOpen = 
		rootwindow.popwindows && 
		rootwindow.popwindows[wintitle] && 
		!rootwindow.popwindows[wintitle].closed;
	
	return IsOpen;
		
}

function PopWindowSetFocus(wintitle)
{
	var rootwindow = GetRootWindow();
	if(	rootwindow.popwindows && 
		rootwindow.popwindows[wintitle] && 
		!rootwindow.popwindows[wintitle].closed)
		rootwindow.popwindows[wintitle].focus();
}

function PopWindowAddNew(wintitle, newwindow)
{
	var rootwindow = GetRootWindow();
	if(	rootwindow.popwindows)
		rootwindow.popwindows[wintitle] = newwindow;
}

function PopWindowGet(wintitle)
{
	var rootwindow = GetRootWindow();
	if(	rootwindow.popwindows && 
		rootwindow.popwindows[wintitle])
		return rootwindow.popwindows[wintitle];
}

function GetRootWindow()
{
	var RootWindow = window;
	while(RootWindow.opener)
		RootWindow = window.opener;
	return RootWindow;
}

function resizePopup(mainobjectid)
	{
		try
		{
			var MainObject = document.getElementById(mainobjectid);
			if (MainObject)
			{
				var width = (MainObject.offsetWidth > 800) ? 800 : (MainObject.offsetWidth > (screen.width-50)) ? screen.width-50 : MainObject.offsetWidth + 50;
				var height = (MainObject.offsetHeight > 700) ? 700 : MainObject.offsetHeight + 70;
				if (MainObject.offsetHeight == 1)
					height = screen.height - screen.height/5; 
				if (MainObject.offsetWidth == 1)
					width = screen.width  - screen.width/10; 
				window.resizeTo(width,height);
			}	
		}
		catch(e)
		{}
	}

var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
  if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

  if (number - 0 != number) return null;  // if number is NaN return null
  var useSeparator = format.indexOf(separator) != -1;  // use separators in number
  var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
  var useCurrency = format.indexOf(currency) != -1;  // use currency format
  var isNegative = (number < 0);
  number = Math.abs (number);
  if (usePercent) number *= 100;
  format = strip(format, separator + percent + currency);  // remove key characters
  number = "" + number;  // convert number input to string

    // split input value into LHS and RHS using decpoint as divider
  var dec = number.indexOf(decpoint) != -1;
  var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
  var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

    // split format string into LHS and RHS using decpoint as divider
  dec = format.indexOf(decpoint) != -1;
  var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
  var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

    // adjust decimal places by cropping or adding zeros to LHS of number
  if (srightEnd.length < nrightEnd.length) {
    var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
    nrightEnd = nrightEnd.substring(0, srightEnd.length);
    if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

// patch provided by Patti Marcoux 1999/08/06
    while (srightEnd.length > nrightEnd.length) {
      nrightEnd = "0" + nrightEnd;
    }

    if (srightEnd.length < nrightEnd.length) {
      nrightEnd = nrightEnd.substring(1);
      nleftEnd = (nleftEnd - 0) + 1;
    }
  } else {
    for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
      if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
      else break;
    }
  }

    // adjust leading zeros
  sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
  while (sleftEnd.length > nleftEnd.length) {
    nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
  }

  if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
  var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
  output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
  if (isNegative) {
    output = (useCurrency) ? "(" + output + ")" : "-" + output;
  }
  return output;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++)
    if (chars.indexOf(input.charAt(i)) == -1)
      output += input.charAt(i);
  return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
  input = "" + input;
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++) {
    if (i != 0 && (input.length - i) % 3 == 0) output += separator;
    output += input.charAt(i);
  }
  return output;
}

