// common.js

// ==================================
// from Aaron's original (Macromedia)

// -----------------------------
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// -----------------------------
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// -----------------------------
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// -----------------------------
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

// ==================================
// Pat & Bob's


// ----------------------------------------------------
function insertText(fieldId, str, selectIt) { 

	var theField = document.getElementById(fieldId); if (!theField) {alert("bad fieldId"); return;}
	
	// insert a string into the middle of a field at the current text insertion point, and select the new text to highlight it

	var selectInsertedText = false;
	if (selectIt) selectInsertedText = true

	if (document.selection) {

		//IE support

		theField.focus();
		
		var sel = "";
		if (theField._selection_createRange) {
			sel = theField._selection_createRange;  // asynchronous, cached select range
		} else {
			sel = document.selection.createRange(); // will only work synchronously
		}
		sel.text = str;
		theField.focus(); // added this, it leaves the cursor sitting right at the end of the inserted text
	}
	
	else if (theField.selectionStart || theField.selectionStart == "0") {
	
		//MOZILLA/NETSCAPE support
	
		var startPos = theField.selectionStart;
		var endPos   = theField.selectionEnd;
		var text     = theField.value;
	
		theField.value = text.substring(0, startPos) + str + text.substring(endPos, text.length);

		if (selectInsertedText) {

			// select the inserted text
			theField.selectionStart = startPos; 
			theField.selectionEnd = theField.selectionStart + str.length; 
			
		} else {
		
			// just put the insertion point immediately following the inserted text
			theField.selectionStart = startPos + str.length;
			theField.selectionEnd = theField.selectionStart;
		}
		
		theField.focus();
	
	} else { 
	
		// everyone else, just place it on the end of the string
	
		theField.value += str;
		
	}
}


// ------------------------------------------------
function toggleCallbackFrame(showOrHide) {

	var theIFrameDiv = document.getElementById("CallbackIframeRegion");
	var theIFrame    = document.getElementById("CallbackIframe");
	var theToggleIframeLink = document.getElementById("toggleIframeLink");
	
	// if incoming desired state, then use it, else toggle the state
	var desiredState = "hide";
	if (theIFrameDiv.style.display == "none") desiredState = "show";
	if (showOrHide) desiredState = showOrHide; 
	
	if (desiredState == "show") {

		theIFrameDiv.style.display = "";
		theToggleIframeLink.innerHTML = "Hide Callback Frame";
		theIFrame.focus();

	} else {

		theIFrameDiv.style.display = "none";
		theToggleIframeLink.innerHTML = "Show Callback Frame";
		
		// clear the src of this iframe, so Safari won't try to visit it again
		if (navigator.userAgent.indexOf("Safari") > -1) {
			var theIframe = theIFrameDiv.getElementsByTagName("IFRAME")[0];
			theIframe.src = "";
		}

	}
		theToggleIframeLink.style.color = "cccccc"; // leave it showing once it's been used.
}

// -----------------------------
function navImage(imagePath, prefix, i, imgWidth, imgHeight, destinationImgId){
	var img = document.getElementById(destinationImgId);
	img.src = imagePath + "/" + prefix + "-" + i + ".jpg";
	img.width = imgWidth;
	img.height = imgHeight;
}

// -----------------------------
function swapImage(imgObjId, imgPath, imgWidth, imgHeight) {

	// Note: to avoid disturbing jittery distortion effect, it's important to size all your images the same size (see showImage.html), 
	// otherwise, all the browsers (IE, NS and Safari) will resize the old image to the new image size before swapping in the new image.
	
	var img = document.getElementById(imgObjId);
	img.width = imgWidth;
	img.height = imgHeight;
	img.src = imgPath;
}

// --------------------------------------------
function newImage(imgObjTDId, imgObjId, imgPath) {

	var theImageTD = document.getElementById(imgObjTDId);

	// recreate the image object

	theImageTD.innerHTML = '<img id="' + imgObjId + '" name="' + imgObjId + '" src="' + imgPath + '" border="0">';

}

// -----------------------------
function getUrlParams() {
	var params = new Array();
	if (location.href.indexOf("?") > -1) {
		var qString = location.href.split("?")[1];
		var nameValuePairs = qString.split("&");
		for (var i=0; i< nameValuePairs.length; i++) {
			var nameNValue = nameValuePairs[i].split("=");
			params[nameNValue[0]] = nameNValue[1];
		}
	}
	return params;
}
	
/*
// -----------------------------
function showImage(frameId, scriptPath, imgPath, imgWidth, imgHeight) {

	// an attempt to use a frame with the showImage(-old).html as a container which top/centers the requested image at the requested 
	// width/height in the right field, with the right background color... works but too many server revisits - slow and jittery, 
	// and besides haven't figured out how to set frame background color, so get white flashing... 
	
	var theFrame = document.getElementById(frameId);
	theFrame.src = scriptPath +"/showImage.html?img=" + imgPath + "&width=" + imgWidth + "&height=" + imgHeight;
}
*/

// --------------------------------------
function leftTrim(a) {
  var b = "";
  for (var i=0; i < a.length; i++) {
		if (a.charAt(i) != " " && a.charAt(i) != "\t" && a.charAt(i) != "\n" && a.charAt(i) != "\r") {
			b = a.substr(i);
			break;
		}
	}
  return b;
}

// --------------------------------------
function trim(a) {
  // trim on left
  re = /^\s*(.*)/;
  re.test(a);
  var b = RegExp.$1;
  // trim on right
  var c = mirror(b);
  re.test(c);
  var d = RegExp.$1;
  return mirror(d);
}
// --------------------------------------
function mirror(a) {
  var b = "";
  for (var i=a.length; i>=0; i--) {b += a.charAt(i)}
  return b;
}
