﻿/********* region Validation *****************/

function Validator()
{
	var ObjectID;
	var Text;
	var Type;
	var Regex;
	var Group;
	var Disabled;
};

var _validators = [];
jQuery.fn.requestValidation = function(text, type, regex, group)
{
	var val = new Validator();
	val.ObjectID = this.get(0).id;
	val.Text = text;
	val.Type = type;
	val.Regex = regex;
	val.Group = group;
	if (group)
		val.Disabled = true;
	_validators.push(val);

	this.get(0).onblur = function()
	{
		var val = GetValidator(this.id);
		if (val)
		{
			if (Validate($("#" + this.id), val) == '')
				this.style.borderColor = '';
		}
	}
}

function EnableValidationGroup(group)
{
	for (var i = 0; i < _validators.length; i++)
		if (_validators[i].Group == group)
		_validators[i].Disabled = false;
}

function DisableValidationGroup(group)
{
	for (var i = 0; i < _validators.length; i++)
		if (_validators[i].Group == group)
		_validators[i].Disabled = true;
}

function GetValidator(objID)
{
	for (var i = 0; i < _validators.length; i++)
	{
		if (objID == _validators[i].ObjectID)
			return _validators[i];
	}

	return null;
}

function Validate(obj, val)
{
	if (val.Disabled)
		return '';

	var message = '';

	if (val.Type == "RF")
	{
		if (obj.attr("value") == '' || (obj.attr("value") == '0' && obj.get(0).tagName.toLowerCase() == 'select'))
		{
			message += val.Text + "\n";
			obj.css("border-color", "#FF0000");
		}
	}
	else
		if (val.Type == "RE")
	{
		if (!obj.attr("value").match(val.Regex))
		{
			message += val.Text + "\n";
			obj.css("border-color", "#FF0000");
		}
	}
	else
		if (val.Type == "RC")
	{
		var msg = '';
		if ((obj.attr("value") == '' && obj.get(0).tagName.toLowerCase() != 'select') ||
					(obj.attr("value") == '0' && obj.get(0).tagName.toLowerCase() == 'select') ||
					(obj.get(0).tagName.toLowerCase() == 'select' && obj.attr('multiple') && obj.children().length == 0))
		{
			var otherobj = $("#" + val.Regex);
			if (otherobj)
			{
				if (otherobj.attr("value") == '' || (otherobj.attr("value") == '0' && otherobj.get(0).tagName.toLowerCase() == 'select'))
				{
					msg += val.Text + "\n";
					obj.css("border-color", "#FF0000");
					otherobj.css("border-color", "#FF0000");
				}
			}
			else
			{
				msg += val.Text + "\n";
				obj.css("border-color", "#FF0000");
			}
		}

		if (msg == '')
		{
			obj.css("border-color", "");
			var otherobj = $("#" + val.Regex);
			if (otherobj)
				otherobj.css("border-color", "");
		}
		else
			message += msg;
	}

	return message;
}

jQuery.fn.validate = function()
{
	this.submit(
		function()
		{
			var message = '';
			var fobj = null;

			for (var i = 0; i < _validators.length; i++)
			{
				var obj = $("#" + _validators[i].ObjectID);
				if (!obj)
					continue;

				var msg = Validate(obj, _validators[i]);
				if (msg != '' && !fobj)
					fobj = obj;
				message += msg;
			}

			if (message != '')
			{
				alert(message);
				fobj.focus();
				return false;
			}

			return true;
		}
	);
}

/********** endregion ***************/

function RefreshImage(img, url)
{
	$.ajax({
		url: url,
		type: 'POST',
		data: {},
		success: function(data, textStatus)
		{
			document.getElementById(img).src = data;
		}
	});
}

function ViewOnGoogleMaps(url)
{
	window.open(url, '', 'width=640,height=480');
}
