(function(){
	dojo.require("dojo.fx");
	dojo.require("dojox.fx.easing");
	
	function validate(evt) {
		//Clear previous validate messages

		//Form starts out valid. Check all inputs to make sure they are filled in
		//If they are not filled in, the isValid boolean is decremented.
		
		var isValid = 1;
		
		if(!validateEmail(email)) {
			isValid--;
		}
		
		if(!theName || !validateInput(theName)) {
			isValid--;
		}

		if(theCompany && !validateInput(theCompany)) {
			isValid--;
		}		
		
		if(isPromoRequest) {
			if(!validateInput(trainingStudentsNode)) {
				isValid--;
			}
		}
		else if(!userMessage || !validateInput(userMessage)){
			isValid--;
		}
		
		if(isValid != 1) { isValid = 0 }
		//If it's invalid stop the event and print out the form errors

		console.log(isValid);
		dojo.stopEvent(evt); // prevent normal action should it bubble
		if(!isValid) {
			return false;
		}
		
		// everything worked: move on:
		submitForm();
		return false;
	}
	
	
	function validateInput(node) {
		//console.log(typeof(node.value));
		if(!node){
			return false;
		}
		if(!node.value) {
			setInputWarning(node);
			return false;
		} else {
			removeInputWarning(node);
			return true;
		}
	}
	
	function validateEmail() {
		var emailFilter=/^.+@.+\..{2,5}$/; 
		//console.error(email.value);
		if(!emailFilter.test(email.value)) {
			setInputWarning(email);
			return false;
		} else {
			removeInputWarning(email);
			return true;
		}
		if(!emailFilter.test(trainingEmail.value)) {
			setInputWarning(trainingEmail);
			return false;
		} else {
			removeInputWarning(trainingEmail);
			return true;
		}		
	}
	
	function validateEmailTraining() {
		var emailFilter=/^.+@.+\..{2,3}$/; 
		//console.error(email.value);
		if(!emailFilter.test(trainingEmail.value)) {
			setInputWarning(trainingEmail);
			return false;
		} else {
			removeInputWarning(trainingEmail);
			return true;
		}		
	}
	
	function setInputWarning(node) {
		dojo.addClass(node, "inputError");
		//displayError(dojo.query("img", node.parentNode)[0]);
	}
	

	function removeInputWarning(node) {
		dojo.removeClass(node, "inputError");
		//removeError(dojo.query("img", node.parentNode)[0]);
	}

	
	function submitForm(){
		// summary: We've passed validation, but haven't heard back from server
		// pseudo:
		// block form, submit data, get callback, fail-> unblock, succed-> hide form, show responsemessage
		var s = dojo.coords("contactForm");
		var ov = dojo.byId("formOverlay") || dojo.doc.createElement('div');
		if(!ov.id){
			ov.id ="formOverlay";
			dojo.byId("contactForm").appendChild(ov);
		}
		dojo.style(ov,"height",s.h+"px");
		dojo.style(ov,"width",s.w+"px");
		dojo.style(ov,"opacity","0");
		dojo.style(ov,"top",s.t+"px");
		dojo.style(ov,"left",s.l+"px");
		dojo._fade({
			node:ov, end:0.65, duration:300,
			onEnd:function(){
				_submitForm();
			}
		}).play(5);
		return false;
	}
	
	function _submitForm(){
		dojo.xhrPost({
			form:"contactForm",
			url:"/contact/submitContactForm.php",
			handleAs:"json",
			load:function(data){
				if(data.status == "SUCCESS"){
					var n = dojo.byId("theContactForm");
					var anim = dojo.fadeOut({
						node:n,
						duration:275,
						onEnd:function(){
							_replaceContent(n,data);
						}
					});
					anim.play(3);
				}
				// FIXME: check for data.status == "FAIL"? there will be
				//	an array dojo.forEach(data.errors, ... ) not sure how
				// to best present
			},
			error:function(){
				// FIXME: no error messages being shown to user, but this error
				// is only 404/500.  "real failures" 
				_resetOverlay().play();
			}
		});
	}

	function _replaceContent(node,data){
		var inquiryNode = dojo.byId("inquiry");
		if (inquiryNode) {
			var inquiryType = inquiryNode.value;
		} else {
			var inquiryType = "";
		}
		node.innerHTML = "<h1 class='heading'></h1><div class='messageArea'></div>";
		dojo.query(".heading",node)[0].innerHTML = data.message + (data.testMode ? data.debug : '');
		dojo.query(".messageArea",node)[0].innerHTML = data.preview;
		dojo.fadeIn({ node: node, duration:375 }).play(10);
		//pageTracker.trackPageView("/contact/" + inquiryType);
	}
	
	function _resetOverlay(){
		return dojo.fadeOut({ node:"formOverlay", duration:275,
			onEnd: function(){
				dojo.style("formOverlay","width","1px");
				dojo.style("formOverlay","height","1px");
			}
		});
	}

	function init() {
		theForm = dojo.byId("contactForm");
		if(dojo.byId("nameNode")) {
			email = dojo.byId("emailNode");
			//trainingEmail = dojo.byId("trainingEmailNode");
			errorMessageContainer = dojo.byId("errorMessageNode");
			theName = dojo.byId("nameNode");
			//trainingName = dojo.byId("trainingNameNode");
			theCompany = dojo.byId("trainingCompanyNode");
			userMessage = dojo.byId("theMessage");
			isPromoRequest = dojo.byId("isPromoRequest") && dojo.byId("isPromoRequest").value == 1;
			trainingStudentsNode = dojo.byId("trainingStudentsNode");
			
			//trainingMessage = dojo.byId("trainingMessageNode");
			dojo.connect(theForm, "onsubmit", validate);
			if(theName){
				dojo.connect(theName, "onblur", function(){ validateInput(theName); });
			}
			/*
			if(trainingName){
				dojo.connect(trainingName, "onblur", function(){ validateInput(trainingName); });
			}
			*/
			if(theCompany){
				dojo.connect(theCompany, "onblur", function(){ validateInput(theCompany); });
			}
			if(userMessage){
				dojo.connect(userMessage, "onblur", function(){ validateInput(userMessage); });
			}
			/*
			if(trainingMessage){
				dojo.connect(trainingMessage, "onblur", function(){ validateInput(trainingMessage); });
			}
			*/
			dojo.connect(email, "onblur", validateEmail);
			/*
			if(trainingEmail){
				dojo.connect(trainingEmail, "onblur", validateEmailTraining);
			}
			*/
		}
	}
	dojo.addOnLoad(init);
})();

