/* Lead Page Generator 2.0rc */

var LPG = {

  opened: false,
  is_in: false,
  is_extended: false,
  CountActive: true,
  CountStepper: -1,
  LeadingZero: true,
  SetTimeOutPeriod: 0,
  obj: new Object(),
  jdata: null,
  external_javascript: true,
  loadingExtendedOptinFormMessage: 'Going to step 2...<BR /><img src=\"LPG/images/bar.gif\">',
  submitOptinFormMessage: 'Submitting data...<BR /><img src=\"LPG/images/bar.gif\">',

  init: function() {
    if( e_active == '1' )
    {
      $().mousemove(function(e)
      {
        LPG.trackMouse(e)
      });
    }
    LPG.get_vars();
    LPG.bodyOnload();
  },

  bodyOnload: function() {
  },
  
  stat_clink: function(link_key)
  {
    $.ajax({
      type: "POST",
      url: "index.php",
      dataType: "json",
      data: 'mode=stat_clink&key=' + link_key,
      success: function(responsedata)
      {
        if( responsedata != -1 )
        {
          if( google_ok )
          {
            pageTracker._trackEvent(platform, responsedata['clink']['link_name'], project_name);
          }
          var URL = "";
          var query = "";
          if( responsedata['template']['project_template_type'] != undefined )
          {
            URL = responsedata['template']['project_template_name'];
            switch( responsedata['template']['project_template_type'] )
            {
              case "salespage" :
              case "orderpage" :
                query = "?optin=true";
                break;
            }
          }
          else
          {
            URL = responsedata['clink']['link_URL'];
          }
          window.location = URL+query;
        }
      }
    });
  },

  validate: function(type, e) {
    switch (type) {
      case "char":
        notallowed = "1234567890!@#$%^&*;:'\",.?/\\|~`_-(){}[]<>+= ";
        break;
      case "charnum":
        notallowed = "!@#$%^&*;:'\",.?/\\|~`_-(){}[]<>+= ";
        break;
      case "simpletext":
        notallowed = "1234567890!@#$%^&*;:'\",.?/\\|~`_-(){}[]<>+=";
        break;
      case "num":
        notallowed = "abcdefghijklmnopqrstuvwxyz!@#$%^&*;:'\",.?/\\|~`_-(){}[]<>+= ";
        break;
      default:
        notallowed = "";
    }
    var input = e.value.split("");
    for (i = 0; i < input.length; i++) {
      for (x = 0; x < notallowed.length; x++) {
        if (input[i] == notallowed.substr(x, 1)) {
          input.splice(i, 1);
          i--;
          break;
        }
      }
    }
    e.value = input.join("");
  },

  submit_optinform: function() {
    var mode = "";
    var oform = null;
    var post_data  = "";
    if( !LPG.is_extended )
    {
      mode = "optinform";
      oform = $('#optinform');
      post_data = $('#optinform').serialize();
    }
    else
    {
      mode = "extended_optinform";
      oform = $('#extended_optinform');
      post_data = $('#extended_optinform').serialize();
    }

    $.ajax({
      type: "POST",
      url: "index.php",
      dataType: "json",
      data: 'mode=' + mode + '&combo_string=' + combo_string + '&template_id=' + template_id + "&" + post_data,
      beforeSend : function()
      {
        $('#ostatus').html(LPG.submitOptinFormMessage);
      },
      success: function(jdata)
      {
        $('#ostatus').html("");
        var error_message = "";
        if( jdata['error'] )
        {
          for( i=0; i<jdata['errors'].length; i++ )
          {
            error_message = error_message + jdata['errors'][i] + "\n";
          }
          alert( error_message );
        }
        else
        {
          if( extended_optinform == "1" && !LPG.is_extended )
          {
            LPG.is_extended = true;
            $("#oform").html(jdata['template_code']);
          }
          else
          {
            if( google_ok )
            {
              var action_type = ( mode == "optinform" ? "Opt-in" : "Extended Opt-in" );
              pageTracker._trackEvent(platform, action_type, project_name);
            }
            oform.submit();
          }
        }
      }
    });
    return false;
  },

  get_vars: function() {
    var q = window.location.search.substring(1);
    var q2 = q.split('&');
    for (var i = 0; i < q2.length; i++) {
      var q3 = q2[i].split('=');
      LPG.obj[q3[0]] = q3[1];
    }
  },

  post_to_tags: function(text)
  {
    for( x in post )
    {
      text = text.replace('%'+x+'%', post[x]);
    }
    return text;
  },

  trackMouse: function(e)
  {
    $('#status').html(e.pageX +', '+ e.pageY);
    if( LPG.opened ) return true;
    if( typeof( window.innerWidth ) == 'number' )
    {
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    }
    else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    {
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }

    var posx = 0;
    var posy = 0;

    if(!e) var e = window.event;
    if(e.pageX || e.pageY)
    {
      posx = e.pageX;
      posy = e.pageY;
    }
    else if( e.clientX || e.clientY )
    {
      posx = e.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
    }

    if( posy < 10 + document.body.scrollTop )
    {
      if(!is_in) nrp++;
      is_in = true;
      LPG.opened = true;
      $.fn.colorbox({href: e_htmlfile, title: e_texttitle, width: e_width, height: e_height, iframe: true, open: true});
      return true;
    }
    else
    {
      is_in = false;
    }

    return true;

  },

  peel_away_code: function() {
    document.write('<style type="text/css">' + '#unlayout { top:-1000px; }');
    if (navigator.appVersion.indexOf('nd') != -1 || navigator.appName.indexOf('ro') != -1) {
      document.write('#cvlayout { top:' + top + '; right:' + right + '; } #unlayout { right:' + right + '; }');
    }
    else {
      document.write('#cvlayout { top:-1000px; right:' + right + '; } #unlayout { right:' + right + '; }');
    }
    document.write('</style>');
    document.write('<script language=\'javascript\'>function pguncover(command, args) {if (command == \'act_ppo\') {cvlayout.style.top = \'-1000px\';unlayout.style.top = top;}}function pgcover(command, args) {if (command == \'act_ppc\') {cvlayout.style.top = top;unlayout.style.top = \'-1000px\';}}function setpos(param1, param2, param3, param4, param5) {document.getElementById(param2).style.top = param3;document.getElementById(param4).style.top = param5;if (navigator.appName.indexOf(\'ro\') != -1) {window.resizeBy(-1, -1); window.resizeBy(1, 1);}}</script><div id="cvlayout" style="position:absolute; width:75px; height:75px; z-index:19001;">' + '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="cover" width="75" height="75">' + '<param name="allowScriptAccess" value="always"><param name="movie" value="LPG/flash/smalltopr.swf"><param name="wmode" value="transparent"><param name="FlashVars" value="smallimg=' + smallimg + '&scrolltxtsmall=' + scrolltxtsmall + '&txtsizesmall=' + txtsizesmall + '&textcolor=' + textcolorsmall + '">' + '<embed src="LPG/flash/smalltopr.swf" id="cover" name="cover" wmode="transparent" flashvars="smallimg=' + smallimg + '&scrolltxtsmall=' + scrolltxtsmall + '&txtsizesmall=' + txtsizesmall + '&textcolor=' + textcolorsmall + '"width="75" height="75" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' + '</object>' + '</div>' + '<div id="unlayout" style="position:absolute; height:600px; width:800px; z-index:20000;">' + '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="uncover" width="800" height="600">' + '<param name="allowScriptAccess" value="always"><param name="movie" value="LPG/flash/bigtopr.swf"><param name="wmode" value="transparent"><param name="FlashVars" value="bigimg=' + bigimg + '&urlval=' + urlval + '&window=' + owindow + '&scrolltxtbig=' + scrolltxtbig + '&txtsizebig=' + txtsizebig + '&textcolor=' + textcolorbig + '&yaxis=0">' + '<embed src="LPG/flash/bigtopr.swf" wmode="transparent" width="800" height="600" flashvars="bigimg=' + bigimg + '&urlval=' + urlval + '&window=' + owindow + '&scrolltxtbig=' + scrolltxtbig + '&txtsizebig=' + txtsizebig + '&textcolor=' + textcolorbig + '&yaxis=0" id="uncover" name="uncover" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' + '</object>' + '</div>');
    if (navigator.appName && navigator.appName.indexOf('ro') != -1 && navigator.userAgent.indexOf('nd') != -1 && navigator.userAgent.indexOf('3.1') == -1) {
      document.write('<script language="VBScript">\n' + 'On Error Resume Next\n' + 'Sub cover_FSCommand(ByVal command, ByVal args)\n' + 'Call pguncover(command, args)\n' + 'End Sub\n' + 'Sub uncover_FSCommand(ByVal command, ByVal args)\n' + 'Call pgcover(command, args)\n' + 'End Sub\n' + '<\/script>');
    }
  },

  countdown_timer: function(params,callback)
  {

    eval( "var params = " + params );
    var start = new Date(params['time']).getTime();
    var today = new Date().getTime() + (LPG.getTZOffset(params['timezone'])*60*60*1000);

    if( start < today )
    {
      eval("window.clearInterval(ct_"+params['id']+");");
      if( window.callback )
      {
        callback(); // call this function once this countdown timer has been completed
      }
      else if( params['url'] != undefined )
      {
        if( params['lightbox'] != undefined )
        {
          $.fn.colorbox({href: params['url'], width: params['width'], height: params['height'], iframe: true, open: true});
        }
        else
        {
          window.location = params['url'];
        }        
      }
    }

    var timeleft = start - today;

    var mill_in_sec = 1000;
    var mill_in_min = 60000;
    var mill_in_hour = 3600000;
    var mill_in_day = 86400000;

    var days = Math.floor(timeleft/mill_in_day);
    if( days < 0 ) days = 0;
    var hours = Math.floor(timeleft/mill_in_hour-(24*days));
    if( hours < 0 ) hours = 0;
    var minutes = Math.floor(timeleft/mill_in_min-(60*hours+(60*24*days)));
    if( minutes < 0 ) minutes = 0;
    var seconds = Math.floor((timeleft/mill_in_sec)-((60*minutes)+(60*60*hours)+(60*60*24*days)));
    if( seconds < 0 ) seconds = 0;

    if( params['text'] != undefined )
    {

      if( params['text'] != "" )
      {

        var text = params['text'];

        // replace are tags!

        text = text.replace(/%day%/, days);
        text = text.replace(/%hour%/, hours);
        text = text.replace(/%minute%/, minutes);
        text = text.replace(/%second%/, seconds);

        // add any post variables to our tags!
        text = LPG.post_to_tags(text);

        $("#ct_"+params['id']).html(text);

      }

    }

  },
  
  getTZOffset: function(timezone)
  {

    var d = new Date();
    var gmtHours = -d.getTimezoneOffset()/60;

    switch( timezone )
    {
      case "US/Eastern" :
        return -5-(gmtHours);
        break;
      case "US/Central" :
        return -6-(gmtHours);
        break;
      case "US/Mountain" :
        return -7-(gmtHours);
        break;
      case "US/Pacific" :
        return -8-(gmtHours);
        break;
      default:
        return -5-(gmtHours);
    }

  }
  
};