/****************************************************************************
 * Copyright (C) 2009-2010 GGA Software Services LLC
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU Affero General Public License version 3 as published by the Free
 * Software Foundation and appearing in the file LICENSE.GPL included in
 * the packaging of this file.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ***************************************************************************/

if (!window.chem || !chem.Struct)
    throw new Error("chem.Molecule should be defined first");

chem.InChiSaver = function() {
};

chem.InChiSaver.prototype.saveMolecule = function(molecule) {
    var ret = '', err = null;
    if (ui.standalone) {
        throw { message : 'InChI is not supported in the standalone mode' };
    } else if (molecule.atoms.count() > 0) {
        molecule = molecule.clone();
        molecule.sgroups.each(function(sgid, sg) {
            if (sg.type != 'MUL') {
                throw { message : "InChi data format doesn't support s-groups" };
            }
        }, this);
        var moldata = new chem.MolfileSaver().saveMolecule(molecule);
        new Ajax.Request(ui.path + 'getinchi', {
            method: 'post',
            asynchronous : false,
            parameters: { moldata: moldata },
            onComplete: function (res) {
                if (res.responseText.startsWith('Ok.')) {
                    ret = res.responseText.split('\n')[1];
                } else if (res.responseText.startsWith('Error.')) {
                    err = { message : res.responseText.split('\n')[1] };
                } else {
                    err = { message : 'Unexpected server message (' + res.responseText + ')' };
                }
            }
        });
    }
    if (err) throw err; else return ret;
};
@@ -854,18 +854,16 @@
 ndalone)
- %7B
 %0A       
@@ -870,25 +870,23 @@
  throw %7B
- 
 message
- 
 : 'InChI
@@ -930,61 +930,224 @@
 ode'
- 
 %7D;%0A    
-%7D else if (molecule.atoms.count() %3E 0) %7B%0A    
+if (molecule.rgroups.count() !== 0)%0A        alert(%22R-group fragments are not supported and will be discarded%22);%0A    molecule = molecule.getScaffold();%0A    if (molecule.atoms.count() === 0)%0A        return ret;%0A
     
@@ -1175,20 +1175,16 @@
 lone();%0A
-    
     mole
@@ -1226,28 +1226,24 @@
 ) %7B%0A        
-  
-  
 if (sg.type 
@@ -1244,16 +1244,17 @@
 .type !=
+=
  'MUL') 
@@ -1271,28 +1271,22 @@
     
-  
-  
 throw %7B
- 
 message
- 
 : %22I
@@ -1327,17 +1327,16 @@
 -groups%22
- 
 %7D;%0A     
@@ -1334,34 +1334,26 @@
 %22%7D;%0A        
-    %7D%0A    
+%7D%0A
     %7D, this)
@@ -1354,20 +1354,16 @@
  this);%0A
-    
     var 
@@ -1420,20 +1420,16 @@
 ecule);%0A
-    
     new 
@@ -1473,20 +1473,16 @@
         
- 
-   
 method: 
@@ -1497,20 +1497,16 @@
         
-  
-  
 asynchro
@@ -1509,17 +1509,16 @@
 chronous
- 
 : false,
@@ -1526,20 +1526,16 @@
         
- 
-   
 paramete
@@ -1539,17 +1539,16 @@
 eters: %7B
- 
 moldata:
@@ -1555,17 +1555,16 @@
  moldata
- 
 %7D,%0A     
@@ -1566,20 +1566,16 @@
         
-    
 onComple
@@ -1586,17 +1586,16 @@
 function
- 
 (res) %7B%0A
@@ -1602,28 +1602,24 @@
             
-    
 if (res.resp
@@ -1664,20 +1664,16 @@
         
-    
 ret = re
@@ -1711,28 +1711,24 @@
             
- 
-   
 %7D else if (r
@@ -1783,36 +1783,30 @@
         
-    
 err = %7B
- 
 message
- 
 : res.re
@@ -1830,24 +1830,19 @@
 '%5Cn')%5B1%5D
- 
 %7D;%0A
-    
         
@@ -1854,20 +1854,16 @@
  else %7B%0A
-    
         
@@ -1877,25 +1877,23 @@
  err = %7B
- 
 message
- 
 : 'Unexp
@@ -1940,17 +1940,16 @@
 xt + ')'
- 
 %7D;%0A     
@@ -1959,40 +1959,28 @@
     
-    %7D%0A            
 %7D%0A    
+    %7D%0A
     %7D);%0A
     
@@ -1975,22 +1975,16 @@
     %7D);%0A
-    %7D%0A
     if (
@@ -1987,16 +1987,24 @@
 if (err)
+%0A       
  throw e
@@ -2006,21 +2006,20 @@
 row err;
- else
+%0A   
  return 

check for empty scaffold when saving to InChi (KETCHER-637)