// JavaScript Document

	formSubmitOnce=true;	

	function GetParentNodeWithTagName( objNode, strTagName ) {
	// travels up from node until a parent with given tag name.
		strTagName = strTagName.toLowerCase();
		for ( 
			objNode = objNode.parentNode ; 
			(objNode && (			
					(objNode.tagName && (objNode.tagName.toLowerCase() != strTagName)) ||
					(!objNode.tagName && (objNode.nodeType != 3)) 
				)
			);
			objNode = objNode.parentNode) {
				// Do nothing 
		};
		// Return the node. 
		return( objNode );
	}

	function doSubmitValue(theLink, theField, theValue) {
		if (formSubmitOnce) {
			if (typeof(theLink)=="object") {
				linkWait(theLink);
				doCancel();
				theForm=GetParentNodeWithTagName(theLink,"form");
				if ((typeof(theForm[theField])=="object")&&(typeof(theValue)=="string")) {
					theForm[theField].value=theValue;
					theForm.submit();
				} else {
					linkRestore(theLink);
				};
			};
		};
	};

	function doReset(theLink) {
		if (typeof(theLink)=="object") {
			theLink.href="javascript:void(0)";
			doCancel();
			theLink.blur();
			theForm=GetParentNodeWithTagName(theLink,"form");
			if (typeof(theForm)=="object") {
				theForm.reset();
			};
		};
		return false;
	};

	function doSubmit(theLink,theValidate) {
		if (formSubmitOnce) {
			if (typeof(theLink)=="object") {
				linkWait(theLink);
				doCancel();
				theForm=GetParentNodeWithTagName(theLink,"form");
				if ((String(theValidate)=="undefined")||(theValidate)) {
					formSubmitOnce=false;
					theForm.submit();
				} else {
					linkRestore(theLink);
				};
			};
		};
		return false;
	};

function doDelete(theLink) {
	if (formSubmitOnce) {
		if (GB_Confirm("This will permanently remove this item from your database. Are you sure you want to proceed?")) {
			doSubmit(theLink);
		};	
	};
	return false;
};

function linkWait(theLink) {
	if (typeof(theLink)=="object") {
		theLink.href="javascript:void(0)";
		theLink.orgHTML=theLink.innerHTML;
		theLink.innerHTML="Please wait";
		theLink.blur();
	};
};

function linkRestore(theLink) {
	if (typeof(theLink)=="object") {
		if (String(theLink.orgHTML)!="undefined") {	theLink.innerHTML=theLink.orgHTML };
		theLink.focus();
	};
};

function doCancel() {
	if (window.event) {
		window.event.cancelBubble=true;
		window.event.returnValue=false;
	};
};
	
function doFocusFirst() {
// loop through all forms on the page
for(var f=0; f < document.forms.length; f++) {
	var frm = document.forms[f];
	// loop through all fields in the form
	for(var x=0; x < frm.length; x++) {
		var fld = frm[x];
		// use a try/catch block so that if anything fails we just go to the next field
		try {
			if(!fld.isDisabled && !fld.readOnly) {
				switch(fld.type) {
					case 'text':
					case 'password':
						fld.select();
						fld.focus();
						return;
						// for check boxes and radio buttons, set the focus to the one that is already
						// checked in the group.
					case 'checkbox':
					case 'radio':
						// if it's not an array, there's only one, so focus on it
						if (frm[fld.name].length == undefined) {
							fld.focus();
						} else {
							// find which one is checked and focus on it
							for (var x=0; x < frm[fld.name].length; x++) {
								fld = frm[fld.name][x];
								if (fld.checked) {
									fld.focus();
									break;
								};
							};
						// if we didn't find the checked one, just focus on the first one
							if (x == frm[fld.name].length) {
								fld = frm[fld.name][0].focus();
							};	
						};
						return;
						// select boxes and buttons can just have a simple focus() call
					case 'select-one':
					case 'select-multiple':
					case 'button':
						fld.focus()
						return;
					};
				};
			}
			// if anything fails for any reason (hidden div, etc.), execution jumps to catch
			// then goes through the loop again
			catch(e) {};
		};
	};
};

function GB_Confirm(confText) {
	confText=GB_TextWrap(50,"\n",unescape(confText));
	return confirm(confText);
};

function GB_TextWrap(gWidth,gSep,gString){
  gArOut=new Array();
  while(gString!=""){
    if(gString.length > gWidth) {
      x=gString.lastIndexOf(" ",gWidth)+1
      gArOut[gArOut.length]=gString.slice(0,x);
      gString=gString.slice(x);
    } else {
      gArOut[gArOut.length]=gString;
      gString="";
    };
  };
  for(x=0;x<gArOut.length;x++) {
    gString+=gArOut[x]+gSep;
  };
  gArOut=null;
  return gString;
}; // end GB_TextWrap


function textCounter(theField, theCountField, theCount) {
	if (theField.value.length > theCount) {
		theField.value = theField.value.substring(0, theCount);
	} else { 
		theCountField.value = theCount - theField.value.length;
	};
};
