var errors = new Array();

function inArray(name, array)
{
	for (var i = 0; i < array.length; i++)
	{
		if(array[i] == name)
			return i;
	}

	return -1;
}

function setError(error_name, error, msg, submit)
{
	errors.push(error_name);

	error.firstChild.nodeValue = msg;
	error.style.visibility = 'visible';

	if(errors.length == 1)
		submit.disabled = true;

	return false;
}

function killError(error_index, error, submit)
{
	error.firstChild.nodeValue = String.fromCharCode(160);
	error.style.visibility = 'hidden';

	errors.splice(error_index, 1);

	if(errors.length == 0)
		submit.disabled = false;

	return true;
}

function kill(field)
{
	field.disabled = true;
	return true;
}

function isSet(field, error, msg, submit)
{
	var error_index = inArray(field.id+"_isSet", errors);

	if(error_index == -1 && error.firstChild.nodeValue == String.fromCharCode(160) && field.value.length == 0)
	{
		setError(field.id+"_isSet", error, 'Veršur aš filla śt', submit);

		return false;

	}

	else if(error_index > -1 && field.value.length > 0)
		return killError(error_index, error, submit);
}

function isLength(field, length, error, msg, submit)
{
	var error_index = inArray(field.id+"_isLength", errors);

	if(error_index == -1 && error.firstChild.nodeValue == String.fromCharCode(160) && field.value.length > 0 && field.value.length != length)
		return setError(field.id+"_isLength", error, msg, submit);

	else if(error_index > -1 && (field.value.length == length || field.value.length == 0))
		return killError(error_index, error, submit);
}

function isNumberic(field, error, msg, submit)
{
	var error_index = inArray(field.id+"_isNumberic", errors);

	var nubericRegex = new RegExp(/^[0-9]+$/);
	var result = nubericRegex.test(field.value);

	if (error_index > -1 && (result == true || field.value.length == 0))
		return killError(error_index, error, submit);

	else if(error_index == -1 && error.firstChild.nodeValue == String.fromCharCode(160) && result == false && field.value.length > 0)
		return setError(field.id+"_isNumberic", error, msg, submit);
}

function isEmail(field, error, msg, submit)
{
	var error_index = inArray(field.id+"_isEmail", errors);

	var regex = new RegExp(/^([a-zA-Z0-9_\.\-]{1,})+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/);
	var result = regex.test(field.value);

	if (error_index > -1 && (result == true || field.value.length == 0))
		return killError(error_index, error, submit);

	else if(error_index < 0 && error.firstChild.nodeValue == String.fromCharCode(160) && result == false && field.value.length > 0)
		return setError(field.id+"_isEmail", error, msg, submit);
}

function isUrl(field, error, msg, submit)
{
	var error_index = inArray(field.id+"_isUrl", errors);

	var urlRegex = new RegExp(/^((http|https|ftp):\/\/)+(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)$/);
	var result = urlRegex.test(field.value);

	if (error_index > -1 && (result == true || field.value.length == 0))
		return killError(error_index, error, submit);

	else if(error_index == -1 && error.firstChild.nodeValue == String.fromCharCode(160) && result == false && field.value.length > 0)
		return setError(field.id+"_isUrl", error, msg, submit);
}