﻿var validatorResetErrorMessage = function(sender) {
	if ($(this).val() == sender.data) {
		$(this).val('').css('color', '').removeClass('blur');
	}
}

function validatorRequiredField(sender, event) {
	var control = $('#' + sender.controltovalidate);
	if ($.trim(control.val()) == '' || control.val() == control.attr('hint') || control.val() == sender.errormessage) {
		control.val(sender.errormessage).css('color', '#EE5F00');
		control.unbind('focus', validatorResetErrorMessage)
		control.bind('focus', sender.errormessage, validatorResetErrorMessage);
		event.IsValid = false;
	}
}

function validatorEmail(sender, event) {
	var control = $('#' + sender.controltovalidate);
	if (!validatorIsValidEmail(control.val(), 0) || control.val() == control.attr('hint') || control.val() == sender.errormessage) {
		control.val(sender.errormessage).css('color', '#EE5F00');
		control.unbind('focus', validatorResetErrorMessage)
		control.bind('focus', sender.errormessage, validatorResetErrorMessage);
		event.IsValid = false;
	}
}

function validatorTelephone(sender, event) {
	var control = $('#' + sender.controltovalidate);
	var tel = control.val();
	if (!validatorIsValidTelephone(tel) || tel == control.attr('hint') || tel == sender.errormessage) {
		if (tel != sender.errormessage) ShowTelephoneErrorDialog(tel);
		control.val(sender.errormessage).css('color', '#EE5F00');
		control.unbind('focus', validatorResetErrorMessage)
		control.bind('focus', sender.errormessage, validatorResetErrorMessage);
		event.IsValid = false;
	}
}

function validatorIsValidTelephone(phone) {
	return (/^(\d{3} \d{4} \d{4}|\+\d{1,3} \d{3} \d{3} \d{4})$/g).test(phone);
}
function validatorIsValidEmail(email, strict) {
	if (!strict) email = email.replace(/^\s+|\s+$/g, '');
	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

var validatorResetSelectTag = function(sender) {
	$(this).css('color', '').removeClass('blur');
}

function validatorRequiredSelectField(sender, event) {
	var control = $('#' + sender.controltovalidate);
	if ($.trim(control.val()) == '' || $.trim(control.val()) == '-- Choose event --' || control.val() == control.attr('hint') || control.val() == sender.errormessage) {
		control.css('color', '#EE5F00');
		control.unbind('change', validatorResetSelectTag)
		control.bind('change', null, validatorResetSelectTag);
		event.IsValid = false;
	}
}

// Shows dialog window with error message for telephone number
function ShowTelephoneErrorDialog(tel) {
	if ($("#dialog1").length == 0) return;
	var correctFormat;
	var dialogTitle;
	if (tel.substring(0, 1) == "+") {
		correctFormat = '+NNN NNN NNN NNNN';
		dialogTitle = "Validate international phone number format";
	}
	else {
		correctFormat = 'NNN NNNN NNNN';
		dialogTitle = "Validate local phone number format";
	}

	$("#dialog1").html("");
	$("#dialog1").append('<p style ="padding-left:10px;padding-right:10px;padding-top:5px;padding-bottom:5px; line-height: 2">\
                                      <span id="msgspan">The number entered is in the incorrect format.<br/>Please re-enter using the following format: ' +
                                      correctFormat + '</span></p><input type="button" value="Close" class="ui-Btn" onclick=" $(\'#dialog1\').dialog(\'close\');" style="float: right; margin-bottom: 15px; margin-right: 10px;" />');
	$("#dialog1").dialog({
		title: dialogTitle,
		modal: true,
		width: 410,
		resizable: false,
		draggable: false,
		position: "center",
		dialogClass: 'msgClass'
	});
}

