//===============================================
// Cartões
// Monta somente a listagem dos cartões disponíveis para posterior montagem da tabela de parcelamento
var oListCards = null;
var listCardsIndex = 0;
var selectedCard = 0;
var oListParcel = new Array()

//Resgata o ID (do array) dos cartões
function getCardIDByItem(item,value){
	var retorno = -1;
	for (i=0;i<oListCards.length;i++){
		if (oListCards[i][item]==value){
			retorno = i;
			break;
		}
	}
	return retorno;
}

function getParcelData(sku, selectedMethod){
	var retorno = null;
	//try{
		for (var io=0;io<oListParcel.length;io++){
			if ((oListParcel[io].sku==sku) && (oListParcel[io].jsData)){
				retorno = oListParcel[io].jsData;
				break;
			}
		}
	//}catch(e){}
	return retorno;
}

//Função que terá como parametro o texto no padrão JSON, e define o objeto oListImages com este texto.
function setCardList(jsonList, desconto){
	//try {
		oListCards = jsonList;
		oDesconto = desconto;
		if (oListCards.length > 0){
			selectedCard = oListCards[0].control_method_id;
			montaCardsHTML();
			loadParcel(oDesconto);
		}
	//}catch(e){}
}

//Função que terá como parametro o texto no padrão JSON, e define o objeto oListImages com este texto.
function setCardListKit(jsonList, desconto){
	//try {
		oListCards = jsonList;
		oDesconto = desconto;
		if (oListCards.length > 0){
			selectedCard = oListCards[0].control_method_id;
			//montaCardsHTML();
			//loadParcelKit(document.getElementById('VV').value);
		}
	//}catch(e){}
}

function cardClick(id){
	var iItem = getCardIDByItem('control_method_id',id);
	if (iItem != -1){
		document.getElementById('li'+ selectedCard).attributes['class'].value = '';
		selectedCard = id;
		document.getElementById('li'+ selectedCard).attributes['class'].value = 'selecionado';
		//Dispara carregamento da tabela de parcelamento
		loadParcel(oDesconto);
	}
}

function montaCardsHTML(){
	var objCards = document.getElementById('cartao_aba')

	var HTML = ''
	
	if (oListCards.length > 0){
		HTML = HTML + '<ul>'
		for (i=0;i<oListCards.length;i++){	
			HTML = HTML + '<li id="li' + oListCards[i].control_method_id + '"'
			if (oListCards[i].control_method_id == selectedCard){
				HTML = HTML + ' class="selecionado"'
			}else{
				HTML = HTML + ' class=""'
			}
			HTML = HTML + '><a href="javascript:cardClick(\'' + oListCards[i].control_method_id + '\')"><img src="'+ iTemplateFolder + '/' + oListCards[i].method_logo_mini + '" border="0"></a></li>'
		}
		HTML = HTML + '</ul>'
	}
	
	objCards.innerHTML = HTML;
	
	if(document.getElementById(selectedCard)){
		document.getElementById(selectedCard).checked = true;
	}
}

function montaParcelHTML(jsDados){
	if (jsDados){
		var iData = jsDados.data[selectedCard]
		try{		
			var HTML = '';
			HTML = HTML + ''
			HTML = HTML + '<table cellpadding="0" cellspacing="1" border="0">'
			HTML = HTML + '<tr>'
			HTML = HTML + '<td class="tit">' + jsDados.parcel + '</td>'
			HTML = HTML + '<td class="tit">' + jsDados.price + '</td>'
			HTML = HTML + '<td class="tit">' + jsDados.parcel + '</td>'
			HTML = HTML + '<td class="tit">' + jsDados.price + '</td>'
			HTML = HTML + '</tr>'
			
			for (var i=0;i<iData.length;i++){
				HTML = HTML + '<tr>'
				HTML = HTML + '<td class="parc1">'+ iData[i].parcel +'</td>'
				HTML = HTML + '<td class="parc2">'+ iData[i].price +'</td>'
				if ((i+1)<iData.length){
					i = i + 1
					HTML = HTML + '<td class="parc1">'+ iData[i].parcel +'</td>'
					HTML = HTML + '<td class="parc2">'+ iData[i].price +'</td>'
				}else{
					HTML = HTML + '<td class="parc1">&nbsp;</td>'
					HTML = HTML + '<td class="parc2">&nbsp;</td>'
				}
				HTML = HTML + '</tr>'
			}
			HTML = HTML + '</table>'
			document.getElementById('cartao_parcelamento').innerHTML = HTML;
		}catch(e){}
	}
}

function carregandoParcelamento(visible){
	try{
		if (visible){
			document.getElementById('cartao_parcelamento').innerHTML = 'Carregando...';
		}else{
			document.getElementById('cartao_parcelamento').innerHTML = '';
		}
	}catch(e){}
}

function loadParcel(oDesconto){
	var msgErro = 'Erro ao processar a tabela de parcelamento.';
	
	var parcelData = getParcelData(oListVariants[listVariantIndex].sku,selectedCard)
	carregandoParcelamento(true);
	if (parcelData != null){
		montaParcelHTML(parcelData);
	}else{
		cAjax.onStateChange = function(classState,httpState,retornoHTML,retornoXML){
			if(httpState == 200) {
				var jsDados = eval('(' + retornoHTML.toString() + ')');
				
				if (jsDados.err.number != undefined){
					if (jsDados.err.number != '0'){
						alert(jsDados.err.description);
					}else{
						oListParcel.push({'sku':oListVariants[listVariantIndex].sku,'jsData':jsDados});
						montaParcelHTML(jsDados);
					}
				}
			}else{
				alert(msgErro);
				carregandoParcelamento(false);
			}
			
		}
		cAjax.load("../product_calc_parcel_normal.asp","card=" + selectedCard + '&sku=' + oListVariants[listVariantIndex].sku + '&desconto=' + oDesconto,"GET")
	}
}

function loadParcelKit(ValorKit){
	var msgErro = 'Erro ao processar a tabela de parcelamento.';
	
	var parcelData = getParcelData(oListVariants[listVariantIndex].sku,selectedCard)
	carregandoParcelamento(true);
	/*if (parcelData != null){
		montaParcelHTML(parcelData);
	}else{*/
		cAjax.onStateChange = function(classState,httpState,retornoHTML,retornoXML){
			//document.getElementById('erro').innerHTML = document.getElementById('erro').innerHTML + retornoHTML;
			if(httpState == 200) {
				var jsDados = eval('(' + retornoHTML.toString() + ')');
				
				if (jsDados.err.number != undefined){
					if (jsDados.err.number != '0'){
						alert(jsDados.err.description);
					}else{
						oListParcel.push({'sku':oListVariants[listVariantIndex].sku,'jsData':jsDados});
						montaParcelHTML(jsDados);
					}
				}
			}else{
				alert(msgErro);
				carregandoParcelamento(false);
			}
			var cAjax = '';
			
		}
	
		cAjax.load("../product_calc_parcel_kit.asp","card=" + selectedCard + '&sku=' + document.getElementById('pf_id').value + '&desconto=' + oDesconto +'&VV=' + ValorKit,"GET")
	//}
}

function showParcel(){
	
	var cartao = document.getElementById('cartao_credito')
	var cboAttrValue1 = document.getElementById('cboAttrValue1')	
	var cboAttrValue2 = document.getElementById('cboAttrValue2')
	try {
		if (cartao.style.visibility == 'visible'){
			cartao.style.visibility = 'hidden';
			//cboAttrValue1.style.visibility = 'visible';
			//try{ cboAttrValue2.style.visibility = 'visible'; }catch(e){};
		}else{
			cartao.style.visibility = 'visible';
			//cboAttrValue1.style.visibility = 'hidden';
			//try{ cboAttrValue2.style.visibility = 'hidden'; }catch(e){};
		}
		if (isIE()) {
			cartao.style.top = (iMouse.y - 190) + 'px';
			cartao.style.left = (iMouse.x - 580) + 'px';
		}else {
			cartao.style.top = (iMouse.y) + 'px';
			cartao.style.left = (iMouse.x - 430) + 'px';
		}
	}catch(e){}

}

function showParcelKit(ValorKit){
	
	montaCardsHTML();
	loadParcelKit(ValorKit);
		
	var cartao = document.getElementById('cartao_credito')
	var cboAttrValue1 = document.getElementById('cboAttrValue1')	
	var cboAttrValue2 = document.getElementById('cboAttrValue2')
	try {
		if (cartao.style.visibility == 'visible'){
			cartao.style.visibility = 'hidden';
			//cboAttrValue1.style.visibility = 'visible';
			//try{ cboAttrValue2.style.visibility = 'visible'; }catch(e){};
		}else{
			cartao.style.visibility = 'visible';
			//cboAttrValue1.style.visibility = 'hidden';
			//try{ cboAttrValue2.style.visibility = 'hidden'; }catch(e){};
		}
		if (isIE()) {
			cartao.style.top = (iMouse.y - 115) + 'px';
			cartao.style.left = (iMouse.x - 580) + 'px';
		}else {
			cartao.style.top = (iMouse.y) + 'px';
			cartao.style.left = (iMouse.x - 430) + 'px';
		}
	}catch(e){}
	

}

// Fim Cartões
//===============================================
