// ########## GENERAL TOOLS - START ##########

// #### CLEAN TEXT LEFT - START #### // Cleans up Text Leading (to the Left) spaces, tabs, carriage returns etc...
function CleanTextL(TxtStr) {
	var TxtStrTmp=TxtStr;					// Set temporary copy of Text
	var TxtStrLen=TxtStrTmp.length;				// get length of text string
	var ChopFlag=0;						// Set text changed flag to false
	while (TxtStrLen>0) {					// while there are still some characters left...
		if (TxtStrTmp.charCodeAt(0)<=32) {			// ...and if they are dud characters...
			TxtStrTmp=TxtStrTmp.slice(1);				// trim left character
			TxtStrLen--;						// decrement text charater length
			ChopFlag=1;						// set Changed flag to yes
		} else { break; }					// else break out of loop
	}
	if (ChopFlag==1) {					// if there was characters to chop, Confirm the chop...
		alert('There are redundant characters at the beginning of this text.\n(eg. spaces, tabs, carriage returns etc...)\n\nThey will be trimmed off.');
		TxtStr=TxtStrTmp;					// The TextString is now the chopped version
	}
	return TxtStr;						// Return the TextString
}
// #### CLEAN TEXT LEFT - END ####

// #### CLEAN TEXT RIGHT - START #### // Cleans up Text Trailing (to the Right) spaces, tabs, carriage returns etc...
function CleanTextR(TxtStr) {
	var TxtStrTmp=TxtStr;					// Set temporary copy of Text
	var TxtStrLen=TxtStrTmp.length;				// get length of text string
	var ChopFlag=0;						// Set text changed flag to false
	while (TxtStrLen>0) {					// while there are still some characters left...
		if (TxtStrTmp.charCodeAt(TxtStrLen-1)<=32) {		// ...and if they are dud characters...
			TxtStrTmp=TxtStrTmp.slice(0,(TxtStrLen-1));		// trim right character
			TxtStrLen--;						// decrement text charater length
			ChopFlag=1;						// set Changed flag to yes
		} else { break; }					// else break out of loop
	}
	if (ChopFlag==1) {					// if there was characters to chop, Confirm the chop...
		alert('There are redundant characters at the end of this text.\n(eg. spaces, tabs, carriage returns etc...)\n\nThey will be trimmed off.');
		TxtStr=TxtStrTmp;					// The TextString is now the chopped version
	}
	return TxtStr;						// Return the TextString
}
// #### CLEAN TEXT RIGHT - END ####

// #### DOUBLE CHECK TEXT - START #### // Trim end spaces, check length acceptable, check for unacceptable characters
function DblChTxt1(FormName,FieldName,MinChar) {
	var Field = document.forms[FormName].elements[FieldName];	// get form element name
	ChTxt=Field.value;					// get value of text string
	Field.select();						// Highlight all the text
	ChTxt=CleanTextL(ChTxt);				// Go chop Left Dud Characters
	Field.value=ChTxt;					// refresh text string field
	Field.select();						// Highlight all the text
	ChTxt=CleanTextR(ChTxt);				// Go chop Right Dud Characters
	Field.value=ChTxt;					// refresh text string field
	ChTxtL=ChTxt.length;					// get length of text string
	if (ChTxtL==0 && MinChar && MinChar!="" && MinChar!=0) {	// BOO BOO	// check if text string is empty
		alert("Oops, I didn't put anything in that box!");	// ...alert Boo Boo
		Field.value=ChTxt;					// put empty value into field
		Field.focus();						// focus on field
		return "1";						// pack up
	}
	if ((MinChar) && (MinChar != "") && (MinChar != 0)) {	// If a MinimumCharacters number was specified...
		if (ChTxtL<MinChar) {					// BOO BOO	// check if text string is shorter than acceptable (MinimumCharacters (from MinChar))
			Field.select();						// Highlight all the text
			alert(ChTxt + "\nis too short.  " + MinChar + " characters is the minimum here.");	// ...alert Boo Boo
			Field.value=ChTxt;					// put empty value into field
			Field.focus();						// focus on field
			return "1";						// pack up
		}
	}
	Field.value=ChTxt;					// refresh text string field
	return;							// pack up
}
// #### DOUBLE CHECK TEXT - END ####

// #### STRIP OUT ALL NON-NUMERIC CHARACTERS - START ####
function StripNonNumeric(TheVal) {
	HTMStrOut='';
	for (i=0; i<TheVal.length; i++) {
		Char=TheVal.charAt(i);
		if (isNaN(parseInt(Char))==false) {
			HTMStrOut+=Char;
		}
	}
	if (TheVal!=HTMStrOut) {
		alert('The number: \''+TheVal+'\'\n\n...contained non-numeric characters.\nThese have been stripped out.\n\nSearch will continue using\nthe number: \''+HTMStrOut+'\'');
	}
	return HTMStrOut;
}
// #### STRIP OUT ALL NON-NUMERIC CHARACTERS - END ####





// #### CHECK KEY STROKES - START ####
// EXAMPLE:
//    onKeyPress="ChkTxt(2,AlertCheck,'MyForm','MyField');"
//    (Also, create array eg.
//                         FldAlrt=new Array();
//                         FldAlrt[1]=0;
//    )
//
// INPUTS:
//   TFormat    = Code for what to do (1=Numbers Only), (2=Numbers & Decimal only)
//   AlertCheck = Array that holds names of Fields to mark if they have been alerted
//   FormName   = Name of form that fields live in
//   FieldName  = Name of field that we're checking
//
function ChkTxt(TFormat,AlertCheck,FormName,FieldName) {
	KT=event.keyCode;		// KeyText = code of key pressed
	if (KT==13) {			// if pressed 'Enter'
		// ADVANCED SEARCH
		DoSubmit();
		return;
	}
	if (TFormat==1) {
		// NUMERICS ONLY
		if (KT>=48 && KT<=57) {
			return;
		} else {
			event.returnValue=false;	// don't let the dud text be added to text box
			if (FldAlrt[AlertCheck]==0) {
				// HAVEN'T TOLD THEM YET
				alert('Numbers only');
				FldAlrt[AlertCheck]=1;		// Told them now
				document.FormName.FieldName.focus();
			}
		}
	} else if (TFormat==2) {
		// NUMERICS & DECIMAL ONLY
		if ((KT>=48 && KT<=57) || KT==46) {
			return;
		} else {
			event.returnValue=false;	// don't let the dud text be added to text box
			if (FldAlrt[AlertCheck]==0) {
				// HAVEN'T TOLD THEM YET
				alert('Numbers & Decimal point only');
				FldAlrt[AlertCheck]=1;		// Told them now
				document.forms[FormName].elements[FieldName].focus();
			}
		}
	}
}
// #### CHECK KEY STROKES - END ####


// ########## GENERAL TOOLS - END ##########
