function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return "";
	}
	if ( start == -1 ) return "";
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	var valuePlus = document.cookie.substring( len, end );
	while(valuePlus.indexOf('+') > -1)
	{
		valuePlus = valuePlus.replace('+', ' ');
	}
	return decodeURIComponent(valuePlus);
}

function SomenteNumeroDecimal(e)
{
	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}

	if(key > 47 && key < 58 || key == 8 || key == 0 || key == 44)
	{
		return;
	}
	else
	{
		if(window.event)
		{
			window.event.returnValue = null;
		}
		else
		{
			e.preventDefault();
		}
	}
}


function reopenModal(url)
{
	closeModal();
	window.setTimeout('openModal("' + url + '");', 500);
}

function VerificaValorOnFocus(txtBusca)
{
  txtBusca.value = "";   
}

function VerificaValorOnBlur(txtBusca,HiddenNome)
{
    
    var hdValorPadrao = document.getElementById(HiddenNome);   
    if(hdValorPadrao)
    {
        if(txtBusca.value == "")
        {
            txtBusca.value = hdValorPadrao.value;
        }
        
        
    }
    
}

function Trim(STRING)
{
    STRING = LTrim(STRING);
    
    return RTrim(STRING);
}   

function RTrim(STRING)
{
    while(STRING.charAt((STRING.length -1))=="")
    {
        STRING = STRING.substring(0,STRING.length-1);
    }
    
    return STRING;
}

function LTrim(STRING)
{
    while(STRING.charAt(0) == "")
    {
        STRING = STRING.replace(STRING.charAt(0) ,"");
    }
    
    return STRING;
}

function SomenteLetra(e)
{
	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}
	
	if((key > 96 && key < 123) || (key > 64 && key < 91) || key == 8 || key == 0)
	{
		return;
	}
	else
	{
		if(window.event)
		{
			window.event.returnValue = null;
		}
		else
		{
			e.preventDefault();
		}
	}
}


function SomenteNumero(e)
{
	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}

	if(key > 47 && key < 58 || key == 8 || key == 0)
	{
		return;
	}
	else
	{
		if(window.event)
		{
			window.event.returnValue = null;
		}
		else
		{
			e.preventDefault();
		}
	}
}

function SomenteNumero2(e, src, mask)
{
	SomenteNumero(e);

	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}


	if(	key != 8)
	{
		Mascara(src,mask);
	}
}

function SomenteNumero_Mascara(e, src, mask)
{
	SomenteNumero(e);
	
	var key;

	if (window.event) 
	{
		key = event.keyCode;
	}
	else
	{ 
		key = e.which;
	}
	
	if (key != 8)
	{
		Mascara(src, mask);
	}
}

function Mascara(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 Mascara2(e, src, mask)
{
	var key;

	if (window.event)
	{
		key = event.keyCode;
	}
	else
	{
		key = e.which;
	}

	if (key != 8)
	{
		Mascara(src, mask);
	}
}

function ColocaFocus(NomeBtn, e)
{
	try
	{
		var key;

		if(window.event)
		{
			key = window.event.keyCode;
		}
		else
		{
			key = e.which;
		}

		document.getElementById(NomeBtn).focus();

		if(window.event)
		{
			window.event.keyCode = 0;
		}
	}
	catch(err)
	{
		alert(err.description);
	}

	return false;
}

function ColocaFocoEm(NomeBtn, e)
{
    try
    {
	    var key;

	    if (window.event)
	    {
		    key = event.keyCode;
	    }
	    else
	    {
		    key = e.which;
	    }
		
	    if (key == 13)
	    {
		    document.getElementById(NomeBtn).focus();
		    key = 13;
	    }
    }
    catch(err)
    {
	    alert(err);
    }
}

function Focar(obj, e)
{
    try
    {
	    var key;

	    if (window.event)
	    {
		    key = event.keyCode;
	    }
	    else
	    {
		    key = e.which;
	    }
		
	    if (key == 13)
	    {
		    obj.focus();
		    key = 13;
	    }
    }
    catch(err)
    {
	    alert(err);
    }
}

function AbrirPopUP(URL,nmPagina)
{
	try
	{
	    void(window.open(Trim(URL),Trim(nmPagina),'status = 1, height = 700, width = 600, resizable = 1,scrollbars=1'));
	}
	catch(err)
	{
		alert(err.description);
	}
}

function AbrirPopUPTamanho(URL, nmPagina, largura, altura)
{
	try
	{
		void(window.open(Trim(URL),Trim(nmPagina),'status = 1, height = ' + altura + ', width = ' + largura + ', resizable = 1,scrollbars=1'));
	}
	catch(err)
	{
		alert(err.description);
	}
}

function AbrirPopUPCompleto(URL, nmPagina, statusBar, largura, altura, resizable, scrollbars, centralizado)
{
	try
	{
		myWindow = window.open(Trim(URL),Trim(nmPagina),'status = ' + statusBar + ', height = ' + altura + ', width = ' + largura + ', resizable = ' + resizable + ',scrollbars= ' + scrollbars);
		if (centralizado)
		{		
		    myWindow.moveTo((screen.width / 2) - (largura / 2), (screen.height / 2) - (altura / 2));
		}
	}
	catch(err)
	{
		alert(err.description);
	}
}

function validaCNPJ(cnpj)
{
	var retorno = true;
	
	CNPJ = Trim(cnpj);
	
	erro = new String;
	
	CNPJ = CNPJ. replace (".","");
	CNPJ = CNPJ. replace (".","");
	CNPJ = CNPJ. replace ("-","");
	CNPJ = CNPJ. replace ("/","");
			
			
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! "; 
	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])){
			erro +="Dígito verificador com problema!";
	}
	
	if (erro.length > 0)
	{
		retorno = false;
	}
				
    return retorno;				
}

function _isinteger(test_string)
{
	var i;
	var non_nums = 0;

	for (i = 1; i < test_string.length; i++)
	{
		if ((test_string.substring(i-1,i) < '0') || (test_string.substring(i-1,i) > '9'))
		{
			non_nums++;
		}
	}

	if (non_nums == 0)
		return true;
	else
		return false;
}

function ImgAmbientadaSubmit(ProdutoCodigo, PartNumber, ProdCodInterno, IndiceArrAtual, strURL)
{
    var ctx =
    {
        ProdutoCodigo: ProdutoCodigo.toString(),
        PartNumber: PartNumber.toString(),
        ProdCodInterno: ProdCodInterno.toString(),
		strURL: strURL.toString()
    };

	PageMethods.ImgAmbientada(ProdutoCodigo.toString(), PartNumber.toString(), ProdCodInterno.toString(), IndiceArrAtual.toString(), ImgAmbientadaSuccess, ImgAmbientadaFailed, ctx);
}

function ImgAmbientadaSuccess(value, ctx, methodName)
{
	if(value[0] != "true")
	{
		closeModal();
		openModal(ctx.strURL + "/erro.aspx?msgerro=" + encodeURIComponent(value[1]));
	}
	else
	{
		var h2IndiceQtd = document.getElementById("h2IndiceQtd");
		var divFotoAnterior = document.getElementById("divFotoAnterior");
		var divFotoProxima = document.getElementById("divFotoProxima");
		var aLinkAnterior = document.getElementById("aLinkAnterior");
		var aLinkProxima = document.getElementById("aLinkProxima");
		var imgFotoAmbientada = document.getElementById("imgFotoAmbientada");
		var pDescricaoAmb = document.getElementById("pDescricaoAmb");

		imgFotoAmbientada.setAttribute("src", value[2]);
		pDescricaoAmb.innerHTML = value[3];
		h2IndiceQtd.innerHTML = value[4];

		if(value[5] == "")
		{
			divFotoAnterior.style.display = "none";
		}
		else
		{
			divFotoAnterior.style.display = "";
			aLinkAnterior.setAttribute("href", "javascript:ImgAmbientadaSubmit('" + ctx.ProdutoCodigo + "', '" + ctx.PartNumber + "', '" + ctx.ProdCodInterno + "', " + value[5] + ", '" + ctx.strURL + "');");
		}

		if(value[6] == "")
		{
			divFotoProxima.style.display = "none";
		}
		else
		{
			divFotoProxima.style.display = "";
			aLinkProxima.setAttribute("href", "javascript:ImgAmbientadaSubmit('" + ctx.ProdutoCodigo + "', '" + ctx.PartNumber + "', '" + ctx.ProdCodInterno + "', " + value[6] + ", '" + ctx.strURL + "');");
		}
	}
}

function ImgAmbientadaFailed(exc, ctx, methodName)
{
	//window.alert(exc.get_exceptionType());
	window.alert(exc.get_stackTrace());
	window.alert(exc.get_message());
	//window.alert(exc.get_statusCode());
	//window.alert(exc.get_timedOut());
}

function ImgDetalheSubmit(ProdutoCodigo, ProdutoCodigoInterno, PartNumber, IndiceArrAtual, strURL)
{
    var ctx =
    {
        ProdutoCodigo: ProdutoCodigo.toString(),
        ProdutoCodigoInterno: ProdutoCodigoInterno.toString(),
        PartNumber: PartNumber.toString(),
		strURL: strURL.toString()
    };

	PageMethods.ImgDetalhe(ProdutoCodigo.toString(), ProdutoCodigoInterno.toString(), PartNumber.toString(), IndiceArrAtual.toString(), ImgDetalheSuccess, ImgDetalheFailed, ctx);
}

function ImgDetalheSuccess(value, ctx, methodName)
{
	if(value[0] != "true")
	{
		closeModal();
		openModal(ctx.strURL + "/erro.aspx?msgerro=" + encodeURIComponent(value[1]));
	}
	else
	{
		var h2IndiceQtd = document.getElementById("h2IndiceQtd");
		var divFotoAnterior = document.getElementById("divFotoAnterior");
		var divFotoProxima = document.getElementById("divFotoProxima");
		var aLinkAnterior = document.getElementById("aLinkAnterior");
		var aLinkProxima = document.getElementById("aLinkProxima");
		var imgFotoDetalhe = document.getElementById("imgFotoDetalhe");
		var pDescricaoDetalhe = document.getElementById("pDescricaoDetalhe");

		imgFotoDetalhe.setAttribute("src", value[2]);
		pDescricaoDetalhe.innerHTML = value[3];
		h2IndiceQtd.innerHTML = value[4];

		if(value[5] == "")
		{
			divFotoAnterior.style.display = "none";
		}
		else
		{
			divFotoAnterior.style.display = "";
			aLinkAnterior.setAttribute("href", "javascript:ImgDetalheSubmit('" + ctx.ProdutoCodigo + "', '" + ctx.ProdutoCodigoInterno + "', '" + ctx.PartNumber + "', " + value[5] + ", '" + ctx.strURL + "');");
		}

		if(value[6] == "")
		{
			divFotoProxima.style.display = "none";
		}
		else
		{
			divFotoProxima.style.display = "";
			aLinkProxima.setAttribute("href", "javascript:ImgDetalheSubmit('" + ctx.ProdutoCodigo + "', '" + ctx.ProdutoCodigoInterno + "', '" + ctx.PartNumber + "', " + value[6] + ", '" + ctx.strURL + "');");
		}
	}
}

function ImgDetalheFailed(exc, ctx, methodName)
{
	//window.alert(exc.get_exceptionType());
	window.alert(exc.get_stackTrace());
	window.alert(exc.get_message());
	//window.alert(exc.get_statusCode());
	//window.alert(exc.get_timedOut());
}

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/

function validaCartao()
{
	var retorno = true;

	var CCval = document.getElementById("CartaoNumero").value;

	// check if credit card name has been entered
	if (arguments.IsValid)
	{
		if (CCval == "")
		{
			retorno = false;
		}
	}

	CCval = _strip_spaces(CCval);

	// check if credit card number is actually a number
	if (_isinteger(CCval) == false)
	{
		retorno = false;
	}
	if (retorno)
	{
		// check for 4242 4242 4242 4242
		if (CCval == "4242424242424242")
		{
			retorno = false;
		}
	}

	if (retorno)
	{
		//check that the credit card number is valid
		if (!CC_Validate(CCval))
		{
			retorno = false;
		}
	}

	return retorno;
}

function _strip_spaces(_ipstr)
{
	var _opstr = '';
	var i;
	for (i = 1; i <= _ipstr.length; i++)
	{
		if(_ipstr.substring(i-1, i) != ' ')
		{
			_opstr = _opstr + _ipstr.substring(i-1, i);
		}
	}
	return _opstr;
}


function _isinteger(test_string)
{
	var i;
	var non_nums = 0;

	for (i = 1; i < test_string.length; i++)
	{
		if ((test_string.substring(i-1,i) < '0') || (test_string.substring(i-1,i) > '9'))
		{
			non_nums++;
		}
	}

	if (non_nums == 0)
		return true;
	else
		return false;
}


function CC_Validate(ccnumber)
{
	var checksum = 0;
	var i;
	var digit;
	var temp;

	var cclength=ccnumber.length;
	if (cclength % 2 != 0)
	{
		cclength += 1;
		ccnumber = "0" + ccnumber;
	}

	for (i = 0; i < cclength; i++)
	{
		digit = parseInt(ccnumber.charAt(i));
		if ((i % 2) == 0)
		{
			digit *= 2;
			if (digit > 9)
			{
				digit = parseInt(digit / 10) + parseInt(digit % 10);
			}
		}
		checksum += digit;
	}


	if (checksum % 10 == 0)
		return true;
	else
		return false;
}


	function ColocaFocus(NomeBtn, e)
	{
		try
		{
			var key;

			if(window.event)
			{
				key = window.event.keyCode;
			}
			else
			{
				key = e.which;
			}
			if(key == 13)
			{
				document.getElementById(NomeBtn).focus();

				if(window.event)
				{
					window.event.keyCode = 0;
				}
			}
		}
		catch(err)
		{
			alert(err.description);
		}

		return false;
	}
	
function MontarLinkSaudacao()
{
	PageMethods.MontarLinkSaudacao(MontarLinkSaudacaoSuccess, MontarLinkSaudacaoFailed,'');
}

function MontarLinkSaudacaoSuccess(value, ctx, methodName)
{
	if(value != '')
	{
		var lnkSaudacao = document.getElementById("ctl00_Header_lnkSaudacao");   
		if(lnkSaudacao)
		{
		    lnkSaudacao.innerHTML  = value;
		}
	}
	
}

function MontarLinkSaudacaoFailed(exc, ctx, methodName)
{
	//window.alert(exc.get_exceptionType());
	//window.alert(exc.get_stackTrace());
	//window.alert(exc.get_message());
	//window.alert(exc.get_statusCode());
	//window.alert(exc.get_timedOut());
}

function CarregarCor()
{
	PageMethods.CarregarCor(CarregarCorSuccess, CarregarCorFailed,'');
}

function CarregarCorSuccess(value, ctx, methodName)
{
  //document.getElementsByTagName("head")[0].innerHTML += value;
  J("head").append(J(document.createElement("link")).attr({rel: "stylesheet",type: "text/css",href: value }));
  
}

function CarregarCorFailed(exc, ctx, methodName)
{
	
}



function EsconderContinuar()
{
	var btnContinuar = document.getElementById("ctl00_ContentPlaceHolderMain_btnContinuar"); 
	if(btnContinuar)
	{
	    btnContinuar.style.display = 'none';  
	}
}

function MostrarContinuar()
{
	var btnContinuar = document.getElementById("ctl00_ContentPlaceHolderMain_btnContinuar"); 
	if(btnContinuar)
	{
	    btnContinuar.style.display = 'block';  
	}
}

//Máscaras

var ObjetoGlobal;
var FuncaoGlobal;

function AplicarMascara(Objeto,Funcao)
{
    ObjetoGlobal    =   Objeto;
    FuncaoGlobal    =   Funcao;
    ExecutarMascara();
    setTimeout("ExecutarMascara()",1);
} 

function ExecutarMascara()
{
    ObjetoGlobal.value = FuncaoGlobal(ObjetoGlobal.value);
}

function MascaraTelefone(Value)
{
  Value     = Value.replace(/\D/g,"");
  var DDD           = "";
  var Parte1        = "";
  var Parte2        = "";
  
  for(iMasc = 0;iMasc < Value.length;iMasc++)
  {
        if(iMasc == 2)
        {
            DDD  = Value.substring(0,iMasc);      
            
        }
        else if(iMasc == 6)
        {
            Parte1  += Value.substring(2,iMasc);  
        }
        else if(iMasc == 8)
        {
            Parte2  += Value.substring(6,Value.length);  
        }
   } 
   
      
   if(DDD != "" && Parte1 == "")
   {
        Value = DDD +"-"+Value.substring(DDD.length,Value.length);
   }
   else if(DDD != "" && Parte1 != "" && Parte2 == "")
   {
        Value = DDD +"-"+Parte1+"-"+Value.substring((DDD.length+Parte1.length),Value.length);
   }
   else if(DDD != "" && Parte1 != ""&& Parte2 != "")
   {
         Value = DDD +"-"+Parte1+"-"+Parte2;
   }
   
   if(Value.length > 12)
   {
        Value = Value.substring(0,12);  
   }
  
     
  return Value;
}
