function OrderHelp() {
  alert("The Order Number is made up of 3 parts -\n\n1. The Order Number.\n2. The Line Number.\n" +
    "3. The Cut Number.\n\n" + 
    "Each order has a unique order number, within that order each different product or requirement " + 
    "has a separate line number.\n\nWhen multiple cuts are ordered for the same product, they are given " +
    "the same line number and different cut numbers. Multiple cuts will only be cut from a single roll, " +
    "and will only be shipped in their entirety. " + 
    "If a \"Z\" cut is shown, this is a cut-up roll order and the Z cut represents the balance.");
}

function ShowStatusFAQ() {
  window.open("faq.asp?q=ostatus", "_blank");
}

function xstooltip_findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) { // IE only
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if (obj.x) curleft += obj.x;
  return curleft;
}

function xstooltip_findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) { // IE only
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }
  else if (obj.y) curtop += obj.y;
  return curtop;
}

function xstooltip_show(parentId, OffsetX, OffsetY, tip, MaxWidth) {
  it = document.getElementById('mytooltip'); // in inc_title.asp
  if (!it) return;
    
  parentobj = document.getElementById(parentId); 
  if (!parentobj) return;

  if (MaxWidth > 0) it.style.width = MaxWidth;
  else it.style.width = 'auto';
  it.innerHTML = tip;

  // not sure now what this is supposed to do...
  // if tooltip is too wide, shift left to be within parent 
  //if (OffsetX + it.offsetWidth > parentobj.offsetWidth) OffsetX = parentobj.offsetWidth - it.offsetWidth;
  //if (OffsetX < 0 ) OffsetX = 0; 
      
  x = xstooltip_findPosX(parentobj) + OffsetX;
  y = xstooltip_findPosY(parentobj) + OffsetY;

  it.style.top = y + 'px';
  it.style.left = x + 'px';

  setTimeout(xstooltip_ready, 800);
}

function xstooltip_ready() {
  it = document.getElementById('mytooltip');
  if (it.innerHTML != '') it.style.visibility = 'visible'; 
}

function xstooltip_hide() {
  it = document.getElementById('mytooltip');
  if (!it) return;
  it.innerHTML = '';
  it.style.visibility = 'hidden'; 
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function WindowSize(Width, Height) {
  var Width = 0, Height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    Width = window.innerWidth;
    Height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    Width = document.documentElement.clientWidth;
    Height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    Width = document.body.clientWidth;
    Height = document.body.clientHeight;
  }
  return [Width, Height];
}

sprintfWrapper = {
	init : function () {
		if (typeof arguments == 'undefined') { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != 'string') { return null; }
		if (typeof RegExp == 'undefined') { return null; }
 
		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;
 
		while (match = exp.exec(string)) {
			if (match[9]) convCount++;
 
			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
 
			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);
 
		if (matches.length == 0) { return string; }
		if (arguments.length - 1 < convCount) { return null; }
 
		var code = null;
		var match = null;
		var i = null;
 
		for (i=0; i < matches.length; i++) {
			if (matches[i].code == '%') substitution = '%';
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else substitution = matches[i].match;
 
			newString += strings[i];
			newString += substitution;
 
		}
		newString += strings[i];
 
		return newString;
	},
 
	convert : function(match, nosign) {
		if (nosign) match.sign = '';
		else match.sign = match.negative ? '-' : match.sign;

		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == '0' || nosign) return match.sign + pad + match.argument;
      else return pad + match.sign + match.argument;
		} 
    else {
			if (match.pad == '0' || nosign) 
				return match.sign + match.argument + pad.replace(/0/g, ' ');
      else return match.sign + match.argument + pad;
		}
	}
}
 
sprintf = sprintfWrapper.init;

function DigitsOnly(myfield, e) {
  var key, keychar;

  if (window.event) key = window.event.keyCode;
  else if (e) key = e.which;
  else return true;

  keychar = String.fromCharCode(key);

  // control keys
  if (key===undefined || key===null || key===0 || key===8 || key===9 || key===13 || key===27) return true;
  // numbers
  else if ((("0123456789").indexOf(keychar) > -1)) return true;
  return false;
}

// first table is often a page-wide description so should be ignored
function TableEqualWidths(WithinDivName, IgnoreFirstTable, IgnoreRowCountInTables) {
  var o = document.getElementById(WithinDivName);
  if (o) {
    var IsFirst = IgnoreFirstTable;
    var MaxWidths = new Array();
    var RequiredWidth = 0;
    for (var i = 0; i < o.children.length; i++) {
      var t = o.children[i];
      if (t && t.tagName == "TABLE") {
        if (IsFirst) {
          if (t.clientWidth > RequiredWidth) RequiredWidth = t.clientWidth;
          IsFirst = false;
        }
        else {
          var r;
          if (t.children.length > IgnoreRowCountInTables) r = t.children[IgnoreRowCountInTables];
          if (r && (r.tagName == "TBODY" || r.tagName == "THEAD")) 
            r = r.children[0];

          // nb: this only looks at the first row because we assume there's no word wrapping been done 
          // so the cols are at the max width required
          if (r && r.tagName == "TR") {
            for (var j = 0; j < r.children.length; j++) {
              if (j == MaxWidths.length) MaxWidths[j] = 0;
              var cell = r.children[j];
              if (cell && cell.tagName == "TD" && cell.clientWidth > MaxWidths[j]) 
                MaxWidths[j] = cell.clientWidth;
            }
          }
        }
      }
    }

    // have worked out widths, now set them
    IsFirst = IgnoreFirstTable;
    IsFirstBeingSet = true;
    for (i = 0; i < o.children.length; i++) {
      var t = o.children[i];
      if (t && t.tagName == "TABLE") {
        if (IsFirst) IsFirst = false;
        else {
          var r;
          if (t.children.length > IgnoreRowCountInTables) r = t.children[IgnoreRowCountInTables];
          if (r && (r.tagName == "TBODY" || r.tagName == "THEAD")) 
            r = r.children[0];

          if (r && r.tagName == "TR") {
            var Repeat;
            do {
              Repeat = false;
              for (var j = 0; j < r.children.length; j++) {
                var cell = r.children[j];
                if (cell && cell.tagName == "TD") {
                  // need to wrap the contents in a div to set the width; if just set the cell that doesn't
                  // fix the width
                  var wrap = document.createElement("div");
                  wrap.style.width = (MaxWidths[j] + 5) + "px"; // +5 for sort indicator
                  wrap.style.cssFloat = "left";
                  wrap.style.styleFloat = "left";
                  wrap.innerHTML = cell.innerHTML;
                  cell.innerHTML = "";
                  cell.appendChild(wrap);
                }
              }
              if (IsFirstBeingSet && RequiredWidth > 0 && t.clientWidth < RequiredWidth) {
                var Diff = RequiredWidth - t.clientWidth;
                while (Diff > 0) {
                  for (j = 0; Diff > 0 && j < MaxWidths.length; j++) {
                    MaxWidths[j]++;
                    Diff--;
                  }
                }
                Repeat = true;
              }
              IsFirstBeingSet = false;
            } while (Repeat);
          }
        }
      }
    }
  }
}

function isdigit(c) { return c >= '0' && c <= '9' ? true : false; }

