/*
 * File: jsValidators.js
 * Project: BillStream Agentpay Admin
 * Author: Justin
 * Email: justin.cornell@billstream.com
 * Creation Date: Sept 6, 2006 at 11:59:33 PM
 */
 
String.prototype.trim = function trim() {
  sInString = this.replace( /^\s+/g, "" );// strip leading
  sInString.replace( /\s+$/g, "" );// strip trailing
  return sInString;
}
String.prototype.isEmpty = function(){
	return (this == null || this == '');
}
String.prototype.test = function(regex){
	if(this.match(regex)){
		return false;
	}
	return true;
}
String.prototype.isString = function(){
	return true;
}

function jsValidators(){
	this.alerts = new Object();
	this.setFocus = function(elementId){
		document.getElementById(elementId).focus();
	}
	
	/**
 	 * Adds an alert the response if and only if the elementId is not in the array
 	 * 	or the element is empty.
 	 * 
 	 * @param string id of the element to check
 	 * @param string readable name of the element
 	 * @return boolean returns true if alert was added otherwise returns false
 	 */
	this.isPresentAlert = function(elementId, elementName, additionalMessage){
		var hasErrors = false;
		var o = document.getElementById(elementId);
		
		if( (o && o.value.trim() == '' ) || !o){
			var verb = this.isVowel(elementName, 0) ? 'an' : 'a';
			var msg = "You must specify "+verb+" "+elementName+" before continuing ("+elementName+" cannot be empty)";
			if(additionalMessage){
				msg += " "+additionalMessage;
			}
			msg = msg.trim();
			this.addAlert(elementId, msg);
			hasErrors = true;
		}
		return hasErrors;
	}
	
	this.isOptionalPresentAlert = function(elementId, elementName, additionalMessage){
		var hasErrors = false;
		var o = document.getElementById(elementId);
		
		if( o && o.value.trim() == '' ){
			var verb = this.isVowel(elementName, 0) ? 'an' : 'a';
			var msg = "You must specify "+verb+" "+elementName+" before continuing ("+elementName+" cannot be empty)";
			if(additionalMessage){
				msg += " "+additionalMessage;
			}
			msg = msg.trim();
			this.addAlert(elementId, msg);
			hasErrors = true;
		}
		return hasErrors;
	}
	
	this.isAlert = function(elementId, required, regex, elementName, message){
		var hasErrors = false;
		var o = document.getElementById(elementId);
		if(required){
			hasErrors = this.isPresentAlert(elementId, elementName);
		}
		
		if(o){
			var val = o.value;
			if(!hasErrors && !val.isEmpty()){
				if(val.test(regex)){
					message = "'"+o.value+"' "+message;
					this.addAlert(elementId, message);
					hasErrors = true;
				}
			}
		}
		return hasErrors;
	}
	this.addAlert = function(elementId, message){
		this.alerts[elementId] = message;
	}
	this.isValidNumbericRangeAlert = function(elementId, begRange, endRange, elementName, required, additionalMessage){
		var hasErrors = this.isPresentAlert(elementId, elementName, additionalMessage);
		var	o = document.getElementById(elementId);
		if(o && !hasErrors){
			if(o.value < begRange || endRange < o.value ){
				var msg = o.value+" is not between "+begRange+" and "+endRange+".  Please correct this value.";
				msg = additionalMessage ? msg+" "+additionalMessage : msg;
				this.addAlert(elementId, msg);
				hasErrors = true;				
			}
		}
		return hasErrors;
	}
	this.isValidHTMLSafeCharsAlert = function(elementId, elementName, required, additionalMessage){
		var regex = "^([\u0020-\u003b]|[\u003d]|[\u003f-\u007e])*$";
		var msg = "is not a valid option.  Entries cannot contain chars ascii chars where the hex values are between 0020-003b, 003d, and 003f-007e."
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, regex, elementName, msg);
	}
	this.isValidEmailAlert = function(elementId, required, additionalMessage){
		var regex  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var elementName = 'email address';
		var msg = "is not a valid email address.  Please correct this value.";
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, regex, elementName, msg);
	}
	this.isValidNumericAlert = function(elementId, required, additionalMessage){
		var regex = "^-?([0-9])+([\.]([0-9])*)?$"; //"([\+|-]?[0-9]*[\.]?[0-9]*)";
		var elementName = 'numeric value';
		var msg = "is not a valid numeric value.  Please correct this value.";
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, regex, elementName, msg);
	}
	this.isValidNumericPercisionAlert = function(elementId, required, percision, additionalMessage){
		var regex = "^((([0-9]{1,3})[\,]?([0-9]{3}))*|([0-9])*)?([\.]([0-9]){0,"+percision+"})?$";
		var elementName = 'numeric value';
		var msg = "is not a valid numeric value with "+percision+" decimal places.  Please correct this value.";
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, regex, elementName, msg);
	}
	this.isValidPhoneAlert = function(elementId, required, additionalMessage){
		//is the actual regex for phone numbers including valid NPA values but allows 0 as preifx
 		var phone_regex = '^[(]?[0,2-9]{1}[0-9]{2}[) -]{0,2}' + '[0-9]{3}[- ]?' + '[0-9]{4}$'; //+ '[ ]?((x|ext)[.]?[ ]?[0-9]{1,5})?$';
		var elementName = 'phone number';
		var msg = "is not a valid phone number.  Please correct this number.";
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, phone_regex, elementName, msg);
	}
	this.isValidSSNAlert = function(elementId, required, additionalMessage){
		var ssn_regex = '^[0-9]{3}[ -.]?[0-9]{2}[ -.]?[0-9]{4}$'; //regex for ssn
 		var elementName = 'SSN';
 		var msg = "is not a valid SSN.  Please correct this SSN.  Format is XXX-XX-XXXX.";
 		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, ssn_regex, elementName, msg);
	}
	this.isValidDateAlert = function(elementId, required, additionalMessage){
//		var date_regex = "^([12][0-9]|3[01])/(0[1-9]|1[0-2])/(19[0-9]{2}|20[0-9]{2})$";
		var date_regex = "^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|[3][0-1])/(19[0-9]{2}|20[0-9]{2})$";
		var elementName = "date";
		var msg = "is not a valid Date.  Please correct this date.  Format is MM/DD/YYYY.";
		msg = additionalMessage ? msg+" "+additionalMessage : msg;
		return this.isAlert(elementId, required, date_regex, elementName, msg);
	}
	this.showFirstAlert = function(){
		for(key in this.alerts){
			if(!key.isString){
				continue;
			}
			var e = this.alerts[key];
			if(!e){
				return false;
			}
			alert(e);
			var elem = document.getElementById(key);
			if(elem){
				elem.focus();
			}
			delete this.alerts.key;
			return true;
		}
		
	}
	this.showAllErrorsOnPage = function(associtaveArray){
		var count = 0;
		for(elementId in this.alerts){
			var tagid = associtaveArray[elementId];
			if(tagid){
				var o = document.getElementById(tagid);
				o.innerHTML = this.alerts[elementId];
				if(count == 0){
					o.focus();
				}
				count++;
			}
		}
		this.clearAlerts();
	}
	this.clearAlerts = function(){
		this.alerts = new Object();
	}
	this.isVowel = function(string, index){
		var c = string.charAt(index);
		var vowels = new Array('a','e','i','o','u');
		
		for(var i=0; i<vowels.length; i++) {
			if(vowels[i] == c){
				return true;
			}
		}
		return false;
	}
}

