Valuation of contracts with counterparty and funding risk under netting and collateral agreements