
function tabBtnClick(object)
{
    var tTabBtnName;
    var tTabAreaName;
    var tTabBtn;
    var tTabArea;
    
    var index = 1;
    while (1 == 1)
    {
        tTabBtnName = 'tab' + index;
        tTabBtn = document.getElementById(tTabBtnName);
        tTabAreaName = 'tab' + index + '_page';
        tTabArea = document.getElementById(tTabAreaName);
        
        if (tTabBtn==null || tTabArea==null)
            break;

        tTabBtn.className = 'FlexTabBtn';
        tTabArea.style.display = 'none';
        index++;
    }
    
    tTabBtnName = object.id;
    tTabBtn = document.getElementById(tTabBtnName);
    if (tTabBtn != null)
        tTabBtn.className = 'FlexTabBtnOn';

    tTabAreaName = object.id + '_page';
    tTabArea = document.getElementById(tTabAreaName);
    if (tTabArea != null)
        tTabArea.style.display = 'block';
    
}


//function contentEditorBannerChange(iNewBannerFilename)
//{
//    var t = document.getElementById('BannerImage');
//    if (!t)
//    {
//        alert('element missing');
//        return;
//    }
//    
//    var b = document.getElementById('fldBanner');
//    //alert (b.value);
//    
//    t.src = b.value;
//        
//    //alert(t.innerHTML);
//}

function ShowPopup(iPopupID)
{
    var blur = document.getElementById('Blur');

    var tPopup = document.getElementById(iPopupID);
    if (!tPopup)
    {
        alert('Popup form ' + iPopupIDd + ' not found.');
        return;
    }
    
    if (tPopup.style.display == 'block')
    {
        tPopup.style.display = 'none';
        blur.style.display = 'none'
        return;
    }
    
    tPopup.style.display = 'block'; 
    blur.style.display = 'block';
    
//    alert(document.documentElement.scrollTop);
//    alert(document.documentElement.clientHeight);
//    alert(tPopup.offsetHeight);
    
    var tPositionX = (document.body.clientWidth - tPopup.offsetWidth)/2
    //var tPositionY = ((document.body.clientHeight - tPopup.offsetHeight)/2) + document.documentElement.scrollTop - tPopup.offsetHeight;
    var tPositionY = ((document.documentElement.clientHeight - tPopup.offsetHeight)/2) + document.documentElement.scrollTop;
    
    tPopup.style.left = tPositionX + "px";
    tPopup.style.top = tPositionY + "px";
    
    
//    if (navigator.userAgent.indexOf("Firefox")==-1)
//        event.cancelBubble=true; 

    
    if (iPopupID.indexOf("_HelpPopup") > 0)
    {
        var tUrl = 'helper.aspx?helper=StaticText&KeyName=' + iPopupID;
        loadData(tUrl, 'PopupContent');
    }
    
}

function PopupFormDelete(iPopupID)
{
    var tFormName = iPopupID + '_form';
    var tForm = document.getElementById(tFormName);
    if (!tForm)
    {
        alert('Form ' + tFormName + ' not available');
        return;
    }

    tForm.DeleteFlag.value = 1;

    tForm.submit();
    ShowPopup(iPopupID);
    
    if (navigator.userAgent.indexOf("Firefox")==-1)
        event.cancelBubble=true;   
}



var xmlhttp;
var resultWindow;

function loadData(url, iResultWindow)
{
    resultWindow = iResultWindow;
    
    var rWindow = document.getElementById(resultWindow);
    if (!rWindow)
    {
        alert('Result window does not exist!');
        return;
    }
    
    rWindow.innerHTML = 'EMPTY'
    
    tUrl = url + '&' + (new Date()).getTime();
   
    xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    if (xmlhttp != null)
    {
        xmlhttp.onreadystatechange = state_change;
        xmlhttp.open("GET", tUrl, true);
        xmlhttp.send();
    }
    else
        alert("Browser needs to be updated");
}

function state_change()
{
    if (xmlhttp.readyState == 4)
    {
        if (xmlhttp.status == 200)
        {
            var r = document.getElementById(resultWindow);
            if (r)
                r.innerHTML  = xmlhttp.responseText;
            else
                alert('Error populating result area');
           
        }
        else
            alert('Problem loading data');
    }
}

function ShowPopupForm(iPopupFormId, iFormRequest)
{
    ShowPopup(iPopupFormId);
    
    var tFormRequest = 'helper.aspx?FormObject=' + iFormRequest;
    loadData(tFormRequest, 'PopupContent');
}

function PopupFormSubmit(iPopupID)
{
    var tFormName = iPopupID + '_form';
    var tForm = document.getElementById(tFormName);
    if (!tForm)
    {
        alert('Form ' + tFormName + ' not available');
        return;
    }
    
    var tTextEditor = document.getElementById('TextEditor');
    if (tTextEditor != null)
    {
        var tTextEditorField = document.getElementById('TextEditorField');
        if (!tTextEditorField)
            alert('TextEditor found but TextEditor form field is missing');
        else
            tTextEditorField.value = tTextEditor.innerHTML;
    }
    
//    alert('saving');
//    return;

    tForm.submit();
    //ShowPopup(iPopupID);
    
    if (navigator.userAgent.indexOf("Firefox")==-1)
        event.cancelBubble=true;  
}

function PopupFormDelete(iPopupID)
{
    var tFormName = iPopupID + '_form';
    var tForm = document.getElementById(tFormName);
    if (!tForm)
    {
        alert('Form ' + tFormName + ' not available');
        return;
    }

    tForm.DeleteFlag.value = 1;

    tForm.submit();
    ShowPopup(iPopupID);
    
    if (navigator.userAgent.indexOf("Firefox")==-1)
        event.cancelBubble=true;   
}

function FlexsFormDelete(state)
{
    var ctrlMain = document.getElementById('FlexsFormControl');
    var ctrlBtnArea = document.getElementById('FlexsFormButtonArea');
    var ctrlDelete = document.getElementById('FlexsFormDeleteArea');
    
    if (state == null)
    {
        ctrlMain.style.backgroundColor = "#cccccc";
        ctrlBtnArea.style.display = 'none';
        ctrlDelete.style.display = 'block';
    }
    else if (state == false)
    {
        ctrlMain.style.backgroundColor = "#ffffff";
        ctrlBtnArea.style.display = 'block';
        ctrlDelete.style.display = 'none';
    }
    else
    {
        var tFormName = 'FlexsFormControl';
        var tForm = document.getElementById(tFormName);
        if (!tForm)
        {
            alert('Form ' + tFormName + ' not available');
            return;
        }

        tForm.DeleteFlag.value = 1;
        tForm.submit();
    }    
}

function SaveBlockEditor()
{
    //alert(window.frames['iFrameName'].pwCurHtmlBox.innerText);
    
    //var htmlContent = window.frames['iFrameName'].pwGetEditorText();
    
//    var x = window.frames['iFrameName'].document.getElementById('ASPxHtmlEditor1');
//    if (x)
//        alert('control is ok');
//    
//    x.setHTML('hello');

var htmlContent = window.frames['iFrameName'].textEditor.GetHtml();
//alert(htmlContent);
    
    //var t = window.frames['iFrameName'].document.getElementById('ASPxHtmlEditor1').GetHtml();
    //var t = window.frames['iFrameName'].document.getElementById('ASPxHtmlEditor1').value;
    //alert(t);
    //return;
    
    
    //alert(htmlContent);
    
    var tFormName = 'BlockEditor';
    var tForm = document.getElementById(tFormName);
    if (!tForm)
    {
        alert('Block editor form is not available');
        return;
    }

    //tForm.TextArea.value = htmlContent;
    tForm.TextArea.value = htmlContent;
    tForm.submit();

}

function iFrameHasLoaded()
{
    var t = document.getElementById('LoadingArea');
    if (t)
    {
        t.style.display = 'none';
    }
    
}

function parentClick()
{
    window.parent.location.href='http://www.bbc.co.uk';

    alert('parentclick');
}