function formDisable()
{
	return document.getElementById('btnEnviar').disabled='disabled';
}
// JavaScript Document

function getPageScroll()
{

	var yScroll;
	
	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
	} 
	else if(document.documentElement && document.documentElement.scrollTop)
	{ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	arrayPageScroll = new Array('',yScroll)
	
	return arrayPageScroll;
}
	
function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else 
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) 
	{ // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else 
	{
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{
		pageWidth = windowWidth - 18;
	} 
	else 
	{
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


var Window =
{
	'_open': function(div) 
	{
	
		
		//$('#'+div).focus();
		//if (document.all)
		//var pos = evt.clientY;
		//else
		// var pos = window.pageYOffset;
		
		var pageSize = getPageSize();
		
		var objScroll = getPageScroll();
		
		//alert(objScroll[1]);
		
		var winW = pageSize[0];
		var winY = pageSize[1];
		
		//menos a largura da div
		var w = (winW - 330) / 2;
		 //menos a altura da div
		var y = (winY - 300) / 2;
		
		
		/*if (objScroll[1] > 167)
			arr = (objScroll[1] - 167) + 'px';
		else
			arr = '0px';*/
			
		document.getElementById(div).style.left = w + 'px';
		document.getElementById(div).style.top = (objScroll[1] + 150) + 'px';
		document.getElementById(div).style.display='block';
	}
}














// Função para CARREGAR todas as IMAGENS ANTES DE SEREM VISUALIZADAS no site
function loadImages()
{
    var img1 = new Image();
    img1.src="imagens/layer.jpg";
}



function isCpf(CPF)
{
	if(CPF.length<14)
		return false;
	
	//retira os '.' e o '-' da string do cpf (ex. '123.123.123-45' => '12312312345') para compatibilizar com a função isCPF
	CPF = CPF.substr(0,3) + CPF.substr(4,3) + CPF.substr(8,3) + CPF.substr(12,2);
	
	var i;
	var c = CPF.substr(0,9);
	var dv = CPF.substr(9,2);
	var d1 = 0;
	
	for (i = 0; i < 9; i++)
		d1 += c.charAt(i)*(10-i);

	if (d1 == 0)
		return false;
	 
	d1 = 11 - (d1 % 11);
	
	if (d1 > 9)
		d1 = 0;
	if (dv.charAt(0) != d1)
		return false;

	d1 *= 2;
	 
	for (i = 0; i < 9; i++)
	d1 += c.charAt(i)*(11-i);
	 
	d1 = 11 - (d1 % 11);
	 
	if (d1 > 9)
		d1 = 0;
	 
	if (dv.charAt(1) != d1)
		return false; 
	 
	return true; 
} //isCpf

function isCnpj(CNPJ)
{
	erro = new String;
	if (CNPJ.length < 18) return false;

	//substituir os caracteres que não são números
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ. substring (3,6);
		x += CNPJ. substring (7,10);
		x += CNPJ. substring (11,15);
		x += CNPJ. substring (16,18);
		CNPJ = x;
	} 
	else
	{
		CNPJ = CNPJ. replace (".","");
		CNPJ = CNPJ. replace (".","");
		CNPJ = CNPJ. replace ("-","");
		CNPJ = CNPJ. replace ("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++)
	{
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	
	for (y=0; y<13; y++) 
		   b += (a[y] * c[y]);
		   
	if ((x = b % 11) < 2)
		a[13] = 0;
	else
		a[13] = 11-x;
		
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
	{
		  return false;
	}
	if (erro.length > 0)
	{
		   alert(erro);
		   return false;
	} 
	else
	{
		alert("CNPJ valido!");
	}
	return true;
} //isCnpj



function $(id){
	
	return document.getElementById(id);

}

function $v(id){
	
	return document.getElementById(id).value;

}

function bg(campo){
	
	var cor_fundo = document.getElementById(campo).style.backgroundColor;
		
	if (cor_fundo == "" ){
		document.getElementById(campo).style.backgroundColor = "#D2CADA";
	}
	
	else {
		document.getElementById(campo).style.backgroundColor = "";
	}
}

var Contato = {
	
	'validar': function(){
		
		if($v('nome') == ""){
			alert('Você precisa digitar seu nome ! ');
			$('nome').focus();
			return false;
			
		}

		if($v('email') == ""){
			alert('Você precisa digitar seu e-mail ! ');
			$('email').focus();
			return false;
			
		}
	
		if($v('msg') == ""){
			alert('Você precisa digitar sua mensagem ! ');
			$('msg').focus();
			return false;
			
		}
	}
	
}




var Promo = {
	
	'valida': function()
	{
		if($v('emailPromocao') == "" || document.getElementById('emailPromocao').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('emailPromocao').focus();
			return false;
		}	
		
		if($('S').checked==false && $('M').checked==false && $('L').checked==false && $('XL').checked==false)
		{
			alert('Você precisa digitar o seu tamanho ! ');
			return false;			
		}
		
		if($('sexoPromocao_M').checked==false &&  $('sexoPromocao_F').checked==false)
		{
			alert('Você precisa digitar o seu sexo ! ');
			return false;			
		}
		
		return document.getElementById('btnPromoCadastro').disabled='disabled';
	}	
}





var Detalhes = {
	
	'validar': function(){
		
			if($v('qtd_s') == "" && $v('qtd_m') == "" && $v('qtd_l') == "" && $v('qtd_xl') == "" && $v('qtd_sm') == "" && $v('qtd_ml') == "" && $v('qtd_lx') == ""){
				if($v('qtd_u') == "" && $('qtd_u').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_u').focus();
					return false;
				}
				if($v('qtd_s') == "" && $('qtd_s').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_s').focus();
					return false;
				}
				if($v('qtd_m') == "" && $('qtd_m').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_m').focus();
					return false;
				}
				if($v('qtd_l') == "" && $('qtd_l').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_l').focus();
					return false;
				}
				if($v('qtd_xl') == "" && $('qtd_xl').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_xl').focus();
					return false;
				}
				if($v('qtd_sm') == "" && $('qtd_sm').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_sm').focus();
					return false;
				}
				if($v('qtd_ml') == "" && $('qtd_ml').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_ml').focus();
					return false;
				}
				if($v('qtd_lx') == "" && $('qtd_lx').type!='hidden'){
					alert('Você precisa digitar uma quantidade ! ');
					$('qtd_lx').focus();
					return false;
				}
			}
	},
		
	'validarAviso': function(){
		
		if($v('email') == ""){
			alert('Você precisa digitar seu e-mail ! ');
			$('email').focus();
		return false;
		}
		else
			if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($v('email'))){
				fecha('divAviso');
			}
			else{
				alert("Por favor, digite um e-mail válido ! ");
				$('email').value = "";
				$('email').focus();
				return false;
			}

	}

	
}

var Carrinho = {
	
	'validar': function(){
	
		if($v('ref_transacao') == ""){
			alert('Você precisa digitar o CPF/CNPJ! ');
			$('ref_transacao').focus();
			return false;
			
		}

		if($('f').checked==true && isCpf($v('ref_transacao')) == false)
		{
			alert('Você precisa digitar um CPF válido! ');
			$('ref_transacao').focus();
			return false;
		}

		if($('j').checked==true && isCnpj($v('ref_transacao')) == false)
		{
			alert('Você precisa digitar um CNPJ válido! ');
			$('ref_transacao').focus();
			return false;
		}

		if($v('cliente_cep') == ""){
			alert('Você precisa digitar o CEP!');
			$('cliente_cep').focus();
			return false;
			
		}

		if($v('tipo_frete') == 0){
			alert('Você precisa escolher o tipo de frete! ');
			$('tipo_frete').focus();
			return false;
			
		}
	
	},
	
	'escolheMascara': function(tipo){
		if (tipo=="f"){
			$('ref_transacao').value="";
			$('labelTipo').innerHTML = 'CPF:&nbsp;';
			$('ref_transacao').maxLength=14;
			$('ref_transacao').focus();
		}
		if (tipo=='j'){
			$('ref_transacao').value="";
			$('labelTipo').innerHTML = 'CNPJ:&nbsp;';
			$('ref_transacao').maxLength=18;
			$('ref_transacao').focus();
		}
	}

	
}

function mascaraCNPJ(objeto){
		if (objeto.value.indexOf(".") == -1 && objeto.value.length > 2){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 2){
			objeto.value += ".";
		}
		
		if (objeto.value.indexOf(".") == -1 && objeto.value.length > 6){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 6){
			objeto.value += ".";
		}
		
		if (objeto.value.indexOf("/") == -1 && objeto.value.length > 10){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 10){
			objeto.value += "/";
		}
		
		if (objeto.value.indexOf("-") == -1 && objeto.value.length > 15){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 15){
			objeto.value += "-";
		}
}

function mascaraCPF(objeto){
		if (objeto.value.indexOf(".") == -1 && objeto.value.length > 3){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 3){
			objeto.value += ".";
		}
		
		if (objeto.value.indexOf(".") == -1 && objeto.value.length > 7){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 7){
			objeto.value += ".";
		}
		
		if (objeto.value.indexOf("-") == -1 && objeto.value.length > 11){ 
			objeto.value = ""; 
		}
		
		if (objeto.value.length == 11){
			objeto.value += "-";
		}
}

function formatar(src, mask)
{
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);
	
	if(texto.substring(0,1) != saida)
		src.value += texto.substring(0,1);
}

function abre(id){
	$(id).style.display='block';	
}

function fecha(id){
	$(id).style.display='none';	
}

function carregafoto(id,foto,imgDivAmpliada)
{
	$(id).src='_imagens/' + foto;
	if(imgDivAmpliada)
	{
		$('imgAmpliada').src='_imagens/' + imgDivAmpliada;
		$(id).onclick = function ()
		{
			Ampliada();
		}
	}
	else
	{
		$(id).style.cursor='auto';
		$('infoImgAmpliada').innerHTML ="";
		$(id).onclick = function ()
		{
			return false;
		}
	}
}

function abreUmFechaOutro(abre,fecha)
{ //Função que quando se abre um div fecha outro, e quando se abre o outro fecha o um
	$(abre).style.display = "block";
	$(fecha).style.display = "none";
}

//Muda a cor da thumb com onmouseover
function selecionaMiniatura(sim,nao){
	$(sim).style.border='1px solid rgb(134, 131, 160)';
	$(nao).style.border='0px solid rgb(134, 131, 160)';	
}

function showTabelaMedidas(){

	var obj = $('divMedidas').style.display;
	if(obj == "none")
		$('divMedidas').style.display = 'block';
	else
		$('divMedidas').style.display = 'none';
}

//Carrinho
function presente(indice){
	var descr = $('item_descr_'+indice).value;
	if ($('presente_'+indice).checked)
		$('item_descr_'+indice).value = descr.substring(0,descr.length-3)+'sim';
	else
		$('item_descr_'+indice).value = descr.substring(0,descr.length-3)+'nao';
}


var qtd_antiga = new Array;

function carrega_qtd(valor,id_produto){
//função usada no carrinho para buscar a quantidade do produto daquela linha, antes de ser digitado a nova qtd
//serve para ajudar na atualização do preço
	qtd_antiga[id_produto] = valor;
}

function Ampliada()
{
	document.getElementById('divAmpliada').style.display='block';
	window.scrollTo(0,0);
}

//CENTRALIZADA
function openWin(url, name, w, h, s) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+s+',resizable=no';
//	winprop = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes';
	
	win = window.open(url, name, winprops)
	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus(); 
	}
}
//AJAX
function loadAjax(url,target,func,msg) {
    // native XMLHttpRequest object
    if (msg!=null)
		document.getElementById(target).innerHTML = '<div style="padding:10px; font-family:Tahoma,Verdana,Arial; font-size:11px;">Aguarde, carregando...</div>';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {jahDone(target, func);};
        req.open("GET", url, true);
        req.send(null);
    // IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {jahDone(target, func);};
            req.open("GET", url, true);
            req.send();
        }
    }
}    

function jahDone(target, func) {
    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
            o = document.getElementById(target);
			o.innerHTML = results;
			execJS(o);
			if (func)
				eval(func + "();");	
		} else {
            document.getElementById(target).innerHTML="jah erro:\n" +
                req.statusText;
        }
    }
}

var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bMoz = (navigator.appName == 'Netscape');

function execJS(node) 
{
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {
	if (bSaf) {
	  strExec = st[i].innerHTML;
	}
	else if (bMoz) {
	  strExec = st[i].textContent;
	}
	else {
	  strExec = st[i].text;
	}
	try {
	  eval(strExec);
	} catch(e) {
	  alert(strExec);
	  if (window.ActiveXObject)
		  alert(e.description);
	  else
	  	  alert(e);
	}
  }
}


var menuBar = {

	'showSubMenu': function (id_sub){
		if($(id_sub).style.display == 'block'){
			$('subMenuHomens').style.display = 'none';
			$('subMenuMulheres').style.display = 'none';
			$('subMenuSM').style.display = 'none';
			$('subMenuOutros').style.display = 'none';
			$('subMenuLancamentos').style.display = 'none';
			$(id_sub).style.display = 'none';
		}
		else{
			$('subMenuHomens').style.display = 'none';
			$('subMenuMulheres').style.display = 'none';
			$('subMenuSM').style.display = 'none';
			$('subMenuOutros').style.display = 'none';
			$('subMenuLancamentos').style.display = 'none';
			$(id_sub).style.display = 'block';
			
		}
	},
	
	'showMenu': function (){
		document.getElementById('menuBar').style.display = 'block';
	}
	
}

//Função para permitir apenas números
function teclas(campo,evt)
{
	if(((evt.keyCode < 96) || (evt.keyCode > 105)) && ((evt.keyCode < 48) || (evt.keyCode > 57)) )
		campo.value = campo.value.replace(String.fromCharCode(evt.keyCode).toLowerCase(),"");
}

function formataMoeda(campo)
{
	str = campo;
	
	while(str.length<4)
		str = '0' + str;
	
	while(str.search(",") != -1)
		str = str.replace(",","");
	i = 0;
	
	while(i< str.length)
	{
		if(str.substr(i,1) == ".")
			str = str.replace(".","");
		i++;
	}
	
	part1 = str.substr(0,str.length - 2);

	while(part1.search(" ") != -1)
		part1 = part1.replace(" ","");
	
	part2 = str.substr(str.length - 2,2);
	res = "";
	i = part1.length;
	sob = i % 3;
	
	if((sob != 0) && (i > 2))
		res = part1.substr(0,sob) + ".";
	else
		res = part1.substr(0,sob);
		
	j = 1;
	part1 = part1.substr(sob);
	i = 0;
	
	while(i < part1.length)
	{
		if(j == 3)
		{
			if(i + 1 == part1.length)
				res = res + part1.substr(i-2,3);
			else 
				res = res + part1.substr(i-2,3) + ".";
		}
	
	i++;
	j = j<3?j+1:1;
	
	}
	return res + "," + part2;
}

function atualizaTotal()
{
	var total=0;
	for(var i=1;document.getElementById('item_valor_' + i ) != null; i++)
	{
		subtotal = (document.getElementById('item_valor_' + i ).value.replace(',','.'))*Math.round((document.getElementById('item_quant_' + i ).value));
		subtotal = Math.round(subtotal*100).toString();

		document.getElementById('valor_total_' + i ).innerHTML = 'R$ ' + formataMoeda(subtotal.substring(0,subtotal.length-2) + ',' + subtotal.substring(subtotal.length-2));
 
		total = total + (document.getElementById('item_valor_' + i ).value.replace(',','.'))*Math.round((document.getElementById('item_quant_' + i ).value));
	}
	
	total = Math.round(total*100);
	total = total.toString();

	document.getElementById('valor_final').innerHTML = 'R$ ' + formataMoeda(total.substring(0,total.length-2)+ ',' + total.substring(total.length-2));
}
