String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

   function newAjaxRequest() {
      var xmlhttp = null;
      if(window.XMLHttpRequest){
          xmlhttp=new XMLHttpRequest();
          if(xmlhttp.overrideMimeType){
          xmlhttp.overrideMimeType('text/xml');
          }
      } else if( window.ActiveXObject ) {
          try {
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
             }
          }
       }
       return xmlhttp ;
   }
   
       function getProps( obj ) {
          var propList = '';
          for ( var props in obj ) {
              propList += (props + "=" + obj[props] + " " );
          }
          return propList ;
       }
       
       function dumpProps( obj ) {
          alert( getProps( obj ) );
       }
  

       function propNames( obj ) {
          var propList = '';
          for ( var props in obj ) {
              propList += (props + " ");
          }
          return propList;
       }


       function dumpPropNames( obj ) {
          alert( propNames( obj ) );
       }
 

       function getScrollBarWidth() {
         return 18;
         // this can be calculated by measuring the difference in 
         // a textarea's offsetHeight when changing wrap from off to soft.
       }
       function getCellPadding() {
          return 4;
       }
       
       
    function findPos(obj) {
        var curleft = curtop = 0;
    
        if (obj.offsetParent) {
           do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
           } while (obj = obj.offsetParent);
        }   
        return {x:curleft, y:curtop};
    }
       

       function getCssRule( selectorText ) {
          var theRules ;
          if (document.styleSheets[0].cssRules) {
          theRules = document.styleSheets[0].cssRules;
      } else if (document.styleSheets[0].rules) {
          theRules = document.styleSheets[0].rules;
          }
          else {
              alert( 'no css rules' );
              return null ;
          }
          for ( var r=0; r<theRules.length; r++ ) {
             var rule = theRules[r];
             if ( rule.selectorText == selectorText ) {
                 return rule;
             }
          }
          // didn't find an exact match, let's look for one ignoring case (cause IE converts types to uppercase)
          selectorText = selectorText.toLowerCase();
          for ( var r=0; r<theRules.length; r++ ) {
             var rule = theRules[r];
             if ( rule.selectorText.toLowerCase() == selectorText ) {
                 return rule;
             }
          }
          alert( 'did not find rule named ' + selectorText );
          return null ;
      }


      var LEFT_ARROW_KEY = 37;
      var UP_ARROW_KEY = 38;
      var RIGHT_ARROW_KEY = 39;
      var DOWN_ARROW_KEY = 40;
      var H_KEY = 72;
      var J_KEY = 74;
      var K_KEY = 75;
      var L_KEY = 76;
      
      var SEMICOLON_KEY = 186;
      
         
      function keyCode( event ) {
        if( window.event ) {
            return event.keyCode;
        }
        else if( event.which ) {
            return event.which;
        }
        return 0;
     }

     function randomInteger( min, max ) {
          return Math.floor( Math.random() * (max-min)) + min ;
     }
     
     function getSelectedValue( selectObj ) {
         return selectObj.options[ selectObj.selectedIndex ].value;
     }
     
     function setSelectedValue( selectObj, value ) {
          for ( var i =0; i<selectObj.options.length; i++ ) {
              if ( selectObj.options[i].value == value ) {
                   selectObj.selectedIndex = i;
                   return ;
              }
          }
          alert( 'value ' + value + ' not found.' );
     }

     function sendAjaxHighScoreMessage( studyStackId, gameId, score ) {
       var xmlhttp = newAjaxRequest();
       xmlhttp.onreadystatechange = function() {
           if (xmlhttp.readyState==4) {
              try {
                  var highScoreContent = xmlhttp.responseText;
                  document.getElementById( 'highScoreContainer' ).innerHTML = highScoreContent ;
                  document.getElementById( 'highScoreFrame' ).style.display = 'inline';
              }
              catch (err ) {
              }    
           }
       }
       
       var url = 'highScores.jsp?studyStackId=' + studyStackId + '&gameId=' + gameId + '&score=' + score;
       xmlhttp.open('GET', url, true);
       xmlhttp.send( 'text=123' );
   }

   function facebook_onlogin() {
     top.location = 'fbConnector.jsp';
   }
   function showLabel( obj ) {
      if ( obj.value.length == 0 ) {
          document.getElementById( obj.id + "Label" ).style.display = "inline";
      }
   }
  
   function hideLabel( obj ) {
      document.getElementById( obj.id + "Label" ).style.display = "none";
   }

   function enableObject( obj, enable  ) {
      if ( obj ) {
           obj.disabled = !enable ;
      }
   }

   var uniqueStatus;
   function checkUniqueValue( value, table, column, originalValue, buttonObj ) {
       uniqueStatus = document.getElementById( "uniqueStatus" );
       if ( value == originalValue ) {
           uniqueStatus.innerHTML = '';
           enableObject( buttonObj, true );
           return ;
       }
       if ( value == '' ) {
           showUniqueStatus( uniqueStatus, false );
           return ;
       }
       uniqueStatus.innerHTML = "<img src='images/spinner.gif' alt='checking'/>";
       var xmlhttp = newAjaxRequest();
       xmlhttp.onreadystatechange = function() {
           if (xmlhttp.readyState==4) {
              try {
                  var count = xmlhttp.responseText ;
                  showUniqueStatus( uniqueStatus, count <= 0, buttonObj );
              }
              catch (err ) {
              }
           }
       }
       var url = "countRecords.jsp" +
                  "?table=" + table +
                  "&whereClause=" + escape( column + "='" + value + "'");
       xmlhttp.open('GET', url, true);
       xmlhttp.send( 'text=text' );
    }
   
    function showUniqueStatus( obj, available, buttonObj ) {
       if ( !available ) {
             obj.innerHTML = "taken";
             obj.style.color = '#dd2222';
       }
       else {
             obj.innerHTML = "available" ;
             obj.style.color = '#00ff00';
       }
       enableObject( buttonObj, available );
    }

   function debug( message ) {
   }

