inlb = 0;

function next_location() {

  var new_domain = 'http://www.tycoonu.com';
  //var new_domain = 'http://signups.mcode.biz';

  var next = window.location.href.split('#')[0];

  if(next.indexOf(new_domain) != -1) {

    var pieces = next.split('/');
    var tmpnext = '';
    for(var i = 0; i < pieces.length; ++i) {

      if(i > 0) tmpnext += '/';
      if(i == (pieces.length - 1)) tmpnext += 'thankyou/';
      tmpnext += pieces[i];
    }
    next = tmpnext;
  }
  else {
    
  }

  return next;
}

function check_email(campaign, email, skip, no_move) {

  var url = campaign;
  var pars = 'a=check_email&email=' + email + '&skip=' + skip + '&no_move=' + no_move + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'DUPLICATE') return false;

    if(n.responseText == 'OK') window.location.href = next_location(); //window.location.href.split('#')[0];
    else if(n.responseText == 'GA_THANKYOU') {

      var ty = window.location.href.replace(/#/g, '');
      if(ty.substr(ty.length - 1, 1) != '/') ty += '/';
      ty += 'thankyou.php';
      window.location.href = ty;
    }
    else if(n.responseText != 'STAY') cpn_lb_show('problem with email address', n.responseText);
    }
  }
  );
}

function check_email_ga(campaign, email, skip, no_move) {

  var url = campaign;
  var pars = 'a=check_email&email=' + email + '&skip=' + skip + '&no_move=' + no_move + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'DUPLICATE') return false;

    if((n.responseText == 'OK') || (n.responseText == 'STAY')) {

      window.location.href = next_location(); //window.location.href;
      //Element.hide('conf_notrcvd_tryagain');
      //Element.show('conf_notrcvd_thankyou');
    }
    else if(n.responseText == 'GA_THANKYOU')
      window.location.href = window.location.href.replace(/#/g, '') + '/thankyou.php';
    else cpn_lb_show('problem with email address', n.responseText);
    }
  }
  );
}

function check_email_ga_alt(campaign, email, skip, no_move, session_id) {

  var url = campaign;
  var pars = 'a=check_email&email=' + email + '&skip=' + skip + '&no_move=' + no_move + '&ssid=' + session_id + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'DUPLICATE') return false;

    if((n.responseText == 'OK') || (n.responseText == 'STAY')) {

      window.location.href = next_location(); //window.location.href;
      //Element.hide('conf_notrcvd_tryagain');
      //Element.show('conf_notrcvd_thankyou');
    }
    else if(n.responseText == 'GA_THANKYOU')
      window.location.href = window.location.href.replace(/#/g, '') + '/thankyou.php';
    else cpn_lb_show('problem with email address', n.responseText);
    }
  }
  );
}

function log_confirmation(campaign, path) {

  var url = campaign;
  var pars = 'a=log_confirmation&path=' + path + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'OK') window.location.href = window.location.href.split('#')[0];
    else if(n.responseText != 'STAY') cpn_lb_show('problem with confirmation', n.responseText);
    }
  }
  );
}

function log_confirmation_alt(campaign, path, session_id) {

  var url = campaign;
  var pars = 'a=log_confirmation&path=' + path + '&ssid=' + session_id + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'OK') window.location.href = window.location.href.split('#')[0];
    else if(n.responseText != 'STAY') cpn_lb_show('problem with confirmation', n.responseText);
    }
  }
  );
}

function log_other_email(campaign, other_email, friend1_name, friend1, friend2_name, friend2, friend3_name, friend3, friend4_name, friend4, friend5_name, friend5, skiplevel) {

  var url = campaign;
  var pars = 'a=log_other_email&other_email=' + other_email + '&friend1_name=' + friend1_name + '&friend1=' + friend1 + '&friend2_name=' + friend2_name + '&friend2=' + friend2 + '&friend3_name=' + friend3_name + '&friend3=' + friend3 + '&friend4_name=' + friend4_name + '&friend4=' + friend4 + '&friend5_name=' + friend5_name + '&friend5=' + friend5 + '&skip=' + skiplevel + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    if(n.responseText == 'OK') window.location.href = window.location.href.split('#')[0];
    else cpn_lb_show('problem with additional email', n.responseText);
    }
  }
  );
}

function cpn_lb_show(caption, msg) {

  $('cpn_lb_caption').innerHTML = caption;
  $('cpn_lb_message').innerHTML = msg;

  $('trans_layer').className = 'opaque';
  $('cpn_lb_main').style.display = 'block';
  $('cpn_lb_message').style.display = 'block';
  cpn_lb_center();
  Effect.Shake('cpn_lb_main');
}

function cpn_lb_hide() {

  Effect.BlindUp('cpn_lb_main');
  setTimeout('$(\'trans_layer\').className = \'\';', 1000);
}

function getStyle(el, style) {

  if(!document.getElementById) return;

  var value = el.style[style];

  if(!value) {

    if(document.defaultView)
      value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);

    else if(el.currentStyle)
      value = el.currentStyle[style];
  }

  return value;
}

function cpn_lb_center() {

  var sw = getWindowWidth();
  var sh = getWindowHeight();
  var lbw = parseInt(getStyle($('cpn_lb_main'), 'width'));
  var lbh = parseInt(getStyle($('cpn_lb_main'), 'height'));
  //var lbw = parseInt($('cpn_lb_main').style.width);
  //var lbh = parseInt($('cpn_lb_main').style.height);

  var lbh = 300;

  $('cpn_lb_main').style.left =  + ((sw / 2.0) - (lbw / 2.0)) + 'px';
  $('cpn_lb_main').style.top = (getScrollTop() + ((sh / 2.0) - (lbh / 2.0))) + 'px';
}

function getScrollTop() {

  if(self.pageYOffset) return self.pageYOffset;
  if(document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
  if(document.body) return document.body.scrollTop;
  return 0;
}

function getWindowWidth() {

  if(self.innerWidth) return self.innerWidth;
  if(document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
  if(document.body) return document.body.clientWidth;
  return 0;
}

function getWindowHeight() {

  if(self.innerHeight) return self.innerHeight;
  if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
  if(document.body) return document.body.clientHeight;
  return 0;
}

function showBusy() {

  var new_top, new_left;

  if($('busyInd')) {

    //$('trans_layer').className = 'opaque';

    new_top = getScrollTop() + (getWindowHeight() / 2.0) - (parseInt($('busyInd').style.height) / 2.0);
    new_left = (getWindowWidth() / 2.0) - (parseInt($('busyInd').style.width) / 2.0);
    $('busyInd').style.top = new_top + 'px';
    $('busyInd').style.left = new_left + 'px';
    Element.show('busyInd');
  }
}

function hideBusy() {

  if($('busyInd')) {

    //if(!Element.visible('cpn_lb_main')) $('trans_layer').className = '';
    Element.hide('busyInd');
  }
}

function submit_eb_form(campaign) {

  $('eb_email').disabled = false;

  var url = campaign;
  var pars = 'a=eb_form&' + Form.serialize('eb_form') + '&ms=' + new Date().getTime();
  var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onComplete: function(n) {

    //if(n.responseText == 'OK') {
    
      Effect.SlideUp('eb_presubmit_area');
      Effect.SlideDown('eb_submitted_area');
    //}
  }});
}

Ajax.Responders.register({onCreate : showBusy, onComplete : hideBusy });
