// ************************************************************************
// WEB-ASSIST4 Script Library
// Copyright 2000 AEB GmbH. All Rights Reserved.
// ************************************************************************
//
//
function As4ClientGetUserLanguage() {
	var lang;
	var index, firstindex;
	//Durchsuchte die accept-language des Browsers nach einer vom Framework
	//unterstützten Spachen und nimmt die, die als erstes im String steht
	
	//acceptLanguage wird in assistsetupbasic.asp definiert
	firstindex = acceptLanguage.length; //aufs ende setzen

	//deutsch
	if ((index = acceptLanguage.indexOf("de")) != -1) {
		if (index < firstindex) {
			firstindex = index;
			lang = "de";
		}
	}

	//englisch
	if ((index = acceptLanguage.indexOf("en")) != -1) {
		if (index < firstindex) {
			firstindex = index;
			lang = "en";
		}
	}
	if (lang == "") {	//im Falle eines Falles auf deutsch setzen
		lang = "de"
	}
	return lang;
}

function As4ClientCheckDate(aControl) {
	var errorMsg;
	var pattern;
	var lang = As4ClientGetUserLanguage();
	if (lang == "de") {
		pattern = /^(0?[1-9]|1[\d]|2[\d]|3[0-1])\.(0?[1-9]|1[0-2])\.(\d{0,4})?$/;
		errorMsg = 'Das eingegebene Datum ist ungültig.\n';
		errorMsg += 'Bitte geben Sie das Datum im Format "TT.MM.JJJJ" ein.';
	};
	if (lang == "en") {
		pattern = /^(0?[1-9]|1[0-2])\/(0?[1-9]|1[\d]|2[\d]|3[0-1])\/(\d{0,4})?$/;
		errorMsg = 'The date you have entered is not valid.\n';
		errorMsg += 'Please enter a date with the following format: "MM/DD/JJJJ".';
	};
	return As4ClientCheckExpression(aControl.value, pattern, errorMsg)
}

function As4ClientCheckTime(aControl) {
	var errorMsg;
	var result;
	var pattern;
	var lang = As4ClientGetUserLanguage();
	if (lang == "de") {
		// var pattern = /^((0?[0-9]|(1[\d])|(2[0-3]))(:[0-5]\d){0,2}|(24)(:00){0,2})$/;
		//pattern = /^(((0?[0-9])|(1[\d])|(2[0-3]))([:|,|\.][0-5]){0,2}|(24)([:|,|\.]00){0,2})$/;
		var pattern = /^((0?[0-9]|(1[\d])|(2[0-3]))([:|,|.][0-5](\d){0,1}){0,2}|(24)([:|,|.]00){0,2}|((:|,|.)[0-5](\d){0,1}){0,2})$/;
	
		errorMsg = 'Die eingegebene Zeit ist ungültig.\n';
		errorMsg += 'Bitte geben Sie die Zeit im Format "10:45:00" ein.';
	};
	if (lang == "en") {
		//NOCH AENDERN, RexExp ist FALSCH (fuer en)
	//	pattern = /^(((0?[0-9])|(1[0-2]))([:|,|\.][0-5]\d){0,2} (AM|PM))$/;
		var pattern = /^((0?[0-9]|(1[\d])|(2[0-3]))([:|,|.][0-5](\d){0,1}){0,2}|(24)([:|,|.]00){0,2}|((:|,|.)[0-5](\d){0,1}){0,2})$/;
		errorMsg = 'The timestamp you have entered is not valid.\n';
	//	errorMsg += 'Please enter a timestamp with the following format: "08:20:00 AM".';
		errorMsg += 'Please enter a timestamp with the following format: "10:15:00".';
	};
	result = As4ClientCheckExpression(aControl.value, pattern, errorMsg);
	errorMsg = 'Die eingegebene Zeit ist ungültig.\n';
	if (!result) {
		return result }
	else {
		aControl.value = As4ClientTimeFromString(aControl.value);
		return result }
}

function As4ClientStringFill (aString, count) {
	var tmp;

	tmp = '';
	for (i=0; i<count; i++) {
		tmp += aString }

	return tmp

}

function As4ClientCheckNumber(aControl, precision, scale, acceptNegative) {
	var errorMsg;
	var pattern;
	var format = '';

	if (typeof(acceptNegative) == 'undefined') {
		//wenn kein wert übergeben wurde, auf false setzen
		var acceptNegative = false;
	}
	if (typeof(precision) == 'undefined')
	   { 
		if (acceptNegative) {
			pattern = /^(\+|-)?\d*(,\d*){0,1}$/; 
		} else {
			pattern = /^\d*(,\d*){0,1}$/; 
		}
		}
	else
	{
	   if (typeof(scale) == 'undefined') {
			if (acceptNegative) {			
				pattern = ("^(\\+|-)?\\d{0," + precision + "}$");
			} else {
				pattern = ("^\\d{0," + precision + "}$");
			}
			format = As4ClientStringFill ('#', precision); }
	   else {
			if (acceptNegative) {
				pattern = ("^(\\+|-)?\\d{0," + (precision-scale) + "}(,\\d{0," + scale + "}){0,1}$");
			} else {
				pattern = ("^\\d{0," + (precision-scale) + "}(,\\d{0," + scale + "}){0,1}$");
			}
			format = As4ClientStringFill ('#', precision-scale) + ',' + As4ClientStringFill ('#', scale); }
	}

	errorMsg = 'Die eingegebene Zahl ist ungültig.\n';
	if (format == '') {
	   errorMsg += 'Bitte geben Sie nur Zahlen ein.'; }
	else {
	   errorMsg += ('Bitte geben Sie die Zahl im Format "' + format + '" ein.'); }
	return As4ClientCheckExpression(aControl.value, pattern, errorMsg)
}

function As4ClientCheckExpression(aString, pattern, errorMsg) {
	var expression;
	if (aString	== "")
	{
		return true;
	}

	if ((typeof(pattern) == 'object') || (typeof(pattern) == 'function'))
	{ expression = pattern; }
	else
	{ expression = new RegExp(pattern, 'i') }

	if (expression.exec(aString) == null)
	{
	   alert(errorMsg);
	   return false
	}

	return true
}

function As4ClientTimeFromString(aString) {
	var firstSeparatorIndex, secondSeparatorIndex, hh, mm, ss,ampm;

	firstSeparatorIndex = aString.indexOf(':');
	if (firstSeparatorIndex == -1) {
		firstSeparatorIndex = aString.indexOf(',');
		if (firstSeparatorIndex == -1) {
			firstSeparatorIndex = aString.indexOf('.');
		}
	}
	
	ampm = '';
	//ein AM oder PM suchen damit es später wieder angehängt wird
	if (aString.indexOf('AM') != -1 || aString.indexOf('am') != -1) {
		ampm = ' AM';
	};

	if (aString.indexOf('PM') != -1 || aString.indexOf('pm') != -1) {
		ampm = ' PM';
	};

	if (firstSeparatorIndex == -1) {		// Es wurden keine Minuten und Sekunden angegeben
		hh = aString.substring(0,2); //kann nur 2stellig sein
		mm = '00';
		ss = '00'; }
	else {
		hh = aString.substring(0, firstSeparatorIndex);
		secondSeparatorIndex = aString.indexOf(':', firstSeparatorIndex + 1);
		if (secondSeparatorIndex == -1) {
			secondSeparatorIndex = aString.indexOf(',', firstSeparatorIndex + 1);
			if (secondSeparatorIndex == -1) {
				secondSeparatorIndex = aString.indexOf('.', firstSeparatorIndex + 1);
			}
		}

		if (secondSeparatorIndex == -1) {	//Es wurden keine Sekunden angegeben
			mm = aString.substring(firstSeparatorIndex + 1, aString.length);
			ss = '00'; }
		else {
			mm = aString.substring(firstSeparatorIndex + 1, secondSeparatorIndex);
//			ss = aString.substring(secondSeparatorIndex + 1, aString.length)
			ss = aString.substring(secondSeparatorIndex + 1, secondSeparatorIndex + 3) // immer 2stellig
		}
	}
	if( hh.length == 0 ) { hh = '00' };
	if( hh.length == 1 ) { hh = '0' + hh };
	if( mm == '3' ) { mm = mm + '0' };

	return (hh + ':' + mm + ':' + ss + ampm)
}

//-----------------------------------------------------------------
//popup-menu
var currentSpanElement		= "";			//Needed to track current span element
var menuArray				= new Array();	//Tracks what divs are showing so it knows what to hide when a new span is clicked
var onTextColor				= "#FFFFFF";	//Text color with mouseover
var offTextColor			= "#000000";	//Text color with NO mouseover
var onCellColor				= "#A61616";	//Span color with mouseover
var offCellColor			= "#EEEEE6";	//Span color with nomouseover
var offsetMenuX				= 110;			//X Distance new menu will be from parent menu
var offsetMenuY				= 5;			//Y Distance increase of new menu compared to parent span
var startDistanceX			= -10;			//Sets how far off onMouseOver start element we should go
var startDistanceY			= 5;			//Sets how far off onMouseOver start element we should go
var menuOn					= false;		//Only one set of menus can be displayed at once
var started					= false;		//Used to track if we just started the menus
var clickStart				= true;			//Allow an onClick event to start the menu or not here
var clickX					= 0;			//Location X of event to start menu
var clickY					= 0;			//Location Y of event to start menu
var selectCount				= 0;
var select					= "";
var appletCount				= 0;			//If we have applets, track the # so we can temporarily hide them
var applets					= "";			//Tracks the document.all.tags("applet") collection so we can reference it once rather than twice

var currentParameter        = "";           //speichern des zusätzlichen menü-parameters

function invokeMenuItem(command)  
	{										//übergebene Funktion auf dem server aufrufen
		var mth;
		if (typeof(currentParameter) == 'string')
			mth = '__eM1'
		else
			mth = '__eM1Int';
		thisPage.invokeMethod('',mth,new Array(command,currentParameter));
	} 


function startIt(menu,thisItem,level,parameter) {						//menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
											//neuer Parameter: der menü-parameter
	currentParameter = parameter;           //ende ergänzung											
	if (menuOn == true) {
		window.event.cancelBubble = true;
		hideAllDivs();
		return;												//Only allow one menu to be activated at a time
	} else {
		select = document.all.tags("select");
		selectCount = select.length;
		if (selectCount > 0) {
			for (i=0;i<selectCount;i++) {
				select.item(i).style.visibility = "hidden";
			}
		}
		applets = document.all.tags("applet");
		appletCount = applets.length;
		if (appletCount > 0) {
			for (i=0;i<appletCount;i++) {
				applets.item(i).style.visibility = "hidden";
			}
		}
		menuOn = true;			
		started = true;										//Lets us know we're coming in for the 1st time
		clickX = event.clientX;
		clickY = event.clientY;
		if (clickStart) window.event.cancelBubble = true;		
		stateChange(menu,thisItem,level);
	}	
}

function stateChange(menu,thisItem,level) {									//menu = menu to display,thisItem=name of span item to use,level=current depth of menus
	
	if (currentSpanElement != thisItem.id && started != true) {							//Only hit this if they changed span elements	
		if (currentSpanElement == "") currentSpanElement = thisItem.id;	//Used 1st time through only	

		eItemOld = eval("document.all('" + currentSpanElement + "')");
		eItemNew = eval("document.all('" + thisItem.id + "')");
		eParent = eItemNew.parentElement;
			
		//Turn off whatever span was turned on
		eItemOld.className = 'menuitem';
		//Turn on new span
		eItemNew.className = 'menuselected';
		currentSpanElement = thisItem.id;					//Track where the last mouseover came from
	}
	
	if (menu != "") {
		eMenu = eval("document.all('" + menu + "')");			
		eItem = eval("document.all('" + thisItem.id + "')");				//Used for x,y coordinates
		hideDiv(level);
		menuArray[menuArray.length] = menu;									//Tracks open menus	


		//IE 5.5 schreibt den scrollwert in document.body.scrollLeft....
		//IE 6.0 schreibt den scrollwert in document.documentElement.scrollLeft....!!! (aha)   
		//also einer der beiden werte ist der scrollwert, der andere 0 -> addieren dann passts immer


		var positionX =  eItem.parentElement.offsetLeft + offsetMenuX + document.body.scrollLeft + document.documentElement.scrollLeft;
		var positionY =   eItem.parentElement.offsetTop + eItem.offsetTop + offsetMenuY + document.body.scrollTop + document.documentElement.scrollTop;
		if (started) {
			positionX =	clickX + startDistanceX	+ document.body.scrollLeft + document.documentElement.scrollLeft	//eItem.offsetLeft + startDistanceX;
			positionY =	clickY + startDistanceY	+ document.body.scrollTop + document.documentElement.scrollTop	//eItem.offsetTop + startDistanceY;
		}
		//If screen isn't wide enough to fit menu, bump menu back to the left some
		if ((positionX + eMenu.offsetWidth) >= document.body.clientWidth) {
			positionX -= (eMenu.offsetWidth * 1.3);
			positionY += 15;
		}
		//If the menu is too far to the left to display, bump it to the right some
		if ((positionX + eMenu.offsetWidth) <= eMenu.offsetWidth) {
			positionX += (eMenu.offsetWidth * 1.3);
		}
		//If the menu is too far down, bump the menu up to the bottom equals the body clientHeight property
		if ((positionY + eMenu.offsetHeight) >= document.body.clientHeight) {
			if (started != true) positionY = document.body.clientHeight - eMenu.offsetHeight;
		}
	
		eMenu.style.left = positionX;
		eMenu.style.top = positionY;
		//eMenu.style.zIndex = level;									//Only use this if we don't reverse the arrays in the ASP/XML Script
		eMenu.style.visibility='visible';
	}
	
	started = false;												//After 1st menu, turn of started variable
}

function hideDiv(currentLevel) {
		for (var i=currentLevel;i<menuArray.length;i++) {
			var arrayString = new String(menuArray[i]);
			if (arrayString == "undefined") continue;
			eval("document.all('" + menuArray[i] + "').style.visibility='hidden'");
		}
			menuArray.length = currentLevel;
}

function hideAllDivs() {
	if (menuOn == true) {		//Don't loop through document elements if they clicked a hyperlink since it wastes time
		for (var i=0;i<menuArray.length;i++) {
			var arrayString = new String(menuArray[i]);
			if (arrayString == "undefined") continue;
			document.all(menuArray[i]).style.visibility = "hidden";
			document.all(menuArray[i]).style.left = 0;
			document.all(menuArray[i]).style.top = 0;

		}
		if (currentSpanElement != "") {					//No currentSpanElement if they haven't mousedOver any span element
			eItem = eval("document.all('" + currentSpanElement + "')");
			eItem.className = 'menuitem';//Ensure current span's color is reverted to "not selected"
			menuArray = new Array();	
			currentSpanElement = "";		
		}
		if (selectCount > 0) {
			for (i=0;i<selectCount;i++) {
				select.item(i).style.visibility = "visible";
			}
			selectCount = 0;
			select = "";
		}
		if (appletCount > 0) {
			for (i=0;i<appletCount;i++) {
				applets.item(i).style.visibility = "visible";
			}
			appletCount = 0;
			applets = "";
		}
	}
	menuOn = false;								//Menus off, so set this to false


}

document.onclick = hideAllDivs;	
//end popupmenu
//----------------------------------------------

