//Trim leading and trailing spaces
String.prototype.trim = function()
	{
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}

//checks to see if it is a number
function isNumber(control){
	if (isNaN(control.value)){
		alert(control.value + ' is not a valid number');
		control.focus();
		control.select();
		return false
	}
	    return true
}

function IsDate(control){
	var sDate=control.value;
	var err=0;
	var psj=0;
	var a,b,d,f;
	if (sDate.indexOf("/")>=1){
		a=sDate.split("/",3);
	} else {a=sDate.split("-",3);}
	b = a[0];	//month	
	d = a[1];	//day
	f = a[2];	//year

	var integer = /(^-?\d\d*$)/; //integer
	if (integer.exec(b) && integer.exec(d) && integer.exec(f)) {
		//basic error checking
		if (b<1 || b>12 || !b) err = 1;
		if (d<1 || d>31 || !d) err = 1;
		if (f<0 || !f) err = 1;
		if (f.length != 4 && f.length != 2) err = 1;
		//advanced error checking

		// months with 30 days
		if (b==4 || b==6 || b==9 || b==11){
			if (d==31) err=1;
		}

		// february, leap year
		if (b==2){
			// feb
			var g=parseInt(f/4)
			if (isNaN(g)) {
				err=1;
			}

			if (d>29) err=1;
				if (d == 29) {
					if (err == 0) {
						err = 1;
		  			if ((f/4) == parseInt(f/4)) err = 0;
						if ((f/100) == parseInt(f/100)) err = 1;
   					if ((f/400) == parseInt(f/400)) err =0;
					}
				}
			}

		if (err == 0){
			return true;
		}
		alert(sDate + ' is not a valid date\nDate MUST be in the form of mm/dd/yyyy');
		control.focus();
		control.select();
		return false
	}
	alert(sDate + ' is not a valid date\nDate MUST be in the form of mm/dd/yyyy');
	control.focus();
	control.select();
	return false
}

//CompareValues
function CompareValues(theFirstField,theSecondField,CaseSensitive,FailureMessage){
	var f1=theFirstField;
	var f2=theSecondField;
	var f1Val=f1.value;
	var f2Val=f2.value;
	
	if (CaseSensitive.toUpperCase()=='N'){
		f1Val=f1Val.toUpperCase();
		f2Val=f2Val.toUpperCase();
	}
	
	if (f1Val != f2Val) {
		alert(FailureMessage);
		f1.focus();
		f1.select();
		return false
	}
	return true
}

//Form validation 
//DataType a=alpha an=AlphaNumeric n=Numeric
//Required y=yes m=no
//DataMask and DataValidChar is not working
function ValField(theField,DataType,DataMin,DataMax,DataMask,DataValidChars,Required,DisplayFieldName){
	var f=theField;
	var value=f.value.trim();
	var Valid=true;
	var DisplayMessage='';
	
	if (Required.toUpperCase()=='Y' && (Valid)){
		if(value==''){
			Valid=false;
			DisplayMessage=DisplayFieldName + ' is a required field';
		}
	}	
	
	
	if (value.length < DataMin && (Valid)){
		Valid=false;
		DisplayMessage=DisplayFieldName + '\nMust have a Minimum length of ' + DataMin ;
	}
	
	if (value.length > DataMax && (Valid)){
		Valid=false;
		DisplayMessage=DisplayFieldName + '\nMust have a Maximum length of ' + DataMax ;
	}
	
	if (DataType.toUpperCase()=='N' && (Valid)){
		if(isNaN(value)){
			Valid=false;
			DisplayMessage=DisplayFieldName + '\nMust be Numeric';
		}
	}
	
	if (! Valid){
		alert(DisplayMessage);
		f.focus();
		f.select();
		return false;
	}
	
	return true;
}

//Check for valid email address
function isEmail(theField){
	var f=theField;
	var EmailValidation=true;
	var EMLen = theField.value.length;
	
	for (var i = 0; i <= theField.value.length; i++){
		if (theField.value.charAt(i) == "@"){
			EmailValidation=true;
			break;
		}
		else{
			EmailValidation=false;
		}
	}
	
	for (var i = 0; i <= theField.value.length; i++){
		if (theField.value.charAt(i) == "."){
			EmailValidation=true;
			break;
		}
		else{
			EmailValidation=false;
		}
	}
	
	//if (theField.value.substring(EMLen - 3, EMLen) != "com" &&
	//    theField.value.substring(EMLen - 3, EMLen) != "net" &&
	//    theField.value.substring(EMLen - 3, EMLen) != "org"){
	//	EmailValidation=true;	
	//}
	
	if (! EmailValidation){
		alert('E-Mail address is invalid');
		f.focus();
		f.select();
		return false;
	}
	return true;
}


