// Resolve uso da JQuery junto a demais frameworks.
var $j = jQuery.noConflict();

// Extensão customizada para o Qualitor de $.ajax (JQuery).
function QAjax(obj) {
	if (!obj.beforeSend)
		obj.beforeSend = function() { iniciaLoading();	};

	if (!obj.complete)
		obj.complete = function() { encerraLoading(); };

	if (!obj.dataType)
		obj.dataType = "script";

	if (!obj.type)
		obj.type = "POST";

	$j.ajax(obj);
}

// Timer de carregamento.
var loadingTimerInterval = null;
var loadingTimerHour = 0;
var loadingTimerMinute = 0;
var loadingTimerSecond = 0;

// Integração com div de loading automático.
function iniciaLoading() {
	try {
		var spanHTML = "<span class=\"COLORLABEL\"><img src=\"" + pathWeb + "/framework/images/default/general/loading.gif\" class=\"VMIDDLE\" align=\"absmiddle\" />  " + QLabel[3] + "<br />(<span id=\"divLoadingTimer\">000:00:00</span>)</span>";
	} catch(e) {
		var spanHTML = "<span class=\"COLORLABEL\">  Carregando...<br />(<span id=\"divLoadingTimer\">000:00:00</span>)</span>";
	}

	if($("divLoading")) {
		if ($("divLoading").className == "DIVSHOW")
			return true;
		
		$("divLoading").className = "DIVSHOW";
		$("divLoading").innerHTML = spanHTML;
	}else{
		var body = document.getElementsByTagName("body")[0];
		var div = document.createElement("div");
		div.id = "divLoading";
		div.className = "DIVSHOW";
		div.zIndex = layerIndex;
		body.appendChild(div);
		div.innerHTML = spanHTML;
	}
	
	// Dispara timer de carregamento.
	if (loadingTimerInterval == null)
		loadingTimerInterval = setInterval('processLoadingTimer();', 1000);
}

function encerraLoading() {
	// Limpa timer de carregamento.
	if (loadingTimerInterval)
	{
		clearInterval(loadingTimerInterval);
		loadingTimerInterval = null;
	}
	loadingTimerHour = 0;
	loadingTimerMinute = 0;
	loadingTimerSecond = 0;
	
	$("divLoading").className = "DIVHIDE";
}

/**
 * Atualiza timer de processamento.
 * @return void
 */
function processLoadingTimer()
{
	loadingTimerSecond++;
	if (loadingTimerSecond >= 60)
	{
		// Zera segundo e incrementa minuto.
		loadingTimerSecond = 0;
		loadingTimerMinute++;
		// Se fechou minuto.
		if (loadingTimerMinute >= 60)
		{
			// Zera minuto e incrementa hora.
			loadingTimerMinute = 0;
			loadingTimerHour++;
		}
	}
	
	// Trata os segundos.
	var secondDisplay = loadingTimerSecond.toString();
	secondDisplay = (secondDisplay.length == 1 ? "0" + secondDisplay : secondDisplay);
	// Exibe o timer.
	if ($("divLoadingTimer"))
		$("divLoadingTimer").innerHTML = hourFormat(loadingTimerHour, loadingTimerMinute, 3) + ":" + secondDisplay;
}

function hasActiveRequest() {
	for (var i = 0; i < QTransport.length; i++) {
		if (QTransport[i].readyState < 4)
			return true;
	}

	return false;
}

var codingRule = [
	{entity: '%2F', expression: /\//g},
	{entity: '%2B', expression: /\+/g},
	{entity: '%3F', expression: /\?/g},
	{entity: '%3D', expression: /=/g},
	{entity: '%26', expression: /&/g},
	{entity: '%40', expression: /@/g},
	{entity: '%22', expression: /%u201C/g},
	{entity: '%22', expression: /%u201D/g},
	{entity: '%91', expression: /%u2018/g},
	{entity: '%92', expression: /%u2019/g},
	{entity: '%2A', expression: /%u2022/g},
	{entity: '%2D', expression: /%u2013/g}
];

function codificarHTML(myString) {
	if (myString == "") {
		return "";
	}
	
	if (checkURLEncode(myString))
		return myString;

	var encodedString = escape(myString);

	foreach (codingRule, function(myRule) {
		encodedString = encodedString.replace(myRule.expression, myRule.entity);
	});

	return encodedString;
}

function decodificarHTML(dsText) {
	
	if (checkURLDecode(dsText))
		return dsText;
	
	return unescape(dsText.replace(/\+/g, " "));
}

function checkURLEncode(dsText)
{
	return /%[0-9a-fA-F]{2}/.test(dsText);
}

function checkURLDecode(dsText)
{
	return !/(\+|%[0-9a-fA-F]{2})/.test(dsText);
}
