//------------ BEGIN ROBOHOLP -----------------------------------

var gbNav6=false;
var gbNav61=false;
var gbNav4=false;
var gbIE4=false;
var gbIE=false;
var gbIE5=false;
var gbIE55=false;

var gAgent=navigator.userAgent.toLowerCase();
var gbMac=(gAgent.indexOf("mac")!=-1);
var gbSunOS=(gAgent.indexOf("sunos")!=-1);
var gbOpera=(gAgent.indexOf("opera")!=-1);

var HH_DISPLAY_TOPIC = 0;
var HH_DISPLAY_TOC = 1;
var HH_DISPLAY_INDEX = 2;
var HH_DISPLAY_SEARCH = 3;
var HH_HELP_CONTEXT = 15;

var gVersion=navigator.appVersion.toLowerCase();

var gnVerMajor=parseInt(gVersion);
var gnVerMinor=parseFloat(gVersion);

gbIE=(navigator.appName.indexOf("Microsoft")!=-1);

var blockKeyboardHiding = false;

if(gnVerMajor>=4)
{
	if(navigator.appName=="Netscape")
	{
		gbNav4=true;
		if(gnVerMajor>=5)
			gbNav6=true;
	}
	gbIE4=(navigator.appName.indexOf("Microsoft")!=-1);
}

if(gbNav6)
{
	document.gnPageWidth=innerWidth;
	document.gnPageHeight=innerHeight;
	var nPos=gAgent.indexOf("netscape");
	if(nPos!=-1)
	{
		var nVersion=parseFloat(gAgent.substring(nPos+10));
		if(nVersion>=6.1)
			gbNav61=true;
	}
}
else if(gbIE4)
{
	var nPos=gAgent.indexOf("msie");
	if(nPos!=-1)
	{
		var nVersion=parseFloat(gAgent.substring(nPos+5));
		if(nVersion>=5)
			gbIE5=true;
		if(nVersion>=5.5)
			gbIE55=true;
	}
}

function RH_ShowHelp(hParent, a_pszHelpFile, uCommand, dwData){
	var strHelpPath = a_pszHelpFile;
	var strWnd = "";
	var nPos = a_pszHelpFile.indexOf(">");
	if (nPos != -1){
		strHelpPath = a_pszHelpFile.substring(0, nPos);
		strWnd = a_pszHelpFile.substring(nPos+1);
	}
	RH_ShowWebHelp(hParent, strHelpPath, strWnd, uCommand, dwData);
}

function RH_ShowWebHelp(hParent, strHelpPath, strWnd, uCommand, dwData){
	ShowWebHelp(strHelpPath, strWnd, uCommand, dwData);
}

function ShowWebHelp(strHelpPath, strWnd, uCommand, nMapId){
	strHelpPath  =  strHelpPath + "?nMapId="+nMapId;
	var a_pszHelpFile = "";
	if (uCommand == HH_DISPLAY_TOPIC){a_pszHelpFile = strHelpPath + "#<id=0";}
	if (uCommand == HH_HELP_CONTEXT){a_pszHelpFile = strHelpPath + "#<id=" + nMapId;}
	else if (uCommand == HH_DISPLAY_INDEX){a_pszHelpFile = strHelpPath + "#<cmd=idx";}
	else if (uCommand == HH_DISPLAY_SEARCH){a_pszHelpFile = strHelpPath + "#<cmd=fts";}
	else if (uCommand == HH_DISPLAY_TOC){a_pszHelpFile = strHelpPath + "#<cmd=toc";}
	if (strWnd) a_pszHelpFile += ">>wnd=" + strWnd;

	if (a_pszHelpFile){
		// open help window
		var rightMargin = screen.availWidth * 0.02;
		var widthInt = (screen.availWidth * 0.65) - rightMargin;
		var heightInt = screen.availHeight * 0.65;
		var leftInt = screen.availWidth-widthInt-rightMargin;
		var topInt = 50;
		var sParam = "location=no,toolbar=yes,menubar=no,status=no,scrollbars=yes,resizable=yes,left=" + leftInt + ",top=" + topInt + ",width=" + widthInt + ",height=" + heightInt;
		var helpWinRef = window.open(a_pszHelpFile, strWnd, sParam);
		if(helpWinRef.focus) {
			helpWinRef.focus();
		}
	}
}

//------------ END ROBOHOLP -----------------------------------



// user agent
var agt=navigator.userAgent.toLowerCase();
var is_msie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

var act_form_object = "";

// get rid of frames
if (self != top) { 
	top.location=self.location; 
} 

function responseFocus(event) {
	act_form_object = event;
}

function whichElement(e) {
	var element;
	if (!e) {
		var e=window.event;
	}
	if (e.target) {
		element=e.target;
	}
	else if (e.srcElement) {
		element=e.srcElement;
	}
	if (element.nodeType==3) { // defeat Safari bug
		element = element.parentNode;
	}
	return element;
}

function responseKeyup(event) {
	if (document.LoginForm.response1 && document.LoginForm.response2 && document.LoginForm.response3 && document.LoginForm.response4) {
		var el = whichElement(event);				
		if (is_msie && window.event) {
			if (window.event.keyCode > 32) { 
				goto_next_field(el);
			}					  
		} 
		else {
			if (event.which > 32) {
				goto_next_field(el);
			}
		}
	}
	return true;
}
			
function goto_next_field(element)
{
	if (element.name == 'response1' && element.value.length == 2) {
		element.value = element.value.toUpperCase();
		document.LoginForm.response2.focus();
	} 
	else if (element.name == 'response2' && element.value.length == 2) {
		element.value = element.value.toUpperCase();
		document.LoginForm.response3.focus();
	} 
	else if (element.name == 'response3' && element.value.length == 2) {
		element.value = element.value.toUpperCase();
		document.LoginForm.response4.focus();
	} 
	else if (element.name == 'response4' && element.value.length == 2) {
		element.value = element.value.toUpperCase();
	}
	return true;
}

if (!is_msie && window.captureEvents) {
	window.captureEvents(Event.KEYPRESS);
}

function hookEvents()
{	
	if (document.LoginForm.response1
		&& document.LoginForm.response2
		&& document.LoginForm.response3
		&& document.LoginForm.response4)
	{
		document.LoginForm.response1.onkeyup=responseKeyup;
		document.LoginForm.response2.onkeyup=responseKeyup;
		document.LoginForm.response3.onkeyup=responseKeyup;
		document.LoginForm.response4.onkeyup=responseKeyup;		
	}
}

function focusOnFirstLoginFormField()
{
	for (ii = 0; ii < document.LoginForm.elements.length; ii++) {
		if (document.LoginForm.elements[ii].type && 
			(document.LoginForm.elements[ii].type=='text' || 
			document.LoginForm.elements[ii].type=='password')
			&& (document.LoginForm.elements[ii].value=='')) {
			document.LoginForm.elements[ii].focus();
			break;
		}
	}
}

function onbodyload(dialogName)
{

   	if (dialogName == 'ZticAuthSignDialog' || dialogName == 'ZticSaveAppidAndContractNr' || dialogName == 'ZticNoCard') {
    		document.LoginForm.submit();
    	}

	if (dialogName == 'ZticFWUpdate') {
		disableLanguageChangeLinks();
		document.location.href = getElementByIdFailsafe('ZticFWUpdate_firmware_link').href;
	}

	hookEvents();

	//register onclick event handler to catch klick events to close the keyboard
	document.onclick = hideKeyboard;
}

function formatCurrentDate()
{
	var now = new Date();
	var tzoffset = now.getTimezoneOffset() / 60;
	var day = now.getDate();
	var month = now.getMonth() + 1;
	var year = now.getFullYear();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	
	var _day = day;
	if (day < 10) {
		_day = "0" + day;
	}
	var _month = month;
	if (month < 10) {
		_month = "0" + month;
	}
	var _hours = hours;
	if (hours < 10) {
		_hours = "0" + hours;
	}
	var _minutes = minutes;
	if (minutes < 10) {
		_minutes = "0" + minutes;
	}
	var _seconds = seconds;
	if (seconds < 10) {
		_seconds = "0" + seconds;
	}
	
	var s = "" + _day + "." + _month + "." + year + " " + _hours + ":" + _minutes + ":" + _seconds + " MEZ";
	return s;
}

function isiwebJavaScriptCheck()
{
	buttons = document.getElementsByName('submit'); 
	if (buttons != null && buttons.length > 0) {
		buttons[0].disabled=true;
	}
}

function getElementByIdFailsafe( name) {
	var elem;
        if( document.getElementById ) {
                elem = document.getElementById( name );
        }
        else if( document.all ) {
                elem = document.all[name];
        }
        else if( document.layers ) {
                elem = document.layers[name];
        }
	return elem;	
}

function toggleVisibility( name )
{
        var elem, style;
        elem = getElementByIdFailsafe(name);
	if (elem != null) {
	        style = elem.style;
        	if(style.visibility == 'hidden' ) {
        	        style.visibility = 'visible';
        	}
        	else {
        	        style.visibility = 'hidden';
        	}
	}
}

function checkEmptyField(name)
{
	var elem;
	elem = getElementByIdFailsafe(name);
	if (elem != null) {
		if (elem.value == '') {
			alert('Vertragsnummer is empty');
		}
	}
}

function hideElement(name)
{
	var elem, style;
	elem = getElementByIdFailsafe(name);
	if (elem != null) {
		style = elem.style;
		if(style.visibility == 'visible' ) {
			style.visibility = 'hidden';
			style.position = 'absolute';
		}
	}
}

function copyValueFromTo( from, to )
{
        var fromElem, toElem;
        fromElem = getElementByIdFailsafe(from);
	toElem = getElementByIdFailsafe(to);
	toElem.innerHTML = fromElem.value;
}

function showKeyboadAtPosition( top, left, fieldName) {
	var keyboard;
	var keyBoardName = "KeyBoard";
	
	var whereToFillName = "keyboardWhereToFill";
	var whereToFillElem = getElementByIdFailsafe(whereToFillName);
	
	if (whereToFillElem.value == fieldName) {
		toggleVisibility(keyBoardName);
	}
	else {
		var keyboard = getElementByIdFailsafe(keyBoardName);
		var visibility = keyboard.style.visibility;
                whereToFillElem.value = fieldName;

                //reset position
                keyboard.style.top = top+"px";
                keyboard.style.left = left+"px";

		if (visibility == 'hidden') {
			//only toggle when not already visible. The user has to close one keyboard before open up on for another input field.
			toggleVisibility(keyBoardName);
		}
	}
	//the first hiding attempt will be the one from the documents onClick handler (the elements eventhandler is invoked before the documents one)
	//block one closing attempt to not have the keyboard closed right again in the same click
	blockKeyboardHiding = true;
}

function keyboardAppend(number) {
	var whereToFillName = "keyboardWhereToFill";
	var whereToFillElem = getElementByIdFailsafe(whereToFillName);
	var whereToFill = whereToFillElem.value;
	var field = getElementByIdFailsafe(whereToFill);
	var maxlength = field.getAttribute("maxlength");
	var oldValue = field.value;
	if(oldValue.length < maxlength) {
		var newValue = oldValue+number;
		field.value = newValue;
	}
	//the first hiding attempt will be the one from the documents onClick handler (the elements eventhandler is invoked before the documents one)
	//block one closing attempt to not have the keyboard closed in the same click
	blockKeyboardHiding = true;
}

function keyboardDeleteOne() {
	var whereToFillName = "keyboardWhereToFill";
	var whereToFillElem = getElementByIdFailsafe(whereToFillName);
	var whereToFill = whereToFillElem.value;
	var field = getElementByIdFailsafe(whereToFill);
	oldValue = field.value;
	if(oldValue.length > 0) {
		var newValue = oldValue.substring(0, oldValue.length - 1);
		field.value = newValue;	
	}
	//the first hiding attempt will be the one from the documents onClick handler (the elements eventhandler is invoked before the documents one)
	//block one closing attempt to not have the keyboard closed in the same click
	blockKeyboardHiding = true;
}

function hideKeyboard() {
	if (!blockKeyboardHiding) {
		var keyBoardName = "KeyBoard";
		var keyboard = getElementByIdFailsafe(keyBoardName);
		if (keyboard != null && keyboard.style.visibility == 'visible') {
			keyboard.style.visibility = 'hidden';
		}
	}
	blockKeyboardHiding = false;
}

function blockKeyboardHidingOnce() {
	blockKeyboardHiding = true;
}

function acceptDisclaimer() {

	var newField = document.createElement("input");
	newField.setAttribute("type", "hidden");
	newField.setAttribute("name","hasdisclaimeraccepted");
	newField.setAttribute("value", "1");
	document.LoginForm.appendChild(newField);

	document.LoginForm.submit();
}

function disableLanguageChangeLinks() {
	disableElem('languageChangeEn');
	disableElem('languageChangeDe');
	disableElem('languageChangeFr');
	disableElem('languageChangeIt');
}

function disableElem(name) {
	var elem = getElementByIdFailsafe(name);	
	if(elem != null) {
		elem.onclick = Function('return false;');
		elem.style.color = 'lightgrey';
	}
}

function isValueEmpty(name) {
	var elem = getElementByIdFailsafe(name);
	var value = elem.value;
	if (value == "") {
		return true;
	}
	else {
		return false;
	}
}













