/*****************************************************************
 21.12.2004 - VD - Support for events for both IE and Netscape
 
 Instead of the IE global "event" use the function getEvent().
 Instead of the IE specific event.srcElement use the
 function getEventSrcElement().
 Use isIExplorer() to check if the client browser is IE.

*****************************************************************/

var g_event;	// global event object, do not use directly

function getEvent()	// use this function to get the "event" object
{
	if (typeof(event) != "undefined")
		return event;
	return g_event;
}

function getEventSrcElement()	// use this function to get the event source element
{
	if (typeof(event) != "undefined")
		return event.srcElement;
	return g_event.target;
}

function getEventKeyCode()		// returns the key pressed
{
	if (typeof(event) != "undefined")
		return event.keyCode;
	return g_event.which;
}

function setEventKeyCode(keyCode)
{
	if (typeof(event) != "undefined")
		event.keyCode = keyCode;
	else
		g_event.which = keyCode;
}

function isIExplorer()	// use to check if the client browser is IE
{
	return (typeof(event) != "undefined");
}

function p_setEvent(evt)	// do not use directly
{
	if (evt)
		g_event = evt;
}

// register all necessary events (for Netscape)
// this is used to register the event (initialize g_event with it)
// prior to the call to any user defined event handler
// so the user functions can use the getEvent() and
// getEventSrcElement() functions
if (document.addEventListener)
{
	// last parameter meaning:
	// when true: first this event is called, then any user defined event
	// when false: first user events are called, then this event
	document.addEventListener("abort", p_setEvent, true);
	document.addEventListener("blur", p_setEvent, true);
	document.addEventListener("focus", p_setEvent, true);
	document.addEventListener("change", p_setEvent, true);
	document.addEventListener("click", p_setEvent, true);
	document.addEventListener("dblClick", p_setEvent, true);
	document.addEventListener("error", p_setEvent, true);
	document.addEventListener("keyDown", p_setEvent, true);
	document.addEventListener("keyPress", p_setEvent, true);
	document.addEventListener("keyUp", p_setEvent, true);
	//document.addEventListener("load", p_setEvent, true);
	//document.addEventListener("unload", p_setEvent, true);
	document.addEventListener("mouseDown", p_setEvent, true);
	document.addEventListener("mouseUp", p_setEvent, true);
	document.addEventListener("mouseOver", p_setEvent, true);
	document.addEventListener("mouseOut", p_setEvent, true);
	//document.addEventListener("reset", p_setEvent, true);
	//document.addEventListener("submit", p_setEvent, true);
}

// End of Support for events for both IE and Netscape
/****************************************************************/

//Bild austauschen
function swapImage(imgname, newsrc) {
	newimg = new Image();
	newimg.src = newsrc;
 	window.document.images[imgname].src = newimg.src;
}

//minical öffnen
function callCalendar(control){ 
	var Calendar = window.open("Calendar.aspx?TargetField=" + control + "&TID=" + getTimeID(), "Calendar", "width=150, height=155");
    Calendar.focus();
}

//07.09.2004_Boyan - use it to create unique url for every request
function getTimeID()
{
	var now = new Date;
	return now.valueOf();
}

//popup selector öffnen
function callPopupSelector(valueControl, textControl, valInit, txtInit, myWidth, myHeight, myBars, type, hideCreate){ 
	window.open("Selector.aspx?ValueField=" + valueControl + "&TextField=" + textControl + "&TextInit=" + txtInit  + "&ValueInit=" + valInit + "&type=" + type + "&hideCreate=" + hideCreate + "&TID=" + getTimeID(), "", "width=" + myWidth + ", height=" + myHeight + ", scrollbars=" + myBars);
}

//group selector öffnen
function callGroupSelector(valueControl, textControl, valInit, txtInit, myWidth, myHeight, myBars, type){ 
	window.open("SelectorGroup.aspx?ValueField=" + valueControl + "&TextField=" + textControl + "&TextInit=" + txtInit  + "&ValueInit=" + valInit + "&type=" + type + "&TID=" + getTimeID(), "", "width=" + myWidth + ", height=" + myHeight + ", scrollbars=" + myBars);
}

function callPopupSelectorEstate(valueControl, textControl, valInit, txtInit, includeDevOps, searchActiveCourtageOnly, searchOwnVsOnly, showResearchLists, searchType, groupID, myWidth, myHeight, myBars)
{
	var Selector = window.open("SelectorEstate.aspx?ValueField=" + valueControl + "&TextField=" + textControl + "&TextInit=" + txtInit  + "&ValueInit=" + valInit + "&IncludeDevOps=" + includeDevOps + "&SearchActiveCourtageOnly=" + searchActiveCourtageOnly + "&SearchOwnVsOnly=" + searchOwnVsOnly + "&ShowResearchLists=" + showResearchLists + "&SearchType=" + searchType + "&GroupID=" + groupID + "&TID=" + getTimeID(), "Selector", "width=" + myWidth + ", height=" + myHeight + ", scrollbars=" + myBars);
    Selector.focus();
}
//multiple popup selector öffnen
function callMultiplePopupSelector(myWidth, myHeight, myBars, type){ 
	var MultipleSelector = window.open("SelectorMultiple.aspx?type=" + type + "&TID=" + getTimeID(), "Selector", "width=" + myWidth + ", height=" + myHeight + ", scrollbars=" + myBars);
    MultipleSelector.focus();
}

//treeview selector offnen
function callTreeViewSelector(valueControl, textControl, myWidth, myHeight, myBars, type){
	var valueCtl = document.getElementById(valueControl);
	var textCtl = document.getElementById(textControl);
	var Selector = window.open("Structure.aspx?ValueField=" + valueControl + "&TextField=" + textControl + "&TextInit=" + textCtl.value  + "&ValueInit=" + textCtl.value + "&type=" + type + "&TID=" + getTimeID(), "TreeViewSelector", "width=" + myWidth + ", height=" + myHeight + ", scrollbars=" + myBars);
    Selector.focus();
}

function choosePopupSelector(strValueControl, strTextControl, strValue, strText ) {

	wnd = window.opener;
	if(wnd)
	{
		wnd.document.forms[0].elements[strTextControl].value = strText;
		wnd.document.forms[0].elements[strValueControl].value = strValue;
		
		//GS: display the delete icon
		if(wnd.document.forms[0].elements['DEL_' + strTextControl])
			wnd.document.forms[0].elements['DEL_' + strTextControl].style.visibility = 'visible';
		
		if(wnd.document.forms[0].elements[strValueControl].onchange)
			wnd.document.forms[0].elements[strValueControl].onchange();
	}
	self.close();
}

//clear popup selector
function clearPopupSelector (strTextControl, strValueControl) {
	document.forms[0].elements[strTextControl].value = "";
	document.forms[0].elements[strValueControl].value = "0";
	document.forms[0].elements['DEL_' + strTextControl].style.visibility = 'hidden';
}
/* 26.11.2004 Emil - not used
//make a double value a currency
function makeCurrencyFromDouble(betrag)
{
	//make it a string
	var s = "" + betrag;
	
	//find the dot and make it a comma
	var komma = s.lastIndexOf(".");

	if(komma != -1)	
		s = s.substring(0,komma) + "," + s.substring(komma+1,s.length);
	
	//now we have a string like input value and use the standard makeCurrency
	return(makeCurrency(s));	
}

//make a input value a currency
// 25.05.2004 Emil - changed to give zeroOnZero AND zeroOnEmpty parameters
function makeCurrency(betrag)
{
	return makeNumeric(betrag, "Decimal", 2, 2, 1, 0, 1);
}
*/

/* 26.11.2004 Emil - not necessary - makeNumeric handles it
//make a double value a currency
// 25.05.2004 Emil - changed to use zeroOnZero AND zeroOnEmpty parameters
function makeNumericFromDouble(betrag, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators)
{
	//make it a string
	var s = "" + betrag;
	
	//find the dot and make it a comma
	var komma = s.lastIndexOf(".");

	if(komma != -1)	
		s = s.substring(0,komma) + "," + s.substring(komma+1,s.length);
	
	//now we have a string like input value and use the standard makeCurrency
	return(makeNumeric(s, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators));	
}
*/

function makeNumeric(betrag, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators, decimalSeparator, groupSeparator)
{
	return internalMakeNumeric(betrag, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators, decimalSeparator, groupSeparator, 0);
}

//make a input value a numeric value
// 25.05.2004 Emil - changed to use zeroOnZero AND zeroOnEmpty parameters
// 13.01.2005 Emil - added param isformatted - if true local separators are already processed - bug[2609]
function internalMakeNumeric(betrag, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators, decimalSeparator, groupSeparator, isformatted)
{
	//make it a string
	var s = "" + betrag;
	
	// check for zeroOnEmpty
	if (s == "")
	{
		if(zeroOnEmpty == 1)
			return "0";
		else
			return "";
	}
	
	// 22.12.2004 Emil - Only change string if it is not already well formatted - bug[2609]
	// 13.01.2005 Emil - now this depends on isformatted param - bug[2609]
	if(isformatted == 0)
	{
		// remove groupSeparator-s and put . for decimalSeparator
		for(i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if(c == groupSeparator || (groupSeparator == "\xA0" && c == " "))
				s = s.substring(0,i) + s.substring(i+1,s.length);
			else if(c == decimalSeparator)
				s = s.substring(0,i) + "." + s.substring(i+1,s.length);
			else if(c != "-" && isNaN(c))
				return "";
		}
	}
	
	//CHECK IF WE HAVE A NUMBER
	if(isNaN(s))
		return "";
		
	//do some rounding if necessary
	if(type == "Int")
	{
		s = s * 1;
		s = Math.round(s);
	}
	else if(type == "Decimal" && decpoints != -1)
	{
		var multiplikator = "" + "1";
		
		for(f = 0; f < decpoints; f++)
		{
			multiplikator += "" + "0";
		}
			
		s = s * (multiplikator * 1);
		s = Math.round(s);
		s = s / (multiplikator * 1);
	}
	
	if(s == 0 && zeroOnZero == 0)
		return "";

	// 08.03.2005 Emil - limit number to avoid losing precision and problems with saving - bug[2914]
	if(s > 9999999999999.99)
		s = 9999999999999.99;
	
	s = "" + s;
	var komma = s.lastIndexOf(".");
	
	//find out if we have at least the dec points required
	if(type == "Decimal")
	{
		if(mindecs > 0)
		{
			if(komma == -1)
			{
				s = s + decimalSeparator;
				komma = s.length - 1;
			}
			else
			{
				s = s.replace(".", decimalSeparator);
			}

			var positionsAfterTheComma = s.length - komma - 1;
			var positionsToPad = mindecs - positionsAfterTheComma;
			
			for(g = 1; g <= positionsToPad; g++)
			s = s + "0";
		}
		else
		{
			s = s.replace(".", decimalSeparator);
		}
	}
	
	//now add the groupSeparator every 3 index starting from the end
	if(thousandsSeperators == 1)
	{
		pos = 0;
		if(komma == -1)
			komma = s.length;
			
		for(b = komma-1; b > 0; b--)
		{
			pos++;
			if(pos == 3 && s.charAt(b) != "-")
			{
				s = s.substring(0, b) + groupSeparator + s.substring(b, s.length);
				pos = 0;
			}
		}
	}
	
	return s;
}

//stop typing more than allowed digits after a comma in numeric boxes
function limitDecPoints(betrag, decpoints, decimalSeparator)
{
	allowOneCommaOnly(betrag, decimalSeparator);
	
	if(decpoints == 0)
		decpoints = -1;
	
	var komma;
	
	//make it a string
	s = "" + betrag.value;
	
	//find a comma	
	komma = s.lastIndexOf(decimalSeparator);
	
	//find out how long the field input may be
	if(komma >= 0)
	{
		betrag.value = s.substring(0, komma + 1 + decpoints);
	}
}

//do only allow one comma for numeric boxes
function allowOneCommaOnly(betrag, decimalSeparator)
{
	var kommaLast, kommaFirst;
	
	//make it a string
	s = "" + betrag.value;
	
	//find the first & last comma	
	kommaFirst = s.indexOf(decimalSeparator);
	kommaLast = s.lastIndexOf(decimalSeparator);
	
	if(kommaFirst != kommaLast)
		betrag.value = s.substring(0, kommaLast);
}

/* old version of NumericBox scripts
function makeNumeric(betrag, type, decpoints, mindecs, zeroOnZero, zeroOnEmpty, thousandsSeperators)
{

	//make it a string
	var s = "" + betrag;

	// check for zeroOnEmpty
	if (s == "" && zeroOnEmpty == 0)
		return "";

	//remove all given dots
	for(i = 0; i < s.length; i++) 
		if(s.charAt(i)==".")
			s = s.substring(0,i) + "" + s.substring(i+1,s.length);
	
	//find the last komma and make it a dot
	var komma = s.lastIndexOf(",");

	if(komma != -1)	
		s = s.substring(0,komma) + "." + s.substring(komma+1,s.length);
	
	//remove the other commas
	for(i = 0; i < s.length; i++) 
		if(s.charAt(i)==",")
			s = s.substring(0,i) + "" + s.substring(i+1,s.length);
	
	//CHECK IF WE HAVE A NUMBER
	if(isNaN(s))
		return "";
		
	//do some rounding if necessary
	if(type == "Int")
	{
		s = s * 1;
		s = Math.round(s);
	}
	
	else if(type == "Decimal" && decpoints != -1)
	{
		var multiplikator = "" + "1";
		
		for(f = 0; f < decpoints; f++)
		{
			multiplikator += "" + "0";
		}
			
		s = s * (multiplikator * 1);
		s = Math.round(s);
		s = s / (multiplikator * 1);
	}
	
	//make the dot a comma again
	s = "" + s;
	for(i = 0; i < s.length; i++) 
		if(s.charAt(i)==".")
			s = s.substring(0,i) + "," + s.substring(i+1,s.length);
		
	//find the  comma
	komma = s.lastIndexOf(",");
	
	//find out if we have at least the dec points required
	if(type == "Decimal" && mindecs > 0)
	{
		if(komma == -1)
		{
			s = s + ",";
			komma = s.lastIndexOf(",");
		}

		var positionsAfterTheComma = s.length - komma;
		var positionsToPad = mindecs - positionsAfterTheComma;
		
		for(g = 0; g <= positionsToPad; g++)
		 s = s + "0";
		 
		komma = s.lastIndexOf(",");
	}
	
	//limit dec points
	if(decpoints != -1)
	{
		if(decpoints == 0)
			s = Math.round(s * 1) + "";
		
		else
		{
			if(komma != -1)
				s = s.substring(0, komma + 1 + decpoints);
		}
	}

	//now add the dots every 3 index starting from the end
	if(thousandsSeperators == 1)
	{
		pos = 0;
		if(komma == -1)
			komma = s.length;
			
		for(b = komma-1; b > 0; b--)
		{
			pos++;
					
			if(pos == 3 && s.substring(b-1, b) != "-")
			{
				s = s.substring(0, b) + "." + s.substring(b, s.length);
				pos = 0;
			}
		}
	}
		
	if((s.replace(",",".") * 1 == 0) && zeroOnZero == 0)
		s = "";
	
	return s;
}

//stop typing more than allowed digits after a comma in numeric boxes
function limitDecPoints(betrag, decpoints)
{
	allowOneCommaOnly(betrag);
	
	if(decpoints == 0)
		decpoints = -1;
	
	var wert, komma;
	
	//make it a string
	s = "" + betrag.value;
	
	//find a comma	
	komma = s.lastIndexOf(",");
	
	//find out how long the field input may be
	if(komma == -1)
		wert = s.length;
	else
		wert = komma + decpoints;
	
	inputLength = s.length;
	
	if (inputLength > wert+1) 
	{
		betrag.value = s.substring(0, (s.length - 1)); 
	}
	
}

//do only allow one comma for numeric boxes
function allowOneCommaOnly(betrag)
{
	var kommaLast, kommaFirst;
	
	//make it a string
	s = "" + betrag.value;
	
	//find the first & last comma	
	kommaFirst = s.indexOf(",");
	kommaLast = s.lastIndexOf(",");
	
	if(kommaFirst != kommaLast)
		betrag.value = s.substring(0, (s.length - 1));
}
*/ // end of old version of NumericBox scripts


//do only allow positive numbers
function positiveOnly(betrag)
{
	//remove any given minus
	if(betrag.value < 0)
		betrag.value = betrag.value * -1;
}

//function to do some client side easy calculation
function calcIt(readfield, putfield, formula, valdat, type, decpoints, mindecs, zeroOnEmpty, thousandsSeperators, decimalSeparator, groupSeparator)
{

	//if the readfield is not empty
   	if(document.forms[0].elements[readfield].value != "") 
	{
		//make it a string
		var s = "" + document.forms[0].elements[readfield].value;
		
		/*
		//remove the dots
		for(i=0; i<s.length; i++)
			if(s.charAt(i) == ".")
				s = s.substring(0,i) + "" + s.substring(i+1, s.length);

		//find the last komma and make it a dot
		var komma = s.lastIndexOf(",");

		if(komma != -1)	
			s = s.substring(0,komma) + "." + s.substring(komma+1,s.length);

		//remove the other commas
		for(i = 0; i < s.length; i++) 
			if(s.charAt(i)==",")
				s = s.substring(0,i) + "" + s.substring(i+1,s.length);
		*/
		
		// remove groupSeparator-s and put . for decimalSeparator
		for(i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if(c == groupSeparator || (groupSeparator == "\xA0" && c == " "))
				s = s.substring(0,i) + s.substring(i+1,s.length);
			else if(c == decimalSeparator)
					s = s.substring(0,i) + "." + s.substring(i+1,s.length);
		}

		//check if we have a number
		if(isNaN(s))
			return;

		var p = 0;

		//now perform the formula
		if(formula == "*12")
			p = s * 12;
		else if(formula == "/12")
			p = s / 12;
		else if(formula == "/m2")
			if(valdat > 0)
				p = s / valdat;
			else
				p = 0;

		//output
		//25.05.2004 Emil - give 1 for zeroOnZero to makeNumericFromDouble
		//13.01.2005 Emil - give 1 for isformatted to indicate that value can be directly used as an int - bug[2609]
		document.forms[0].elements[putfield].value = internalMakeNumeric(p, type, decpoints, mindecs, 1, zeroOnEmpty, thousandsSeperators, decimalSeparator, groupSeparator, 1);
	}

}

//reset form height
function resetFormHeight()
{
	/*
	var formHeight = 470;
	
	if(document.all["info_area"])
		formHeight = formHeight - document.all["info_area"].offsetHeight - 30;
	
	if(document.all["wizard_area"])
		formHeight = formHeight - document.all["wizard_area"].offsetHeight - 20;
	
	if(document.height_spacer)
	{
		document.height_spacer.height = formHeight;
	}
	*/
}

function contactSheetTurnDown()
{
	var Selector = window.open("ContactSheetTurnDown.aspx?TID=" + getTimeID(), "Confirm", "width=640, height=400, scrollbars=no");
    	Selector.focus();
}
// 20.02.2004_Veselin Dimov
var submitCtrl;

function SubmitOnEnter()
{
	var code = getEventKeyCode();
	var CtrlEnter = ""; //added Lyubomir Milkov 06.02.2004 bug[1676]
	if(code == 13)
	{
		//added Lyubomir Milkov 06.02.2004 bug[1676]
		CtrlEnter = "document.getElementById('" + submitCtrl + "').click();";
		//alert(submitCtrl);
		//window.alert('enter pressed');
		//event.cancelBubble = true;
		//event.returnValue = false;
		//alert(eval(postBackStr));
		eval(CtrlEnter);
		//return true;
		return false;
	}
}

function SetKeyDownEvent()
{
	document.onkeydown = SubmitOnEnter;
}

//[10.05.2004] Michael Stockhausen
function ShowHelpForObjectNumber()
{
	var HelpPopup = window.open("HelpForObjectNumber.aspx?TID=" + getTimeID(),"HelpForObjectNumberWindow","height=320,width=250,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	HelpPopup.focus();
}

function OpenManual()
{
	if(document.forms[0].manual_displayed.value == "false")
	{
		window.open("manual/manual.pdf", "manual", "");
		document.forms[0].manual_displayed.value = "true";
	}
}

function OpenAdminManual()
{
	if(document.forms[0].manual_displayed.value == "false")
	{
		window.open("manual/admin.pdf", "manual", "");
		document.forms[0].manual_displayed.value = "true";
	}
}

function OpenManualSelector()
{
	if(document.forms[0].manual_displayed.value == "false")
	{
		var manualselector = window.open("Manual.aspx?TID=" + getTimeID(), "manualselector", "width=400, height=400, scrollbars=0");
		manualselector.focus();
		document.forms[0].manual_displayed.value = "true";
	}
}

function tmt_winCentre() {

    /*if (document.layers) {

        var sinist = screen.width / 2 - outerWidth / 2 - 3;

        var toppo = screen.height / 2 - outerHeight / 2;

    } else {

        var sinist = screen.width / 2 - document.body.offsetWidth / 2 - 3;

        var toppo = -35 + screen.height / 2 - document.body.offsetHeight / 2;

    }

    self.moveTo(sinist, toppo);*/

	// 16.06.2005 Emil - place popups in the top left corner - bug 2077
    self.moveTo(0, 0);

}
// checks whether there is a cookie windowOpenTID and if there is and its value is the same as TID parameter, returns TRUE otherwise - FALSE
// used in OpenDocuments method
// Lyubomir Milkov 20.12.2004 bug[2256]
// 21.07.2005 Emil - made it reusable by using a jey use a key; key shouldn't contain '=' or ';' (bug 3737)
function firstTime(key)
{
	var idx1 = document.URL.indexOf("TID=");
	if (idx1 < 0)
		return true; // shouldn't come to this
	
	var idx2 = document.URL.indexOf("&", idx1);
	if (idx2 < 0)
		idx2 = document.URL.length;

	var tid = document.URL.substring(idx1+4, idx2);
	
	var keyHeader = key + "=";
	var pos = document.cookie.indexOf(keyHeader);
	if (pos >=0)
	{
		var tidCookie = "";
		var arr = document.cookie.split(";");
		var i;
		for(i=0; i<arr.length; i++)
		{
			pos = arr[i].indexOf(keyHeader);
			if(pos == 0)
				tidCookie = arr[i].substr(keyHeader.length);
				
			if (tid != tidCookie)
			{
				arr[i] = keyHeader + tid;
				document.cookie = arr.join(";");
				return true;
			}
			else
			{
				return false;
			}
		}
		return true; // shouldn't come to this
	}
	else
	{
		document.cookie = keyHeader + tid + ";";
		return true;
	}
}
// leave in page for way to customize layout of tooltip 
// and avoid errors if onmouseovers/outs before page completely loaded
function doTooltip(e, msg) 
{
	if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
	Tooltip.show(e, msg);
}

function hideTip()
{
	if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
	Tooltip.hide();
}

function hideButton(buttonName, withValidation)
{
	var hidingAllowed = false;
	if (withValidation == true)
	{
		if (typeof(Page_ClientValidate) == 'function')
		{
			Page_ClientValidate();
		}
		if (Page_IsValid == true)
		{
			hidingAllowed = true;
		}
	}
	else
	{
		hidingAllowed = true;
	}
	if (hidingAllowed == true)
	{
		document.getElementById(buttonName).style.visibility = 'hidden';
	}
}