var BeginningWhiteSpaceRegExp = /^\s+/;
var EndingWhiteSpaceRegExp = /\s+$/;

function CheckField(Required,WhichField, FieldType, DisplayName, ReqLength, Numeric) {
	var RegExp;
	var TestVal;
	var MatchChar;
	var ErrMsg = 'None';
		
	//Replace any whitespace at beginning and end of string with nothing	
	if (FieldType == 'Text' || FieldType == 'Alpha'  || 
		FieldType == 'Money' || FieldType == 'MoneyWhole' || FieldType == 'Pct' || FieldType == 'Email' || FieldType == 'SSN') 
		{
		TestVal = WhichField.value.replace(BeginningWhiteSpaceRegExp,'');
		TestVal = TestVal.replace(EndingWhiteSpaceRegExp,'');
	} else { 
		if (WhichField.selectedIndex == -1 || WhichField.options[WhichField.selectedIndex].value == 0) {
			TestVal = '';
		} else {	
			TestVal = WhichField.options[WhichField.selectedIndex].text;
		}
	}
	if (TestVal == '' && Required == true) {
		ErrMsg = DisplayName + ' must be entered';
	} 
	
	
	//If required check has been passed, continue with field type edits 
	
	if (ErrMsg == 'None' && TestVal != '') {	
		
		//Check required length 	
		if (ReqLength > 0) {
			if (TestVal.length != ReqLength) {
				ErrMsg = DisplayName + ' must be ' + ReqLength + ' characters';
			}
		}  
		
		//For money or money whole dollars, remove leading $ and all commas.
		if (FieldType == 'Money' || FieldType == 'MoneyWhole') {
			if (TestVal.charAt(0) == '$') {
				TestVal = TestVal.substr(1);
			}
			
			RegExp = /,/g;
			TestVal = TestVal.replace(RegExp,'');
			
			//For money only, remove one decimal
			if (FieldType == 'Money') {
				RegExp = /\./;
				TestVal = TestVal.replace(RegExp,'');
			}
			
			//Set to Numeric to force numeric check
			Numeric = true;
		}
		
		//For percent remove trailing % and set to Numeric to force numeric check
		 if (FieldType == 'Pct') {
			if (TestVal.charAt(TestVal.length - 1) == '%') {
				TestVal = TestVal.substr(0,TestVal.length - 1);	
			}
			Numeric = true;
		}
		
			

		//Numeric check - match on any non-numeric character. If found, return Error.
		if (Numeric == true) {
			RegExp = /\D/;
			MatchChar = TestVal.match(RegExp);
			if (MatchChar != null) {
				ErrMsg = DisplayName + ' must be numeric';
			}
		}
		
		//Pct cannot exceed 100
		if (FieldType == 'Pct' && ErrMsg == 'None') {
			if (TestVal > 100) {
				ErrMsg = DisplayName + ' cannot exceed 100';
			}
		}
	
		//Alpha check - match on any non A-Z character. If found, return Error.
		if (FieldType == 'Alpha') {
			RegExp = /[^A-Z]/;
			TestVal = TestVal.toUpperCase();
			MatchChar = TestVal.match(RegExp);
			if (MatchChar != null) {
				ErrMsg = DisplayName + ' must be alphabetic';
			}
		}
	
		
	
		//Check for valid email address
		if (FieldType == 'Email') {
			var no=0;
			var c;
    
			for(i=0;i<TestVal.length;i++) {
				c=TestVal.charAt(i);
			    if(i==0) { 
			        if(!isNaN(c)) {
						no=2;  
					}
				}
			    if(c==' ') no=2;
			    if(c =='@')  no++;
			}

			if(no!=1 || TestVal.indexOf('@')<2) {
				ErrMsg = TestVal + ' is not a valid E-mail Address';
			}
				    
			if (TestVal.lastIndexOf('.')<5) {
				ErrMsg = TestVal + ' is not a valid E-mail Address';
			}
		}
	}
	
	if (ErrMsg == 'None') {
		return('OK');
	} else {
		if (FieldType == 'List') {
			WhichField.selectedIndex = 0;
		}
		WhichField.focus();
		alert(ErrMsg);
		return('Error');
	} 
}


