var $bFormValidationTooltipVisible = false;
$(document).ready(function()
{
	// Form validation
	$("form").each( function() // Form Validation
	{
		$(this).submit(function () // On Submit
		{
			var bIsValid = true;
			var bIsFiledValid;
			$("input, select, textarea", this).each( function()
			{
				if($(this).parent().children(".validate").html() != null)
				{
					bIsFieldValid = validateField($(this));
					bIsValid = !bIsValid ? false : bIsFieldValid;
				}
			});
			return bIsValid;
		});
		$("input, select, textarea").each( function() // On the fly
		{
			if($(this).parent().children(".validate").html() != null)
			{
				$(this).blur( function(){validateField($(this));});
    		}
		});
	});
});

function validateField(a_oField)
{
	var bIsValid = true;
	var sCriterias = $(a_oField).parent().children(".validate").children(".form-validator-criterias").text();
	var sValue = trim($(a_oField).val());

	if(trim(sValue) != '')
	{
		if(sCriterias.indexOf("isDate") != -1)
		{
			bIsValue = validateIsDate(sValue);
		}

		if(sCriterias.indexOf("isTimestamp") != -1)
		{
			bIsValue = validateIsTimestamp(sValue);
		}

		if(sCriterias.indexOf("isEmail") != -1 && sValue != '')
		{
			bIsValid = validateIsEmail(sValue);
		}

		if(sCriterias.indexOf("isInt") != -1)
		{
			bIsValid = validateIsInt(sValue);
		}

		if(sCriterias.indexOf("isFloat") != -1)
		{
			bIsValid = validateIsFloat(sValue);
		}

		if(sCriterias.indexOf("isSafeString") != -1)
		{
			bIsValid = validateIsSafeString(sValue);
		}

		if(sCriterias.indexOf("isURL") != -1)
		{
			bIsValid = validateIsUrl(sValue);
		}
		
		if(sCriterias.indexOf("isPhone") != -1)
		{
			bIsValid = validateIsPhone(sValue);
		}
	}

	if(sCriterias.indexOf("notEmpty") != -1)
	{
		bIsValid = bIsValid ? validateNotEmpty(sValue) : bIsValid;
	}

	if (!bIsValid)
	{
		$(a_oField).parent().children(".validate").addClass("form-validation-error");
		$(a_oField).addClass("form-validation-error");
		if($(a_oField).data('onflyadded') === undefined || $(a_oField).data('onflyadded') == false)
		{
			$('.errorMessage').show();
			$('.errorMessage').append('<div id="err_' + $(a_oField).attr('id') + '>' + $(a_oField).parent().children(".validate").children(".form-validator-error-message").text()+'</div>');
			$(a_oField).data('onflyadded', true);
		}
	}
	else
	{
		$(a_oField).parent().children(".validate").removeClass("form-validation-error");
		$('.errorMessage').children('#err_' + $(a_oField).attr('id')).remove();
		$(a_oField).removeClass("form-validation-error");
		$(a_oField).data('onflyadded', false);
		if($('.errorMessage').text() == '')
		{
			$('.errorMessage').hide();
		}
	}
	return bIsValid;
}

function validateIsDate(value)
{
	if(!(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsTimestamp(value)
{
	if(!(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsEmail(value)
{
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsInt(value)
{
	if(!(/^[0-9]*$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsFloat(value)
{
	if(!(/^[0-9]*\.?[0-9]+$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsSafeString(value)
{
	if(!(/^[A-Za-z#0-9@&._ ()`*+,/:^~-]+$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsUrl(value)
{
	if(!(/^http:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_\/\.#?&=]+$/.test(value)))
	{
		return false;
	}
	return true;
}

function validateIsPhone(value){
	if(value.indexOf("+") !== 0 && value.indexOf("00") !== 0){
		return false;
	}
	value = value.replace('+', '');
	return (validateIsInt(value) && value.length >= 9);
}

function validateNotEmpty(value)
{
	if(value.length == 0 || value == 'none' || value == '-1' || value == 'null')
	{
		return false;
	}
	return true;
}
