var Application = new Class({
	
	credential: {},
	ajaxScript: 'form.inc.php',
	
	initialize: function(){
		var _class = this;
		$$('.calendar').each(function(el){new vlaDatePicker(el.id,{format: 'm/d/y', prefillDate: false});});
		$('submitApplication').addEvent("click", this.submitForm.bind(_class));
		$('resetApplication').addEvent("click", function(){$$('.invalid').removeClass("invalid");});
		$$('.wednesday').addEvent("click",function(){$$('.altdate').removeClass("invalid");});
		$('other_school').addEvent("focus",function(){if ($('school_id').value == "other") $('school_id').removeClass("invalid");});
		$('school_id').addEvent("change", _class.refreshReferrals.bind(_class));
		//$('school_id').addEvent("change",function(){if (this.value != "other" && this.value != "") $('other_school').removeClass("invalid");});
		$('interview_day').addEvent("change", function(){
			switch (this.value) {
				case "Monday": case "Friday":
					$('timeslots1').addClass("hide");
					$$('#timeslots1 .wednesday').removeProperty("checked");
					$('timeslots2').removeClass("hide");
					break;
				case "Wednesday":
					$('timeslots2').addClass("hide");
					$$('#timeslots2 .wednesday').removeProperty("checked");
					$('timeslots1').removeClass("hide");
					break;
				case "":
					$$('#timeslots1, #timeslots2').addClass("hide");
					break;
			}
		});
		$$('.withLabel').addEvent("focus",function(){
			if (this.hasClass("untouched")) {
				this.value = "";
				this.removeClass("untouched");
			}
		});
		$$('.withLabel').addEvent("blur",function(){
			if (this.value == "") {
				this.value = this.alt;
				if (!this.hasClass("untouched"))
					this.addClass("untouched");
			}
		});
		$$('.heardabout').addEvent("click", function(){_class.getChildList(this);});
		//$$('.heardabout').addEvent("click",function(){$$('.heardabout').removeProperty("checked"); this.set("checked",true);});
	},
	
	refreshReferrals: function() {
		var _class = this;
		var postObj = {task: "getParentReferrals", schoolID: $('school_id').value};
		_class.submitAjaxRequest(postObj, _class.cb_refreshReferrals.bind(_class));
	},
	
	getChildList: function(el) {
		var _class = this;
		if (el.hasClass("parent")) {
			var postObj = {task: "getChildReferrals", parentID: el.value};
			_class.submitAjaxRequest(postObj, _class.cb_getChildList.bind(_class));
			$('childRow').removeClass("hide");
		} else
			$('childRow').addClass("hide");
	},
	
	submitForm: function(){
		var _class = this;
		var data = $H({});
		var checkboxGroups = ["paying","misc","wednesday"];
		$$('#applicationForm input[type="text"], #applicationForm select').each(function(el){data.set(el.id, el.value);});
		$$('#applicationForm .heardabout').each(function(el){ if (el.checked) data.set("referral", el.value); });
		checkboxGroups.each(function(group){
			var values = [];
			$$('.'+group).each(function(box){
				if (box.checked)
					values.push(box.value);
			});
			data.set(group, values);
		});
		$$('.untouched').each(function(el){data.set(el.id, "");});
		$('ajax-loading').removeClass("hide");
		data.task = "submitApplication";
		_class.submitAjaxRequest(data, _class.cb_submitForm);
		return false;
	},
	
	submitAjaxRequest: function(data, callback) {
		var _class = this;
		new Request({url: _class.ajaxScript, method: 'post', data: data, onComplete: callback}).send();
	},
	
	cb_refreshReferrals: function(html) {
		var _class = this;
		$('referralCell').set("html", html);
		$$('.heardabout').addEvent("click", function(){_class.getChildList(this);});
		$('childRow').addClass("hide");
	},
	
	cb_getChildList: function(data) {
		var _class = this;
		var obj = JSON.decode(data);
		$('parentName').set("html", obj.parent);
		$('childList').getChildren().dispose();
		$('childList').grab(new Element("option", {value: "", html: "- Please Select -"}));
		$H(obj.list).each(function(name, id){ $('childList').grab(new Element("option", {value: id, html: name})); });
	},
	
	cb_submitForm: function(data){
		var obj;
		var _class = this;
		$('ajax-loading').addClass("hide");
		if (!(obj = JSON.decode(data))) {
			alert("There was a problem communicating with the server.  Please use the feedback form and let us know what happened.");
			return false;
		}
		if (obj.success == "false") {
			obj.invalid.each(function(field){
				$(field).addClass("invalid");
				var eventType = ($(field).get("tag") == "input") ? "click" : "change";
				$(field).addEvent(eventType, function(){this.removeClass("invalid");});
			});
			alert(obj.message);
		} else {
			var success = new Element("div", {"class": "fieldSet success", text: obj.message});
			$('application-body').getChildren().dispose();
			$('application-body').grab(success);
		}
	}
	
});
window.addEvent("domready", function(){var app = new Application();});