/***************************************************************/
/*               AVIGO-DARWIN FRAME CONTROL LIBRARY            */
/*               Version: 0.1                                  */
/***************************************************************/

/* Global definitions */

var AVD_IbeFrameId = 'darwinFrame';
var AVD_IbeTransferParmName = 'ibetransfer';
var AVD_IbeUpdateParmName = 'ibeupdate';
var AVD_DarwinPageUrl = '/app/content/pagedarwin';
var AVD_MinDarwinFrameHeight = 600;
var AVD_WaitingIconId = 'darwinWaitingIcon';
var AVD_WaitingTimerId = 0;
var AVD_ProcessingTransfer = false;
var AVD_LinkIdParmName = 'linkId';
var AVD_TransferServiceParm = 'service=transfer';
var AVD_ResourceIdParmName = 'resource';
var AVD_ResourceJsId = 'darwinPageResourceId';
var AVD_IbeSessionInfoId = 'ibeSessionInfo';
//MK: #7768
var AVD_BusyTimerId = 0;

/* Changes made for php integration */

var AVD_CurrentFrameId = '';
var AVD_PhpFrameId = 'phpFrame';
var AVD_PhpPageUrl = '/app/content/pagephp';
var AVD_IFrameId = 'iFrame';

//Timer event for Darwin IFrame repositioning.
//MUST be made with timer for IE6 because all settings made in AVD_SetIframeHeight
//are executed once after the call of AVD_OnIbeFrameReload is completed.
var AVD_RepositionIFrameEvent;
// List of browsers and versions that need a delay for certain actions
var browserMatchDelay = [
//	{browser: "Firefox"},
	{browser: "Explorer", versions: ["6", "7"]}
];



/* Public functions */

/*
  Function:	AVD_SwitchToDarwinPage
  Description:	Switches to Avigo IBE page with frame with session in correct state
  Usage:	Used on Avigo pages
*/
document.AVD_SwitchToDarwinPage = function( transferParameters, resourceName ) {
  if( !document.body.onclick )
    document.body.onclick = document.AVD_BlockRequests;
  if( !document.body.onkeydown )
    document.body.onkeydown = document.AVD_BlockRequests;
	AVD_ProcessingTransfer = true;
  var wrappedParms = '';
  if( transferParameters && transferParameters.length ) {
		var date = new Date();
  	transferParameters = AVD_LinkIdParmName + '=' + date.getTime() + '&' + AVD_TransferServiceParm + '&' + transferParameters;
  	wrappedParms = document.AVD_EncodeIbeParameters( null, transferParameters );
  }
  AVD_WaitingTimerId = window.setTimeout( "document.AVD_ShowWaitingInfo();", 1000 );
  document.AVG_showSiteBusyMessage();
	document.AVD_SubmitFormRequest( document.AVD_getPageUrl( AVD_DarwinPageUrl ), AVD_IbeTransferParmName, wrappedParms, resourceName );
}

/*
  Function:	AVD_SwitchToPhpPage
  Description:	Switches to PHP page with frame
  Usage:	Used on Dertour pages
*/
document.AVD_SwitchToPhpPage = function( transferParameters, resourceName ) {
  if( !document.body.onclick )
    document.body.onclick = document.AVD_BlockRequests;
  if( !document.body.onkeydown )
    document.body.onkeydown = document.AVD_BlockRequests;
  var wrappedParms = '';
  if( resourceName ) {
    wrappedParms += AVD_ResourceIdParmName + '/' + resourceName;
  }
  AVD_WaitingTimerId = window.setTimeout( "document.AVD_ShowWaitingInfo();", 1000 );
  window.location.href = document.AVD_getPageUrl( AVD_PhpPageUrl ) +'/'+ wrappedParms ;
}

/*
  Function:	AVD_UpdateDarwinInfo
  Description:	Called from darwin frame when to update darwin session info in Avigo app
  Usage:	Used on Avigo IBE page
*/
document.AVD_UpdateDarwinInfo = function( darwinInfoString ) {
	if( !darwinInfoString )
	  return;
	if( !document.AVD_CheckFrameAccess() )
		return;
	darwinInfoString = unescape( darwinInfoString );
	var date = new Date();
	var updateUrl = '';
	var resourceName = null;
	var resourceNameSpan = document.getElementById( AVD_ResourceJsId );
	if( resourceNameSpan ) {
	  resourceName = resourceNameSpan.innerHTML;
	}
	updateUrl += AVD_LinkIdParmName + ':' + date.getTime() + ':' + darwinInfoString;
  //--- Showing waiting message (MK)
  AVD_WaitingTimerId = window.setTimeout( "document.AVD_ShowWaitingInfo();", 1000 );
	document.AVD_SubmitFormRequest( document.AVD_getPageUrl( AVD_DarwinPageUrl ), AVD_IbeUpdateParmName, updateUrl, resourceName );
  document.AVG_showSiteBusyMessage();
}

/*
  Function:	AVD_AjaxUpdateDarwinInfo
  Description:	Called from darwin frame to update darwin session info in Avigo app
  Usage:	Used on Avigo IBE page
*/
document.AVD_AjaxUpdateDarwinInfo = function() {

	if( !document.AVD_CheckFrameAccess() )
		return;
	//get darwin session info
  var ibeFrameWin = document.AVD_GetIbeFrameWin();
  if( !ibeFrameWin || !ibeFrameWin.document )
	  return;
  var ibeSessionInfo = ibeFrameWin.document.getElementById( AVD_IbeSessionInfoId );
  if( !ibeSessionInfo || !ibeSessionInfo.innerHTML )
	  return;
	var ajaxUpdaterForm = document.getElementById( 'darwinAjaxTransmitterPanel_form' );
	if( !ajaxUpdaterForm )
	  return;
	var ajaxUpdaterField = document.getElementById( 'darwinAjaxTransmitterPanel_form_darwinParameter' );
	if( !ajaxUpdaterField )
	  return;
	var darwinInfoString = unescape( ibeSessionInfo.innerHTML );
	var date = new Date();
	var updateUrl = AVD_LinkIdParmName + ':' + date.getTime() + ':' + darwinInfoString;
	ajaxUpdaterField.value = updateUrl;
  ajaxUpdaterForm.onsubmit();
}

/*
  Function:	AVD_ResizeIbeFrame
  Description:	Resize IBE frame with parameters retrieved from main site URL
  Usage:	Used on Avigo IBE page
*/
document.AVD_ResizeIbeFrame = function( preservePosition, minHeight ) {
	if( !document.AVD_CheckFrameAccess() ) {
		return;
	}
	
	var scrollTop = 0;
	//MK #7007
	//var useDocElement = !document.all && document.documentElement;
	var useDocElement = document.documentElement != null;
	if( useDocElement ) {
		scrollTop = document.documentElement.scrollTop;
	} else {
		scrollTop = document.body.scrollTop;
	}
	document.AVD_SetIframeHeight( minHeight );
	if( scrollTop && preservePosition ) {
		if( useDocElement ) {
			document.documentElement.scrollTop = scrollTop;
		} else {
			document.body.scrollTop = scrollTop;
		}
	} else {
		try{
			var ibeFrameWin = document.AVD_GetIbeFrameWin();
	
			// check for navigation after a DARWIN page change (eg. a navigation to previous/next page)  
			if(ibeFrameWin && ibeFrameWin.document) {
				var pos = 0;
				for(pos = 0; pos < ibeFrameWin.document.anchors.length; pos++) {
					var anchorObj = ibeFrameWin.document.anchors[pos];
					if(anchorObj.name == "mainAnchor") {
						// use the title tag for checking and indicating the navigation
						if(anchorObj.title == "") {
							anchorObj.title = "initPortalPageNavigationDone";
							window.scrollTo(0,0); // window.location.hash = "topAnchorPortal";
							return;
						}
					}
				}
			}
	
			if( ibeFrameWin && ibeFrameWin.location.hash && ibeFrameWin.location.hash.indexOf( "scrollAnchor" ) >= 0 ) {
				ibeFrameWin.location.hash = 'scrollAnchor';
			}
		} catch(err) {
			// most likely an error while accessing DARWIN in SSL mode
		}
	}
}

/*
  Function:	AVD_OnIbeFrameReload
  Description:	Called upon onload() call for ibe frame
  Usage:	Used on Avigo IBE page
*/
document.AVD_OnIbeFrameReload = function() {
	//MK: #7768

	if( document.AVD_CheckFrameAccess() ) {
	  var ibeFrameWindow = document.AVD_GetIbeFrameWin();
		if( ibeFrameWindow && ibeFrameWindow.document ) {
			document.AVG_resetSiteBusyMessage();
			ibeFrameWindow.document.showSiteBusyMessage = document.AVG_showSiteBusyMessage;
		}
	}

  document.AVD_checkAndDelayExecution("document.AVD_ResizeIbeFrame();", 100, browserMatchDelay);
  document.AVD_AjaxUpdateDarwinInfo();
}
document.AVD_OnPhpFrameReload = function() {
  AVD_CurrentFrameId= AVD_PhpFrameId;
  document.AVD_checkAndDelayExecution("document.AVD_ResizeIbeFrame();", 100, browserMatchDelay);
}
document.AVD_OnIFrameReload = function() {
  AVD_CurrentFrameId= AVD_IFrameId;
  document.AVD_checkAndDelayExecution("document.AVD_ResizeIbeFrame();", 100, browserMatchDelay);
}




/* Service functions */
document.AVD_GetFrameId = function() {
  if( !AVD_CurrentFrameId || !AVD_CurrentFrameId.length || AVD_CurrentFrameId.length<=0)
  	AVD_CurrentFrameId= AVD_IbeFrameId;
  return AVD_CurrentFrameId;
}
document.AVD_GetIbeFrameWin = function() {
  return window.frames[ document.AVD_GetFrameId() ];
}

document.AVD_AddUrlParameters = function( url, parameters ) {
  if( !parameters || !parameters.length )
    return url;
  var questionPos = url.lastIndexOf( '?' );
  var anchorPos = url.lastIndexOf( '#' );
  var ampPos = url.lastIndexOf( '&' );
  var parametersStr;
  if( questionPos >= 0 ) {
    if( ampPos >= 0 ) {
      parametersStr = '&' + parameters;
    }
    else {
      parametersStr = parameters;
    }
  }
  else {
    parametersStr = '?' + parameters;
  }
  if( anchorPos < 0 )
    return url + parametersStr;
  return url.substr( 0, anchorPos ) + parametersStr + url.substr( anchorPos );
}

document.AVD_EncodeIbeParameters = function( ibeParmName, parameters ) {
  if( !parameters )
    return '';
  if( parameters == '' )
    return '';
  var result = '';
  var parmsArr = parameters.split( '&' );
  for( var i = 0; i < parmsArr.length; i++ ) {
    if( !parmsArr[ i ] )
      continue;
    if( parmsArr[ i ] == '' )
      continue;
    var parmArr = parmsArr[ i ].split( '=' );
    if( parmArr.length == 0 || !parmArr[ 0 ] )
      continue;
    var parmName = parmArr[ 0 ];
    if( parmName == '' )
      continue;
    var parmValue = '';
    if( parmArr.length >= 2 && parmArr[ 1 ] ) {
      parmValue = encodeURI( parmArr[ 1 ].replace( '\:', '|' ) );
    }
    if( result != '' ) {
      result += ':';
    }
    else {
      result = ibeParmName != null ? ( ibeParmName + '=' ) : '';
    }
    result += parmName + ':' + parmValue;
  }  
  return result;
}

document.AVD_DecodeIbeParameters = function( queryString ) {
  if( !queryString )
    return '';
  if( queryString == '' )
    return '';
  if( queryString.charAt( 0 ) == '?' ) {
    queryString = queryString.substr( 1 );
  }
  var ibeParmStr = null;
  var queryArr = queryString.split( '&' );
  for( var i = 0; i < queryArr.length; i++ ) {
    if( !queryArr[ i ] )
      continue;
    if( AVD_IbeParmName == queryArr[ i ].substr( 0, AVD_IbeParmName.length ) ) {
      ibeParmStr = queryArr[ i ];
      break;
    }
  }
  var ibeParmsArr = new Array();
  if( ibeParmStr == null )
    return null;
  var equalPos = ibeParmStr.indexOf( '=' );
  if( equalPos < 0 )
    return null;
  var ibeParmVal = ibeParmStr.substr( equalPos + 1 );
  if( !ibeParmVal || ibeParmVal == '' )
    return null;
  var ibeParmValArr = ibeParmVal.split( ':' );
  for( var i = 0; i < ( ibeParmValArr.length / 2 * 2 ); i += 2 ) {
    var parmName = ibeParmValArr[ i ];
    var parmValue = ibeParmValArr[ i + 1 ];
    if( parmValue ) {
      parmValue = unescape( parmValue );
      parmValue = parmValue.replace( '\|', ':' );
    }
    ibeParmsArr[ parmName ] = unescape( parmValue );
  }
  return ibeParmsArr;
}

document.AVD_GetIbeParametersString = function( queryString ) {
  var ibeParmsArr = document.AVD_DecodeIbeParameters( queryString );
  if( ibeParmsArr == null )
    return '';
  var result = '';
  var curParm;
  for( curParm in ibeParmsArr ) {
    if( result != '' )
      result += '&';
    result += curParm + '=' + ibeParmsArr[ curParm ];
  }
  return result;
}

document.AVD_CheckFrameAccess = function() {
	try {
		var frameWin = window.frames[ document.AVD_GetFrameId() ];
		if( !frameWin )
		  return false;
		var docHt = frameWin.document.height;
	}
	catch( err ) {
		return false;
	}
	return true;
}

document.AVD_ShowWaitingInfo = function() {
	if( AVD_WaitingTimerId )
	  window.clearTimeout( AVD_WaitingTimerId );
	var waitingIcon = document.getElementById( AVD_WaitingIconId );
	if( !waitingIcon )
	  return;
	waitingIcon.style.display = '';
}

document.AVD_BlockRequests = function( evt ) {
  var eventObj = evt ? evt : window.event;
  if( !eventObj )
    return true;
  if( AVD_ProcessingTransfer ) {
  	eventObj.returnValue = false;
  	return false;
 	}
 	else {
  	eventObj.returnValue = true;
  	return true;
  }
}
document.AVD_getPageUrl = function( pageUrlPostfix ) {
    var pageUrl = pageUrlPostfix;
	var curPath = window.location.pathname;
	if( curPath ) {
		//--- fix for 'portal/<agency>/'
		var posEnd = curPath.indexOf( '/', curPath.indexOf( '/', 1 )+1);
		if( posEnd < 0 ) {
			pageUrl = curPath + pageUrl;
		}
		else {
			pageUrl = curPath.substr( 0, posEnd ) + pageUrl;
		}
	}
	//alert("document.avigoSessId"+document.avigoSessId);
	var JSESSIONID = ";jsessionid=";
	if(document.avigoSessId){
		//--- appends jSessionId from the variable
		return pageUrl + JSESSIONID + document.avigoSessId;
	}else{
		//--- appends jSessionId from the URL
		var url = window.location.href;
		if((ind=url.indexOf(JSESSIONID)) < 0){
			return pageUrl;
		}
		var sessId = url.substr((ind + JSESSIONID.length), 32);
		return pageUrl + JSESSIONID + sessId;
	}
}

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/
document.AVD_GetDocHeight = function( doc, minHeight ) {
  var docHt = 0, sh, oh;

  if( doc.body ) {
    if( doc.body.scrollHeight ) 
    	docHt = sh = doc.body.scrollHeight;
    if( doc.body.offsetHeight ) 
    	docHt = oh = doc.body.offsetHeight;
    if( sh && oh ) 
    	docHt = Math.max(sh, oh);
  } else   if( doc.height ) {
  	docHt = doc.height;
  }
  if( !minHeight ) {
	  minHeight = AVD_MinDarwinFrameHeight;
	}
  if( docHt < minHeight )
    docHt = minHeight;
  return docHt;
}
document.AVD_SetIframeHeight = function( minHeight ) {
  var iframeWin = window.frames[ document.AVD_GetFrameId() ];
  var iframeEl = document.getElementById? document.getElementById( document.AVD_GetFrameId() ): document.all ? document.all[ document.AVD_GetFrameId() ]: null;
  if ( iframeEl && iframeWin ) {
    var docHt = document.AVD_GetDocHeight( iframeWin.document, minHeight );
    //need to add to height to be sure it will all show
    if( docHt ) 
    	iframeEl.style.height = docHt + 16 + "px";
  }
  AVD_RepositionIFrameEvent = window.setInterval('document.AVD_RepositionIframe()', 100);
}

//Force repositioning of IFrame content: in some cases, especially when displaying
//more content, some content is cropped at the top of the frame.
//--> reposition once to top and ignore errors (e. g. in ssl some actions are not allowed)
document.AVD_RepositionIframe = function () {
	var iframeWin = window.frames[ document.AVD_GetFrameId() ];
	var iframeEl = document.getElementById? document.getElementById( document.AVD_GetFrameId() ): document.all ? document.all[ document.AVD_GetFrameId() ]: null;
	
	if ( iframeEl && iframeWin ) {
		try {
			if(iframeWin.document.documentElement.scrollTop != 0) {
				// FF: remember scroll position
				var lastPos = document.documentElement.scrollTop; // current scroll position
				lastPos += iframeWin.document.documentElement.scrollTop; // wrong offset to correct
				
				iframeWin.document.documentElement.scrollTop = 0;
				document.documentElement.scrollTop = lastPos;
			}
		} catch (e) { var ii; ii = 1; }
	}
	
	try {
		window.clearInterval(AVD_RepositionIFrameEvent);
	} catch (e) { var iii; iii = 1; }
}

function openwindow(t_url, sbars, width, height, bnav, screenLeft, screenTop) {
    var wx, wy;
    if (bnav == 1) {
        bnav = "menubar=yes,toolbar=yes,";
    } else {
        bnav = "";
    }
    if (sbars == 1) {
        scrollb = "scrollbars=yes,";
    } else {
        scrollb = "";
    }
    if (screen.availWidth >= width) {
       wx = (screen.availWidth - width) / 2;
       if (screenLeft > 0)
         wx = screenLeft;
    } else {
       wx = 0;
    }
    if (screen.availHeight >= height) {
       wy = (screen.availHeight - height) / 2;
       if (screenTop > 0)
         wy = screenTop;
    } else {
        wy = 0;
    }
    return window.open(t_url, '', scrollb + bnav + 'width=' + width + ',height=' + height + ',left=' + wx + ',top=' + wy + ', resizable=yes');
}

function wait(){
	document.AVG_showCenteredMessage( 'globalSiteBusyMessage', 297 );
}
/* ------- check correct email address in RedDot content -------*/
function chkFormular()
{
  var emailTxtObj= document.anmeldung.email;
    if (!(emailTxtObj.value.search(/[@.]/) >= 0)
        || !(emailTxtObj.value.search(/.+@..+\...+/) >= 0)
        || !(emailTxtObj.value.length >= 8)) {
            alert("Bitte geben Sie Ihre korrekte E-Mail Adresse ein");
            emailTxtObj.focus();
            return false;
    }
    return true;
 }
/* --------------------- BEGIN: Waiting Screen ---------------- */
document.AVG_showCenteredMessage = function( id, baseScrollX ) {
  var el, windowHeight, elHeight, elWidth, cy, scrollX, scrollY;
  scrollX = 0;
  if( baseScrollX == null ) {
    baseScrollX = 153;
  }
  if( document.layers ) { /*NN4*/
    el = document[ id ];
    if (el == null){
    	return;
    }
    windowHeight = window.innerHeight;
    scrollY = window.pageYOffset;
    elHeight = el.document.height;
    elWidth = el.document.width;
  }
  else if( document.all ) {/*IE5+*/
		el = document.all[ id ];
	    if (el == null){
	    	return;
	    }
		windowHeight = top.document.documentElement.clientHeight;
		scrollY = top.document.documentElement.scrollTop;
		elHeight = el.offsetHeight;
	  elWidth = el.offsetWidth;
	  if( top != self ) {
	    var curParent = self.frameElement;
	    while( curParent != top.document.body ) {
	      scrollX += curParent.offsetLeft;
	      curParent = curParent.offsetParent;
	    }
	  }
  }
  else if( document.getElementById ) {/*NN6+,FF*/
    el = document.getElementById( id );
    if (el == null){
    	return;
    }
    windowHeight = top.document.documentElement.clientHeight;
	  var frameOffset = 0;
	  if( top != self ) {
	    var curParent = self.frameElement;
	    while( curParent != top.document.body ) {
	      frameOffset += curParent.offsetTop;
	      curParent = curParent.offsetParent;
	    }
	  }
	  scrollY = top.document.documentElement.scrollTop - frameOffset;
	elHeight = el.offsetHeight;
    elWidth = el.offsetWidth;
  }
  else {
  	return;
  }
  cx = baseScrollX + scrollX;
  cy = Math.max( Math.floor( ( windowHeight - elHeight ) / 2 ), 0 ) + scrollY;
	if( cy < 0 ) {
	  cy = 0;
	}
  if( document.layers ) {/*NN4*/
    el.left = cx;
    el.top = cy;
    el.visibility = 'show';
  }
  else if( document.all ) {/*IE5+*/
	  if( !window.DRW_CenteredMessagePopup ) {
			window.DRW_CenteredMessagePopup = window.createPopup();
			if( window.DRW_CenteredMessagePopup ) {
			  window.DRW_CenteredMessagePopup.document.body.innerHTML = el.innerHTML;
			  window.DRW_CenteredMessagePopup.document.body.style.margin = '0px';
			}
	  }
	  if( window.DRW_CenteredMessagePopup ) {
	  	if( window.screenLeft >= 0 || window.screenTop >= 0 ) {
		  window.DRW_CenteredMessagePopup.show( cx, cy, elWidth, elHeight, top.document.body );
	    }
	    else {
	      window.DRW_CenteredMessagePopup.hide();
	    }
	  }
	  else {
			el.style.left = cx + 'px';
	  	el.style.top = cy + 'px';
	  	el.style.visibility = 'visible';
	  }
  }
  else if( document.getElementById ) {/*NN6+,FF*/
    el.style.left = cx + 'px';
    el.style.top = cy + 'px';
    el.style.visibility = 'visible';
	}
}

document.AVG_busySplashScreenDelaySec=2;
document.AVG_showSiteBusyMessage = function() {
	//MK: #7768
	document.AVG_resetSiteBusyMessage();
	AVD_BusyTimerId = self.setInterval( "document.AVG_showCenteredMessage( 'globalSiteBusyMessage', 297 );", (document.AVG_busySplashScreenDelaySec*1000) );
	return true;
}
//MK: #7768
document.AVG_resetSiteBusyMessage = function() {
	if( AVD_BusyTimerId ) {
	  clearInterval( AVD_BusyTimerId );
		AVD_BusyTimerId = 0;
	}
	var busyMessage = document.getElementById( 'globalSiteBusyMessage' );
	if( busyMessage ) {
		busyMessage.style.visibility = 'hidden';
	}
	if( window.DRW_CenteredMessagePopup ) {
		window.DRW_CenteredMessagePopup.hide();
	}
	return true;
}
/* --------------------- END: Waiting Screen ---------------- */

//MK: functions from avigo.js are temporarily moved here to be accessible
function GetDocWidth(doc) {
  var docWt = 0, sw, ow;
  if( doc.width ) {
  	docWt = doc.width;
  }
  else if( doc.body ) {
    if( doc.body.scrollWidth ) 
    	docWt = sw = doc.body.scrollWidth;
    if( doc.body.offsetWidth ) 
    	docWt = ow = doc.body.offsetWidth;
    if( sw && ow )
    	docWt = Math.max(sw, ow);
  }
  return docWt;
}

document.CreateDIV = function (parentObj, id) {
	if(!parentObj) return false;
	var addParentOffset = true;
	if(arguments.length>2) addParentOffset = (arguments[2]=="true");
	var docWidth=GetDocWidth(document);
	var n = parentObj;
	var xpos = n.offsetLeft;
	while ((n=n.offsetParent) != null && addParentOffset) { xpos += n.offsetLeft; }
	var n = parentObj;
	var ypos = n.offsetTop;
	while ((n=n.offsetParent) != null && addParentOffset) { 
		if (!n.id || n.id!="ignoreOffset") 
			ypos += n.offsetTop;
			//alert("ypos="+ypos + " -> "+ n.offsetTop +" ("+ n.tagName+"."+n.id+"."+n.className+")");
	}
	//if ((xpos + 141) > 1002) xpos = 711;
	if ((xpos + 150) > docWidth) xpos = docWidth-155;
	//alert(xpos+"; "+ypos)

	var my_div = document.createElement('DIV');
	my_div.setAttribute("id", id);
	my_div.style.position = 'absolute';
	my_div.style.zIndex = 10;
	my_div.style.left = xpos+"px";
	my_div.style.top = ypos+"px";
	parentObj.parentNode.appendChild(my_div);
	return my_div;
}
document.AVD_SubmitFormRequest = function( actionUrl, requestParmName, requestParmValue, resourceName ) {
	var form = document.createElement( 'form' );
	form.setAttribute( 'method', 'post' );
	form.setAttribute( 'action', actionUrl );
	var requestField = document.createElement( 'input' );
	requestField.setAttribute( 'type', 'hidden' );
	requestField.setAttribute( 'name', requestParmName )
	requestField.setAttribute( 'value', requestParmValue );
	form.insertBefore( requestField, null );
	if( resourceName ) {
		var resourceField = document.createElement( 'input' );
		resourceField.setAttribute( 'type', 'hidden' );
		resourceField.setAttribute( 'name', AVD_ResourceIdParmName )
		resourceField.setAttribute( 'value', resourceName );
		form.insertBefore( resourceField, null );
	}
	document.body.insertBefore( form, null );
	form.submit();
}

document.AVD_showWicketBusy = function( fieldToReset ) {
	document.getElementById(fieldToReset).options[0].selected = true;
	wicketShow('main_body_form_criteria_workingLink--ajax-indicator');
}

document.AVG_resetParentSiteBusyMessage = function() {
	if( AVD_BusyTimerId ) {
	  clearInterval( AVD_BusyTimerId );
		AVD_BusyTimerId = 0;
	}
	var busyMessage = top.document.getElementById( 'globalSiteBusyMessage' );
	if( busyMessage ) {
		busyMessage.style.visibility = 'hidden';
	}
	if( window.DRW_CenteredMessagePopup ) {
		window.DRW_CenteredMessagePopup.hide();
	}
	return true;
}

/*
  Function:	AVD_AjaxUpdateDarwinInfo
  Description:	Called from darwin frame to update darwin session info in Avigo app
  Usage:	Used on Avigo IBE page
*/
function AVD_AjaxUpdateDarwinInfoModalFrame () {

   var ibeFrameWin = document.AVD_GetIbeFrameWin();
  if( !ibeFrameWin || !ibeFrameWin.document )
	  return;
	
  var ibeSessionInfo = ibeFrameWin.document.getElementById( AVD_IbeSessionInfoId );
 	if( !ibeSessionInfo || !ibeSessionInfo.innerHTML )
	  return;
	  
	var ajaxUpdaterForm = parent.document.getElementById( 'darwinAjaxTransmitterPanel_form' );

	if( !ajaxUpdaterForm )
	  return;
	  
	var ajaxUpdaterField =  parent.document.getElementById( 'darwinAjaxTransmitterPanel_form_darwinParameter' );
	
	if( !ajaxUpdaterField )
	  return;
	  
	var darwinInfoString = unescape( ibeSessionInfo.innerHTML );
	var date = new Date();
	var updateUrl = AVD_LinkIdParmName + ':' + date.getTime() + ':' + darwinInfoString;
	ajaxUpdaterField.value = updateUrl;
	
  ajaxUpdaterForm.onsubmit();
}

document.AVD_callUnload = function() {
	document.unload;
}

document.AVG_resetSiteBusyMessageAndPopup = function() {
	if( AVD_BusyTimerId ) {
	  clearInterval( AVD_BusyTimerId );
		AVD_BusyTimerId = 0;
	}
	var busyMessage = document.getElementById( 'globalSiteBusyMessage' );
	if( busyMessage ) {
		busyMessage.style.visibility = 'hidden';
	}
	if( window.DRW_CenteredMessagePopup ) {
		window.DRW_CenteredMessagePopup.hide();
	}
	if(callDarwinClick())
		return true;
}


// This is a fix for browsers which need a delay before execution of a function call
document.AVD_checkAndDelayExecution = function(functionToCall, delay, browserMatch) {
	BrowserDetect.init();
	var match = BrowserDetect.isMatching(browserMatch);

	if(match) {
		// proceed and call after timeout
		window.setTimeout(functionToCall, delay);
	} else {
		// call now
		eval(functionToCall);
	}
}

var BrowserDetect = {
	// http://www.quirksmode.org/js/detect.html#string
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	isMatching: function (browserMatch) { // *** NEW ***
		// BrowserDetect.init() must be called prior to this function call
		// var browserMatch = [ {browser: "Firefox"}, {browser: "Explorer", versions: ["6", "7"]} ];
		
		var browser = BrowserDetect.browser;
		var version = BrowserDetect.version
		// alert(browser + " " + version);
		
		var match = false;
		if(browserMatch) {
			for(var bID = 0; bID < browserMatch.length; bID++) {
				var b = browserMatch[bID];
				if(b.browser == browser) {
					if(b.versions) {
						for(var vID = 0; vID < b.versions.length; vID++) {
							var v = b.versions[vID];
							if(v == version) {
								match = true;
								break;
							}
						}
					} else {
						match = true;
						break;
					}
				}
				
				if(match == true) { break; }
			}
		} else {
			// no browsers defined --> equals all browsers
			match = true;
		}
		return match;
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.userAgent,
			subString: "iPhone",
			identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};
