﻿var App = {
	start: function() {
		App.VietTyping.initialize();
		App.SearchOptions.initialize();
		App.Contact.initialize();
	}
};

App.VietTyping = {
	_el: null,
	
	initialize: function() {
		this._el = $E("img.vietTyping", "top-search-box");
		if ($defined(this._el)) {
			this._el.addEvent("click", this._toggleOnOff.bind(this));
			
			var on = Cookie.get("AVIM_on_off");
			if (on && on.toInt())
				this._toggleOnOff();
		}
	},
	
	_toggleOnOff: function() {
		if (this._el.hasClass("on")) {
			AVIMObj.setMethod(-1);
			
			this._el.title = "Bật bộ gõ tiếng Việt";
		}
		else {
			AVIMObj.setMethod(0);
			
			this._el.title = "Tắt bộ gõ tiếng Việt";
		}
		
		this._el.toggleClass("on");
	}
};

App.SearchOptions = {
	initialize: function() {
		if ($("top-topic-search-option").getProperty("checked"))
			this._changeToTopicSearchLayout();
		else
			this._changeToUserSearchLayout();
		
		$("top-topic-search-option").addEvent("click", this._changeToTopicSearchLayout);
		$("top-user-search-option").addEvent("click", this._changeToUserSearchLayout);
	},
	
	_changeToTopicSearchLayout: function() {
		$$("#top-search-box input.query", "#top-search-box select.category").removeClass("user");
	},
	
	_changeToUserSearchLayout: function() {
		$$("#top-search-box input.query", "#top-search-box select.category").addClass("user");
	}
};

App.Contact = {
	_el: null,
	
	initialize: function() {
		if (window.gecko)
			this._fixCSSForFirefox();
		
		this._el = $("contact");
		
		this._el.addEvent("submit", this._submit.bindWithEvent(this));
		$E("input[type=reset]", this._el).addEvent("click", this._reset.bindWithEvent(this));
	},
	
	_fixCSSForFirefox: function() {
		$("message").setStyle("width", $("name").getStyle("width"));
	},
	
	_submit: function(evt) {
		var errors = [];
		
		var name = $("name").getProperty("value").trim();
		if (name.length == 0)
			errors.push("Vui lòng nhập họ & tên.");
		else if (name.length > 50)
			errors.push("Họ & tên không thể vượt quá 50 kí tự.");
		
		var email = $("email-from").getProperty("value").trim();
		if (email.length == 0)
			errors.push("Vui lòng nhập email.");
		else if (email.length > 30 || !this._isValidEmail(email))
			errors.push("Email không hợp lệ.");
		
		var subject = $("subject").getProperty("value").trim();
		if (subject.length == 0)
			errors.push("Vui lòng nhập tiêu đề.");
		else if (subject.length > 100)
			errors.push("Tiêu đề không thể vượt quá 100 kí tự.");
		
		if ($("email-to").getProperty("value") == "0")
			errors.push("Vui lòng chọn nơi nhận.");
		
		if ($("security-code").getProperty("value").length == 0)
			errors.push("Vui lòng nhập mã số xác thực.");
		
		var message = $("message").getProperty("value").trim();
		if (message.length == 0)
			errors.push("Vui lòng nhập nội dung tin.");
		else if (message.length > 1000)
			errors.push("Nội dung tin không thể vượt quá 1000 kí tự.");
		
		if (errors.length > 0)
		{
			alert(errors.join("\n"));
			
			evt.stop();
		}
	},
	
	_reset: function(evt) {
		evt.stop();
		
		$ES("input[type=text]", this._el).each(function(el, idx) {
			el.setProperty("value", "");
		});
		$("email-to").setProperty("value", "0");
		$("message").setProperty("value", "");
	},
	
	_isValidEmail: function(email) {
		return /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(email);
	}
};