var isIE = /*@cc_on!@*/false; //vendo se é o IE

function ajaxGo(param){
/* Versão: 2.0 - 21/06/2008 - Autores: Micox - Klawdyo - José Cláudio - GigaBytes - Jader A. W. */
var url, the_form, add_query='', callback, html_loading='', elem_return, unescape_, hide_err;
var concat, url_orig, msg, ajax;
var method='GET', query='', loadpos=0, self=this;
if(!param.url && !param.form){
return false;
}
if(param.url){ url = param.url; }
if(param.form){
if(param.form.constructor==String){
the_form = document.getElementById(param.form);
}else if(typeof(param.form.nodeType)!='undefined'){
the_form = param.form;
}
if(the_form && the_form.nodeName.toLowerCase()=='form'){
if(!url) { url = the_form.action; }
if(the_form.method) { method = the_form.method.toUpperCase();}
}else{
return false;
}
}
if(param.add_query){ add_query = param.add_query; }
if(param.callback){ callback = param.callback; }
if(param.loading){ html_loading = param.loading; }
if(param.unescape){ unescape_ = param.unescape;}
if(param.hide_err){ hide_err = param.hide_err;}
if(param.elem_return){
if(param.elem_return.constructor==String){
elem_return = document.getElementById(param.elem_return);
}else if(typeof(param.elem_return.nodeType)!='undefined'){
elem_return = param.elem_return;
}
if(!elem_return){
return false;
}
}
ajax = getAjax();
if(ajax){    
if(html_loading){
put(html_loading);
}        
if(the_form){    query = getFieldsForm(the_form); }
url_orig = url;
concat = (url.indexOf('?')>=0) ? "&" : '?';
if (add_query) {
url += concat + add_query;
concat = '&';
}
if(isIE){
dt = new Date();
url += concat + encodeURI(dt.getTime());
concat = '&';
delete dt;
}
ajax.onreadystatechange = ajaxOnReady;
try{
if(method=='GET'){
query = query.substr(0,2030);
ajax.open(method, url + concat + query ,true)
ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
ajax.setRequestHeader("Content-length", query.length);
query='';
}else{
ajax.open(method, url ,true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax.setRequestHeader("Content-length", query.length);            
}
ajax.setRequestHeader('X-Requested-With', 'ajax');
ajax.setRequestHeader("Cache-Control", "no-cache");
ajax.setRequestHeader("Pragma", "no-cache");
ajax.send(query);
}catch(e){
return;
}
return true;
}else{
return false;
}
function ajaxOnReady(){
if(!ajax) { return }
if(ajax.readyState==4){
if(ajax.status == 200){
var texto=ajax.responseText;
if(texto.indexOf(" ")<0) texto=texto.replace(/\+/g, " ");
if(unescape_==true){
texto=unescape(texto);
}
put(texto);
getScripts(texto);
if(isIE){ getStyles(texto) }
if(callback){ callback(texto); }
}else{
msg = "Falha no carregamento. " + httpStatus(ajax.status);
if(!hide_err){
if(html_loading){                    
put(msg);
}
}
if(callback){ callback(msg); }
}
ajax = null; delete ajax;
delete self; return;
}else if(ajax.readyState==2 && typeof(ajax.status)!='unknown'){
if(ajax.status != 200){
msg = "Falha no carregamento. " + httpStatus(ajax.status);
if(!hide_err){
if(html_loading){                    
put(msg);
}
}
if(callback){ callback(msg); }
ajax = null; delete ajax;
delete self; return;
}
}
}
function put(valor){
if(elem_return){
if(elem_return.nodeName.toLowerCase()=="input"){
valor = escape(valor).replace(/\%0D\%0A/g, "");
elem_return.value = unescape(valor);
}else if(elem_return.nodeName){
elem_return.innerHTML = valor;
}  
}
}
function getAjax(){
if(typeof(XMLHttpRequest)!='undefined'){return new XMLHttpRequest();}
var axO=['Microsoft.XMLHTTP', 'Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
for(var i=0;i<axO.length;i++){ try{ return new ActiveXObject(axO[i]);}catch(e){} }
return null;
}
function httpStatus(stat){
switch(stat){
case 0: return "Erro desconhecido de javascript. " + ajax.statusText;
case 400: return "400: Solicitacao incompreensivel";
case 403: case 404: return "404: Nao foi encontrada a URL solicitada " + url;
case 405: return "405: O servidor nao suporta o metodo solicitado";
case 500: return "500: Erro desconhecido do servidor";
case 503: return "503: Capacidade maxima do servidor alcancada";
default: return "Erro HTTP " + stat + ': ' + ajax.statusText + ". Informacoes em http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html";
}
}
}

function getScripts(texto){
var ini = 0;
while (ini!=-1){
ini = texto.indexOf('<script', ini);
if (ini >=0){
ini = texto.indexOf('>', ini) + 1;
var fim = texto.indexOf("<\/script>", ini);
codigo = texto.substring(ini,fim);
var novo = document.createElement("script")
novo.text = codigo; novo.type = 'text/javascript';
document.body.appendChild(novo);
}
}
}

function getStyles(texto){
var ini = 0;
while (ini!=-1){
ini = texto.indexOf('<style', ini);
if (ini >=0){
ini = texto.indexOf('>', ini) + 1;
var fim = texto.indexOf("<\/style>", ini);
codigo = texto.substring(ini,fim);
var rules = codigo.split('}')
var rule, selector, style;
for(var i=0; i<rules.length-1 ; i++){
rule = rules[i].split('{')
selector = rule[0].trim()
style = rule[1].trim()	
document.styleSheets[document.styleSheets.length-1].addRule(selector,style)
}
}
}
}

function getFieldsForm(fform){
var buff = [];
for (var i=0; i< fform.length;i++)  {
var campo = fform.elements[i];

if((campo.type=="checkbox" || campo.type=="radio")){
if(campo.checked==true && campo.name){
buff.push(campo.name + "=" + encodeURI(campo.value));
}
}else if(campo.name){
buff.push(campo.name + "=" + encodeURI(campo.value));
}
}
return buff.join("&");
}

/* FIM AJAXGO */


function move_box(an, box) {
if (intervalo) clearInterval(intervalo);
var cleft = 0;
var ctop = 5;
var obj = an;
while (obj.offsetParent) {
cleft += obj.offsetLeft;
ctop += obj.offsetTop;
obj = obj.offsetParent;
}
box.style.left = cleft + 'px';
ctop += an.offsetHeight;
if (document.body.currentStyle && document.body.currentStyle['marginTop']) {
ctop += parseInt(document.body.currentStyle['marginTop']);
}
box.style.top = ctop + 'px';
box.style.display='block';
bringToFront(box);
}

function getAbsoluteDivs() {
var arr = new Array();
var all_divs = document.body.getElementsByTagName("DIV");
var j = 0;
for (i = 0; i < all_divs.length; i++)
if (all_divs.item(i).style.position=='absolute') {
arr[j] = all_divs.item(i);
j++;
}
return arr;
}

function bringToFront(obj) {
if (!document.getElementsByTagName) return;
var divs = getAbsoluteDivs();
var max_index = 0;
var cur_index;
for (i = 0; i < divs.length; i++) {
var item = divs[i];
if (item == obj || item.style.zIndex == '') continue;
cur_index = parseInt(item.style.zIndex);
if (max_index < cur_index) max_index = cur_index;
}
obj.style.zIndex = max_index + 2;
var IfrRef = document.getElementById('sobretudo');
IfrRef.style.width = obj.offsetWidth;
IfrRef.style.height = obj.offsetHeight;
IfrRef.style.top = obj.style.top;
IfrRef.style.left = obj.style.left;
IfrRef.style.zIndex = max_index + 1;
IfrRef.style.display = "block";
}


function show_loading(an, texto) {
var boxdiv = document.getElementById('carregando');
if (boxdiv != null) {
boxdiv.innerHTML = '<img src="images/loading.gif" border="0" style="vertical-align:middle"> '+texto;
move_box(an, boxdiv);
}
}

function hide_loading() {
document.getElementById('sobretudo').style.display='none';
document.getElementById('carregando').style.display='none';
}


// variaveis globais
var cached = false;
var time_out_limit = 1;
var intervalo = 0;


function show_box(an,elemento) {
if (intervalo) clearInterval(intervalo);
if (an == null) return false;
var href = an.href;
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
if (cached == false) {
ajaxGo({ url:href, add_query: 'ajax=true', elem_return: boxdiv });
cached = true;
}
move_box(an, boxdiv);
}
return false;
}


function hide_box(elemento) {
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
if (boxdiv.style.display=='none') return false;
}
intervalo = window.setTimeout(function() {
boxdiv.style.display='none';
document.getElementById('sobretudo').style.display='none';
}, (1000*time_out_limit));
return false;
}


function buy_now(an,elemento) {
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
ajaxGo({ url:an.href, add_query: 'ajax=true', elem_return: boxdiv, loading: '<img src="images/loading.gif" border="0" style="vertical-align:middle"> <b>adicionando ao carrinho...</b>' });
cached = true;
move_box(an, boxdiv);
}
return false;
}


function remove_from_cart(an,elemento) {
cached = true;
show_loading(an, 'removendo do carrinho...');
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
ajaxGo({ url: an.href, add_query: 'ajax2=true', elem_return: boxdiv });
}
return false;
}


function update_cart(an,elemento,formulario) {
cached = true;
show_loading(an, 'atualizando quantidade...');
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
ajaxGo({ form: formulario, add_query: 'ajax2=true', elem_return: boxdiv });
}
return false;
}


function calcular_frete(an,elemento,formulario) {
cached = true;
show_loading(an, 'calculando frete...');
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
ajaxGo({ form: formulario, add_query: 'ajax2=true', elem_return: boxdiv });
}
return false;
}


function notify(an,elemento) {
show_loading(an, 'gravando notificação...');
var boxdiv = document.getElementById(elemento);
if (boxdiv != null) {
ajaxGo({ url: an.href, add_query: 'ajax=true', elem_return: boxdiv });
}
return false;
}
