﻿


var MedrecruitValidation = {

	RegEmail: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
	PasswordSecure: /^.*(?=.{6,}).*$/,
	WeakPasswordLenght: 3,
	ModeratePasswordLenght: 5,
	MinPasswordLenght: 6,
	ErrorBox: "<span class=\"valid error\"><div class=\"rollover\"><div class=\"text\">{0}</div></div></span>",
	ErrorStyle: " error",
	ToolTipStyle: "tooltip",
	OkStyle: 'valid ok',

	blockNumbersInput: function (source, eventArgs) {
		var c = eventArgs.get_keyCode();
		if (((c < 65) ||
                (c > 90 && c < 97) ||
                (c > 122)) && (c != 8) && (c != 32) && (c != 9) && (c != 13))
			eventArgs.set_cancel(true);
	},
	blockNotPhoneInput: function (source, eventArgs) {
		var c = eventArgs.get_keyCode();
		if (((c < 48) ||
                (c > 57)) && c != 32 && c != 28 && c != 29 && c != 45 && (c != 8) && (c != 9) && (c != 13))
			eventArgs.set_cancel(true);
	},
	showWarning: function (checkbox, label) {
		if (!checkbox.get_checked())
			label.style.display = 'none';
		else
			label.style.display = '';
	},
	isRatingNotEmpty: function (source, rating, tip, message) {
		tip.hide();
		if (rating._value == 0) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
			return false;
		}

		MedrecruitValidation.showOk(tip);
		return true;
	},
	updatePageStatus: function (submit, errorImage, errorMessage) {
		if (!Page_IsValid) {
			errorImage.show();
			errorMessage.show();
			submit.disabled = true;
		}
		else {
			errorImage.hide();
			errorMessage.hide();
			submit.disabled = true;
		}
	},

	showOk: function (tip) {
		tip.removeClass(MedrecruitValidation.ToolTipStyle);
		tip.addClass(MedrecruitValidation.OkStyle);
		tip.show();
	},
	setRadTextBoxErrorStyle: function (control, setError) {
		control.get_styles().EnabledStyle[1] = control.get_styles().EnabledStyle[1].replace(MedrecruitValidation.ErrorStyle, "");
		control.get_styles().DisabledStyle[1] = control.get_styles().DisabledStyle[1].replace(MedrecruitValidation.ErrorStyle, "");
		control.get_styles().EmptyMessageStyle[1] = control.get_styles().EmptyMessageStyle[1].replace(MedrecruitValidation.ErrorStyle, "");
		control.get_styles().FocusedStyle[1] = control.get_styles().FocusedStyle[1].replace(MedrecruitValidation.ErrorStyle, "");
		control.get_styles().HoveredStyle[1] = control.get_styles().HoveredStyle[1].replace(MedrecruitValidation.ErrorStyle, "");
		control.get_styles().InvalidStyle[1] = control.get_styles().InvalidStyle[1].replace(MedrecruitValidation.ErrorStyle, "");

		if (setError == true) {
			control.get_styles().EnabledStyle[1] += MedrecruitValidation.ErrorStyle;
			control.get_styles().DisabledStyle[1] += MedrecruitValidation.ErrorStyle;
			control.get_styles().EmptyMessageStyle[1] += MedrecruitValidation.ErrorStyle;
			control.get_styles().FocusedStyle[1] += MedrecruitValidation.ErrorStyle;
			control.get_styles().HoveredStyle[1] += MedrecruitValidation.ErrorStyle;
			control.get_styles().InvalidStyle[1] += MedrecruitValidation.ErrorStyle;
		}
		control.updateCssClass();
	},
	isRadTextBoxNotEmpty: function (source, control, tip, message) {
		if (tip != null)
			tip.hide();

		if (control.get_value() == "") {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}

			control.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		return true;
	},
	isDoubleRadTextBoxNotEmpty: function (source, control, control1, tip, tip1, message) {
		if (tip != null)
			tip.hide();
		if (tip1 != null)
			tip1.hide();

		if (control.get_value() == "") {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
				MedrecruitValidation.setRadTextBoxErrorStyle(control1, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
				MedrecruitValidation.setRadTextBoxErrorStyle(control1, true);
			}

			control.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);
		MedrecruitValidation.setRadTextBoxErrorStyle(control1, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		if (tip1 != null)
			MedrecruitValidation.showOk(tip1);
		return true;
	},
	isPasswordNotEmptyAndSecure: function (source, password, passwordConfirm, tip, tip1, skip) {
		if (skip != true)
			tip.hide();
		tip1.hide();

		if (password.get_value() == "") {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_password_required);
			MedrecruitValidation.setRadTextBoxErrorStyle(password, true);
			MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, true);
			password.focus();
			tip.hide();
			return false;
		}

		if (!MedrecruitValidation.PasswordSecure.test(password.get_value())) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_password_tooshort);
			MedrecruitValidation.setRadTextBoxErrorStyle(password, true);
			MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, true);
			password.focus();
			tip.hide();
			return false;
		}

		if (skip)
			return true;

		MedrecruitValidation.setRadTextBoxErrorStyle(password, false);
		MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, false);
		MedrecruitValidation.showOk(tip);
		MedrecruitValidation.showOk(tip1);
		return true;
	},
	isPasswordConfirmed: function (source, password, passwordConfirm, tip, tip1) {
		tip.hide();
		tip1.hide();

		if (passwordConfirm.get_value() == "") {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_password_required);
			MedrecruitValidation.setRadTextBoxErrorStyle(password, true);
			MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, true);
			passwordConfirm.focus();
			return false;
		}

		if (password.get_value() != passwordConfirm.get_value()) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_password_mismatch);
			MedrecruitValidation.setRadTextBoxErrorStyle(password, true);
			MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, true);
			passwordConfirm.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(password, false);
		MedrecruitValidation.setRadTextBoxErrorStyle(passwordConfirm, false);
		MedrecruitValidation.showOk(tip);
		MedrecruitValidation.showOk(tip1);
		return true;
	},
	onPasswordBlur: function (sender, args) {
		//if (sender.get_value() != '')
		//	sender._textBoxElement.type = 'Password';
		//else
		//	sender._textBoxElement.type = 'SingleLine';
	},
	onPasswordFocus: function (sender, args) {
		//sender._textBoxElement.type = 'Password';
	},
	setRadDatePickerErrorStyle: function (control, setError) {
		control.removeCssClass(MedrecruitValidation.ErrorStyle);

		if (setError == true) {
			control.addCssClass(MedrecruitValidation.ErrorStyle);
		}
	},
	setRadComboBoxErrorStyle: function (control, setError) {
		control.removeCssClass(MedrecruitValidation.ErrorStyle);

		if (setError == true) {
			control.addCssClass(MedrecruitValidation.ErrorStyle);
		}
	},
	setCountryCodeComboBoxErrorStyle: function (control, setError) {
		control._element.firstChild.control.removeCssClass(MedrecruitValidation.ErrorStyle);

		if (setError == true) {
			control._element.firstChild.control.addCssClass(MedrecruitValidation.ErrorStyle);
		}
	},
	isEmailNotEmptyAndSecure: function (source, email, tip, message) {
		if (tip != null)
			tip.hide();

		if (email.get_value() == "") {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
			}
			else {
				source.ErrorMessage = message;
			}
			MedrecruitValidation.setRadTextBoxErrorStyle(email, true);
			email.focus();
			return false;
		}

		if (MedrecruitValidation.RegEmail.test(email.get_editValue()) == false) {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_email_notvalid);
			}
			else {
				source.ErrorMessage = StringConstants.error_email_notvalid;
			}
			MedrecruitValidation.setRadTextBoxErrorStyle(email, true);
			email.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(email, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		return true;
	},
	showRadTextBoxTip: function (control, tip) {
		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);

		tip.removeClass(MedrecruitValidation.OkStyle);
		tip.addClass(MedrecruitValidation.ToolTipStyle);
		control.updateCssClass();
		tip.show();
	},
	showTip: function (tip) {

		tip.removeClass(MedrecruitValidation.OkStyle);
		tip.addClass(MedrecruitValidation.ToolTipStyle);
		tip.show();
	},
	showRadComboTip: function (control, tip) {
		MedrecruitValidation.setRadComboBoxErrorStyle(control, false);

		tip.removeClass(MedrecruitValidation.OkStyle);
		tip.addClass(MedrecruitValidation.ToolTipStyle);
		tip.show();
	},
	resetRadTextBoxTip: function (control, tip) {
		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);

		tip.removeClass(MedrecruitValidation.OkStyle);
		control.updateCssClass();
	},
	resetTip: function (tip) {
		tip.removeClass(MedrecruitValidation.OkStyle);
	},
	resetRadComboTip: function (control, tip) {
		MedrecruitValidation.setRadComboBoxErrorStyle(control, false);

		tip.removeClass(MedrecruitValidation.OkStyle);
	},
	isRadComboBoxNotEmpty: function (source, value, position, tip, message) {
		tip.hide();

		if (value == "" || value == position._emptyMessage) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
			MedrecruitValidation.setRadComboBoxErrorStyle(position, true);
			//position.focus();
			return false;
		}

		MedrecruitValidation.setRadComboBoxErrorStyle(position, false);
		MedrecruitValidation.showOk(tip);
		return true;
	},
	isCountryCodeNotEmpty: function (source, countryCode, tip, message) {
		if (tip != null)
			tip.hide();

		if (countryCode.getSelectedValue() == null || countryCode.getSelectedValue() == "") {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, true);
			}
			return false;
		}
		MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		return true;
	},
	isPhoneNotEmptyAndValid: function (source, phone, countryCode, tip, ctip, validator) {
		ctip.hide();
		tip.hide();

		if (countryCode.getSelectedValue() == null || countryCode.getSelectedValue() == "") {
			MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, true);
			validator.style.display = "inline";
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", StringConstants.error_contacts_required);
			MedrecruitValidation.setRadTextBoxErrorStyle(phone, true);
			return false;
		}

		if (!MedrecruitValidation.isRadTextBoxNotEmpty(source, phone, tip, StringConstants.error_contacts_required)) {
			MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, true);
			phone.focus();
			validator.style.display = "inline";
			return false;
		}

		if (!MedrecruitValidation.isShortPhoneValid(source, phone, tip, StringConstants.error_contacts_required)) {
			MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, true);
			phone.focus();
			validator.style.display = "inline";
			return false;
		}

		MedrecruitValidation.setCountryCodeComboBoxErrorStyle(countryCode, false);
		MedrecruitValidation.showOk(ctip);
		MedrecruitValidation.showOk(tip);
		validator.style.display = "none";
		return true;
	},
	isNameValid: function (source, control, tip, message) {
		if (tip != null)
			tip.hide();

		NameFormat = /^[A-z\s]*$/;
		if (!NameFormat.test(control.get_value())) {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}

			control.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		return true;
	},
	isDoubleNameValid: function (source, control, control1, tip, tip1, message) {
		if (tip != null)
			tip.hide();
		if (tip1 != null)
			tip1.hide();

		NameFormat = /^[A-z\s]*$/;
		if (!NameFormat.test(control.get_value())) {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
				MedrecruitValidation.setRadTextBoxErrorStyle(control1, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
				MedrecruitValidation.setRadTextBoxErrorStyle(control1, true);
			}

			control.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);
		MedrecruitValidation.setRadTextBoxErrorStyle(control1, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		if (tip1 != null)
			MedrecruitValidation.showOk(tip1);
		return true;
	},
	isShortPhoneValid: function (source, control, tip, message) {
		if (tip != null)
			tip.hide();

		ShortPhoneFormat = /^[0-9\s()]*$/;
		if (!ShortPhoneFormat.test(control.get_value())) {
			if (tip != null) {
				source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message);
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}
			else {
				source.ErrorMessage = message;
				MedrecruitValidation.setRadTextBoxErrorStyle(control, true);
			}

			control.focus();
			return false;
		}

		MedrecruitValidation.setRadTextBoxErrorStyle(control, false);
		if (tip != null)
			MedrecruitValidation.showOk(tip);
		return true;
	},
	initializeSaveCompleteField: function (hiddenFieldId) {
		var hiddenSaveComplete = $(hiddenFieldId);
		if (hiddenSaveComplete.attr('Value') === 'true') {
			$('.save-complete-message').show().delay(10000).fadeOut('slow');
		}
		else {
			$('.save-complete-message').hide();
		}
		hiddenSaveComplete.attr('Value', 'false');
	},
	validateDates: function (source, startDateControl, endDateControl, tip, message_empty, message_greater) {
		if (tip != null)
			tip.hide();

		var startDate = startDateControl.get_selectedDate();
		var endDate = endDateControl.get_selectedDate();

		if (startDate == null || endDate == null) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message_empty);
			return false;
		}

		if (startDate > endDate) {
			source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message_greater);
			endDateControl.showPopup();
			return false;
		}
		return true;
	},
	isFileUploadNotEmpty: function (source, radUpload, message_empty) {
		var uploadedFiles = radUpload.getUploadedFiles()

		if (uploadedFiles.length > 0)
			return true;

		source.innerHTML = MedrecruitValidation.ErrorBox.replace("{0}", message_empty);
		return false;
	}
}


