// Copyright 2005-2008 Socializr, Inc.
// Author: Jonathan Abrams

// Why are you reading this?
// If you are a great programmer, email your resume to jobs@socializr.com

function createHttpRequest() {
  var httpRequest = false;
  
  if (window.XMLHttpRequest) {
    // Mozilla
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
      httpRequest.overrideMimeType('text/xml');
    }
  }
  else if (window.ActiveXObject) {
    // IE
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
      }
    }
  }
  
  return httpRequest;
}

function getPosition(element) {
  var position = new Object;
  position.left = 0;
  position.top = 0;
  
  while (element) {
    position.left = position.left + element.offsetLeft;
    position.top = position.top + element.offsetTop;
    element = element.offsetParent;
  }
  
  return position;
}

function getScrollTop() {
  // this value contains the accurate scrollTop position of the document for IE6, IE7, FF2, FF3
  return document.documentElement.scrollTop;
}

function parsePixels(string) {
  return parseInt(string.substring(0, string.length - 2));
}

function addTextColumn(row, className, text) {
  var column = document.createElement("td");
  column.className = className;
  column.appendChild(document.createTextNode(text));
  row.appendChild(column);
}

function addLinkColumn(row, className, href, onclick, text) {
  var column = document.createElement("td");
  column.className = className;
  var link = document.createElement("a");
  link.href = href;
  link.onclick = onclick;
  link.appendChild(document.createTextNode(text));
  column.appendChild(link);
  row.appendChild(column);
}

function addTextHeader(row, className, text) {
  var column = document.createElement("th");
  column.className = className;
  column.appendChild(document.createTextNode(text));
  row.appendChild(column);
}

function addEvent(element, type, fn, capture) {
  if (element.addEventListener) {
    element.addEventListener(type, fn, capture);
  }
  else if (element.attachEvent) {
    element.attachEvent('on' + type, fn);
  }
}

function removeEvent(element, type, fn, capture) {
  if (element.removeEventListener) {
    element.removeEventListener(type, fn, capture);
  }
  else if (element.detachEvent) {
    element.detachEvent('on' + type, fn);
  }
}

function showElementById(id) {
  var element = document.getElementById(id);
  if (element) {
    element.style.display = "";
  }
}

function hideElementById(id) {
  var element = document.getElementById(id);
  if (element) {
    if (element.style.display != "none") {
      element.style.display = "none";
    }
  }
}

function getEvent(evt) {
  return (evt ? evt : ( window.event ? window.event : null ));
}

function getSrcElem(evt) {
  return (evt.target ? evt.target : evt.srcElement);
}

function getStyle(element, styleName) {
  if (element.currentStyle) {
    // IE
    if (styleName == "background-color") {
      styleName = "backgroundColor";
    }
    else if (styleName == "border-color") {
      styleName = "borderColor";
    }
    return element.currentStyle[styleName];
  }
  else if (window.getComputedStyle) {
    // Firefox
    return document.defaultView.getComputedStyle(element, null).getPropertyValue(styleName);
  }
}

function getSixDigitColor(color) {
  if (color == "white") {
    return "FFFFFF";
  }
  else if (color == "black") {
    return "000000";
  }
  else if (color.substr(0, 1) == "#") {
    if (color.length == 7) {
      // six digit hex
      return color.substr(1);
    }
    else if (color.length == 4) {
      // three digit hex
      var red = color.substr(1,1);
      var green = color.substr(2,1);
      var blue = color.substr(3,1);
      
      return red + red + green + green + blue + blue;
    }
  }
  else if (color.substr(0, 3) == "rgb") {
    var colors = color.substring(4, color.length - 1).split(",");
    
    var red = convertToHex(colors[0]);
    var green = convertToHex(colors[1]);
    var blue = convertToHex(colors[2]);
    
    return red + green + blue;
  }
  
  return "";
}


String.prototype.trim = function() {
  var string = this;
  if (string.length == 0) {
    return string;
  }
  
  var index = 0;
  // we trim the front
  while (string.charAt(index) == " " && index < string.length) {
    index++;
  }
  if(index > 0) {
    string = string.substring(index, string.length);
  }
  
  //now we trim the back
  index = string.length-1;
  while (string.charAt(index) == " " && index >= 0) {
    index--;
  }
  if(index < string.length-1) {
    // the substring method has an exclusive endPositionIndex so we +1
    string = string.substring(0, index+1);
  }
  
  return string;
}

String.prototype.trimStart = function() {
  var string = this;
  if (string.length == 0) {
    return string;
  }
  
  var index = 0;
  // we trim the start
  while (string.charAt(index) == " " && index < string.length) {
    index++;
  }
  if(index > 0) {
    string = string.substring(index, string.length);
  }
  
  return string;
}

function toggleVisibility(id) {
  var element = document.getElementById(id);
  if(element.style.display == 'block')
    element.style.display = 'none';
  else
    element.style.display = 'block';
}