function show_hide(el2show, el2hide)
{
    document.getElementById(el2show).style.display = 'block';
    document.getElementById(el2hide).style.display = 'none';
}
    
function openWindow(url, name, width, height, options)
{
    var wnd = window.open(url, name, "width=" + width + ",height=" + height +
	         ",left=" + (screen.width - width) / 2 + ",top=" + (screen.height - height) / 2 + "," + options);
    wnd.creator = self;
    wnd.focus();
}

function openPopupWindow(url, name, width, height, options)
{
    var wnd;
    if ( window.showModalDialog && navigator.appVersion.indexOf("MSIE 7") != -1 ) {
        window.showModalDialog(url, window, "dialogWidth:" + width + "px;" + "dialogHeight:" + height + "px;center:yes;" + options);
    }
    else {
        wnd = window.open(url, name, "width=" + width + ",height=" + height +
	         ",left=" + (screen.width - width) / 2 + ",top=" + (screen.height - height) / 2 + "," + options);
	    wnd.creator = self;
        wnd.focus();
    }
    return false;
}

function showElement(id, show, x, y)
{
    var el = document.getElementById(id);
    if ( el != null ) {
        if ( show ) {
            if ( x != null )
                el.style.left = x;
            if ( y != null )
                el.style.top = y;
        }
        el.style.display = show ? "block" : "none";
    }
    return false;
}

function switchPopup(id)
{
    var shown_name = id + "_shown";
    var shown;
    try {
        shown = eval(shown_name);
        alert(shown);
    }
    catch ( err ) {
        eval("var " + shown_name + " = 0;");
        shown = 0;
    }
    
    showElement(id, shown ? "none" : "block");
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
     if (isIntegerInRange.arguments.length == 1) return false;
     else return (isIntegerInRange.arguments[1] == true);

  // Catch non-integer strings to avoid creating a NaN below,
  // which isn't available on JavaScript 1.0 for Windows.
  if (!isInteger(s, false)) return false;

  // Now, explicitly change the type to integer via parseInt
  // so that the comparison code below will work both on
  // JavaScript 1.2 (which typechecks in equality comparisons)
  // and JavaScript 1.1 and before (which doesn't).
  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}

function isInteger(s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit(c)
{
  return ((c >= "0") && (c <= "9"))
}

function loadScript(doc, sScriptSrc, callbackfunction)
{
     //gets document head element
     var oHead = doc.getElementsByTagName('head')[0];
     if(oHead)
     {
         //creates a new script tag
         var oScript = doc.createElement('script'); 

         //adds src and type attribute to script tag
         oScript.setAttribute('src', sScriptSrc);
         oScript.setAttribute('type', 'text/javascript');

         //calling a function after the js is loaded (IE)
         var loadFunction = function()
             {
                 if (this.readyState == 'complete' || this.readyState == 'loaded')
                 {
                    if( callbackfunction != null ) callbackfunction();
                 }
             };
         oScript.onreadystatechange = loadFunction;

         //calling a function after the js is loaded (Firefox)
         oScript.onload = callbackfunction;

         //append the script tag to document head element
         oHead.appendChild(oScript);
     }
}

function toggle(elt_id)
{
    var elt = document.getElementById(elt_id);
    if (elt.style.display == 'none') 
        elt.style.display = 'block';
    else
        elt.style.display = 'none';
}

function loadImage(container, path) {
	var img = new Image();
	$(img)
		.load(function () {
			$(this).hide();

			container.removeClass('loading').append(this);

			$(this).fadeIn();
		})
		.error(function (ex) {
//			$(this).attr('src', 'images/tab.png');
			alert('Error: cannot load image');
		})
		.attr('src', path);
}
