/// <reference path="jquery.js" />
/// <reference path="DdmSys.js" />
/**********************************************************
DdmSys.Validation library is an extension to the DDM library.
This library is used for client side validation. It can be
used for form validation or real-time form element format
validation. It should also be used with the Validation.css
Style sheet file.
***********************************************************/
if((typeof DdmSys=='undefined'))
	alert("DdmSys.Validation requires the DdmSys JavaScript framework >= 2.0.0");

DdmSys.Validation = {
	Version: '2.0.01',
	Copyright: 'Copyright (c)Dynamic Digital Media Group',
	Author: 'Glenn Byron',
	countryUSA: '229',

	FirstEmptyField: null,
	// **************************************************************
	// Used by form validation most of the time.
	// When used in form validation make sure you check
	// the different form elements in order so the first element
	// that is emptpy can be found. Then use the setFocusFirstField()
	// function to return to or set focus to the first empty element.
	// **************************************************************
	isNotEmpty: function(el) {
		if (el.type == 'text' || el.type == 'password' || el.type == 'hidden' || el.type == 'textarea') {
			if (el.value == "" || el.value == null) {
				this.setErrorColor(el);
				this._setFirstEmpty(el);
				return false;
			}
		}
		return true
	},

	isNumeric: function(el) {
		if (el.value.toString().isNumberic()) return true;
		alert("The selected field must be a number.");
		this.selectError(el);
		return false;
	},

	isSignedInteger: function(el) {
		if (el.value.toString().isSignedInteger()) return true;
		alert("The selected field must be a whole number (no decimal).");
		this.selectError(el);
		return false;
	},

	isPositiveInteger: function(el) {
		if (el.value.toString().isPostiveInteger()) return true;
		alert("The selected field must be a positive, whole number (no decimal).");
		this.selectError(el);
		return false;
	},

	isEmailAddr: function(el) {
		var inputStr = el.value.toString();
		if (inputStr.length == 0) return true

		var checkTLD = 1;
		var knownDomsPat = new $A(Array(
		'com', 'net', 'org', 'biz', 'coop', 'info', 'museum', 'name', 'pro', 'edu', 'gov', 'int', 'mil',
		'ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'as', 'at', 'au', 'aw', 'az',
		'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz',
		'ca', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz',
		'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'ee', 'eg', 'eh', 'er', 'es', 'et', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr',
		'ga', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gv', 'gy',
		'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'io', 'iq', 'ir', 'is', 'it',
		'je', 'jm', 'jo', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz',
		'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly',
		'ma', 'mc', 'md', 'mg', 'mh', 'mk', 'ml', 'mm', 'mn', 'mo', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz',
		'na', 'nc', 'ne', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz',
		'om', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rw', 'ru',
		'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'st', 'sv', 'sy', 'sz', 'tc',
		'td', 'tf', 'tg', 'th', 'tj', 'tk', 'tm', 'tn', 'to', 'tp', 'tr', 'tt', 'tv', 'tw', 'tz',
		'ua', 'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'ws', 'wf', 'ye', 'yt', 'yu', 'za', 'zm', 'zw'));
		var emailPat = /^(.+)@(.+)$/;
		var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars = "\[^\\s" + specialChars + "\]";
		var quotedUser = "(\"[^\"]*\")";
		var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom = validChars + '+';
		var word = "(" + atom + "|" + quotedUser + ")";
		var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
		var matchArray = inputStr.match(emailPat);

		if (matchArray == null) {
			alert("The Email Address Is Invalid");
			this.selectError(el);
			return false;
		}

		var user = matchArray[1];
		var domain = matchArray[2];
		for (i = 0; i < user.length; i++) {
			if (user.charCodeAt(i) > 127) {
				alert("The Username Contains Invalid Characters.");
				this.selectError(el);
				return false;
			}
		}

		for (i = 0; i < domain.length; i++) {
			if (domain.charCodeAt(i) > 127) {
				alert("Ths Domain Name Contains Invalid Characters.");
				this.selectError(el);
				return false;
			}
		}

		if (user.match(userPat) == null) {
			alert("The Username Is Invalid.");
			this.selectError(el);
			return false;
		}

		var IPArray = domain.match(ipDomainPat);
		if (IPArray != null) {
			for (var i = 1; i <= 4; i++) {
				if (IPArray > 255) {
					alert("The Destination IP Address Is Invalid.");
					this.selectError(el);
					return false;
				}
			}
			return true;
		}

		var atomPat = new RegExp("^" + atom + "$");
		var domArr = domain.split(".");
		var len = domArr.length;
		for (i = 0; i < len; i++) {
			if (domArr[i].search(atomPat) == -1) {
				alert("The Domain Name Is Invalid.");
				this.selectError(el);
				return false;
			}
		}

		if (checkTLD && null == knownDomsPat.find(function(domainType) { return (domainType == domArr[domArr.length - 1]); })) {
			alert("The Domain Name Extension Is Invalid");
			this.selectError(el);
			return false;
		}

		if (len < 2) {
			alert("The Address Is Missing A Hostname.");
			this.selectError(el);
			return false;
		}
		return true;
	},

	hasNoSpaces: function(el) {
		var inputStr = el.value.toString();
		if (el.type == 'text' || el.type == 'textarea' || el.type == 'password') {
			if (inputStr.indexOf(' ') >= 1) {
				alert("The selected field must not have spaces in it.")
				this.selectError(el);
				return false;
			}
		}
		return true;
	},

	isUSPhone: function(el) {
		if (el.value.toString().isUSPhone()) {
			el.value = el.value.toString().cleanSeperators().restoreUSPhone();
			return true;
		}
		alert("A phone number must be 10 numbers only, including Area Code. The entry format is \'212-555-1212\' or \'2125551212\' or \'(714)555-1212\'.")
		this.selectError(el);
		return false
	},

	isUSDate: function(el) {
		if (el.value.toString().isUSDate()) return true;
		alert("The is not a vaild date.\n\nYou can enter dates in the following formats: mmddyyyy, mm/dd/yyyy, or mm-dd-yyyy.  (If the month or date data is not available, enter \'01\' in the appropriate location.)")
		this.selectError(el);
		return false; //any other values, bad date
	},

	isUSZipCode: function(el) {
		if (el.value.toString().isPostiveInteger()) {
			if (el.value.toString().isUSZipCode()) return true;
			alert("Enter the first five digits of the ZIP code in this field.")
			this.selectError(el);
			return false
		} else {
			alert("A Zip Code must be all numbers in this field.")
			this.selectError(el);
			return false;
		}
	},

	//********************
	// validation utilities
	//********************
	setErrorColor: function(el) {
		if (el.length > 0 && el.type != 'select-one') return;
		if (!el) return;
		var tempList;
		var labelList;
		var ckLabel;

		if (el.style) {
			if (el.type == 'text' || el.type == 'password' || el.type == 'textarea') {
				$(el).removeClass('defaultTextBox').addClass('errorTextBox');
				$(el).one('blur', function() {
					$(this).removeClass('errorTextBox');
					$(this).addClass('defaultTextBox');
				});
			}
		}
	},

	setDefaultColor: function(el) {
		var tempResetColor = new Function();
		tempResetColor = this.resetColor.bind($(el)[0]);
		tempResetColor();
		return;
	},

	resetColor: function() {
		//Event.stopObserving(this, 'blur', this.resetColor);
		if (this.style) {
			if (this.type == 'text' || this.type == 'password' || this.type == 'textarea') {
				this.removeClassName("errorTextBox");
				this.addClassName("defaultTextBox");
			}
		}
	},

	selectError: function(el, doNotDefault) {
		if (!el) return false;
		//setTimeouts required for IE
		if (!doNotDefault) el.value = el.defaultValue
		//setTimeout(function() { DdmSys.Validation.setErrorColor(el) } .bind(el), 0);
		setTimeout(function() { el.focus() } .bind(el), 0);
		if (el.type != 'select-one') el.select();
	},

	_setFirstEmpty: function(el) {
		if (!this.FoundEmpty) {
			this.FirstEmptyField = el;
			this.FoundEmpty = true;
		}
	},

	setFocusFirstField: function() {
		if (this.FirstEmptyField && this.FirstEmptyField.style) setTimeout("DdmSys.Validation.FirstEmptyField.focus()", 0);
		this.FoundEmpty = false;
	},

	//*****************
	// Submit validation stuff
	//********************
	formElementsToValidate: function(elementId, errorMsg) {
		this.ElementId = elementId;
		this.ErrorMsg = errorMsg;
	},

	getErrorMessageTop: function(msg) {
		if (msg == "") {
			msg = "______________________________________________________\n\n";
			msg += "You were not able to Continue because of the following error(s).\n";
			msg += "Please correct these error(s) and click Continue.\n";
			msg += "______________________________________________________\n\n";
		}
		return msg;
	},

	getAddressErrorMessageTop: function(msg) {
		if (msg == "") {
			msg = "______________________________________________________\n\n";
			msg += "You started an address, but we don't have everything required.\n";
			msg += "Please correct these error(s) and click Continue.\n";
			msg += "______________________________________________________\n\n";
		}
		return msg;
	},

	submitValidation: function(formElements) {
		var msg = '';
		for (var i = 0; i < formElements.length; i++) {
			if (!this.isNotEmpty($('#' + formElements[i].ElementId)[0])) {
				msg = this.getErrorMessageTop(msg);
				msg += "-" + formElements[i].ErrorMsg + ".\n";
			}
		}
		return msg;
	},

	compareValidator: function(firstElement, secondElement, errorMsg) {
		var msg = '';

		//If these are IDs they are now elements
		firstElement = $(firstElement)[0];
		secondElement = $(secondElement)[0];

		if (firstElement.value != '' && secondElement.value != '') {
			if (firstElement.value.toLowerCase() != secondElement.value.toLowerCase()) {
				msg = this.getErrorMessageTop(msg);
				msg += "-" + errorMsg + "\n";
				this.setErrorColor(firstElement);
				this.setErrorColor(secondElement)
				this._setFirstEmpty(firstElement);
			} else {
				this.setDefaultColor.bind(firstElement);
				this.setDefaultColor.bind(secondElement);
			}
		}
		return msg;
	},

	submitPhoneNumberValidation: function(phoneNumber, phoneNumberType) {
		var msg = '';

		phoneNumber = $(phoneNumber)[0];
		phoneNumberType = $(phoneNumberType)[0];

		if (phoneNumber.value != '' && phoneNumberType.value == '') {
			msg = this.getErrorMessageTop(msg);
			msg += "-You must select a phone number type\n";
			this.setErrorColor(phoneNumberType);
			this._setFirstEmpty(phoneNumberType);
		} else {
			this.setDefaultColor(phoneNumberType);
		}
		return msg;
	},

	submitAddressValidation: function(elAddress1, elAddress2, elCity, elState, elPostalCode, elCountryID) {
		for (var i = 0; i < arguments.length; i++) {
			if (typeof (arguments[i]) == 'string') arguments[i] = $(arguments[i])[0]; //make sure they are form elements
			if (typeof (arguments[i]) != 'undefined' && arguments[i]) {
				this.setDefaultColor(arguments[i]);
			}
		}

		var countryID = this.countryUSA; // default to USA if no country form field
		if (typeof (elCountryID) != 'undefined' && elCountryID != null) countryID = elCountryID.value;

		// if address is only in second address element swap with first
		if (elAddress1.value == "" && elAddress2.value != "") {
			elAddress1.value = elAddress2.value;
			elAddress2.value = "";
		}

		var msg = ""
		//if (elState.type == 'select-one' && elState.value != "0") elState.value = "";
		if (elAddress1.value + elAddress2.value + elCity.value + elState.value + elPostalCode.value != "" ||
				(typeof (elCountryID) != 'undefined' && countryID == this.countryUSA && elState.value != "")) {

			formElements = new Array();
			formElements[0] = new this.formElementsToValidate(elAddress1, 'You must enter an address');
			formElements[1] = new this.formElementsToValidate(elCity, 'You must enter a city');

			for (var i = 0; i < formElements.length; i++) {
				if (!this.isNotEmpty(formElements[i].ElementId)) {
					msg = this.getAddressErrorMessageTop(msg);
					msg += "-" + formElements[i].ErrorMsg + ".\n";
				}
			}
			if (countryID == this.countryUSA && !this.isNotEmpty(elState)) {
				msg = this.getAddressErrorMessageTop(msg);
				msg += "-" + "You must select a state.\n";
				this._setFirstEmpty(elState);
			}
			if (countryID == this.countryUSA && !this.isNotEmpty(elPostalCode)) {
				msg = this.getAddressErrorMessageTop(msg);
				msg += "-" + "You must enter a postal code.\n";
				this._setFirstEmpty(elPostalCode);
			}
		}
		return msg;
	}
}
