//<![CDATA[

/// <reference name="MicrosoftAjax.js"/>

/// <summary>
/// Opens a webpage in a new browser window
/// </summary>
/// <param name="url">The URL of the page to open</param>
/// <param name="closeParent">Close the browser window which opens the new window?</param>
/// <param name="winWidth">The width of the new window</param>
/// <param name="winHeight">The height of the new window</param>
/// <param name="centerScreen">Center the window on the screen?</param>
/// <param name="winFeatures">Specify your own featurs like hiding bars</param>
/// <returns>A reference to the window object</returns>
function OpenWindow(url, closeParent, winWidth, winHeight, centerScreen, winFeatures)
{
    //Set (default) window size
    if (winWidth == null) { winWidth = screen.availWidth - 10; }
    if (winHeight == null) { winHeight = screen.availHeight - 28; }

    //Set window position
    var winLeft = 0;
    var winTop = 0;
    if (centerScreen && winWidth != screen.availWidth - 10)
    {
        winLeft = (screen.availWidth - winWidth) / 2;
        if (winLeft < 0) { winLeft = 0; }
        winTop = (screen.availHeight - winHeight) / 2;
        if (winTop < 0) { winTop = 0; }
    }

    //Set window bar properties
    var winBars;
    if (winFeatures == null)
        winBars = "";
    else
        winBars = winFeatures;

    //Set all window properties
    winFeatures = "width=" + winWidth + ", height=" + winHeight + ", left=" + winLeft + ", top=" + winTop + ", " + winBars;

    //Open the window and return the object
    var winOpen = window.open(url, "", winFeatures);

    //Close the browser window which opens the new window	
    if (closeParent)
    {
        window.opener = window.self;
        window.close();
    }

    //Focus window
    try { winOpen.focus(); }
    catch (e) { }

    //Return the reference to new window object
    return winOpen;
}

/// <summary>
/// Opens a new page in a new browser window and sets the specified HTML in the window
/// </summary>
/// <param name="url">The URL of the page to open</param>
/// <param name="closeParent">Close the browser window which opens the new window?</param>
/// <param name="winWidth">The width of the new window</param>
/// <param name="winHeight">The height of the new window</param>
/// <param name="centerScreen">Center the window on the screen?</param>
/// <param name="winFeatures">Specify your own featurs like hiding bars</param>
/// <returns>A reference to the window object</returns>
function OpenWindowWithHtml(html, closeParent, winWidth, winHeight, centerScreen, winFeatures)
{
    var win = OpenWindow('', closeParent, winWidth, winHeight, centerScreen, winFeatures);
    win.document.body.innerHTML = html;
}
/// <summary>
/// Opens a new page in a new browser window and sets the HTML from the specified element value in the window
/// </summary>
/// <param name="url">The URL of the page to open</param>
/// <param name="closeParent">Close the browser window which opens the new window?</param>
/// <param name="winWidth">The width of the new window</param>
/// <param name="winHeight">The height of the new window</param>
/// <param name="centerScreen">Center the window on the screen?</param>
/// <param name="winFeatures">Specify your own featurs like hiding bars</param>
/// <returns>A reference to the window object</returns>
function OpenWindowWithHtmlFromElement(elementId, closeParent, winWidth, winHeight, centerScreen, winFeatures)
{
    var win = OpenWindow('', closeParent, winWidth, winHeight, centerScreen, winFeatures);
    win.document.body.innerHTML = $get(elementId).value;
}

/// <summary>
/// Asks the user for confirmation: Yes/No (using either the askMsg or the controlMsg.value to ask)
/// </summary>
/// <param name="askMsg">The message to ask the user for</param>
/// <param name="controlMsg">Reference to the form control which contains the message (to ask the user for)</param>
/// <returns>True (when the user enters Yes); else False</returns>
function AskConfirmation(askMsg, controlMsg)
{
	var message;

	//If specified use the message from the parameter askMsg
	if (askMsg!=null)
		message = askMsg;
	//else: if available, get the message (for confirmation) from the control send as parameter (controlMsg)
	else if (controlMsg!=null)
		message = controlMsg.value;
	//else: use an empty message
	else
		message = "[...]";
		
	return confirm(message);
}


/// <summary>
/// Shows the user a message (using either the showMsg or the controlMsg.value to show)
/// </summary>
/// <param name="showMsg">The message to show the user</param>
/// <param name="controlMsg">Reference to the form control which contains the message (to show the user)</param>
/// <returns>nothing</returns>
function ShowMessage(showMsg, controlMsg)
{
    //Set cursor back to default (possibly set to Wait by GeneralCmsScripting.OnRequestStart)
    document.body.style.cursor = "default";
    
	var message;

	//If specified use the message from the parameter showMsg
	if (showMsg!=null)
		message = showMsg;
	//else: if available, get the message from the control send as parameter (controlMsg)
	else if (controlMsg!=null)
		message = controlMsg.value;
	//else: use an empty message
	else
		message = "[...]";
		
	alert(message);
}

/// <summary>
/// Handles the default button for a control on the current page (when the user types the Enter key)
/// </summary>
function HandleDefaultButton(btnID, event)
{
	btn = $get(btnID);
	var keyCode;
	if (document.all)
		keyCode = event.keyCode;
	else if (document.getElementById || document.layers)
		keyCode = event.which;

	if (keyCode==13)
	{
		event.returnValue=false;
		event.cancel = true;
		btn.focus();
		btn.click();
	}
}

/// <summary>
/// Resizes the specified image object (by setting its width/height attributes)
/// </summary>
/// <param name="imageObject">HTML Image object</param>
/// <param name="maxWidth">Max. width of the image (leave 0/null to allow all widths)</param>
/// <param name="maxHeight">Max. height of the image (leave 0/null to allow all widths)</param>
function ResizeImage(imageObjectId, maxWidth, maxHeight, newDisplayStyle)
{
    var elm = $get(imageObjectId);
    if (elm.tagName == "A")
        elm = elm.childNodes[0]; //Hyperlink control with ImageUrl set
    
    var img = new Image();
    img.src = elm.src;

    if (maxWidth != null && maxWidth > 0)
    {
        if (img.width > maxWidth)
            elm.style.width = maxWidth;
    }
    if (maxHeight != null && maxHeight > 0)
    {
        if (img.height > maxHeight)
            elm.style.height = maxHeight;
    }
    if (newDisplayStyle != null)
        elm.style.display = newDisplayStyle;
}

/// <summary>
/// Shows the cooltip on the specified element, using the RadTooltipManager
/// </summary>
function showCoolTip(tooltipMgrId, element, cooltipText) 
{
    var tooltipManager = $find(tooltipMgrId);

    //If the user hovers the element before the page has loaded, there is no manager created
    if (!tooltipManager) return;

    //Find the cooltip for this element if it has been created 
    var cooltip = tooltipManager.getToolTipByElement(element);
    //Create a tooltip if no tooltip exists for such element
    if (!cooltip) 
    {
        cooltip = tooltipManager.createToolTip(element);
        //Set the cooltip text
        cooltip.set_text(cooltipText);
    }

    //Let the tooltip's own show mechanism take over from here - execute the onmouseover just once
    element.onmouseover = null;
    //Show the cooltip, using the ShowDelay of the tooltip manager
    window.setTimeout(function() { cooltip.show(); }, tooltipManager.get_showDelay());
}

/// <summary>
/// Centers the specified element on the screen
/// </summary>
function centerElement(elementId)
{
    var element = $get(elementId);
    if (element == null)
        return;

    try
    {
        var scrollTop = document.body.scrollTop;
        var scrollLeft = document.body.scrollLeft;
        var viewPortHeight = document.body.clientHeight;
        var viewPortWidth = document.body.clientWidth;
        if (document.compatMode == "CSS1Compat")
        {
            viewPortHeight = document.documentElement.clientHeight;
            viewPortWidth = document.documentElement.clientWidth;
            scrollTop = document.documentElement.scrollTop;
            scrollLeft = document.documentElement.scrollLeft;
        }
        var topOffset = Math.ceil(viewPortHeight / 2 - element.offsetHeight / 2);
        var leftOffset = Math.ceil(viewPortWidth / 2 - element.offsetWidth / 2);
        var top = scrollTop + topOffset - 40;
        var left = scrollLeft + leftOffset - 70;
        element.style.position = "absolute";
        element.style.top = top + "px";
        element.style.left = left + "px";
    }
    catch (e) { }
}

/// <summary>
/// Sets a textbox readonly (or not readonly) and set is CssClass
/// </summary>
function setTextboxReadonly(elementId, readOnly)
{
    var elm = $get(elementId);
    if (elm == null)
        return;
    
    if (readOnly)
    {
        elm.readOnly = true;
        if (elm.className.indexOf("Readonly") == -1)
            elm.className = elm.className + "Readonly";
    }
    else
    {
        elm.readOnly = false;
        if (elm.className.indexOf("Readonly") != -1)
            elm.className = elm.className.replace("Readonly", "");
    }
}

/// <summary>
/// Sets the specifed value as the selected value in the dropdown
/// </summary>
function setDropdownValue(dropdownId, selectedValue)
{
    try
    {
        var ddl = $get(dropdownId);
        if (ddl == null)
            return;

        for (var i = 0; i < ddl.options.length; i++)
        {
            if (ddl.options[i].value == selectedValue)
            {
                ddl.options[i].selected = true;
                break;
            }
        }
    }
    catch (e) { }
}

/// <summary>
/// Sets the specifed value as the selected value in the radiobutton list
/// </summary>
function setRadiobuttonValue(groupName, selectedValue)
{
    try
    {
        var radioButtons = document.getElementsByName(groupName);
        if (radioButtons == null || radioButtons.length == 0)
            return;

        for (var i = 0; i < radioButtons.length; i++)
        {
            if (radioButtons[i].value == selectedValue)
            {
                radioButtons[i].checked = true;
                break;
            }
        }
    }
    catch (e) { }
}


/// <summary>
/// Sets the value of the specified radiobutton into the specified hidden field
/// </summary>
function setRadioValueInHiddenField(radioButton, hiddenFieldId)
{
    try
    {
        var hf = $get(hiddenFieldId);
        if (hf == null)
            return;
        hf.value = radioButton.value;
    }
    catch (e) { }
}

/// <summary>
/// Shows/hides the specified element
/// </summary>
function showHide(elementId, show)
{
    var elm = $get(elementId);
    if (elm != null)
        elm.style.display = (show ? "" : "none");
    return false;
}

/// <summary>
/// Toggles visibility of the specified element
/// </summary>
function toggleVisibility(elementId)
{
    var elm = $get(elementId);
    if (elm != null)
    {
        if (elm.style.display == "none")
            elm.style.display = "";
        else
            elm.style.display = "none";
    }
    return false;
}

function stopPropagation(e)
{
    if (!e)
    {
        e = window.event;
    }

    e.cancelBubble = true;
}

function isItemInArray(array, item)
{
    if (array != null)
    {
        for (var i = 0; i < array.length; i++)
        {
            if (array[i] == item)
                return true;
        }
    }

    return false;
}

var ExecutedPageLoadScripts = new Array();
function execPageLoadScript(scriptId, functionCall)
{
    if (!isItemInArray(ExecutedPageLoadScripts, scriptId))
    {
        ExecutedPageLoadScripts.push(scriptId);
        window.setTimeout(functionCall, 0);
    }
}
//]]>
