
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

// These are probably helping to preload into the browser,
//  but we're not using the image variable itself in changeImages().
//  That function is changing the src as a string.
//  So, we're counting on the browser caching the image, which is still ok.
//  These variable names are unused, but cannot match the image name itself.
//  Include this in body tag when needed: onload="preloadImages();"
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		// none for now. This is the format:
		// btn_cart_over = newImage("/img/btn_cart_over.gif");
		preloadFlag = true;
	}
}

// 2007-08-14 TaskID:5961 RBC
function PopProductImage(url)
{
  popwin = window.open(url,"winImg",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=500,height=500');
  popwin.focus();   // Bring it to the front when reloading it.
  self.name = "mainWin"; 
}

// findPos from www.quirksmode.org/js/findpos.html
function findPos(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent)
  {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent)
    {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft, curtop];
}

// Browser dimensions and scroll positions, still inconsisently implemented.
//  This is from www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
//  Usage: <textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea>
// This is a complex issue, but this script seems to handle most of the situations, including pasting. 
//  See comments at the bottom of www.codeproject.com/KB/aspnet/Textarea_Length_Validator.aspx for overkill.
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)
}

// Set an element's css class name.
function SetElementCssClass(elementId, className)
{
  var el = document.getElementById(elementId);
  if (el)
  {
    el.className = className;
  }
}

// Set an element's display style.
function SetElementDisplay(elementId, displayValue)
{
  var el = document.getElementById(elementId);
  if (el)
  {
    el.style.display = displayValue;
  }
}

// Toggle two elements' display styles.
//  Toggle one element to 'none', the other to 'block'.
//  ASSUME: For current state, look for 'none', else displayed;
//          since if the style isn't set, the element is displayed.
//  ASSUME: Element 1's current state determines the toggle.
function ToggleElementDisplays(elementId1, elementId2)
{
  var elem1 = document.getElementById(elementId1);  // Name avoids el1 confusion.
  var elem2 = document.getElementById(elementId2);
  if (elem1 && elem2)
  {
    if (elem1.style.display != "none")
    {
      elem1.style.display = "none";
      elem2.style.display = "block";
    }
    else
    {
      elem1.style.display = "block";
      elem2.style.display = "none";
    }
  }
}

// Toggle a 'read more' construct:
//  Expanding element's css class, Read More trigger's display text.
//  ASSUME: Use the class name to condition the current state.
//          If the class name is neither, it will default to state 1.
function ToggleMore(classElementId, className1, className2, moreElementId, moreText1, moreText2)
{
  var el = document.getElementById(classElementId);
  if (el)
  {
    var isState2 = (el.className == className1);
    el.className = (isState2) ? className2 : className1;

    // In here, instead of fully independent, because it assumed the class name conditions the state.
    el = document.getElementById(moreElementId);
    if (el)
    {
      el.innerText = (isState2) ? moreText2 : moreText1;
    }
  }
}

// Disable enter key from submitting the form.
//  Modified from www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx
function DisableEnterKey(e)
{
  // Condition IE or other browsers.
  var key = (window.event) ? window.event.keyCode : e.which;
  return (key != 13);
}

// Test if a variable contains a numeric value.
//  From answer here stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844
//  Describes this is the only flavor that passed all 30 of his unit tests here:
//  dl.dropbox.com/u/35146/js/tests/isNumber.html
function isNumeric(n)
{
  return !isNaN(parseFloat(n)) && isFinite(n);
}
