/* useful JavaScript functions */


if (typeof STARMINE == 'undefined') {
    var STARMINE = {};
}

//---------------------------------------------------------------------------
STARMINE.Utilities = {

	isArray : function(o) {
	if (o.constructor.toString().indexOf('Array') == -1)
		return false;
	else
		return true;
	},

	//---------------------------------------------------------------------------
	displayObjectProperties : function(o) {

		var str = '';
		if (typeof o == 'object') {
			for(x in o) {
				str += "<b>" + x + "</b> -- " + o[ x ] + "<br /><br />\n";
			}
			var div = document.createElement('div');
			div.id = 'obj-properties';
			str = str || "No Properties To Display";
			div.innerHTML = str;

			div.style.textAlign = 'left';
			div.style.backgroundColor = '#eee';

			var body = document.getElementsByTagName("BODY")[0];
			body.appendChild(div);
		} else {
			alert("** displayObjectProperties ** \n" + o);
		}
	},

	//---------------------------------------------------------------------------
	namespace : function() { // adapted from YUI

	    var a=arguments, o=null, i, j, d;
	    for (i=0; i<a.length; i=i+1) {
	        d=a[i].split(".");
	        o=STARMINE;

	        // STARMINE is implied, so it is ignored if it is included
	        for (j=(d[0] == "STARMINE") ? 1 : 0; j<d.length; j=j+1) {
	            o[d[j]]=o[d[j]] || {};
	            o=o[d[j]];
	        }
	    }

	    return o;
	},

	//---------------------------------------------------------------------------
	buildUrl : function(target, parameters) {
	  var url = new Array();
	  for(attribute in parameters){
	    if(YAHOO.lang.isString(parameters[attribute]) || YAHOO.lang.isNumber(parameters[attribute])){
	      url.push(attribute + '=' + parameters[attribute]);
	    }
	  }

	  url = url.join('&');
	  url = encodeURI(url);

	  if (target == ''){
		target = document.location.toString();

		if (target.indexOf){
			var query_string_index = target.indexOf('?');

			if (query_string_index > 0){
				target = target.substr(0, query_string_index);
			}
          	}
	  }

	  url = target + '?' + url;

	  return url;
	},

	//---------------------------------------------------------------------------
	getName : function(limit, prompt_message, previous_name, source_label, uri_encode) {
  		possible_name = prompt(prompt_message, previous_name);
		limit = (limit < 1 ? 50 : limit);

		if (possible_name != null){
			possible_name = possible_name.replace(/["]/g, '').trim();
		}

  	  	if ((possible_name != null) && (possible_name != '') && (possible_name != previous_name)) {
      			while ((possible_name != null) && (possible_name != '') && (possible_name.length < 1 || possible_name.length > limit)) {
            			alert(source_label + " names must be between 1 and " + limit + " characters long.");
            			possible_name = prompt(prompt_message, possible_name);

				if (possible_name != null){
					possible_name = possible_name.replace(/["]/g, '').trim();
				}
      			}
		}


		if (possible_name != null && possible_name != '' && uri_encode){
			possible_name = encodeURIComponent(possible_name);
		}

		return possible_name;
	},

	//---------------------------------------------------------------------------
	in_array : function(what, where) {

		var a = false;
		var len = where.length;
	 	for (var i=0; i<len; i++) {
	 		if (what == where[i]) {
	 			a = true;
	 			break;
	 		}
	 	}
	 	return a;
	},

	//---------------------------------------------------------------------------
	// sorts an array and maintains index association
	asort : function(arr) {

		if (STARMINE.Utilities.isArray(arr)) {
			var vals_only = [];
			for (var key in arr) {
				vals_only.push(arr[String(key)]);
			}
			var sorted = vals_only.sort(function(x,y){
      						var a = String(x).toUpperCase();
      						var b = String(y).toUpperCase();
      						if (a > b)
         						return 1
      						if (a < b)
         						return -1
     				 		return 0;
    					});
			var new_array = [];
			for (var key in sorted) {
				for (var i in arr) {
					if (arr[i] == sorted[key]) {
						new_array[i] = arr[i];
						break;
					}
				}
			}
			return new_array;
		}
	},


  //---------------------------------------------------------------------------
  // inserts the option into the dropdown in the correct place alphabetically. if an existing option
  // has the same text as the inserted option, the inserted option is placed after the existing option.
  // bSelectOption: optional boolean, determines if inserted option should be selected (if not set
  // inserted option is selected by default.)
  insertNewOption : function(oDropDown, newOption, bSelectOption) {

    oDropDown.selectedIndex = -1;
    for(var i = 0; i < oDropDown.options.length; i++) {

      if ((oDropDown.options[i].innerText.localeCompare(newOption.innerText) == 1) ) {
        var insertedElement = oDropDown.insertBefore(newOption, oDropDown.options[i]);
        break;
      }

      var sharing_name = oDropDown.options[i].getAttribute('sharing_name');
      if ((sharing_name != '') && (sharing_name != null)) {
        // add option at the end of the list of owned views
        var insertedElement = oDropDown.insertBefore(newOption, oDropDown.options[i]);
        break;
      }
    }

    if (typeof insertedElement == 'undefined') {
      oDropDown.appendChild(newOption);
    }

    if (typeof bSelectOption == 'undefined') {
      bSelectOption = true;
    }

    newOption.selected = bSelectOption;
  }

};


//---------------------------------------------------------------------------
String.prototype.trim = function() { // usage: var test = "   Test   "; var test3 = test.trim(); // returns "Test"
   return this.replace(/^\s+|\s+$/g,"");
};

//---------------------------------------------------------------------------
String.prototype.stripTags = function () {
	return this.replace(/<[^>]+>/g,'');
};

//---------------------------------------------------------------------------
String.prototype.toArray = function() {
	return this.split('');
};

//---------------------------------------------------------------------------
//common shortuct for getElementById (note: if obj is not a string, return the obj)
$ = function(obj) {
  return (typeof obj == "string") ? document.getElementById(obj) : obj;
}
