﻿$(document).ready(function(){
	$("input").not(".Button").focus(function(){ this.className += " Focus"; });
	$("input").not(".Button").blur(blurInput);
	$("textarea").focus(function(){ this.className += " Focus"; });
	$("textarea").blur(blurInput);
});

function blurInput() {
	var oldClass = " Focus";
	if (this.nodeName.toLowerCase() == "textarea") oldClass = "Focus";
	this.className = this.className.replace(oldClass, "");
	
	var type = "none";
	var errorMessage;
	
	switch (this.id) {
		case "NameTextBox": 
			type = "string";
			errorMessage = "Du måste ange namn";
			break;
		case "EmailTextBox": 
			type = "email";
			errorMessage = "Du måste ange en korrekt adress";
			break;
		case "PhoneTextBox":
		case "MessageTextBox": break;
	}

	if (type != "none") {
		if (isValid(type, this.value)) {
			this.className = this.className.replace(" Highlight", "");
			$("p", this.parentNode).remove();
		} else{
			if (this.className.indexOf("Highlight") == -1)
				this.className += " Highlight";
			
			$("p", this.parentNode).remove();
			
			var pObj = document.createElement("p");
			var pText = document.createTextNode(errorMessage);
			pObj.appendChild(pText);
			this.parentNode.appendChild(pObj);
		}
	}
}

function isValid(type, input) {
	var reg;
	switch (type) {
		case "string":
			reg = /^[a-zA-ZåäöÅÄÖ]+/;
			break;
		case "email":
			reg = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			break;
		default:
			return true;
	}
	return reg.test(input);
}
