﻿var nPage = '0';
var curEditor = GetUrlParam('Edtr');
var curBase = window.location.toString().substr(0,window.location.toString().toLowerCase().indexOf('app_')-1).replace('http://','').replace('https://','');
if (curBase.indexOf('/')>0) {curBase='/'+curBase.split('/')[1];} else {curBase='/';}
var curSite = window.location.toString().substr(0,window.location.toString().toLowerCase().indexOf('app_')-1).replace('http://','').replace('https://','');
if (curSite.indexOf('/')>0) {curSite='/'+curSite.split('/')[1];} else {curSite='';}
var curPage = window.location.toString().split('?')[0].split('/')[window.location.toString().split('?')[0].split('/').length-1];
var SelectorURL = curSite+'/App_Editor/editor/cmvdialog/myeditor.asp?Site='+curBase+'&Page='+curPage;
var EditorURL = curSite+'/App_Editor/editor/cmvdialog/myeditor.aspx?Edtr=Content';
var LinkBrowserURL = curSite+'/App_Editor/editor/cmvdialog/filebrowser/browser.html?Connector=connectors/connector.asp&ServerPath='+curSite+'/app_content/&Type=';
var FileBrowserURL = curSite+'/App_Editor/editor/cmvdialog/filebrowser/browser.html?Connector=connectors/connector.asp&ServerPath='+curSite+'/app_content/file/&Type=File';
var ImageBrowserURL = curSite+'/App_Editor/editor/cmvdialog/filebrowser/browser.html?Connector=connectors/connector.asp&ServerPath='+curSite+'/app_content/&Type=Image';
var FileUploadURL = curSite+'/App_Editor/editor/cmvdialog/upload/browser.html?Connector=connectors/connector.asp&ServerPath='+curSite+'/app_content/&Type=File';
var ImageUploadURL = curSite+'/App_Editor/editor/cmvdialog/upload/browser.html?Connector=connectors/connector.asp&ServerPath='+curSite+'/app_content/&Type=Image';
var PromptURL = curSite+'/App_Editor/editor/cmvdialog/prompt/frmprompt.html';
function logOff() {document.location=document.location.toString().split('?')[0]+'?Auth=LogOff';}
function togEditor(edtr)
{
    if (!edtr) {edtr='';}
    if (edtr!='' && window.event.ctrlKey!=true && window.event.altKey!=true) {return;}
    if (edtr==GetUrlParam('Edtr').toString() && GetUrlParam('Edtr').toString().length>0) {return;}
    if (edtr!=GetUrlParam('Edtr').toString() && GetUrlParam('Edtr').toString().length>0) 
    {
        var oEditor = FCKeditorAPI.GetInstance('MyEditor');
        if (oEditor.IsDirty()==true) {if (!window.confirm('Are you sure you wish to cancel editing on this page? Any changes will be lost.')) {return;}}
    }
    //when clicking a section to (re-)activate editor (load new editor)
    if (edtr.toString().length>0) {document.location=document.location.toString().split('?')[0]+'?Edtr='+edtr; return;}
    //when clicking Page Editor button when editor is not active (select editor)
    if (edtr.toString().length==0 && GetUrlParam('Edtr').toString().length==0) 
    {
        nPage='0';
        dlgRtn=window.showModalDialog(SelectorURL,'Editor','dialogHeight:200px;dialogWidth:400px;center:yes;resizable:yes;scroll:no;status:no;location:no');
        if (!dlgRtn || dlgRtn==null || dlgRtn=='') {return;}
        curPage=dlgRtn.split('|')[0];
        curEditor=dlgRtn.split('|')[1];
        nPage=dlgRtn.split('|')[2];
        if (curPage=='') {return;} 
        if (curEditor=='') {curEditor='Content';}
        if (nPage=='1') 
            window.open(EditorURL+'&Page='+curSite+'/App_Content/'+curPage);
            //{if (curPage.substr(0,4).toLowerCase()!='frm_' && curPage.split(".")[curPage.split(".").length-1].toLowerCase()=="aspx") {window.open(curSite+'/App_Content/'+curPage+'?Edtr='+curEditor);} else {window.open(EditorURL+'&Page='+curSite+'/App_Content/'+curPage);}} 
        else {document.location=curSite+'/App_Content/'+curPage+'?Edtr='+curEditor;}
        return;
    }
    //when clicking Page Editor button when editor is active (unload editor)
    if (edtr.toString().length==0 && GetUrlParam('Edtr').toString().length>0) {document.location=document.location.toString().split('?')[0]; return;}
}
function loadEditor(edtr)
{
    if (GetElement('auth_Editor').value.length==0) {document.location='../App_Editor/login.aspx?Edtr='+edtr+'&Rtn='+document.location.toString().split('?')[0]; return;}
    if (GetElement('auth_Editor').value=='Admin') {if (!edtr || edtr=='') {edtr='Editor'; tbr='AdminNoPage';} else {tbr='Admin';}} else {tbr='Utilities';}
    var div = GetElement('ctl00_pan_'+edtr);
    var fck = new FCKeditor('MyEditor');
    fck.BasePath = '../App_Editor/';
    fck.Config['CustomConfigurationsPath'] = '../myconfig.js';
    fck.Config['EditorAreaCSS'] = '../../App_Themes/Content/content.css';
    fck.ToolbarSet = tbr;
    if (tbr=='Utilities' || tbr.indexOf('NoPage')>0)
    {
        fck.Value = 'This is a placeholder';
        fck.Height=0;
        fck.Width=0;
    }
    else
    {
        fck.Value = GetElement('ctl00_EdtrCont').value;
        fck.Height=div.offsetHeight-110 || 250;
        fck.Width=div.offsetWidth-10 || 750;
    }
    div.innerHTML = fck.CreateHtml();
}
function savEditor(edtr)
{
    if (GetUrlParam('Edtr').length==0 || GetElement('file_Editor').value.length==0) {return;}
    var oEditor = FCKeditorAPI.GetInstance('MyEditor');
    var oEditorDoc = oEditor.EditorDocument;
    GetElement('beg_Editor').value=GetElement('ctl00_EdtrBeg').value;
    GetElement('end_Editor').value=GetElement('ctl00_EdtrEnd').value;
    GetElement('cont_Editor').value=oEditor.GetXHTML(false);
    GetElement('edtrForm').submit();
}
var addPage=0;
function newPage(pgNm) 
{
    if (addPage>0) {return;}
    if (!pgNm) {return;}
    if (pgNm.length==0) {return;}
    window.alert(pgNm);
    if (!curEditor) {return;}
    if (curEditor.toString().toLowerCase() != 'content') {window.alert('You can only create new pages in the main content area.'); return false;}
    var oEditor = FCKeditorAPI.GetInstance('MyEditor');
    if (oEditor.IsDirty()==true) {if (!window.confirm('Are you sure you wish to cancel editing on this page? Any changes will be lost.')) {return;}}
    GetElement('file_Editor').value=curSite+'/App_Content/'+pgNm;
    GetElement('ctl00_EdtrBeg').value='';
    GetElement('ctl00_EdtrEnd').value='';
    var oEditorAPI = FCKeditorAPI.GetInstance('MyEditor');
    oEditorAPI.Commands.GetCommand('NewPage').Execute();
    addPage=1; 
}
function GetUrlParam(paramName)
{
	var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' );
	var oMatch = oRegex.exec( window.top.location.search );
	if (oMatch && oMatch.length > 1) {return decodeURIComponent(oMatch[1]);} else {return '';}
}
function OpenFileBrowser( url, width, height )
{
    var iLeft = ( oEditor.FCKConfig.ScreenWidth  - width ) / 2 ;
    var iTop  = ( oEditor.FCKConfig.ScreenHeight - height ) / 2 ;
    var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes" ;
    sOptions += ",width=" + width ;
    sOptions += ",height=" + height ;
    sOptions += ",left=" + iLeft ;
    sOptions += ",top=" + iTop ;
    if ( oEditor.FCKConfig.PreserveSessionOnFileBrowser && oEditor.FCKBrowserInfo.IsIE )
    {
        var oWindow = oEditor.window.open( url, 'FCKBrowseWindow', sOptions ) ;
        if ( oWindow )
        {
	        try
	        {
		        var sTest = oWindow.name ; 
		        oWindow.opener = window ;
	        }
	        catch(e)
	        {
		        alert( oEditor.FCKLang.BrowseServerBlocked ) ;
	        }
        }
        else
	        alert( oEditor.FCKLang.BrowseServerBlocked ) ;
    }
    else
        window.open( url, 'FCKBrowseWindow', sOptions ) ;
}
