// ======================================================================================
// Opens a new windows with the borderSelection
// ======================================================================================
function OpenBorderWindow(
	kBorderLocationId,
	kPreviewInDocLocationId,
	ePreviewInDocDocType,
	ePreviewInDocLogType,
	kPreviewInDocId,
	strCommandFieldId,
	strSubmitButtonId
	)
{
	var strUrl = m_strWCMSPath + "Order/Query/BorderGalleryPage.aspx?" +
		"kLocationId=" + kBorderLocationId +
		"&kPreviewInDocLocationId=" + kPreviewInDocLocationId +
		"&ePreviewInDocDocType=" + ePreviewInDocDocType +
		"&ePreviewInDocLogType=" + ePreviewInDocLogType +
		"&kPreviewInDocId=" + kPreviewInDocId +
		"&strCommandField=" + strCommandFieldId +
		"&strCommandSubmit=" + strSubmitButtonId;

	var strOptions = "width=630,height=420,resizable=yes,scrollbars=yes,"+
	"status=yes,menubar=no,toolbar=no,location=no,directories=no";

	var oBorderGallery = window.open(strUrl, 'BorderGallery', strOptions);
	oBorderGallery.focus();	
}

// ======================================================================================
// Open the pictureGallery in a new window
// ======================================================================================
function OpenPictureGallery(
	strHiddenCommandFieldId, 
	strSubmitButtonId, 
	strBoxName,
	strSessionId
	)
{
	var strUrl = m_strWCMSPath + "Order/Query/PictureGalleryPage.aspx?strCommandField=" +
		strHiddenCommandFieldId + "&strCommandSubmit=" + strSubmitButtonId + "&strBoxName=" +
		strBoxName + "&bLinkPicturesForInsert=true";
	var strOptions = "width=660,height=450,resizable=yes,scrollbars=yes,"+
		"status=yes,menubar=no,toolbar=no,location=no,directories=no";
	
	var oNewWin = window.open(strUrl, 'pictureWin', strOptions);
	oNewWin.focus();
	
	// return false so parentWindow isn't reloaded if this function is called
	return false;
}

// ======================================================================================
// Set the selected borderId back to parent (which opened the window)
// ======================================================================================
function InsertCommandIntoParent(strHiddenCommandFieldId, strSubmitButtonId, strCommand) 
{
	var oHidden = window.opener.document.getElementById(strHiddenCommandFieldId);	
	if(oHidden == null)	//it could be a firefoxBrwoser
	{	// try firefox syntax
		strHiddenCommandFieldId = strHiddenCommandFieldId.replace(/:/g, "_");
		strSubmitButtonId = strSubmitButtonId.replace(/:/g, "_");
		oHidden = window.opener.document.getElementById(strHiddenCommandFieldId);
	}
	if(oHidden != null)
	{
		oHidden.value = strCommand;
		var oSubmit = window.opener.document.getElementById(strSubmitButtonId);	
		oSubmit.click();
	}
	else
	{
		alert('Hidden field not found');
	}

	window.close();
	
	// return false so parentWindow isn't reloaded if this function is called
	return false;
}

// ======================================================================================
// Opens a new window to show crop
// ======================================================================================
function OpenEditImgCropWindow(
	kLocationId,
	eLogType,
	kId,
	nRow,
	dblProportion,
   nImageXSize,
   nImageYSize,
	nBoxWidth,
	nBoxHeight,
	nBoxXPos,
	nBoxYPos,
	strHiddenCommandFieldId,
	strSubmitButtonId
	)	 
{
	var strOptions = "width=800,height=600," +
		"resizable=yes,scrollbars=yes,status=yes," +
		"menubar=no,toolbar=no,location=no,directories=no";
	var strUrl = m_strWCMSPath + "Order/Booking/BookingEditImgCropPage.aspx?" +
		"XOffset=50" +
		"&YOffset=50" +
		"&kLocationId=" + kLocationId +
		"&eLogType=" + eLogType +
		"&kId=" + kId +
		"&nRow=" + nRow +
		"&dblProportion=" + dblProportion +
		"&nImageXSize=" + nImageXSize +
		"&nImageYSize=" + nImageYSize +
		"&nBoxWidth=" + nBoxWidth +
		"&nBoxHeight=" + nBoxHeight +
		"&nBoxXPos=" + nBoxXPos +
		"&nBoxYPos=" + nBoxYPos +
		"&strHiddenCommandFieldId='" + strHiddenCommandFieldId + "'" +
		"&strSubmitButtonId='" + strSubmitButtonId + "'";
	var newWin = window.open(strUrl, 'newWin', strOptions);
	newWin.focus();
}

// ======================================================================================
// Will be called if the levelCbx is changed
// parameter nCbxIdx: the zeroBasedIndex witch describes which box was changed
// parameter strSelValue: the new selected value of this box
// ======================================================================================
// OH-TODO: overwork
var bIsTopLevelActive = false;
var strLastSubLevel = "";
function SignalLevelCbxChanged(nCbxIdx, strSelValue)
{
//	var oChifre = document.getElementById('_ctl0__ctl0_m_ctrlMainContentPlaceHolder_m_ctrlMainContentPlaceHolder_BookingMgrCtrl_BookingClassificationCtrl_m_oIdChifreSelection');
//	var oIdsForChifreSecondLevel = document.getElementById('_ctl0__ctl0_m_ctrlMainContentPlaceHolder_m_ctrlMainContentPlaceHolder_BookingMgrCtrl_BookingClassificationCtrl_m_oClfIdsForChifreSecondLevel');
//	var oIdsForChifreListSecondLevel = oIdsForChifreSecondLevel.value.split(',');

//	var oIdsForChifre = document.getElementById('_ctl0__ctl0_m_ctrlMainContentPlaceHolder_m_ctrlMainContentPlaceHolder_BookingMgrCtrl_BookingClassificationCtrl_m_oClfIdsForChifre');
//	var oIdsForChifreList = oIdsForChifre.value.split(',');
//	var bIsChifreContained = false;
//	var bIsChifreContainedSecondLevel = false;

//	if(nCbxIdx == 0)
//	{
//		for(var nI = 0; nI < oIdsForChifreList.length && bIsChifreContained == false; nI++)
//		{
//			if(oIdsForChifreList[nI] == strSelValue)
//			{
//				bIsChifreContained = true;
//			}
//		}
//		for(var nI = 0; nI < oIdsForChifreListSecondLevel.length && bIsChifreContainedSecondLevel == false; nI++)
//		{
//			if(oIdsForChifreListSecondLevel[nI] == strLastSubLevel)
//			{
//				bIsChifreContainedSecondLevel = true;
//			}
//		}

//		if((oChifre != null) && (oIdsForChifre != null))
//		{
//			if((!bIsChifreContained) && (!bIsChifreContainedSecondLevel))
//			{
//				bIsTopLevelActive = false;
//				oChifre.style.visibility = 'hidden';
//				document.getElementById('_ctl0__ctl0_m_ctrlMainContentPlaceHolder_m_ctrlMainContentPlaceHolder_BookingMgrCtrl_BookingClassificationCtrl_m_cbxBoxNumberAd').selectedIndex = -1;
//			}
//			else
//			{
//				oChifre.style.visibility = '';
//				bIsTopLevelActive = bIsChifreContained;

//			}

//		}
//	}
//	if(nCbxIdx == 1)
//	{
//		strLastSubLevel = strSelValue;
//		for(var nI = 0; nI < oIdsForChifreListSecondLevel.length && bIsChifreContainedSecondLevel == false; nI++)
//		{
//			if(oIdsForChifreListSecondLevel[nI] == strSelValue)
//			{
//				bIsChifreContainedSecondLevel = true;
//			}
//		}
//		if((oChifre != null) && (oIdsForChifreSecondLevel != null))
//		{
//			if((!bIsChifreContainedSecondLevel) && (!bIsTopLevelActive))
//			{
//				oChifre.style.visibility = 'hidden';
//				document.getElementById('_ctl0__ctl0_m_ctrlMainContentPlaceHolder_m_ctrlMainContentPlaceHolder_BookingMgrCtrl_BookingClassificationCtrl_m_cbxBoxNumberAd').selectedIndex = -1;
//			}
//			else
//			{
//				oChifre.style.visibility = '';
//			}
//		}
//	}
}

// ======================================================================================
// Highlights the selected rows if a row isclicked, all other rows will be set back to default
// (only for etuModes RadioButtonList and CheckBoxList)
// (set 'bOnlyOneSelectableRow' is true if we have e.g. radioButtons as selectors)
// (set 'bAllowOneRadioSelectionOnEachGroup' is true if we allow one radioSelection foreach group)
// ======================================================================================
function BookingETUCtrlMarkUpSelectedRow(
	oSelector,
	bOnlyOneSelectableRow,
	bAllowOneRadioSelectionOnEachGroup
	)
{
	// get row
	var oCurElement = oSelector;
	while(oCurElement && !(oCurElement.tagName == "TR"))
	{
		oCurElement = oCurElement.parentNode;
	}

	if(bOnlyOneSelectableRow)
	{	// deselect all rows
		if(bAllowOneRadioSelectionOnEachGroup)
		{
			// get table>
			var oTableBody = oCurElement.parentNode;
			for(var i = 0; i < oTableBody.childNodes.length; i++)
			{	// don't set style for separatorRows
				// search for childCell
				if(oTableBody.childNodes[i].childNodes.length > 1 &&
				oTableBody.childNodes[i].className != "WcmsETUListSeparatorRow")
				{
					oTableBody.childNodes[i].className = "WcmsETUListRow";
				}
			}
		}
		else
		{
			var oTRs = document.getElementsByTagName("TR");
			for(var i = 0; i < oTRs.length; i++)
			{	// don't set style for separatorRows
				// search for childCell
				if(oTRs[i].childNodes.length > 1 &&
				oTRs[i].className == "WcmsETUListRowHighlighted")
				{
					oTRs[i].className = "WcmsETUListRow";
				}
			}
		}
	}

	// set style for selected element
	if(oCurElement != oSelector)
	{
		if(oSelector.checked)
		{
			oCurElement.className = "WcmsETUListRowHighlighted";
		}
		else
		{
			oCurElement.className = "WcmsETUListRow";
		}
	}
}

// ======================================================================================
// Fires the selector by clicking the complete row (only for etuModes RadioButtonList and CheckBoxList)
// (set 'bOnlyOneSelectableRow' is true if we have e.g. radioButtons as selectors)
// ======================================================================================
function BookingETUCtrlRowSelect(strSelectorName, bOnlyOneSelectableRow)
{
	/* comment all following line out if we don't want selecting the entry by the complete row */
	var oSelector = document.getElementById(strSelectorName);
	if(bOnlyOneSelectableRow)
	{
		oSelector.checked = true;
	}
	else
	{
		oSelector.checked = !oSelector.checked;
	}
	oSelector.onclick();
}

// ======================================================================================
// This function tests, if the correct version of silverlight (for adLight) is installed
// ======================================================================================
function CheckSilverlightVersion(version, strControlName)
{
	var isVersionSupported = false;
	var container = null;

	try
	{
		try
		{
			// check Silverlight in IE
			var control = new ActiveXObject('AgControl.AgControl');
			if(version == null)
			{
				isVersionSupported = true;
			}
			else if(control.IsVersionSupported(version))
			{
				isVersionSupported = true;
			}
			control = null;
		}
		catch(e)
		{
			// check Silverlight for other browser
			var plugin = navigator.plugins["Silverlight Plug-In"];
			if(plugin)
			{
				if(version == null)
				{
					isVersionSupported = true;
				}
				else
				{
					var actualVer = plugin.description;
					// split both versions -> needed and existing into their subparts to check each of them
					var actualVerArray = actualVer.split(".");
					var reqVerArray = version.split(".");

					var requiredVersionPart;
					var actualVersionPart
					var index = 0;

					isVersionSupported = true;
					do
					{
						requiredVersionPart = parseInt(reqVerArray[index]);
						actualVersionPart = parseInt(actualVerArray[index]);
						index++;

						if(actualVersionPart < requiredVersionPart)
						{
							isVersionSupported = false;
							break;
						}
					}
					while(index < reqVerArray.length && index < actualVerArray.length);
				}
			}
		}
	}
	catch(e)
	{
		isVersionSupported = false;
	}
	if(container)
	{
		document.body.removeChild(container);
	}

	if(strControlName != null && !isVersionSupported)
	{
		document.getElementById(strControlName).style.display = '';
    }

    return isVersionSupported;
}

// ======================================================================================
function PollSilverlightInstalled(version) {

    try {
        navigator.plugins.refresh();
    }
    catch (e) {
    }

    var bInstalled = CheckSilverlightVersion(version, null);
    if (bInstalled) {
        window.location.reload(false);
    }
    else {
        setTimeout(function() { PollSilverlightInstalled(version) }, 3000);
    }
}

// ======================================================================================
// Use global vars for slogan insertion to prevent quotation confusion.
// Insert a selected slogan in the sloganBox.
// ======================================================================================
function InsertSloganGVars(nSloganNumber, bUseHTMLEditor)
{
	debugger;
	// set selected slogan in text box or radEditorBox
	if(bUseHTMLEditor)
	{
		var wtbxSlogan = GetRadEditor(m_strTextBoxId);
		if(wtbxSlogan != null)
		{
			wtbxSlogan.SetHtml(m_strSlogan[nSloganNumber]);
		}
	}
	else
	{
		var wtbxSlogan = document.getElementById(m_strTextBoxId);
		if(wtbxSlogan != null)
		{
			wtbxSlogan.innerHTML = m_strSlogan[nSloganNumber];
		}
	}

	var wtbnPreview = document.getElementById(m_strBtnPreviewId);
	if(wtbnPreview != null)
	{
		// connent following line in to make a automatic refresh after inserting the slogan
		//wtbnPreview.click();
	}
}
