/*
Plugin Name: EmailXchange
Plugin URI: http://voidstarmedia.com
Description: A widget that asks for an email address before enabling a download link.
Version: 0.1
Author: Nick
Author URI: http://voidstarmedia.com
License: GPL2

Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
*/
 
function emailxchange_A(d){	
  
  if (jQuery.cookie("xchange_email_address")==null){
    d.children(".emailxchange_step1").fadeOut(200, function() {
    	d.children(".emailxchange_step2").fadeIn(200);
    	});
  }else{

    // DOWNLOAD MEDIA
    location.href=d.children(".emailxchange_step3").children(".emailxchange_destination").attr("href");
    
  }
  return false;
}

function emailxchange_validate(f){
  
  var d = f.parent().parent();
  var why = f.children(".emailxchange_why");
  var err = f.children(".emailxchange_err");
  var email = f.children(".emailxchange_input").val();
    
  var re = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
  
  if (re.test(email)){ 
    jQuery.cookie("xchange_email_address", email, { expires: 365*1000 });
    d.children(".emailxchange_step2").fadeOut(200, function(){
      d.children(".emailxchange_step3").fadeIn(200);
      });
    jQuery.post('/lists/?p=subscribe',{
    	p: "subscribe",
    	id: "1",
    	email: email,
    	emailconfirm: email,
    	htmlemail: "1",
    	'list[2]': "signup",
    	'listname[2]': "Voidstar Newsletter",
    	VerificationCodeX: "",
    	subscribe: "subscribe",
    	makeconfirmed: "1" });
    
  }else{
    
    why.fadeOut(100, function(){
	    err.fadeOut(100, function(){
    	    err.attr("innerHTML","Please try again.");
        	err.fadeIn(100);
      	});
	});
    
  }
  return false;
}

function emailxchange_formFocus(s){
  //if(s.value=='Enter Your Email Address'){
  if(s.value=='your email'){
  	s.value='';
  	s.style.color='#000000'
  }
}
function emailxchange_formBlur(s){
  if(s.value==''){
    //s.value='Enter Your Email Address';
    s.value='your email';
    s.style.color='#999999'
  }
}


// TEST CODE
function emailxchange_resetCookies(){
	jQuery.cookie("xchange_email_address", null);
	return false;
}
