function im_inArray(a,v)
{
   var i = a.length;
   var result = false;

   while ( i--  &&  !result ) {
     result = a[ i ] == v;
   }
   return result;
}

function im_trim(s)
{
   s = s.replace( /^\s+/g, '' );
   s = s.replace( /\s+$/g, '' );
   return s;
}

function im_clearStdText(o)
{
   if ( im_clearStdText.objList == null ) {
     im_clearStdText.objList = new Array();
   }
   if ( !im_inArray(im_clearStdText.objList, o) ) {
     var i = im_clearStdText.objList.length;
     im_clearStdText.objList[ i ] = o;
     o.value = '';
   }
}
im_clearStdText.objList = null;

function im_ignoreEnterKey(src,e)
{  var key;

   if (!e) e = window.event;
   if (e.keyCode) key = e.keyCode;
     else key = e.which;

   return key != 13;
}

function im_notEmpty(e)
{
   var s = e.value;
   s = im_trim( s );
   return (s.length > 0);
}

function im_oco_zoeken(e)
{
   var result = true;
   if ( e ) {
     result = im_notEmpty( e );
   }
   return result;
}

function im_findValueInListBox(listboxId,compareValue)
{  var  result = false;
   var  src = document.getElementById(listboxId);
   var  srcIndex = src.options.length;
   
   while ( !result  &&  srcIndex-- ) {
     var srcValue = src.options[ srcIndex ].value;
     result = srcValue == compareValue;
   }
   
   return result;
}

function im_copyElemSelect(srcId,destId)
{  var src = document.getElementById(srcId);
   var dest = document.getElementById(destId);

   var srcIndex = src.selectedIndex;

   if ( srcIndex >= 0 ) {
     var srcText  = src.options[ srcIndex ].text;
     var srcValue = src.options[ srcIndex ].value;

     if ( !im_findValueInListBox(destId,srcValue) ) {
       var newOption = document.createElement('option');
       newOption.text  = srcText;
       newOption.value = srcValue;

       dest.options[ dest.options.length ] = newOption;
     }
   }
}

function addEvent(obj,e,fun)
{ 
   var  result = false;

   if (obj.addEventListener) { 
     obj.addEventListener( e, fun, false );
     result = true;
   } else
   if (obj.attachEvent) {
     var r = obj.attachEvent( "on"+e, fun );
     result = r;
   }

   return result;
}

function getSourceFromEvent(e)
{
   if ( !e ) e = window.event;
   if ( e.target ) src = e.target;
     else src = e.srcElement; 
   return src;
}

function CGfxRatingItem()
{  this.reference = null;
   this.url_unselected = null;
   this.url_selected = null;
   this.value = null;
   
   this.clone = function() {
     var  item = new CGfxRatingItem();
     item.reference = this.reference;
     item.url_unselected = this.url_unselected;
     item.url_selected = this.url_selected;
     item.value = this.value;
     return item;
   }
}

function CGfxRating()
{  this.items = new Array();
   this.index = -1;
   this.index_hover = -1;
   this.count = 0;
   this.idField = null;
  
   this.add = function(item) {
     this.items[ this.count ] = item;
     this.count++;
     item.reference._owner = this;

     addEvent( item.reference, 'click', this.onclick );
     addEvent( item.reference, 'mousemove', this.onmousemove );
     addEvent( item.reference, 'mouseout', this.onmouseout );
   }

   this.get = function(index)
   {
      return this.items[ index ]; 
   }
   
   this.indexFromReference = function(ref)
   {  var  result = -1;
      var  index = this.count-1;
      
      while ( index >= 0  &&  result == -1 ) {
        var  item = this.get( index );
        if ( item.reference == ref ) result = index;
        index--;
      }
      
      return result;
   }
   
   this.copyValueToField = function() {
     if ( this.idField != null  &&  this.index >= 0 ) {
       var  item = this.get( this.index );
       this.idField.value = item.value;
     }
   }
   
   this.redraw = function() {
     for ( var i=0; i<this.count; i++ ) {
       var  item = this.get( i );
       
       if ( item != null  &&  item.reference != null ) {
         var  url = null;

         if ( this.index_hover >= 0 ) {
           if ( i > this.index_hover ) url = item.url_unselected;
             else url = item.url_selected;
         } else {
           if ( i <= this.index ) url = item.url_selected;
             else url = item.url_unselected;
         }

         item.reference.src = url;
       }
     }
   }
   
   this.onclick = function(e) {
     src = getSourceFromEvent( e );
     var index = src._owner.indexFromReference( src );
     src._owner.index = index;
     src._owner.copyValueToField();
     src._owner.redraw();
   }
   
   this.onmousemove = function(e) {
     src = getSourceFromEvent( e );
     var  index = src._owner.indexFromReference( src );
     src._owner.index_hover = index;
     src._owner.redraw();
   }
   
   this.onmouseout = function(e) {
     src = getSourceFromEvent( e );
     src._owner.index_hover = -1;
     src._owner.redraw();
   }
}
