// Validating form 
var Valid={
	
	form:null,//the form to validate
	formBtn:null, //submit-button
	whitespace: " \t\n\r", // whitespace characters
	
	init:function ()
	{
		if (!document.getElementById){return;}
		
		//Get the form in question
		Valid.form=document.getElementById("contact-form");
		
		//test if there is a form on teh page (it might not be if the form was recently submitted)
		if(Valid.form)
		{
			//Add an onchange-function on each and every input-field to show errors
			Valid.form.elements["contactName"].onblur=Valid.validName;
			Valid.form.elements["contactEmail"].onblur=Valid.validEmail;
			Valid.form.elements["contactMessage"].onblur=Valid.validMessage;
			
			Valid.form.elements["contactName"].onfocus=function(){Valid.form.elements["contactName"].className="text-input";}
			Valid.form.elements["contactEmail"].onfocus=function(){Valid.form.elements["contactEmail"].className="text-input";}
			Valid.form.elements["contactMessage"].onfocus=function(){Valid.form.elements["contactMessage"].className="";}
		
			Valid.form.onsubmit=function()
			{
				Valid.validateAll();
				return false;
			}	
		}

	},
	
	validateAll:function()
	{
		var n = Valid.validName();
		var e = Valid.validEmail();
		var m = Valid.validMessage();
			
		if(n && e && m)	
		{
			Valid.form.submit();
			return true;
		}
		else
		{
			return false;	
		}
	},
	
	getError:function(id,text)
	{
		var p = document.createElement("p");
		p.className="error-icon";
		p.setAttribute("id", id)
		var pText=document.createTextNode(text);
		p.appendChild(pText);
		
		document.getElementById("user-message").appendChild(p);
		return p;
	},
	
	isWhitespace:function(s)
    {
           var i;

           // Is s empty?
           if (s=="") return true;

           // Search through string's characters one by one
           // until we find a non-whitespace character.
           // When we do, return false; if we don't, return true.

           for (i = 0; i < s.length; i++)
           {
                // Check that current character isn't whitespace.
                var c = s.charAt(i);
                if (Valid.whitespace.indexOf(c) == -1) return false;
           }

           // All characters are whitespace.
           return true;
     },
	
	//validating the name
	validName:function()
	{	
		var pattern = /^[-a-zA-ZÀ-ÿ ]+$/;
		
		//change the background-colour of the input-field to show error
		Valid.form.elements["contactName"].className+=" error";
		
		if(document.getElementById("nameError"))
		{
			//take away the errormessage to make space for a new one
			document.getElementById("user-message").removeChild(document.getElementById("nameError"));
		}
		
		//check for whitespace
		var w = Valid.isWhitespace(Valid.form.elements["contactName"].value);
		if(w==true)
		{	
			//add an error-text
			var s = Valid.getError("nameError","Det är bra om du skriver ditt namn.");
			return false;
		}
		//check for not valid characters
		else if(!Valid.form.elements["contactName"].value.match(pattern))
		{
			//add an error-text
			var s = Valid.getError("nameError", "Ogiltiga tecken i namnet.");
			return false;
		}
		//Test if the name is too long
		else if(Valid.form.elements["contactName"].value.length > 50)
		{
			//add an error-text
			var s = Valid.getError("nameError", "Prova att förkorta ditt namn lite.");
			return false;
		}
		else
		{
			//change class to valid to take away the "error-color"
			Valid.form.elements["contactName"].className="text-input";
			return true;
		}
	},
	
	//validating the email
	validEmail:function()
	{
		var pattern = /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-zA-Z]{2,7}$/;
		
		
		//change the background-colour of the input-field to show error
		Valid.form.elements["contactEmail"].className+=" error";
		
		
		if(document.getElementById("emailError"))
		{
			//take away the errormessaeg to make space for a new one
			document.getElementById("user-message").removeChild(document.getElementById("emailError"));
		}
		
		//check for whitespace
		var w = Valid.isWhitespace(Valid.form.elements["contactEmail"].value);
		if(w==true)
		{
			//add an error-text
			var s = Valid.getError("emailError", "Jo, du måste skriva email-address.");
			return false;
		}
		else if(!Valid.form.elements["contactEmail"].value.match(pattern))
		{
			//add an error-text
			var s = Valid.getError("emailError", "Din email verkar vara ogiltig.");
			return false;
		}
		//Test if the name is too long
		else if(Valid.form.elements["contactEmail"].value.length > 100)
		{
			//add an error-text
			var s = Valid.getError("emailError", "Din email-address är för lång.");
			return false;
		}
		else
		{
			//change class to valid to take away the "error-color"
			Valid.form.elements["contactEmail"].className="text-input";
			return true;
		}
		
	},
	
	//validating the message
	validMessage:function()
	{	
		var pattern = /^[\wÀ-ÿ,.?!-:;\s0-9]+$/;
		
		//change the background-colour of the input-field to show error
		Valid.form.elements["contactMessage"].className="error";
		
		//take away the errormessaeg to make space for a new one
		if(document.getElementById("messageError"))
		{
			//take away the errormessage to make space for a new one
			document.getElementById("user-message").removeChild(document.getElementById("messageError"));
		}
		
		//check for whitespace
		var w = Valid.isWhitespace(Valid.form.elements["contactMessage"].value);
		if(w==true)
		{
			//add an error-text
			var s = Valid.getError("messageError", "Inget meddelande?");
			return false;
		}
		else if(!Valid.form.elements["contactMessage"].value.match(pattern))
		{
			//add an error-text
			var s = Valid.getError("messageError", "Tyvärr, ogiltiga tecken i ditt meddelande.");
			return false;
		}
		//Test if the name is too long
		else if(Valid.form.elements["contactMessage"].value.length > 1000)
		{
			//add an error-text
			var s = Valid.getError("messageError", "För långt meddelande(max 1000 tecken).");
			return false;
		}
		else
		{
			//change class to valid to take away the "error-color"
			Valid.form.elements["contactMessage"].className="";
			return true;
		}
	},

	addLoadEvent:function (handler)
	{
		var oldonload = window.onload;
		if(typeof window.onload != "function")
		{
			window.onload = handler;
		}
		else
		{
			window.onload = function() 
			{
					oldonload();
					handler();
			};
		}
	}
}//end of object

Valid.addLoadEvent(Valid.init);
