serviceproviders/jsapi/platformservices/src/contacts.js
changeset 27 02682e02e51f
child 34 5dae2c62e9b6
equal deleted inserted replaced
26:5d0ec8b709be 27:02682e02e51f
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Defination of class CCalendarInterface
       
    15 *
       
    16 */
       
    17 
       
    18 function __device_contacts_descriptor(provider){
       
    19   this.interfaceName = provider.interfaceName;
       
    20   this.version = provider.version;
       
    21 }
       
    22 
       
    23 function __device_contacts_startEditor(callback, contact, ErrorCallback){
       
    24  return this.provider.startEditor(callback, contact, ErrorCallback);
       
    25 }
       
    26 
       
    27 function __device_contacts_getContacts(callback, match, sortOrder, ErrorCallback ){
       
    28   return this.provider.getContacts(callback, match, sortOrder, ErrorCallback );
       
    29 }
       
    30 
       
    31 function __device_contacts_add(contact){
       
    32   return this.provider.addContact(contact);
       
    33 }
       
    34 
       
    35 function __device_contacts_update(contact){
       
    36   this.provider.updateContact(contact);
       
    37 }
       
    38 
       
    39 function __device_contacts_delete(id){
       
    40   this.provider.deleteContacts(id);
       
    41 }
       
    42 
       
    43 function __device_contacts_getContactInfo(id){
       
    44   return this.provider.getContactInfo(id);
       
    45 }
       
    46 function __device_contacts_addGroup(groupName){
       
    47 	return this.provider.addGroup(groupName);
       
    48 }
       
    49 
       
    50 function __device_contacts_getGroups(callback, ErrorCallback){
       
    51     return this.provider.getGroups(callback, ErrorCallback);
       
    52 }
       
    53 
       
    54 function __device_contacts_deleteGroups(id){
       
    55 	this.provider.deleteGroups(id);
       
    56 }
       
    57 
       
    58 function __device_contacts_addContactsToGroup(groupId,id)
       
    59 {
       
    60 	this.provider.addContactsToGroup(groupId,id);
       
    61 }
       
    62 
       
    63 function __device_contacts_getContactIds(callback, match, sortOrder, ErrorCallback)
       
    64 {
       
    65 	return this.provider.getContactIds(callback, match, sortOrder, ErrorCallback);
       
    66 }
       
    67 
       
    68 function __device_contacts_getGroupIds(callback, ErrorCallback)
       
    69 {
       
    70 	return this.provider.getGroupIds(callback, ErrorCallback);
       
    71 }
       
    72 
       
    73 function __device_contacts_removeContactsFromGroup(groupId,id)
       
    74 {
       
    75 	this.provider.removeContactsFromGroup(groupId,id);
       
    76 }
       
    77 
       
    78 function __device_contacts_cancel(transactionId)
       
    79 {
       
    80 	this.provider.cancel(transactionId);
       
    81 }
       
    82 
       
    83 function __device_contacts_updateGroup(group)
       
    84 {
       
    85 	this.provider.updateGroup(group);
       
    86 }
       
    87 
       
    88 function __device_contacts_getGroupInfo(grpid)
       
    89 {
       
    90 	return this.provider.getGroupInfo(grpid);
       
    91 }
       
    92 
       
    93 // Private location prototype: called from service factory
       
    94 function __device_contacts(provider){
       
    95 //alert("inside ");
       
    96   //Private properties
       
    97   this.provider = provider;
       
    98   //Read-only properties
       
    99   this.interfaceName = provider.descriptor.interfaceName;
       
   100   this.version = provider.descriptor.version;
       
   101   
       
   102     //constants
       
   103   this.SORT_ASCENDING = 0;
       
   104   this.SORT_DESCENDING = 1;
       
   105   
       
   106   //Core methods
       
   107   this.startEditor = __device_contacts_startEditor;
       
   108   //Extended methods
       
   109   this.getContacts = __device_contacts_getContacts;
       
   110   this.addContact = __device_contacts_add;
       
   111   this.updateContact = __device_contacts_update;
       
   112   this.deleteContacts = __device_contacts_delete;
       
   113   this.getContactInfo = __device_contacts_getContactInfo;
       
   114   this.addGroup = __device_contacts_addGroup;
       
   115   this.getGroups = __device_contacts_getGroups;
       
   116   this.deleteGroups = __device_contacts_deleteGroups;
       
   117   this.addContactsToGroup = __device_contacts_addContactsToGroup;
       
   118   this.getContactIds = __device_contacts_getContactIds;
       
   119   this.getGroupIds = __device_contacts_getGroupIds;
       
   120   this.removeContactsFromGroup = __device_contacts_removeContactsFromGroup;
       
   121   this.cancel = __device_contacts_cancel;
       
   122   this.updateGroup = __device_contacts_updateGroup;
       
   123   this.getGroupInfo = __device_contacts_getGroupInfo;
       
   124 }
       
   125 
       
   126 // contacts service interface
       
   127 
       
   128 // Namepace: com.nokia.device.service
       
   129 
       
   130 var __device_contacts_service_entry = {"name": null, 
       
   131 					  "version": null,
       
   132 					  "proto": __device_contacts,
       
   133 					  "descriptor": __device_contacts_descriptor,
       
   134 					  "providers": [{"descriptor": __sp_contacts_descriptor, "instance": __sp_contacts_instance}]
       
   135 					 };
       
   136 		   
       
   137 		   
       
   138 		   
       
   139 		   
       
   140 		   
       
   141 		   
       
   142 		   
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 
       
   153 
       
   154 /*
       
   155  Copyright © 2009 Nokia. All rights reserved.
       
   156  Code licensed under the BSD License:
       
   157  Software License Agreement (BSD License) Copyright © 2009 Nokia.
       
   158  All rights reserved.
       
   159  Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       
   160  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
       
   161  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
       
   162  Neither the name of Nokia Corporation. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Nokia Corporation.
       
   163  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
   164  version: 1.0
       
   165  */
       
   166 // Table of user callbacks. These are "indexed" by transaction IDs.
       
   167 // These need to be global since the actual sp callback does
       
   168 // not have the location instance object in its scope chain
       
   169 
       
   170 /*
       
   171  if((typeof (com.nokia.device.contacts) == 'undefined'))
       
   172  {
       
   173  com.nokia.device.contacts = {};
       
   174  }*/
       
   175 nokia.device.contacts.SORT_ASCENDING = 0;
       
   176 nokia.device.contacts.SORT_DESCENDING = 1;
       
   177 
       
   178 
       
   179 //nokia.devive.
       
   180 //SORT_ASCENDING = 0;
       
   181 //SORT_DESCENDING = 1;
       
   182 
       
   183 function __sp_contacts_descriptor(){
       
   184   //Read-only properties 
       
   185    this.interfaceName = "contacts";
       
   186   if(window.__Service_Interface_Ver){
       
   187 //	alert('in __sp_contacts_descriptor'+window.__Service_Interface_Ver);
       
   188 	this.version =__Service_Interface_Ver;
       
   189 	}
       
   190   else{
       
   191 	this.version =1.0;
       
   192 	}
       
   193   //Class-static properties 
       
   194 }
       
   195 
       
   196 
       
   197 /*--------- device<->sp mapping functions --------*/
       
   198 function __s60_enumerate_contact_object(object, namespace, func, param){
       
   199     try {
       
   200         var key;
       
   201         for (key in object) {
       
   202             var propname;
       
   203             if (namespace) {
       
   204                 propname = namespace + "." + key;
       
   205                 
       
   206             }
       
   207             
       
   208             else {
       
   209                 propname = key;
       
   210             }
       
   211             var value = object[key];
       
   212             if (value instanceof Array) {
       
   213                 func(propname, value, param);
       
   214             }
       
   215             else 
       
   216                 if (value instanceof Date) {
       
   217                     func(propname, value, param);
       
   218                 }
       
   219                 else 
       
   220                     if (typeof value == "object") {
       
   221                         __s60_enumerate_contact_object(value, propname, func, param);
       
   222                     }
       
   223                     else {
       
   224                         func(propname, value, param);
       
   225                     }
       
   226         }
       
   227     } 
       
   228     
       
   229     catch (e) {
       
   230 		throw e;
       
   231 		//alert("exception in __s60_enumerate_contact_object" + e.toString());
       
   232     }
       
   233 }
       
   234 
       
   235 // Map from device contact to sp contact
       
   236 
       
   237 // TBD: labels need localization
       
   238 function __sp_contact_extract(name, value, sp_contact){
       
   239     //__device_debug("sp_contact_extract: " + name + " " + value); 
       
   240     switch (name) {
       
   241         case "name.last":
       
   242             sp_contact.LastName = {};
       
   243             modifyObjectBaseProp(sp_contact.LastName);
       
   244             sp_contact.LastName.Label = "Last name";
       
   245             sp_contact.LastName.Value = value;
       
   246             break;
       
   247         case "name.first":
       
   248             sp_contact.FirstName = {};
       
   249             modifyObjectBaseProp(sp_contact.FirstName);
       
   250             sp_contact.FirstName.Label = "First name";
       
   251             sp_contact.FirstName.Value = value;
       
   252             break;
       
   253         case "name.middle":
       
   254             sp_contact.MiddleName = {};
       
   255             modifyObjectBaseProp(sp_contact.MiddleName);
       
   256             sp_contact.MiddleName.Label = "Middle name";
       
   257             sp_contact.MiddleName.Value = value;
       
   258             break;
       
   259         case "name.prefix":
       
   260             sp_contact.Prefix = {};
       
   261             modifyObjectBaseProp(sp_contact.Prefix);
       
   262             sp_contact.Prefix.Label = "Prefix";
       
   263             sp_contact.Prefix.Value = value;
       
   264             break;
       
   265         case "name.suffix":
       
   266             sp_contact.Suffix = {};
       
   267             modifyObjectBaseProp(sp_contact.Suffix);
       
   268             sp_contact.Suffix.Label = "Suffix";
       
   269             sp_contact.Suffix.Value = value;
       
   270             break;
       
   271         case "tel.land":
       
   272             sp_contact.LandPhoneGen = {};
       
   273             modifyObjectBaseProp(sp_contact.LandPhoneGen);
       
   274             sp_contact.LandPhoneGen.Label = "Landline";
       
   275             sp_contact.LandPhoneGen.Value = value;
       
   276             break;
       
   277         case "tel.mobile":
       
   278             sp_contact.MobilePhoneGen = {};
       
   279             modifyObjectBaseProp(sp_contact.MobilePhoneGen);
       
   280             sp_contact.MobilePhoneGen.Label = "Mobile";
       
   281             sp_contact.MobilePhoneGen.Value = value;
       
   282             break;
       
   283         case "tel.video":
       
   284             sp_contact.VideoNumberGen = {};
       
   285             modifyObjectBaseProp(sp_contact.VideoNumberGen);
       
   286             sp_contact.VideoNumberGen.Label = "Video";
       
   287             sp_contact.VideoNumberGen.Value = value;
       
   288             break;
       
   289         case "tel.fax":
       
   290             sp_contact.FaxNumberGen = {};
       
   291             modifyObjectBaseProp(sp_contact.FaxNumberGen);
       
   292             sp_contact.FaxNumberGen.Label = "Fax";
       
   293             sp_contact.FaxNumberGen.Value = value;
       
   294             break;
       
   295         case "tel.voip":
       
   296 		//alert("adding gen"+value);
       
   297             sp_contact.VOIPGen = {};
       
   298             modifyObjectBaseProp(sp_contact.VOIPGen);
       
   299             sp_contact.VOIPGen.Label = "Voip";
       
   300             sp_contact.VOIPGen.Value = value;
       
   301             break;
       
   302         case "tel.home.land":
       
   303             sp_contact.LandPhoneHome = {};
       
   304             modifyObjectBaseProp(sp_contact.LandPhoneHome);
       
   305             sp_contact.LandPhoneHome.Label = "Home Landline";
       
   306             sp_contact.LandPhoneHome.Value = value;
       
   307             break;
       
   308         case "tel.home.mobile":
       
   309             sp_contact.MobilePhoneHome = {};
       
   310             modifyObjectBaseProp(sp_contact.MobilePhoneHome);
       
   311             sp_contact.MobilePhoneHome.Label = "Home Mobile";
       
   312             sp_contact.MobilePhoneHome.Value = value;
       
   313             break;
       
   314         case "tel.home.video":
       
   315             sp_contact.VideoNumberHome = {};
       
   316             modifyObjectBaseProp(sp_contact.VideoNumberHome);
       
   317             sp_contact.VideoNumberHome.Label = "Home Video";
       
   318             sp_contact.VideoNumberHome.Value = value;
       
   319             break;
       
   320         case "tel.home.fax":
       
   321             sp_contact.FaxNumberHome = {};
       
   322             modifyObjectBaseProp(sp_contact.FaxNumberHome);
       
   323             sp_contact.FaxNumberHome.Label = "Home Fax";
       
   324             sp_contact.FaxNumberHome.Value = value;
       
   325             break;
       
   326         case "tel.home.voip":
       
   327 			//alert("adding home"+value);
       
   328             sp_contact.VoipHome = {};
       
   329             modifyObjectBaseProp(sp_contact.VoipHome);
       
   330             sp_contact.VoipHome.Label = "Home Voip";
       
   331             sp_contact.VoipHome.Value = value;
       
   332             break;
       
   333         case "tel.work.land":
       
   334             sp_contact.LandPhoneWork = {};
       
   335             modifyObjectBaseProp(sp_contact.LandPhoneWork);
       
   336             sp_contact.LandPhoneWork.Label = "Work Landline";
       
   337             sp_contact.LandPhoneWork.Value = value;
       
   338             break;
       
   339         case "tel.work.mobile":
       
   340             sp_contact.MobilePhoneWork = {};
       
   341             modifyObjectBaseProp(sp_contact.MobilePhoneWork);
       
   342             sp_contact.MobilePhoneWork.Label = "Work Mobile";
       
   343             sp_contact.MobilePhoneWork.Value = value;
       
   344             break;
       
   345         case "tel.work.video":
       
   346             sp_contact.VideoNumberWork = {};
       
   347             modifyObjectBaseProp(sp_contact.VideoNumberWork);
       
   348             sp_contact.VideoNumberWork.Label = "Work Video";
       
   349             sp_contact.VideoNumberWork.Value = value;
       
   350             break;
       
   351         case "tel.work.fax":
       
   352             sp_contact.FaxNumberWork = {};
       
   353             modifyObjectBaseProp(sp_contact.FaxNumberWork);
       
   354             sp_contact.FaxNumberWork.Label = "Work Fax";
       
   355             sp_contact.FaxNumberWork.Value = value;
       
   356             break;
       
   357         case "tel.work.voip":
       
   358 		//alert("adding work"+value);
       
   359             sp_contact.VoipWork = {};
       
   360             modifyObjectBaseProp(sp_contact.VoipWork);
       
   361             sp_contact.VoipWork.Label = "Work Voip";
       
   362             sp_contact.VoipWork.Value = value;
       
   363             break;
       
   364         case "address.street":
       
   365             sp_contact.AddrStreetGen = {};
       
   366             modifyObjectBaseProp(sp_contact.AddrStreetGen);
       
   367             sp_contact.AddrStreetGen.Label = "Street Address";
       
   368             sp_contact.AddrStreetGen.Value = value;
       
   369             break;
       
   370         case "address.local":
       
   371             sp_contact.AddrLocalGen = {};
       
   372             modifyObjectBaseProp(sp_contact.AddrLocalGen);
       
   373             sp_contact.AddrLocalGen.Label = "City";
       
   374             sp_contact.AddrLocalGen.Value = value;
       
   375             break;
       
   376         case "address.region":
       
   377             sp_contact.AddrRegionGen = {};
       
   378             modifyObjectBaseProp(sp_contact.AddrRegionGen);
       
   379             sp_contact.AddrRegionGen.Label = "State/Province";
       
   380             sp_contact.AddrRegionGen.Value = value;
       
   381             break;
       
   382         case "address.code":
       
   383             sp_contact.AddrPostCodeGen = {};
       
   384             modifyObjectBaseProp(sp_contact.AddrPostCodeGen);
       
   385             sp_contact.AddrPostCodeGen.Label = "Postal code";
       
   386             sp_contact.AddrPostCodeGen.Value = value;
       
   387             break;
       
   388         case "address.country":
       
   389             sp_contact.AddrCountryGen = {};
       
   390             modifyObjectBaseProp(sp_contact.AddrCountryGen);
       
   391             sp_contact.AddrCountryGen.Label = "Country";
       
   392             sp_contact.AddrCountryGen.Value = value;
       
   393             break;
       
   394         case "address.email":
       
   395             sp_contact.EmailGen = {};
       
   396             modifyObjectBaseProp(sp_contact.EmailGen);
       
   397             sp_contact.EmailGen.Label = "EMail";
       
   398             sp_contact.EmailGen.Value = value;
       
   399             break;
       
   400         case "address.uri":
       
   401             sp_contact.URLGen = {};
       
   402             modifyObjectBaseProp(sp_contact.URLGen);
       
   403             sp_contact.URLGen.Label = "Website";
       
   404             sp_contact.URLGen.Value = value;
       
   405             break;
       
   406         case "address.home.street":
       
   407             sp_contact.AddrStreetHome = {};
       
   408             modifyObjectBaseProp(sp_contact.AddrStreetHome);
       
   409             sp_contact.AddrStreetHome.Label = "Home Address";
       
   410             sp_contact.AddrStreetHome.Value = value;
       
   411             break;
       
   412         case "address.home.local":
       
   413             sp_contact.AddrLocalHome = {};
       
   414             modifyObjectBaseProp(sp_contact.AddrLocalHome);
       
   415             sp_contact.AddrLocalHome.Label = "City";
       
   416             sp_contact.AddrLocalHome.Value = value;
       
   417             break;
       
   418         case "address.home.region":
       
   419             sp_contact.AddrRegionHome = {};
       
   420             modifyObjectBaseProp(sp_contact.AddrRegionHome);
       
   421             sp_contact.AddrRegionHome.Label = "State/Province";
       
   422             sp_contact.AddrRegionHome.Value = value;
       
   423             break;
       
   424         case "address.home.code":
       
   425             sp_contact.AddrPostCodeHome = {};
       
   426             modifyObjectBaseProp(sp_contact.AddrPostCodeHome);
       
   427             sp_contact.AddrPostCodeHome.Label = "Postal code";
       
   428             sp_contact.AddrPostCodeHome.Value = value;
       
   429             break;
       
   430         case "address.home.country":
       
   431             sp_contact.AddrCountryHome = {};
       
   432             modifyObjectBaseProp(sp_contact.AddrCountryHome);
       
   433             sp_contact.AddrCountryHome.Label = "Country";
       
   434             sp_contact.AddrCountryHome.Value = value;
       
   435             break;
       
   436         case "address.home.email":
       
   437             sp_contact.EmailHome = {};
       
   438             modifyObjectBaseProp(sp_contact.EmailHome);
       
   439             sp_contact.EmailHome.Label = "Home EMail";
       
   440             sp_contact.EmailHome.Value = value;
       
   441             break;
       
   442         case "address.home.uri":
       
   443             sp_contact.URLHome = {};
       
   444             modifyObjectBaseProp(sp_contact.URLHome);
       
   445             sp_contact.URLHome.Label = "Home Website";
       
   446             sp_contact.URLHome.Value = value;
       
   447             break;
       
   448         case "address.work.street":
       
   449             sp_contact.AddrStreetWork = {};
       
   450             modifyObjectBaseProp(sp_contact.AddrStreetWork);
       
   451             sp_contact.AddrStreetWork.Label = "Work Address";
       
   452             sp_contact.AddrStreetWork.Value = value;
       
   453             break;
       
   454         case "address.work.local":
       
   455             sp_contact.AddrLocalWork = {};
       
   456             modifyObjectBaseProp(sp_contact.AddrLocalWork);
       
   457             sp_contact.AddrLocalWork.Label = "City";
       
   458             sp_contact.AddrLocalWork.Value = value;
       
   459             break;
       
   460         case "address.work.region":
       
   461             sp_contact.AddrRegionWork = {};
       
   462             modifyObjectBaseProp(sp_contact.AddrRegionWork);
       
   463             sp_contact.AddrRegionWork.Label = "State/Province";
       
   464             sp_contact.AddrRegionWork.Value = value;
       
   465             break;
       
   466         case "address.work.code":
       
   467             sp_contact.AddrPostCodeWork = {};
       
   468             modifyObjectBaseProp(sp_contact.AddrPostCodeWork);
       
   469             sp_contact.AddrPostCodeWork.Label = "Postal code";
       
   470             sp_contact.AddrPostCodeWork.Value = value;
       
   471             break;
       
   472         case "address.work.country":
       
   473             sp_contact.AddrCountryWork = {};
       
   474             modifyObjectBaseProp(sp_contact.AddrCountryWork);
       
   475             sp_contact.AddrCountryWork.Label = "Country";
       
   476             sp_contact.AddrCountryWork.Value = value;
       
   477             break;
       
   478         case "address.work.email":
       
   479             sp_contact.EmailWork = {};
       
   480             modifyObjectBaseProp(sp_contact.EmailWork);
       
   481             sp_contact.EmailWork.Label = "Work EMail";
       
   482             sp_contact.EmailWork.Value = value;
       
   483             break;
       
   484         case "address.work.uri":
       
   485             sp_contact.URLWork = {};
       
   486             modifyObjectBaseProp(sp_contact.URLWork);
       
   487             sp_contact.URLWork.Label = "Work Website";
       
   488             sp_contact.URLWork.Value = value;
       
   489             break;
       
   490         case "company.name":
       
   491             sp_contact.CompanyName = {};
       
   492             modifyObjectBaseProp(sp_contact.CompanyName);
       
   493             sp_contact.CompanyName.Label = "Company";
       
   494             sp_contact.CompanyName.Value = value;
       
   495             break;
       
   496         case "company.title":
       
   497             sp_contact.JobTitle = {};
       
   498             modifyObjectBaseProp(sp_contact.JobTitle);
       
   499             sp_contact.JobTitle.Label = "Title";
       
   500             sp_contact.JobTitle.Value = value;
       
   501             break;
       
   502         case "id":
       
   503             sp_contact.id = value;
       
   504             break;
       
   505         case "notes":
       
   506             sp_contact.Note = {};
       
   507             modifyObjectBaseProp(sp_contact.Note);
       
   508             //sp_contact.Note = {};
       
   509             sp_contact.Note.Label = "Note";
       
   510             sp_contact.Note.Value = value;
       
   511             break;
       
   512         case "anniversary":
       
   513             sp_contact.Anniversary = {};
       
   514             modifyObjectBaseProp(sp_contact.Anniversary);
       
   515             //sp_contact.Anniversary = {};
       
   516             sp_contact.Anniversary.Label = "Anniversary";
       
   517             sp_contact.Anniversary.Value = value;
       
   518             break;
       
   519         case "birthday":
       
   520             sp_contact.Date = {};
       
   521             modifyObjectBaseProp(sp_contact.Date);
       
   522             //sp_contact.Date = {};
       
   523             sp_contact.Date.Label = "BirthDay";
       
   524             sp_contact.Date.Value = value;
       
   525             break;
       
   526         case "nickName":
       
   527             sp_contact.SecondName = {};
       
   528             modifyObjectBaseProp(sp_contact.SecondName);
       
   529             //sp_contact.SecondName = {};
       
   530             sp_contact.SecondName.Label = "NickName";
       
   531             sp_contact.SecondName.Value = value;
       
   532             break;
       
   533         case "photo":
       
   534             sp_contact.CallerObjImg = {};
       
   535             modifyObjectBaseProp(sp_contact.CallerObjImg);
       
   536             //sp_contact.CallerObjImg = {};
       
   537             sp_contact.CallerObjImg.Label = "CallerObjImg";
       
   538             sp_contact.CallerObjImg.Value = value;
       
   539             break;
       
   540         case "xspid":
       
   541             sp_contact.IMPP = {};
       
   542             modifyObjectBaseProp(sp_contact.IMPP);
       
   543             //sp_contact.IMPP = {};
       
   544             sp_contact.IMPP.Label = "IMPP";
       
   545             sp_contact.IMPP.Value = value;
       
   546             break;
       
   547             
       
   548     }
       
   549 }
       
   550 
       
   551 function __s60_enumerate_build_contact_object(object, namespace, func, param){
       
   552 	//alert("In __s60_enumerate_build_contact_object");
       
   553     var key;
       
   554     for (key in object) {
       
   555         //if (object.hasOwnProperty('key')) {
       
   556         var propname;
       
   557         if (namespace) {
       
   558             propname = namespace + "." + key;
       
   559         }
       
   560         else {
       
   561             propname = key;
       
   562         }
       
   563         var value = object[key];
       
   564         //alert("propname"+propname);
       
   565         if (propname == "IMPP") {
       
   566             //	   //alert("val"+value[0]);
       
   567             func(propname, value, param);
       
   568         }
       
   569         if (typeof value == "object") {
       
   570             /*	if(value instanceof Array)
       
   571              {
       
   572              func(propname,value, param);
       
   573              }
       
   574              else
       
   575              {*/
       
   576             __s60_enumerate_build_contact_object(value, propname, func, param);
       
   577             //}
       
   578         }
       
   579         else {
       
   580             func(propname, value, param);
       
   581         }
       
   582     }
       
   583     //}
       
   584 }
       
   585 
       
   586 //Map from sp contact to device contact
       
   587 
       
   588 
       
   589 
       
   590 function __sp_device_contact_extract(name, value, contact){
       
   591 	
       
   592     if (name == "id") {
       
   593         contact.id = value;
       
   594         return;
       
   595     }
       
   596     
       
   597     // We only care about the Value fields
       
   598     if (!name.match(".Value")) {
       
   599         return;
       
   600     }
       
   601     //__device_debug("sp_device_contact_extract: " + name + ": " + value);
       
   602     
       
   603     try {
       
   604         if (name.match("IMPP.Value.0")) {
       
   605             contact.xspid = [];
       
   606         }
       
   607         if (name.match("Name")) {
       
   608             if (name.match("CompanyName")) {
       
   609                 if (!contact.company) {
       
   610                     contact.company = {};
       
   611                 }
       
   612             }
       
   613             else 
       
   614                 if (!contact.name) {
       
   615                     contact.name = {};
       
   616                 }
       
   617         }
       
   618         else 
       
   619             if (name.match("Phone") || name.match("Number") || name.match("VOIP") || name.match("Voip")) {
       
   620                 if (!contact.tel) {
       
   621                     contact.tel = {};
       
   622                 }
       
   623                 if (name.match("Home")) {
       
   624                     if (!contact.tel.home) {
       
   625                         contact.tel.home = {};
       
   626                     }
       
   627                 }
       
   628                 else 
       
   629                     if (name.match("Work")) {
       
   630                         if (!contact.tel.work) {
       
   631                             contact.tel.work = {};
       
   632                         }
       
   633                     }
       
   634             }
       
   635             else 
       
   636                 if (name.match("Addr") || name.match("Email") || name.match("URL")) {
       
   637                     if (!contact.address) {
       
   638                         contact.address = {};
       
   639                     }
       
   640                     if (name.match("Home")) {
       
   641                         if (!contact.address.home) {
       
   642                             contact.address.home = {};
       
   643                         }
       
   644                     }
       
   645                     else 
       
   646                         if (name.match("Work")) {
       
   647                             if (!contact.address.work) {
       
   648                                 contact.address.work = {};
       
   649                             }
       
   650                         }
       
   651                 }
       
   652                 else 
       
   653                     if (name.match("JobTitle")) {
       
   654                         if (!contact.company) {
       
   655                             contact.company = {};
       
   656                         }
       
   657                     }
       
   658     } 
       
   659     catch (e) {
       
   660         __device_handle_exception(e, "__sp_device_contact_extract: " + e);
       
   661     }
       
   662     if (name.match("IMPP.Value")) {
       
   663         var splitstr = name.split(".");
       
   664         var index = splitstr[2];
       
   665         if (index != undefined) {
       
   666             contact.xspid[splitstr[2]] = value;
       
   667             //alert("after assigning" + contact.xspid[splitstr[2]]);
       
   668             return;
       
   669         }
       
   670     }
       
   671     switch (name) {
       
   672         case "LastName.Value":
       
   673             contact.name.last = value;
       
   674             break;
       
   675         case "FirstName.Value":
       
   676             contact.name.first = value;
       
   677             break;
       
   678         case "MiddleName.Value":
       
   679             contact.name.middle = value;
       
   680             break;
       
   681         case "Prefix.Value":
       
   682             contact.name.prefix = value;
       
   683             break;
       
   684         case "Suffix.Value":
       
   685             contact.name.suffix = value;
       
   686             break;
       
   687         case "LandPhoneGen.Value":
       
   688             contact.tel.land = value;
       
   689             break;
       
   690         case "MobilePhoneGen.Value":
       
   691             contact.tel.mobile = value;
       
   692             break;
       
   693         case "VideoNumberGen.Value":
       
   694             contact.tel.video = value;
       
   695             break;
       
   696         case "FaxNumberGen.Value":
       
   697             contact.tel.fax = value;
       
   698             break;
       
   699         case "VOIPGen.Value":
       
   700 		//alert(" voip gen= "+value);
       
   701             contact.tel.voip = value;
       
   702             break;
       
   703         case "LandPhoneHome.Value":
       
   704             contact.tel.home.land = value;
       
   705             break;
       
   706         case "MobilePhoneHome.Value":
       
   707             contact.tel.home.mobile = value;
       
   708             break;
       
   709         case "VideoNumberHome.Value":
       
   710             contact.tel.home.video = value;
       
   711             break;
       
   712         case "FaxNumberHome.Value":
       
   713             contact.tel.home.fax = value;
       
   714             break;
       
   715         case "VoipHome.Value":
       
   716 			//alert(" voip home Value = "+value);
       
   717             contact.tel.home.voip = value;
       
   718             break;
       
   719         case "LandPhoneWork.Value":
       
   720             contact.tel.work.land = value;
       
   721             break;
       
   722         case "MobilePhoneWork.Value":
       
   723             contact.tel.work.mobile = value;
       
   724             break;
       
   725         case "VideoNumberWork.Value":
       
   726             contact.tel.work.video = value;
       
   727             break;
       
   728         case "FaxNumberWork.Value":
       
   729             contact.tel.work.fax = value;
       
   730             break;
       
   731         case "VoipWork.Value":
       
   732 		//alert(" voip work= "+value);
       
   733             contact.tel.work.voip = value;
       
   734             break;
       
   735         case "AddrStreetGen.Value":
       
   736             contact.address.street = value;
       
   737             break;
       
   738         case "AddrLocalGen.Value":
       
   739             contact.address.local = value;
       
   740             break;
       
   741         case "AddrRegionGen.Value":
       
   742             contact.address.region = value;
       
   743             break;
       
   744         case "AddrPostCodeGen.Value":
       
   745             contact.address.code = value;
       
   746             break;
       
   747         case "AddrCountryGen.Value":
       
   748             contact.address.country = value;
       
   749             break;
       
   750         case "EmailGen.Value":
       
   751             contact.address.email = value;
       
   752             break;
       
   753         case "URLGen.Value":
       
   754             contact.address.uri = value;
       
   755             break;
       
   756         case "AddrStreetHome.Value":
       
   757             contact.address.home.street = value;
       
   758             break;
       
   759         case "AddrLocalHome.Value":
       
   760             contact.address.home.local = value;
       
   761             break;
       
   762         case "AddrRegionHome.Value":
       
   763             contact.address.home.region = value;
       
   764             break;
       
   765         case "AddrPostCodeHome.Value":
       
   766             contact.address.home.code = value;
       
   767             break;
       
   768         case "AddrCountryHome.Value":
       
   769             contact.address.home.country = value;
       
   770             break;
       
   771         case "EmailHome.Value":
       
   772             contact.address.home.email = value;
       
   773             break;
       
   774         case "URLHome.Value":
       
   775             contact.address.home.uri = value;
       
   776             break;
       
   777         case "AddrStreetWork.Value":
       
   778             contact.address.work.street = value;
       
   779             break;
       
   780         case "AddrLocalWork.Value":
       
   781             contact.address.work.local = value;
       
   782             break;
       
   783         case "AddrRegionWork.Value":
       
   784             contact.address.work.region = value;
       
   785             break;
       
   786         case "AddrPostCodeWork.Value":
       
   787             contact.address.work.code = value;
       
   788             break;
       
   789         case "AddrCountryWork.Value":
       
   790             contact.address.work.country = value;
       
   791             break;
       
   792         case "EmailWork.Value":
       
   793             contact.address.work.email = value;
       
   794             break;
       
   795         case "URLWork.Value":
       
   796             contact.address.work.uri = value;
       
   797             break;
       
   798         case "CompanyName.Value":
       
   799             contact.company.name = value;
       
   800             break;
       
   801         case "JobTitle.Value":
       
   802             contact.company.title = value;
       
   803             break;
       
   804         case "Note.Value":
       
   805             contact.notes = value;
       
   806             break;
       
   807         case "Anniversary.Value":
       
   808             contact.anniversary = value;
       
   809             break;
       
   810         case "Date.Value":
       
   811             contact.birthday = value;
       
   812             break;
       
   813         case "SecondName.Value":
       
   814             //alert("nickname value"+value);
       
   815             contact.nickName = value;
       
   816             //alert("after assigning"+contact.nickName);
       
   817             break;
       
   818         case "CallerObjImg.Value":
       
   819             contact.photo = value;
       
   820             break;
       
   821             
       
   822         default:
       
   823         //__device_debug("Default: " + value );
       
   824     }
       
   825 }
       
   826 
       
   827 function __sp_device_contact_build(sp_contact, contact){
       
   828 	//alert("In __sp_device_contact_build");
       
   829     __s60_enumerate_build_contact_object(sp_contact, null, __sp_device_contact_extract, contact);
       
   830 	//alert("After __s60_enumerate_build_contact_object");
       
   831 }
       
   832 
       
   833 /* ------ Wrap sp iterator ------- */
       
   834 function __sp_contact_iterator_get_next(){
       
   835     //__device_debug("__sp_contact_iterator_get_next"); 
       
   836 	//alert("Before calling iter.getnext  "); 
       
   837     var sp_contact = this.iter.getNext();
       
   838 	//alert("After calling iter.getnext  "); 
       
   839     if (typeof sp_contact == "undefined") {
       
   840 		//alert("sp_contact == undefined  "); 
       
   841         return null;
       
   842     }	
       
   843     var rval = {};	
       
   844     __sp_device_contact_build(sp_contact, rval);	
       
   845     sp_contact.close();	
       
   846     return rval;
       
   847 }
       
   848 
       
   849 function __sp_contact_iterator(js_iterator){
       
   850     this.iter = js_iterator;
       
   851 	//alert("before __sp_contact_iterator_get_next");
       
   852     this.next = __sp_contact_iterator_get_next;
       
   853 	//alert("After __sp_contact_iterator_get_next ");
       
   854     this.close = function(){
       
   855         this.iter.close();
       
   856     };
       
   857 }
       
   858 
       
   859 function __s60_enumerate_group_object(object, func, param){
       
   860     var key;
       
   861     for (key in object) {
       
   862         //if (object.hasOwnProperty('key')) {
       
   863         var propname = key;
       
   864         var value = object[key];
       
   865         func(propname, value, param);
       
   866         //}
       
   867     }
       
   868 }
       
   869 
       
   870 function __sp_device_groupinfo_extract(name, value, groupinfo){
       
   871 
       
   872     if (groupinfo.group == undefined) {
       
   873         groupinfo.group = {};
       
   874     }
       
   875     if (name == "id") {
       
   876         groupinfo.group.groupId = value;
       
   877     }
       
   878     else 
       
   879         if (name == "GroupLabel") {
       
   880             groupinfo.group.groupName = value;
       
   881         }
       
   882         else 
       
   883             if (name == "Contents") {
       
   884                 groupinfo.contents = value;
       
   885             }
       
   886     return;
       
   887 }
       
   888 
       
   889 function __sp_device_groupinfo_build(sp_group, groupinfo){
       
   890     __s60_enumerate_group_object(sp_group, __sp_device_groupinfo_extract, groupinfo);
       
   891 }
       
   892 
       
   893 
       
   894 
       
   895 //Build group data
       
   896 function __sp_device_group_extract(name, value, group){
       
   897     if (name == "id") {
       
   898         group.groupId = value;
       
   899         return;
       
   900     }
       
   901     if (name == "GroupLabel") {
       
   902         group.groupName = value;
       
   903         return;
       
   904     }
       
   905 }
       
   906 
       
   907 function __sp_device_group_build(sp_group, group){
       
   908     __s60_enumerate_object(sp_group, null, __sp_device_group_extract, group);
       
   909 }
       
   910 
       
   911 
       
   912 
       
   913 //Iterate the group data
       
   914 function __sp_group_iterator_get_next(){
       
   915     var sp_group = this.iter.getNext();
       
   916     if (typeof sp_group == "undefined") {
       
   917         return null;
       
   918     }
       
   919     var rval = {};
       
   920     __sp_device_group_build(sp_group, rval);
       
   921     sp_group.close();
       
   922     return rval;
       
   923 }
       
   924 
       
   925 function __sp_group_iterator(js_iterator){
       
   926     this.iter = js_iterator;
       
   927     this.next = __sp_group_iterator_get_next;
       
   928     this.close = function(){
       
   929         this.iter.close();
       
   930     };
       
   931 }
       
   932 
       
   933 /* --------- Public methods ---------- */
       
   934 
       
   935 
       
   936 var CONTACTS_APP_ID = 0x101f4cce;
       
   937 
       
   938 // Apps should take care that this is not reinvoked
       
   939 // while the editor is already running. 
       
   940 
       
   941 function __sp_contacts_startEditor(editor_cb, contact, ErrorCallback){
       
   942     if (!editor_cb) 
       
   943         throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: StartEditor:Missing Success Callback");
       
   944     
       
   945     if ((typeof editor_cb) != "function") 
       
   946         throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: StartEditor:Invalid Success Callback");
       
   947     
       
   948     if ((contact) == null) 
       
   949         contact = {};
       
   950     
       
   951     if ((typeof contact) == "undefined") 
       
   952         contact = {};
       
   953     
       
   954     if ((contact) == undefined) 
       
   955         contact = {};
       
   956     
       
   957     if ((typeof contact) != "object") {
       
   958         throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: StartEditor:Invalid Contact Data");
       
   959     }
       
   960     
       
   961     
       
   962     if (ErrorCallback) 
       
   963         if ((typeof ErrorCallback) != "function") 
       
   964             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts :StartEditor:Invalid Error Callback");
       
   965     
       
   966     var finished = function(arg1, arg2, arg3){
       
   967         var iter = null;
       
   968         editor_cb(iter);
       
   969         //editor_cb(arg1,arg2,arg3);
       
   970     };
       
   971     __s60_start_and_wait(CONTACTS_APP_ID, "", finished);
       
   972     return 0; //This value is returned as the transaction id is not supported
       
   973 }
       
   974 
       
   975 // TBD: add support for match patterns
       
   976 function __sp_contacts_getContacts_cb(arg1, arg2, arg3){
       
   977 	//alert(" __sp_contacts_getContacts_cb");
       
   978     var iter = null;
       
   979     var contacts_cb;
       
   980     var error_cb;
       
   981     var CbObj;
       
   982     
       
   983     //alert(" before getFromArray");
       
   984     CbObj = glob_obj.getFromArray(arg1);
       
   985 	//alert("after getFromArray");
       
   986     
       
   987     
       
   988     if (CbObj) {
       
   989 		//alert(" before CbObj.success_cb");
       
   990         contacts_cb = CbObj.success_cb;
       
   991 		//alert(" After CbObj.success_cb");
       
   992         error_cb = CbObj.error_cb;
       
   993 		//alert(" After CbObj.error_cb");
       
   994     }
       
   995     
       
   996     else {
       
   997         alert("Contacts: __sp_contacts_getContacts_cb : Callback not found ");
       
   998 		return;
       
   999     }
       
  1000     
       
  1001     if (arg3.ReturnValue) {
       
  1002         iter = new __sp_contact_iterator(arg3.ReturnValue);
       
  1003     }
       
  1004     else 
       
  1005         if (arg3.ErrorCode != 0) {
       
  1006             //alert(arg3.ErrorCode);
       
  1007             var err_msg;
       
  1008             if (arg3.ErrorMessage) 
       
  1009                 err_msg = splitErrorMessage(arg3.ErrorMessage);
       
  1010             else 
       
  1011                 err_msg = "Operation Failed";
       
  1012             
       
  1013             error_cb(new DeviceException(MapErrorCode[arg3.ErrorCode], "Contacts: getContacts: " + err_msg));
       
  1014             return;
       
  1015         }
       
  1016     contacts_cb(iter);
       
  1017     glob_obj.removeFromArray(arg1);
       
  1018 }
       
  1019 
       
  1020 function __sp_contacts_getContacts(contacts_cb, match, sortOrder, ErrorCallback){
       
  1021     //alert("__sp_contacts_getContacts");
       
  1022     var criteria = {};
       
  1023     modifyObjectBaseProp(criteria);
       
  1024     criteria.Type = 'Contact';
       
  1025     if (match) {
       
  1026         criteria.Filter = {};
       
  1027         modifyObjectBaseProp(criteria.Filter);
       
  1028         criteria.Filter.SearchVal = match;
       
  1029         
       
  1030     }
       
  1031     
       
  1032     //if (sortOrder) {
       
  1033 	if(sortOrder != null && sortOrder != undefined){		
       
  1034         criteria.Sort = {};
       
  1035         modifyObjectBaseProp(criteria.Sort);
       
  1036         if (typeof sortOrder == "number") {
       
  1037             if (sortOrder == this.SORT_ASCENDING) {
       
  1038                 criteria.Sort.Order = "Ascending";
       
  1039             }
       
  1040             else 
       
  1041                 if (sortOrder == this.SORT_DESCENDING) {
       
  1042                     criteria.Sort.Order = "Descending";
       
  1043                 }
       
  1044                 
       
  1045                 
       
  1046                 else {   
       
  1047 						//alert(" In datanotfound");                
       
  1048 					 ErrorCallback(new DeviceException(this.error.DATA_OUT_OF_RANGE_ERR, "Contacts: getContacts: invalid sortOrder input"));
       
  1049 					 return;				
       
  1050                 }
       
  1051         }
       
  1052         else {			
       
  1053             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContacts: invalid sortOrder input");
       
  1054         }
       
  1055     }
       
  1056     
       
  1057     try {
       
  1058     
       
  1059         if (!contacts_cb) {
       
  1060 			//alert(" ! contacts_cb");
       
  1061             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getContacts: callback is missing");
       
  1062         }
       
  1063         else 
       
  1064             if (typeof contacts_cb != "function") {
       
  1065                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContacts: callback is invalid");
       
  1066             }
       
  1067         if (ErrorCallback) {
       
  1068             if (typeof(ErrorCallback) != 'function') {
       
  1069                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContacts: ErrorCallback is invalid");
       
  1070             }
       
  1071         }
       
  1072         
       
  1073 			temp_scb = contacts_cb;
       
  1074 			temp_ecb = ErrorCallback;
       
  1075         var rval = this.so.IDataSource.GetList(criteria, this.getContactsCb);
       
  1076         //alert(" errorcode = "+rval.ErrorCode);
       
  1077         if (rval.TransactionID) {
       
  1078 			//alert(" if TransactionID ");
       
  1079             glob_obj.addToGlobalArray(rval.TransactionID, contacts_cb, ErrorCallback);
       
  1080         }
       
  1081         //alert("error code = "+rval.ErrorCode);	
       
  1082         if (criteria.Sort) {
       
  1083             delete criteria.Sort.Order;
       
  1084         }
       
  1085         if (criteria.Filter) {
       
  1086             delete criteria.Filter.SearchVal;
       
  1087         }
       
  1088         if (criteria) {
       
  1089             delete criteria.Type;
       
  1090             delete criteria.Filter;
       
  1091             delete criteria.Sort;
       
  1092         }
       
  1093         if (rval.ErrorCode != 0) {			
       
  1094             var doMap = mappingVerification(rval.ErrorCode);
       
  1095             var err_msg = null;
       
  1096             if (rval.ErrorMessage) {
       
  1097                 err_msg = splitErrorMessage(rval.ErrorMessage);
       
  1098             }
       
  1099             if (doMap) {
       
  1100                 switch (MapErrorCode[rval.ErrorCode]) {
       
  1101                     case this.error.MISSING_ARG_ERR:
       
  1102                     case this.error.INVALID_ARG_ERR:
       
  1103                     case this.error.NOT_SUPPORTED_ERR:  {						
       
  1104 						if (err_msg) {
       
  1105 							throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts: " + err_msg);
       
  1106 						}
       
  1107 						else {
       
  1108 							throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts:Operation Failed");
       
  1109 						}
       
  1110 					}
       
  1111                         break;
       
  1112                     default:
       
  1113                         //contacts_cb(0, MapErrorCode[rval.ErrorCode],0);
       
  1114                         //contacts_cb(iter);
       
  1115                         if (ErrorCallback != undefined) {
       
  1116                             if (err_msg) {
       
  1117                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts: " + err_msg));
       
  1118                             }
       
  1119                             else 
       
  1120                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts:Operation Failed "));
       
  1121                         }
       
  1122                         else {
       
  1123                             if (err_msg) 
       
  1124                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts: " + err_msg);
       
  1125                             else 
       
  1126                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContacts:Operation Failed");
       
  1127                         }
       
  1128                 }
       
  1129             }
       
  1130             else {
       
  1131                 if (ErrorCallback != undefined) {
       
  1132                     if (err_msg) {
       
  1133                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getContacts: " + err_msg));
       
  1134                     }
       
  1135                     else 
       
  1136                         ErrorCallback(new DeviceException(rval.ErrorCode, "getContacts:Operation Failed "));
       
  1137                 }
       
  1138                 else {
       
  1139                     if (err_msg) 
       
  1140                         throw new DeviceException(rval.ErrorCode, "Contacts: getContacts: " + err_msg);
       
  1141                     else 
       
  1142                         throw new DeviceException(rval.ErrorCode, "Contacts: getContacts:Operation Failed");
       
  1143                 }
       
  1144             }
       
  1145         }
       
  1146         return rval.TransactionID;
       
  1147     } 
       
  1148     catch (e) {
       
  1149         __device_handle_exception(e, "__sp_contacts_getContacts: " + e);
       
  1150     }
       
  1151 }
       
  1152 
       
  1153 function __sp_contacts_add(contact){
       
  1154 	
       
  1155 	if((contact) == null)
       
  1156 			throw new DeviceException(this.error.MISSING_ARG_ERR, 'addContact:Contact Data Needed');
       
  1157 			
       
  1158 	if((typeof contact) == "undefined")
       
  1159 		throw new DeviceException(this.error.MISSING_ARG_ERR, 'addContact:Contact Data Needed');
       
  1160 		
       
  1161 	if((contact) == undefined)
       
  1162 		throw new DeviceException(this.error.MISSING_ARG_ERR, 'addContact:Contact Data Needed');
       
  1163 		
       
  1164 	if ((typeof contact) != "object") {
       
  1165 		throw new DeviceException(this.error.INVALID_ARG_ERR, 'addContact:Invalid Contact Data');
       
  1166 	}
       
  1167 			
       
  1168     var sp_data = {};
       
  1169     modifyObjectBaseProp(sp_data);
       
  1170     __s60_enumerate_contact_object(contact, null, __sp_contact_extract, sp_data);
       
  1171     var criteria = {};
       
  1172     modifyObjectBaseProp(criteria);
       
  1173     criteria.Type = "Contact";
       
  1174     criteria.Data = sp_data;
       
  1175     try {
       
  1176         if (sp_data.id) {
       
  1177             throw new DeviceException(this.error.NOT_SUPPORTED_ERR, "Contacts: addContact: Id Not Supported");
       
  1178         }
       
  1179         var result = this.so.IDataSource.Add(criteria);
       
  1180         //alert("res.errorcode = "+result.ErrorCode);
       
  1181         if (sp_data) {
       
  1182             delete sp_data.id;
       
  1183         }
       
  1184         if (criteria) {
       
  1185             delete criteria.Type;
       
  1186             delete criteria.Data;
       
  1187         }
       
  1188         var cntId = result["ReturnValue"];
       
  1189         if (result.ErrorCode != 0) {
       
  1190             var doMap = mappingVerification(result.ErrorCode);
       
  1191             if (result.ErrorMessage) {
       
  1192                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1193                 if (doMap) {
       
  1194 					//alert("In do map");
       
  1195                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addContact: " + err_msg);
       
  1196                 }
       
  1197                 else {
       
  1198 					//alert("In do map else");
       
  1199                     throw new DeviceException(result.ErrorCode, "Contacts: addContact: " + err_msg);
       
  1200                 }
       
  1201             }
       
  1202             else {
       
  1203                 if (doMap) {
       
  1204                 
       
  1205                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addContact: Operation Failed");
       
  1206                 }
       
  1207                 else {
       
  1208                     throw new DeviceException(result.ErrorCode, "Contacts: addContact: Operation Failed");
       
  1209                 }
       
  1210             }
       
  1211         }
       
  1212         
       
  1213         
       
  1214         //Note: sp addContact no longer supports returning the new contact id
       
  1215         //__device_debug("sp_contacts_add returns: " + result);
       
  1216         //return result;
       
  1217         return cntId;
       
  1218     } 
       
  1219     catch (e) {
       
  1220         __device_handle_exception(e, "__sp_contacts_addContact: " + e);
       
  1221     }
       
  1222 }
       
  1223 
       
  1224 function __sp_contacts_update(contact){
       
  1225 
       
  1226     //alert("In  __sp_contacts_update");
       
  1227     try {
       
  1228         if (!(contact && contact.id)) {
       
  1229 			//alert(" contact && contact.id ");
       
  1230             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: updateContact: contact is missing");
       
  1231         }
       
  1232         var sp_data = {};
       
  1233         modifyObjectBaseProp(sp_data);
       
  1234         __s60_enumerate_contact_object(contact, null, __sp_contact_extract, sp_data);
       
  1235 	if (sp_data.id == undefined) {
       
  1236 			//alert("sp_data.id == undefined");
       
  1237             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: updateContact: Invalid id type");
       
  1238             
       
  1239         }
       
  1240         
       
  1241         if (typeof sp_data.id != "string") {
       
  1242 			//alert("typeof sp_data.id != string")
       
  1243             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: updateContact: Invalid id type");
       
  1244             
       
  1245         }
       
  1246         var criteria = {};
       
  1247         modifyObjectBaseProp(criteria);
       
  1248         criteria.Type = "Contact";
       
  1249         criteria.Data = sp_data;
       
  1250         var result = this.so.IDataSource.Add(criteria);
       
  1251         //alert("result = "+ result.ErrorCode);
       
  1252         if (sp_data) {
       
  1253             delete sp_data.id;
       
  1254         }
       
  1255         if (criteria) {
       
  1256             delete criteria.Type;
       
  1257             delete criteria.Data;
       
  1258         }
       
  1259         if (result.ErrorCode != 0) {
       
  1260             var doMap = mappingVerification(result.ErrorCode);
       
  1261             if (result.ErrorMessage) {
       
  1262                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1263                 if (doMap) {
       
  1264                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: updateContact: " + err_msg);
       
  1265                 }
       
  1266                 else {
       
  1267                     throw new DeviceException(result.ErrorCode, "Contacts: updateContact: " + err_msg);
       
  1268                 }
       
  1269             }
       
  1270             else {
       
  1271                 if (doMap) {
       
  1272                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: updateContact: Operation Failed");
       
  1273                 }
       
  1274                 else {
       
  1275                     throw new DeviceException(result.ErrorCode, "Contacts: updateContact: Operation Failed");
       
  1276                 }
       
  1277             }
       
  1278         }
       
  1279     } 
       
  1280     catch (e) {
       
  1281         __device_handle_exception(e, "__sp_contacts_updateContact: " + e);
       
  1282     }
       
  1283     
       
  1284 }
       
  1285 
       
  1286 function __sp_contacts_delete(id){
       
  1287     __device_debug("sp_contacts_delete id: " + id);
       
  1288     var contactdata = {};
       
  1289     modifyObjectBaseProp(contactdata);
       
  1290     if (id == undefined) {
       
  1291         throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: deleteContacts: idlist is missing");
       
  1292     }
       
  1293     
       
  1294     if (typeof id == "object") {
       
  1295         contactdata.IdList = id;
       
  1296     }
       
  1297     else {
       
  1298         contactdata.IdList = [];
       
  1299         contactdata.IdList[0] = id;
       
  1300         //delete contactdata.IdList;
       
  1301     }
       
  1302     var criteria = {};
       
  1303     modifyObjectBaseProp(criteria);
       
  1304     
       
  1305     criteria.Type = "Contact";
       
  1306     criteria.Data = contactdata;
       
  1307     
       
  1308     try {
       
  1309         var result = this.so.IDataSource.Delete(criteria);
       
  1310        // alert(" result.errorcode = "+result.ErrorCode);
       
  1311         if (contactdata) {
       
  1312             delete contactdata.IdList;
       
  1313         }
       
  1314         if (criteria) {
       
  1315             delete criteria.Type;
       
  1316             delete criteria.Data;
       
  1317         }
       
  1318         
       
  1319         if (result.ErrorCode != 0) {
       
  1320             var doMap = mappingVerification(result.ErrorCode);
       
  1321             if (result.ErrorMessage) {
       
  1322                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1323                 if (doMap) {
       
  1324                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: deleteContacts: " + err_msg);
       
  1325                 }
       
  1326                 else {
       
  1327                     throw new DeviceException(result.ErrorCode, "Contacts: deleteContacts: " + err_msg);
       
  1328                 }
       
  1329             }
       
  1330             else {
       
  1331                 if (doMap) {
       
  1332                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: deleteContacts: Operation Failed");
       
  1333                 }
       
  1334                 else {
       
  1335                     throw new DeviceException(result.ErrorCode, "Contacts: deleteContacts: Operation Failed");
       
  1336                 }
       
  1337             }
       
  1338         }
       
  1339     } 
       
  1340     catch (e) {
       
  1341         __device_handle_exception(e, "__sp_contacts_deleteContacts: " + e);
       
  1342     }
       
  1343 }
       
  1344 
       
  1345 function __sp_contacts_get(id){
       
  1346     //alert("__sp_contacts_get"+typeof id);
       
  1347     var criteria = {};
       
  1348     modifyObjectBaseProp(criteria);
       
  1349     criteria.Type = 'Contact';
       
  1350     
       
  1351     if (id == null) {
       
  1352         throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getContactInfo: id should not be null");
       
  1353     }
       
  1354     if ( typeof id != "string") {
       
  1355 		throw new DeviceException(this.error.INVALID_ARG_ERR, 'Contacts: getContactInfo: id should be string');
       
  1356 	}
       
  1357     criteria.Filter = {};
       
  1358     modifyObjectBaseProp(criteria.Filter);
       
  1359     criteria.Filter.id = id;
       
  1360     
       
  1361     try {
       
  1362         var rval = this.so.IDataSource.GetList(criteria);
       
  1363         //alert("rval .errorcode = "+rval.ErrorCode);
       
  1364         
       
  1365         if (criteria.Filter) {
       
  1366             delete criteria.Filter.id;
       
  1367         }
       
  1368         if (criteria) {
       
  1369             delete criteria.Type;
       
  1370             delete criteria.Filter;
       
  1371         }
       
  1372         if (rval) {
       
  1373             if (rval.ErrorCode != 0) {
       
  1374                 var doMap = mappingVerification(rval.ErrorCode);
       
  1375                 if (rval.ErrorMessage) {
       
  1376                     var err_msg = splitErrorMessage(rval.ErrorMessage);
       
  1377                     if (doMap) {
       
  1378                         throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactInfo: " + err_msg);
       
  1379                     }
       
  1380                     else {
       
  1381                         throw new DeviceException(rval.ErrorCode, "Contacts: getContactInfo: " + err_msg);
       
  1382                     }
       
  1383                 }
       
  1384                 else {
       
  1385                     if (doMap) {
       
  1386                         throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactInfo: Operation Failed");
       
  1387                     }
       
  1388                     else {
       
  1389                         throw new DeviceException(rval.ErrorCode, "Contacts: getContactInfo: Operation Failed");
       
  1390                     }
       
  1391                 }
       
  1392             }
       
  1393             var contact = {};
       
  1394             modifyObjectBaseProp(contact);
       
  1395             var sp_contact = rval.ReturnValue.getNext();
       
  1396             __sp_device_contact_build(sp_contact, contact);
       
  1397             return contact;
       
  1398         }
       
  1399         
       
  1400     } 
       
  1401     catch (e) {
       
  1402         __device_handle_exception(e, "__sp_contacts_getContactInfo:" + e);
       
  1403     }
       
  1404 }
       
  1405 
       
  1406 function __sp_contacts_addGroup(groupName){
       
  1407     //alert(" In __sp_contacts_addGroup");
       
  1408     var sp_data = {};
       
  1409     modifyObjectBaseProp(sp_data);
       
  1410     sp_data.GroupLabel = groupName;
       
  1411     
       
  1412     if (typeof(sp_data.GroupLabel) == null) {
       
  1413         throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: addGroups: callback is missing");
       
  1414     }
       
  1415     
       
  1416     
       
  1417     if ((typeof sp_data.GroupLabel) == "undefined") {
       
  1418         throw new DeviceException(this.error.MISSING_ARG_ERR, 'addGroup:Group Data Needed');
       
  1419     }
       
  1420     
       
  1421     if ((sp_data.GroupLabel) == undefined) {
       
  1422         throw new DeviceException(this.error.MISSING_ARG_ERR, 'addGroup:Group Data Needed');
       
  1423     }
       
  1424     
       
  1425     var criteria = {};
       
  1426     modifyObjectBaseProp(criteria);
       
  1427     criteria.Type = "Group";
       
  1428     criteria.Data = sp_data;
       
  1429     try {
       
  1430         var result = this.so.IDataSource.Add(criteria);
       
  1431         //alert(" In __sp_contacts_addGroup result = "+result.ErrorCode);
       
  1432         if (sp_data) {
       
  1433             delete sp_data.GroupLabel;
       
  1434         }
       
  1435         if (criteria) {
       
  1436             delete criteria.Type;
       
  1437             delete criteria.Data;
       
  1438         }
       
  1439         var grpId = result["ReturnValue"];
       
  1440         if (result.ErrorCode != 0) {
       
  1441             var doMap = mappingVerification(result.ErrorCode);
       
  1442             if (result.ErrorMessage) {
       
  1443                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1444                 if (doMap) {
       
  1445                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addGroup: " + err_msg);
       
  1446                 }
       
  1447                 else {
       
  1448                     throw new DeviceException(err_msg, result.ErrorCode, "Contacts: addGroup: Operation Failed ");
       
  1449                 }
       
  1450             }
       
  1451             else {
       
  1452                 if (doMap) {
       
  1453                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addGroup: Operation Failed");
       
  1454                 }
       
  1455                 else {
       
  1456                     throw new DeviceException(result.ErrorCode, "Contacts: addGroup: Operation Failed");
       
  1457                 }
       
  1458             }
       
  1459         }
       
  1460         return grpId;
       
  1461     } 
       
  1462     catch (e) {
       
  1463         __device_handle_exception(e, "__sp_contacts_addGroup: " + e);
       
  1464     }
       
  1465 }
       
  1466 
       
  1467 function __sp_contacts_getGroups_cb(arg1, arg2, arg3){
       
  1468     var iter = null;
       
  1469     var groups_cb;
       
  1470     var error_cb;
       
  1471     var CbObj;
       
  1472     
       
  1473     
       
  1474     CbObj = glob_obj.getFromArray(arg1);
       
  1475     
       
  1476     if (CbObj) {
       
  1477         groups_cb = CbObj.success_cb;
       
  1478         error_cb = CbObj.error_cb;
       
  1479     }
       
  1480     
       
  1481     else {
       
  1482         //alert("Contacts : __sp_contacts_getGroups_cb : Callback not found ");
       
  1483 		return;
       
  1484     }
       
  1485     
       
  1486     iter = arg3.ReturnValue;
       
  1487     if (arg3.ReturnValue) {
       
  1488         iter = new __sp_group_iterator(arg3.ReturnValue);
       
  1489     }
       
  1490     else 
       
  1491         if (arg3.ErrorCode != 0) {
       
  1492             var err_msg;
       
  1493             if (arg3.ErrorMessage) 
       
  1494                 err_msg = splitErrorMessage(arg3.ErrorMessage);
       
  1495             else 
       
  1496                 err_msg = "Operation Failed";
       
  1497             
       
  1498             error_cb(new DeviceException(err_msg, MapErrorCode[arg3.ErrorCode], "Contacts: getGroups:"));
       
  1499             return;
       
  1500         }
       
  1501     
       
  1502     groups_cb(iter);
       
  1503     glob_obj.removeFromArray(arg1);
       
  1504 }
       
  1505 
       
  1506 function __sp_contacts_getGroups(groups_cb, ErrorCallback){
       
  1507     //alert("In __sp_contacts_getGroups");
       
  1508     var criteria = {};
       
  1509     modifyObjectBaseProp(criteria);
       
  1510     criteria.Type = 'Group';
       
  1511     
       
  1512     try {
       
  1513         if (!groups_cb) {
       
  1514             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getGroups: callback is missing");
       
  1515         }
       
  1516         else 
       
  1517             if (typeof groups_cb != "function") {
       
  1518                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getGroups: callback is invalid");
       
  1519             }
       
  1520         if (ErrorCallback) {
       
  1521             if (typeof(ErrorCallback) != 'function') {
       
  1522                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getGroups: ErrorCallback is invalid");
       
  1523             }
       
  1524         }
       
  1525         
       
  1526         temp_scb = groups_cb;
       
  1527 		temp_ecb = ErrorCallback;
       
  1528         var rval = this.so.IDataSource.GetList(criteria, this.getGroupsCb);
       
  1529        // alert(" errorcode = "+rval.ErrorCode);
       
  1530         if (rval.TransactionID) {
       
  1531             glob_obj.addToGlobalArray(rval.TransactionID, groups_cb, ErrorCallback);
       
  1532         }
       
  1533         //alert("In __sp_contacts_getGroups result = "+rval.ErrorCode);
       
  1534         if (criteria) {
       
  1535             delete criteria.Type;
       
  1536         }
       
  1537         if (rval.ErrorCode != 0) {
       
  1538            // var doMap = mappingVerification(result.ErrorCode);
       
  1539 		   var doMap = mappingVerification(rval.ErrorCode);
       
  1540             var err_msg = null;
       
  1541             if (rval.ErrorMessage) {
       
  1542                 err_msg = splitErrorMessage(rval.ErrorMessage);
       
  1543             }
       
  1544             if (doMap) {
       
  1545                 switch (MapErrorCode[rval.ErrorCode]) {
       
  1546                     case this.error.MISSING_ARG_ERR:
       
  1547                     case this.error.INVALID_ARG_ERR:
       
  1548                     case this.error.NOT_SUPPORTED_ERR:
       
  1549                         if (err_msg) {
       
  1550                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroups: " + err_msg);
       
  1551                         }
       
  1552                         else {
       
  1553                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroups: Operation Failed");
       
  1554                         }
       
  1555                         break;
       
  1556                     default:
       
  1557                         //groups_cb(0,MapErrorCode[rval.ErrorCode] ,0) ;
       
  1558                         //groups_cb(iter) ;
       
  1559                         if (ErrorCallback != undefined) {
       
  1560                             if (err_msg) {
       
  1561                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroups: " + err_msg));
       
  1562                             }
       
  1563                             else 
       
  1564                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts:  getGroups: Operation Failed "));
       
  1565                         }
       
  1566                         else {
       
  1567                             if (err_msg) 
       
  1568                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroups: Operation Failed" + err_msg);
       
  1569                             else 
       
  1570                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroups: Operation Failed");
       
  1571                         }
       
  1572                         
       
  1573                 }
       
  1574             }
       
  1575             else {
       
  1576                 //groups_cb(0,rval.ErrorCode ,0) ;
       
  1577                 //groups_cb(iter) ;
       
  1578                 if (ErrorCallback != undefined) {
       
  1579                     if (err_msg) {
       
  1580                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getGroups: " + err_msg));
       
  1581                     }
       
  1582                     else 
       
  1583                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getGroups: Operation Failed"));
       
  1584                 }
       
  1585                 else {
       
  1586                     if (err_msg) 
       
  1587                         throw new DeviceException(rval.ErrorCode, "Contacts: getGroups: Operation Failed" + err_msg);
       
  1588                     else 
       
  1589                         throw new DeviceException(rval.ErrorCode, "Contacts: getGroups: Operation Failed");
       
  1590                 }
       
  1591             }
       
  1592         }
       
  1593         
       
  1594         return rval.TransactionID;
       
  1595     } 
       
  1596     catch (e) {
       
  1597         __device_handle_exception(e, "__sp_contacts_getGroups: " + e);
       
  1598     }
       
  1599 }
       
  1600 
       
  1601 function __sp_contacts_deleteGroups(id){
       
  1602     //alert("In __sp_contacts_deleteGroups");
       
  1603     var contactdata = {};
       
  1604     modifyObjectBaseProp(contactdata);
       
  1605     var IdList = [];
       
  1606     if (typeof id == "object") {
       
  1607         var i = 0;
       
  1608         for (var val in id) {
       
  1609             //	if (id.hasOwnProperty('val')) {
       
  1610             IdList[i] = id[val];
       
  1611             i++;
       
  1612             //}		
       
  1613         }
       
  1614     }
       
  1615     else {
       
  1616         IdList[0] = id;
       
  1617     }
       
  1618     contactdata.IdList = [];
       
  1619     contactdata.IdList = IdList;
       
  1620     var criteria = {};
       
  1621     modifyObjectBaseProp(criteria);
       
  1622     criteria.Type = "Group";
       
  1623     criteria.Data = contactdata;
       
  1624     
       
  1625     try {
       
  1626         var result = this.so.IDataSource.Delete(criteria);
       
  1627         //alert("In __sp_contacts_deleteGroups result = "+result.ErrorCode);
       
  1628         /*if (contactdata) {
       
  1629          delete contactdata.IdList;
       
  1630          }
       
  1631          if (criteria) {
       
  1632          delete criteria.Type;
       
  1633          delete criteria.Data;
       
  1634          }*/
       
  1635         if (result.ErrorCode != 0) {
       
  1636             var doMap = mappingVerification(result.ErrorCode);
       
  1637             if (result.ErrorMessage) {
       
  1638                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1639                 if (doMap) {
       
  1640                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: deleteGroups: " + err_msg);
       
  1641                 }
       
  1642                 else {
       
  1643                     throw new DeviceException(result.ErrorCode, "Contacts: deleteGroups: " + err_msg);
       
  1644                 }
       
  1645             }
       
  1646             else {
       
  1647                 if (doMap) {
       
  1648                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: deleteGroups: Operation Failed");
       
  1649                 }
       
  1650                 else {
       
  1651                     throw new DeviceException(result.ErrorCode, "Contacts: deleteGroups: Operation Failed");
       
  1652                 }
       
  1653             }
       
  1654         }
       
  1655     } 
       
  1656     catch (e) {
       
  1657         __device_handle_exception(e, "__sp_contacts_deleteGroups: " + e);
       
  1658     }
       
  1659 }
       
  1660 
       
  1661 function __sp_contacts_addContactsToGroup(groupId, id1){
       
  1662     //alert("__sp_contacts_addContactsToGroup");
       
  1663     try {
       
  1664         var criteria = {};
       
  1665         modifyObjectBaseProp(criteria);
       
  1666         var mapFound = false;
       
  1667         criteria.Type = "Group";
       
  1668         var map = {};
       
  1669         modifyObjectBaseProp(map);
       
  1670         //	if (id1 != undefined) {			
       
  1671         var IdList = [];
       
  1672         if (typeof id1 == "object") {
       
  1673             var i = 0;
       
  1674             for (var val in id1) {
       
  1675                 //if (id1.hasOwnProperty('val')) {						
       
  1676                 IdList[i] = id1[val];
       
  1677                 i++;
       
  1678                 //	}					
       
  1679             }
       
  1680         }
       
  1681         else {
       
  1682             IdList[0] = id1;
       
  1683         }
       
  1684         map.IdList = IdList;
       
  1685         mapFound = true;
       
  1686         delete IdList.index;
       
  1687         //	} 
       
  1688         if (groupId != undefined) {
       
  1689             map.id = groupId;
       
  1690             mapFound = true;
       
  1691         }
       
  1692         if (mapFound) {
       
  1693             criteria.Data = map;
       
  1694         }
       
  1695         
       
  1696         criteria.OperationType = "Associate";
       
  1697         var result = this.so.IDataSource.Organise(criteria);
       
  1698        // alert("__sp_contacts_addContactsToGroup result.errorCode = "+result.ErrorCode);
       
  1699         if (map) {
       
  1700             delete map.id;
       
  1701             delete map.IdList;
       
  1702         }
       
  1703         if (criteria) {
       
  1704             delete criteria.Data;
       
  1705             delete criteria.OperationType;
       
  1706             delete criteria.Type;
       
  1707         }
       
  1708         if (result.ErrorCode != 0) {
       
  1709             var doMap = mappingVerification(result.ErrorCode);
       
  1710             if (result.ErrorMessage) {
       
  1711                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  1712                 if (doMap) {
       
  1713                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addContactsToGroup: " + err_msg);
       
  1714                 }
       
  1715                 else {
       
  1716                     throw new DeviceException(result.ErrorCode, "Contacts: addContactsToGroup: " + err_msg);
       
  1717                 }
       
  1718             }
       
  1719             else {
       
  1720                 if (doMap) {
       
  1721                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: addContactsToGroup: Operation Failed");
       
  1722                 }
       
  1723                 else {
       
  1724                     throw new DeviceException(result.ErrorCode, "Contacts: addContactsToGroup: Operation Failed");
       
  1725                 }
       
  1726             }
       
  1727         }
       
  1728     } 
       
  1729     catch (e) {
       
  1730         __device_handle_exception(e, "__sp_contacts_addContactsToGroup: " + e);
       
  1731     }
       
  1732 }
       
  1733 
       
  1734 function __sp_contacts_getContactIds_cb(arg1, arg2, arg3){
       
  1735     var item = null;
       
  1736     var idList = null;
       
  1737     var contacts_cb;
       
  1738     var error_cb;
       
  1739     var CbObj;
       
  1740     
       
  1741     
       
  1742     CbObj = glob_obj.getFromArray(arg1);
       
  1743     
       
  1744     
       
  1745     if (CbObj) {
       
  1746         contacts_cb = CbObj.success_cb;
       
  1747         error_cb = CbObj.error_cb;
       
  1748     }
       
  1749     
       
  1750     else {
       
  1751         alert("Contacts: __sp_contacts_getContactIds_cb : Callback not found ");
       
  1752 		return;
       
  1753     }
       
  1754     
       
  1755     var id = null;
       
  1756     if (arg3["ReturnValue"] != undefined) {
       
  1757         item = arg3["ReturnValue"];
       
  1758         id = item["IdList"];
       
  1759         idList = [];
       
  1760         idList = id;
       
  1761         
       
  1762         delete idList.index;
       
  1763     }
       
  1764     else 
       
  1765         if (arg3.ErrorCode != 0) {
       
  1766             var err_msg;
       
  1767             if (arg3.ErrorMessage) 
       
  1768                 err_msg = splitErrorMessage(arg3.ErrorMessage);
       
  1769             else 
       
  1770                 err_msg = "Operation Failed";
       
  1771             
       
  1772             error_cb(new DeviceException(MapErrorCode[arg3.ErrorCode], "Contacts: getContactIds:" + err_msg));
       
  1773             return;
       
  1774         }
       
  1775     //var error = arg3.ErrorCode;
       
  1776     contacts_cb(idList);
       
  1777     glob_obj.removeFromArray(arg1);
       
  1778 }
       
  1779 
       
  1780 function __sp_contacts_getContactIds(contacts_cb, match, sortOrder, ErrorCallback){
       
  1781     //alert("__sp_contacts_getContactIds sortOrder =  " +sortOrder);	
       
  1782     var criteria = {};
       
  1783    // var errcb = ErrorCallback;
       
  1784     modifyObjectBaseProp(criteria);
       
  1785     criteria.Type = 'Contact';
       
  1786     
       
  1787     if (match) {
       
  1788         criteria.Filter = {};
       
  1789         modifyObjectBaseProp(criteria.Filter);
       
  1790         criteria.Filter.SearchVal = match;
       
  1791     }
       
  1792     
       
  1793 //    if (sortOrder) {
       
  1794   if(sortOrder != null && sortOrder != undefined){
       
  1795         criteria.Sort = {};
       
  1796         modifyObjectBaseProp(criteria.Sort);
       
  1797         if (typeof sortOrder == "number") {
       
  1798             if (sortOrder == this.SORT_ASCENDING) {
       
  1799                 criteria.Sort.Order = "Ascending";
       
  1800             }
       
  1801             else 
       
  1802                 if (sortOrder == this.SORT_DESCENDING) {
       
  1803                     criteria.Sort.Order = "Descending";
       
  1804                 }
       
  1805                 else {
       
  1806                    // criteria.Sort.Order = sortOrder; // sapi is taking care of this
       
  1807 				    ErrorCallback(new DeviceException(this.error.DATA_OUT_OF_RANGE_ERR, "Contacts: getContacts: invalid sortOrder input"));
       
  1808 					return;
       
  1809                 }
       
  1810         }
       
  1811         else {
       
  1812             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContactIds: SortOrder is Invalid");
       
  1813         }
       
  1814         
       
  1815     }
       
  1816     
       
  1817     try {
       
  1818         if (!contacts_cb) {
       
  1819             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getContactIds: callback is missing");
       
  1820         }
       
  1821         
       
  1822         else 
       
  1823             if (typeof contacts_cb != "function") {
       
  1824                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContactIds: callback is invalid");
       
  1825             }
       
  1826         if (ErrorCallback) {
       
  1827             if (typeof(ErrorCallback) != 'function') {
       
  1828                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getContactIds: ErrorCallback is invalid");
       
  1829             }
       
  1830         }
       
  1831         
       
  1832         temp_scb = contacts_cb;
       
  1833 		temp_ecb = ErrorCallback;
       
  1834         
       
  1835         var rval = this.so.IDataSource.GetIds(criteria, this.getContactIdsCb);
       
  1836         if (rval.TransactionID) {
       
  1837             glob_obj.addToGlobalArray(rval.TransactionID, contacts_cb, ErrorCallback);
       
  1838         }
       
  1839         //alert(" rval.errorcode = "+rval.ErrorCode);
       
  1840         /*if(criteria.Sort){
       
  1841          delete criteria.Sort.Order;
       
  1842          }
       
  1843          if(criteria.Filter){
       
  1844          delete criteria.Filter.SearchVal;
       
  1845          }
       
  1846          if (criteria) {
       
  1847          delete criteria.Filter;
       
  1848          delete criteria.Sort;
       
  1849          delete criteria.Type;
       
  1850          }*/
       
  1851         if (rval.ErrorCode != 0) {
       
  1852             var doMap = mappingVerification(rval.ErrorCode);
       
  1853             var err_msg = null;
       
  1854             if (rval.ErrorMessage) {
       
  1855                 var err_msg = splitErrorMessage(rval.ErrorMessage);
       
  1856             }
       
  1857             if (doMap) {
       
  1858                 switch (MapErrorCode[rval.ErrorCode]) {
       
  1859                     case this.error.MISSING_ARG_ERR:
       
  1860                     case this.error.INVALID_ARG_ERR:
       
  1861                     case this.error.NOT_SUPPORTED_ERR:
       
  1862                         if (err_msg) {
       
  1863                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds: " + err_msg);
       
  1864                         }
       
  1865                         else {
       
  1866                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds: Operation Failed");
       
  1867                         }
       
  1868                         break;
       
  1869                     default:
       
  1870                         //contacts_cb(0,MapErrorCode[rval.ErrorCode] ,0) ;
       
  1871                         //contacts_cb(idList);
       
  1872                         if (ErrorCallback != undefined) {
       
  1873                             if (err_msg) {
       
  1874                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds: " + err_msg));
       
  1875                             }
       
  1876                             else 
       
  1877                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds: Operation Failed "));
       
  1878                         }
       
  1879                         else {
       
  1880                             if (err_msg) {
       
  1881                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds:" + err_msg);
       
  1882                             }
       
  1883                             else 
       
  1884                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getContactIds: Operation Failed");
       
  1885                         }
       
  1886                 }
       
  1887             }
       
  1888             else {
       
  1889                 //contacts_cb(0,rval.ErrorCode ,0) ;
       
  1890                 //contacts_cb(idList);
       
  1891                 if (ErrorCallback != undefined) {
       
  1892                     if (err_msg) {
       
  1893                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getContactIds: " + err_msg));
       
  1894                     }
       
  1895                     else 
       
  1896                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getContactIds: Operation Failed "));
       
  1897                 }
       
  1898                 else {
       
  1899                     if (err_msg) {
       
  1900                         throw new DeviceException(rval.ErrorCode, "Contacts: getContactIds:" + err_msg);
       
  1901                     }
       
  1902                     else 
       
  1903                         throw new DeviceException(rval.ErrorCode, "Contacts: getContactIds: Operation Failed");
       
  1904                 }
       
  1905             }
       
  1906         }
       
  1907         return rval.TransactionID;
       
  1908     } 
       
  1909     catch (e) {
       
  1910         __device_handle_exception(e, "__sp_contacts_getContactIds: " + e);
       
  1911     }
       
  1912     
       
  1913 }
       
  1914 
       
  1915 function __sp_contacts_getGroupIds_cb(arg1, arg2, arg3){
       
  1916 	//alert("In __sp_contacts_getGroupIds_cb");
       
  1917     var error_cb = null;
       
  1918     var item = null;
       
  1919     var idList = [];
       
  1920     
       
  1921     var contacts_cb;
       
  1922     var error_cb;
       
  1923     var CbObj;
       
  1924     
       
  1925     //alert("Before getfromarray ");
       
  1926     CbObj = glob_obj.getFromArray(arg1);
       
  1927 	//alert("After getfromarray ");
       
  1928     
       
  1929     if (CbObj) {
       
  1930         contacts_cb = CbObj.success_cb;
       
  1931         error_cb = CbObj.error_cb;
       
  1932     }
       
  1933     
       
  1934     else {
       
  1935         //alert("Contacts: __sp_contacts_getGroupIds_cb : Callback not found ");
       
  1936 		return;
       
  1937     }
       
  1938     
       
  1939     
       
  1940     if (arg3.ReturnValue) {
       
  1941         item = arg3["ReturnValue"];
       
  1942         arg3.ReturnValue.close();
       
  1943         var id = item["IdList"];
       
  1944         idList = id;
       
  1945     }
       
  1946     else 
       
  1947         if (arg3.ErrorCode != 0) {
       
  1948             var err_msg;
       
  1949             if (rval.ErrorMessage) 
       
  1950                 err_msg = splitErrorMessage(rval.ErrorMessage);
       
  1951             else 
       
  1952                 err_msg = "Operation Failed";
       
  1953             
       
  1954             error_cb(new DeviceException(MapErrorCode[arg3.ErrorCode], "Contacts: getGroupIds:" + err_msg));
       
  1955             return;
       
  1956         }
       
  1957     //var error = arg3.ErrorCode;	
       
  1958     contacts_cb(idList);
       
  1959     delete idList.index;
       
  1960     glob_obj.removeFromArray(arg1);
       
  1961 }
       
  1962 
       
  1963 function __sp_contacts_getGroupIds(contacts_cb, ErrorCallback){	
       
  1964     var criteria = {};
       
  1965     modifyObjectBaseProp(criteria);
       
  1966     criteria.Type = 'Group';
       
  1967     
       
  1968     
       
  1969     try {
       
  1970         if (!contacts_cb) {
       
  1971             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getGroupIds: callback is missing");
       
  1972         }
       
  1973         else 
       
  1974             if (typeof contacts_cb != "function") {
       
  1975                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getGroupIds: callback is invalid");
       
  1976             }
       
  1977         if (ErrorCallback) {
       
  1978             if (typeof(ErrorCallback) != 'function') {
       
  1979                 throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getGroupIds: ErrorCallback is invalid");
       
  1980             }
       
  1981         }
       
  1982         
       
  1983         temp_scb = contacts_cb;
       
  1984 		temp_ecb = ErrorCallback;
       
  1985         
       
  1986         var rval = this.so.IDataSource.GetIds(criteria, this.getGroupIdsCb);
       
  1987 		//alert(" rval = "+rval.ErrorCode);
       
  1988         if (rval.TransactionID) {
       
  1989 			//alert("if TransactionID");
       
  1990             glob_obj.addToGlobalArray(rval.TransactionID, contacts_cb, ErrorCallback);
       
  1991         }
       
  1992         
       
  1993         if (criteria) {
       
  1994             delete criteria.Type;
       
  1995         }
       
  1996         if (rval.ErrorCode != 0) {
       
  1997         
       
  1998             var doMap = mappingVerification(rval.ErrorCode);
       
  1999             var err_msg = null;
       
  2000             if (rval.ErrorMessage) {
       
  2001                 var err_msg = splitErrorMessage(rval.ErrorMessage);
       
  2002             }
       
  2003             if (doMap) {
       
  2004                 switch (rval.ErrorCode) {
       
  2005                     case this.error.MISSING_ARG_ERR:
       
  2006                     case this.error.INVALID_ARG_ERR:
       
  2007                     case this.error.NOT_SUPPORTED_ERR:
       
  2008                         if (err_msg) {
       
  2009                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupIds: " + err_msg);
       
  2010                         }
       
  2011                         else {
       
  2012                             throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupIds: Operation Failed");
       
  2013                         }
       
  2014                         break;
       
  2015                     default:
       
  2016                         if (ErrorCallback != undefined) {
       
  2017                             if (err_msg) {
       
  2018                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupIds: " + err_msg));
       
  2019                             }
       
  2020                             else 
       
  2021                                 ErrorCallback(new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupIds: Operation Failed"));
       
  2022                         }
       
  2023                         else {
       
  2024                             if (err_msg) {
       
  2025                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: Contacts: getGroupIds:" + err_msg);
       
  2026                             }
       
  2027                             else 
       
  2028                                 throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupIds: Operation Failed");
       
  2029                         }
       
  2030                 }
       
  2031             }
       
  2032             
       
  2033             
       
  2034             else {
       
  2035                 if (ErrorCallback != undefined) {
       
  2036                     if (err_msg) {
       
  2037                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getGroupIds: " + err_msg));
       
  2038                     }
       
  2039                     else 
       
  2040                         ErrorCallback(new DeviceException(rval.ErrorCode, "Contacts: getGroupIds: Operation Failed"));
       
  2041                 }
       
  2042                 else {
       
  2043                     if (err_msg) {
       
  2044                         throw new DeviceException(rval.ErrorCode, "Contacts: Contacts: getGroupIds:" + err_msg);
       
  2045                     }
       
  2046                     else 
       
  2047                         throw new DeviceException(rval.ErrorCode, "Contacts: getGroupIds: Operation Failed");
       
  2048                 }
       
  2049             }
       
  2050         }
       
  2051 		//alert(" TransactionID = "+TransactionID);
       
  2052         return rval.TransactionID;
       
  2053     } 
       
  2054     catch (e) {
       
  2055         __device_handle_exception(e, "__sp_contacts_getGroupIds: " + e);
       
  2056     }
       
  2057     
       
  2058 }
       
  2059 
       
  2060 function __sp_contacts_removeContactsFromGroup(groupId, id){
       
  2061     //alert("__sp_contacts_removeContactsFromGroup");
       
  2062     try {
       
  2063         var criteria = {};
       
  2064         modifyObjectBaseProp(criteria);
       
  2065         var mapFound = false;
       
  2066         criteria.Type = "Group";
       
  2067         var map = {};
       
  2068         if (id != undefined) {
       
  2069             //alert("id != undefined");	
       
  2070             var IdList = [];
       
  2071             if (typeof id == "object") {
       
  2072                 var i = 0;
       
  2073                 for (var val in id) {
       
  2074                     //	if (id.hasOwnProperty('val')) {
       
  2075                     IdList[i] = id[val];
       
  2076                     i++;
       
  2077                     //	}
       
  2078                 }
       
  2079             }
       
  2080             else {
       
  2081                 //alert("id != undefined");
       
  2082                 IdList[0] = id;
       
  2083             }
       
  2084             map.IdList = IdList;
       
  2085             mapFound = true;
       
  2086             //delete IdList.index;
       
  2087         }
       
  2088         if (groupId != undefined) {
       
  2089             map.id = groupId;
       
  2090             mapFound = true;
       
  2091         }
       
  2092         if (mapFound) {
       
  2093             criteria.Data = map;
       
  2094         }
       
  2095         
       
  2096         criteria.OperationType = "Disassociate";
       
  2097         var result = this.so.IDataSource.Organise(criteria);
       
  2098         //alert("__sp_contacts_removeContactsFromGroup result"+result.ErrorCode);
       
  2099         if (map) {
       
  2100             delete map.IdList;
       
  2101         }
       
  2102         if (criteria) {
       
  2103             delete criteria.Data;
       
  2104             delete criteria.OperationType;
       
  2105             delete criteria.Type;
       
  2106         }
       
  2107         if (result.ErrorCode != 0) {
       
  2108             var doMap = mappingVerification(result.ErrorCode);
       
  2109             if (result.ErrorMessage) {
       
  2110                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  2111                 if (doMap) {
       
  2112                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: removeContactsFromGroup: " + err_msg);
       
  2113                 }
       
  2114                 else {
       
  2115                     throw new DeviceException(result.ErrorCode, "Contacts: removeContactsFromGroup: " + err_msg);
       
  2116                 }
       
  2117             }
       
  2118             else {
       
  2119                 if (doMap) {
       
  2120                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: removeContactsFromGroup: Operation Failed");
       
  2121                 }
       
  2122                 else {
       
  2123                     throw new DeviceException(result.ErrorCode, "Contacts: removeContactsFromGroup: Operation Failed");
       
  2124                 }
       
  2125             }
       
  2126         }
       
  2127     } 
       
  2128     catch (e) {
       
  2129         __device_handle_exception(e, "__sp_contacts_removeContactsFromGroup: " + e);
       
  2130     }
       
  2131 }
       
  2132 
       
  2133 function __sp_contacts_cancel(transactionId){
       
  2134     //alert("__sp_contacts_cancel");
       
  2135     try {
       
  2136         var criteria = {};
       
  2137         
       
  2138         if ((transactionId) == null) 
       
  2139             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: cancel: transactionId is missing " );
       
  2140         
       
  2141         if ((transactionId) == undefined) 
       
  2142             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: cancel:transactionId is undefined ");
       
  2143         
       
  2144         if ((typeof transactionId) == "undefined") 
       
  2145             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: cancel: transactionId is undefined ");
       
  2146         
       
  2147         if (((typeof transactionId) != "number") || (transactionId <= 0)) 
       
  2148             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: cancel: transactionId is Invalid" );
       
  2149         
       
  2150         modifyObjectBaseProp(criteria);
       
  2151         criteria.TransactionID = transactionId;
       
  2152         var result = this.so.IDataSource.Cancel(criteria);
       
  2153         //alert("result error code = " +result.ErrorCode);
       
  2154         if (criteria) {
       
  2155             delete criteria.TransactionID;
       
  2156         }
       
  2157         var errorcode = result["ErrorCode"];
       
  2158         if (errorcode != 0) {
       
  2159             var doMap = mappingVerification(result.ErrorCode);
       
  2160             if (result["ErrorMessage"]) {
       
  2161                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  2162                 if (doMap) {
       
  2163                     throw new DeviceException(MapErrorCode[errorcode], "Contacts: cancel: " + err_msg);
       
  2164                 }
       
  2165                 else {
       
  2166                     throw new DeviceException(errorcode, "Contacts: cancel: " + err_msg);
       
  2167                 }
       
  2168             }
       
  2169             else {
       
  2170                 if (doMap) {
       
  2171                     throw new DeviceException(MapErrorCode[errorcode], "Contacts: cancel: Operation Failed");
       
  2172                 }
       
  2173                 else {
       
  2174                     throw new DeviceException(errorcode, "Contacts: cancel: Operation Failed");
       
  2175                 }
       
  2176             }
       
  2177         }
       
  2178     } 
       
  2179     catch (e) {
       
  2180     
       
  2181         __device_handle_exception(e, "__sp_contacts_cancel: " + e);
       
  2182     }
       
  2183 }
       
  2184 
       
  2185 function __sp_contacts_updateGroup(group){
       
  2186     //alert("In __sp_contacts_updateGroup"+typeof group)
       
  2187     
       
  2188     if (!(group)) {
       
  2189         throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: updateGroup:Group Data is missing");
       
  2190     }
       
  2191     if ((typeof group) != "object") {
       
  2192         //alert(" not object");
       
  2193         throw new DeviceException(this.error.INVALID_ARG_ERR, 'Contacts: updateGroup:Invalid Group Data');
       
  2194     }
       
  2195     if (!(group && group.groupId)) {
       
  2196 			//alert(" contact && contact.id ");
       
  2197             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: updateGroup: Group id is missing");
       
  2198         }
       
  2199 	if(typeof group.groupId != "string")
       
  2200 	{
       
  2201 		   throw new DeviceException(this.error.INVALID_ARG_ERR, 'Contacts: updateGroup:Invalid Group id input');
       
  2202 	}
       
  2203     
       
  2204     try {
       
  2205         var criteria = {};
       
  2206         modifyObjectBaseProp(criteria);
       
  2207         criteria.Type = "Group";
       
  2208         var sp_data = {};
       
  2209         modifyObjectBaseProp(sp_data);
       
  2210         if (group != undefined) {
       
  2211             if (group.groupId != undefined) {
       
  2212                 sp_data["id"] = group.groupId;
       
  2213             }
       
  2214             else {
       
  2215                 throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: updateGroup: Id input is mandatory");
       
  2216             }
       
  2217             if (group.groupName != undefined) {
       
  2218                 sp_data["GroupLabel"] = group.groupName;
       
  2219             }
       
  2220         }
       
  2221         criteria.Data = sp_data;
       
  2222         
       
  2223         var result = this.so.IDataSource.Add(criteria);
       
  2224         //alert("In __sp_contacts_updateGroup result = "+result.ErrorCode);
       
  2225         if (sp_data) {
       
  2226             delete sp_data.prototype;
       
  2227         }
       
  2228         if (criteria) {
       
  2229             delete criteria.Data;
       
  2230             delete criteria.Type;
       
  2231         }
       
  2232         if (result.ErrorCode != 0) {
       
  2233             var doMap = mappingVerification(result.ErrorCode);
       
  2234             if (result.ErrorMessage) {
       
  2235             
       
  2236                 var err_msg = splitErrorMessage(result.ErrorMessage);
       
  2237                 if (doMap) {
       
  2238                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: updateGroup: " + err_msg);
       
  2239                 }
       
  2240                 else {
       
  2241                 
       
  2242                     throw new DeviceException(result.ErrorCode, "Contacts: updateGroup: " + err_msg);
       
  2243                 }
       
  2244             }
       
  2245             else {
       
  2246                 if (doMap) {
       
  2247                 
       
  2248                     throw new DeviceException(MapErrorCode[result.ErrorCode], "Contacts: updateGroup: Operation Failed");
       
  2249                 }
       
  2250                 else {
       
  2251                 
       
  2252                     throw new DeviceException(result.ErrorCode, "Contacts: updateGroup: Operation Failed");
       
  2253                 }
       
  2254             }
       
  2255         }
       
  2256     } 
       
  2257     catch (e) {
       
  2258     
       
  2259         __device_handle_exception(e, "__sp_contacts_updateGroup: " + e);
       
  2260     }
       
  2261 }
       
  2262 
       
  2263 
       
  2264 function __sp_contacts_getGroupInfo(grpid){
       
  2265     try {
       
  2266         var criteria = {};
       
  2267         modifyObjectBaseProp(criteria);
       
  2268         criteria.Type = 'Group';
       
  2269         
       
  2270         criteria.Filter = {};
       
  2271         modifyObjectBaseProp(criteria.Filter);
       
  2272         if (grpid == undefined) {
       
  2273             throw new DeviceException(this.error.MISSING_ARG_ERR, "Contacts: getGroupInfo: Id input is mandatory");
       
  2274         }
       
  2275         
       
  2276         if ((typeof grpid) != "string") 
       
  2277             throw new DeviceException(this.error.INVALID_ARG_ERR, "Contacts: getGroupInfo: Id input is mandatory");
       
  2278         criteria.Filter.id = grpid;
       
  2279         var rval = this.so.IDataSource.GetList(criteria);
       
  2280         if (criteria.Filter) {
       
  2281             delete criteria.Filter.id;
       
  2282         }
       
  2283         if (criteria) {
       
  2284             delete criteria.Filter;
       
  2285             delete criteria.Type;
       
  2286         }
       
  2287         if (rval) {
       
  2288             if (rval.ErrorCode != 0) {
       
  2289                 var doMap = mappingVerification(rval.ErrorCode);
       
  2290                 if (rval.ErrorMessage) {
       
  2291                     var err_msg = splitErrorMessage(rval.ErrorMessage);
       
  2292                     if (doMap) {
       
  2293                         throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupInfo: " + err_msg);
       
  2294                     }
       
  2295                     else {
       
  2296                         throw new DeviceException(rval.ErrorCode, "Contacts: getGroupInfo: " + err_msg);
       
  2297                     }
       
  2298                 }
       
  2299                 else {
       
  2300                     if (doMap) {
       
  2301                         throw new DeviceException(MapErrorCode[rval.ErrorCode], "Contacts: getGroupInfo: Operation Failed");
       
  2302                     }
       
  2303                     else {
       
  2304                         throw new DeviceException(rval.ErrorCode, "Contacts: getGroupInfo: Operation Failed");
       
  2305                     }
       
  2306                 }
       
  2307                 
       
  2308             }
       
  2309             else {
       
  2310                 var groupinfo = {};
       
  2311                 modifyObjectBaseProp(groupinfo);
       
  2312                 var sp_group = rval.ReturnValue.getNext();
       
  2313                 __sp_device_groupinfo_build(sp_group, groupinfo);
       
  2314                 return groupinfo;
       
  2315             }
       
  2316         }
       
  2317     } 
       
  2318     catch (e) {
       
  2319         __device_handle_exception(e, "__sp_contacts_getGroupInfo:" + e);
       
  2320     }
       
  2321 }
       
  2322 
       
  2323 
       
  2324 function mappingVerification(errorCode){
       
  2325     //alert ("In mappingVerification errorCode = "+errorCode);	
       
  2326     if (errorCode === 1016 || errorCode === 1012 || errorCode === 1010 || errorCode === 1009 || errorCode === 1005 || errorCode === 1000 || errorCode === 1011 || errorCode === 1007 || errorCode === 1003 || errorCode === 1002) {
       
  2327         //alert("errorCode = "+errorCode);
       
  2328         return true;
       
  2329     }
       
  2330     else {
       
  2331         //alert("In mappingVerification errorCode **else part**");
       
  2332         return false;
       
  2333     }
       
  2334 }
       
  2335 
       
  2336 
       
  2337 
       
  2338 function __sp_contacts_instance(){
       
  2339     //alert(" In __sp_contacts_instance");
       
  2340     
       
  2341   	 	this.SORT_ASCENDING = 0;
       
  2342   		this.SORT_DESCENDING = 1;
       
  2343     
       
  2344     //Descriptor
       
  2345     this.descriptor = new __sp_contacts_descriptor();
       
  2346     //Core methods
       
  2347     this.startEditor = __sp_contacts_startEditor;
       
  2348     //Extended methods
       
  2349     this.getContacts = __sp_contacts_getContacts;
       
  2350     this.getContactsCb = __sp_contacts_getContacts_cb;
       
  2351     
       
  2352     // this.getListSync = __sp_contacts_getListSync;
       
  2353     this.addContact = __sp_contacts_add;
       
  2354     this.updateContact = __sp_contacts_update;
       
  2355     this.deleteContacts = __sp_contacts_delete;
       
  2356     this.getContactInfo = __sp_contacts_get;
       
  2357     this.addGroup = __sp_contacts_addGroup;
       
  2358     
       
  2359     this.getGroups = __sp_contacts_getGroups;
       
  2360     this.getGroupsCb = __sp_contacts_getGroups_cb;
       
  2361     
       
  2362     this.deleteGroups = __sp_contacts_deleteGroups;
       
  2363     this.addContactsToGroup = __sp_contacts_addContactsToGroup;
       
  2364     
       
  2365     this.getContactIds = __sp_contacts_getContactIds;
       
  2366     this.getContactIdsCb = __sp_contacts_getContactIds_cb;
       
  2367     
       
  2368     this.getGroupIds = __sp_contacts_getGroupIds;
       
  2369     this.getGroupIdsCb = __sp_contacts_getGroupIds_cb;
       
  2370     
       
  2371     this.removeContactsFromGroup = __sp_contacts_removeContactsFromGroup;
       
  2372     this.cancel = __sp_contacts_cancel;
       
  2373     this.updateGroup = __sp_contacts_updateGroup;
       
  2374     this.getGroupInfo = __sp_contacts_getGroupInfo;
       
  2375     this.error = new DeviceException(0, "Dummy");
       
  2376     
       
  2377     try {
       
  2378         //__device_debug("Trying contacts service");
       
  2379         this.so = device.getServiceObject("Service.Contact", "IDataSource");
       
  2380         //__device_debug("Got contacts service object");    
       
  2381     } 
       
  2382     catch (e) {
       
  2383         __device_handle_exception(e, "Contacts service not available " + e);
       
  2384     }
       
  2385 }