var objContainer = null;
var mouseX, mouseY;
var loading = document.getElementById('i_ajxloading');

var numFormBeChanged = 0;

/* Jeden Mausklick aufzeichnen */
document.onmousemove	= updateTooltipPosition;

function setChanged(boolChanged)
{
	numFormBeChanged = boolChanged;	
}

function checkForChanges(strMsg)
{
	if(numFormBeChanged==0)
		return true;
	else if(numFormBeChanged==1 && !confirm(strMsg))
		return false;
	else
		return true;
}

function updateTooltipPosition(e) 
{
	var scrollX=0, scrollY=0;
	
	if(document.body && (document.body.scrollLeft || document.body.scrollTop) )
	{
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;	
	}
		
	if( document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop) )
	{
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;	
	}
	
	if(!document.all)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	
	if(window.event && document.all)
	{
		mouseX = window.event.clientX + scrollX;
		mouseY = window.event.clientY + scrollY;
	}
	
	if(loading)
	{
		loading.style.top		= mouseY + 'px';
		loading.style.left		= mouseX + 'px';
	}
}

function absLeft(el)
{
    return (el.offsetParent) ? el.offsetLeft+absLeft(el.offsetParent) : el.offsetLeft;
}

function absTop(el)
{
     return (!document.body.all && el.offsetParent) ? el.offsetTop + absTop(el.offsetParent) : el.offsetTop;
}

function showLoading()
{
	if (!loading)
	{
		loading = document.createElement('div');
		loading.id = 'i_show_loading';
		loading.className = 'show_loading';
		loading.innerHTML = '<img src="gfx/ajaxload.gif" border="0" vspace="0" hspace="0" alt="Bitte warten ... in Bearbeitung" />';
		
		document.getElementsByTagName('body').item(0).appendChild(loading);
	}
	
	//loading.style.top		= mouseY + 'px';
	//loading.style.left		= mouseX + 'px';
	//loading.style.display	= 'block';
	
	return false;
}

function hideLoading()
{
	if(loading)
		loading.style.display	= 'none';
		
	if(objContainer)
	{
		numWidth	= objContainer.offsetWidth;
		numHeight	= objContainer.offsetHeight;
		
		numClientWidth	= document.body.clientWidth;
		numClientHeight	= document.body.clientHeight;
		
		if(numWidth && (absLeft(objContainer)+numWidth) > numClientWidth)
		{
			numX = absLeft(objContainer) - numWidth - 2;
			objContainer.style.left		= numX + 'px';
		}
		
		if(numHeight && (absTop(objContainer)+numHeight) > numClientHeight)
		{
			numY = absTop(objContainer);
			
			numY -= absTop(objContainer) + numHeight - numClientHeight - 2;
			objContainer.style.top		= numY + 'px';
		}
			
	}
}

function loadOverlayContainer(objCaller, strContainerId)
{
	objContainer	= document.getElementById(strContainerId);
	
	if(objContainer)
	{
		objContainer.style.top		= mouseY + 'px';
		objContainer.style.left		= mouseX + 'px';
		objContainer.style.display	= 'block';
	}
}

function unloadOverlayContainer(strContainerId)
{
	var objContainer	= document.getElementById(strContainerId);
	
	if(objContainer)
	{
		objContainer.style.display	= 'none';
		objContainer.innerHTML	= '';
	}
}

function disableField(strObjId, boolDisable)
{
	var objField = document.getElementById(strObjId);
	
	if(objField)
	{
		boolDisable ? objField.setAttribute('disabled', 'disabled') : objField.removeAttribute('disabled'); 
	}
}

/* Funktionen zum Buchungsformular */
var numBookingPriceSummary = 0.00;
var strDecSeperator		= ',';
var strThouSeperator	= '.';

// selektierten Wert eines Dropdowns auslesen
function getSelectedValue(objSelect)
{
	var selectedValue = 0;
	
	if(objSelect)
		for(var i=0; i<objSelect.length; i++)
			if(true==objSelect.options[i].selected)
				selectedValue = objSelect.options[i].value;
		
	return selectedValue;
}

function getSelectedOptionTxt(objSelect)
{
	var selectedTxt = '';
	
	if(objSelect)
		for(var i=0; i<objSelect.length; i++)
			if(true==objSelect.options[i].selected)
				selectedTxt = objSelect.options[i].text;
		
	return selectedTxt;
}

function show(strObjectId)
{
		document.getElementById(strObjectId).style.display = 'block';
}

function hide(strObjectId)
{
		document.getElementById(strObjectId).style.display = 'none';
}

function copyValueToField(strValue, strFieldId)
{
		var objField = document.getElementById(strFieldId);	
		
		if(objField)
			objField.value = strValue;
}

function assignTextTo(strElId, strText)
{
	var objEl = document.getElementById(strElId);
	
	if(objEl)
			objEl.innerHTML = strText;
}

// ### Overlay Zuordnung Objekte
function hideConfigurator()
{
	if(document.getElementById('configurator_layer'))
		document.body.removeChild(document.getElementById('configurator_layer'));
		
	if(document.getElementById('configurator_mask'))
		document.body.removeChild(document.getElementById('configurator_mask'));
		
	if(document.getElementById('footer'))
		document.getElementById('footer').style.display = '';
}

function loadConfigurator(strTableKey, numRelatedId)
{
	self.scrollTo(0,0);
	
	var arrayPageSize	= getPageSize();
	
	var objConfiguratorMask	= document.createElement("div");
	objConfiguratorMask.setAttribute('id', 'configurator_mask');
	objConfiguratorMask.style.width = arrayPageSize[0] + 'px';
	objConfiguratorMask.style.height = '100px';// arrayPageSize[0] + 'px';
	
	var objConfiguratorLayer	= document.createElement("div");
	objConfiguratorLayer.setAttribute('id', 'configurator_layer');
	
	var objConfiguratorWrap	= document.createElement("div");
	objConfiguratorWrap.setAttribute('id', 'configurator_wrap');
	
	var strIframe = '<span class="close_link" style="float:right;"><img src="gfx/close.gif" border="0" alt="Fenster schließen" align="absmiddle" /> <a href="#" onclick="hideConfigurator(); return false;">Fenster schließen</a></span><h1 class="configurator_header">Objekte zuordnen<br />&nbsp;</h1><iframe name="configurator_iframe" id="configurator_iframe" marginwidth="0" marginheight="0" width="760" height="540" src="admin.php5?load=a_objectcollect&do=listRelated&p='+strTableKey+'|'+numRelatedId+'" frameborder="0" scrolling="auto"></iframe>';
	
	objConfiguratorWrap.innerHTML = strIframe;
	
	objConfiguratorLayer.appendChild(objConfiguratorWrap);
	
	document.body.appendChild(objConfiguratorMask);
	document.body.appendChild(objConfiguratorLayer);
	
	if(document.getElementById('footer'))
		document.getElementById('footer').style.display = 'none';
}

function loadConfigurator2(numGalleryId)
{
	self.scrollTo(0,0);
	
	var arrayPageSize	= getPageSize();
	
	var objConfiguratorMask	= document.createElement("div");
	objConfiguratorMask.setAttribute('id', 'configurator_mask');
	objConfiguratorMask.style.width = arrayPageSize[0] + 'px';
	objConfiguratorMask.style.height = arrayPageSize[0] + 'px';
	
	var objConfiguratorLayer	= document.createElement("div");
	objConfiguratorLayer.setAttribute('id', 'configurator_layer');
	
	var objConfiguratorWrap	= document.createElement("div");
	objConfiguratorWrap.setAttribute('id', 'configurator_wrap');
	
	var strIframe = '<span class="close_link" style="float:right;"><img src="gfx/close.gif" border="0" alt="Fenster schließen" align="absmiddle" /> <a href="#" onclick="top.location.reload(); hideConfigurator(); return false;">Fenster schließen</a></span><h1 class="configurator_header">Bilder erstellen<br />&nbsp;</h1><iframe name="configurator_iframe" id="configurator_iframe" marginwidth="0" marginheight="0" width="760" height="540" src="admin.php5?load=a_gallerygen&do=generate&p='+numGalleryId+'" frameborder="0" scrolling="auto"></iframe>';
	
	objConfiguratorWrap.innerHTML = strIframe;
	
	objConfiguratorLayer.appendChild(objConfiguratorWrap);
	
	document.body.appendChild(objConfiguratorMask);
	document.body.appendChild(objConfiguratorLayer);
	
	if(document.getElementById('footer'))
		document.getElementById('footer').style.display = 'none';
}

function getPageSize() 
{
	var xScroll, yScroll;
		
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
		
	var windowWidth, windowHeight;
		
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
		
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
