//===============================================================================
//= FUNÇÕES DE FORMULÁRIO =======================================================
//===============================================================================
var tipo = 0;

function NoSubmit() {
	tipo = 0;
}

function FaleConosco() {
	tipo = 1;
	document.form1.action = "mail.asp";
	document.form1.method = "post";
}

function Newsletter() {
	tipo = 2;
	document.form1.action = "newsletter-act.asp";
	document.form1.method = "post";
}

function Busca01() {
	tipo = 3;
	document.form1.action = "produtos/bp.asp";
	document.form1.method = "get";
}

function Busca02() {
	tipo = 4;
	document.form1.action = "../produtos/bp.asp";
	document.form1.method = "get";
}

function CadastroCliente() {
	tipo = 5;
	document.form1.action = "inserir-act.asp";
	document.form1.method = "post";
}

function AltCadastroCliente() {
	tipo = 6;
	document.form1.action = "alterar-act.asp";
	document.form1.method = "post";
}

function Login() {
	tipo = 7;
	document.form1.action = "login-act.asp";
	document.form1.method = "post";
}

function ProdutoIndique() {
	tipo = 8;
	document.form1.action = "popup_indique_mail.asp";
	document.form1.method = "post";
}

function AtualizaCarrinho() {
	tipo = 9;
	document.form1.action = "01_cesta_upd.asp";
	document.form1.method = "post";
}

function AtualizaCarrinhoLog() {
	tipo = 9;
	document.form1.action = "01_cesta-log_upd.asp";
	document.form1.method = "post";
}

function ProdutoAvisemeDisp() {
	tipo = 10;
	document.form1.action = "popup_aviseme_act.asp";
	document.form1.method = "post";
}

function ProdutoEncomenda() {
	tipo = 11;
	document.form1.action = "popup_encomenda_act.asp";
	document.form1.method = "post";
}

function CompraEnviarOrcamento(){
	tipo = 20;
	document.form1.action = "orcamento_mail.asp";
	document.form1.method = "post";
}

function CompraFinalizar(){
	tipo = 21;
	document.form1.action = "01_cesta_act.asp";
	document.form1.method = "post";
}

function CompraFinalizarSeg(urlsegura){
	tipo = 21;
	document.form1.action = urlsegura+"/compra/01_cesta_act.asp";
	document.form1.method = "post";
}

function CompraFinalizarLog(){
	tipo = 21;
	document.form1.action = "01_cesta-log_act.asp";
	document.form1.method = "post";
}

function CompraFinalizarSegLog(urlsegura){
	tipo = 21;
	document.form1.action = urlsegura+"/compra/01_cesta-log_act.asp";
	document.form1.method = "post";
}

function CompraEnderecoEntrega(){
	tipo = 22;
	document.form1.action = "03_confirma_endereco_act.asp";
	document.form1.method = "post";
}

function CompraFormaEnvio(){
	tipo = 23;
	document.form1.action = "04_forma_envio_act.asp";
	document.form1.method = "post";
}

function CompraFinalizarGeral(){
	tipo = 24;
	document.form1.action = "../comprapg/compra-act.asp";
	document.form1.method = "post";
}

function NewsletterAreaPP() {
	tipo = 30;
	document.form1.action = "newsletter/newsletter-act.asp";
	document.form1.method = "post";
}

function NewsletterAreaInt() {
	tipo = 31;
	document.form1.action = "../newsletter/newsletter-act.asp";
	document.form1.method = "post";
}

function EnviaFormulario() {
	switch(tipo) {
		case 1: return VFaleConosco(); break;
		case 2: return VNewsletter(); break;
		case 3: if (VBusca()) { document.location.href="produtos/bp.asp?busca="+ document.form1.busca.value +"&cond="+ document.form1.cond.value; } break;
		case 4: if (VBusca()) { document.location.href="../produtos/bp.asp?busca="+ document.form1.busca.value +"&cond="+ document.form1.cond.value; } break;
		case 5: return VCadastroCliente(); break;
		case 6: return VAltCadastroCliente(); break;
		case 7: return VLogin(); break;
		case 8: return VProdutoIndique(); break;
		case 9: return true; break;
		case 10: return VProdutoAvisemeDisp(); break;
		case 11: return VProdutoEncomenda(); break;
		case 20: return VCompraEnviarOrcamento(); break;
		case 21: return true;
		case 22: return VCompraEnderecoEntrega(); break;
		case 23: return VCompraFormaEnvio(); break;
		case 24: return VCompraFinalizarGeral(); break;
		case 30: return VNewsletterArea(); break;
		case 31: return VNewsletterArea(); break;
	}
	return false;
}


//===============================================================================
//= BUSCA =======================================================================
//===============================================================================
function VBusca() {
	if ((document.form1.cond) && (document.form1.cond.value != 0))  {
		return true;
	} else if ((!(document.form1.busca.value.length > 1)) || (document.form1.busca.value == document.form1.vlrHiddenBusca.value)) {
		alert("Informe o texto a ser procurado.");
		document.form1.busca.focus();
		return false;
	};
	return true;
}


//===============================================================================
//= JUMP MENU ===================================================================
//===============================================================================
function JumpMenu(url,parametro,valor) {
	if (valor) { document.location.href = url+'?'+parametro+'='+valor; };
}


//===============================================================================
//= CONTROLE DE DIVS ============================================================
//===============================================================================
function Esconde(id) {
	document.getElementById(id).style.display = (document.getElementById(id).style.display == 'none') ? '' : 'none';
}

function EscondeF(id) {
	tempoesconde[id] = setTimeout("Esconde('"+id+"')", 400);
}

var tempoesconde = new Array();
function Mostra(id) {
	if (typeof(tempoesconde[id]) != 'undefined')
		clearTimeout(tempoesconde[id]);
	document.getElementById(id).style.display =  "";
}

function EscondeDiv(id) {
	document.getElementById(id).style.display = (document.getElementById(id).style.display == 'none') ? '' : 'none';
}


//===============================================================================
//= EXIBE FLASHS ================================================================
//===============================================================================
function ExibeFlash(movie,w,h,wmode,bg) {
	if(wmode == "transparent") { ftransp = true; wmode = "wmode='transparent'"; } else { ftransp = false; wmode = ""; };
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+w+'" height="'+h+'">');
	if(ftransp) { document.write('<param name="wmode" value="transparent"/>'); };
	document.write('<param name="quality" value="high"/>');
	document.write('<param name="movie" value="'+movie+'"/>');
	document.write('<embed src="'+movie+'" quality="high" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" '+wmode+' width="'+w+'" height="'+h+'"></embed>');
	document.write('</object>');
}

function ExibeFlashHttps(movie,w,h,wmode,bg) {
	if(wmode == "transparent") { ftransp = true; wmode = "wmode='transparent'"; } else { ftransp = false; wmode = ""; };
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+w+'" height="'+h+'">');
	if(ftransp) { document.write('<param name="wmode" value="transparent"/>'); };
	document.write('<param name="quality" value="high"/>');
	document.write('<param name="movie" value="'+movie+'"/>');
	document.write('<embed src="'+movie+'" quality="high" pluginspage="https://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" '+wmode+' width="'+w+'" height="'+h+'"></embed>');
	document.write('</object>');
}


//===============================================================================
//= ABRE JANELA IGNORANDO ANTI-POPUP ============================================
//===============================================================================
function OpenWindowFree(url,larg,alt) {
	NewWindow('','janela',larg,alt,'yes');
	document.form1.action = url;
	document.form1.method = "post";
	document.form1.target = "janela";
	document.form1.submit();
}


//===============================================================================
//= ABRE POP-UP CENTRALIZADO ====================================================
//===============================================================================
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = parseInt((screen.width) ? (screen.width-w)/2 : 0);
	TopPosition = parseInt((screen.height) ? (screen.height-h)/2 : 0);
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',noresizable,status=yes'
	win = window.open(mypage,myname,settings)
}


//===============================================================================
//= REDIMENSIONA POP-UP =========================================================
//===============================================================================
function Redimensiona(w, h) {
	//Corrige o tamanho da janela
	if (navigator.appName == "Microsoft Internet Explorer") {
		w = parseInt(w) + 10;
		h = parseInt(h) + 60;
	} else {
		w = parseInt(w) + 6;
		h = parseInt(h) + 60;
	};
	// IE 7
	var nversao = navigator.userAgent;
	var nresult = nversao.search(/\bMSIE 7.0\b/g);
	if (nresult > 0) {
		h = parseInt(h) + 30;
	};
	//
	// IE 8
	var nversao = navigator.userAgent;
	var nresult = nversao.search(/\bMSIE 8.0\b/g);
	if (nresult > 0) {
		h = parseInt(h) + 40;
	};
	//
	// IE 9
	var nversao = navigator.userAgent;
	var nresult = nversao.search(/\bMSIE 9.0\b/g);
	if (nresult > 0) {
		h = parseInt(h) + 40;
	};
	//
	// FIREFOX 3
	var nversao = navigator.userAgent;
	var nresult = nversao.search(/\bFirefox\/3\b/g);
	if (nresult > 0) {
		h = parseInt(h) + 30;
	};
	//
	// FIREFOX 4
	var nversao = navigator.userAgent;
	var nresult = nversao.search(/\bFirefox\/4\b/g);
	if (nresult > 0) {
		h = parseInt(h) + 40;
	};
	//
	window.resizeTo(w, h);
	LeftPosition =  parseInt((screen.width) ? (screen.width-w)/2 : 0);
	TopPosition =  parseInt((screen.height) ? (screen.height-h)/2 : 0);
	window.moveTo(LeftPosition,TopPosition);
	window.focus();
}


//===============================================================================
//= FORMATAÇÃO DE VALOR EM MOEDA - R$ ===========================================
//===============================================================================
function formatCurrency(num) {
	num = num.toString().replace(/R|\$|\,/g,'');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100+0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
	return ('R$ ' + ((sign) ? '' : '-') + num + ',' + cents);
}


//===============================================================================
//= AJAX ========================================================================
//===============================================================================
function ajaxInit() {
	var req;
	
	try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(ex) {
			try {
				req = new XMLHttpRequest();
			} catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				req = null;
			}
		}
	}
	return req;
};

function LoadAjax(metodo, url, send, destino, destinojanela, loadshow, loadjanela, loaddestino, loadcontent, wdclose, scriptexec, scriptdet) {
	var ajax = ajaxInit();	
	if(ajax) {  
		if(metodo == "post") {  
			ajax.open("POST", url, true);  
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");   
			ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");   
			ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");   
			ajax.setRequestHeader("Pragma", "no-cache");   
		} else {
			ajax.open("GET", url + "?rnd="+ Math.random() +"&"+ send, true);  
		}
		ajax.onreadystatechange = function() { 
			if (ajax.readyState == 1) {
				if (loadshow == 2) {
					if (loadjanela == 1) {
						document.getElementById(loaddestino).innerHTML = loadcontent;
					} else {
						opener.document.getElementById(loaddestino).innerHTML = loadcontent;
					};
				};
			}
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					var resultado = ajax.responseText; // Coloca o retornado pelo Ajax nessa variável 
//					resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4) 
//					resultado = unescape(resultado); // Resolve o problema dos acentos 
					if (loadshow == 2) {
						if (loadjanela == 1) {
							document.getElementById(loaddestino).innerHTML = '';
						} else {
							opener.document.getElementById(loaddestino).innerHTML = '';
						};
					};
					if (destinojanela == 1) {
						document.getElementById(destino).innerHTML = resultado;
					} else {
						opener.document.getElementById(destino).innerHTML = resultado;
						if (wdclose == 2) {
							setTimeout("window.close()", 1000);
						};
					};
					if (scriptexec == 2) {
						setTimeout(scriptdet, 1000);
					};
				};
			}
		}  
			
		if(metodo == "post") {  
			ajax.send(send);  
		} else {  
			ajax.send(null);  
		}  
	}  
}
