function setTranspImg(divObj, urlImagem, width, height) {
	divObj.style.background = 'url('+urlImagem+')';
	divObj.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+urlImagem+'\',sizingMethod=\'scale\')';
	divObj.style.width = width + 'px';
	divObj.style.height = height + 'px';
}

function setTranspImgById(id, urlImagem, width, height) {
	var divObj = document.getElementById(id);
	setTranspImg(divObj, urlImagem, width, height);
}

//Cria o objeto XMLHTTPRequest
function getHTTPRequest() {
	var xmlhttp = null;
	
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	
	return xmlhttp;
}

function showLoad() {
	var div = document.getElementById('loadBar');
	div.style.position = 'fixed';
	div.style.top = '0px';
	div.style.right = '0px';
	div.style.width = '100px;'
	div.style.height = '15px';
	div.innerHTML = 'Carregando...';
	div.classId = 'loadBar';
	div.style.textAlign = 'center';
	div.style.fontWeight = 'bolder';
	div.style.backgroundColor = '#FF0000';
	div.style.color = '#FFFFFF';
	div.style.display = '';
}

function hideLoad() {
	document.getElementById('loadBar').style.display = 'none';
}

function site_url(url) {
	var base = document.getElementById('_BASE_URL').value;
	
	return base + url;
}

function redirect(url, txt) {
	url = site_url(url);
	var redir = true;
	
	if(txt != '') {
		redir = confirm(txt);
	}
	
	if(redir) {
		window.location = url;
	}
	
	return redir;
}

//Função que retorna o texto/XML de um arquivo
function getTotalProduto(obj) {
	var qtde = obj.value;
	var idBPN = obj.title;
	var url = site_url('pedido/calcula_preco/'+idBPN+'/'+qtde);
	var xmlhttp = getHTTPRequest();
	xmlhttp.open("GET", url,false);
	//showLoad();		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var valor = xmlhttp.responseText;
			var obj = valor.parseJSON();
			
			var div = document.getElementById('bpn'+idBPN);
			
			if(div) {
				div.innerHTML = obj.calculo.total;
			}
			//hideLoad();
		}
	}
	
	xmlhttp.send(null);
}

function getTotal() {
	var dtGrid = document.getElementById('dtGrid');
	var cedulas = dtGrid.getElementsByTagName('TD');
	var total = 0;
	
	for(var i = 0; i < cedulas.length; i++) {
		if(cedulas[i].id == 't_bpn') {
			txtSubTotal = cedulas[i].childNodes[1].innerHTML;
			subtotal = parseFloat(txtSubTotal);
			
			if(!isNaN(subtotal)) {
				total += subtotal;
			}
		}
	}
	
	document.getElementById('totalPedido').innerHTML = "R$ "+total;
}

//Função que retorna o texto/XML de um arquivo
function carregarEndereco(tipoEndereco) {
	var url = site_url('pedido/carrega_endereco/'+tipoEndereco);
	var xmlhttp = getHTTPRequest();
	xmlhttp.open("GET", url,false);
	showLoad();		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var d = xmlhttp.responseText;
			var dados = d.parseJSON();
			document.getElementById('endereco').value = dados.endereco;
			document.getElementById('bairro').value = dados.bairro;
			document.getElementById('cidade').value = dados.cidade;
			document.getElementById('estado').value = dados.estado;
			document.getElementById('cep').value = dados.cep;
			
			hideLoad();
		}
	}
	
	xmlhttp.send(null);
}

function finalizaPedido() {
	//if(confirm('Deseja finalizar o pedido?')) {
	window.location = site_url('pedido/finaliza');
	//}
}

(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();

String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};
