/*
 * Function which opens a window with the specified url of the specified size.
 */
photoWindow = null;
function displayPhoto(url, width, height) {
  try {
    photoWindow.close();
  } catch (e) { }
  photoWindow = window.open(url,"photoWindow",'width='+width+',height='+height+',status=no,scrollbars=no,location=no,toolbar=no,resizable=no');
  photoWindow.focus();
}

/*
 * Set focus on the specified element, or if no element is specified try to set 
 * focus on the first text field on the page.
 */
function setFocus(eid) {
  try {  
    
    var isFocused = 0;
    if (eid != null && document.getElementById) {
      var control = document.getElementById(eid);
      if (control != null) {
        var controls = control.getElementsByTagName('input');
        for (i = 0; i < controls.length; i++) {
            if ((controls[i].type == "text" || 
                controls[i].type == "password") && 
                controls[i].style.display != "none" && 
                !controls[i].disabled) 
            {
              controls[i].focus();        
              isFocused = 1;
              break;
            }
        }
      }
    } 
    
    if(!isFocused)
    {
        if (document.getElementsByTagName) {
          var controls = document.getElementsByTagName('input');
          if (controls != null && controls.length > 0) {
            for (i = 0; i < controls.length; i++) {
                if ((controls[i].type == "text" || 
                    controls[i].type == "password") && 
                    controls[i].style.display != "none" && 
                    !controls[i].disabled) {

                  controls[i].focus();        
                  break;
                }
            }    
          }
        }  
    }

    
  } catch (e) { }
}


/*
 * Parse the value to make sure that it is a timespan.
 *
 * The following formats are recognized:
 *
 * HH:mm  -> HH:mm
 * HHmm   -> HH:mm
 * H:mm   -> 0H:mm
 * Hmm    -> 0H:mm
 * H:m    -> 0H:0m
 * :mm    -> 00:mm
 * :m     -> 00:0m
 * HH     -> HH:00
 * H      -> 0H:00
 */
function parseTime(val) {

  // regexps to parse the time
  var reg1 = /^(\d{1})(\d{2})$/;
  var reg2 = /^(\d{0,2})\D?(\d{0,2})$/;
  var timeParts;
  
  try {

    if (val != null && val.length > 0 && (reg1.test(val) || reg2.test(val))) {

		  if (reg1.test(val)) {
		     timeParts = val.match(reg1);
		  } else if (reg2.test(val)) {
		     timeParts = val.match(reg2);
		  } 

		  // zero length times are 0.
		  if (timeParts[1].length == 0) timeParts[1] = 0;
		  if (timeParts[2].length == 0) timeParts[2] = 0;
  		
		  // parse the values as numbers.
	    timeParts[1] = parseInt(timeParts[1], 10);
      timeParts[2] = parseInt(timeParts[2], 10);

		  // make sure that we are dealing with numbers.
		  if (isNaN(timeParts[1]) || isNaN(timeParts[2])) throw 1;
  		
		  // return the time formatted as HH:mm
		  return (timeParts[1]<10?'0'+timeParts[1]:timeParts[1]) + ':' + (timeParts[2]<10?'0'+timeParts[2]:timeParts[2]);

	  } else {
	    // return val unchanged
	    return val;
	  }
	  
  } catch (e) {
    // return val unchanged
    return val;
	}
}

/*
 * Copy the value from one input field to another.
 */
function copyValue(fid, tid) {
  try {
    var from = document.getElementById(fid);
    var to = document.getElementById(tid);
    if (to.value.length == 0) {
      to.value = from.value;
    }
  } catch (e) { }
}

/**********************************/
/* javascript for Bubble Tooltips */
/**********************************/

function enableTooltips(){
    var links,i,h;
    if(!document.getElementById || !document.getElementsByTagName) {
        return;
    }
    h=document.createElement("span");
    h.id="btc";
    h.setAttribute("id","btc");
    h.style.position="absolute";
    document.getElementsByTagName("body")[0].appendChild(h);
    links = document.getElementsByTagName("table");
    for(i=0;i<links.length;i++){
        if (links[i].title.indexOf("tip:") >= 0)
        {
            Prepare(links[i]);
        }
    }
}

function Prepare(el) {
    var tooltip,t,b,s,l;

    t=el.getAttribute("title");

    if(t==null || t.length==0) {
        return;
    }

    el.removeAttribute("title");
    tooltip=CreateEl("span","tooltip");
    s=CreateEl("div","top");

    var thisMatch = t.split(":");
    if (thisMatch[1]) {
        var testNode = document.getElementById(thisMatch[1]);
        var newNode = testNode.cloneNode(1);
        s.appendChild(newNode);
    } else {
        s.appendChild(document.createTextNode(t));
    }

    tooltip.appendChild(s);
    b=CreateEl("b","bottom");

    tooltip.appendChild(b);
    setOpacity(tooltip);
    el.tooltip=tooltip;
    el.onmouseover=showTooltip;
    el.onmouseout=hideTooltip;
    el.onmousemove=showTooltip;
}

function showTooltip(e){
    var d=document.getElementById("btc");
    if(d.childNodes.length==0) {
        d.appendChild(this.tooltip);
    }
    Locate(e);
}

function hideTooltip(e){
    var d=document.getElementById("btc");
    if(d.childNodes.length>0) {
        d.removeChild(d.firstChild);
    }
}

function setOpacity(el){
    el.style.filter="alpha(opacity:95)";
    el.style.KHTMLOpacity="0.95";
    el.style.MozOpacity="0.95";
    el.style.opacity="0.95";
}

function CreateEl(t,c){
    var x=document.createElement(t);
    x.className=c;
    x.style.display="block";
    return(x);
}

function Locate(e){
    var posx=0,posy=0;
    if (e==null) {
        e=window.event;
    }
    if (e.pageX || e.pageY) {
        posx=e.pageX; posy=e.pageY;
    } else if(e.clientX || e.clientY) {
        if(document.documentElement.scrollTop){
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
        } else{
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
        }
    }
    document.getElementById("btc").style.zIndex="10000";
    document.getElementById("btc").style.top=(posy+7)+"px";
    document.getElementById("btc").style.left=(posx-65)+"px";
}
/********************************************************************************************************/
/********************************************************************************************************/
/********************************************************************************************************/

