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/cmv_editor.asp?Site='+curBase+'&Page='+curPage;
var EditorURL = curSite+'/App_Editor/editor/cmvdialog/cmv_editor.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 (!window.event && edtr) {curEditor=edtr; return;}
    if (window.event && window.event.srcElement && window.event.ctrlKey != true && window.event.altKey != true) {curEditor=edtr; return;}
    if (GetUrlParam('Edtr').toString().length>0) 
    {
	    var oEditor = FCKeditorAPI.GetInstance('ctl00_edtr_'+GetUrlParam('Edtr').toString());
	    if (oEditor.IsDirty()==true) {if (!window.confirm('Are you sure you wish to cancel editing on this page? Any changes will be lost.')) {return;}}
    }
    nPage='0';
    curPage=document.location.toString().split('?')[0];
    if (edtr) {if (curEditor==edtr) {curEditor='';} else {curEditor=edtr;}}
    else
    {
        if (GetUrlParam('Edtr').toString().length>0) {curEditor='';}
        else 
        {
            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=='' || curEditor=='') {return;} //{window.alert('Please select which area to edit by clicking on the desired area.'); return;}
            if (nPage=='1') 
                {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;
        }
    }
    document.location=curPage+'?Edtr='+curEditor;
}
function savEditor(edtr)
{
    curEditor = GetUrlParam('Edtr');
	var oEditor = FCKeditorAPI.GetInstance('ctl00_edtr_'+curEditor);
	var oEditorDoc = oEditor.EditorDocument;
    GetElement('file_Editor').value=GetElement('ctl00_file_'+curEditor).value;
    if (GetElement('ctl00_beg_'+curEditor)) {GetElement('beg_Editor').value=GetElement('ctl00_beg_'+curEditor).value;}
    if (GetElement('ctl00_end_'+curEditor)) {GetElement('end_Editor').value=GetElement('ctl00_end_'+curEditor).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;}
    if (!curEditor) {return;}
    if (curEditor.toString().toLowerCase() != 'content') {window.alert('You can only create new pages in the main content area.'); return false;}
    if (!GetElement('ctl00_file_Content')) {return;}
    var oEditor = FCKeditorAPI.GetInstance('ctl00_edtr_'+GetUrlParam('Edtr').toString());
    if (oEditor.IsDirty()==true) {if (!window.confirm('Are you sure you wish to cancel editing on this page? Any changes will be lost.')) {return;}}
    if (GetElement('ctl00_file_Content')) {GetElement('ctl00_file_Content').value=curSite+'/App_Content/'+pgNm;}
    if (GetElement('ctl00_beg_Content')) {GetElement('ctl00_beg_Content').value='';}
    if (GetElement('ctl00_end_Content')) {GetElement('ctl00_end_Content').value='';}
    var oEditorAPI = FCKeditorAPI.GetInstance('ctl00_edtr_Content');
    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 ) ;
}
