<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kohactive &#187; API</title>
	<atom:link href="http://www.kohactive.com/html/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kohactive.com/html</link>
	<description>kohactive  interactive design and marketing</description>
	<lastBuildDate>Mon, 23 Aug 2010 21:26:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AS3 MailChimp Library</title>
		<link>http://www.kohactive.com/html/labs/actionscript-3-0-and-mailchimps-api/</link>
		<comments>http://www.kohactive.com/html/labs/actionscript-3-0-and-mailchimps-api/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:32:23 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[MailChimp]]></category>

		<guid isPermaLink="false">http://www.kohactive.com/html/?p=123</guid>
		<description><![CDATA[One of our latest projects required MailChimp integration through Flash AS3, while we couldn't find anything that worked properly, we found a nice and easy way to build our own class.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.kohactive.com/html/wp-content/uploads/mailchimp.jpg"><img class="alignnone size-full wp-image-124" title="mailchimp" src="http://www.kohactive.com/html/wp-content/uploads/mailchimp.jpg" alt="" width="535" height="235" /></a></p>
<p>While building our awesome new website, we wanted to ensure that users could easily subscribe to our newsletter through the Flash version, without having to be redirected anywhere else. Convenience is king, and we wanted to deliver it!</p>
<p>We use <a href="http://www.mailchimp.com/">MailChimp</a>, the best email marketing platform available! And luckily, they have a great API to work with. Through Flash, we were able to add subscribers to our list and also return any results, i.e. error or success.</p>
<p>While searching Google I found an article by <a href="http://christiancox.com/?p=29">Christian Cox </a>on MailChimp’s API and AS2, but that wasn’t enough.  We needed a nice AS3 version of the code. So Justin and I put together this nice, simple code that should work for anyone. If I had more time I would definitely make a class.. Perhaps later this month.</p>
<pre><code lang="as3">
//api and list id keys
var _api:String = "MAIL_CHIMP_API_KEY_HERE";
var _listID:String = "LIST_ID_HERE";

//text box handler
emailAddress.text = "email address";
emailAddress.tabIndex = 1;
emailAddress.addEventListener(FocusEvent.FOCUS_IN, txtFocusIn);
emailAddress.addEventListener(FocusEvent.FOCUS_OUT, txtFocusOut);

function txtFocusIn(e:FocusEvent) {
        emailAddress.text = "";
}
function txtFocusOut(e:FocusEvent) {
        if (emailAddress.text == "") {
                emailAddress.text = "email address";
        }
}

//button setup
submitBtn.buttonMode = true;
submitBtn.addEventListener(MouseEvent.CLICK, submitForm);

function submitForm(e:Event) {

        var email:String = emailAddress.text;

        //check if valid
        if (isValidEmail(email)) {

                //set response text
                responseText.text = "sending...";

                //disable the submit button
                submitBtn.removeEventListener(MouseEvent.CLICK, submitForm);

                //setup POST
                var variables:URLVariables = new URLVariables("method=listSubscribe&amp;amp;output=xml&amp;amp;apikey=" + _api + "&amp;amp;id=" + _listID + "&amp;amp;email_address=" + email + "&amp;amp;merge_vars=");
                var request:URLRequest = new URLRequest();
                request.url = "http://api.mailchimp.com/1.2/?method=listSubscribe";
                request.method = URLRequestMethod.POST;
                request.data = variables;

                var loader:URLLoader = new URLLoader();
                loader.dataFormat = URLLoaderDataFormat.VARIABLES;
                loader.addEventListener(Event.COMPLETE, completeHandler);

                try {
                        trace("loading...");
                        responseText.text = "loading...";
                        loader.load(request);
                }
                catch(error:Error) {
                        trace("unable to load URL");
                        responseText.text = "Oh, that's embarassing. something went wrong, please try again. Thanks!";
                        trace(e.target.data);
                }
                function completeHandler(e:Event) {
                        var _t:String = unescape(e.target.data); //decode the uri

                        var _xml:XMLList = new XMLList(_t); //parse the xml
                        trace(_xml.@type);
                        if (_xml.@type == "array") { //check to see if there is an error
                                trace(_xml.error);
                                responseText.text = _xml.error;
                                resetForm();
                        } else if (_xml.@type == "boolean") { //check to see if successfully added
                                trace("successfully added to list");
                                responseText.text = "You have beeen successfully added to our list. Thank you!";
                                resetForm();
                        }
                }

        } else {
                trace ("email invalid");
        resetForm ();
                responseText.text = "invalid email!";
        }

}

//validate given email
function isValidEmail(_e:String):Boolean {
        var exp:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    return exp.test(_e);
}

//reset the form elements
function resetForm(){
        submitBtn.addEventListener(MouseEvent.CLICK, submitForm);
        emailAddress.text = "email address";

}
</code>
</pre>
<p>Done and done. Pretty easy, right? You can also download the <a href="http://www.kohactive.com/html/wp-content/uploads/mailchimp_api.zip">mailchimp_api</a>.</p>
<p><span class="post_update">UPDATE: A new version has been created that includes First and Last Name data collection [<a href="http://www.kohactive.com/html/wp-content/uploads/mailchimp_api_1.3.zip">mailchimp_api_1.3</a>].</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kohactive.com/html/labs/actionscript-3-0-and-mailchimps-api/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
