//HyperLink Dialog
function res_RunHyperLinkDialog(wsroot, title, href, target, isCreateMode) {
        var path= wsroot + "~/AGP.Net/Resources/HyperLinkDialog/HyperLinkDialog.aspx";
        var hyperlink= res_InitHyperLinkDialog();
        hyperlink.Title= title;
        hyperlink.Href= href;
        hyperlink.Target= target;
        hyperlink.IsCreateMode= isCreateMode;
        var res=  window.showModalDialog(path, hyperlink, 'dialogHeight:200px;dialogWidth:420px;scroll:no;resizable:no;status:no;help:no;');
        if (res) return hyperlink;
        return null;
}

function res_InitHyperLinkDialog() {
        var hyperlink= new Object();
        hyperlink.IsCreateMode= false;
        hyperlink.ClearLink= false;
        hyperlink.Href= "";
        hyperlink.Target;
        hyperlink.Name;
        hyperlink.Title;
        return hyperlink;
}

// Picture Dialog

function res_RunPictureDialog(wsroot, BasePath, ReplaceLeadingWorkspaceRoot) {
        //alert(ReplaceLeadingWorkspaceRoot);
        var path= wsroot + "~/ASP.Net/Resources/PictureDialog/PictureDialog1.aspx";
        var Additional= "";
        if(ReplaceLeadingWorkspaceRoot) Additional+="&ReplaceLeadingWorkspaceRoot=1"
        var picture= res_InitPicture();
        var res= window.showModalDialog(path + "?BasePath=" + BasePath + Additional, picture, 'dialogHeight:500px;dialogWidth:430px;scroll:no;resizable:no;status:no;help:no;');
        if (res) return picture;
        return null;
}

//function res_RunPictureDialog(wsroot, BasePath) {
//        var path= wsroot + "~/AGP.Net/Resources/PictureDialog/PictureDialog1.aspx";
//        var picture= res_InitPicture();
//        var res= window.showModalDialog(path + "?BasePath=" + BasePath, picture, 'dialogHeight:500px;dialogWidth:430px;scroll:no;resizable:no;status:no;help:no;');
//        if (res) return picture;
//        return null;
//}

function res_RunUserPictureDialog(wsroot) {
        return res_RunPictureDialog(wsroot, wsroot + "UserImages");
}

function res_InitPicture() {
        var picture= new Object();
        picture.Title= "";
        picture.Src= "";
        picture.Width= "100";
        picture.Height= "40";
        return picture;
}

function res_GetPictureTag(wsroot, BasePath) {
        var picture= res_RunPictureDialog(wsroot, BasePath);
        if (picture != null) {
                        return "<img src='"+picture.Src+"' width='"+picture.Width+"' height='"+picture.Height+"'>";
                }
        return null;
}

function res_GetPictureSrc(wsroot, BasePath) {
        var picture = res_RunPictureDialog(wsroot, BasePath);
        if (picture != null) return picture.Src;
        return null;
}

// Font Dialog
function res_RunFontDialog(wsroot, font) {
        var path= wsroot + "~/ASP.NET/Resources/SelectFont/SelectFont.aspx";
        if (font == null) font = res_InitFont();
        if (window.showModalDialog( path, font, "dialogWidth:380px;dialogHeight:350px;help:no;status:no;resizable:no")) {
                var fontFamily = "font-family: " + font.fontName;
                var fontStyle = "font-style: " + ((font.fontStyle=="i" || font.fontStyle=="bi") ? "italic" : "normal");
                var fontWeight = "font-weight: " + ((font.fontStyle=="b" || font.fontStyle=="bi") ? "bold" : "normal");
                var fontSize = "font-size: " + font.fontSize + font.fontUnit;
                var fontDecoration= "text-decoration: " + (font.isUnderline ? "underline " : "") + (font.isOverline ? "overline " : "") + (font.isStrikeout ? "line-through" : "");
                var fontColor= "color: " +((font.foreColor=="" || font.foreColor==null) ? "#000000" : font.foreColor);
                var fontBackColor= "background-color : "+ ((font.backColor=="" || font.backColor==null) ? "transparent" : font.backColor);
                return fontFamily+";"+fontStyle+";"+fontWeight+";"+fontSize+";"+fontDecoration+";"+fontColor+";"+fontBackColor;
        }
        else return null;
}

function res_InitFont() {
        var font = new Function();
        font.fontName                = "Tahoma";
        font.fontStyle                = "r";
        font.fontSize                = "8";
        font.fontUnit                = "pt";
        font.isOverline                = false;
        font.isUnderline        = false;
        font.isStrikeout        = false;
        font.foreColor                = "#000000";
        font.backColor                = "transparent";
        return font;
}

// Color Dialog
function res_RunColorDialog(wsroot, InitialColor) {
        var path= wsroot + "~ASP.NET/Resources/SelectColor/SelectColor.aspx";
        var args = new Function();
        args.color = InitialColor;
        var colorValue = window.showModalDialog(path, args, "dialogHeight:400px;dialogWidth:600px;status:no;help:no;scroll:no;");
        if  (colorValue!=undefined) return colorValue; else return null;
}

// Palette Class Chooser
function res_RunPaletteClassChooser(wsroot, Initial) {
        var path= wsroot + "~ASP/NET/Resources/PaletteClassChooser/main.aspx";
        var args= new Object();
        args.InputClass=Initial;
        window.showModalDialog(path,args,        "dialogWidth:500px;dialogHeight:350px;scroll:no;resizable:no;status:no;help:no;");
        if (args.OutputClass!=undefined) return args.OutputClass; else return null;
}

// Html Editor
function res_RunHtmlEditor(wsroot, baseurl, htmlstr, url, ImagePath, edit_mode) {
        var path= wsroot + "~/ASP.NET/Resources/HtmlEditor/HtmlEditor.aspx";
        var width= 800;
        var height= 500;
        return res_InnerRunHtmlEditor(wsroot, path, baseurl, htmlstr, url, null, ImagePath, width, height, edit_mode);
        //return res_RunHtmlEditor2(url, htmlstr);
}

function res_RunHtmlEditor4WebPart(wsroot, baseurl, wpID, ImagePath) {
        var path= wsroot + "~/ASP.NET/Resources/HtmlEditor/HtmlEditor.aspx";
        var width= 800;
        var height= 500;
        return res_InnerRunHtmlEditor(wsroot, path, baseurl, null, null, wpID, ImagePath, width, height);
}

function res_RunUserHtmlEditor(wsroot, baseurl, htmlstr, url, edit_mode) {
        return res_RunHtmlEditor(wsroot, baseurl, htmlstr, url, wsroot + "UserImages", edit_mode);
        //return res_RunHtmlEditor2(url, htmlstr);
}

function res_InnerRunHtmlEditor(wsroot, path, baseurl, htmlstr, url, wpID, ImagePath, width, height, edit_mode) {
        if (url == null) url= ""; // TEMP
        var args= new Object();
        args.wpID= wpID;
        args.url= url;
        args.htmlstr= htmlstr;
        args.baseurl= baseurl;
        args.WorkspaceRoot= wsroot;
        args.ImagePath= ImagePath;
        args.EditMode= edit_mode;
        if (window.showModalDialog(path, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;scroll:no;resizable:no;status:no;help:no;")) {
                return args.htmlstr;
        }
        return null;
}
// Css Editor
function res_RunCssEditor(wsroot, ImagePath, cssStr) {
        var path= wsroot + "~/ASP.NET/Resources/CssEditor/startTree.aspx";
        var width= 800;
        var height= 550;
        return res_InnerRunCssEditor(path, ImagePath, cssStr, width, height);
}

function res_RunUserCssEditor(wsroot, cssStr) {
        return res_RunCssEditor(wsroot, wsroot + "UserImages", cssStr);
}

function res_InnerRunCssEditor(path, ImagePath, cssStr, width, height) {
        var args= new Object();
        args.ImagePath= ImagePath;
        cssStr= res_String2CssEditor(cssStr);
        var cssText= showModalDialog(path + "?cssText=" + cssStr, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;status:no;help:no;scroll:no;");
        if (cssText == undefined) return null;
        return cssText;
}

function res_String2CssEditor(str) {
        return str.replace(/#/gi, "%23");
}

function OpenPermissionManager(Value, Name, Source, SubSource, ShowRights) {
        if (ShowRights == null) ShowRights= true;
        var strQuery= "~/ASP.NET/Components/PermissionManager/PermissionManagerForm.aspx?ObjectValue=" + Value + "&Name=" + Name + "&Source=" + Source + "&SubSource=" + SubSource;
        if (typeof(WorkspaceUrl) != "undefined") strQuery= WorkspaceUrl + strQuery;
        if (ShowRights == false) strQuery+= "&ShowRights=0";
        return window.showModalDialog(strQuery, "", "font-family:Verdana; font-size:12; dialogHeight:445px;dialogWidth:600px;status:no;help:no;scroll:no;");
}

function res_PermissionManager2(id, type) {
        var url= WorkspaceRoot + "~/ASP.NET/Resources/PermissionManager2/main.aspx?id=" + id + "&type=" + type;
        return window.showModalDialog(url, "", "font-family:Verdana; font-size:12; dialogWidth:640px;dialogHeight:430px;status:no;help:no;scroll:no;");
}

function OpenPartStorageManager(WorkspaceRoot, wpID, IsEditMode) {
        var strControlUrl= WorkspaceRoot + "AGP.Net/Components/AdvPortalManager/PartStorageEditor/PartStorageControl.ascx";
        var query= "wpID=" + wpID;
        if (IsEditMode == true) query+= "&edit=1";
        var res= res_RunModalControl(WorkspaceRoot, strControlUrl, 750, 550, query, "Part Storage Editor");
        return (res == true);
}



// Web Page Explorer
function res_RunWebPageExplorer(wsroot, args) {

        var path= wsroot + "/ASP.Net/Resources/WebPageExplorer/WebPageExplorer.aspx";
        if (args == null) args= new Object();
        var res= res_ShowModalDialog(path, "", args, 500, 400);
        return res;
}

// Content CMS Folder Explorer
function res_RunDocumentExplorer(wsroot, args) {
        //var path=wsroot+ "ASP.Net/Resources/CMSExplorer/CMSExplorer.aspx";
        var path=wsroot+ "ASP.Net/Components/AX.CMS.UI/OpenItemDialogLoader.aspx";
        if (args == null) args= new Object();
        var res= res_ShowModalDialog(path, "", args, 500, 400);
        if(res==null) return res;
        if(res.Type== 'fldr') return wsroot+ "Document_Console.aspx?ID=" + res.ID
        if(res.Type== 'doc' ) return wsroot+ "ASP.Net/Components/AX.CMS.DocumentViewer/Download.aspx?ID=" + res.ID
        
        return res;
}

// Content CMS Document Explorer
//function res_RunDocumentExplorer(wsroot, aux_query) {
//        var path= "/ASP.Net/Resources/FTB.HtmlEditor/Resources_Default.ascx";
//        var res= res_RunModalControl(wsroot, path, 500, 500, aux_query, null);
//        if (res == undefined) res= null;
//        return res;
//}

function res_ShowModalDialog(url, query, params, width, height, features) {
//"status:no;help:no;resizable:" + isResizable + ";scroll:no;dialogHeight:" + height + "px;dialogWidth:" + width + "px";
        if(features == null) features= "";
        if(features.search("dialogWidth") < 0) features= "dialogWidth:" + width + "px;" + features;
        if(features.search("dialogHeight") < 0) features= "dialogHeight:" + height + "px;" + features;
        if(features.search("scroll") < 0) features= "scroll:no;"  + features;
        if(features.search("help") < 0) features= "help:no;"  + features;
        if(features.search("resizable") < 0) features= "resizable:no;"  + features;
        if(features.search("status") < 0) features= "status:no;"  + features;
        return window.showModalDialog(url + "?" + query, params, features);
}

function res_ShowModelessDialog(url, query, params, width, height, features) {
//"status:no;help:no;resizable:" + isResizable + ";scroll:no;dialogHeight:" + height + "px;dialogWidth:" + width + "px";
        if(features == null) features= "";
        if(features.search("dialogWidth") < 0) features= "dialogWidth:" + width + "px;" + features;
        if(features.search("dialogHeight") < 0) features= "dialogHeight:" + height + "px;" + features;
        if(features.search("scroll") < 0) features= "scroll:yes;"  + features;
        if(features.search("help") < 0) features= "help:no;"  + features;
        if(features.search("resizable") < 0) features= "resizable:yes;"  + features;
        if(features.search("status") < 0) features= "status:yes;"  + features;
        return window.open(url + "?" + query, params, features);
}

// analog of format function
function Format() {
        var len= arguments.length;
        if (len < 1) return null;
        var format= arguments[0];
        var re= new RegExp("(\{(.+?)\})", "ig"); // ? - non-gready matching
        var arr;
        var result= "";
        var index, si, ei;
        ei= 0;
        while ((arr= re.exec(format)) != null) {
                si= arr.index;
                result+= format.substring(ei, si);
                ei= arr.lastIndex;
                index= parseInt(RegExp.$2);
                if (index + 1 < len) result+= arguments[index + 1];
        }
        result+= format.substr(ei);
        return result;
}

// Modal WebPart Runner
function res_RunChildWebPart(wsroot, wpName, wpPath, wpClass, wpParentID, width, height, aux_query, features) {
        if (wsroot == null) wsroot= "";
        if (wpClass == null) wpClass= "";
        var url= wsroot + "ModalPageRunner.aspx";
        var Title= "Web Snippet";
        var Mode= 1; // modal webpart
        var query= Format("wpMode={0}&wpName={1}&wpPath={2}&wpClass={3}&wpParentID={4}&Title={5}&{6}", Mode, wpName, wpPath, wpClass, wpParentID, Title, aux_query);
        return res_ShowModalDialog(url, query, null, width, height, features);
}

function res_RunModalWebPart(wsroot, wpName, wpPath, wpClass, width, height, aux_query, features) {
        return res_RunChildWebPart(wsroot, wpName, wpPath, wpClass, "", width, height, aux_query, features);
}

function res_RunModalControl(wsroot, CtlPath, width, height, aux_query, Title, params) {
        var url= wsroot + "ModalPageRunner.aspx";
        if (Title == null) Title= "Modal Control";
        var Mode= 0; // modal control
        var query= Format("wpMode={0}&CtlPath={1}&Title={2}&{3}", Mode, CtlPath, Title, aux_query);
//        window.open(url + "?" + query);
        return res_ShowModalDialog(url, query, params, width, height);
}

function res_RunModelessControl(wsroot, CtlPath, width, height, aux_query, Title, params) {
        var url= wsroot + "ModalPageRunner.aspx";
        if (Title == null) Title= "Modal Control";
        var Mode= 0; // modal control
        var query= Format("wpMode={0}&CtlPath={1}&Title={2}&{3}", Mode, CtlPath, Title, aux_query);
//        window.open(url + "?" + query);
        return res_ShowModelessDialog(url, query, params, width, height);
}

function res_RunControl(wsroot, CtlPath, width, height, aux_query, Title, params) {
        var url= wsroot + "ModalPageRunner.aspx";
        if (Title == null) Title= "Modal Control";
        var Mode= 0; // modal control
        var query= Format("wpMode={0}&CtlPath={1}&Title={2}&{3}", Mode, CtlPath, Title, aux_query);
        window.open(url + "?" + query);
}


// Calendar
function res_CalendarArguments() {
        var dtDate;
        var strType;
}

function res_CalendarArgumentsAdv() {
        var iDATE_FORMAT;
        var strDATE_SEPARATOR;
        var iDATE_START_DAY;
        var strSHOW_WEEK_NUMBER;
}

function res_OpenCalendar(wsroot, date2Open) {
        var path= wsroot + "ASP.Net/Resources/Calendar/LoadCalendar.aspx";
        var objArgs= window.showModalDialog(path + "?dType=eventdate&dOpen=" + date2Open, null, "font-family:Verdana; font-size:12; dialogHeight:275px;dialogWidth:230px;status:no;help:no;scroll:no;");
        if(objArgs == null) return null;
        return objArgs.dtDate;
}

function res_OpenCalendarAdv(strFunction, date2Open, objArgs) {
        var ORG_DATE_FORMAT = 7;
        var ORG_DATE_SEPARATOR = '/';
        var ORG_DATE_START_DAY = 0;
        var ORG_SHOW_WEEK_NUMBER = "1";
        if(objArgs != null) {
                ORG_DATE_FORMAT = objArgs.iDATE_FORMAT;
                ORG_DATE_SEPARATOR = objArgs.strDATE_SEPARATOR;
                ORG_DATE_START_DAY = objArgs.iDATE_START_DAY;
                ORG_SHOW_WEEK_NUMBER = objArgs.strSHOW_WEEK_NUMBER;
        }
        InitCalendar( ORG_DATE_FORMAT, ORG_DATE_SEPARATOR, ORG_DATE_START_DAY );
        var _d = new Date();
        if(date2Open != null) _d.setTime(ParseDate(date2Open));
        LaunchCalendar(strFunction, _d);
}

function res_RunStyleEditor(wsroot, CssPath, FileName) {
        res_RunModalControl(wsroot, "AGP.Net/Resources/StyleEditor/Default.ascx", 800, 550, "CssPath="+CssPath+"&FileName="+FileName, "StyleEditor");
}

// HTMLEditor2 START
function res_RunHtmlEditorForUrl(ResourceUrl) {
        return res_RunHtmlEditor2(null, null, null, ResourceUrl);
}
function res_RunHtmlEditor4WebPart(wpID) {
        return res_RunHtmlEditor2(null, null, wpID, null);
}
// PageUrl - relative path to HtmlPage
// HtmlValue - HTML value
// wpID - Web Part ID for flushing web part cache
function res_RunHtmlEditor2(PageUrl, HtmlValue, wpID, ResourceUrl) {
        //var HtmlEditorUrl= WorkspaceRoot + "AGP.Net/Resources/HtmlEditorEx/HtmlEditor.aspx?m=1";
        var HtmlEditorUrl= WorkspaceRoot + "_HTMLEditor.aspx?isModalContext=1";
        var width= 810;
        var height= 610;
        var args= new Object();
        args.PageUrl= PageUrl;
        args.ResourceUrl= ResourceUrl;
        args.HtmlValue= HtmlValue;
        if (PageUrl != null) HtmlEditorUrl+= "&PageUrl=" + PageUrl;
        if (wpID != null) HtmlEditorUrl+= "&wpID=" + wpID;
        var NewHtmlValue= window.showModalDialog(HtmlEditorUrl, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;scroll:no;resizable:no;status:yes;help:no;");
        return NewHtmlValue;
}
// HTMLEditor2 END

function res_RunHtmlEditor(PageUrl, HtmlValue, wpID, ResourceUrl) {
        var HtmlEditorUrl= WorkspaceRoot + "ASP.Net/Resources/HtmlEditor2/HtmlEditor.aspx?m=1";
        //var HtmlEditorUrl= WorkspaceRoot + "_HTMLEditor.aspx?isModalContext=1";
        var width= 810;
        var height= 610;
        var args= new Object();
        args.PageUrl= PageUrl;
        args.ResourceUrl= ResourceUrl;
        args.HtmlValue= HtmlValue;
        if (PageUrl != null) HtmlEditorUrl+= "&PageUrl=" + PageUrl;
        if (wpID != null) HtmlEditorUrl+= "&wpID=" + wpID;
        var NewHtmlValue= window.showModalDialog(HtmlEditorUrl, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;scroll:no;resizable:no;status:yes;help:no;");
        return NewHtmlValue;
}


// HTMLViewer2 START
var HtmlEditorExPath= "AGP.Net/Resources/HtmlEditorEx/";
//function HtmlViewer2_ExternalEdit(ViewerControlID, PageUrl, wpID) {
//  var HtmlValue= res_RunHtmlEditor2(PageUrl, null, wpID);
//  if (HtmlValue != null) {
//          var ViewerControl= document.all[ViewerControlID];
//          try {
//                  ViewerControl.innerHTML= HtmlValue;
//          } catch (e) {alert('Error on update HTML view for web snippet. The Page will be refreshed.'); return true;}
//  }
//  return false;
//}
//function HtmlViewer2_InternalEdit(ViewerControlID, PageUrl, wpID) {
//        var EditorControl= document.all[ViewerControlID];
//        if (typeof(EditorControl.CanvasID) == "string") { EditorControl.Close();        return false;        }
//        var behaviorUrl= WorkspaceRoot + HtmlEditorExPath + "JS/DIVFlowCanvas2.htc";
//        HtmlViewer2_DownloadHTC(behaviorUrl);
//        window.setTimeout("HtmlViewer2_Activate('" + behaviorUrl + "', '" + ViewerControlID + "', '" + PageUrl + "', '" + wpID + "')", 300);
//        return false;
//}
function HtmlViewer2_Activate(behaviorUrl, VCID, PageUrl, wpID) {
        var VC= document.all[VCID];
        VC.PageUrl= PageUrl;
        VC.WebPartID= wpID;
        VC.WorkPath= WorkspaceRoot + HtmlEditorExPath;

        VC.style.behavior= "url(" + behaviorUrl + ")";
}
var HTC_frame= null;
function HtmlViewer2_DownloadHTC(behaviorUrl) {
        if (HTC_frame == null) {
                HTC_frame= document.createElement("IFRAME");
                HTC_frame.src= behaviorUrl;
        }
}
//function axp_OpenToolbar(WorkObjectID) {
//        var WorkObject= document.all(WorkObjectID);
//         window.showModelessDialog(WorkspaceRoot + HtmlEditorExPath + "ModalToolbar.aspx", WorkObject, 'status:no;help:no;resizable:yes;dialogWidth:430px;dialogHeight:90px');
//}
// HTMLViewer2 END

function res_OpenApproveWindow(wpID, mode) {
        var path= "AGP.Net/Resources/WPApproving/ApprovingStartControl.ascx";
        var query= "wpID=" + wpID;
        if (mode != null) query+= "&mode=" + mode;
        var res= res_RunModalControl(WorkspaceRoot, path, 550, 400, query, null);
        return (res == true);
}
function res_OpenDashboardFinder() {
        var url= WorkspaceRoot + "AGP.Net/Components/AdvPortalManager/Search/SMDashboards.aspx";
        if (event.shiftKey) url+= "?ShowAll=1";
        return window.showModalDialog(url, "", "dialogHeight:440px;dialogWidth:880px;status:no;help:no;scroll:no;");
}

function res_FindDashboard() {
        var res= res_OpenDashboardFinder();
        if (res != null) window.location= "main.aspx?dbID=" + res;
}

function res_RunDataTableExplorer(wsroot) {
        var path= wsroot + "AGP.Net/Resources/FormManager/Explorer/SmartExplorer.aspx";
        return res_ShowModalDialog(path, "", null, 500, 400);
}
function res_RunDataFormExplorer(wsroot, table) {
        var path= wsroot + "AGP.Net/Resources/FormManager/Explorer/SmartFormExplorer.aspx";
        return res_ShowModalDialog(path, "t=" + table, null, 500, 400);
}

function res_RunViewerMenuBuilder(wsroot, args) {
        var path= wsroot + "AGP.Net/Resources/FormManager/Explorer/MenuBuilder/main.aspx";
        return res_ShowModalDialog(path, "", args, 620, 400);
}
