
/* ==================== DATA SECTION - ENTER YOUR DATA HERE ==================== */

/* Put your Facebook Application ID here */
facebook_app_id = "234853016531561";

/* Your iContact listID, specialID, clientID and formID (must be taken from an iContact form!) */
iContact_listID = "37556";
iContact_specialID = "NMEL";
iContact_clientID = "939770";
iContact_formID = "4125";

/* The user will be redirected to this URL if the signup process was successful */
iContact_thank_you_url = "http://www.propennystocks.com/activate.php";

/* The user will be redirected to this URL if the signup process was NOT successful */ 
iContact_error_url = "http://www.icontact.com/www/signup/error.html";

/* 0 = Disable double opt-in, 1 = Enable double opt-in */
iContact_enableDoubleOptIn = "1";

/* ============================= END DATA SECTION ============================== */

/* Loads the Facebook API */
function loadFBAsync(){
	window.fbAsyncInit = function() {
		FB.init({appId: facebook_app_id, status: true, cookie: true, xfbml: true,oauth:true});
	};
	
	/* Loads Facebook API */
	(function() {
		var e = document.createElement('script'); 
		e.async = true;
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	    document.getElementById('fb-root').appendChild(e);
	}());
}

/* Called when the user Click the button */
function loginWithFacebook(){	
	FB.login(function(response) {
	  if (response.authResponse) {
	  	FB.api('/me', function(response) {
			/* Add user's data to iContact */
			addToiContact(response.first_name, response.last_name, response.email);
		});
	  } else {
	    // user is not logged in
	  }
	}, {scope:"email"});
}

/* Sends the data to iContact */
function addToiContact(first_name, last_name, email){
	
	data =
	{	
		"redirect": iContact_thank_you_url,
		"errorredirect": iContact_error_url,
		"fields_email": email,
		"fields_fname": first_name,
		"fields_lname": last_name,
		"clientid": iContact_clientID,
		"listid": iContact_listID,
		"formid": iContact_formID,
		"reallistid": 1,
		"doubleopt": iContact_enableDoubleOptIn,
		"Submit": "Submit"
	}
	data["specialid:" + iContact_listID] = iContact_specialID;
	
	post_to_url("https://app.icontact.com/icp/signup.php", data, "POST")
}

/* Helper function for POSTing data */
function post_to_url(path, params, method) {
    method = method || "post";

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}

