
var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;
var isIE = navigator.userAgent.indexOf("MSIE") != -1;
var isOpera = navigator.userAgent.indexOf("Opera") != -1;
var isSafari = navigator.userAgent.indexOf("AppleWebKit") != -1;

/**********************************************************
*  Functions used with 'tabbed' panels
**********************************************************/
function toggleTab(divname, tabidname, state)
{
   /* set the visibility of the tab's info */
   divObj = document.getElementById(divname);
   divObj.style.display = state;
   /* now set the "selectedness" of the tab itself */
   tabObj = document.getElementById(tabidname);
   if (state == 'none') { tabObj.className = "notselected"; }
   else { tabObj.className = "tabselected"; }
}

// function controls tab content visibility
function controlsubmenu(region, tabnum)
{
   var l = region.length;
   var r = document.getElementById(region);
   var y = r.getElementsByTagName('a');
   for (var i = 1; i <= y.length; i+=1)
   {
      var s1 = region+'__'+i;
      var s2 = region+'_'+i;
      toggleTab(s1, s2, 'none');
   }
   toggleTab(region+'__'+tabnum, region+'_'+tabnum, 'block');
}

function enteringTab()
{
   if (this.className == "notselected")
   {
      this.className = "notselected_hover";
   }
}
function leavingTab()
{
   if (this.className == "notselected_hover")
   {
      this.className = "notselected";
   }
}
function sortOn()
{
   /*this.style.cursor = 'hand';*/
   this.style.backgroundColor = "#FFFF33";
}
function sortOff()
{
   /*this.style.cursor='';*/
   this.style.backgroundColor = "#fff";
}

/**********************************************************
*  END OF Functions used with 'tabbed' panels
**********************************************************/


function submitForm(formToSubmit, target)
{
   formToSubmit.action = target;
   formToSubmit.submit();
}

function disableButtonById(id)
{
   var btn = document.getElementById(id);
   disableButton(btn);
}
   
function toggleButton(btn)
{
   if (isButton(btn))
   {
      btn.disabled = (btn.disabled === 0) ? 1 : 0;
   }
}
 
function disableButton(btn) { if (isButton(btn)) { btn.disabled = 1; } }
function enableButton(btn)  { if (isButton(btn)) { btn.disabled = 0; } }
function disableMyButton(id)
{
   var btn = null;
   var obj = document.getElementById(id);
   var len = obj.childNodes.length;
   for(var i = 0; i < len; i+=1)
   {   
      if (isButton(obj.childNodes[i]))
      {
         btn = obj.childNodes[i];
      }
   }
   if (btn) { disableButton(btn); }
   return btn;
}
function findAllButtons()
{
   var btns = new Array();
   var inps = document.getElementsByTagName("input");
   var len = inps.length;
   var j = 0;
   for(var i = 0; i < len; i+=1)
   {   
      if (isButton(inps[i]))
      {
         btns[j] = inps[i];
         j += 1;
      }
   }

   return btns;
}
function isButton(obj)
{
   if (obj === null) { return false; }
   if (obj === undefined) { return false; }
   if (obj.type && obj.type.match(/button/i)) { return true; }
   return false;
}
function disableAllButtons()
{
   var btns = findAllButtons();
   for (var i = 0; i < btns.length; i+=1) 
   {
      btns[i].disabled = 1;
   }
}
function enableAllButtons()
{
   var btns = findAllButtons();
   for (var i = 0; i < btns.length; i+=1) 
   { 
      btns[i].disabled = 0;
   }
}

function goToPage(loc)
{
   window.location.replace(loc);
}

/**********************************************************
*  A pair of functions to highlight table rows
**********************************************************/
function selectRow(x, col)
{
   x.style.backgroundColor = col;
}

function clearRow(x, col)
{
   x.style.backgroundColor = col;
}

function clearValue(id)
{
   var obj = document.getElementById(id);
   if (obj !== null) { obj.value = ''; }
}
function disableIt(id,val)
{
   if (!val) { val = false; }
   var obj = document.getElementById(id);
   if (obj !== null) { obj.disabled = val; }
}
function visIt(id,val)
{
   if (!val) { val = 'hidden'; }
   else { val = 'visible'; }
   var obj = document.getElementById(id);
   if (obj !== null) { obj.style.visibility = val; }
}
/**********************************************************
*  Functions that hide and show a window component
**********************************************************/
function show(id)
{
   var obj = document.getElementById(id);
   if (obj !== null) { obj.style.display = 'block'; }
}
function hide(id)
{
   var obj = document.getElementById(id);
   if (obj !== null) { obj.style.display = 'none'; }
}
function toggle(id)
{
   toggleObj(document.getElementById(id));
}
function toggleObj(obj)
{
   if (obj === null) { return; }
   var nm = obj.tagName.toUpperCase();
   if (isIE === true) {
      if (obj.style.display != 'none')      { obj.style.display = 'none'; }
      else if (obj.style.display == 'none') { obj.style.display = 'block'; }      
   } else if (nm == 'TD') {
      if (obj.style.display != 'none')      { obj.style.display = 'none'; }
      else if (obj.style.display == 'none') { obj.style.display = 'table-cell'; }      
   } else if (nm == 'TR') {
      if (obj.style.display != 'none')      { obj.style.display = 'none'; }
      else if (obj.style.display == 'none') { obj.style.display = 'table-row'; }      
   } else {
      if (obj.style.display != 'none')      { obj.style.display = 'none'; }
      else if (obj.style.display == 'none') { obj.style.display = 'block'; }      
   }
}

function isVisible(id)
{
   var shown = false;
   var obj = document.getElementById(id);
   if (obj !== null)
   {
      if (obj.style.display != 'none') { shown = true; }
   }
   return shown;
}
/*************************************************************
**  pops up a window for the given image, 
**  sized to exactly fit the image
*************************************************************/
function popWindow(winDoc,winTitle)
{
	if (document.all) 
	{
	  myY = window.screenTop;
	  myX = window.screenLeft;
	}
	else if (document.layers) 
	{
	  myX = window.screenX;
	  myY = window.screenY;
	}
	mouseX = window.event.x;
	mouseY = window.event.y;
	x = mouseX + myX;       
	y = mouseY + myY;  
	
	window.document.title = winTitle;      
    var settings = 'left='+x+',top='+y+',width=80,height=80,resizable=1,location=0,menubar=0,scrollbars=0,status=0,toolbar=0';
    //alert(settings);
	var imgWin = window.open('','_blank',settings);
	if( !imgWin ) { return true; } //pop up blockers should not cause errors
	imgWin.document.writeln(winDoc);
	imgWin.document.close();
	if( imgWin.focus ) 
	{ 
        imgWin.focus(); 
	}
	return false;
}

/*************************************************************
 **  Disable the enter key, thereby forcing users to use
 **  the various buttons to submit info
 **/
function disableEnterKey()
{
   if (window.event.keyCode == 13) window.event.keyCode = 0;
}

/*************************************************************
 **  Sumbit the form when the user presses the "Enter" key
 **/
function submitenter(myfield,e)
{
   var keycode;
   if (window.event) keycode = window.event.keyCode;
   else if (e) keycode = e.which;
   else return true;
   if (keycode == 13)
   {
      myfield.form.submit();
      return false;
   }
   else
      return true;
}
/*************************************************************
 **  Used in the drug editing screens to move from
 **  the current page to the next (if all fields validate)
 **/
function nextPage(form)
{
   if (validateAllFields())
   {
      form.submit();
   }
}

/*************************************************************
 **  These are used by the drug editing for to allow
 **  for partitioning the displayed form into several
 **  pages.
var currentLayer = 'page1';
function showLayer(lyr)
{
   hideLayer(currentLayer);
   document.getElementById(lyr).style.visibility = 'visible';
   currentLayer = lyr;
}

function hideLayer(lyr)
{
   document.getElementById(lyr).style.visibility = 'hidden';
}
 **/

/*************************************************************
 **  Some basic input validation functions
 **/
function validateNumber(field, msg, min, max) 
{
   if (!min) { min = 0 }
   if (!max) { max = 255 }

   if ( (parseInt(field.value) != field.value)
        || field.value.length < min
        || field.value.length > max) 
   {
      alert(msg);
      field.focus();
      field.select();
      return false;
   }
   return true;
}

function validatePrice(field, msg) 
{
   var min = 0;
   var max = 999999999;

   if ( (parseFloat(field.value) != field.value)
        || field.value < min
        || field.value > max) 
   {
      alert(msg);
      /*field.focus();
      field.select();*/
      return false;
   }
   return true;
}

function validateString(field, msg, min, max) 
{
   if (!min) { min = 1 } 
   if (!max) { max = 65535 }

   if (!field.value || field.value.length < min || field.value.max > max) 
   {
      alert(msg);
      field.focus();
      field.select();
      return false;
   }
   return true;
}

function numbersOnly(myfield, e, dec)
{
   var key = null;
   var keychar;
   var ans = false;
	
   if (window.event) key = window.event.keyCode;
   else if (e) key = e.which;

   if (key != null)
   {
      keychar = String.fromCharCode(key);
      if ((("0123456789").indexOf(keychar) > -1))
      {
         ans = true;
      }
   }
   return ans;
}



function getWidth()
{
   var w = 0;
   if (document.body.clientWidth)
   {
      w = document.body.clientWidth;
   }
   else if (window.innerWidth)
   {
      w = window.innerWidth;
   }
   return w;
}

function getHeight()
{
   var h = 0;
   if (document.body && document.body.clientHeight)
   {
      h = document.body.clientHeight;
   }
   else if (window.innerHeight)
   {
      h = window.innerHeight;
   }
   return h;
}



/***********************************************
* Cool DHTML tooltip script- ? Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
*
* Significantly modified by DAF
***********************************************/
//var ie = document.all;
//var ns6 = document.getElementById && !document.all;
var tip_DelayTime = 750 // Delay to hide in milliseconds
var tip_DisplayTimer = null;

function showTip(thelabel, thetext, thecolor, thewidth)
{
   stopTipTimer();
   var cmd = "ddrivetip('"+thelabel+"','"+thetext+"','"+thecolor+"','"+thewidth+"')";
   tip_DisplayTimer = setTimeout(cmd, tip_DelayTime);
}
function hideTip()
{
   stopTipTimer();
   var tipobj = getTipObj();
   if (tipobj != null)
   {
      tipobj.style.visibility="hidden"
   }
}

function stopTipTimer() 
{
   if (tip_DisplayTimer != null) clearTimeout(tip_DisplayTimer);
   tip_DisplayTimer = null;
}

function getTipObj()
{
  var tipobj = document.all ? 
                 document.all["dhtmltooltip"] : 
                 document.getElementById ? 
                    document.getElementById("dhtmltooltip") : 
                    "";
   return tipobj;
}

function ddrivetip(thelabel, thetext, thecolor, thewidth)
{
  var tipobj = getTipObj();
  if (tipobj != null)
  {
      if (thewidth != "") tipobj.style.width = thewidth+"px";
      if (thecolor != "") tipobj.style.backgroundColor = thecolor;
      tipobj.innerHTML = 
         '<div style="font-size:90%;"><b>'+thelabel+'</b><hr>'+thetext+'</div>';
      positionTip();
   }
   stopTipTimer();
   return false;
}

function positionTip()
{
   var tipobj = getTipObj();
   if (tipobj != null)
   {
	   var curX=xMousePos+5;  //=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
	   var curY=yMousePos+5;  //=(ns6)?e.pageY : event.y+ietruebody().scrollTop;
	   //Find out how close the mouse is to the corner of the window
	   var rightedge = xMousePosMax-20;
	   var bottomedge = yMousePosMax-20;
	   //var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
	   //var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
	
	   var leftedge=20;//(offsetxpoint<0)? offsetxpoint*(-1) : -1000

      //if the horizontal distance isn't enough to accomodate the width of the context menu
      if (rightedge < tipobj.offsetWidth)
         //move the horizontal position of the menu to the left by it's width
         //tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
         tipobj.style.left=xMousePos-tipobj.offsetWidth+"px";
      else if (xMousePos < leftedge)
         tipobj.style.left="5px"
      else
         //position the horizontal position of the menu where the mouse is positioned
         tipobj.style.left=(xMousePos+5)+"px"

      //same concept with the vertical position
      if (bottomedge < yMousePos+tipobj.offsetHeight)
         //tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
         tipobj.style.top=yMousePos-tipobj.offsetHeight+"px"
      else
         tipobj.style.top=(yMousePos+5)+"px"
         
      tipobj.style.visibility="visible"
   }
}

/***********************************************
* END OF
* Cool DHTML tooltip script- ? Dynamic Drive 
* DHTML code library (www.dynamicdrive.com)
***********************************************/

function trim(text) {
  	text = text.replace(/^\s*/, '');
  	text = text.replace(/\s*$/, '');
  	return text;
}

function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i+=1) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
}

/*************************************************************/
var rptXHR;
function getDetailRpt(url)
{ 
   rptXHR = null;
   grayOut(true);
   rptXHR = runXHR(gotDetailRpt, url);
}
function gotDetailRpt() 
{
   var ans = responseIsGood(rptXHR);
   if (ans) 
   { 
      ans = ans.trim();
      results = ans.split("~");
      informMsg('Details Exported',      
      'The details you requested are in this CSV file:'+
      '<div style="font-weight:bold;padding:4px;text-align:center">'+
      '<a href="/tools/retrieve?id='+results[0]+'" target="_blank">'+results[0]+'</a> ('+results[1]+')'+
      '</div>Right click on the file name and select "Save Target As..." '+
      'to download the report to a file on your computer. '+
      'Click the "Done" button to remove this dialog.',
      'Done','clearIt();');
      rptXHR = null;
   } 
   else if (responseIsExpired(rptXHR))
   {
      informMsg('Session Timeout',
      'Your login session has expired. Please log in again.',       
      'OK','clearIt();');
      dateXHR = null;
   }
}
var snapXHR;
function shootSnap(url)
{ 
   snapXHR = null;
   grayOut(true);
   snapXHR = runXHR(shotSnap, url);
}
function shotSnap() 
{
   var ans = responseIsGood(snapXHR);
   if (ans) 
   { 
      ans = ans.trim();
      results = ans.split("~");
      informMsg('Summary Exported',      
      'The summary you requested has been exported into this CSV file:'+
      '<div style="font-weight:bold;padding:4px;text-align:center">'+
      '<a href="/tools/retrieve?id='+results[0]+'" target="_blank">'+results[0]+'</a> ('+results[1]+')'+
      '</div>Right click on the file name and select "Save Target As..." '+
      'to download the report to a file on your computer. '+
      'Click the "Done" button to remove this dialog.',
      'Done','clearIt();');
      snapXHR = null;
   } 
   else if (responseIsExpired(snapXHR))
   {
      informMsg('Session Timeout',
      'Your login session has expired. Please log in again.',       
      'OK','clearIt();');
      snapXHR = null;
   }
}

function flipPlusMinus(btn)
{
   var pos = -1;
   if (btn.src) { pos = btn.src.indexOf("blue_minus.png"); }
   if (pos >= 0) { btn.src = "/images/blue_plus.png";  }
   else          { btn.src = "/images/blue_minus.png"; }
}
function showAll(lst)
{
   for(var i=0;i<lst.length;i+=1) { toggleObj(lst[i]); }
}
function clearIt()
{
   hidePopup();
   grayOut(false);
}
