            function strltrim() {
                return this.replace(/^\s+/,'');
            }

            function strrtrim() {
                return this.replace(/\s+$/,'');
            }
            function strtrim() {
                return this.replace(/^\s+/,'').replace(/\s+$/,'');
            }

            String.prototype.ltrim = strltrim;
            String.prototype.rtrim = strrtrim;
            String.prototype.trim = strtrim;

/* true if whitespace, false otherwise */
function iswhitespace(c)
{
	if ((c != ' ') && (c != '\n') && (c != '\t') && (c != '\r') && (c != '\f'))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function closewin()
{
		if (self.parent.frames.length != 0) 
		{
			self.parent.close()
		} 
		else 
		{
			window.close()
		}
}


/* count number of words in a string i.e tokens delmited by spaces. */
function intWordCount(sval)
{
	var i;
	var wcount;
	var wtoggle;
	wcount = 0;
	wtoggle = false;
		
	if (isblank(sval))
	{
		wcount = 0;
		return wcount;
	}
	
	for ( i = 0; i < sval.length; i++ )
	{
		var c = sval.charAt(i);
		
		if (iswhitespace(c))
		{
			if ( wtoggle == false )
			{
				wcount++;
				wtoggle = true;
			}
		}
		else
		{
			/* blank space - turn word toggle off */
			wtoggle = false;
		}
	}
	
	if (wtoggle == false)
	{
		wcount++;
	}
	
	return wcount;
}

function isblank(s)
{
	if (s.length == 0 )
	{
		return true;
	}
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (!iswhitespace(c)) return false;
	}
	return true;
}

function isIntNum(s)
{
	if (isNaN(parseInt(s)))
	{
		return false;
	}
	return true;
}

function isFloatNum(s)
{
	if (isNaN(parseFloat(s)))
	{
		return false;
	}
	return true;
}

function isInRange(s,low,high)
{
	if ((s >= low) && (s <= high))
	{
		return true;
	}
	return false
}

function getqfldval(e)
{
	if (e.quotes)
	{
		var quotestr = "'"
	}
	else
	{
		var quotestr = ""
	}

	if (e.numcomp == true)
	{
		if (isNaN(parseInt(e.value)))
		{
			var errstring = "__Err " + " Error: " + e.showname + " value is not valid: " + e.value
			return(errstring);
		}

		/* look for first operator  */
		var regexp1 = /^(\s*)(!=|<=|>=|<|>)(\s*)(\w+)/;
		var matchres = e.value.match(regexp1);
		if (matchres != null )
		{
			var numop = matchres[2];
			var numval = matchres[4];
			return(e.name + " " + numop + " " + quotestr + numval + quotestr);
		}
	}
	else if (e.datecomp == true)
	{
		quotestr = '#';
		/* look for first operator and mm/dd/yy or mm/dd/yyyy format only */
		var regexp2 = /^(\s*)(!=|<=|>=|<|>)?(\s*)(\d+)\/(\d+)\/(\d+)/;
		var matchres = e.value.match(regexp2);
		if (matchres != null)
		{
			var dateop = matchres[2];
			var datemm = matchres[4];
			var datedd = matchres[5];
			var dateyy = matchres[6];
			return(e.name + " " + dateop + quotestr + datemm + "/" + datedd + "/" + dateyy + quotestr);
		}
		else
		{
			var errstring = "__Err " + " Error: " + e.showname + " value is not valid: " + e.value
			return(errstring);
		}
	}
	return(e.name + " = " + quotestr + e.value + quotestr);
}


function buildquery(finp)
{
	var msg;
	var qstr;
	var qcount;
	
	qcount = 0;

	for ( var i = 0; i < finp.length; i++ )
	{
		var e = finp.elements[i];
		if ((e.query) && (e.value != null) && (e.value.length > 0))
		{
			var qfldval = getqfldval(e);
			
			if ( qfldval.search(/^__Err/) != -1 )
			{
				var errstring = qfldval.substring(5,qfldval.length);
				alert(errstring);
				e.focus();
				e.select();
				return false;
			}
				
			if ( qcount == 0 )
			{
				qstr = qfldval;
			}
			else
			{
				qstr = qstr + ' AND ' + qfldval;
			}
			qcount++;
		}
	}
	finp.wherequery.value = qstr;
	return true;
}

/* ssno verification ######### no spaces, dashes always 9 digits 0-9 */
function verifyssno(argssno)
{
	if ( argssno.length != 9)
	{
		return false;
	}

	var regexp = /^(\d+)/;
	var matchres = argssno.match(regexp);
	if ( matchres != null)
	{
		return true;
	}
	
	return false;
}

function verifydate(argdt)
{
	monlength = new Array(12);
	monlength[0] = 31; monlength[1] = 28; monlength[2] = 31;
	monlength[3] = 30; monlength[4] = 31; monlength[5] = 30;
	monlength[6] = 31; monlength[7] = 31; monlength[8] = 30;
	monlength[9] = 31; monlength[10] = 30; monlength[11] = 31;

	var regexp = /^(\d+)\/(\d+)\/(\d+)/;
	var matchres = argdt.match(regexp);
	if (matchres != null)
	{
		var matchparts = argdt.split("\/");
		/* mm test */
		if ((parseInt(matchparts[0]) < 1 ) || (parseInt(matchparts[0]) > 12))
		{
			return false;
		}
		/* yyyy  AND dd test */
		if ((parseInt(matchparts[0]) == 2) && (parseInt(matchparts[1]) == 29))
		{
			/* leapyear check */
			if (((parseInt(matchparts[2]) % 4 ) != 0 ) || (((parseInt(matchparts[2]) % 100) == 0) && ((parseInt(matchparts[2]) % 400) != 0)))
			{	
				return false;
			}
		}
		else
		{
			/* dd test */
			if ((parseInt(matchparts[1]) < 1) || (parseInt(matchparts[1]) > monlength[parseInt(matchparts[0]) - 1]))
			{
				return false;
			}
		}

		return true;
	}
	else
	{
		return false;
	}
}

function formverify(finp)
{
	var msg;
	var empty_fields, error_fields;
	var errors;
	empty_fields = "";
	error_fields = "";
	var	wc_total;

	for ( var i = 0; i < finp.elements.length; i++ )
	{
		var e = finp.elements[i];

		if (((e.type == "text") || (e.type == "textarea")) && (e.optional == false))
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				empty_fields += "\n         "  + e.showname;
				continue;
			}
		}
		
		if (((e.type == "text") || (e.type == "textarea")) && (!isblank(e.value)))
		{	
			if (e.maxwordlen > 0)
			{
				wc_total = intWordCount(e.value);
				if (wc_total > e.maxwordlen )
				{
					error_fields += "\n   " + e.showname + " is " + wc_total + " words and exceeds maximum word limit of " + e.maxwordlen;
				}
			}
		}

		if (( e.type == "select-one") && ((e.optional == false) && e.selectedIndex == -1 ))
		{
				empty_fields += "\n        "  + e.showname;
				continue;
		}

		if ((e.valtype == "date") && (e.value != "") && !isblank(e.value) && (e.optional == false))
		{
			if (verifydate(e.value) != true)
			{
				error_fields += "\n    " + e.showname + " has invalid value! ";
			}
		}
	
		/* ssno check */
		if ((e.valtype == "ssno") && (e.value != "") && !isblank(e.value) && (e.optional == false))
		{
			if (verifyssno(e.value) != true)
			{
				error_fields += "\n   " + e.showname + " has invalid value! No space or dashes allowed, digits only, format: ######### ";
			}
		}	
		
		if ((e.valtype == "int") && (e.value != "") && !isblank(e.value))
		{
			if (isIntNum(e.value) != true)
			{
				error_fields += "\n    " + e.showname + " has invalid value! ";
				continue;
			}
		}

		if ((e.valtype == "float") && (e.value != "") && !isblank(e.value))
		{
			if (isFloatNum(e.value) != true)
			{
				error_fields += "\n    " + e.showname + " has invalid value! ";
				continue;
			}
		}			
		
		if ((e.valtype == "phone") && (e.value != "") && !isblank(e.value))
		{
			if (checkphone(e) != true)
			{
				error_fields += "\n    " + e.showname + " Must have at least 10 digits. ";
				continue;
			}
		}					

		if ((e.valtype == "email") && (e.value != "") && !isblank(e.value))
		{
			if (verifyemail(e.value) != true)
			{
				error_fields += "\n	  " + e.showname + " is invalid. Please try again. It should be in the format: name@host.domain.";
				continue;
			}
		}		
		
	}

	msg = "==========================\n\n";
	msg = msg += "The following errors occurred.  Please re-submit.\n";
	msg = msg += "=========================\n\n";
	
	if (empty_fields)
	{
		msg += " The following field(s) are empty: " + empty_fields + "\n";
		alert(msg);
		return false;
	}

	if (error_fields)
	{
		msg += " The following fields have invalid values: " + error_fields + "\n";
		alert(msg);
		return false;
	}

	return true;
}

function verifyemail(stremail)
{
	if ( stremail.search(/[\w-_]+@[\w-_]+\.[\w-_]+/) == -1 )	
	/*
	if ( stremail.search(/^([A-Za-z0-9_-]+\.)*[A-Za-z0-9_-]+@([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]+$/) == -1 )
	*/
	{
		return false;
	}
	return true;
}


/* check for 10 digits in phone number */
function checkphone(e)
{
	var digitcount;
	digitcount = 0;
		
	if (isblank(e.value))
	{
		return false;
	}
	var sval = e.value;
	
	for ( i = 0; i < sval.length; i++)
	{
		if ((sval.charAt(i) >= '0') && (sval.charAt(i) <= '9'))
		{
			digitcount++;
		}
	}
	
	if (digitcount < 10)
	{
		return false;
	}
	
	return true;
}


function caseviewchangebuttons(stropt)
{
	if (parent.frames[0].document.readyState == 'complete')
	{
		if (parent.frames[0].document.title == 'Case Pair NavBar')
		{
			parent.frames[0].setcaseviewchangebuttons(stropt);
		}
	}
}