function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function getQSParameterByName(name, searchString) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(searchString);
    if (results == null)
        return "";
    else
        return results[1];
}

var postMessageToParent = function(binding_type) {
  if ( window.addEventListener ) {
    var target_origin = '*';
    var doc_height = j$('.page-body').height();
    parent.postMessage( {'binding_type': binding_type, 'doc_height': doc_height}, target_origin );
    console.log('parent post message: ' + binding_type + ' ' +  doc_height);
  }
};

var parent_flep_values = {};
var callParentWindowForFLEP = function() {
  var id_url_param_set = getQSParameterByName('id', window.location.href);
  var quick_donate_cookie_set = getCookie('apex__QD_GUID');
  if (id_url_param_set || quick_donate_cookie_set) {
    // if the id URL parameter is set OR the quick donate cookie is set, we don't want to update the 
    // FLEP field values with what is stored in the browser session storage

    return;
  }

  if ( window.location !== window.parent.location ) {
    parent.postMessage("send-flep","*");
  }
};

window.addEventListener('message', function (e) {
  var origin_ok = false;
  var id_url_param_set = getQSParameterByName('id', window.location.href);
  var quick_donate_cookie_set = getCookie('apex__QD_GUID');
  if (id_url_param_set || quick_donate_cookie_set) {
    // if the id URL parameter is set OR the quick donate cookie is set, we don't want to update the 
    // FLEP field values with what is stored in the browser session storage
    return;
  }

  if (/force\.com$/.test(e.origin)) {
    origin_ok = true;
  } else if (/liberal\.ca$/.test(e.origin)) {
    origin_ok = true;
  } else if (/wordpress\.test$/.test(e.origin)) {
    origin_ok = true;
  } else if (/liberal\.test$/.test(e.origin)) {
    origin_ok = true;
  }
  if (!origin_ok) {
    return;
  }
  var payload = JSON.parse(e.data);

  switch(payload.method) {
    case 'set-flep-values':
    parent_flep_values = payload.data;
    populateFLEPFromSessionStorage();
    break;
  }

}, false);

var populateFLEPFromSessionStorage = function() {
  console.log('in populate session storage');
  if (parent_flep_values && !j$.isEmptyObject(parent_flep_values)) {
    j$('input[id$="firstname"]').val(parent_flep_values.sf_first_name);
    j$('input[id$="lastname"]').val(parent_flep_values.sf_last_name);
    j$('input[id$="email"]').val(parent_flep_values.sf_email);
    j$('input[id$=postalcode]').val(parent_flep_values.sf_postal_code);

    var phone = parent_flep_values.sf_phone;
    phone     = phone.replace(/\D/g,'');
    j$('input[id$=phone]').val(phone);


    console.log('populated fields');
  }
};