// Globals
// By default, Ronza is in the list
var INCLUDE_RONZA = true;

////////////////
// Prototypes //
////////////////

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


String.prototype._$$split = String.prototype._$$split || String.prototype.split;



String.prototype.split = function (s /* separator */, limit) {
	// if separator is not a regex, use the native split method
	if (!(s instanceof RegExp))
		return String.prototype._$$split.apply(this, arguments);

	var	flags = (s.global ? "g" : "") + (s.ignoreCase ? "i" : "") + (s.multiline ? "m" : ""),
		s2 = new RegExp("^" + s.source + "$", flags),
		output = [],
		origLastIndex = s.lastIndex,
		lastLastIndex = 0,
		i = 0, match, lastLength;

	/* behavior for limit: if it's...
	- undefined: no limit
	- NaN or zero: return an empty array
	- a positive number: use limit after dropping any decimal
	- a negative number: no limit
	- other: type-convert, then use the above rules
	*/
	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit)
			return [];
	}

	if (s.global)
		s.lastIndex = 0;
	else
		s = new RegExp(s.source, "g" + flags);

	while ((!limit || i++ <= limit) && (match = s.exec(this))) {
		var emptyMatch = !match[0].length;

		// Fix IE's infinite-loop-resistant but incorrect lastIndex
		if (emptyMatch && s.lastIndex > match.index)
			s.lastIndex--;

		if (s.lastIndex > lastLastIndex) {
			// Fix browsers whose exec methods don't consistently return undefined for non-participating capturing groups
			if (match.length > 1) {
				match[0].replace(s2, function () {
					for (var j = 1; j < arguments.length - 2; j++) {
						if (arguments[j] === undefined)
							match[j] = undefined;
					}
				});
			}

			output = output.concat(this.slice(lastLastIndex, match.index));
			if (1 < match.length && match.index < this.length)
				output = output.concat(match.slice(1));
			lastLength = match[0].length; // only needed if s.lastIndex === this.length
			lastLastIndex = s.lastIndex;
		}

		if (emptyMatch)
			s.lastIndex++; // avoid an infinite loop
	}

	// since this uses test(), output must be generated before restoring lastIndex
	output = lastLastIndex === this.length ?
		(s.test("") && !lastLength ? output : output.concat("")) :
		(limit ? output : output.concat(this.slice(lastLastIndex)));
	s.lastIndex = origLastIndex; // only needed if s.global, else we're working with a copy of the regex
	return output;
};

// Create a contains function for arrays to identify if an array contains a certain value.
Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}

// Create a implode function for arrays to convert itself into a delimited string
Array.prototype.implode = function(delim) {
  if (delim==null) delim=",";
  var str = "";
  for (var i=0; i<this.length; i++) {
    if (str.length>0) str+=delim;
    str+=this[i];
  }
  return str;
}

// If Array.indexOf does not exist (like in IE), create it.
if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
    for(var i=0; i<this.length; i++){
      if(this[i]==obj) return i;
    }
    return -1;
  }
}

String.prototype.trim = function() {
	var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.substring(0, i + 1);
}

///////////////////////
// Generic Functions //
///////////////////////

// Reads a select and returns either the selected id or an array of the selected ids.
function readSelect(selectId) {
  var sel = document.getElementById(selectId);
  var op = [];
  if (sel.multiple) {
    for (var i=0; i<sel.length; i++) {
      if (sel.options[i].selected) {
        op[op.length] = sel.options[i].value;
      }
    }
  } else {
    selIndex = sel.length;
    op = sel[sel.selectedIndex].value;
  }
  return op;
}

// Reads a set of checkboxes labeled <SomeName># and returns an array of the selected checkboxes.
function readCheckboxes(checkboxId, startN, endN) {
  var op = [];
  for (var i=startN; i<=endN; i++) {
    if (document.getElementById(checkboxId+""+i).checked) {
      op[op.length] = i;
    }
  }
  return op;
}

// Converts a number into a string with a defined number of decimal places.
function fixDP(number, numDP) {
  var rounded = Math.round(number*Math.pow(10,numDP))/Math.pow(10,numDP);
  str = rounded + "";
  d = str.indexOf(".");
  if (d == -1) {
    str += ".";
    d = str.length-1;
  }
  // Enough or fewer dp than required
  for (i=str.length;i<=d+numDP;i++) {
    str += "0";
  }
  return str;
}

// Clears all data from a jqGrid
function clearGrid(table) {
  $(table).find('tbody tr').each(function() {
    $(table).delRowData($(this).attr('id'));
  });    
}

////////////
// Cookies //
////////////

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Reads every item out of the cookie and returns it as an array of [name, value] arrays
function parseCookie() {
  var crumbs = document.cookie.split(';');
  var cArray = [];
  for (var i=0; i<crumbs.length; i++) {
    var pair = crumbs[i].split('=');
    if (pair.length>1) {
      for (var j=0; j<pair.length; j++) {
        pair[j]=pair[j].trim();
      }
      cArray.push(pair);
    }
  }
  return cArray;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

///////////////////////
// Stat Value Functions //
///////////////////////

// Use case-insensitive lookup
function getIndexFromStringArray(item, itemArray) {
  item=item.toLowerCase();
  for (i=1; i<itemArray.length; i++) {
    if (itemArray[i].toLowerCase() == item) {
      return i;
    }    
  }
  return 0;
}
