﻿/* =============================================
Author:		Tarun Kumar
Create date: 8-April-2008
Revision : 
=============================================*/
var sendAllPartFlag="";
var shoppingListData="";
function formatData(partSendOption)
{
 /* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description:	this function is responsible for creating Array each element of which is an object and that 
    object will have properties to store partNumber, qty and part name. Array will contain either all parts 
    information or selected parts information depending on user choice. 
    =============================================*/

    var partData;
    var dataArray = new Array();
    var partArray = new Array();
    var partDataArray = new Array();
    var j=0;
    if(partSendOption==null || !partSendOption){return;}
    
   
    if(partSendOption=='SendAllParts')
    {
           
        dataArray = ShoppingListItems;
        if(dataArray.length <= 0 )
        {
            return false;
        }  
    }
    else
    {
        for(i=0;i<ShoppingListItems.length;i++)
        {
           if(ShoppingListItems[i].Checked.toString().toLowerCase() == "true")
           {
                dataArray[j] = new Object();
                dataArray[j].PartNumber = ShoppingListItems[i].PartNumber;
                dataArray[j].PartName = ShoppingListItems[i].PartName;
                dataArray[j].Qty = ShoppingListItems[i].Qty;
                j++;
           }
        }
        if(dataArray.length <= 0 )
        {
            return false;
        }

    }
    
    
   return dataArray; 
}






function sendToSelectedSession(name, type, partSendOption) {
 /* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description:	this function will call the sendToPIA() function of pia.js to send the part information to PIA. 
    This function call the formatData() function to obtain a list of all the parts that need to be send and then pass 
    this information to SendToPIA()
    =============================================*/


  var dataArray;
  if (!name || !type || !partSendOption) {return;}
  dataArray = formatData(partSendOption);
  if(!dataArray){return};
  if (type == "PIA") {
    sendToPIA(name, dataArray);
  } 
  
  
}



function sendConfirmationDialog()
{
    var retValue
        
    for(i=0;i<ShoppingListItems.length;i++)
    {
     if(ShoppingListItems[i].Checked.toString().toLowerCase() == "true")
     {
        sendToSelectedSession(defaultSession,"PIA","SendSelected");
        return;
     }
    }
    
    if(sendAllPartFlag!= null && sendAllPartFlag== "SendAll")
    {
        sendToSelectedSession(defaultSession,"PIA","SendAllParts");
    }
    else
    {
        
        showDMSConfirmationPopup();
       
    }
}

function sendAllItems()
{
    sendToSelectedSession(defaultSession,"PIA","SendAllParts");
}

function checkIntegrationService()
{
 /* =============================================
    Author:		Tarun Kumar
    Create date: 8-April-2008
    Description:	this function check if the integration session selected by the user is available on the current 
    machine or not . if not then this function will open up integration manager popup window.
    =============================================*/

    var PIA_Response;
    var newSessionName;
    if(defaultSession==null)
    {
        defaultSession = "";
    }
    PIA_Response = initPIA(defaultSession);
    
    if(ShoppingListItems==null || ShoppingListItems.length <= 0){return;}
            
    if(PIA_Response == "Session initialization failed")
    {
       
       openPopUpWindow(integrationManagerUrl,"IntegrationManager","dialogWidth=390px;dialogHeight=470px;status:no;scroll:no;menubar:no;location:no",true);
       callCheckAvailableService=true;
       RunDMSIntegrationService();
       return;
    }
    
    showSendConfirmationDialog();
    
}

function showSendConfirmationDialog()
{
    callCheckAvailableService=false;
    PIA_Response = initPIA(defaultSession);
                    
    if(PIA_Response != "Session initialization failed")
    {
        if(defaultSession!=null && defaultSession!="")
        {
            sendConfirmationDialog();
        }
    }
}

function openPopUpWindow(url,title,option,modelPopUp)
{
    var obj;
    if(modelPopUp == true)
    {
        obj = window.showModalDialog(url,option,option);
        return obj;
        
    }
    else
    {
        window.open(url,title,option);
    }
    
}

function DMSIntegration ()
{

      this.DMSSettings =  null;
      this.getDMSSetting = function (settings)
      {
          this.DMSSettings = settings;
            
            arrServiceName = this.DMSSettings.arrServiceName.toString().split(",");
            arrClassName = this.DMSSettings.arrClassName.toString().split(",");
            arrServiceValue = this.DMSSettings.arrServiceValue.toString().split(",");
            sendAllPartFlag = this.DMSSettings.sendAllPartFlag;
            defaultSession = this.DMSSettings.sessionName;
            cultureCode = this.DMSSettings.cultureCode;
           if(callCheckAvailableService==false)
           {
            checkAvailableService(); 
           }
           else
           {
            showSendConfirmationDialog();
           }
           
      }
}



function RunDMSIntegrationService()
{
      var  DMS = new DMSIntegration();
      AWS.ChryslerAccessories.WebServices.DMSInfo.getSettings(Function.createDelegate (DMS,DMS.getDMSSetting));  
      
}


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();