function getElementsByClass(className, here, tagname){
  if(here==null)
    here = document;
  if(tagname == null)
    tagname = 'div';

  var divs = here.getElementsByTagName(tagname);
  var s = '';
  var classElements = new Array();

  var j = 0;
  for( var i=0; i<divs.length; i++ )
    {
      if( divs[i].className ==  className)
	{
	  classElements[j] = divs[i];
	  j++;
	}
    }

  return classElements;  
  }



function getHTTPObject(){
  if (window.ActiveXObject) 
    return new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest) 
    return new XMLHttpRequest();
  else
    alert("Your browser does not support AJAX.");

  return null;
}

function getXmlHTTPObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}



function ajax_mysql_query( query ){
        httpObject = getHTTPObject();
      
      if(httpObject != null)
	{
	  var link =  "ajax_mysql_query.php?query=" + query;
	  httpObject.open("GET", link, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = setOutput;
	}
}

function ajaxQueryOutput(){
    if(httpObject.readyState == 4)
    {
      var response = httpObject.responseText;
      if(response != 'true')
	alert(response);
    }
}

function findPos(obj) {
  var curtop;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	  do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
	  } while (obj = obj.offsetParent);	
	  return [curleft,curtop];
	}
}


function open_child(file,window, width, height) {
    childWindow=window.open(file,'','resizable=yes,width='+width+',height='+height);
    if (childWindow.opener == null) childWindow.opener = self;
}

function set_form_default( id, val ){
  var f = document.getElementById( id );
  if(f)
    {
      f.value = val;
      f.style.color = 'gray';
      f.setAttribute('onfocus', "clear_form_default(this, '" + val + "');");
  
      f.onblur = function(){
	if( f.value == '' )
	  {
	    f.style.color = 'gray';
	    f.value = val;
	    set_form_default( id, val );
	  }
      }
    }
}

function clear_form_default( where, val  ){
  if(where && where.value == val )
    {
      where.value = '';
      where.style.color = 'black';
      where.setAttribute('onfocus', '');
    }
}


/********* CENTERED MESSAGE BOXES ***************/

function display_error( msg, title,  width, height ){
  center_box( msg, title,  width, height );
}

function popup( msg, id ){
  if( id == null )
    id = 'popup';

  var theContainer;
  $('#'+id).remove();
     
  theContainer = document.createElement('div');
  theContainer.id = id;
  
  var theMessage = document.createElement('div');
  theMessage.className = 'jqmMessage';

  theContainer.className = 'jqmWindow';
  theContainer.innerHTML = "<p class='closeMessage'><a href='#' class='jqmClose'>X</a></p>";
  theContainer.appendChild(theMessage);
  theMessage.innerHTML = msg;

  document.body.appendChild(theContainer);

  $('#'+id).jqm({closeClass: 'jqmClose'}).jqmShow();
}


function popupAjax( link, wrapperClass ){
  $('#popup').remove();
  var theContainer;
  theContainer = document.createElement('div');
  theContainer.id = 'popup';
  
  var theMessage = document.createElement('div');
  theMessage.className = 'jqmMessage';
  theMessage.id = 'jqmMessage';
  theContainer.className = 'jqmWindow';
  theContainer.innerHTML = "<p class='closeMessage'><a href='#' class='jqmClose'>X</a></p>";
  theContainer.appendChild(theMessage);

  document.body.appendChild(theContainer);

  $('#popup').jqm({closeClass: 'jqmClose', ajax:link, target:'div.jqmMessage'}).jqmShow();

}

function center_box( msg, title,  width, height, isURL ){

  var error = document.createElement('div');
  error.setAttribute('id', 'floatingMessage');
  error.innerHTML = "<p class='floatHeader'><a onclick=\"close_error();\">X</a><b>" + title + "</b></p>";

  var content = document.createElement('div');
  content.setAttribute('id', 'floatingMessageContent');
  content.innerHTML = "<p><img src='images/loadingAnimation.gif'></p>";
  error.appendChild(content);

  pageSize = get_page_size();

  var leftMargin = Math.floor(pageSize[0]/2) - width/2;
  var topMargin = Math.floor(pageSize[1]/2) - height/2;

  error.style.marginLeft = leftMargin + 'px';
  error.style.marginTop = topMargin + 'px';
  error.style.width = width + 'px';
  //  error.style.height =  height + 'px';

  document.body.insertBefore(error, document.body.firstChild);
  error.style.display = 'block';

  // load content

  if(isURL)
    {
      var httpObject = getHTTPObject();
      if(httpObject != null)
	{
	  httpObject.open("GET", msg, true);
	  httpObject.send(null);
	  httpObject.onreadystatechange = function(){
	    if(httpObject.readyState == 4)
	      {
		var response = httpObject.responseText;
		content.innerHTML = response;
  	      }
	  }
	}
    }
  else
    {
      content.innerHTML = "<p>" + msg + "</p>";
    }

  content.style.padding = '12px 12px 12px 12px';
  content.style.width = (width - 24) + 'px';
  content.style.height =  height + 'px';
  content.style.overflow = 'auto';
      
}

function get_page_size(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	var arrayPageSize = [w,h];
	return arrayPageSize;
}

function close_error(){
  close_box();
}
function close_box(){
  var error = document.getElementById('floatingMessage');
  error.parentNode.removeChild(error);
}

function explain_expert( id ){
  if(id==null)
    id='';
  popupAjax( 'explain_expert.php?id=' + id);
}

(function($) {
  $.fn.equalizeCols = function(children){
    var child = Array(0);
    if (children) child = children.split(",");
    var maxH = 0;
    this.each(
      function(i) 
      {
        if (this.offsetHeight>maxH) maxH = this.offsetHeight;
      }
    ).css("height", "auto").each(
      function(i)
      {
        var gap = maxH-this.offsetHeight;
        if (gap > 0)
        {
          var t = document.createElement('div');
          $(t).attr("class","fill").css("height",gap+"px");
          if (child.length > i)
          {
            $(this).find(child[i]).children(':last-child').after(t);
          } 
          else 
          {
            $(this).children(':last-child').after(t);
          }
        }
      }  
    );
    
  }
})(jQuery);
