var ipgupdateurl = '';

function ipgreload() {
	$.post(ipgupdateurl, $("form.ipgform").serialize(), populate_ipgs, "json");
}

function populate_ipgs(data, textstatus) {
	if(textstatus != 'success' || data.length == 0) {
		return;
	}

	if(window['ipgpreupdate']) {
		ipgpreupdate();
	}

	var selectedipg;
	if(preselectedipg) {
		selectedipg = preselectedipg;
		preselectedipg = undefined;
	} else {
		selectedipg = $('input[name=ipg]:checked').val();
	}

	$('#ipglist li:not(:first)').remove();

	var tplrow = $('#ipglist li:first');

	$.each(data, function(i, ipg) {
			var ipgrow = tplrow.clone();
			ipgrow.appendTo('#ipglist');
			ipgrow.css("display", "");
			ipgrow.attr("ipgname", ipg.ipgCode);

			var radio = $('input[name=ipg]', ipgrow);
			radio
				.removeAttr("disabled")
				.attr('id', 'ipg_' + ipg.ipgCode)
				.val(ipg.ipgCode);
			$('label', ipgrow).attr('for', 'ipg_' + ipg.ipgCode);
			$('.field_ipg', ipgrow).html(ipg.ipgDisplayName);
			if(ipg.ipgInstruction != null){
				$('.field_description', ipgrow).html(ipg.ipgDescription + "<a target='_blank' href='" + ipg.ipgInstruction + "'>(Instruction)</a>");
			}else{
				$('.field_description', ipgrow).html(ipg.ipgDescription);
			}

			var img = $('img', ipgrow);
			img.attr('src', ipgimgbase + '/' + ipg.ipgCode.toUpperCase() + '_logo.png');
			img.click(function() { radio.attr('checked', 'checked'); });

			// handle range
			var transactionFees = ipg.estimates.transactionFeesMin.toFixed(2);
			var depositedAmount = ipg.estimates.depositedAmountMin.toFixed(2);
			if (ipg.estimates.transactionFeesRanged) {
				transactionFees += ' - ' + ipg.estimates.transactionFeesMax.toFixed(2);
				depositedAmount += ' - ' + ipg.estimates.depositedAmountMax.toFixed(2);
			}
			//var virtualCurrency;
			if(ipg.estimates.virtualCurrency != null){
				$('.virtualAmount', ipgrow).html("Rounded up to " + ipg.estimates.virtualAmount + " " + ipg.estimates.virtualCurrency.description);
			}

			$('.field_transactionFees', ipgrow).html(ipg.estimates.currency.code + " " + transactionFees);
			$('.field_depositedAmount', ipgrow).html(ipg.estimates.currency.code + " " + depositedAmount);
			$('.field_amountCharged', ipgrow).html(ipg.estimates.currency.code + " " + $.currency(ipg.estimates.amountCharged));
			
			// if(ipg.localCurrency && ipg.currency != ipg.estimates.localCurrency) {
				// $('.localAmount', ipgrow).show();
				// //$('.field_localAmount', ipgrow).html(ipg.localCurrency + " " + $.currency(ipg.localAmount));
				// $('.field_localAmount', ipgrow).html(ipg.estimates.localCurrency.code + " " + ipg.estimates.localAmount);
			// } else {
				// $('.localAmount', ipgrow).hide();
			// }
			if(ipg.estimates.currency.ocde != ipg.estimates.localCurrency.code) {
				$('.localAmount', ipgrow).show();
				//$('.field_localAmount', ipgrow).html(ipg.localCurrency + " " + $.currency(ipg.localAmount));
				$('.field_localAmount', ipgrow).html(ipg.estimates.localCurrency.code + " " + ipg.estimates.localAmount);
			} else {
				$('.localAmount', ipgrow).hide();
			}
			
	});

	if(!selectedipg) {
		selectedipg = 'PAYPAL';
	}

	$('#ipglist input[name=ipg][value=' + selectedipg + ']').attr("checked", "checked");

	if(window['ipgpostupdate']) {
		ipgpostupdate();
	}
}
