// === Get/Hide/Show/Toggle ===

function ge()
{
  var ea;
  for( var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];
    if( typeof e == 'string' )
      e = document.getElementById(e);
    if( arguments.length == 1 )
      return e;
    if( !ea )
      ea = new Array();
    ea[ea.length] = e;
  }
  return ea;
}

function show()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = '';
  }
  return false;
}

function hide()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = 'none';
  }
  return false;
}

function shown(el) {
    el = ge(el);
    return (el.style.display != 'none');
}

function toggle()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    element.style.display = (element.style.display != 'none') ? 'none' : '';
  }
  return false;
}

function remove_node(node)
{
  if (node.removeNode)
    node.removeNode(true);
  else {
    for (var i=node.childNodes.length-1; i>=0; i--)
      remove_node(node.childNodes[i]);
      node.parentNode.removeChild(node);
    }
  return null;
}

// === Modifying Class Names ===//
function has_css_class_name(elem, cname) {
  return (elem && cname) ? new RegExp('\\b'+trim(cname)+'\\b').test(elem.className) : false;
}

//Returns true if css class was added to element. False otherwise.
function add_css_class_name(elem, cname) {
  if(elem && cname) {
    if(elem.className) {
      if(has_css_class_name(elem, cname)) {
        return false;
      } else {
        elem.className += ' ' + trim(cname);
        return true;
      }
    } else {
      elem.className = cname;
      return true;
    }
  } else {
    return false;
  }
}

//Returns true if css class was removed from element. False otherwise
function remove_css_class_name(elem, cname) {
  if(elem && cname && elem.className) {
    cname = trim(cname);
    var old = elem.className;
    elem.className = elem.className.replace(new RegExp('\\b'+cname+'\\b'), '');
    return elem.className != old;
  } else {
    return false;
  }
}

// === Event Info Access ===

var KEYS = { BACKSPACE: 8,
             TAB:       9,
             RETURN:   13,
             ESC:      27,
             LEFT:     37,
             UP:       38,
             RIGHT:    39,
             DOWN:     40,
             DELETE:   46 };

function mouseX(event)
{
  return event.pageX || (event.clientX +
    (document.documentElement.scrollLeft || document.body.scrollLeft));
}

function mouseY(event)
{
  return event.pageY || (event.clientY +
    (document.documentElement.scrollTop || document.body.scrollTop));
}

function pageScrollX()
{
  return document.body.scrollLeft || document.documentElement.scrollLeft;
}

function pageScrollY()
{
  return document.body.scrollTop || document.documentElement.scrollTop;
}

function elementX(obj)
{
  var curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function elementY(obj)
{
  var curtop = 0;
  if(obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

// === Onload Registry ===

function onloadRegister(handler) {
  if (window.onload) {
    var old=window.onload;
    window.onload=function() { old(); handler(); };
  }
  else {
    window.onload=handler;
  }
}

// === Event Attaching ===
// (see: http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html)

function addEventBase(obj, type, fn, name_hash)
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
                obj["e"+type+fn+name_hash] = fn;
		obj[type+fn+name_hash] = function() { obj["e"+type+fn+name_hash]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn+name_hash] );

	}
}


// why name_hash? So you can use the same function and pass different name_hashes and ie won't get confused
function removeEventBase(obj, type, fn, name_hash)
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn+name_hash] );
		obj[type+fn+name_hash] = null;
		obj["e"+type+fn+name_hash] = null;
	}
}


// === Placeholder Text ===

function placeholderSetup(id) {
  var el = ge(id);
  if(!el) return;
  /*if(el.type != 'text') return;*/

  var ph = el.getAttribute("placeholder");
  if( ph && ph != "" ) {
    el.value = ph;
    el.style.color = '#777';
    el.is_focused = 0;
    addEventBase(el, 'focus', placeholderFocus);
    addEventBase(el, 'blur', placeholderBlur);
  }
}

function placeholderFocus() {
  if(!this.is_focused) {
    this.is_focused = 1;
    this.value = '';
    this.style.color = '#000';

    var rs = this.getAttribute("radioselect");
    if( rs && rs != "" ) {
      var re = document.getElementById(rs);
      if(!re) { return; }
      if(re.type != 'radio') return;

      re.checked=true;
    }
  }
}

function placeholderBlur() {
  var ph = this.getAttribute("placeholder")
  if( this.is_focused && ph && this.value == "" ) {
    this.is_focused = 0;
    this.value = ph;
    this.style.color = '#777';
  }
}

// === String Utilities ===

function htmlspecialchars(text) {
  return text ? text.toString().replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#039;').replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
}

function escape_js_quotes(text) {
  if (!text) {
    return;
  }

  return text.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/"/g, '\\x22').replace(/'/g, '\\\'').replace(/</g, '\\x3c').replace(/>/g, '\\x3e').replace(/&/g, '\\x26');
}

function trim(str) {
  var delim = arguments.length > 1 ? arguments[1] : ' ';
  for (var i=0, c=str.length-delim.length; i<=c; i+=delim.length) {
    if (str.substring(i, i + delim.length) != delim) {
      break;
    }
  }

  for (var j=str.length, c=Math.max(i, delim.length - 1); j>c; j-=delim.length) {
    if (str.substring(j - delim.length, j) != delim) {
      break;
    }
  }

  return str.substring(i, j);
}

// === URI Handling ===

function escapeURI(u)
{
    if(encodeURIComponent) {
        return encodeURIComponent(u);
    }
    if(escape) {
        return escape(u);
    }
}

function goURI(href) {
  window.location.href = href;
}

function is_email(email) {
  return /^[\w!.%+]+@[\w]+(?:\.[\w]+)+$/.test(email);
}

//13th parallel
function getViewportWidth() {
  var width = 0;
  if( document.documentElement && document.documentElement.clientWidth ) {
    width = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth ) {
    width = document.body.clientWidth;
  }
  else if( window.innerWidth ) {
    width = window.innerWidth - 18;
  }
  return width;
}

function getViewportHeight() {
  var height = 0;
  if( window.innerHeight ) {
    height = window.innerHeight - 18;
  }
  else if( document.documentElement && document.documentElement.clientHeight ) {
    height = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight ) {
    height = document.body.clientHeight;
  }
  return height;
}

function getPageScrollHeight() {
  var height;
  if ( typeof(window.pageYOffset) == 'number') {
    height = window.pageYOffset;
  } else if (document.body && document.body.scrollTop) {
    height = document.body.scrollTop;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    height = document.documentElement.scrollTop;
  }
  return height;
}

function getRadioFormValue(obj) {
  for(i = 0; i < obj.length; i++) {
   if(obj[i].checked) {
     return obj[i].value;
   }
  }
  return null;
}

function isIE() {
 return (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
}

function isSafari() {
 return (navigator.userAgent.indexOf("Safari") != -1);
}

function getTableRowShownDisplayProperty() {
  if (isIE()) {
    return  'inline';
  } else {
    return 'table-row';
  }
}

function showTableRow()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display =
        getTableRowShownDisplayProperty();
  }
  return false;
}

function getParentRow(el) {
    el = ge(el);
    while (el.tagName && el.tagName != "TR") {
        el = el.parentNode;
    }
    return el;
}

function stopPropagation(e) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
}

function remove_node(node) {
  if (node.removeNode)
    node.removeNode(true);
  else {
    for (var i=node.childNodes.length-1; i>=0; i--)
      remove_node(node.childNodes[i]);
    node.parentNode.removeChild(node);
  }
  return null;
}

function set_opacity(obj, opacity) {
  try {
    obj.style.opacity=(opacity==1?'':opacity);
    obj.style.filter=(opacity==1?'':'alpha(opacity='+opacity*100+')');
  }
  catch (e) {}
  obj.setAttribute('opacity', opacity); // for future reference
}

function get_opacity(obj) {
  return obj.opacity ? obj.opacity : 1;
}

function array_indexOf(arr, val, index) {
  if (!index) {
    index=0;
  }
  for (var i=index; i<arr.length; i++) {
    if (arr[i] == val) {
      return i;
    }
  }
  return -1;
}

function dp(object)
{
  var descString = "";
  for(var value in object)
    descString += (value + " => " + object[value] + "\n");
  if( descString != "" )
    alert(descString);
  else
    alert(object);
}

function Ajax(doneHandler, failHandler)
{
  newAjax = this;
  this.onDone = doneHandler;
  this.onFail = failHandler;
  this.transport = this.getTransport();
  this.transport.onreadystatechange = ajaxTrampoline(this);
}

Ajax.prototype.get = function (uri, query, force_sync)
{
  // Firefox doesn't call onDone and onFail handlers if you force_sync
  force_sync = force_sync || false;
  if( typeof query != 'string' )
    query = ajaxArrayToQueryString(query);
  fullURI = uri+(query ? ('?'+query) : '');
  this.transport.open('GET', fullURI, !force_sync );
  this.transport.send('');
}

Ajax.prototype.post = function (uri, data, force_sync, no_post_form_id)
{
  force_sync = force_sync || false;
  no_post_form_id = no_post_form_id || false;

  if( typeof data != 'string' ) {
    data = ajaxArrayToQueryString(data);
  }
  if (!no_post_form_id) {
    var post_form_id=ge('post_form_id');
    if (post_form_id) {
      data+='&post_form_id='+post_form_id.value;
    }
  }
  this.transport.open('POST', uri, !force_sync);
  this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  this.transport.send(data);
}

Ajax.prototype.stateDispatch = function ()
{
  try {
    if( this.transport.readyState == 1 && this.showLoad )
      ajaxShowLoadIndicator();

    if( this.transport.readyState == 4 ) {
      if( this.showLoad )
        ajaxHideLoadIndicator();
      if( this.transport.status >= 200 &&
          this.transport.status < 300 &&
          this.transport.responseText.length > 0 ) {
        try {
          if( this.onDone ) this.onDone(this, this.transport.responseText);
        } catch(tempError) {
          console ? console.error(tempError) : false;
        }
      } else {
        try {
          if( this.onFail ) this.onFail(this);
        } catch(tempError) {
          console ? console.error(tempError) : false;
        }
      }
    }
  } catch (error) {
    if( this.onFail ) this.onFail(this);
  }
}

Ajax.prototype.getTransport = function ()
{
  var ajax = null;

  try { ajax = new XMLHttpRequest(); }
  catch(e) { ajax = null; }

  try { if(!ajax) ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) { ajax = null; }

  try { if(!ajax) ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch(e) { ajax = null; }

  return ajax;
}

function ajaxTrampoline(ajaxObject)
{
  return function () { ajaxObject.stateDispatch(); };
}

function ajaxArrayToQueryString(query) {
  var params = [];

  for (var key in query) {
    if (typeof query[key] == 'object') {
      for (var i=0; i<query[key].length; i++) {
        params.push(encodeURIComponent(key)+'[]='+encodeURIComponent(query[key][i]));
      }
    } else {
      params.push(encodeURIComponent(key)+'='+encodeURIComponent(query[key]));
    }
  }
  return params.join('&');
}

var ajaxLoadIndicatorRefCount = 0;

function ajaxShowLoadIndicator()
{
  indicatorDiv = ge('ajaxLoadIndicator');
  if( !indicatorDiv ) {
    indicatorDiv = document.createElement("div");
    indicatorDiv.id = 'ajaxLoadIndicator';
    indicatorDiv.innerHTML = 'Loading';
    indicatorDiv.className = 'ajaxLoadIndicator';
    document.body.appendChild(indicatorDiv);
  }

  indicatorDiv.style.top = (5 + pageScrollY()) + 'px';
  indicatorDiv.style.left = (5 + pageScrollX()) + 'px';
  indicatorDiv.style.display = 'block';
  ajaxLoadIndicatorRefCount++;
}

function ajaxHideLoadIndicator()
{
  ajaxLoadIndicatorRefCount--;
  if( ajaxLoadIndicatorRefCount == 0 )
    ge('ajaxLoadIndicator').style.display = '';
}

