// JavaScript Document

function charPostCod(str) {
	//Codifica os strings para o Ajax
	/*
	1 > &
	2 > %
	3 > " "
	4 > \
	5 > +
	*/
	var newStr;
	newStr = replaceStr(str, "&", "codStr[$*1*$]");
	newStr = replaceStr(newStr, "%", "codStr[$*2*$]");
	newStr = replaceStr(newStr, " ", "codStr[$*3*$]");
	newStr = replaceStr(newStr, "\\", "codStr[$*4*$]");
	newStr = replaceStr(newStr, "+", "codStr[$*5*$]");
	newStr = replaceStr(newStr, "'", "codStr[$*6*$]");
	return newStr;
}

function charDecodJavascript(str) {
	//Decodifica os strings para o Javascript
	/*
	1 > &
	2 > %
	3 > " "
	4 > \
	5 > +
	*/
	var newStr;
	newStr = replaceStr(str, "codStr[$*1*$]", "&");
	newStr = replaceStr(newStr, "codStr[$*2*$]", "%");
	newStr = replaceStr(newStr, "codStr[$*3*$]", " ");
	newStr = replaceStr(newStr, "codStr[$*4*$]", "\\");
	newStr = replaceStr(newStr, "codStr[$*5*$]", "+");
	newStr = replaceStr(newStr, "codStr[$*6*$]", "'");
	return newStr;
}

function quebraLinhaAjaxCod(str) {
	//Codifica a quebra de linha para o Ajax
	var newStr;
	newStr = replaceStr(str, "\n", "codStr[$*Q*$]");
	return newStr;
}

function deletaRegDBGeral(id, origem) {
	deletar = confirm("Deletar este item?");
	if (deletar == true) {
		window.location.href = "deletaRegDB.asp?acao=deletar&id=" + id + "&origem=" + origem;
	} else {
		return false;
	}
}

function replaceStr(texto, procurar, novo) {
	len = procurar.length;
	pos = texto.indexOf(procurar);
	while (pos > -1) {
		parte1 = texto.substring(0, pos);
		parte2 = texto.substring(pos + len , texto.length);
		texto = parte1 + novo + parte2;
		pos = texto.indexOf(procurar);
	}
	return texto;
}

function createXMLHTTP() {
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
			alert(ajax);
		}
		catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}
			catch(exc) {
				alert("Este browser nao tem recursos para executar esta tarefa.");
				ajax = null;
			}
		}
	return ajax;
	}
	var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	for (var i = 0; i < arrSignatures.length; i++) {
		try {
			var oRequest = new ActiveXObject(arrSignatures[i]);
			return oRequest;
		}
		catch (oError) {
		}
	}
	throw new Error("MSXML nao esta instalado no seu sistema.");
}

/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
var Utf8 = {
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

// Função para aparar strings
function TrimJS(str) {
	while (str.charAt(0) == " ")
		str = str.substr(1, str.length -1);
	
	while (str.charAt(str.length - 1) == " ")
		str = str.substr(0, str.length-1);
	
	return str;
}

function Abrir(pag) {
	window.open("loja_"+pag+".asp","pop_"+pag,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=350");
}

function PaginaBoleto() {
	window.open("boleto.asp","popBoleto","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=450");
}

function retira_acentos(str) {
	com_acento = 'çÇáàâãäÁÀÂÃÄëéèêËÉÈÊíïîìÍÏÎÌöóòôõÖÓÒÔÕüúùûÜÚÙÛ';
	sem_acento = 'cCaaaaaAAAAAeeeeEEEEiiiiIIIIoooooOOOOOuuuuUUUU';
	nova='';
	for(i = 0; i < str.length; i++) {
		if (com_acento.search(str.substr(i, 1)) >= 0) {
			nova += sem_acento.substr(com_acento.search(str.substr(i, 1)), 1);
		}
		else {
			nova += str.substr(i, 1);
		}
	}
	return nova;
}
