
(function() {
	window.reloadPub = function() {
		jQuery(".pub iframe").parent(".pub").each(function() {
			var pub = $(this);
			pub.html(pub.html());
		});
	}
})();

(function($) {
	$.pesquisaTipo = function(form, urlMunicipios) {
		var p = $("select[name=sp]", form);
		var m = $("select[name=sm]", form);
		var sk = $("input[name=sk]", form);
		
		var optionAll = $("option:eq(1)", m).remove();
		
		$(form).submit(function() {
			$("span:hidden input[name=sk][checked]", form).attr("checked", false);
			
			var p_val = p.val();
			var m_val = m.val();
			var sk_val = sk.filter(":checked").val();
			
			var check = $("#search_options_error");
			
			if (!p_val || !sk_val) {
				check.show();
			} else {
				check.hide();
				
				var url = this.action;
				var params = [ sk_val, p_val, m_val ];
				
				$.each(params, function(k, v) {
					if (v && v != "") url += "/" + encodeURIComponent(v);
				});
				
				window.location = url;
			}
			
			return false;
		});
		
		p.bind("change", function() {
			var value = p.val();
			
			var invis = $("span:not(.searchK" + value + ")", form);
			var vis = $("span.searchK" + value, form);
			
			var label = $("h1.pqesq2", form);
			var esq = $("p.pqesq", form);
			var dta = $("p.pqdta", form);
			
			invis.hide();
			if (vis.size() == 0) label.hide();
			
			if (dta.size() > 0) {
				var half = Math.ceil(vis.size() / 2);
				var items = $("span", form);
				var idx = items.index(vis.eq(half));
				items.slice(0, idx).appendTo(esq);
				items.slice(idx).appendTo(dta);
			}
	
			vis.show();
			if (vis.size() > 0) label.show();
			
			$("option:gt(0)", m).remove();
			
			if (!value || value == "") {
				return;
			}
			
			var data = {};
			data["p"] = value;
			jQuery.ajax({
				type: "GET",
				url: urlMunicipios,
				data: { p: value },
				dataType: "json",
				error: function() {},
				success: function(municipios) {
					if (p.val() == value) {
						$("option:gt(0)", m).remove();
						if (municipios.length > 1) {
							m.append(optionAll);
						}
						for (var i = 0; i < municipios.length; ++i) {
							m.append(new Option(municipios[i][1], municipios[i][0]));
						}
					}
				}
			});	
		});
	};
})(jQuery);

(function($) {
	$.pesquisaGeral = function(form) {
		form = $(form);
		var text = $("input[name=st]", form);
		var url = form.attr("action");
		
		form.submit(function() {
			var st = $.trim(text.val());
			if (st != "") {
				window.location = url + "/" + encodeURIComponent(st);
			}
			return false;
		});
	};
})(jQuery);

jQuery(function($) {
	var form = $("#emailForm form");
	var contactNameField = $("input[name=contactName]", form);
	var contactEmailField = $("input[name=contactEmail]", form);
	var commentField = $("textarea[name=comment]", form);
	var commentCounter = $("#commentCounter", form);
	
	function showError(field, errored) {
		var error = field.siblings(".erro");
		
		if (errored) {
			error.show();
			field.attr("class", "campo-pq-verm");
		} else {
			error.hide();
			field.attr("class", "campo-pq");
		}
	}
	
	contactNameField.change(function() {
		showError(contactNameField,  $.trim(this.value) == "");
	});
	contactEmailField.change(function() {
		var contactEmail = $.trim(this.value);
		showError(contactEmailField, contactEmail == "" || !checkEmail(contactEmail));
	});
	
	commentField.change(function() {
		var text = $.trim(this.value);
		var charsLeft = 500 - text.length;
		var error = commentField.siblings(".erro");
		
		if (charsLeft < 0) {
			commentCounter.hide();
			error.show();
		} else {
			error.hide();
			var counterText = commentCounter.text();
			var m = counterText.match(/^(.*)\s\d+\s(.*)$/);
			if (m && m.length == 3) {
				counterText = m[1] + " " + charsLeft + " " + m[2];
			}
			commentCounter.text(counterText).show();
		}
	});
	
	commentField.keyup(function() { commentField.change(); });
	
	form.submit(function() {
		contactNameField.change();
		contactEmailField.change();
		commentField.change();
		
		return $(".erro:visible", form).size() == 0;
	});
	
	commentField.change();
	
	//setTimeout(function() { $('#emailFormSent,#emailFormSentError').fadeOut(); }, 3000);
});


/**
 * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
 * http://javascript.internet.com
 */
function checkEmail(emailStr) {
	if (emailStr.length == 0) {
		return true;
	}
	// TLD checking turned off by default
	var checkTLD=0;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}
	if (user.match(userPat)==null) {
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}
	if (len<2) {
		return false;
	}
	return true;
}