$(document).ready(function() {
	$().ajaxStart(function() { $('#div_loading').show(); });
	$().ajaxStop(function() { $('#div_loading').hide(); });
}); 

f_ajax = function(p_div_id, p_url, p_params, p_method) {
	var l_div_id = p_div_id, l_url = p_url, l_params = p_params, l_method = 'get';
	
	if (l_params != '' && l_params != null) { l_params += '&' + getRndParam(); } else { l_method = 'post'; l_params += getRndParam(); }

	$.ajax(
		{contentType: 'text/html;charset=ISO-8859-1',
		 type:l_method,
		 url:l_url,
		 data:unescape(l_params),
		 dataType:'html',
		 success:function(html) {
			f_dln('success (' + l_method + '):' + l_div_id + '=' + l_url + '?' + l_params);

			if (l_div_id != null && l_div_id != "" && $('#' + l_div_id) != null) {
				$('#' + l_div_id).html(html);
			}
			
		 	html.evalScripts;
		 },
		 error:function() {
			f_dln('error (' + l_method + '):' + l_div_id + '=' + l_url + '?' + l_params);

			if (l_div_id != null && l_div_id != "" && $('#' + l_div_id) != null) {
				$('#' + l_div_id).html('Problemas com o servidor (F).<br/><br/><a href="javascript:popupClose();">Fechar</a>');
			}
		 }}
	);
}


var g_debug_count = 0;
f_dln = function(p_text) {
	if ($('#div_debug').show) {
		g_debug_count += 1;
		$('#div_debug').html($('#div_debug').html() + "" + g_debug_count + '. ' + p_text + '<br/>');
	}
}

f_wl = function(p_url, p_params) {
	if (p_params == undefined) {
		window.location.href = p_url; // + '?' + getRndParam();
	} else {
		window.location.href = p_url + '?' + p_params + '&' + getRndParam();
	}
}

$$ = function(p_object_id) {
	return document.getElementById(p_object_id);
}

f_changeOpacity = function(p_object, p_opacity) {
	p_object.style.opacity = (p_opacity / 100);
	p_object.style.MozOpacity = (p_opacity / 100);
	p_object.style.KhtmlOpacity = (p_opacity / 100);
	p_object.style.filter = "alpha(opacity=" + p_opacity + ")";
}

f_windowSize = function() {
    return new Dimension($(document).width() - 4, $(document).height());
}

f_xy = function(aTag) {
	var l_left = aTag.offsetLeft;
	var l_top = aTag.offsetTop;

	do {
		aTag = aTag.parentNode;
		if (aTag.tagName == "TD") {
			l_left = aTag.offsetLeft + 2;
			l_top += aTag.offsetTop;
		}
	} while (aTag.tagName != "BODY" && aTag.tagName != "HTML");

	l_top += $$('div_header').offsetHeight; // header

	return new Point(l_left, l_top);;
}

Dimension = function(p_w, p_h){
	this.width = p_w;
	this.height = p_h;
}

Point = function(p_left, p_top){
	this.left = p_left;
	this.top = p_top;
}

getKeyCode = function(e) {
	var l_key = null;
	if (e.which != undefined) l_key = e.which;
	else l_key = e.keyCode;
	
	return l_key;
}

isEnterKey = function(e) {
	if (getKeyCode(e) == 13)
		return true;
	else
		return false;
}


f_trim = function(p_text) {
   while(''+p_text.charAt(0)==' ')p_text=p_text.substring(1,p_text.length);
   while(''+p_text.charAt(p_text.length-1)=='')p_text=p_text.substring(0,p_text.length-1);
   return p_text;
}

f_limitSize = function(p_object_id, p_size) {
	var l_obj = $$(p_object_id);
	
	if (l_obj.value.length > p_size) {
		l_obj.value = l_obj.value.substring(0, p_size);
	}
}

/* cookie */
setCookie = function(name,value) {
	if (10) {
		var date = new Date();
		date.setTime(date.getTime()+(10*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var l_ldc = name+"="+value+expires+"; path=/;"
	if (g_domain != "") l_ldc += "domain=" + g_domain + ";";

	document.cookie = l_ldc;
}

getCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

getRndParam = function() {
	return 'rnd=' + Math.floor(Math.random()*10000000);
}