﻿Websajt.Form.Validation = null;



Websajt.Form.GlobalActions = function() {

	/*
	---------------------------------------------
	Prevents auto submit with "enter".
	---------------------------------------------
	*/
	$("input").keypress(function(e) {
		try {
			if (e.keyCode == 13) {
				e.returnValue = false;
				e.cancel = true;
				e.preventDefault();
				e.stopPropagation();
			}
		}
		catch (exp) { }
	});

	$(".validator-message-top").each(function() {
		var strMessage = $(this).html();
		$(this).hide();
		var strID = $('div', this).attr("id");
		$('div', this).change(function() { alert("CHANGE") })
		var ErrorBubble = new Websajt.Utils.BubbleDotNet('Något var inte korrekt ifyllt', strMessage, strID, -100, 80, 1000, "");
		ErrorBubble.Init();
	});

	/*
	---------------------------------------------
	Watermark on all input forms that have rel
	"watermark"
	---------------------------------------------
	*/
	$('input.watermark').focus(function() {
		if ($(this).val() == this.defaultValue)
			$(this).val('');
		else if ($(this).val() == '')
			$(this).val(this.defaultValue);
	});

	$('input.watermark').blur(function() {
		if ($(this).val() == this.defaultValue)
			$(this).val('');
		else if ($(this).val() == '')
			$(this).val(this.defaultValue);
	});

	$('input.submit-button').each(function() {
		$(this).hide();
		var strText = $(this).attr("value");
		var strID = $(this).attr("id");

		$(this).after('<a href="javascript:Websajt.Form.DoSubmit(\'' + strID + '\')" id="lnk' + strID + '" class="submit-button"><span>' + strText + '</span></a>');

		$('#lnk' + strID).hover(function() {
			$(this).addClass("submit-button-hover");
			$(this + " span").addClass("submit-button-hover");
		},
		function() {
			$(this).removeClass("submit-button-hover");
			$(this + " span").removeClass("submit-button-hover");
		})
	});

	/*
	---------------------------------------------
	Adds a class name on all types of input boxes
	when the user have that field focused.
	---------------------------------------------
	*/
	$('input[type=text],input[type=password],textarea').focus(function() {
		$(this).addClass("focus");
	});

	$('input[type=text],input[type=password],textarea').blur(function() {
		$(this).removeClass("focus");
	});

}

Websajt.Form.DoSubmit = function(strID) {
	$("#" + strID).click();
}


Websajt.Form.ValidateValue = function(strValue, strMatchPattern) {
	var objRegExp = new RegExp(strMatchPattern);
	return objRegExp.test(strValue);
}
