function jsUCase(s)
{
    return String(s).toUpperCase();
}

// IN: str - the string whose length we are interested in
// RETVAL: The number of characters in the string
function jsLen(str)
{
	return String(str).length;
}


function jsRight(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else 
    {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//IN: str - the string we are LEFTing
//    start - our string's starting position (0 based!!)
//    len - how many characters from start we want to get
//RETVAL: The substring from start to start+len
function jsMid(str, start, len)
{
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) return "";

	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;

	return String(str).substring(start,iEnd);
}


// InStr function written by: Steve Bamelis - steve.bamelis@pandora.be
function jsInStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < jsLen(strSearch); i++)
	{
	    if (charSearchFor == jsMid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}


function constrainToAlphaKeyPress(evt, obj)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);

		if (((key >= 65) && (key <= 90)) || ((key >= 97) && (key <= 122)) ||(key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46))
			return true;
		else
			return false;
	}
}


function constrainToNumericKeyPress(evt, obj)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);
		
		if (((key >= 48) && (key <= 57)) || (key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46) || (key == 190))
			if (((key == 46) || (key == 190)) && (jsInStr(obj.value, ".") > 0))
				return false;
			else
				return true;
		else
			return false;
	}
}


function constrainToNumberKeyPress(evt)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);
		if (((key >= 48) && (key <= 57)) || (key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46))
			return true;
		else
			return false;
	}
}


function convertKeyToUpperCase(evt)
{
    if (!evt) evt = window.event;

    var key = evt.keyCode;
    if (!key)
    {
        key = evt.keycode;
    }
    if (!key)
    {
		key = evt.charCode;
    }

    key = parseInt(key);

    if ((key >= 97) && (key <= 122))
    {
        evt.charCode = key - 32;
    }
}


//  check for valid numeric strings	
function jsIsNumeric(sString)
{
	var sValidChars = "0123456789.";
	var sChar;
	var blnResult = true;

//   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
	for (i = 0; i < sString.length && blnResult == true; i++)
	{
		sChar = sString.charAt(i);
		if (sValidChars.indexOf(sChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}


function isMaxLength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}


function doPriceSync(gp, bShowWithGST, obj)
{
	// Note: Keep tenths of cents...
	var p1 = obj.value;

	if (bShowWithGST)
	{
		obj.value = Math.round(p1 * (1.0 + gp / 100.0) * 1000) / 1000.0;
	}
	else
	{
		obj.value = Math.round(p1 / (1.0 + gp / 100.0) * 1000) / 1000.0;
	}
}


function window_openWH(url, w, h)
{
	window.open(url, '_blank', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h);
}


function validateEmail(obj, msg, bAllowBlank)
{
 var str = obj.value;
 if (jsTrim(str) == "")
 {
  if (bAllowBlank)
  {
   return true;
  }
  else
  {
   alert(msg);
   obj.focus();
   return false;
  }
 }

 var at = "@";
 var dot = ".";
 var lat = str.indexOf(at);
 var lstr = str.length;
 var ldot = str.indexOf(dot);

 if ( (str.indexOf(at) == -1)
   || (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
   || (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
   || (str.indexOf(at, (lat + 1)) != -1)
   || (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot)
   || (str.indexOf(dot, (lat + 2)) == -1)
   || (str.indexOf(" ") != -1) )
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}

 
function nonWS(s)
{
	if (s == " ") return false;
	if (s == "\t") return false;
	return true;
}


function jsTrim(s)
{
 var nLen = s.length;
 var nStart = 0;
 var nEnd = nLen - 1;
 var i;

 for (nStart = 0; nStart <= nEnd; nStart++)
  if (nonWS(s.charAt(nStart))) break;

 for (nEnd = nLen - 1; nEnd >= nStart; nEnd--)
  if (nonWS(s.charAt(nEnd))) break;

 return s.substr(nStart, nEnd - nStart + 1);
}


function validateNoQuotes (obj, msg)
{
 var s = new String(obj.value);
 var i;
 for (i = 0; i < s.length; i++)
 {
  switch (s.charAt(i))
  {
   case "'":
   case '"':
    alert(msg);
    obj.focus();
    return false;
  }
 }
 return true;
}


function validateNonZero(obj, msg)
{
	if ((obj == null) || (obj.value == '0'))
	{
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}


function validatePresence(obj, msg)
{
 if (obj == null)
 {
  return true;
 }
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceAlphaNumeric(obj, msg, msgAlpha)
{
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 if (!isStringAlphaNumeric(obj.value))
 {
  alert(msgAlpha);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceNonNegNumber(obj, msg)
{
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 if (!isFinite(obj.value) || obj.value < 0)
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}


function validateLimit(obj, nLimit, sLimitMsg)
{
 if (obj.value.length > nLimit)
 {
  alert(sLimitMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceLimit(obj, sMsg, nLimit, sLimitMsg)
{
 if (jsTrim(obj.value) == "")
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value.length > nLimit)
 {
  alert(sLimitMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validateCurrency(sName, sMessage)
{
 var ta;
 ta = document.getElementById(sName);
 if (ta)
 {
  if (!isFinite(ta.value))
  {
   alert(sMessage);
   ta.focus();
   return false;
  }
 }
 return true;
}


// Ensure an input value is either blank, or a number between limits.
function validateNumberBoundOrBlank(obj, sMsg, nLow, nHigh, sBoundMsg)
{
	if (jsTrim(obj.value) == "")
	{
		return true;	// A blank input is OK!
	}

	if (!isFinite(obj.value))
	{
		alert(sMsg);
		obj.focus();
		return false;
	}

	if (obj.value < nLow || obj.value > nHigh)
	{
		alert(sBoundMsg);
		obj.focus();
		return false;
	}
	return true;
}


function validatePresenceBound(obj, sMsg, nLow, nHigh, sBoundMsg)
{
 if (!isFinite(obj.value))
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value < nLow || obj.value > nHigh)
 {
  alert(sBoundMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceAtLeast(obj, sMsg, nLow)
{
 if (jsTrim(obj.value) == "")
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (!isFinite(obj.value))
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value < nLow)
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 return true;
}

function isStringAlphaNumeric(s)
{
 var i;
 for (i = s.length - 1; i >= 0; i--)
  if (!isCharAlphaNumeric(s.charAt(i))) return false;

 return true;
}


function isCharAlphaNumeric(c)
{
 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_'));
}


