/*
 * Common K-State functions
 */
 
YAHOO.namespace("ksu");

// As a shortcut, declare "KSU" as the same as "YAHOO.ksu"
var KSU = YAHOO.ksu;

KSU.lang = {

   trim:  function(s) {
      if (s===null) {
         return null;
      }   
      
      /**
       * The \A0 character in the following regular expressions are to allow the
       * non-breakable-space character to match as whitespace.  This character
       * can be created with "&nbsp;" in HTML, and then possibly copied into
       * JavaScript character strings.
       *
       * In Firefox 2.0.0.3 (at least, probably other versions),
       * the non-breakable-space character matches \s.
       *
       * In Internet Explorer 7.0.5730.11 (at least, probably other versions),
       * the non-breakable-space character does *not* match \s.
       */
      s = s.replace( /^[\s\xA0]+/g, "" );      // strip leading...
      return s.replace( /[\s\xA0]+$/g, "" );   // ...and trailing whitespace.
   },

   isEmpty: function(s) {
   
      if (s===null  ||  YAHOO.lang.isUndefined(s)) {
         return true;
      }
      
      /*
       * If the argument is a form field element, use the value of the field
       * rather than the field object itself.  Actually, this will happen with
       * any object that has a "value" property.
       */
      //if ((typeof s == "object")  &&  YAHOO.lang.hasOwnProperty(s, "value")) {
      //   s = s.value;
      //}
      if (!YAHOO.lang.isUndefined(s.value)) {
         s = s.value;
      }
      
      return s===null  ||  KSU.lang.trim(s)==="";
   },
   
   repeat: function(s, count) {

      if (count < 1) {
         return "";
      }
   
      var result = s;
      for ( ; count > 1; count--) {
         result += s;
      }
   
      return result;
   }
   
};


KSU.util = {

/*
 * parseRequest -- parse the parameters specified in the URL into a "request" object
 *
 * This function returns an object that has a property for each parameter
 * in the URL.  The value of the property is the value as specified in the
 * URL.  For example, with the URL "whatever.html?dog=Max&cat=Saturn",
 * the object returned would have two properties:
 *
 *    request.dog = "Max" and request.cat="Saturn".
 *
 * The returned object is typically named "request", and is modelled after the
 * Server-Side JavaScript "request" object.
 *
 * Note: No facility is available to handle parameters that are specified
 * multiple times.  The last value specified on the URL is the one returned.
 */
	parseRequest: function(s) {
	   var request = {};
	   var a, i, v;
	
	   // If the caller didn't specify a string, use the page's URL.
	   if (!s) {
	      s = window.location.search; 
	   }
	   
	   // Strip the leading question mark, if any
	   if (s.substr(0,1) == '?') {
	      s = s.substr(1);
	   }
	
	   // Split the query/search string into "name=value" strings.
	   a = s.split(/&/);
	
	   // For each string, split into the name and value, and assign to
	   // the request object.
	   for (i = 0; i < a.length; i++) {
	      v = a[i].match(/^([^=]*)=(.*)$/);
	      if (v) {
            v[1] = v[1].replace( /\+/g, " " );      // change '+' into ' '
            v[2] = v[2].replace( /\+/g, " " );      // change '+' into ' '
	         request[v[1]] = unescape(v[2]);
	      }
	   }
	
	   return request;
	
	}
};