function toggleVis(elem, displaytype) {

    e = document.getElementById(elem);
    toggleElementVis(e, displaytype);
    
    return e;
}

function toggleParentVis(elem, displaytype) {
    e = document.getElementById(elem);
    if (e != null) {
        toggleElementVis(e.parentNode, displaytype);
    }
}

function toggleElementVis(e, displaytype) {
 if (typeof displaytype == 'undefined') {
        displaytype="block";
    }
    if (e != null) {
        if (e.style.display == "none" || e.style.display == "") {
            e.style.display = displaytype;
        }
        else {
            e.style.display = "none";
        }
    
    }
//     else {
//         alert("e is null");
//     }
}

function deleteBox(boxNum) {
    toggleParentVis('list' + boxNum);
    // update cookie
    pref = readCookie('EVE-Market-Summary-List');
    
    var reg = new RegExp("(%2Cm?" + boxNum + ")|(m?" + boxNum + "%2C)|^m?" + boxNum + "$");
    
    writeCookie('EVE-Market-Summary-List', pref.replace(reg, ""), 365);    
}

function shadeBox(boxNum) {
    e = toggleVis('list' + boxNum, 'table');
    pref = readCookie('EVE-Market-Summary-List');
    if (e.style.display == "none") {
        var regnom = new RegExp(boxNum + "(?=%2C|$)");
        writeCookie('EVE-Market-Summary-List', pref.replace(regnom, "m" + boxNum), 365);
    }
    else {
        var regm = new RegExp("m" + boxNum + "(?=%2C|$)");
        writeCookie('EVE-Market-Summary-List', pref.replace(regm, boxNum), 365);
    }
}

function writeCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function reloadChart(chartNo, query)
{
    if(chartNo == 1) {
        tmp = findSWF("chart");
    }
    else {
        tmp = findSWF("chart_" + chartNo);
    }
  
  //
  // reload the data:
  //
  //x = tmp.reload();
  
  //
  // to load from a specific URL:
  // you may need to 'escape' (URL escape, i.e. percent escape) your URL if it has & in it
  //
  if (tmp != null) {
    x = tmp.reload(query);
    return false;
  }
  else {
    return true;
  }
  
  //
  // do NOT show the 'loading...' message:
  //
  //x = tmp.reload("gallery-data-32.php?beer=1", false);
}

function findSWF(movieName) {
  if (navigator.appName.indexOf("Microsoft")!= -1) {
    return window["ie_" + movieName];
  } else {
    return document[movieName];
  }
}

function getAjaxObject() {
	try {
	  // Firefox, Opera 8.0+, Safari
    return new XMLHttpRequest();
	} catch (e) {
    // Internet Explorer
	  try {
	    return new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
	    try {
	      return new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {
	        return false;
	    }
	  }
	}
}

function addToOnload(func)
{
  /* This function adds a function the window.onload event,
     so it will run after the document has finished loading.
  */
  var oldOnLoad;

  /* Taken from: http://simon.incutio.com/archive/2004/05/26/addLoadEvent */

  oldOnLoad = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldOnLoad();
      func();
    };
  }
}

