/*global window, unescape*/
var Sfr = {};

Sfr.QueryString = function() {
   this.properties = {};

   var qs = window.location.search.substring(1);
   var parts = qs.split('&');
   parts.each(function(part) {
      var kv = part.split('=');
      this.properties[kv[0]] = unescape(kv[1]).replace("+", " ");
   }, this);

   this.get = function(key) {
      return this.properties[key];
   };

   this.set = function(key, value) {
      this.properties[key] = value;
   };

   this.remove = function(key) {
      delete this.properties[key];
   };

   this.toString=function() {
      var parts = [];
      for (var p in this.properties) {
         parts.push(p + "=" + this.properties[p]);
      }
      return parts.join('&');
   };
};

//compare the contents of 2 arrays
Sfr.compare_array = function(a,b) {
   if (a instanceof Array && b instanceof Array) {
      if (a.length !== b.length) {
         return false;
      }

      for (var i=0; i < a.length; i++) {
         if (a[i] !== b[i]) {
            return false;
         }
      }
      return true;
   } else {
      throw 'both params to compare_array must be arrays';
   }
};

Sfr.dollars = function(number) {
   if(!number){
      return '$0.00';
   } else {
      return '$' + parseInt(number * 100, 10).toString().replace(/(..)$/, '.$1');
   }
};

//Convenience function to do xor since js lacks one
//returns true if a and b are different, i.e. true/false or false/true
Sfr.xor = function(a,b) {
   return (a && !b) || (!a && b);
};

//myField accepts an object reference, myValue accepts the text strint to add
Sfr.insertAtCursor = function(myField, myValue) {
   //IE support
   if (document.selection) {
      myField.focus();

      //in effect we are creating a text range with zero
      //length at the cursor location and replacing it
      //with myValue
      var sel = document.selection.createRange();
      sel.text = myValue;
      sel.collapse(false);
   }

   //Mozilla/Firefox/Netscape 7+ support
   else if (myField.selectionStart || myField.selectionStart === '0') {

      //Here we get the start and end points of the
      //selection. Then we create substrings up to the
      //start of the selection and from the end point
      //of the selection to the end of the field value.
      //Then we concatenate the first substring, myValue,
      //and the second substring to get the new value.
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
      myField.focus();
      myField.setSelectionRange(myField.selectionStart + myValue.length, myField.selectionStart + myValue.length);
   } else {
      myField.value += myValue;
   }
};

Sfr.track_textarea_focus = function() {
     if(window.addEventListener) {
        //Mozilla/Safari/Opera: blur and focus events do not bubble but are available during capture phase
        window.addEventListener("focus",function(eventObj){
            if (eventObj.target.tagName === 'TEXTAREA') {
               Sfr.last_focused_textarea = eventObj.target;
            }
         },true); //this true means capture phase
    }
    //IE
    else if(window.attachEvent) {
       window.attachEvent('onfocusin', function(eventObj) {
            if (eventObj.target.tagName === 'TEXTAREA') {
               Sfr.last_focused_textarea = eventObj.target;
            }
       });
    }
};

