
if (Array.prototype.push == null) { // Declare Array.push if not declared (old Javascript)
  Array.prototype.push = push;
}

function push(o) {
  this[this.length] = o;
}

if (Array.prototype.splice == null) { // Declare Array.splice if not declared (not a full implimentation yet)
  Array.prototype.splice = mySplice;
}

function mySplice(st, len) {
  // Not interested in the full functionality of splice just yet,
  // just want to insert one item

  for (var i = this.length; i >= st; i--) {
    this[i] = this[i - 1];
  }

  this[st] = arguments[2];
}


if (Array.prototype.getElemAt == null) { // This allows for easy switching between an ordered Vector and an array
  Array.prototype.getElemAt = getElemAt;
}

function getElemAt(i) {
  return this[i];
}

function OrderedVector() {
  this.arr = new Array();
  for ( var i = 1; i < arguments.length; i++  ) {
		this.arr.push(arguments[i]);
  }

  this.comp_op = arguments[0];

  this.push = add;
  this.setComparator = setComparator;
  this.length = this.arr.length;
  this.getLength = getLength;
  this.getElemAt = getElemAt;

  function add( o ) {
    this.length++;
    if ( typeof(this.comp_op) == 'undefined' ) {
      this.arr.push(o);
      this.arr.sort();
    } else {
      for ( var i = 0; i < this.arr.length; i++ )	{
        if (this.comp_op(o, this.arr[i]) < 0) {
			 //The array should already be sorted, so as soon as we find
			 // a value that the new object should proceed, we splice it in.
          this.arr.splice(i, 0, o);
          return;
        }
      }
      this.arr.push(o);
    }
  }

  function setComparator( f ) {
    this.comp_op = f;
  }

  function getLength() {
    return this.arr.length;
  }

  function getElemAt( i ) {
    return this.arr[i];
  }
}

function OrderedSet() {
  this.arr = new Array();
  for ( var i = 1; i < arguments.length; i++  ) {
		this.arr.push(arguments[i]);
  }

  this.comp_op = arguments[0];

  this.push = add;
  this.setComparator = setComparator;
  this.length = this.arr.length;
  this.getLength = getLength;
  this.getElemAt = getElemAt;

  function add( o ) {
    this.length++;
    if ( typeof(this.comp_op) == 'undefined' ) {
      this.arr.push(o);
      this.arr.sort();
    } else {
      for ( var i = 0; i < this.arr.length; i++ )	{
		  var compVal = this.comp_op(o, this.arr[i]);
        if (compVal < 0) {
			 //The array should already be sorted, so as soon as we find
			 // a value that the new object should proceed, we splice it in.
          this.arr.splice(i, 0, o);
          return;
        } else if (compVal == 0) {
			 //The new object already exists in the array. Return without adding it.
			 this.length--;
			 return;
		  }
      }
      this.arr.push(o);
    }
  }

  function setComparator( f ) {
    this.comp_op = f;
  }

  function getLength() {
    return this.arr.length;
  }

  function getElemAt( i ) {
    return this.arr[i];
  }
}

function option_comp(f, s) {
  return insensitive_string_comp(f.text, s.text)
}

function insensitive_string_comp(f, s) {
  if (f.toLowerCase() < s.toLowerCase()) {
	 return -1;
  } else if (f.toLowerCase() > s.toLowerCase()) {
	 return 1;
  } else {
	 return 0;
  }
}

function string_comp(f, s) {
  if (f < s) {
	 return -1;
  } else if (f > s) {
	 return 1;
  } else {
	 return 0;
  }
}

function size_comp(f, s) {

  var size_obj = new Object(), f_wt, s_wt;

  size_obj['XXS'] = 0;
  size_obj['XS']  = 1;
  size_obj['S']   = 2;
  size_obj['S/M'] = 3;
  size_obj['M']   = 4;
  size_obj['L']   = 5;
  size_obj['L/XL']= 6;
  size_obj['XL']  = 7;
  size_obj['XXL'] = 8;
  size_obj['XXXL']= 9;

  if (size_obj[f] == null) {
    f_wt = 100;
  } else {
    f_wt = size_obj[f];
  }

  if (size_obj[s] == null) {
    s_wt = 100;
  } else {
    s_wt = size_obj[s];
  }

  return (f_wt - s_wt);
}

function numeric_comp( f, s ) {
  return fractionalVal(f) - fractionalVal(s);
}

function fractionalVal(s) {
  s = new String(s);
  tot = 0;
  nums = s.split(" ");
  for (var i = 0; i < nums.length; i++) {
    if ( !isNaN(parseInt(nums[i])) ) {
      tot += eval(nums[i]);
    }
  }
  return tot;
}

// Items will store everything from the DB, the other arrays will just act as sets
// to maintain a count of unique values
var items = new Array(), gl_colors = new Array(), gl_sizes = new Array();
//added for Casual Apparel
var gl_hem = new Array(), gl_inseam = new Array(), hems = new Array();

