// 1.5 - { DOMAIN COOKIE FIX } ( July 31st, 2008 )

function get_extended_optinform() {

  var post_data = $('optinform').serialize();

  new Ajax.Request( 'index.php', {
  parameters : 'project_id='+project_id+'&project_key='+project_key+'&template_type=6&template_id='+extended_optinform_template+'&'+post_data,
  evalJSON : true,
  onLoading : function() {hide_optinform();$('ext_oform').innerHTML = 'Going to step 2...<BR /><img src=\"images/bar.gif\">';},
  method : 'POST',
  onComplete : function(r) { $('ext_oform').innerHTML = r.responseText; }

  });
  
  return false;
  
}

function hide_optinform() {
  $("optinform").style.display="none";
}

function submit_optinform() {

  if( is_extended ) {
    
     if( !checkform( $("extended_optinform" ) ) ) {
    
      return false;
      
    }
    
  } else {
    
    if( !checkform( $("optinform" ) ) ) {
    
      return false;
      
    }
  
  }
  
  if( extended_optinform == "1" && !is_extended ) {
  
    // show extended optinform
    get_extended_optinform();
    is_extended = true;
    return false;
    
  } else {
  
    /*
   1. submit optin form
   2. check auto responder conditions
   3. fetch correct auto responder form code
   4. place response code into o_form layer
   5. submit form
   
    */
 
    var post_data = $('optinform').serialize();
    var post_data2 = "";
    
    if( extended_optinform == "1" ) {
    
      var post_data2 = $('extended_optinform').serialize();
      
    }
 
    new Ajax.Request( 'index.php', {
    parameters : 'sub=&project_id='+project_id+'&project_key='+project_key+'&redir='+redir+'&wh='+wh+'&cd='+cd+'&combo='+combo+'&template_id='+template_id+'&credit_combo='+credit_combo+'&u_id='+u_id+'&r='+escape(r)+'&template_type='+template_type+'&ip='+ip+'&Contact0Leadsource='+leadsource+'&Contact0_SEOType='+ana_type+'&Contact0_Keywords='+ana_keywords+'&Contact0_Domain='+ana_domain+'&escape(user_agent)='+user_agent+'&'+post_data+'&'+post_data2,
    evalJSON : true,
    method : 'POST',
    onLoading : function() { $('Submit').disabled='disabled';$('ext_oform').innerHTML = 'Submitting data...<BR /><img src=\"images/bar.gif\">' },
    onComplete : function(r) { $('o_form').innerHTML = r.responseText; create_cookie( 'optin_'+project_id,'true');create_cookie( 'status_'+project_id,'OKAY'); document.optin_form.submit(); }
 
    });
   
    return false;
  
  }
   
}
      
function openWindow() {
    
    opened = true; // only will open once, unless browser is refreshed...
    return GB_show( e_texttitle, e_htmlfile, e_height,e_width );
    
}

function get_vars() {
  
  var obj = new Object();
  var q = window.location.search.substring(1);
  var q2 = q.split('&');
  
  for( var i=0; i<q2.length; i++ ) {
  
    var q3 = q2[i].split('=');
    obj[q3[0]] = q3;
  
  }
  
  return obj;

}

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

}

function get_google_cookie() {

  var cookie = readCookie('__utmz');
  var ppc = false;
  
  if( obj['ppc'] != undefined ) {
  
    ana_type = 'ppc';
    ppc = true;
  
  }
  
  if( cookie != null ) {
  
    var parts = cookie.split( '|' );
    
    for( i=0; i<parts.length; i++ ) {
    
      var v = parts[i].split( '=' );
      
      if (v[0] == 'utmcsr') { 
        
        ana_domain = v[1]; 
        
      }
      
      if (v[0] == 'utmcmd') { 
      
        if( !ppc ) {
          
          ana_type = v[1];
          
        }
        
      }
      
      if (v[0] == 'utmctr') { 
       
        ana_keywords = v[1]; 
        
      }
    
    }
    
  }
  
}

function checkform( f ) {

  var error_message = new Object();
  var ok = true;
  
  for( z=0; z<f.length; z++ ) {
  
    var type = f[z].type;
    var name = "";
    
    if( f[z].name != undefined ) {
      
      name = f[z].name;
      
    } else if( f[z].id != undefined ) {
      
      name = f[z].id;
      
    } else {
      
      alert( "extended_optinform error { must set attrubute NAME or ID }" );
      ok = false;
      break;
      
    }
  
    if( type == 'select-one' || type == 'checkbox' || type == 'radio' || type == 'text' ) {
    
      error_message[name] = true;
      
    }
  
  }
  
  if( ok ) {
  
    for( i=0; i<f.length; i++ ) {
        
      var value = f[i].value;
      var type = f[i].type;
      var name = "";
      
      if( f[i].name != undefined ) {
        
        name = f[i].name;
        
      } else if( f[i].id != undefined ) {
        
        name = f[i].id;
        
      }
          
      if( type == 'select-one' ) {
        
        if(f[i].options[f[i].selectedIndex].value != '-1' ) {
          
          error_message[name] = false;			
          
        }
        
      } else if( type == 'checkbox' || type == 'radio' ) {
      
        if( f[i].checked == true ) {
          
          error_message[name] = false;
          
        }
        
      } else if( type == 'text' ) {
        
        if( name == "email" ) {
        
          var RegExPattern = /^.+@.+\..{2,3}$/;
      
          if( value.match(RegExPattern) ) {
      
            error_message[name] = false;
            
          }
          
        } else {
        
          if( value != '' ) {
          
            error_message[name] = false;
            
          }
        
        }
        
      }
    
    }
    
    var error = false;
    var message = 'Please correct the following problems...\r\n';
    
    for( x in error_message ) {
    
      if( error_message[x] ) {
      
        message += x + '\r\n';
        error = true;
        
      }
    
    }
    
    if( error ) {
    
      alert( message );
      return false;
    
    }
    
    return true;

  }
  
  return false;
  
}
				
function callInProgress(xmlhttp) {
  switch (xmlhttp.readyState) {
    case 1:
    case 2:
    case 3:
      return true;
      break;
    default:
      return false;
      break;
  }
}
    
    
function showFailureMessage() {
  
  // we cannot connect to the LPG server, so we
  // will redirect to the sales page
  // no optins will be counted, and if an
  // extended optinform is being used, it
  // will fail and the user will be redirected
  // to the sales page...
  
  create_cookie( 'optin_'+project_id,'true');
  create_cookie( 'status_'+project_id,'FALIED');
  window.location='index.php?template_type=2';
  
}
    
Ajax.Responders.register({onCreate:function (request) {request.timeoutId = window.setTimeout(function () {if (callInProgress(request.transport)) {request.transport.abort();showFailureMessage();if (request.options.onFailure) {request.options.onFailure(request.transport, request.json);}}}, 10000);}, onComplete:function (request) {window.clearTimeout(request.timeoutId);}});

function create_cookie( c_name, c_data ) {

  var exdate = new Date();
  exdate.setDate( exdate.getDate() + 365 );
  document.cookie = c_name + '=' + escape( c_data ) + ';path=/;expires=' + exdate.toGMTString() + ';domain=' + server_domain;

}

function exit_hover_load() {
  
  document.onmousemove = mmove;				

}

function mmove(e) {

  if(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;
    openWindow();
    return true;

  }

  else {

    is_in = false;

  }

  return true;

}

function peel_away_code() {
  
  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('<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="smalltopr.swf"><param name="wmode" value="transparent"><param name="FlashVars" value="smallimg='+smallimg+'&scrolltxtsmall='+scrolltxtsmall+'&txtsizesmall='+txtsizesmall+'&textcolor='+textcolorsmall+'">'+'<embed src="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="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="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>');}
  
}

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); 
	}
}
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 redirect() {
  if( countdown_gb_yes == "true" ) {
    GB_show( "", countdown_redirect_url, countdown_gb_height,countdown_gb_width );
  } else {
    window.location = countdown_redirect_url;
  }
}

function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return "<b>" + s + "</b>";
}

function CountBack(secs) {
  if (secs < 0) {
    if( countdown_redirect_yes == "true" ) {
      if( countdown_delay != "0" ) {
        document.getElementById("cntdwn").innerHTML = FinishMessage;
        setTimeout( redirect, countdown_delay*1000 );
      } else {
        redirect();
      }
    } else {
      document.getElementById("cntdwn").innerHTML = FinishMessage;
    }
    return;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}