function createXMLHttpRequest() 
{
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser does not support AJAX!");
	      return false;
	      }
	    }
	  }
	return xmlHttp
}

/*Handy functions by by Peter-Paul Koch & Alex Tingle (http://blog.firetree.net/2005/07/04/javascript-find-position/)*/
function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}
/*---*/
function get_by_tag(tag)
{
    objs = null;
    if (document.all)
    { 
        objs = document.all.tags(tag);
    }
    else if (document.getElementsByTagName)
    { 
        objs = document.getElementsByTagName(tag);
    }
    else if (document.layers)
    { 
        objs = document.layers[tag];
    }
    return objs;
}

function removeSelectDuplicateRows(obj)
{
    var i = 0;
    var cache = {};

    for(; i < obj.options.length ; i++)
    {
        if(obj.options[i].text != "" && obj.options[i].text != "None")
            cache[obj.options[i].text] = obj.options[i].value;
    }

    for(i = obj.options.length ; i >= 0 ; i--)
    {
        obj.options[i] = null;
    }
    
    obj.options[obj.options.length] = new Option("--------------------",0);
    for(k in cache)
    {
        obj.options[obj.options.length] = new Option(k,cache[k]);
    }
    obj.size = obj.options.length;
}

function addClass(el, cls){
    var cn = el.className;
    if (typeof(cn) != 'undefined' && cn.indexOf(cls) == -1){
        el.className += " " + cls;
    }
    return false;
}

function removeClass(el, cls){
    var cn = el.className;
    if (typeof(cn) != 'undefined' && cn.indexOf(cls) != -1){
        cn = cn.replace(cls, "");
        el.className = cn;
    }
    return false;
}

var HIDING_CLASS = 'display-none';

function show_element(id){
    var el = document.getElementById(id);
    if (el){
        removeClass(el, HIDING_CLASS);
    }
    return false;
}

function hide_element(id){
    var el = document.getElementById(id);
    if (el){
        addClass(el, HIDING_CLASS);
    }
    return false;
}

function toggle_element(id){
    var el = document.getElementById(id);
    if (el.className.indexOf(HIDING_CLASS) != -1){
        removeClass(el, HIDING_CLASS);
    }
    else{
    	addClass(el, HIDING_CLASS);
    }
    return false;
}

var TIMEOUT = null;
function show_dropdown(trigger_element_id, dropdown_element_id, offset_top, offset_left){
    if (offset_top == null) offset_top = 0;
    if (offset_left == null) offset_left = 0;
    var trigger = document.getElementById(trigger_element_id);
    var block = document.getElementById(dropdown_element_id);
    if (block && trigger){
	    var top = findPosY(trigger);
	    var left = findPosX(trigger);
	    
	    block.style.top = top + offset_top + "px";
	    block.style.left = left + offset_left + "px";
	    show_element(dropdown_element_id);
	}
	clearTimeout(TIMEOUT);
}
function hide_dropdown(id)
{
    TIMEOUT=setTimeout("hide_element( '"+ id +"' );", 100);
}

//function hide_help(url, help_page_id)
//{
//    var xhReq = createXMLHttpRequest();
//    xhReq.open("GET", url + "?hide-help-text=1&page-code=" + help_page_id, false);
//    xhReq.send(null);
//    if( xhReq.responseText == "OK"){
//        var help = document.getElementById("base_help_text");
//        if (help != null)
//            addClass(help, 'display-none');
//    }
//}

function hide_warning_message(url)
{
	if (confirm("Do you want these messages not to be shown anymore?")){
	    var xhReq = createXMLHttpRequest();
	    xhReq.open("GET", url + "?hide-warning-message=1", false);
	    xhReq.send(null);
	    if( xhReq.responseText == "OK"){
	    	hide_element("base_warning_container");
	    }
	    else{
	    	alert("Unable to ignore warnings. Please check that you have not beed logged out.");
	    }
	}
}

function loadEntities(url) 
{
    var xhReq = createXMLHttpRequest();
    xhReq.open("GET", url , false);
    xhReq.send(null);
    return eval ('(' + xhReq.responseText + ')');
}

function count_chars_left(input_id, output_id, max_chars, block){
	if(max_chars == null) max_chars = 1;
	var remainder = 0
	var input = document.getElementById(input_id);
	if (input){
		var text = input.value;
		remainder = max_chars - text.length;
		if (block == true && remainder < 0){
			input.value = text.slice(0, remainder);
			remainder = 0;
		}
	}
	
	if (output_id){
		var output = document.getElementById(output_id);
		if(output){
			if (output.tagName == 'INPUT'){ output.value = remainder; }
			else { output.innerHTML = remainder; }
		}
	}
	return remainder;
}

function parse_date(str){ // from format 'yyyy-mm-dd'
	var yr = parseInt(str.substr(0,4), 10) 
    var mon = parseInt(str.substr(4+1,2), 10) // +1 is '-'
    var day = parseInt(str.substr(6+2,2), 10) //+2 is two '-'    
    var date_obj = new Date();
	date_obj.setFullYear(yr);
    date_obj.setMonth(mon-1);
    date_obj.setDate(day);
    
    return date_obj;
}

function parse_date_by_format(date_str, format){ // format is string like 'yyyy-mm-dd'
	var day_string = date_str.substr(format.indexOf('dd'), 2);
	var month_string = date_str.substr(format.indexOf('mm'), 2);
	var year_string = date_str.substr(format.indexOf('yyyy'), 4);
	var date_obj = new Date();
	date_obj.setFullYear(parseInt(year_string), parseInt(month_string)-1, parseInt(day_string));
	return date_obj;
}

function date_to_array(date_obj){ // to [yyyy,mm,dd]
	var yr = date_obj.getYear();
    if (yr < 1900) yr = yr + 1900;
    var value = [yr, date_obj.getMonth() + 1, date_obj.getDate()];
    return value
}

function set_content(id, html){
	var container = document.getElementById(id);
    if (container){
        container.innerHTML = html;
    }
}

function init_copy_flash(){
	ZeroClipboard.setMoviePath( '/static/ZeroClipboard.swf' );
    var clip = new ZeroClipboard.Client();
    clip.setHandCursor( true );
    clip.setCSSEffects( true );
    window.onresize = function(){clip.reposition()};
    clip.addEventListener( 'onComplete', function ( client, text ) {
        alert("The text is copied to your clipboard...");
    } );
    return clip
}

function add_copy_flash(clip, id, text_to_copy){
    clip.setText(text_to_copy);
    if (clip.div) {
		clip.receiveEvent('mouseout', null);
		clip.reposition(id);
	}
	else clip.glue(id);
	clip.receiveEvent('mouseover', null);
    return false;
}

function countValues(arr, value)
{
	var b = {}, i = arr.length, j;
	while( i-- ){
		j = b[arr[i]];
		b[arr[i]] = j ? j+1 : 1;
	}
	var res = b[value];
	if (typeof(res) == 'undefined') res = 0;
	return res;
}

function removeElement(arr, el){
	for (var i = 0; i < arr.length; i++){
		if (arr[i] == el){
			arr.splice(i, 1);
		}
	}
}