
// Check browser info
var brwVersion = 0;
var brwName = "";
var brwCompliant = false;
if (navigator.appName.indexOf("Microsoft") != -1)
{
   var ieidx = navigator.appVersion.indexOf("MSIE"); // MSIE 5.01;
   if (ieidx != -1) brwVersion = navigator.appVersion.substr(ieidx + 5);
   brwVersion = brwVersion.replace(/;.*$/, "");
   if (navigator.appName == "Microsoft Internet Explorer") brwName = "Internet Explorer";
   if (brwVersion >= 4 && navigator.appName == "Microsoft Internet Explorer")
      brwCompliant = true;
   else
      alert("Application not tested with this browser/version (" + navigator.appName + "/" + brwVersion + ")");
   brwName = "Internet Explorer";
}

function errorHandler(msg, URL, lineNum)
{
   var l = msg.length;
   var ch = msg.charAt(l-1);
   if (ch != '\n') msg = msg + "\n";
   alert("Error : " + msg + "Line " + lineNum + " (" + brwName + ", " + brwVersion + " : " + navigator.appVersion + ")\n" + URL);
   return(true);
}

// Set-up a custom error handler
window.onerror = errorHandler;

// Constants
var EDIT_TEXT_COLOR = "black";
var EDIT_BG_COLOR = "white";
var READ_TEXT_COLOR = "rgb(128,128,128)";
var READ_BG_COLOR = "rgb(255,255,221)"; //"rgb(221,222,255)"; //"rgb(255,255,192)"; //rgb(238,238,238)";

var KEY_BACKSPACE = 8;
var KEY_DELETE = 46;
var KEY_ESCAPE = 27;
var KEY_DOWN = 40;
var KEY_UP = 38;
var KEY_FIREFOX_F1 = 112;

/**
 * Quick and easy case conversion function.
 */
function toCaseConvert(s)
{
   var str = "";
   var word, sep;
   var prevIndex = -1;
   var re = new RegExp(/[^ \t\.,\-\']{1,}/g);
   while (re.exec(s))
   {
      // Check for preceding non-matching characters
      if (prevIndex == -1 && RegExp.index > 0) str = s.substring(0, RegExp.index);

      // Split out the word and preceding separator
      word = s.substring(RegExp.index, RegExp.lastIndex);
      sep = (prevIndex == -1 ? "" : s.substring(prevIndex, RegExp.index));
      prevIndex = RegExp.lastIndex;
      //alert(RegExp.index + ":" + word + "(" + sep + ")");

      // Case convert the word
      word = word.toLowerCase();
      word = word.substring(0, 1).toUpperCase() + word.substring(1);
      if (word.substring(0,2) == "Mc")
         word = "Mc" + word.substring(2,3).toUpperCase() + word.substring(3);
      else if (word.substring(0,3) == "Mac" &&
               word != "Macclesfield" &&
               word != "Macedonia" &&
               word != "Macabre" &&
               word != "Macaque" &&
               word != "Machipisa" &&
               !word.match(/^Machin/) &&
               word != "Machismo" &&
               word != "Macho" &&
               word != "Mack" &&
               word != "Mackerel" &&
               word != "Mackintosh" &&
               word != "Macrame" &&
               word != "Machell" &&
               word != "Machen" &&
               !word.match(/^Macro/) &&
               !(word.match(/^Mace/) && word.length <= 5))
         word = "Mac" + word.substring(3,4).toUpperCase() + word.substring(4);

      // Concatenate to a new string
      str = str + sep + word;
   }

   // Check for suffixed non-matching characters
   if (prevIndex != s.length) str += s.substring(prevIndex, s.length);

   return(str);
}

function getParameter(name)
{
   var paramlist = document.location.toString().replace(/^.*\?/, "");
   var fld = paramlist.split("&");
   var i, vname, value;
   for (i=0; i<fld.length; i++)
   {
      vname = fld[i].replace(/\=.*$/, "");
      if (vname == name)
      {
         value = fld[i].replace(/^[^=]*=/, "");
         return(value);
      }
   }

   return(null);
}

function hasProperty(obj, prop)
{
   for (p in obj)
      if (p == prop) return(true);
   return(false);
}

function getCookie()
{
   //GOZ var cookie = top.location.toString().replace(/^.*cookie=/, "");
   //var cookie = location.toString().replace(/^.*cookie=/, "");
   //cookie = cookie.replace(/\&.*$/, "");
   return(document.getElementById("cookie").value);
}

function getSessionManager()
{
   //GOZ var manager = top.location.toString().replace(/^.*session\.manager=/, "");
   var manager = location.toString().replace(/^.*session\.manager=/, "");
   manager = manager.replace(/\&.*$/, "");
   return(manager);
}

function getSessionApplication()
{
   //GOZ var application = top.location.toString().replace(/^.*session\.application=/, "");
   var application = location.toString().replace(/^.*session\.application=/, "");
   application = application.replace(/\&.*$/, "");
   return(application);
}

function setDocumentSize(width, height)
{
   window.resizeTo(width, height);
   var wd = width - document.body.clientWidth;
   var hd = height - document.body.clientHeight;
   window.resizeTo(width + wd, height + hd);
}

var minWidth = 0, minHeight = 0;
function setMinimumWindowSize(width, height)
{
   minWidth = width;
   minHeight = height;
   window.attachEvent("onresize", checkMinimumWindowSize);
   checkMinimumWindowSize(null);
}

function checkMinimumWindowSize(ev)
{
   var wd = minWidth - document.body.clientWidth;
   var hd = minHeight - document.body.clientHeight;
   if (wd > 0 || hd > 0)
   {
      if (wd < 0) wd = 0;
      if (hd < 0) hd = 0;
      setDocumentSize(document.body.clientWidth + wd, document.body.clientHeight + hd);
   }
}

var CURSOR_DEFAULT = 1;
var CURSOR_WAIT = 2;
function setCursor(ref)
{
   var cursor;

   switch (ref)
   {
      case CURSOR_WAIT:
         cursor = "wait";
         break;

      case CURSOR_DEFAULT:
      default:
         cursor = "default";
         break;
   }

   var strSearch = "cursor {0,}: {0,}[a-z]{1,}";
   var strReplace = "cursor:" + cursor;

   // Sub Frames
   for (var i=0; i<document.frames.length; i++)
      changeStyle(document.frames[i].document, strSearch, strReplace);

   // Main Body
   changeStyle(document, strSearch, strReplace);

   // Framework
   changeStyle(top.document, strSearch, strReplace);

   // To make sure the cursor doesn't get stuck
   document.focus();
}

function changeStyle(doc, strSearch, strReplace)
{
   for (var i=0; i<doc.styleSheets.length; i++)
   {
      var re = new RegExp(strSearch, "gi");
      doc.styleSheets[i].cssText = doc.styleSheets[i].cssText.replace(re, strReplace);
   }
}

function changeClassName(elem, strSearch, strReplace)
{
   var re = new RegExp(strSearch, "gi");

   if (elem.className != null)
      elem.className = elem.className.replace(re, strReplace);

   for (var i=0; i<elem.childNodes.length; i++)
   {
      if (elem.childNodes[i].className != null)
         elem.childNodes[i].className = elem.childNodes[i].className.replace(re, strReplace);
      if (elem.childNodes[i].hasChildNodes()) changeClassName(elem.childNodes[i], strSearch, strReplace);
   }
}

function showProperties(obj)
{
   var txt = "";
   for (var prop in obj)
   {
      var val = eval("obj." + prop);
      val = "" + val;
      if (val.match(/^function/)) continue;
      if (val.match(/^\s{0,}$/)) continue;
      txt += (prop + " = " + val + "\n");
   }
   alert(txt);
}

function formHistory(servletName)
{
   // Make sure a record is selected
   if (viewGetSelectedRow() == null)
   {
      alert("No record selected");
      return;
   }

   var width = 650;
   var height = 300;
   var x = top.screenLeft + ((document.body.clientWidth - width)/2);
   var y = top.screenTop + ((document.body.clientHeight - height)/2);

   winWait = window.open((servletName + "?action=viewHistory&cookie=" + getCookie() + "&key=" + viewGetSelectedPrimaryKey()),"wait","width=" + width + ",height=" + height + ",dependent=yes,fullscreen=no,location=no,resizable=yes,status=no,menubar=no,scrollbars=no,toolbar=no,top=" + y + ",left=" + x + ",edge=raised,unadorned=yes");
}

function isBlank(location)
{
   location = location.toString();
   var isBlank = (location == "about:blank" || location.match(/[^a-zA-Z_]blank\.html/) != null);
   return(isBlank);
}

/**
 * Replace any <!--MAP:property--> tags in the supplied text with the value
 * of each named property as taken from the specified object.
 */
function replaceTags(txt, obj)
{
   for (prop in obj)
   {
      var re = new RegExp("<\!\-\-MAP:" + prop + "\-\->", "g");
      txt = txt.replace(re, eval("obj." + prop));
   }
   return(txt);
}

/**
 * Get a string copy of the hidden DIV HTML represented by the specified id
 * and remove any txt_ prefixes from it.
 */
function getHTMLText(id)
{
   var el = document.getElementById(id);
   var txt = el.innerHTML.toString().replace(/txt_/g, "");
   return(txt);
}

/**
 * Used to limit text within a field
 */
function limitText(limitField, limitCount, limitNum)
{
   if (limitField.value.length > limitNum)
      limitField.value = limitField.value.substring(0, limitNum);
   else
   {
      if (limitCount != null) limitCount.value = limitNum - limitField.value.length;
   }
}

/**
 * Find the absolute position of the specified object
 */
function findPos(obj)
{
   var curleft = curtop = 0;
   if (obj.offsetParent)
   {
      do
      {
         curleft += obj.offsetLeft;
         curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
   }

   return[curleft,curtop];
}

/**
 *
 */
function formatPrice(val)
{
   val = val.toString();
   if (val == "" || val == null || val == 0) return("0.00");
   var dp = val.indexOf(".");
   if (dp == -1) return(val + ".00");
   var pnds = val.substring(0, dp);
   pnds = pnds.replace(/^$/, "0");
   var pnce = val.substr(dp + 1);
   pnce = pnce.replace(/^$/, "00");
   if (pnce.length == 1) pnce = pnce + "0";

   // Watch out for rounding issues ...
   var carry = false;
   for (var i=pnce.length-1; i>=2; i--)
   {
      var digit = pnce.substring(i,i+1);
      if (carry) digit++;
      if (digit >= 5) carry = true;
      else carry = false;
   }
   pnce = pnce.substring(0, 2);
   if (carry) pnce = (1 * pnce) + 1;
   pnce = "" + pnce;
   if (pnce.length == 1) pnce = "0" + pnce;

   val = pnds + "." + pnce;
   return(val);
}

function setInnerText(elem, txt)
{
   if (elem.innerText == null || elem.innerText == "undefined") elem.textContent = txt;
   else elem.innerText = txt;
}

function getInnerText(elem)
{
   if (elem.innerText == null || elem.innerText == "undefined") return(elem.textContent);
   else return(elem.innerText);
}

function setStyle(elem, prop, value)
{
   var styleRef = elem;
   if (styleRef.style) { styleRef = styleRef.style; }
   eval("styleRef." + prop + " = \"" + value + "\"");
}

function getStyle(elem, prop)
{
   var styleRef = elem;
   if (styleRef.style) { styleRef = styleRef.style; }
   return(eval("styleRef." + prop));
}

function setSelectedOption(select, value)
{ 
   for (var i=0; i<select.options.length; i++)
   {
      if (select.options[i].value == value)
      {
         select.selectedIndex = i;
         break;
      }
   }
}

