﻿//===================================================================
//generic form validation logic
//===================================================================
var myValidator = new FormValidation();
$(document).ready(function () {
	$('form[data-validation="generic"]').submit(function () {
		//trigger ckeditors to update their textareas with currently entered string
		if (typeof CKEDITOR != "undefined") {
			var test = CKEDITOR.instances;
			for (var name in test)
				CKEDITOR.instances[name].updateElement();
		}
		if (myValidator.ValidateAll(this)) {
			var applyurl = $(this).data("applyurl");
			if (applyurl)
				window.open(applyurl, "blank", "width=1024,height=768,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,fullscreen=0,channelmode=0,screenX=50,left=200,screenY=50,top=0");

			return myValidator.ValidateAll(this);
		}
		else
			return false;
	});

	$('form[data-validation="register"]').submit(function () {
		//trigger ckeditors to update their textareas with currently entered string
		if (typeof CKEDITOR != "undefined") {
			var test = CKEDITOR.instances;
			for (var name in test)
				CKEDITOR.instances[name].updateElement();
		}
		if (myValidator.ValidateAll(this)) {
			checkSignup(this, true, true);
			return myValidator.ValidateAll(this);
		}
		else
			return false;
	});
});

function checkSignup(_form, doValidate, doSendPassword) {
	//if the form validated we only want to do the rest of this once
	//copied from functions.form.freepage-src.js, which will be deleted eventually when we no longer need that file
	$(_form).unbind("submit");
	$.post
	("/common/services/jobseeker/jobseeker.validateSignup/default.asp?email=" + $(_form).find("#FEAdd").val(), {},
		function (data) {
			data = data.toUpperCase();
			if (data == "RETRO") {
				//old account - login or signup
				//hide the signup form
				$("#signupForm").hide();
				//prepop email address in the login form
				$("#FEmailAddress").val($("#FEAdd").val());
				//flag signup form so we can write retro contactoption
				$("#FRetro").val("Y");
				//show user message
				$("#retroMessage").show();
				//show login form
				$("#loginForm").show();
				//show request password button
				$("#reqPassword").show();
				//show continue button option
				$("#continueSignup").show();
			}
			else if (data == "DUPE") {
				//dupe account - send password and force login
				//send password
				if (doSendPassword) {
					sendPassword($("#FEAdd").val())
				}
				//hide the signup form
				$("#signupForm").hide();
				//prepop email address in the login form
				$("#FEmailAddress").val($("#FEAdd").val());
				//show user message
				$("#dupeMessage").show();
				//show login form
				$("#loginForm").show();
			}
		}
	);
}

//form validation object controling what validation to run
//validates all visible inputs/textareas using custom attributes for error message, and class/type to determine validation methodology
function FormValidation() {
	return {
		isValid: true,
		errorSummary: "",
		form: "",
		ValidateAll: function (formToValidate) {
			this.form = formToValidate;
			this.errorSummary = "";

			this.isValid = true;
			this.ValidateRequired();
			this.ValidateEmail();
			this.ValidateDependency();
			this.ValidateDependencyCustom();
			this.ValidateAlpha();
			if (!this.isValid)
				alert("The form can not be submitted due to invalid or missing data.\n" + this.errorSummary + "\n\nPlease review the field(s) and rectify the problem(s).");

			function resetValidation() {
				$('form[data-validation="register"]').submit(function () {
					if (myValidator.ValidateAll(this)) {
						checkSignup(this, true, true);
						return myValidator.ValidateAll(this);
					}
					else
						return false;
				});
			}

			return this.isValid;
		},

		//validates required fields
		//supports textareas, inputs
		ValidateRequired: function () {
			var myValidator = this;
			var emptyString = /^\s*$/;
			$(this.form).find("input.required:visible, textarea.required:visible, select.required:visible, input.required[myTip], .alwaysValidate").each(function (index) {
				if (emptyString.test($(this).val()))
					controlFailed(this, myValidator);
				else
					controlPassed(this);
			});
			$(this.form).find("input.required:checkbox").each(function (index) {
				if (!$(this).prop("checked"))
					controlFailed(this, myValidator);
				else
					controlPassed(this);
			});
		},

		//if the "depends" attribute is present and checked/has text, then the dependant control must have text as well
		//supports textareas, inputs
		ValidateDependency: function () {
			var myValidator = this;
			$(this.form).find("input:visible[depends], textarea:visible[depends]").each(function (index) {

				if (($(this.form).find("#" + $(this).attr("depends")).attr("checked") != false | $(this.form).find("#" + $(this).attr("depends")) == "") & ($(this).val() == ""))
					controlFailed(this, myValidator);
				else
					controlPassed(this);
			});
		},
		ValidateEmail: function () {
			var myValidator = this;
			$(this.form).find("input.email:visible, textarea.email:visible").each(function (index) {
				if (validateEmailRegex($(this).val()) & validateEmailBlacklist($(this).val()) & validationEmailTypos($(this).val()))
					controlPassed(this);
				else
					controlFailed(this, myValidator);
			});
		},

		ValidateAlpha: function () {
			var myValidator = this;
			$(this.form).find("input.alpha:visible, textarea.alpha:visible").each(function (index) {
				var filter = /^[a-zA-Z]+$/;
				if (filter.test($(this).val()))
					controlPassed(this);
				else
					controlFailed(this, myValidator);
			});
		},

		ValidateNoHTML: function () {
			var myValidator = this;
			$(this.form).find("input.nohtml:visible, textarea.nohtml:visible").each(function (index) {
				var filter = /<\/?[^>]*>/;
				if (filter.test($(this).val()))
					controlPassed(this);
				else
					controlFailed(this, myValidator);
			});
		},

		//1.	if autotrack is checked on edit content page, verify '##BYERED##', doesn't occur in the html field
		//supports textareas, inputs
		//will implement when needed
		ValidateDependencyCustom: function () {
			/*
			var myValidator = this;
			$(form).find("input:visible[dependsTrack], textarea:visible[dependsTrack]").each(function () {
			var htmlString = $(this).val();
			var redFilter = /##BYDRED##/;

			if (($(form).find("#" + $(this).attr("dependsTrack")).attr("checked")) & (redFilter.test(htmlString)))
			controlFailed(this, myValidator);
			else
			controlPassed(this);
			});
			*/
		}
	};
}

function controlFailed(control, validatorObj) {
	//make sure this input has not already had a failure based on the "myTip" message being in the errorSummary
	if (validatorObj.errorSummary.indexOf($(control).attr("myTip")) == -1) {
		//add this error message to the summary
		validatorObj.errorSummary += "\n* " + $(control).attr("myTip");
		//form state is invalid
		validatorObj.isValid = false;

		//mark form field as invalid and set tool tip
		var styleFilter = /\/common\/templates\/template10\/images\/bg-input\.png/;
		if (styleFilter.test($(control).css("background-image")))
			$(control).css("background-image", 'url(/common/templates/template10/images/bg-input-error.png)');
		else
			$(control).css("background-color", "#ffc82e");

		$(control).attr("title", $(control).attr("myTip"));
	}
}

function controlPassed(control) {
	//mark form field as valid and clear tool tip
	var styleFilter = /\/common\/templates\/template10\/images\/bg-input\.png/;
	if (styleFilter.test($(control).css("background-image")))
		$(control).css("background-image", 'url(/common/templates/template10/images/bg-input.png)');
	else
		$(control).css("background-color", "white");

	$(control).attr("title", "");
}

/* NOTE: this is from the common/javascript/beyond/beyond.js file, which this validation should call or be merged with at some point */
function validateEmailRegex(email) {
	var filter = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email))
		return true;
	else
		return false;
}

function validateMultiEmail(email) {
	email = email.replace(/[\s]|[ ]/g, "");
	var filter = /^(([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+([\,]|[\;]){0,1}([\n])*)+$/;
	if (filter.test(email))
		return true;
	else
		return false;
}

function validateEmailBlacklist(email) {
	if (email.indexOf("@hotjobs") >= 0) { return (false); }
	if (email.indexOf("@monster") >= 0) { return (false); }
	if (email.indexOf("blacklist@") >= 0) { return (false); }
	if (email.indexOf("bulkmail@") >= 0) { return (false); }
	if (email.indexOf("devnull@") >= 0) { return (false); }
	if (email.indexOf("mailer-daemon@") >= 0) { return (false); }
	if (email.indexOf("@mailinator.com") >= 0) { return (false); }
	if (email.indexOf("@mytrashmail.com") >= 0) { return (false); }

	return true;
}

function validationEmailTypos(email) {
	email = email.toLowerCase();

	if (email.indexOf("@gmial.com") >= 0) { return (false); }
	if (email.indexOf("@gmail.cm") >= 0) { return (false); }
	if (email.indexOf("@hotmaill.com") >= 0) { return (false); }
	if (email.indexOf("@htomail.com") >= 0) { return (false); }
	if (email.indexOf("@yahoomail.com") >= 0) { return (false); }
	if (email.indexOf("@a0l.com") >= 0) { return (false); }
	if (email.indexOf("@ahoo.com") >= 0) { return (false); }
	if (email.indexOf("@ayahoo.com") >= 0) { return (false); }
	if (email.indexOf("@ayhoo.com") >= 0) { return (false); }
	if (email.indexOf("@cocast.net") >= 0) { return (false); }
	if (email.indexOf("@cocmast.net") >= 0) { return (false); }
	if (email.indexOf("@comast.net") >= 0) { return (false); }
	if (email.indexOf("@comcas.net") >= 0) { return (false); }
	if (email.indexOf("@comcst.net") >= 0) { return (false); }
	if (email.indexOf("@concast.net") >= 0) { return (false); }
	if (email.indexOf("@gamil.com") >= 0) { return (false); }
	if (email.indexOf("@gmai.com") >= 0) { return (false); }
	if (email.indexOf("@gmaill.com") >= 0) { return (false); }
	if (email.indexOf("@gmal.com") >= 0) { return (false); }
	if (email.indexOf("@homail.com") >= 0) { return (false); }
	if (email.indexOf("@homtail.com") >= 0) { return (false); }
	if (email.indexOf("@hotamil.com") >= 0) { return (false); }
	if (email.indexOf("@hotmai.com") >= 0) { return (false); }
	if (email.indexOf("@hotmal.com") >= 0) { return (false); }
	if (email.indexOf("@hotmial.com") >= 0) { return (false); }
	if (email.indexOf("@hotmil.com") >= 0) { return (false); }
	if (email.indexOf("@yaahoo.com") >= 0) { return (false); }
	if (email.indexOf("@yah00.com") >= 0) { return (false); }
	if (email.indexOf("@yahaoo.com") >= 0) { return (false); }
	if (email.indexOf("@yahho.com") >= 0) { return (false); }
	if (email.indexOf("@yaho.com") >= 0) { return (false); }
	if (email.indexOf("@yahool.com") >= 0) { return (false); }
	if (email.indexOf("@yahooo.com") >= 0) { return (false); }
	if (email.indexOf("@yahpp.com") >= 0) { return (false); }
	if (email.indexOf("@yaoo.com") >= 0) { return (false); }
	if (email.indexOf("@yayoo.com") >= 0) { return (false); }
	if (email.indexOf("@yhaoo.com") >= 0) { return (false); }
	if (email.indexOf("@yhoo.com") >= 0) { return (false); }
	if (email.indexOf("@yohoo.com") >= 0) { return (false); }
	if (email.indexOf("@yahoo.cm") >= 0) { return (false); }
	if (email.indexOf("@yshoo.com") >= 0) { return (false); }
	if (email.indexOf("@aol.ocm") >= 0) { return (false); }
	if (email.indexOf("@aol.om") >= 0) { return (false); }
	if (email.indexOf("@aol.con") >= 0) { return (false); }
	if (email.indexOf("@gail.com") >= 0) { return (false); }
	if (email.indexOf("@gmail.con") >= 0) { return (false); }
	if (email.indexOf("@gmail.om") >= 0) { return (false); }
	if (email.indexOf("@gnail.com") >= 0) { return (false); }
	if (email.indexOf("@hotmail.cm") >= 0) { return (false); }
	if (email.indexOf("@hotmail.con") >= 0) { return (false); }
	if (email.indexOf("@hotmail.om") >= 0) { return (false); }
	if (email.indexOf("@verion.net") >= 0) { return (false); }
	if (email.indexOf("@yahoo.cim") >= 0) { return (false); }
	if (email.indexOf("@yahoo.come") >= 0) { return (false); }
	if (email.indexOf("@yahoo.comm") >= 0) { return (false); }
	if (email.indexOf("@yahoo.con") >= 0) { return (false); }
	if (email.indexOf("@yahoo.coom") >= 0) { return (false); }
	if (email.indexOf("@yahoo.cpm") >= 0) { return (false); }
	if (email.indexOf("@yahoo.ocm") >= 0) { return (false); }
	if (email.indexOf("@yahoo.om") >= 0) { return (false); }
	if (email.indexOf("@yahoo.vom") >= 0) { return (false); }
	if (email.indexOf("@tahoo.com") >= 0) { return (false); }


	//catch domains that are not valid
	var filter = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+(cmo|hotmial|htomail|yhoo|hotmial|ogr|eud|rog|deu|nte|txt)+$/;

	if (filter.test(email))
		return false;
	else
		return true;

	return true;
}
//email validators===============================================================================================================
