var __checkoutWizard = null;

var FunmoCheckoutWizard = function(appId, contentTypeId, mediaId, options) {
    __checkoutWizard = this;
    this.appId = appId;
    this.contentTypeId = contentTypeId,
    this.mediaId = mediaId;
    if (typeof options == "undefined")
       options = {}
    this.options = options;
    this.eventHandlers = {};
}

FunmoCheckoutWizard.prototype.load = function(container){
    this.container = (container == null) ? "remote_checkout_box_inner" : container;
    var wizard = document.createElement('div');
    wizard.setAttribute('id', 'checkout_wizard');

  if (this.container.appendChild) {
    this.container.appendChild(wizard);
    
    new Ajax.Updater('checkout_wizard', '/checkouts/new', {
      asynchronous: true,
      evalScripts: true,
      parameters: {
        'app_id': this.appId,
        'content_type_id': this.contentTypeId,
        'media_id': this.mediaId,
        'user_id': (this.options['user_id'] == null)? -1: this.options['user_id'],
        'phone_number': this.options['phone_number'],
        'campaign_id': (this.options['campaign_id'] == null)? -1: this.options['campaign_id'],
        'partner_id': (this.options['partner_id'] == null)? -1: this.options['partner_id'],
        'callback_data': (this.options['callback_data'] == null)? '': this.options['callback_data'],
        'source_url': (this.options['source_url'] == null)? '': this.options['source_url'],
        'source_referer': (this.options['source_referer'] == null)? '': this.options['source_referer'],
        'client_ip_address': (this.options['client_ip_address'] == null)? '': this.options['client_ip_address'],
        'transactional': this.options['transactional']
      }
    });
  }
}

FunmoCheckoutWizard.prototype.addListener = function(event, handler){
    this.eventHandlers[event] = handler;
}

function __checkoutWizardTrigger(event){
    var handler = __checkoutWizard.eventHandlers[event];
    if (handler != null && Object.isFunction(handler)) 
      handler();    
}

// For phone number fields 
var __downStrokeField;
function autojump(fieldName,nextFieldName,fakeMaxLength) {
    var myForm=document.getElementById('checkout_form');
    var myField=myForm.elements[fieldName];
    var nextField=myForm.elements[nextFieldName];
    
    if (myField.maxLength == null)
       myField.maxLength=fakeMaxLength;
    
    myField.onkeydown=autojump_keyDown;
    myField.onkeyup=autojump_keyUp(nextField);
}

function autojump_keyDown() {
    this.beforeLength=this.value.length;
    __downStrokeField=this;
}

function autojump_keyUp(nextField) {
    return (function(){
        if (
           (this == __downStrokeField) && 
           (this.value.length > this.beforeLength) && 
           (this.value.length >= this.maxLength)
           )
           nextField.focus();
        __downStrokeField=null;
    });
}

function _pincode_text_onfocus(objName) {
    obj = document.getElementById(objName);
    if (obj && obj.className == 'pin_code_inactive') {
        obj.value = '';
        obj.className = 'pin_code_active';
        obj.maxLength = 6;
    }
}

function timeout_submit(form_id, time) {
  element = $(form_id);
  setTimeout("element.onsubmit()", time);
}

function set_border_color(obj) {
  obj.style.border = '1px solid #0099dd';
}

function import_mail(client, title, url, type, media_id, app_id, category_id, content_type_id, tracking_id) {
  new Ajax.Updater('share-box', "/m/share/import_mail",{
    asynchronous: true, evalScripts: true, 
    parameters: {client: client,
                 title: title,
                 url: url,
                 type: type,
                 media_id: media_id,
                 app_id: app_id,
                 category_id: category_id,
                 content_type_id: content_type_id,
                 tracking_id: tracking_id
                }        
  });
  return false;
}

function getEmails() {
  inviteForm = $('send-invitations');
  checkboxes = inviteForm.contacts;
  emailList = "";

  if (checkboxes != null && checkboxes.tagName == 'INPUT') {
    if (checkboxes.checked) {
      inviteForm.list.value = checkboxes.value;
      return true;
    } else {
      $('choose-error').style.display = 'block';
      return false;
    }
  } else {
    for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].checked) {
        emailList += checkboxes[i].value + ',';
      }
    }

    if (emailList == "") {
      $('choose-error').style.display = 'block';
      return false;
    }

    inviteForm.list.value = emailList;
    return true;
  }
}

function checkAllPurchase() {
  inviteForm = $('send-invitations');
  checkboxes = inviteForm.contacts;
  checkAllBox = inviteForm.check_all;
  checked = checkAllBox.checked;

  if (checkboxes != null && checkboxes.tagName == 'INPUT') {
    checkboxes.checked = checked;
  } else {
    for (var i = 0; i < checkboxes.length; i++) {
      checkboxes[i].checked = checked;
    }
  }
}
