﻿// JScript File

/* =============================================
Author:		Tarun Kumar
Create date: 8-April-2008
Revision : 
=============================================*/


var PIA_CONST = "PIA";
var SET_PROPERTIES = "SET_PROPERTIES";
var PIA_SETUP_ERROR = "PIA_SETUP_ERROR";
var defaultSession="";    
var defaultSessionExists=false;    

var ConfiguredSessionLisBoxID="";
var AvailableServiceLisBoxID="";
var sessionNameHFID="";
var serviceTypeHFID="";
var btnPropertiesID="";
var btnSendToDMSID="";
var btnDefaultID="";

var arrServiceName = "";
var arrClassName = "";
var arrServiceValue = "";
var integrationManagerUrl="";
var callCheckAvailableService=false;

function setProperties() 
{
 /* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description:	this function is responsible for start of process for creation of new PIA session.performs few checks to determine if the user has selected a integration service(Proquest integration assistant) and a session if yes then call the setupPIAService() method.
    =============================================*/

  
  var configuredService = document.getElementById(ConfiguredSessionLisBoxID);
  var avilableService = document.getElementById(AvailableServiceLisBoxID);
  if ((!configuredService || configuredService == null) && (!avilableService || avilableService == null)) {return false;}
  if ((configuredService.selectedIndex < 0) && (avilableService.selectedIndex < 0)) {return false;}
  var arrIntegrationSession = new Array();
  var name;
  var type;
  
  
  if((configuredService!= null) && (configuredService.selectedIndex >= 0))
  {
      arrIntegrationSession = configuredService.options[configuredService.selectedIndex].innerText.toString().split("(");
      if(arrIntegrationSession.length > 0)
      {
        name = arrIntegrationSession[0].toString().split("(")[0].trim();
        type = configuredService.value.toString().split("#")[1];
      }
   
  }
  else
  {
    type = avilableService.value;
    name = avilableService.options[avilableService.selectedIndex].innerText;
  
    
    if(defaultSession!=null && defaultSession!= "")
     {
     
        
        name=defaultSession;
     }
  }
   
   
     
   
   
  if (type)
  {
    
    document.getElementById(serviceTypeHFID).value = type;
    
    if (type == PIA_CONST)
    {
      
      

        return setupPIAService(name, SET_PROPERTIES);
      
    }
    
  }
}



function setupPIAService(name, caller)
{
/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: calls the setupPIA() function of pia.js. it catch the name of newly created session and call the fillConfiguredSession() function.
    =============================================*/
    var pageUrl;
    
    var configuredService = document.getElementById(ConfiguredSessionLisBoxID);
    
    var newName = setupPIA(name);
    
    if (!newName || newName == null 
    		|| newName == PIA_SETUP_ERROR)
    {
        fillConfiguredSession();
      return false;
    }

    
   
    if (newName != null && ( newName != name))
    {
        fillConfiguredSession();
        return true;
    }
    else if (newName = name)
    {
        fillConfiguredSession();
        return false;
    }
    

    
}



function CheckConfiguredService(configuredService)
{
/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: calls the setupPIA() function of pia.js. it catch the name of newly created session and call the fillConfiguredSession() function.
    =============================================*/

  
  var configuredService = document.getElementById(ConfiguredSessionLisBoxID);
  if (configuredService.selectedIndex < 0) 
  {
  return false;
  }
  else
  {
  return true;
  }
  
}


function openIntegrationManager()
{
/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this function will open a integrtaion manager screen as a popup window. This function is required user try to send part to DMS and we don’t have any integration session configured on client machine
    =============================================*/


    var status;

    status=checkService()
    if(status!=false)
    {
        
        
        openPopUpWindow(integrationManagerUrl,"IntegrationManager","dialogWidth=390px;dialogHeight=470px;status:no;scroll:no",true);
        callCheckAvailableService=false;
        RunDMSIntegrationService();
    }
        
}


function closeIntegrationManager()
{

/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this function will pass the return value (default) to the window which has opened the integration manager pop up window and close the integration manager popup.
    =============================================*/

        
    if((defaultSession!=null && defaultSession=="") || defaultSessionExists ==false)
    {
        
        var userChoice= confirm(DMSLocalized.IntegrationManagerConfirmMessage);
        
        if(userChoice==true)
        {
            window.returnValue = ""
            window.close();
        }
    }
    else
    {
            window.returnValue = defaultSession;
            window.close();
    }
}


function checkAvailableService()
{

/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this function perform a check to determine if the integration service is installed on client machine by trying to create a object of integration serivce. If the service is not installed it will call removeAvailableService() otherwise it will call fillConfiguredSession()
    =============================================*/


    
    var element;
    var objService;
    var serviceExists=false;
    for(i=0;i<arrServiceName.length;i++)
    {
        try
        {
            objService = new ActiveXObject(arrClassName[i].toString());
            if(arrServiceValue[i] == "PIA")
            {
                selectService(arrServiceValue[i]);
                enablePropertyButton();
            }
            serviceExists=true;         
        }
        catch(e)
        {
            
            removeAvailableService(arrServiceName[i]);
        }
    }
    
    element = document.getElementById(btnSendToDMSID);
    
    if(serviceExists ==false)
    {
        
        if(element!=null)
        {
            element.style.visibility='hidden';
            
        }
        
        return false;
    }
    else
    {
        fillConfiguredSession()
        if(element!=null)
        {
            if(defaultSession!=null && defaultSession != "")
            {
                element.title = "Send to DMS Session: " + defaultSession;
            }
            else
            {
                element.title = "Send to DMS Session: [not set]";
            }
            
        }
        
    }
    return true;
}

function checkService()
{
/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this  function call the checkAvailableService() . this function is required in the case where we need to show a popup message PIA is not installed.
    =============================================*/

    var retValue;
        retValue=checkAvailableService()
        if(retValue==false)
        {
            
            showPIARequiredPopup();
            return false;
        }
        return true;
}
function removeAvailableService(serviceName)
{

/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this function will remove the integration service from the list of available integration service if the integration service is not installed on client machine.
    =============================================*/


    
    var avilableService = document.getElementById(AvailableServiceLisBoxID);
    if(avilableService==null){return;}
    
    for (j = 0; j < avilableService.options.length; j++)
	{
	     if(avilableService.options[j].text == serviceName)
	     {
	          avilableService.remove(j);
	      }
	}


}

function selectService(serviceValue)
{
    var avilableService = document.getElementById(AvailableServiceLisBoxID);
    if(avilableService==null){return;}
    for (j = 0; j < avilableService.options.length; j++)
	{
	     if(avilableService.options[j].value == serviceValue)
	     {
	          avilableService.selectedIndex = j;
	      }
	}

    
}

function fillConfiguredSession()
{
/* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description: this function will call the getPIASession() function of pia.js and  fill the name of configured session in a list box on client site.
    =============================================*/

    var sessioName;
    var arrSessioName = new Array();
    
    
    var element = document.getElementById(ConfiguredSessionLisBoxID);
    var avilableService = document.getElementById(AvailableServiceLisBoxID);
    
    if(element==null){return;}
    
    element.innerHTML = "";
    
    if(!element || element==null){return};
    sessioName = getPIASession();
    
    if(sessioName!=null && sessioName!="")
    {
       arrSessioName = sessioName.toString().split(",");
       defaultSessionExists = false;
       for(i=0;i<arrSessioName.length;i++)
       {
            var opt = document.createElement("option");
            if(arrSessioName[i] == defaultSession)
            {
                  opt.text =arrSessioName[i] + " (" + DMSLocalized.defaultLabel + ")";
                  defaultSessionExists = true;
            }
            else
            {
                opt.text =arrSessioName[i];
            }
            
            opt.value =arrSessioName[i] + "#PIA" ;
            element.options.add(opt);

          
       }
    }
    if ((element.selectedIndex < 0) && (avilableService.selectedIndex < 0))
    {
        
        document.getElementById(btnPropertiesID).disabled=true;
    }
}

function enablePropertyButton()
{
    
    var configuredService = document.getElementById(ConfiguredSessionLisBoxID);
    var avilableService = document.getElementById(AvailableServiceLisBoxID);    
    if(document.getElementById(btnPropertiesID)==null || !document.getElementById(btnPropertiesID)){return;}
    if ((configuredService.selectedIndex < 0) && (avilableService.selectedIndex < 0))
    {
        
        document.getElementById(btnPropertiesID).disabled=true;
    }
    else
    {
        
        document.getElementById(btnPropertiesID).disabled=false;
    }
    
    if(document.getElementById(btnDefaultID)==null || !document.getElementById(btnDefaultID)){return;}
    if(configuredService.selectedIndex < 0)
    {
        
        document.getElementById(btnDefaultID).disabled=true;
    }
    else
    {
        
        document.getElementById(btnDefaultID).disabled=false;
    }
    
}


DMSLocalized={
"IntegrationManagerConfirmMessage":"No default session has been selected.  To send list items to the DMS, you must select a default session.   \r\n Are you sure you would like to close this window without selecting a default session? \r\n\r\n NOTE:  If no sessions are available, you can create one through the Properties button.",
"defaultLabel":"default"
};

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();