﻿function configWorkspace(wkSpcID)
{
    window.radopen("ActiveContent/manageWorkspace.aspx?ID=" + wkSpcID,"wndAddWorkSpace");               
}

function configGrid(p, p2) {

    var oWnd;
    if (isNaN(p)) {
        oWnd = window.radopen("ActiveContent/configureGrid.aspx?S=" + p + "&V=" + p2, null);
    }
    else {
        oWnd = window.radopen("ActiveContent/configureGrid.aspx?P=" + p, "wndConfigureGrid");
    }
    
    oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Reload + Telerik.Web.UI.WindowBehaviors.Close);
    oWnd.set_width("475px");
    oWnd.set_height("550px");
    oWnd.set_modal(true);
}

   
function openConfigPaneWnd(pane)
{
    window.radopen("ActiveContent/configurePanel.aspx?P=" + pane,"wndConfigurePane"); 
}

function openSchedSetup() {

    var oWindow = window.radopen("ActiveContent/UserControls/scheduler_setup.aspx", null);
    oWindow.set_width("655px");
    oWindow.set_height("225px");
    oWindow.set_title("Scheduler Setup")
    oWindow.center();
    oWindow.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
    oWindow.set_modal(true);
}

function closeAllPopups()
{
    for (var i in openWnd) 
    {
        try
        {
            openWnd[i].close()
        }
        catch(e){}
    }       
}


function onTabSelecting(sender, args)
{

    if(args.get_tab().get_value() == "NEWWORKSPACE")
    {            
        configWorkspace(0)
        args.get_tab().set_postBack(false);
        args.set_cancel(true) 
    }
}


function clientButtonClicking(sender, args)
{
    var s = args.get_item().get_value()

    switch(s)
    {
        case "NEWAPPOINTMENT":            
        var now = new Date()
        newAppointment(now)
        break;
        
        case "POWERSEARCH":
        var disp_setting="toolbar=no,location=no,directories=no,menubar=no,resizable=yes,alwaysRaised=yes,"; 
	        disp_setting+="scrollbars=yes,width=700, height=600, left=100, top=25"; 
	    
	    if(!openWnd['PowerSearch'])
	    {
	        openWnd['PowerSearch'] = window.open("ActiveContent/PowerSearch.aspx", "wndPowerSearch", disp_setting);
	        openWnd['PowerSearch'].focus()
	    }
	    else
	    {
	        try{openWnd['PowerSearch'].focus()}
	        catch(e){openWnd['PowerSearch'] = window.open("ActiveContent/PowerSearch.aspx", "wndPowerSearch", disp_setting);}
	    }
        break;
        
        case "NEWACCOUNT":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=NewAccountX", "0")
        break;
        
        case "NEWLEAD":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=NewLeadX&New=1", "0")
        break;
        
        case "KNOWLEDGEBASE":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=KbaseX", "0")
        break;   
        
        case "NEWISSUE":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=ContactLookupX&SubDirective=CreateIssueX", "0")   
        break;  
        
        case "REPORTCENTER":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=ReportsX", "0")   
        break;     
        
        case "NEWPO":    
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=NewPOX", "0")   
        break;   
        
        case "INVENRESEARCH":
        loadEditWindow("ActiveContent/DrivenCRM.aspx?Directive=InvResearchX", "0")   
        break;           
    }
}

function loadEditWindow(url, id)
{
    var disp_setting="toolbar=no,location=no,status=yes,directories=no,menubar=no,resizable=yes,alwaysRaised=yes,"; 
    var w=800; 
    var h=600; 
    if (window.screen) { 
        w = window.screen.availWidth-30; 
        h = window.screen.availHeight-50; 
    }
    disp_setting+="scrollbars=yes,width="+w+", height="+h+", left=3, top=3";

    var urlArr = url.split("/")
    var w = "wnd_" + id.trim()
    var wndName = "wnd" + urlArr[urlArr.length - 1].split(".")[0] + id.trim()
    //url += "?ID=" + id.trim()


    if (navigator.userAgent.indexOf('Chrome') > 0) {
        if (openWnd[w]) {
            if (openWnd[w].closed)
                openWnd[w] = null;
        }
    }

    
    
    if(!openWnd[w])
    {
        openWnd[w] = window.open(url, wndName, disp_setting);
    }
    else
    {
        try {
            openWnd[w].close() //focus()
            //openWnd[w].location = url;
            openWnd[w] = window.open(url, wndName, disp_setting);
        }
          catch (e) {
              openWnd[w] = window.open(url, wndName, disp_setting);
            }
    }        
}


function schedulerFormCreated(scheduler, eventArgs)
{
	var mode = eventArgs.get_mode();
	if (mode == Telerik.Web.UI.SchedulerFormMode.AdvancedInsert ||
		mode == Telerik.Web.UI.SchedulerFormMode.AdvancedEdit)
	{
		// Initialize the client-side object for the advanced form
		var schedulerElement = scheduler.get_element();
		var formElement = eventArgs.get_formElement();
		var isModal = scheduler.get_advancedFormSettings().modal;
		var advancedTemplate = new window.SchedulerAdvancedTemplate(schedulerElement, formElement, isModal);
		advancedTemplate.initialize();

		// Are we using Web Service data binding?
		if (!scheduler.get_webServiceSettings().get_isEmpty())
		{
			// Populate the form with the appointment data
			var apt = eventArgs.get_appointment();
			var isInsert = mode == Telerik.Web.UI.SchedulerFormMode.AdvancedInsert;
			var editSeries = eventArgs.get_editingRecurringSeries();
			advancedTemplate.populate(apt, isInsert, editSeries);
		}
	}				
}
