/* queryString accepts the key and src arguments passed using a get method 
 * or as part of a hard coded URL
 * key the arg/var which value is to be isolated
 * src the URL with aoptional arguments
 */

function queryString(key, src) {
  //call to stripQuery to get portion of URL after the ?
  var __qstr = stripQuery(src);
  var strIndex = __qstr.indexOf(key+'=');
  if(strIndex == -1) return null;
  var strReturn = '', ch = '';
  //build value of StrReturn returns the value of the key as a string
  for(var i = strIndex + key.length; i < __qstr.length; i++) {
    ch = __qstr.charAt(i);
    if(ch == '&' || ch == ';') break;
    if(ch == '+') strReturn += ' ';
    else if(ch != '=') strReturn += ch;
  }
  return unescape(strReturn);
}


/* fileTyper is a function that strips the extension off of a filename
 * and returns the extension as an UpperCase string
*/
function fileTyper(src) {
 if(src == null) return null;
  var strIndex = src.indexOf('.');
  if(strIndex == -1) return null;
  var strReturn = '', ch = '';
  for(var i = strIndex+1; i <= src.length; i++) {
    ch = src.charAt(i);
    if(ch == '&' || ch == ';') break;
    if(ch == '+') strReturn += ' ';
    else if(ch != '=') strReturn += ch;
  }
  strReturn = strReturn.toUpperCase();
  return unescape(strReturn);
}

/* Given the source address (optional), returns the query string portion
 * of the address.  If the source address is not given, returns
 * the query string portion for current window/frame.
 *
 * Accepts src as a URL in string format or a Location object.
 */
function stripQuery(src) {
//alert('strip');
  if(src == null)
  	{
			return null;
	}else{
		  if(typeof src == 'string') {
			var __qstr   = new String();
			var __tmpNum = src.indexOf('?');
			
			__qstr = (__tmpNum != -1)
					 ? src.substr(
						 __tmpNum + 1, src.length
					   )
					 : null;
			
			delete __tmpNum;
			return __qstr;
		  }
		  else if(typeof src == 'object') { // assumes the object is of type location
			return location.search.substr(1, location.search.length);
		  }
		  else return null;
	}
}


/* Given an array of keys, an array of values, and a destination
 * address (optional), builds the query string on the end of
 * the destination address.  Any elements in the `valueRay'
 * beyond the length of the `keyRay' will be ignored.
 *
 * If the destination address is not passed, just builds the
 * query string.
 *
 * Elements in the `keyRay' should only be letters and numbers.
 *
 * Returns the new address.
 */
function formatQuery(keyRay, valueRay, destAddr) {
  var newAddr = ((destAddr == null) ? '' : destAddr) + '?';
  for(var i = 0; i < keyRay.length; i++) {
    newAddr += keyRay[i] + "=";
    if(valueRay[i] != null) newAddr += escape(valueRay[i]);
    if(i+1 < keyRay.length) newAddr += '&';
  }
  return newAddr;
}  



/* Currently not used --
//function fileDimensions(src) {
 //var __qstr = stripQuery(src);
  //var strIndex = src.substring(0,7);
  //alert(strIndex);
  //if(strIndex == -1) return null;
 // var strReturn = '', ch = '';

  //for(var i = strIndex+1; i <= src.length; i++) {
  //  ch = src.charAt(i);
  //  if(ch == '&' || ch == ';') break;
   // if(ch == '+') strReturn += ' ';
   // else if(ch != '=') strReturn += ch;
 // }
  //strReturn = strReturn.toUpperCase();
  //alert(strReturn);
  //return unescape(strReturn);
  //return(true);
//}
*/