/*Show an Inline Image Container
* Doesnt work in opera
*/
function showInline(id, url) {
   var div = document.getElementById(id+"_DIV");
   var imgTemp = new Image();
   imgTemp.src = url;
   if(imgTemp.complete) {
      displayInline();
   } else {
      imgTemp.onload =  displayInline;
   }  
   function displayInline() {
      var img = document.getElementById(id+"_IMG");
      div.style.display = "none";
      img.src = url;
      div.style.display = "block";
   }
}

/*Show a floating local Image Container
* Doesnt work in opera
*/
function showFloatLocal(linkref, url) {
   var div = document.getElementById("FLOAT_LOCAL_DIV");
   div.style.display = "none";

   var imgTemp = new Image();
   imgTemp.src = url;
   if(imgTemp.complete) {
      displayFloatLocal();
   } else {
      imgTemp.onload = displayFloatLocal;
   }
   function displayFloatLocal() {
      var img = document.getElementById("FLOAT_LOCAL_IMG");
      img.src = url;
      div.style.left = "0px";
      div.style.top = "0px";
      div.style.display = "block";
      var coords = getPageCoords(linkref);
      var tooltipX = coords.x + linkref.offsetWidth + 2;
      if ((div.clientWidth + tooltipX) > document.body.scrollWidth) {
         tooltipX -= 20;
         tooltipX -= linkref.offsetWidth;
         tooltipX -= div.clientWidth;
      }
      div.style.left = tooltipX + "px";
      div.style.top = coords.y + "px";
      div.style.display = "block";
   }
}

/*Show a floating bubble Image Container
* Doesnt work in opera
*/
function showFloatBubble(linkref, url) {
   var div = document.getElementById("FLOAT_BUBBLE_DIV");
   div.style.display = "none";

   var imgTemp = new Image();
   imgTemp.src = url;
   if(imgTemp.complete) {
      displayFloatBubble();
   } else {
      imgTemp.onload = displayFloatBubble;
   }
   function displayFloatBubble() {
      var img = document.getElementById("FLOAT_BUBBLE_IMG");
      img.src = url;
      div.style.left = "0px";
      div.style.top = "0px";
      
      div.style.display = "block";
      var coords = getPageCoords(linkref);
      var tooltipX = coords.x + linkref.offsetWidth;
      if ((div.clientWidth + tooltipX) > document.body.scrollWidth) {
         tooltipX -= linkref.offsetWidth;
         tooltipX -= div.clientWidth;
         document.getElementById("ic_tooltip-left-arrow").style.display = "none";
         document.getElementById("ic_tooltip-right-arrow").style.display = "block";
      } else {
         document.getElementById("ic_tooltip-left-arrow").style.display = "block";
         document.getElementById("ic_tooltip-right-arrow").style.display = "none";
      }
      div.style.left = tooltipX + "px";
      div.style.top = (coords.y - linkref.offsetHeight - (linkref.offsetHeight/2)) + "px";
      div.style.display = "block";
   }

}

/*Show a Floating page Image Container
* Doesnt work in opera
*/
function showFloatPage(url) {
   var div = document.getElementById("FLOAT_PAGE_DIV");
   var imgTemp = new Image();
   imgTemp.src = url;
   if(imgTemp.complete) {
      displayFloatPage();
   } else {
      imgTemp.onload = displayFloatPage;
   }
   function displayFloatPage() {
      var img = document.getElementById("FLOAT_PAGE_IMG");
      div.style.display = "none";
      img.src = url;
      div.style.display = "block";
   }
}

/*Close an Image Container
*/
function hideImage(id) {
   var div = document.getElementById(id+"_DIV");
   var img = document.getElementById(id+"_IMG");
   div.style.display = "none";
   img.src = "/prolog_common/gif/transparent.gif";
}


/**
*   Audit a client-side change to a component, so it can be actioned on the server.
*
*   Example: auditControlChange("SELTAB:", elem.parentNode.id, index);
*/
function auditControlChange(auditType, auditKey, value) {
   if (!auditType) { alert("auditControlChange cannot audit a null auditType. - check the javascript"); }
   if (!auditKey) { auditKey = ""; }
   if (!value) { value = ""; }
   var key = auditType + auditKey + "[";

   var firstFound = false;
   var controls = "";
   var inputs = document.getElementsByTagName("input");
   for (var index = 0; index < inputs.length; index++) {
      if (inputs[index].name == "prolog-form") {
         if (firstFound == false) {
            firstFound = true;
            controls = inputs[index].value;
            var foundIndex = controls.indexOf(key);
            if (foundIndex >= 0) {
               var startPos = foundIndex + key.length;
               var endPos = controls.indexOf("]", startPos);
               controls = controls.substr(0, startPos) + value + controls.substring(endPos, controls.length);
            } else {
               controls += key + value + "]";
            }
//            alert("Type: " + auditType + ", key: " + auditKey + ", value: " + value + ".\n controls: " + controls);
         }
         inputs[index].value = controls;
      }
   }
}

function quantityChange(upTriangle, up) {
   var inputField = getParent(getParent(upTriangle, "table").parentNode, "table").getElementsByTagName("input")[0];
   var value = inputField.value;
   if (value != null && value.length > 0) {
      value = parseInt(value, 10);
      if (isNaN(value)) {
         value = up ? 1 : 0;
      } else {
         value = value + (up ? 1 : -1);
      }
   } else {
      value = up ? 1 : 0;
   }
   if (value < 1) {
      value = "";
   }
   inputField.value = "" + value;
}

function maskKeyPress(ev, validChars)
{
   var iKeyCode, strKey;
   var keyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;

   if (document.all)
   {
      iKeyCode = ev.keyCode;
   }
   else
   {
      iKeyCode = ev.which;
   }
   strKey = String.fromCharCode(iKeyCode);

   if (!validChars.test(strKey) && !keyboardChars.test(strKey) && !checkClipboardCode(ev, strKey))
   {
      return false;
   }
}

function checkClipboardCode(ev, strKey)
{
   var clipboardChars = /[cvxz]/i;

   if (document.all)
   {
      return false;
   }
   else
   {
      return ev.ctrlKey && clipboardChars.test(strKey);
   }
}

/*
 * Used from a onkeypress event. Returns true if the key was enter, false otherwise.
 */
function enterPressed(e){
     if (!e) e = window.event;
     if (e.keyCode == 13){
		return true;
     } else {
		return false;
     }     
}

/*
 * submitOnEvent, clickLink and clickButton have all been stolen from
 * MyFaces.
 *
 * Fires a click event on an object.
 */
function submitOnEvent(componentId) {
    var clickComponent = document.getElementById(componentId);
    if ((clickComponent.nodeName && clickComponent.nodeName.toLowerCase() == "a")
        || (clickComponent.tagName && clickComponent.tagName.toLowerCase() == "a"))
    {
        clickLink(clickComponent);
        return false;
    }
    else if (clickComponent.type
        && (clickComponent.type.toLowerCase() == "submit" || clickComponent.type.toLowerCase() == "image"))
    {
        clickButton(clickComponent);
        return false;
    }
    else
    {
        //alert("SubmitOnEvent: don't know how to fire component '" + componentId + "'");
    }

    return true;
}

function clickLink(fireOnThis) {
    if (document.createEvent)
    {
        var evObj = document.createEvent('MouseEvents')
        evObj.initEvent('click', true, false)
        fireOnThis.dispatchEvent(evObj)
    }
    else if (fireOnThis.fireEvent)
    {
        fireOnThis.fireEvent('onclick')
    }
    else
    {
        //alert("SubmitOnEvent: your browser do not support fireing an event on a link");
    }
}

function clickButton(fireOnThis) {
    fireOnThis.click();
}

/*
 * Used to modify the appearance of the currently selected row in a table. (21/04/2006-STM)
 */
function singleSelect(elem) {
   var tableElem = getParent(elem, 'table');
   if (tableElem.selectedRow) {
      tableElem.selectedRow.className = tableElem.selectedRow.oldclassname;
   }

   elem.oldclassname = elem.className;
   elem.className = 'rowselected';
   tableElem.selectedRow = elem;
}

/**
 * Gets a parent of the specified element with the given tag name.
 * The parent that is returned may be the direct parent, or may
 * be higher up the DOM tree. (21/04/2006-STM)
 */
function getParent(elem, tagName)
{
   // If element is null then give up, otherwise if the element is a real
   // element (nodeType of 1 is element) then check the tag name,
   // otherwise recurse to the parent
   if (elem == null) {
      return null;
   } else
   if (elem.nodeType == 1 && elem.tagName.toLowerCase() == tagName.toLowerCase()) {
      return elem;
   } else {
      return getParent(elem.parentNode, tagName);
   }
}

function showTip(current, event, message) {
   var tooltipElem = document.getElementById("tooltip");

   if (tooltipElem == null)
   {
      var divElem = document.createElement("div");
      divElem.id = "tooltip";
      divElem.style.position = "absolute";
      divElem.style.display = "none";
      divElem.style.zIndex = "10";

      var bodyElem = document.getElementsByTagName("body")[0];
      bodyElem.appendChild(divElem);
      tooltipElem = document.getElementById("tooltip");
   } else
   if (tooltipElem.style.display != "none") {
      // Already showing, no need to redisplay
      return;
   }

   if (tooltipElem != null) {
      tooltipElem.innerHTML = formatTipMessage(message);
      tooltipElem.style.display = "";

      var coords = getPageCoords(current);
      var tooltipX = coords.x + 15;

      if ((tooltipElem.clientWidth + tooltipX) > document.body.scrollWidth) {
         tooltipX -= 19;
         tooltipX -= tooltipElem.clientWidth;
         document.getElementById("tooltip-left-arrow").style.display = "none";
      } else {
         document.getElementById("tooltip-right-arrow").style.display = "none";
      }

      tooltipElem.style.left = tooltipX + "px";
      tooltipElem.style.top = (coords.y - 22) + "px";
   }

}

function hideTip() {
   var tooltipElem = document.getElementById("tooltip");
   if (tooltipElem != null) {
      tooltipElem.style.display = "none";
   }
}

function formatTipMessage(message) {
   return "<table class=\"tooltiptable\" cellspacing=\"0\"><tr>" +
          "<td style=\"width:23px;height:8px;\"><img src=\"/prolog_common/tip-topleft-cnr.gif\"/></td>" +
          "<td style=\"background-image:url(/prolog_common/tip-topbar.gif);\"></td>" + 
          "<td style=\"width:23px;height:8px;\"><img src=\"/prolog_common/tip-topright-cnr.gif\"/></td>" +
          "</tr><tr>" +
          "<td style=\"background-image:url(/prolog_common/tip-leftbar.gif);vertical-align:top;\"><img id=\"tooltip-left-arrow\" src=\"/prolog_common/tip-dwnarrow.gif\" style=\"width:23px;height:24px;\"/></td>" +
          "<td style=\"background-color:#FFF;\">" + message + "</td>" +
          "<td style=\"background-image:url(/prolog_common/tip-rightbar.gif);vertical-align:top;\"><img id=\"tooltip-right-arrow\" src=\"/prolog_common/tip-dwnarrow-right.gif\" style=\"width:23px;height:24px;\"/></td>" +
          "</tr><tr>" +
          "<td style=\"height:10px\"><img src=\"/prolog_common/tip-btmleft-cnr.gif\"/></td>" +
          "<td style=\"background-image:url(/prolog_common/tip-btmbar.gif);\"></td>" +
          "<td><img src=\"/prolog_common/tip-btmright-cnr.gif\"/></td>" +
          "</tr></table>";
}

/* The following two functions are used for
 * finding the absolute coordinates of an object
 * on the page.
 */
function findPosX(obj) {
   var coords = getPageCoords(obj);
   return coords.x;
}

function findPosY(obj) {
   var coords = getPageCoords(obj);
   return coords.y;
}


function getPageCoords(element)
{
   var coords = {x: 0, y: 0};
/*
  Get the total offset from the offsetParent hierarchy
*/
	if (element.offsetParent)
	{
      obj = element;
		while (obj.offsetParent)
		{
         coords.x += obj.offsetLeft;

         if (obj.tagName == "TABLE" && (obj.style.display == "inline" || obj.style.display == "inline-table"))         
         {
            /** Tables displayed inline need their position calculating
             * differently. You cannot rely on the offset parent, so
             * instead, we calculate the offset as being:
             * offset from top of table to next offset parent
             * minus the height of the table (last row offset top + last row offset height)
             * plus the offset height of the table.
             */
            var inlineOffset = obj.offsetTop;
            var lastRow = obj.rows[obj.rows.length - 1];
            var fromEnd = 2;
            while (lastRow.style.display == "none") {
               // If the last row is hidden (ie. an expanded row), find the last visible row
               lastRow = obj.rows[obj.rows.length - fromEnd++];
            }
            inlineOffset -= lastRow.offsetTop;
            inlineOffset -= lastRow.offsetHeight;
            inlineOffset += obj.offsetHeight;
            if (isOpera()) {
               // Opera-specific code
               coords.y -= inlineOffset;
               coords.y += obj.offsetTop;
            } else {
               coords.y += inlineOffset;
   			}
         } else {
    			coords.y += obj.offsetTop;
   		}
   		
			obj = obj.offsetParent;
		}
	}
	else if (element.y)
	{
		coords.x += obj.x;
		coords.y += obj.y;
    }

/*
  Get the total scroll offset from the parentNode hierarchy
*/
	if (element.parentNode)
	{
      obj = element;
      while (obj.parentNode)
		{
         if  (obj.nodeName == "BODY") { 
            break;
         }
         /* Opera reports scrolling within tables for row offset.
            The following if statements prevent the deduction of bogus scrolling offset for Opera
         */
         // In Opera - don't deduct horizontal for the child of the TD.
			if (!isOpera() 
			|| (obj.parentNode.tagName != "TH" && obj.parentNode.tagName != "TD")) 
			{
            if ((obj.tagName == "TABLE" 
               && (obj.style.display == "inline" 
                  || obj.style.display == "inline-table"))         
            || (obj.tagName == "TR" 
               && (obj.parentNode.parentNode.style.display == "inline" 
                  || obj.parentNode.parentNode.style.display == "inline-table")))         
            {
               /* Don't deduct inline table scroll left. */
            } else {
               coords.x -= obj.scrollLeft;
            }
         }
         // In Opera - don't deduct vertical scrolling for the TR or the child of the TD.
			if (!isOpera() 
			|| (obj.tagName != "TR" 
	   		&& (!obj.parentNode 
   	   		|| (obj.parentNode.tagName != "TH" && obj.parentNode.tagName != "TD"))))
   		{
            if (obj.tagName == "TABLE" 
               && (obj.style.display == "inline" 
                  || obj.style.display == "inline-table"))         
            {
               /* Don't deduct inline table scroll top. */
            } else {
   		   	coords.y -= obj.scrollTop;
   		   }
			} else {
			}
			obj = obj.parentNode;
		}
	}
   return coords;
}

function isIE()
{
   return navigator.appName.indexOf("Microsoft") > -1;
}

function isOpera()
{
      return navigator.appName.indexOf("Opera") > -1; 
}

function toggleRow(cell) {
   var row = cell.parentNode;
   var table = row.parentNode.parentNode;
   var allRows = table.rows;
   var expandedRow = null;
   for (index = 0; index < allRows.length; index++) {
      if (allRows[index] == row) {
         expandedRow = allRows[++index];
         break;
      }
   }
   var isVisible;
   var images = cell.getElementsByTagName("img");
   for (index = 0; index < images.length; index++) {
      isVisible = toggleVisibility(images[index]);
      if (images[index].alt == "Expand") {
         expandedRow.style.display = isVisible ? "none" : "";
      } else {
         // Collapse
         expandedRow.style.display = isVisible ? "" : "none";
      }
   }
}

function toggleVisibility(element) {
   if (element.style.display == "none") {
      element.style.display = "";
      return true;
   } else {
      element.style.display = "none";
      return false;
   }
}

function areYouSure(prompt) {
	if (prompt == null) {
		prompt = 'Are you sure?';
	}
	return confirm(prompt);
}