var xmlHttp = new Object();
var idCont;

function _jsPage() {
	if (_jsPage.arguments.length == 1) idCont = "tdCont";
	else idCont = _jsPage.arguments[1];
	page = _jsPage.arguments[0];

	if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest()
	else if (window.ActiveXObject) xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
	xmlHttp.onreadystatechange = _jsDone
	xmlHttp.open("GET", page, true)
	xmlHttp.send(null)
}

function _jsDone() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById(idCont).innerHTML = xmlHttp.responseText;
	}
	else {
		document.getElementById(idCont).innerHTML = "<img src=\"../img/imgCarregando.gif\" style=\"margin:2px 2px 2px 2px;\">";
	}
}




function popup(x,n,w,h,s) {
    var l = ( (window.screen.width  - w) / 2 );
    var t = ( (window.screen.height - h) / 2 );
   janelinha = window.open(x,n,'leftmargin=-15,topmargin=-15,marginheight=0,marginwidth=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+s+',resizable=no,top='+t+',left='+l+',screenX='+l+',screenY='+t+',copyhistory=no,height='+h+',width='+w+'');
}


//============ AJAX =================
//===================================


function FormataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	//alert(tecla)
	if (teclapres.keyCode != 8) {
		vr = document.getElementById(campo).value;
		vr = vr.replace( "/","");
		vr = vr.replace( "/","");
		vr = vr.replace( ",","");
		vr = vr.replace( ".","");
		vr = vr.replace( ".","");
		vr = vr.replace( ".","");
		vr = vr.replace( ".","");
		tam = vr.length;
		
		if (tam < tammax && tecla != 8) tam = vr.length + 1;
		if (tecla == 8 ) tam = tam - 1;
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tam <= 2 ) document.getElementById(campo).value = vr;
			if ( (tam > 2) && (tam <= 5) ) document.getElementById(campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
			if ( (tam >= 6) && (tam <= 8) ) document.getElementById(campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
			if ( (tam >= 9) && (tam <= 11) ) document.getElementById(campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
			if ( (tam >= 12) && (tam <= 14) ) document.getElementById(campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
			if ( (tam >= 15) && (tam <= 17) ) document.getElementById(campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
		} else return false;
	}
}

var TimeToFade = 2000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null 
        || element.style.opacity == '' 
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() 
        + ",'" + eid + "')", 33);
  }  
}
function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' 
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 
      'alpha(opacity = ' + (newOpVal*100) + ')';
  
  setTimeout("animateFade(" + curTick 
      + ",'" + eid + "')", 33);
}

function _jsBoxResposta(css, msg){
	document.getElementById('boxMensagem').className = css;
	document.getElementById('retornoMensagem').innerHTML = msg;
	document.getElementById('boxMensagem').style.display = '';
	//setTimeout('_jsDetalhes02('+Tipo_Id+')',100);
	setTimeout('fade(\'boxMensagem\')', 2000);
	//fade('boxMensagem');
}

function mostraSubCat(Categoria){
	if(document.getElementById('menuSubCat'+Categoria).style.display == "none")
		document.getElementById('menuSubCat'+Categoria).style.display = "";
	else	
		document.getElementById('menuSubCat'+Categoria).style.display = "none";
}

function _jsAtualizaProduto(Opcao_Id, Produto_Quantidade){
	location.href="acoes.php?Acao=3&Opcao_Id="+Opcao_Id+"&Produto_Quantidade="+Produto_Quantidade;	
}

function _jsAdicionaProduto(Produto_Id, Opcao_Id){
	location.href="acoes.php?Acao=1&Produto_Id="+Produto_Id+"&Opcao_Id="+Opcao_Id;	
}

function _jsLimpaCarrinho(){
	location.href="acoes.php?Acao=9";	
}

function _jsFecharPedido(){
	if((document.getElementById('Carrinho_CEP').value == "") || (document.getElementById('Verifica_CEP').value == "0")){
		alert('Calcule o frete primeiro.');	
	}
	else{
		location.href="index.php?ir=carrinhoLogin.php";	
	}
}

function _jsPedDetalhe(pedido){
	if(document.getElementById(pedido).style.display == ""){
		document.getElementById(pedido).style.display = "none";
	}
	else{
		document.getElementById(pedido).style.display = "";
	}
}

function _jsValidaCPF(CPF){ 
 	var i; 
	s = CPF  
	var c = s.substr(0,9); 
	var dv = s.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; 
}
