﻿Event.observe(window, 'load', onPageLoad);
Event.observe(window, 'resize', calcContentSize);

var functionForPageProperties = null;
var bPageLoaded = false;

function onPageLoad()
{
    hideLinksStatus();
    pageLoaded();
    calcContentSize();
    updateRoundedButtons();
    switchOffEnterPress();
}

function pageLoaded()
{
    bPageLoaded = true;
}

function switchOffEnterPress() 
{
    var aControls = $$('input.ActivityPropertyControl');
    for(var iControl = 0; iControl < aControls.length; iControl++)
    {
        var oControl = aControls[iControl];
        Event.observe(oControl, 'keydown', onActivityPropertyControlKey);
        Event.observe(oControl, 'keyup', onActivityPropertyControlKey);
        Event.observe(oControl, 'keypress', onActivityPropertyControlKey);
    }
}

function onActivityPropertyControlKey(p_oEvent) 
{
    if (p_oEvent.keyCode == Event.KEY_RETURN) 
    {
        Event.stop(p_oEvent);
        return false;
    }
}

function onKeyDown(p_oEvent, p_sAccountID, p_sLoginID, p_sPasswordID, p_sLoginButtonID)
{
    if (p_oEvent.keyCode == 13) 
    {
        $(p_sLoginButtonID).focus();
    }
}

function hideLinksStatus()
{
    var aLinks = document.getElementsByTagName('a');
    for(var iLink = 0; iLink < aLinks.length; iLink++)
    {
        Event.observe(aLinks[iLink], 'mousemove', hideStatus);
        Event.observe(aLinks[iLink], 'mouseover', hideStatus);
        Event.observe(aLinks[iLink], 'mouseout', hideStatus);
        validateUrl(aLinks[iLink]);
    }
}

function validateUrl(p_oLink)
{
    if(!p_oLink || !p_oLink.parentNode)
        return;
        
    var ctlTopAnchor = $('topAnchor');
    var sTopAnchor = "#";
    if(ctlTopAnchor != null)
        sTopAnchor = sTopAnchor + ctlTopAnchor.name;
        
    if(p_oLink.parentNode.className == 'topbuttonbutton')
        p_oLink.href = sTopAnchor;
}

function hideStatus()
{
    window.status = "";
}

function ChangeLayout(p_iLayoutID)
{   
    var sMode = eecUtils.getQueryStringValue('Mode');
    if(sMode == 'View')
        return;
    
    var hid = $("hidLayoutID");
    var cmd = $("cmdChangeLayout");
    
    if (hid != null && cmd != null)
    {
        hid.value = p_iLayoutID;
        cmd.click();
    }
}

function OpenEditWindow(p_iContentID, p_iControlID, p_sParameters) {
    if (typeof (p_sParameters) == "undefined" || p_sParameters == null)
        p_sParameters = "";
        
    var wnd = window.open("EditContent.aspx?ContentID=" + p_iContentID + "&ControlID=" + p_iControlID
        + "&SiteID=" + document.all["txtHiddenSiteID"].value + p_sParameters, 
        "EditContent", "width=800,height=500,top=0,left=0,scrollbars=yes,resizable=yes");
    wnd.focus();
}

function editNavigateBarProperties()
{
    var frmProperties = window.parent.frames["frmProperties"].window;
    frmProperties.location.replace("PageProperties.aspx?Mode=NavigateBar&SiteID=" + iSiteID); 
}

function editZoneProperties(zoneID)
{
    var propertiesWindow = window.parent.frames["frmProperties"].window;    
    propertiesWindow.location.replace("ZoneProperties.aspx?ZoneID=" + zoneID);    
}

function CallFunctionForPageProperties()
{
    if(functionForPageProperties && null != functionForPageProperties)
    {
        window.execScript(functionForPageProperties, "javascript");
        functionForPageProperties = null;
    }
}

function SaveNavigateBarProperties(
    p_sTextColor, 
    p_sBackgroundColor,
    p_sHeaderColor,
    p_sLinkColor
)
{            
    document.getElementById("hidNBTextColor").value = p_sTextColor;
    document.getElementById("hidNBBackgroundColor").value = p_sBackgroundColor;
    document.getElementById("hidNBHeaderColor").value = p_sHeaderColor;
    document.getElementById("hidNBLinkColor").value = p_sLinkColor;
    document.getElementById("cmdUpdateNavigateBarProperties").click();
}

function EditTabsProperties()
{
    var propertiesWindow = window.parent.frames["frmProperties"].window;  
    functionForPageProperties = "SetTabsProperties();";
    propertiesWindow.location.replace("PageProperties.aspx");    
}

function SetTabsProperties()
{
    var propertiesWindow = window.parent.frames["frmProperties"].window;
    if (propertiesWindow.SetTabsProperties)
    {
        propertiesWindow.SetTabsProperties(
            document.getElementById("hidTabsStandardText").value,
            document.getElementById("hidTabsStandardBackground").value,
            document.getElementById("hidTabsSelectedText").value,
            document.getElementById("hidTabsSelectedBackground").value,
            document.getElementById("hidTabsHeight").value,
            document.getElementById("hidTabsDisplayMode").value
        );            
    }
}

function SaveTabsProperties(
    p_sTabsStandardText,
    p_sTabsStandardBackground,
    p_sTabsSelectedText,
    p_sTabsSelectedBackground,
    p_iTabsHeight,
    p_iTabsDisplayMode
)
{             
    document.getElementById("hidTabsStandardText").value = p_sTabsStandardText;
    document.getElementById("hidTabsStandardBackground").value = p_sTabsStandardBackground;
    document.getElementById("hidTabsSelectedText").value = p_sTabsSelectedText;
    document.getElementById("hidTabsSelectedBackground").value = p_sTabsSelectedBackground;
    document.getElementById("hidTabsHeight").value = p_iTabsHeight;
    document.getElementById("hidTabsDisplayMode").value = p_iTabsDisplayMode;
           
    document.getElementById("cmdUpdateTabsProperties").click();
}

function EditPageProperties()
{
    var propertiesWindow = window.parent.frames["frmProperties"].window; 
    functionForPageProperties = "SetPageProperties();"; 
    propertiesWindow.location.replace("PageProperties.aspx");
}
function SetPageProperties()
{
    var propertiesWindow = window.parent.frames["frmProperties"].window;
    if (propertiesWindow.SetPageProperties)
    {            
        propertiesWindow.SetPageProperties(
            document.getElementById("hidPageTitleColor").value,
            document.getElementById("hidPageBackgroundColor").value,
            document.getElementById("hidDescriptionBackgroundColor").value,
            document.getElementById("hidPartHeaderColor").value,
            document.getElementById("hidPartHeaderTextColor").value,
            document.getElementById("hidDescriptionEditable").value
        );            
    }
}

function SavePageProperties(
    p_sPageTitleColor,
    p_sPageBackgroundColor,
    p_sDescriptionBackgroundColor,
    p_sPartHeaderColor,
    p_sPartHeaderTextColor,
    p_bDescriptionEditable
)
{            
    document.getElementById("hidPageTitleColor").value = p_sPageTitleColor;
    document.getElementById("hidPageBackgroundColor").value = p_sPageBackgroundColor;
    document.getElementById("hidDescriptionBackgroundColor").value = p_sDescriptionBackgroundColor;
    document.getElementById("hidPartHeaderColor").value = p_sPartHeaderColor;
    document.getElementById("hidPartHeaderTextColor").value = p_sPartHeaderTextColor;
    document.getElementById("hidDescriptionEditable").value = p_bDescriptionEditable;
           
    document.getElementById("cmdUpdatePageProperties").click();
}

function SaveProperties(p_sTitle, p_iChromeType, p_sHeight, p_iHeightType, p_sWidth, p_iWidthType, p_bEditable, p_sWebPartID, p_sSettingsJson)
{
//            document.getElementById("hidWebPartID").value = p_sWebPartID;

//            document.getElementById(p_sWebPartID + "_hidTitle").value = p_sTitle;
//            document.getElementById(p_sWebPartID + "_hidChromeType").value = p_iChromeType;
//            document.getElementById(p_sWebPartID + "_hidHeight").value = p_sHeight;
//            document.getElementById(p_sWebPartID + "_hidHeightType").value = p_iHeightType;
//            document.getElementById(p_sWebPartID + "_hidWidth").value = p_sWidth;
//            document.getElementById(p_sWebPartID + "_hidWidthType").value = p_iWidthType;
//            var hidEditable = document.getElementById(p_sWebPartID + "_hidEditable");
    document.getElementById("hidPartID").value = p_sWebPartID;

    document.getElementById("hidTitle").value = p_sTitle;
    document.getElementById("hidChromeType").value = p_iChromeType;
    document.getElementById("hidHeight").value = p_sHeight;
    document.getElementById("hidHeightType").value = p_iHeightType;
    document.getElementById("hidWidth").value = p_sWidth;
    document.getElementById("hidWidthType").value = p_iWidthType;     
    document.getElementById("hidSettingsJson").value = p_sSettingsJson;       
    var hidEditable = document.getElementById("hidEditable");
    if (hidEditable)
        hidEditable.value = p_bEditable;
   // document.getElementById("hidPartID").value = p_iWidthType;    
        
    document.getElementById("cmdUpdatePartProperties").click();
}               

function EditPartProperties(p_sWebPartID)
{
    var hidPartID = document.getElementById(p_sWebPartID + "_hidPartID");
    var propertiesWindow = window.parent.frames["frmProperties"].window;  
    propertiesWindow.location.replace("PartProperties.aspx?PropertiesType=Part&PartID=" + hidPartID.value + "&CompanyID=" + currentCompanyID);
} 

function CustomPropertyDescriptor(p_sID, p_sType, p_sTitle, p_value)
{
	this.ID = p_sID;
	this.Type = p_sType;
	this.Title = p_sTitle;
	this.Value = p_value;
}        

function SetCustomProperties(aPropertyDescriptors, sCommand, fncValidate)
{			
	var propertiesWindow = window.parent.frames["frmProperties"].window;  
    if (propertiesWindow.document.Script["SetCustomProperties"] != null)
    {
		propertiesWindow.SetCustomProperties(aPropertyDescriptors, sCommand, SaveCustomProperties, fncValidate);
    }
}

function SaveCustomProperties(sCommand, aResults)
{						
	Repository_SetTargetCommand(sCommand);
	for (var sKey in aResults)
	{
		Repository_AddParameter(sKey, aResults[sKey]);
	}
	Repository_Submit();
}

function HideProperties()
{                        
    var propertiesFrame = window.parent.frames["frmProperties"]
    if (propertiesFrame)
    {
        var propertiesWindow = propertiesFrame.window;  
        if (propertiesWindow)
        {
            if (propertiesWindow.HideProperties)
                propertiesWindow.HideProperties();
        }
    }
   
}                  

function SelectHReference_Close(p_sAreaID, p_sHref)
{
    var areaElement = document.getElementById(p_sAreaID);
    if (areaElement)
    {
        areaElement.title = p_sHref;
        var href = areaElement.href;
        areaElement.href = href.substring(0, href.indexOf('(') + 1) + "'" + p_sHref + "'" + href.substring(href.indexOf(','));
    }
}

function EditPartSettings(p_sWebPartID)
{
    var propertiesWindow = window.parent.frames["frmProperties"].window;
    if (propertiesWindow.ShowSettings)
    {
        propertiesWindow.ShowSettings(p_sWebPartID);
    }
}     

function SavePageContent(stageId)
{
    var html = document.getElementsByTagName("html")[0];
    if(!html || null == html)
        return;
    $("hdnMailTemplateStageID").value = stageId;
    $("btnSavePageContent").click();
}   
function SendTestMail(stageId)
{
    $("hdnMailTemplateStageID").value = stageId;
    $("btnSendTestMail").click();
}
function SetClientWidthToElement(containerId, elementId)
{    
    var element = $(elementId);
    var container = $(containerId);
    if(null != element && null != container) 
        element.style.width = container.clientWidth;
    
    //alert($(elementId).getWidth());
}
function refreshToolbar()
{
    window.parent.frames["frmToolBox"].location.replace("ToolBox.aspx");  
}
function reloadPage()
{
    window.location.reload();
}

function SelectPage(iSiteID, iPageID, sOrderUID)
{
    if ((window.parent != window) && (window.parent.frames["frmSiteExplorer"] != null)) 
    {
        window.parent.frames["frmSiteExplorer"].SelectPage(iSiteID, iPageID);
    }
    else
    {
        sUrl = "Page.aspx?PageID=" + iPageID;
        if (sOrderUID != null && sOrderUID != "")
            sUrl += "&OrderUID=" + sOrderUID;
        window.location.href = sUrl;
    }
}

function setGridHeight()
{
   if(!bPageLoaded)
   {
        Event.observe(window, 'load', setHeightToGrid);
   }
   else
   {
        setHeightToGrid();
   }
}

// sGridFrameId - declared in BaseGrid.cs
function setHeightToGrid() {
    var fGrid;
    var cGrid;
    //debugger;
    for(var iGridFrame = 0; iGridFrame < frames.length; iGridFrame++)
    {
        if (frames[iGridFrame].frameElement.id != sGridFrameId) 
            continue;

        fGrid = frames[iGridFrame].window;
        cGrid = $(frames[iGridFrame].frameElement);
    }
    // set grid height to 100, for valid page empty space calculation    
    cGrid.setStyle({height : "100px"});
   
    var iFooterPosY = Prototype.Position.cumulativeOffset($('tFooter'))[1];
    var iGridPosY = Prototype.Position.cumulativeOffset(cGrid)[1];
    var iNewGridHeight = iFooterPosY - iGridPosY - $('tFooter').getHeight() - 11;
        
    if(fGrid!= null && fGrid.getGridHeight)
    {
        var iRGGridHeight = fGrid.getGridHeight() + 5;
        if(window.opera)
            iRGGridHeight += 11;
            
        if(iNewGridHeight < iRGGridHeight)
        {
            iNewGridHeight = iRGGridHeight;
        }
    }

    // stop and begin observing again after 100ms. because otherwise IE enters to infinite loop
    Event.stopObserving(window, 'resize', setGridHeight);
    window.setTimeout("Event.observe(window, 'resize', setGridHeight);", 100);
    
    if(window.opera)
    {
        // in Opera 9.5 grid height does not applies if resize window only by height
        // this fixes problem
        setTimeout(delayedSetHeight.bindAsEventListener(null, sGridFrameId, iNewGridHeight), 0);
    }
    else
    {
        cGrid.setStyle({height : iNewGridHeight + "px"});
    }    
}

function setShoppingCartGridHeight() {
    if (!bPageLoaded) {
        Event.observe(window, 'load', setHeightToShoppingCartGrid);
    }
    else {
        setHeightToShoppingCartGrid();
    }
}
// sShoppingCartGridFrameId - declared in ShoppingCartGridPart.cs
function setHeightToShoppingCartGrid() {
    var fGrid;
    var cGrid;

    //debugger;
    for (var iGridFrame = 0; iGridFrame < frames.length; iGridFrame++) {
        if (frames[iGridFrame].frameElement.id != sShoppingCartGridFrameId) 
            continue;

        fGrid = frames[iGridFrame].window;
        cGrid = $(frames[iGridFrame].frameElement);
    }
    // set grid height to 100, for valid page empty space calculation    
    cGrid.setStyle({ height: "100px" });

    var iFooterPosY = Prototype.Position.cumulativeOffset($('tFooter'))[1];
    var iGridPosY = Prototype.Position.cumulativeOffset(cGrid)[1];
    var iNewGridHeight = iFooterPosY - iGridPosY - $('tFooter').getHeight() - 11;
    var iNewFooterStuffHeight = 0;

    if (fGrid != null && fGrid.getShoppingCartGridHeight) {
        var RGGHeight = fGrid.getShoppingCartGridHeight();

        iNewGridHeight = typeof (RGGHeight) == "undefined" ? 80 : fGrid.getShoppingCartGridHeight() + 25;

        //if ProductGrid visible
        if (!IsGridVisible()) {
            iProductGridFooterPosY = Prototype.Position.cumulativeOffset($(GetProductGridFooterID()))[1];
            iEmptySpace = iFooterPosY - iProductGridFooterPosY - $(GetProductGridFooterID()).getHeight();

            if (iEmptySpace > $(sShoppingCartFooterStuff).getHeight())
                iNewFooterStuffHeight = iEmptySpace;
            else
                iNewFooterStuffHeight = $(sShoppingCartFooterStuff).getHeight();
        }
    }

    // stop and begin observing again after 100ms. because otherwise IE enters to infinite loop
    Event.stopObserving(window, 'resize', setShoppingCartGridHeight);
    window.setTimeout("Event.observe(window, 'resize', setShoppingCartGridHeight);", 100);

    if (window.opera) {
        // in Opera 9.5 grid height does not applies if resize window only by height
        // this fixes problem
        setTimeout(delayedSetHeight.bindAsEventListener(null, sShoppingCartGridFrameId, iNewGridHeight), 0);
    }
    else {
        if (iNewFooterStuffHeight >= 0)
            $(sShoppingCartFooterStuff).setStyle({ height: iNewFooterStuffHeight + "px" });
        cGrid.setStyle({ height: iNewGridHeight + "px" });
    }
}

function delayedSetHeight(something, sGridFrameId, iFrameHeight)
{
    $(sGridFrameId).setStyle({height : iFrameHeight + "px"});
}

function showGrid(something, sGridFrameId)
{
    var cGrid = $(sGridFrameId);
    if(cGrid != null)
    {
        cGrid.setStyle({visibility : "visible"});
    }
}

//function onKeyDown(p_oEvent, p_sAccountID, p_sLoginID, p_sPasswordID, p_sLoginButtonID)
//{
////    if(p_oEvent.keyCode == 13)
//    {
//        $(p_sLoginButtonID).click();
//    }
//}

function calcContentSize(p_oEvent) {
    var lblZonesPlaceholder = $('lblZonesPlaceholder');
    if (lblZonesPlaceholder == null || lblZonesPlaceholder.childNodes == null || !lblZonesPlaceholder.childNodes.length) {
        window.setTimeout("calcContentSize();", 100);
        return;
    }
    
    var tblContent = lblZonesPlaceholder.childNodes[0];
    if(!tblContent || tblContent.className != 'rounded')
        return;
        
    tblContent = $(tblContent);
    var oContentPosition = tblContent.cumulativeOffset();
    var iContentHeight = tblContent.getHeight();
    var tblFooter = $('tFooter');
    var oFooterPosition = tblFooter.cumulativeOffset();
    var iEmptySpace = oFooterPosition.top - oContentPosition.top - 10;
    if(iContentHeight < iEmptySpace)
        tblContent.style.height = iEmptySpace + "px";
}

function updateRoundedButtons()
{
    var aButtonLinks = $$('a.btnlnk');
    if(aButtonLinks && aButtonLinks.length)
    {
        var tblButton;
        for(var iButton = 0; iButton < aButtonLinks.length; iButton++)
        {
            tblButton = findButtonTable(aButtonLinks[iButton]);
            if(tblButton)
            {
                tblButton = $(tblButton);
                tblButton.style.cursor = "pointer";
                Event.observe(tblButton ,'click', onRebtnClick.bindAsEventListener(null, aButtonLinks[iButton]));
            }
        }
    }
}

function findButtonTable(p_oButtonLink)
{
    if(!p_oButtonLink ||
       !p_oButtonLink.tagName ||
       p_oButtonLink.tagName.toLowerCase() != "a" ||
       !p_oButtonLink.parentNode || 
       !p_oButtonLink.parentNode.tagName ||
       p_oButtonLink.parentNode.tagName.toLowerCase() != "td")
        return null;
        
    var tblButton = p_oButtonLink.parentNode;
    while(tblButton && tblButton.tagName && tblButton.tagName.toLowerCase() != "table")
    {
        tblButton = tblButton.parentNode
    }
    if(!tblButton || !tblButton.tagName || tblButton.tagName.toLowerCase() != "table")
        return null;
    return tblButton;
}

function onRebtnClick(p_oEvent, p_aButton)
{
    window.location.href = p_aButton.href;
}