// BE SURE TO INCLUDE THIS FILE IN THE PAGE YOU ARE DOING THE VALIDATION
<!--- include the Qforms custom validation java scipt file --->
//<script language="JavaScript1.2" src="/shared/dsp/common/js/validation_custom.js"></script>
//SAMPLE CALL
//objForm.s_field_name.validatePositiveInt();

function __isPositiveInt(){
  // check to make sure the current value is greater then zero
  if( parseInt(this.value) < 1 ){
    // here's the error message to display
    this.error = "The " + this.description + " field must";
    this.error += " contain an integer greater then zero.";
  }
}
_addValidator("isPositiveInt", __isPositiveInt);

function __isSelectRequired(){
  // check to make sure the current value is greater then zero
  if( parseInt(this.value) < 1 ){
    // here's the error message to display
    this.error = "You must select from  the " + this.description + " list";
  }
}
_addValidator("isSelectRequired", __isSelectRequired);

// Make sure it is a decimal
// the built in validateNumeric does not recognize a decimal i.e. 12.00 as a valid number
function __isDecimalInt(){
	//alert(this.value)
	//alert(this.value.length)
	if(this.value.length) {
		var n_value
		var s_regexp
		var b_invalid		

		// check if the current value is an integer
		s_regexp=/^(-?)([0-9])+$/
		n_value = this.value
		//alert(n_value)
		// this checks if it matches the s_regexp pattern
		b_invalid = n_value.search(s_regexp); 
		//alert(b_invalid)
		
		// it's not an integer is it a decimal
		if( b_invalid ) { 
			// check if the current value is decimal
			s_regexp=/^(-?)([0-9])+\.([0-9])+$/
			n_value = this.value
			//alert(n_value)
			// match the whole string from begin to end (because of ^ says begin of line and $ says end of line
			// if matches return 0 else return -1
			b_invalid = n_value.search(s_regexp); 
			//alert(b_invalid)
		}
					
		if( b_invalid != 0 ){  
			// here's the error message to display
			this.error = "The " + this.description + " field must";
			this.error += " contain a number or a decimal.";
	  	}
		
	}
}
_addValidator("isDecimalInt", __isDecimalInt);

// Make sure it is a decimal
// the built in validateNumeric does not recognize a decimal i.e. 12.00 as a valid number
function __isDecimal(){
	//alert(this.value)
	//alert(this.value.length)
	if(this.value.length) {
		var n_value
		var s_regexp
		var b_invalid		
				
		// is it a decimal
			// check if the current value is decimal
			s_regexp=/^(-?)([0-9])+\.([0-9])+$/
			n_value = this.value
			//alert(n_value)
			// match the whole string from begin to end (because of ^ says begin of line and $ says end of line
			// if matches return 0 else return -1
			b_invalid = n_value.search(s_regexp); 
			//alert(b_invalid)
					
		if( b_invalid != 0 ){  
			// here's the error message to display
			this.error = "The " + this.description + " field must";
			this.error += " contain a number with a decimal.";
	  	}
		
	}
}
_addValidator("isDecimal", __isDecimal);

// check if there is room for the 2 decimal places (.00) if they don't enter them
function __isLengthDec(n_max_length){
	s_regexp=/[.]/
	n_decimal_position = this.value.search(s_regexp);
	//alert(n_decimal_position);

	// if they enter .0 you make sure there is room for the other 0
	// if they enter and integer make sure there is room for .00	
	// -1 = no decimal point
	// n_decimal_position is 0 based number meaning the first digit is 0		
	if( ( (n_decimal_position < 0) && (this.value.length + 3) > n_max_length ) || ( (n_decimal_position >= 0) && (n_decimal_position + 3) > n_max_length ) ){
//	if( ( n_decimal_position < 0 ) || ( (n_decimal_position + 3) > n_max_length ) ){
		// here's the error message to display
		this.error = "The " + this.description + " field must not be greater than ";
		this.error += n_max_length;
		this.error += " characters.";
		this.error += " When a 3 character (.00) decimal value is included in the number you entered it will exceed this length.";
	}
}
_addValidator("isLengthDec", __isLengthDec);

// check if the length will be less than or equal to the passed n_max_length when .00 is added to an integer
function __isLengthLTE(n_max_length){
  if( this.value.length > n_max_length ){
    // here's the error message to display
    this.error = "The " + this.description + " field must not be greater than ";
	this.error += n_max_length;
	this.error += " characters.";
  }
}
_addValidator("isLengthLTE", __isLengthLTE);

function __NoSpecChars(){
	//alert(this.value)
	//alert(this.value.length)
	if(this.value.length) {
		var n_value
		var s_regexp
		var b_invalid
		
		n_value = this.value
	
		// check if there are any special characters in the value
		// Dan's criteria "[^A-Z,^a-z,^0-9, ]+"
		// this checks if it DOESN'T match the s_regexp pattern
		// returns the index number where the bad character is found otherwise -1
		//outside [] ^ means begin of line - inside [] ^ means NOT
		s_regexp=/[^A-Za-z0-9 ]/
		
		b_invalid = n_value.search(s_regexp); 
		
		if( b_invalid >= 0 ){
		// here's the error message to display
		this.error = "The " + this.description + " field must";
		this.error += " not contain any special characters.";
		}
		
	}
}
_addValidator("NoSpecChars", __NoSpecChars);

function __isValidDate(mask){

var strMask=_param(arguments[0], "mm/dd/yyyy");
var iMaskMonth=strMask.lastIndexOf("m")-strMask.indexOf("m")+1;
var iMaskDay=strMask.lastIndexOf("d")-strMask.indexOf("d")+1;
var iMaskYear=strMask.lastIndexOf("y")-strMask.indexOf("y")+1;
var strDate=this.value;
var delim="", lstMask="mdy";

for(var i=0;i<strMask.length;i++){
	if(lstMask.indexOf(strMask.substring(i, i+1))==-1){
		delim=strMask.substring(i, i+1);
		break;
	}
}
aMask=strMask.split(delim);
if(aMask.length==3){
	dt=this.value.split(delim);
	if(dt.length!=3)
		this.error="An invalid date was provided for "+this.description+" field.";
	for(i=0;i<aMask.length;i++){
		if(aMask[i].indexOf("m")>-1)
			var sMonth=dt[i];
		else if(aMask[i].indexOf("d")>-1)
			var sDay=dt[i];
		else if(aMask[i].indexOf("y")>-1)
			var sYear=dt[i];
	}
} 
else if(mask.length==1){
	var sMonth=this.value.substring(strMask.indexOf("m")-1, strMask.lastIndexOf("m"));
	var sDay=this.value.substring(strMask.indexOf("d")-1, strMask.lastIndexOf("d"));
	var sYear=this.value.substring(strMask.indexOf("y")-1, strMask.lastIndexOf("y"));
} 
else{
	this.error="An invalid date mask was provided for "+this.description+" field.";
}

var iMonth=parseInt(sMonth, 10);
var iDay=parseInt(sDay, 10);
var iYear=parseInt(sYear, 10);
if(isNaN(iMonth)|| sMonth.length>iMaskMonth)
	iMonth=0;
if(isNaN(iDay)|| sDay.length>iMaskDay)
	iDay=0;
if(isNaN(sYear)|| sYear.length!=iMaskYear)
	sYear=null;
lst30dayMonths=",4,6,9,11,";
if(sYear==null){
	this.error="An invalid year was provided for the "+this.description+" field. The year \n   should be a "+iMaskYear+" digit number.";
} 
else 
	if((iMonth<1)||(iMonth>12)){
		this.error="An invalid month was provided for "+this.description+" field.";
	} 
	else{
		if(iYear<100)
			var iYear=iYear+((iYear<30)? 1900 : 2000);
			var iYear=(sYear.length==4)? parseInt(sYear): parseInt("20"+sYear);
			if(lst30dayMonths.indexOf(","+iMonth+",")>-1){
				if((iDay<1)||(iDay>30))
					this.error="An invalid day was provided for the "+this.description+" field.";
			} 
			else 
				if(iMonth==2){
					if((iDay<1)||(iDay>29))
						this.error="An invalid day was provided for the "+this.description+" field.";
					if((iDay==29)&&(iYear%4!=0))
						this.error="An invalid day was provided for the "+this.description+" field.";
				} 
			else{
				if((iDay<1)||(iDay>31))
					this.error="An invalid day was provided for the "+this.description+" field.";
			}
	}
}
_addValidator("isValidDate", __isValidDate);

// FOR USAGE INSTRUCTIONS ON THIS SEE THE obj.fieldName.isDifferent(string field) DOCUMENTATION
// AT http://www.pengoworks.com/qForms/docs/extension_validation.htm
// define Field isIdentical(); prototype
function _Field_isIdentical(field){
	if( ! this.compare(field) ){
		this.error = "The " + this.description + " and " + this.qForm[field].description + " must be identical.";
	}
}
_addValidator("isIdentical", _Field_isIdentical);

// Make sure it is a valid Subscriber Number
function __isValidLengthMask( n_required_length,s_required_case ){
	//alert(this.value)
	//alert(this.value.length)
	if(this.value.length) {
		var n_value
		var s_regexp
		var b_invalid		

		// check if there are any special characters in the value
		// Dan's criteria "[^A-Z,^a-z,^0-9, ]+"
		// this checks if it DOESN'T match the s_regexp pattern
		// returns the index number where the bad character is found otherwise -1
		//outside [] ^ means begin of line - inside [] ^ means NOT
		// no special characters
		//s_regexp=/[^A-Za-z0-9 ]/
		
		if(s_required_case == "upper"){
			// Check that there are only UPPER case characters
			s_regexp=/[^A-Z ]/
			s_required_case_message ="UPPER case"
		}
		else if(s_required_case == "lower"){
			// Check that there are only lower case characters
			s_regexp=/[^a-z ]/
			s_required_case_message ="lower case"
		}
		else if(s_required_case == "letter"){
			// Check that there are only letter characters
			s_regexp=/[^A-Za-z]/
			s_required_case_message ="letter"
		}			
		else if(s_required_case == "numeric"){
			// Check that there are only numeric characters
			s_regexp=/[^0-9 ]/
			s_required_case_message ="numeric"
		}
		else if(s_required_case == "dwCode"){
			// Check that there are no lower case letters
			//s_regexp=/[^0-9 ]/
			//s_regexp=/[^A-Z0-9~!@#$%^&*()_+`-=<>?]/
			s_regexp=/[^A-Z0-9~!@#$%^&*()_+-=<>(?#escape the question mark)\?/(?#escape the forward slash)\\]/
			s_required_case_message ="A-Z0-9~!@#$%^&*()_+-=<>\?/\\"
		}		
		else if(s_required_case == "booleanNumeric"){
			// Check that there is a 1 or a 0
			s_regexp=/[^01]/
			s_required_case_message ="0 or 1"
		}	
		else if(s_required_case == "booleanAlpha"){
			// Check that there is a Y or a N
			s_regexp=/[^YN]/
			s_required_case_message ="Y or N"
		}						
		else if(s_required_case == "sysprin"){
			// Check that there is only 82231 followed by only numeric characters
			//BEST TRY - s_regexp=/^82231([^0-9]{3})$/
			//WORKS - s_regexp=/^82231[^0-9]$/
			//s_regexp=/^(-?)([0-9])+\.([0-9])+$/ if it matches return 0 else return -1
			//s_regexp=/([0-9]){3}/
			s_regexp=/[^0-9 ]/
			s_required_case_message ="numeric (8223####)"
			//alert(s_regexp)
		}				
		else{
			// Check that there are only UPPER and lower case and numeric characters
			s_regexp=/[A-Za-z0-9 ]/
			s_required_case_message ="any"
		}		
		//alert(s_required_case_message)
		
		n_value = this.value
		//alert(n_value)
		// this checks if it matches the s_regexp pattern
		b_invalid = n_value.search(s_regexp); 
		//alert(b_invalid)
		
		// Check that there are an exact number of characters
		/**/	
		if( this.value.length != n_required_length ){  
			b_invalid = 999; //This is an error
	  	}
			 
//alert(s_required_case_message) 	
//alert(b_invalid)
//b_invalid = 999;
		if( b_invalid >= 0 ){
			// here's the error message to display
			this.error = "The " + this.description + " field must";
			this.error += " be " + n_required_length + " character(s) " + s_required_case_message + ".";
	  	}
		
	}
}
_addValidator("isValidLengthMask", __isValidLengthMask);
