// JavaScript Document

// ===============  Validator How-To  ========================================
// Validator Functions for HTML Controls are expecting <SPAN> or <DIV> controls for validation messaging.
//
// Might need to alter for other HTML controls
// ====================================================================

function Validator( validatorControlName ) {	
	var _validatorControlName;
	var _isValid = true;
		
	// properties & methods declarations	
	_validatorControlName		= validatorControlName;	
	this.spanValidators			= new Array();		
	this.reset					= _reset;
	this.getIsValid				= _getIsValid;
	this.setIsValid				= _setIsValid;
	this.validateRequired		= _validateRequired;
	this.validateEmail			= _validateEmail;
	this.validateEmails			= _validateEmails;
	this.validateDate			= _validateDate;	
	this.validateZip			= _validateZip;
	this.validatePasswords		= _validatePasswords;
	this.validateCheckbox		= _validateCheckbox;
		
			
	function setValidatorControl( controlName ) { _validatorControlName = controlName; 	}
	function _getIsValid() { return _isValid; }
	function _setIsValid( bool ) { _isValid = bool; }
	function _reset() {	
		_isValid = true; 		
		document.getElementById( _validatorControlName ).innerHTML = '';
		// reset all validator spans to empty
		for(i=0; i < this.spanValidators.length; i++) { document.getElementById( this.spanValidators[i] ).innerHTML = ''; }
	}	
	// ----- Blank Is Required Validator -------------
	function _validateRequired( controlToValidate, validationText, validationControl ) {		
		if (trim( document.getElementById( controlToValidate ).value ) == '' ) { displayMessage( validationText, validationControl ); return false; }		
		else { return true; }
	}	
	// ----- Email Validator -------------
	function _validateEmail( controlToValidate, validationText, validationControl ) {	
		var email = trim( document.getElementById( controlToValidate ).value );
		if(email == "") return true;		
		// matches anything with an '@' followed by anything and a '.' followed by anything
		var reEmail = /^.+\@.+\..+$/;		
		if ( (!reEmail.test(email)) || (email == "")) { displayMessage( validationText, validationControl ); }
	}

	function _validateEmails( controlToValidate, validationText, validationControl ) {	
		var email = trim( document.getElementById( controlToValidate ).value );
//		email = email.replace(/,/g, ";").replace(/\n/g, ";").replace(/\r/g, ";").replace(/</g, ";").replace(/;;/g, ";");
		email = email.replace(/,/g, ";");
		email = email.replace(/\n/g, ";");
		email = email.replace(/\r/g, ";");
		email = email.replace(/</g, ";");
		email = email.replace(/>/g, ";");
		email = email.replace(/'/g, ";");
		email = email.replace(/"/g, ";");
		email = email.replace(/!/g, ";");
		email = email.replace(/#/g, ";");
		email = email.replace(/\$/g, ";");
		email = email.replace(/%/g, ";");
		email = email.replace(/&/g, ";");
		email = email.replace(/\*/g, ";");
		email = email.replace(/\(/g, ";");
		email = email.replace(/\)/g, ";");
		email = email.replace(/=/g, ";");
		email = email.replace(/\~/g, ";");
		email = email.replace(/\^/g, ";");
		email = email.replace(/\`/g, ";");
		email = email.replace(/\+/g, ";");
		email = email.replace(/\[/g, ";");
		email = email.replace(/\]/g, ";");
		email = email.replace(/{/g, ";");
		email = email.replace(/}/g, ";");
		email = email.replace(/\:/g, ";");
		email = email.replace(/\?/g, ";");
		email = email.replace(/\//g, ";");
		email = email.replace(/\\/g, ";");
		email = email.replace(/;;/g, ";");
		email = email.replace(/;;/g, ";");
		email = email.replace(/;;/g, ";");
		email = email.replace(/;;/g, ";");
		email = email.replace(/;;/g, ";");
		
		document.getElementById( controlToValidate ).value = email;
		if(email == "") return true;		
		// evaluate multiple e-mails separeted by colon or semi-colon
		var reEmail = /(([a-zA-Z0-9\-?\.?]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})(\W?[,;]\W?(?!$))?)+$/i;
		if ( (!reEmail.test(email)) || (email == "")) { displayMessage( validationText, validationControl ); }
	}

	// ----- ZipCode Validator - Validate only if not null (attempted entry) -------------
	function _validateZip( controlToValidate, validationText, validationControl ) {	
		var zip = trim( document.getElementById( controlToValidate ).value );
		if(zip == "") return true;
				
		// matches anything with an '@' followed by anything and a '.' followed by anything
		var reZip = /^\d{5}([\-]\d{4})?$/;	
		if ( (!reZip.test(zip)) || (zip == "")) { displayMessage( validationText, validationControl ); }
	}	
	// ----- Date Format Validator -------------
	function _validateDate( controlToValidate, validationText, validationControl ) {	
		var date = trim( document.getElementById( controlToValidate ).value );
				
		// RegEx #1 - below tests for valid dates in format of:  MM/dd/yyyy
		// var reDate = /((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))/
		// RegEx #2 - below test for:  yyyy/MM/dd
		 //var reDate = /(19|20)\d\d[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])/	
		//if ( (!reDate.test(date)) || (date == "")) { displayMessage( validationText, validationControl ); }
	}
	// ----- Passwords Match Validator -------------
	function _validatePasswords( controlPassword, controlConfirmPassword, validationText, validationControl ) {	
		var password = trim( document.getElementById( controlPassword ).value );
		var password2 = trim( document.getElementById( controlConfirmPassword ).value );
		if ( password != password2 ) { displayMessage( validationText, validationControl ); }
	}
	// ----- 1 Checkbox Item Is Required Validator -------------
	function _validateCheckbox( controlToValidate, validationText, validationControl ) {	
		var total = controlToValidate;
		if (total < 1) { displayMessage( validationText, validationControl ); }
	}
	
	// =====================  Private Class Functions   ==========================
	function displayMessage( validationText, validationControl ) {
		// first is entering this Fxn, then total validate global is now false
		_isValid = false;
		if ( (validationControl != "") && ( validationControl != undefined) ) 
			document.getElementById( validationControl ).innerHTML = validationText; 						
		else // for appending all messaging in one given <span> validator
			document.getElementById( _validatorControlName ).innerHTML += validationText + "<br/>"; 			
	}	
		
}


	// ======================  Utility Functions   ==============================
	// RegEx Trim Fxn
	function trim(value) {
		var temp = value;
		var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
		if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
		//var obj = /  /g;	// deletes empty strings in middle of string
		//while (temp.match(obj)) { temp = temp.replace(obj, " "); }
		return temp;
	}
	// Javascript Trim Fxn
	function trimAll(sString) 	{
		while (sString.substring(0,1) == ' ') 	{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ') 	{
			sString = sString.substring(0,sString.length-1);
		}
		return sString;
	}		




/*   ===============  SAVED FUNCTIONS ==================

function validate( controlToValidate, type, message, controlForMessage ) {	
		//if ( (controlToValidate != "" && controlToValidate != undefined ) && (controlForMessage != "" && controlForMessage != undefined ) ) { alert("set val control"); break;}
		switch ( type.toUpperCase() ) {
		
			case "REQUIRED":							
				if (trim( document.all( controlToValidate ).value ) == '' ) { displayMessage( message, controlForMessage ); }				
				break;
			
			case "EMAIL":
				//alert( type );
				break;
				
			case "MATCH_PASSWORDS":
				//alert( type );
				break;	
			
			default:			
				alert("Unknown validation type for javascript validator.");
		}
	}

======================================================  */

function validateDDL(ddl, msg) {
	if (ddl.value == 0) {
		alert(msg);
		return false;
	}
	
	return true;
}

function validateTB(txt, msg) {
	var regex = /\d*/;
	
	if (txt == null || txt.value.replace(/ /g, "") == "" || !regex.test(txt.value)) {
		alert(msg);
		return false;
	}
	
	return true;
}
function validateTextFieldByID(tfID, msg) {
	tf = document.getElementById(tfID);
	var regex = /\d*/;
	if (tf == null || tf.value.replace(/ /g, "") == "" || !regex.test(tf.value)) {
		alert(msg);
		return false;
	}
	
	return true;
}


