function _escapeSingleQ(item, keywords){

if( item == void( 0 ) || item.length == 0 )
  return "";

if( keywords != (void 0) ) {
  
  for(i=0; i < keywords.length; i++){
    if( item == keywords[i])
      return item;
  }

  if( item.length > 2 && item.substring(0,1) == "'" && item.substring( item.length-1, item.length) == "'") 
  {
    var innerItem = item.substring(1, item.length-1);
    for(i=0; i < keywords.length; i++){
      if( innerItem == keywords[i])
        return item;
    }
  }  
}

var o = "";
o = o + "'";
for( i=0; i < item.length; i++) 
{  

  if( item.substring(i, i +1) != "'")
    o = o + item.substring(i, i + 1);  
  else {
    o = o + "''"; 
  } 
}

o = o + "'";  

return o;
}

// item can have both index and value, or just index or just value
// format 1: (index) value
// format 2: (index)
//format 3: value

function _getItemIndexAndValue(item){

  var o = new Object();
  o.single = true;

  if( item.substring(0, 2) == "_v") {
    o.value = item.substring(2, item.length);
    o.index = "";
    return o;
  }
  
  
  if( item.substring(0, 2) ==  "_i" ) {
    o.index = item.substring(2, item.length);
    o.value = "";
    return o;
  }
  
  if( item.substring(0, 2) == "_b") {    
    item = item.substring(2, item.length);    
  }

  if( item.indexOf('(') != 0 ) {

    // value only
    o.value = item;
    o.index ="";
  }

  else if( item.lastIndexOf(")") == item.length - 1) {

    //index only
    o.index = item.substring(1, item.length-1);
    o.value = "";
  }

  else {

    //both index and value are present
    o.index = item.substring(1, item.indexOf(')'));
    o.value = item.substring(item.indexOf(')') + 2, item.length);
    o.single = false;
  }
  
  return o;  

}


// here, f is the form
// fien is the name of the LOV filed
// newValue is the new value for a single select LOV
// new value can be in the form of "(index1) value1;(index2) value2"
// or the new value can be in the form of "value1;value2"

function _setFieldValue(f, fien, rValue){

  if( rValue == (void 0) )
    return;

  var value = rValue.value;
  var index = rValue.index;
  var single = rValue.single;

  if( index == null || index == "") {
    //no index field

    for( var i=0; i < f.elements.length; i++){
    
      var e = f.elements[i];

      if(e.name == fien)
      {
        if( value != (void 0 ) )
          e.value = value;
        break;
      }
    }
  }

  else {
    //we have both index and value

    var lovField;
    var selectedIndex;    

    for( var i=0; i < f.elements.length; i++){

      var e = f.elements[i];
      if(e.name == fien)
      {
        lovField = e;
      }

      if( e.name == fien + "_choice"){
        selectedIndex = e.options.selectedIndex;
      }

      if( e.name == fien + "_value"  && single == false ) {
          e.value = value;
      }

      if( e.name == fien + "_index" && single == false) {
          e.value = index;
      }
    }
    if( selectedIndex == 0 ) {
      //the choice bean has "value" 
      if( value != (void 0) )
        lovField.value = value;
    }
    else {
      //the choice bean has "index" 
      if( index != (void 0) )
        lovField.value = index;    
    }
  }
}
