﻿var isIE  	= (document.all) ? true : false;
var isN4  	= (document.layers) ? true : false;
var isDOM 	= (document.getElementById && !document.all) ? true : false;
function PrintPage()
{
	if (isIE)
		window.print();
	else
		alert('Por favor selecione a opcao "Arquivo -> Imprimir" do menu superior para imprimir.');
}
function GoToNext(size,obj1,obj2)
{
   if(document.getElementById(obj1.id).value.length >=size)
      document.getElementById(obj2.id).focus();
}
function OpenPopup(idPopup, url, popupW, popupH){
	window.open(url, idPopup, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + popupW + ',height=' + popupH + ',top=55,left=55');
}
function OpenPopupPrintOrder(idPopup, url, popupW, popupH) {
    window.open(url, idPopup, 'toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=' + popupW + ',height=' + popupH + ',top=55,left=55');
}
function OpenPopupRemenber(url)
{
    window.open(url,'ProductRemember','toolbar=no,status=no,menubar=no,scrollbars=no,resizeable=no,top=100,left=50,width=300,height=335');
} 
function OpenGiftCardPopup(idPopup, url, popupW, popupH){
	window.open(url, idPopup, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + popupW + ',height=' + popupH + ',top=55,left=55');
}
function oPenPopupShipCost(zipCode, zipCodeComplement)
{
    var zipfull = zipCode + zipCodeComplement;
    window.open('popupshipcost.aspx?zip='+zipfull,null ,'width=500,height=200')
    return false;
}
function ValidateNumberOfChars(textBox, limit, divResponse)
{
	var validKey = true;
	var obj = document.getElementById(textBox);
	var divResponse = document.getElementById(divResponse);
	var diff = limit - obj.value.length;
	
	if(diff < 0 && 
		window.event.keyCode != 8 &&
		window.event.keyCode != 17 &&
		!(window.event.keyCode >= 33 && window.event.keyCode <= 40) &&
		window.event.keyCode != 46
		)
	{
		obj.value = obj.value.substr(0, limit);
		alert('Atenção: Você não pode exceder ' + limit + ' caracteres neste campo!');
		validKey = false;
	}
	divResponse.innerHTML = "Total de <font color=\"red\"><strong>" + obj.value.length + "</strong></font> caracteres digitados de no máximo <strong>" + limit + "</strong>.";
	
	return validKey;
}
function ProductZoom(imageUrl, fgType, idProduct)
{
    pLX = 625;
	pAY = 560;
	xx=parseInt(((screen.width-pLX-8)/2));
	yy=parseInt(((screen.height-pAY-15)/2));
	pUrl = 'productpopup.aspx?productId=' + idProduct + '&img=' + imageUrl + '&type=' + fgType;
	pTit = 'Vertico20';
    		
	s = window.open(pUrl, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + pLX + ', height=' + pAY + ', top=' + yy + ', left=' + xx + ', screenX=' + xx + ', screenY=' + yy);
}
function ChangeImage(sWebPath, sImgBig, oImgBig, sImgZoom, oAImgSmall, idProduct)
{
    if (isDOM)
    {
        document.getElementById(oImgBig).src = sWebPath + "/images/product/" + sImgBig
        if (sImgZoom != "")
            document.getElementById(oAImgSmall).setAttribute("href", "javascript:ProductZoom('" + sImgZoom + "','1', '" + idProduct + "');");
        else
            document.getElementById(oAImgSmall).removeAttribute("href");
    }
    else if (isN4)
    {
        document.layers[oImgBig].src = sWebPath + "/images/product/" + sImgBig
        if (sImgZoom != "")
            document.layers[oAImgSmall].setAttribute("href", "javascript:ProductZoom('" + sImgZoom + "','1', '" + idProduct + "');");
        else
            document.layers[oAImgSmall].removeAttribute("href");
    }
    else if (isIE)
    {
        document.all[oImgBig].src = sWebPath + "/images/product/" + sImgBig
        if (sImgZoom != "")
            document.all[oAImgSmall].setAttribute("href", "javascript:ProductZoom('" + sImgZoom + "','1', '" + idProduct + "');");
        else
            document.all[oAImgSmall].removeAttribute("href");
    }
}
function ChangeZoomImage(sPath, sImgZoomName, oImgZoom)
{
    if (isDOM)
        document.getElementById(oImgZoom).src = sPath + "/images/product/" + sImgZoomName;
    else if (isN4)
        document.layers[oImgZoom].src = sPath + "/images/product/" + sImgZoomName;
    else if (isIE)
        document.all[oImgZoom].src = sPath + "/images/product/" + sImgZoomName;
}
function GoToNext2(size,cep1,cep2)
{
    if(document.getElementById(cep1).value.length >=size)
        document.getElementById(cep2).focus(); 
}
function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { 
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function isNum( caractere )
{
    var strValidos = "0123456789-,."
    if ( strValidos.indexOf( caractere ) == -1 )
        return false;
    return true;
}
function validaTecla(campo, event)
{
    var BACKSPACE= 8;
    var key;
    var tecla;
    CheckTAB=true;
    if(navigator.appName.indexOf("Netscape")!= -1)
        tecla= event.which;
    else
        tecla= event.keyCode;
    key = String.fromCharCode(tecla);
    if (tecla == 13)
        return false;
    if (tecla == BACKSPACE)
        return true;
    return (isNum(key));
} 
function btnNews() 
{
    pLX = 300;
    pAY = 230;
    xx=parseInt(((screen.width-pLX-8)/2));
    yy=parseInt(((screen.height-pAY-15)/2));
    pUrl = 'newsletter.aspx';
    pTit = 'Cadastre-se';
    s = window.open(pUrl,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+pLX+',height='+pAY+',top='+yy+',left='+xx+',screenX='+xx+',screenY='+yy);
}
function OpenPopupHelp()
{
    pUrl = 'AjudaOnLinePopup.aspx';
    s = window.open('AjudaOnLinePopup.htm','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=302,height=402,top=0,left=0,screenX=0,screenY=0');
}
function OpenPopupCreditCard()
{
    s = window.open('oqueeisso.htm','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=405,height=500,top=0,left=0,screenX=0,screenY=0');
}
function OpenPopupCreditCard2()
{
    s = window.open('creditCard.htm','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=460,height=490,top=0,left=0,screenX=0,screenY=0');
}
function ValidaCPF( campo )
{
	if(!ValidaCPF_CNPJ( campo.value, 1 ))
	{
	    alert('O CPF digitado não é valido!');
	    campo.value = '';
	}
}
function ValidaCNPJ( campo )
{
	if(!ValidaCPF_CNPJ( campo.value, 2 ))
	{
	    alert('O CNPJ digitado não é valido!');
	    campo.value = '';
	}
}
function ValidaCPF_CNPJ(campo,pType)
{
	if( campo == null || campo.Trim() == '' )
		return false;

	var campo_filtrado = '', valor_1 = ' ', valor_2 = ' ', ch = '';
	var valido = false;
        
	for (i = 0; i < campo.length; i++)
	{
		ch = campo.substring(i, i + 1);
		if (ch >= '0' && ch <= '9')
		{
			campo_filtrado = campo_filtrado.toString() + ch.toString()
			valor_1 = valor_2;
			valor_2 = ch;
		}
		if ((valor_1 != ' ') && (!valido)) valido = !(valor_1 == valor_2);
	}
	if (!valido) 
		campo_filtrado = '12345678912';

	if (campo_filtrado.length < 11)
	{
		for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = '0' + campo_filtrado;}
	}


	if(pType <= 1)
	{
		if ( ( campo_filtrado.substring(9,11) == __checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=='') )
			return true;
	}

	if((pType == 2) || (pType == 0))
		if (campo_filtrado.length >= 14)
			if ( campo_filtrado.substring(12,14) == __checkCNPJ( campo_filtrado.substring(0,12) ) )
				return true;
	return false;
}
function __checkCNPJ(vCNPJ)
{
   var mControle = '';
   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
   for (i = 1 ; i <= 2 ; i++){
      mSoma = 0;
      for (j = 0 ; j < vCNPJ.length ; j++)
         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10 ) mDigito = 0;
      mControle1 = mControle ;
      mControle = mDigito;
      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
   }
   return( (mControle1 * 10) + mControle );
}
function __checkCPF(vCPF)
{
   var mControle = ''
   var mContIni = 2, mContFim = 10, mDigito = 0;
   for (j = 1 ; j <= 2 ; j++){
      mSoma = 0;
      for (i = mContIni ; i <= mContFim ; i++)
         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10) mDigito = 0;
      mControle1 = mControle;
      mControle = mDigito;
      mContIni = 3;
      mContFim = 11;
   }
   return( (mControle1 * 10) + mControle );
}
String.prototype.TrimStart = function(c) 
{
    if( !c )
        c = ' ';
	var rt = this;
	while(rt.substring(0,1) == c)
	{
		rt = rt.substring(1, rt.length);
	}
	return rt;
}
String.prototype.TrimEnd = function(c) 
{
    if( !c )
        c = ' ';
	var rt = this;
	while(rt.substring(rt.length-1, rt.length) == c)
	{
		rt = rt.substring(0,rt.length-1);
	}
	return rt;
}
String.prototype.Trim = function(c) 
{
    if( !c )
        c = ' ';
	return this.TrimEnd(c).TrimStart(c);
}
