 Listens to events by the parent window
 ###
 $(window).on "message onmessage", (e) ->
+  if window.allowedOrigin? and e.originalEvent?.origin isnt window.allowedOrigin
+    return console.log('Received message from different origin', e.originalEvent?.origin)
   console.log 'Payment data received message', e.originalEvent?.data
   event = e.originalEvent.data.event
   args = e.originalEvent.data.arguments

###
Converts events sent by the component to messages to the parent window.
###
origin = '*'
$(window).on "sendAttachment.vtex", (e, attachmentName, attachmentData) ->
  parent.postMessage({"event": "sendAttachment.vtex", "arguments": [attachmentName, attachmentData]}, origin)

$(window).on "componentValidated.vtex", (e, validationResults) ->
  parent.postMessage({"event": "componentValidated.vtex", "arguments": [validationResults]}, origin)

$(window).on "startTransaction.vtex", (e, value, referenceValue, payments) ->
  parent.postMessage({"event": "startTransaction.vtex", "arguments": [value, referenceValue, payments]}, origin)

$(window).on "removeAccount.vtex", (e, accountId) ->
  parent.postMessage({"event": "removeAccount.vtex", "arguments": [accountId]}, origin)

$(window).on "authenticateUser.vtexid", (e, options) ->
  parent.postMessage({"event": "authenticateUser.vtexid", "arguments": [options]}, origin)

window.giftCardsProviders = ko.observableArray()
setup = (location, route) ->
  window.paymentData = new PaymentDataViewModel({route: route, location: location});
  window.allowedOrigin = location.protocol + '//' + location.host
  ko.components.register('payment-data', {
    viewModel: {instance: window.paymentData},
    template: paymentDataTemplate
