/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#dodajMailForm");
	var name = $("#name");
	var nameInfo = $("#nameInfo");
	/*var contactPerson = $("#contactPerson");
	var contactPersonInfo = $("#contactPersonInfo");
	var phone = $("#phone");
	var phoneInfo = $("#phoneInfo");*/
	var post = $("#post");
	var postInfo = $("#postInfo");
	var location = $("#location");
	var locationInfo = $("#locationInfo");
	var address = $("#address");
	var addressInfo = $("#addressInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	//var message = $("#message");
	
	//On blur
	name.blur(validateName);
	post.blur(validatePost);
	location.blur(validateLocation);
	address.blur(validateAddress);
	email.blur(validateEmail);
	
	//On Submitting
	form.submit(function(){
		if(validateEmail() & (document.getElementById("address") == null ? true : validateAddress()) & validateName() & validateLocation() & validatePost())
			return true;
		else
			return false;
	});
	
	/*$('.bordered').mouseenter(function(){
      $(this).addClass("highlight");
    }).mouseleave(function(){
      $(this).removeClass("highlight");
    });*/
	
	function validateName(){
		//if it's NOT valid
		if(name.val().length < 3){
			//nameInfo.text("Prosimo vpišite ime!");
			nameInfo.text("");
			nameInfo.removeClass("correct");
			nameInfo.addClass("error");
			
			document.getElementById('imgName').className = ("error");
			return false;
		} else {
			nameInfo.text("");
			nameInfo.removeClass("error");
			nameInfo.addClass("correct");
			
			document.getElementById('imgName').className = ("accept");
			return true;
		}
	}
	
	function validateEmail(){
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		if(filter.test(a)){
			//email.removeClass("error");
			//email.addClass("correct");
			emailInfo.text("");
			emailInfo.removeClass("error");
			emailInfo.removeClass("correct");
			
			document.getElementById('imgEmail').className = ("accept");
			return true;
		} else {
			//email.removeClass("correct");
			//email.addClass("error");
			//emailInfo.text("Oblika naslova ni veljavna!");
			emailInfo.text("");
			emailInfo.removeClass("correct");
			emailInfo.addClass("error");
			
			document.getElementById('imgEmail').className = ("error");
			return false;
		}
	}
	
	function validatePost() {
		if(post.val().length != 4){
			//nameInfo.text("Prosimo vpišite ime!");
			postInfo.text("");
			postInfo.removeClass("correct");
			postInfo.addClass("error");
			
			document.getElementById('imgPost').className = ("error");
			return false;
		} else {
			postInfo.text("");
			postInfo.removeClass("error");
			postInfo.addClass("correct");
			
			document.getElementById('imgPost').className = ("accept");
			return true;
		}
	}
	
	function validateLocation() {
		if (location.val().length < 2) {
			locationInfo.text("");
			locationInfo.removeClass("correct");
			locationInfo.addClass("error");
			
			document.getElementById('imgLocation').className = ("error");
			return false;
		} else {
			locationInfo.text("");
			locationInfo.removeClass("error");
			locationInfo.addClass("correct");
			
			document.getElementById('imgLocation').className = ("accept");
			return true;
		}
	}
	
	function validateAddress() {
		if(address.val().length < 3){
			//nameInfo.text("Prosimo vpišite ime!");
			addressInfo.text("");
			addressInfo.removeClass("correct");
			addressInfo.addClass("error");
			
			document.getElementById('imgAddress').className = ("error");
			return false;
		} else {
			addressInfo.text("");
			addressInfo.removeClass("error");
			addressInfo.addClass("correct");
			
			document.getElementById('imgAddress').className = ("accept");
			return true;
		}
	}
});
