// ======================================================================================
// Call resizeFunction every time
// ======================================================================================
window.onload = ResizePictures;

// ======================================================================================
// Opens a new window to show a doc
// ======================================================================================
function OpenDialogDocWindow(
	kLocationId,
	kPublisherId,
	eLogType,
	kId,
	eDocType,
	kLevelLocationId
	)	 
{
	var strOptions = 'width=400,height=450,resizable=yes,scrollbars=yes,status=yes,' +
		'menubar=no,toolbar=no,location=no,directories=no';
	var strUrl = m_strWCMSPath + 'Common/DialogDoc.aspx?kLocationId=' + kLocationId +
		'&kPublisherId=' + kPublisherId +
		'&eLogType=' + eLogType +
		'&kId=' + kId +
		'&eDocType=' + eDocType +
		'&kLevelLocationId=' + kLevelLocationId;	
	var oWin = window.open(strUrl, 'DialogDocWindow', strOptions);
	oWin.focus();
}

// ======================================================================================
// Show a preview window with given url
// ======================================================================================
function ShowPreview(strUrl) 
{	
	var strOptions = "width=750,height=550," +
		"resizable=yes,scrollbars=yes,status=yes," +
		"menubar=no,toolbar=no,location=no,directories=no";
   var oWin = window.open(strUrl, 'PreviewWindow', strOptions);
	oWin.focus();
}

// ======================================================================================
// Show a hint for forgotten logout
// ======================================================================================
function ForgottenLogout()
{
	var strOptions = "width=400,height=450,resizable=yes,scrollbars=yes,status=yes," +
		"menubar=no,toolbar=no,location=no,directories=no";
	var strUrl = m_strWCMSPath + "Common/ForgottenLogout.htm";
   var oWin = window.open(strUrl, 'ForgottenLogout', strOptions);
   oWin.focus();
}

// ======================================================================================
// Jumps to the next by name declared textbox after inserting the given number of digits
// (ATTENTION: this next textbox must be named)
// ======================================================================================
function JumpToNextTextBox(oEvent, oCurrentTextBox, nNumberOfDigitsToJump, oNameOfNextTextBox)
{
	if(oCurrentTextBox.value.length >= nNumberOfDigitsToJump)
	{
		// get pressed keyChar
		if(!oEvent)
		{
			oEvent = window.event;
		}
		var oKeyNum = oEvent.keyCode;
		var oKeyChar = String.fromCharCode(oKeyNum);

		// don't handle tab and shift
		if(oKeyNum != 0x09 && oKeyNum != 0x10)
		{
			var oElements = document.forms[0].elements;
			for(var i = 0; i < oElements.length; i++)
			{
				if(oElements[i].id.indexOf(oNameOfNextTextBox) != -1)
				{
					document.getElementById(oElements[i].id).focus();
					break;
				}
			}
		}
	}
}

// ======================================================================================
// Checks if number is a key
// ======================================================================================
function IsNumberKey(oEvent, bAllowComma)
{
	var bIsAllowedKey = true;
	var nCharCode = (oEvent.which) ? oEvent.which : event.keyCode
	if(nCharCode > 31 && (nCharCode < 48 || nCharCode > 57) && (!bAllowComma || nCharCode != 44))
	{
		bIsAllowedKey = false;
	}

	return bIsAllowedKey;
}

// ======================================================================================
// Adapt pictureSize automatically
// ======================================================================================
function ResizePictures()
{
	if(document.getElementsByTagName)
	{
		var aoImages = document.getElementsByTagName("img");

		for(var i = 0; i < aoImages.length; i++)
		{
			if(aoImages[i].name.indexOf('ResizePicture') != -1)
			{
				var astrName = aoImages[i].name.split('_');
				if(astrName[0].indexOf('ResizePicture') == -1)
				{
					alert("Base.js:ResizePictures(): for automatically resizing the firstPart of the name must be 'ResizePicture'");
				}
				if(astrName.length != 4)
				{
					alert("Base.js:ResizePictures(): the named must be of 4 values separated by '_' (strName_nWidth_nHeight_bOriginalViewInPopUp) (e.g. 'ResizePicture_400_400_true')");
				}
				// parse needed params
				var nMaxHeightToResize	= astrName[1];	// maximum pictureHeight
				var nMaxWidthToResize	= astrName[2];	// maximum pictureWidth
				var strAlterViewOnClick	= astrName[3];	// 'popup' opens the fullView onClick
																	// 'zoom' resize picture onClick
																	// 'no' nothing is done onClick
																	
				var nResizeWidth = aoImages[i].width;
				var nResizeHeight = aoImages[i].height;

				// parameters for the popUpWindow
				var strOptions = "width=550,height=550," +
					"resizable=yes,scrollbars=yes,status=yes," +
					"menubar=no,toolbar=no,location=no,directories=no";

				if(nResizeWidth > nMaxWidthToResize &&
					(nResizeWidth / nMaxWidthToResize) > (nResizeHeight / nMaxHeightToResize))
				{
					aoImages[i].style.width = nMaxWidthToResize + 'px';
					
					if(strAlterViewOnClick != 'no')
					{
						aoImages[i].title = unescape("Klicken Sie auf das Bild um es in volle Gr%F6%DFe anzusehen.");
						onmouseover = aoImages[i].style.cursor= 'pointer';
						aoImages[i].onclick = function()
						{
							if(strAlterViewOnClick == 'popup')
							{
								var oNewWin = window.open(this.src, 'PreviewWindow', strOptions);
								// alternative: write new windows html self (with e.g. a closeButton)
								// var oNewWin = window.open('', 'PreviewWindow', strOptions);
								// oNewWin.document.write('<html><head><title>Vollbildansicht</title></head><body><table><tr><td><div style="text-align:center;"><input type="button" onclick="window.close(); return false;" value="Fenster schlie&szlig;en" /></div><img src=' + this.src + '><br /><div style="text-align:center;"><input type="button" onclick="window.close(); return false;" value="Fenster schlie&szlig;en" /></div></td></tr></table></body></html>');
							}
							else if(strAlterViewOnClick == 'zoom')
							{
								if(this.style.width != '')
								{
									this.style.width = '';
								}
								else
								{
									this.style.width = nMaxWidthToResize + 'px';
								}
							}
						}
					}
					// else we don't resive onClick
				}
				else if(nResizeHeight > nMaxHeightToResize &&
					(nResizeHeight / nMaxHeightToResize) >= (nResizeWidth / nMaxWidthToResize))
				{
					aoImages[i].style.height = nMaxHeightToResize + 'px';

					if(strAlterViewOnClick != 'no')
					{
						aoImages[i].title = unescape("Auf das Bild klicken f%FCr volle Gr%F6%DFe.");
						onmouseover = aoImages[i].style.cursor= 'pointer';
						aoImages[i].onclick = function()
						{
							if(strAlterViewOnClick == 'popup')
							{
								var oNewWin = window.open(this.src, 'PreviewWindow', strOptions);
								// alternative: write new windows html self (with e.g. a closeButton)
								// var oNewWin = window.open('', 'PreviewWindow', strOptions);
								// oNewWin.document.write('<html><head><title>Vollbildansicht</title></head><body><table><tr><td><div style="text-align:center;"><input type="button" onclick="window.close(); return false;" value="Fenster schlie&szlig;en" /></div><img src=' + this.src + '><br /><div style="text-align:center;"><input type="button" onclick="window.close(); return false;" value="Fenster schlie&szlig;en" /></div></td></tr></table></body></html>');
							}
							else if(strAlterViewOnClick == 'zoom')
							{
								if(this.style.height != '')
								{
									this.style.height = '';
								}
								else
								{
									this.style.height = nMaxHeightToResize + 'px';
								}
							}
						}
					}
					// else we don't react onClick
				}
			}
		}
	}
}

// ======================================================================================
// Load the given values into the given comboBox (if bluue is true an empty item is added)
// ======================================================================================
function LoadComboBox(wcbxSelect, aoDropDownValues, bAddEmptyField)
{
	// clear old item of comboBox
	ClearComboBox(wcbxSelect);

	var nSelectIdx = 0;
	if(bAddEmptyField)
	{
		var oOption = document.createElement('option');
		oOption.text = '';
		oOption.value = '-1';
		// ATTENTION: don't use 'wcbxSelect.add()' becuase it doesn't work the same
		// way in ie and firefox
		wcbxSelect.options[nSelectIdx++] = oOption;
	}

	// fill new values into the combobox
	for(nValueIdx = 0; nValueIdx < aoDropDownValues.length; nValueIdx++)
	{
		var oOption = document.createElement('option');
		oOption.text = aoDropDownValues[nValueIdx].name;
		oOption.value = aoDropDownValues[nValueIdx].value;
		// ATTENTION: don't use 'wcbxSelect.add()' because it doesn't work the same
		// way in ie and firefox
		wcbxSelect.options[nSelectIdx++] = oOption;
	}
}

// ======================================================================================
// Clears all items from given comboBox (select)
// ======================================================================================
function ClearComboBox(wcbxSelect)
{
	while(wcbxSelect.length != null && wcbxSelect.length > 0)
	{
		wcbxSelect.remove(0);
	}
}

// ======================================================================================
// Show a hint in the given comboBox that data is loading from webService
// ======================================================================================
function ShowLoadingMessage(wcbxSelect)
{
	// save last selected value (becauseshowing the hint will delete content)
	var nOldValue = wcbxSelect.value;
	wcbxSelect.setAttribute('nSelectedValue', wcbxSelect.value == '' ? -1 : wcbxSelect.value);

	ClearComboBox(wcbxSelect);

	var oOption = document.createElement('option');
	oOption.text = '[Daten werden geladen]';
	oOption.value = '-1';
	// ATTENTION: don't use 'wcbxSelect.add()' becuase it doesn't work the same
	// way in ie and firefox
	wcbxSelect.options[0] = oOption;

	// deactivate comboBox
	wcbxSelect.disabled = true;
}

// ======================================================================================
// Deactivates the is loading from webService hint
// ======================================================================================
function HideLoadingMessage(wcbxSelect)
{
	ClearComboBox(wcbxSelect);

	// activate comboBox
	wcbxSelect.disabled = false;

	return wcbxSelect.getAttribute('nSelectedValue');
}

