
function CheckControl2(pstrControlObject, pstrMsg, pblnRequired, plngMinLength, plngMaxLength) {
	//
	// Check that field is required
	//
	if (pblnRequired) {
		if (pstrControlObject.value.length == 0) {
			alert(pstrMsg + " is a required field.");
			pstrControlObject.focus();
			pstrControlObject.select();
			return false
		}
	}
			
	//
	// Check maximum length of field
	//
	if (pstrControlObject.value.length > plngMaxLength) {
		alert(pstrMsg + " can only be " + plngMaxLength + " characters long!");
		pstrControlObject.focus();
		pstrControlObject.select();
		return false;
	}
	
	//
	// Check minimum length of field
	//
	if (plngMinLength > 0) {
		if (!pstrControlObject.value.length > plngMaxLength) {
			alert(pstrMsg + " must be at least be " + plngMinLength + " characters long!");
			pstrControlObject.focus();
			pstrControlObject.select();
			return false
		}
	}
	
	return true;
}

function CheckControl(strControlValue, strMsg, iLength, blnSel)
{
	if (blnSel)
	{
		if (strControlValue == "0") 
		{
			alert("Please choose a valid " + strMsg + "!");
			bValidUser = false;
		}
		if (strControlValue.length > iLength)
		{
			alert(strMsg + " can only be " + iLength + " characters long!");
			bValidUser = false;
		}
	}
	else
	{
		if (strControlValue.length > iLength)
		{
			alert(strMsg + " can only be " + iLength + " characters long!");
			bValidUser = false;
		}
		else if (strControlValue == "")
		{
			alert(strMsg + " can not be empty!");
			bValidUser = false;
		}
	}
}



function trimString(inString) {
	var outString;
	var startPos;
	var endPos;
	var ch;
	
	// where do we start?
	startPos = 0;
	ch = inString.charAt(startPos);
	while ((ch == " ") || (ch == "\b") || (ch == "\f") || (ch == "\n") || (ch == "\r") || (ch == "\n")) {
		startPos++;
		ch = inString.charAt(startPos);
	}

	// where do we end?
	endPos = inString.length - 1;
	ch = inString.charAt(endPos);
	while ((ch == " ") || (ch == "\b") || (ch == "\f") || (ch == "\n") || (ch == "\r") || (ch == "\n")) {
		endPos--;
		ch = inString.charAt(endPos);
	}

	// get the string
	outString = inString.substring(startPos, endPos + 1);

	if ((outString == " ")||(outString.charAt(0) == " ")) {
		outString = ""	
	}

	return outString;
}


function Validate(strControlValue, strMsg, regExpStr, flagsString)  {
	var reStr = regExpStr;
	var flags = flagsString;
	var str = strControlValue;
	re = new RegExp(reStr, flags)
	if (!re.test(str)){
		alert(strMsg);
		bValidUser = false
	}
}


/* ****************************************************************************

	Return a reference to an object with the id of pid. pid should be a string.
	
**************************************************************************** */
function getElementById(pid) {
	// use DOM method.
	if (typeof(document.getElementById) != 'undefined') {
		return(document.getElementById(pid));
	}
	// use ie method
	else if (typeof(document.all) != 'undefined') {
		return(document.all(pid));
	}
	// else return null as we can't do much more than that.
	else {
		return(null);
	}
}


/* ****************************************************************************
	This function sets up two combo/select boxes so that they are related to
	each other. This means that if you choose a value in the box with ID of
	pstrCatId then the values in the box with id of pstrSubCatId will change 
	to the related data.
	
	pvntContArray (Content array) should be in the following format:
	(strOptionDescription1, strOptionValue1, vntSubCatArray1, ...,
		strOptionDescriptionN, strOptionValueN, vntSubCatArrayN)
	strOptionDescription: The value that is displayed on the screen for 
		the item.
	      
	strOptionValue: The value that will be retrieved from the object.
                          
	      
	vntSubCatArray: Array storing subcategory items in the following form
		(strOptionDescription1, strOptionValue1, ..., 
		strOptionDescriptionN, strOptionValueN)
		see previous descriptions for these values.
**************************************************************************** */
function setupRelatedSelects (pvntContAry, pstrCatId, pstrSubCatId, pvntSelCatValue, pvntSelSubCatValue) {
	var objOption = new Option;
	var objCategory = getElementById(pstrCatId);
	var objSubCat = getElementById(pstrSubCatId);
	if (objCategory == null || objSubCat == null) {
		alert('could not link the selects \'' + pstrCatId + '\' and \'' + pstrSubCatId + '\' for output.');
		return false;
	}
	
	// Check if the category combo already has items.
	// This is so that when in Netscape it refreshs the screen
	// to adjust the width of the combo box's, it does not
	// refresh the combo box again.
	if (!objCategory.options.length > 0 || (objCategory.options.length > 0 && objCategory.options[0].value == 'ni')) {
		// loop through the array and add each of the items from the
		// array into the combo
		for (var i=0; i < pvntContAry.length / 3; i++) {
			// create the option
			objOption = new Option(pvntContAry[i * 3], pvntContAry[i * 3 + 1]);
			// if it is the first option then select it.
			// this is required for Netscape
			if ( (typeof(pvntSelCatValue) == 'undefined' && i == 0) || (typeof(pvntSelCatValue) != 'undefined' && pvntSelCatValue == objOption.value) ) {
				objOption.selected = true;
			}
			// add the option to the combo
			objCategory.options[i] = objOption;
		}
		
		// refresh the sub category combo box
		populateSubCat(pvntContAry, pstrCatId, pstrSubCatId, pvntSelSubCatValue);
		
		// set up change events on the category
		objCategory.onchange = function () {
			populateSubCat(pvntContAry, pstrCatId, pstrSubCatId);
		}
	}
	
	return true;
} 

function setupRelatedSelects_cust (pvntContAry, pstrCatId, pstrSubCatId, pvntSelCatValue, pvntSelSubCatValue) {
	var objOption = new Option;
	var objCategory = getElementById(pstrCatId);
	var objSubCat = getElementById(pstrSubCatId);
	
	if (objCategory == null || objSubCat == null) {
		alert('could not link the selects \'' + pstrCatId + '\' and \'' + pstrSubCatId + '\' for output.');
		return false;
	}
	
	
	// Check if the category combo already has items.
	// This is so that when in Netscape it refreshs the screen
	// to adjust the width of the combo box's, it does not
	// refresh the combo box again.
	//if (!objCategory.options.length > 0 || (objCategory.options.length > 0 && objCategory.options[0].value == 'ni')) 
	{
		// loop through the array and add each of the items from the
		// array into the combo
		for (var i=0; i < pvntContAry.length / 3; i++) {
			// create the option
			objOption = new Option(pvntContAry[i * 3], pvntContAry[i * 3 + 1]);
			// if it is the first option then select it.
			// this is required for Netscape
			if ( (typeof(pvntSelCatValue) == 'undefined' && i == 0) || (typeof(pvntSelCatValue) != 'undefined' && pvntSelCatValue == objOption.value) ) {
				objOption.selected = true;
			}
			// add the option to the combo
			objCategory.options[i] = objOption;
		}
		
		// refresh the sub category combo box
		populateSubCat(pvntContAry, pstrCatId, pstrSubCatId, pvntSelSubCatValue);
		
		// set up change events on the category
		objCategory.onchange = function () {
			populateSubCat(pvntContAry, pstrCatId, pstrSubCatId);
		}
	}
	
	return true;
} 

/* ****************************************************************************

	This function is called to update the sub-category combo
	with the subcategories for the currently selected category combo
	
	This function is setup automatically by the function setupRelatedSelects()

**************************************************************************** */
function populateSubCat(pvntContAry, pstrCatId, pstrSubCatId, pvntSelSubCatValue) {
	//alert(pvntContAry);
	var objOption = new Option;
	var objCategory = getElementById(pstrCatId);
	var objSubCat = getElementById(pstrSubCatId);
	
	// The currently selected categories subcategory array
	var vntSubItems = pvntContAry[objCategory.selectedIndex * 3 + 2];
	
	// remove all items from the subcategory combo. 
	// this works by checking deleting the first item until
	// no items are left.
	while(objSubCat.options.length > 0) {
		objSubCat.options[0] = null;
	}
	
	// loop through the subcategory array and add each item 
	// to the subcategory combo.
	for (var i=0; i < vntSubItems.length / 2; i++) {
		objOption = new Option(vntSubItems[i * 2], vntSubItems[i * 2 + 1]);
		if ( (typeof(pvntSelSubCatValue) == 'undefined' && i == 0) || (typeof(pvntSelSubCatValue) != 'undefined' && pvntSelSubCatValue == objOption.value) ) {
			objOption.selected = true;
		}
		objSubCat.options[i] = objOption;
	}
}


/* ****************************************************************************

	detectEnterPressOnForm
	
	requires a variable containing a reference to the form object to be setup
	and a reference to the function to be called once the enter key has been 
	pressed.

**************************************************************************** */
function detectEnterPressOnForm(frmFormObject, funCallBack) {
	var blnFoundFirst = false;
	var i = 0;
	for(i = 0; i < frmFormObject.length; i++) {
		if(frmFormObject[i].type == 'text' || frmFormObject[i].type == 'select-one') {
			if (!blnFoundFirst) {
				blnFoundFirst = true;
				//frmFormObject[i].focus();
			}
			frmFormObject[i].onkeydown = function (e) {
				var objEvent = null;
				if(typeof(e) != 'undefined') {
					objEvent = e;
				}
				else if(typeof(event) != 'undefined') {
					objEvent = event;
				}
				if(objEvent == null) 
					return true;
				
				if (objEvent.keyCode == 13) {
					funCallBack();
				}
				return true;
			}
		}
	}
}

