<?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>BIT Consultants &#187; Javascript</title>
	<atom:link href="http://www.bitconsultants.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitconsultants.net</link>
	<description></description>
	<lastBuildDate>Sun, 25 Jul 2010 21:54:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Geocoding with Google Maps and the Zend Framework</title>
		<link>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/</link>
		<comments>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 17:51:40 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Maps]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.bitconsultants.net/?p=231</guid>
		<description><![CDATA[Maps are a great way to engage your users and visualize data, but it can be a little tricky to setup. Here I will walk you through the steps needed to get a map up and running. // Sign Up Apply for a Google Maps...]]></description>
			<content:encoded><![CDATA[<p>Maps are a great way to engage your users and visualize data, but it can be a little tricky to setup. Here I will walk you through the steps needed to get a map up and running.<br />
<script type="text/javascript">// <![CDATA[
 google_ad_client = "pub-4761320180230999"; /* 468x15, created 5/5/10 */ google_ad_slot = "9379654790"; google_ad_width = 468; google_ad_height = 15;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></p>
<h3>Sign Up</h3>
<p>Apply for a Google Maps API Key (free) here using your domain without a subdomain or www (this will allow you to use key with or without www and on subdomains): http://code.google.com/apis/maps/signup.html</p>
<h3>Configuration</h3>
<p>Put the API key in your configuration file (application.ini, config.ini, etc.) like this:</p>
<pre class="brush: php;">
[google]
google_map_key =  YOUR_KEY
</pre>
<h3>Bootstrapping</h3>
<p>In your index.php file, add the following code:</p>
<pre class="brush: php;">
//update CONFIG_PATH to point to your configuration file
defined(CONFIG_PATH)
or define('CONFIG_PATH', APPLICATION_PATH . '/configs/application.ini');
Zend_Registry::set('google',    new Zend_Config_Ini(CONFIG_PATH, 'google'));
</pre>
<p>This will add your api key to the registry so that it can be accessed throughout your application.</p>
<h3>Model</h3>
<p>In your models directory, create a new class called &#8216;Geocoder.php&#8217;. This will be the file that contains the application logic for retreiving coordinates on a given location.</p>
<p><strong>models/Geocoder.php</strong></p>
<pre class="brush: php;">

/**
* Geocoder
*
* @author Robert McVey
*/
class Geocoder {
	protected $key = &amp;quot;&amp;quot;;
	public function __construct($apiKey)
	{
		$this-&gt;key = $apiKey;
	}
	private function _getGeocodedLatitudeAndLongitude($address)
	{
		$client = new Zend_Http_Client();
		$client-&gt;setUri($this-&gt;getGeocodingUri());
		$client-&gt;setParameterGet('q', urlencode($address))
			-&gt;setParameterGet('output', 'json')
			-&gt;setParameterGet('sensor', 'false')
			-&gt;setParameterGet('key', $this-&gt;key);
		$result = $client-&gt;request('GET');
		$response = Zend_Json_Decoder::decode(
			$result-&gt;getBody(),
			Zend_Json::TYPE_OBJECT
		);
		return $response;
	}
	public function getCoordinates($address)
	{
		$response = $this-&gt;_getGeocodedLatitudeAndLongitude($address);
		$return = array();
		if(isset($result-&gt;Placemark[0]-&gt;Point-&gt;coordinates[1])){
			$return(
			'lat' =&gt; $result-&gt;Placemark[0]-&gt;Point-&gt;coordinates[1];
			'lon' =&gt; $result-&gt;Placemark[0]-&gt;Point-&gt;coordinates[0];
			);
		}else{
			$return = null;
		}
		return $return;
	}
	private function getGeocodingUri()
	{
		return 'http://maps.google.com/maps/geo';
	}
}
?&gt;
</pre>
<h3>Usage</h3>
<p>Alright, that was pretty easy. Now we just have to implement this in our controller. Here is a sample model that uses the Geocoder class to return geotagged User objects.<br />
<strong>models/User.php</strong></p>
<pre class="brush: php;">
class User extends Zend_Db_Table_Abstract{
	protected $_name = 'users';
	protected $key;
	protected $geocoder;

	public function init()
	{
		$this-&gt;key = Zend_Registry::get('google')-&gt;google_map_key;
		$this-&gt;geocoder = new Geocoder($this-&gt;key);

	}
	public function getUsersAndGeocode()
	{
		$result = $this-&gt;fetchAll();

		$users    = $result-&gt;_toArray();
		foreach($users as $user)
		{
			$address = &quot;{$user['address']} {$user['city']} {$user['state']} {$user['zip']}&quot;;

			$latlon = $this-&gt;geocoder-&gt;getCoordinates($address);
			if($latlon)
			{
				$user['lat'] = $latlon['lat'];

				$user['lon'] = $latlon['lon'];
			}
		}
		return $users;
	}

}
?&gt;
</pre>
<p>The controller part is a little too easy. We will setup a reference to our user table and create a map action. Within the map action we get all of users and geocode them. We pass that collection to our view. This file should be located here:<br />
<strong>controllers/UserController.php</strong></p>
<pre class="brush: php;">
class UserController extends Zend_Controller_Action{
	protected $user_table;
	public function init()
	{
		//database connectivity stuff is up to you

		$this-&gt;user_table = new User();
	}
	public function mapAction()
	{

		$users = $this-&gt;user_table-&gt;getUsersAndGeocode();
		$this-&gt;view-&gt;users = $users;
	}
}
?&gt;
</pre>
<p>And finally, the view. Here we will create our map div, load the Google Maps API (don’t forget to sub out your key in the script src). We are using a custom marker here (personIcon); you can add your own image here by changing the path to your image. I recommend the image be pretty small with a transparent background. Given our current setup, this file would be located at this path:<br />
<strong>views/scripts/user/map.phtml</strong></p>
<pre class="brush: php;">
&lt;div id=&quot;map&quot;&gt;

&lt;noscript&gt;
&lt;center&gt;
&lt;strong&gt;To view the map feature, you need to have Javascript enabled. If you are unsure about how to turn Javascript support on, please&lt;/p&gt;
&lt;p&gt;read Google's instructions found here: &lt;a href=&quot;https://www.google.com/support/adsense/bin/answer.py?hl=en&amp;amp;answer=12654&quot; target=&quot;_blank&quot;&gt;LINK&lt;/a&gt;&lt;/strong&gt;

&lt;/center&gt;&lt;/p&gt;
&lt;p&gt;&lt;/noscript&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!--
&lt;script type=&quot;text/javascript&quot; src=&quot;http://maps.google.com/maps?file=api&amp;amp;amp;v=2&amp;amp;amp;sensor=false&amp;amp;amp;key=your_key&quot;&gt;&lt;/script&gt;
--&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
 //Sorry, I love jquery! If you don't use window.onload = function(){initialize();}
    $(document).ready(function(){
        initialize();

    });
var map;
var personIcon;
function initialize() {
    if(GBrowserIsCompatible){

        map = new google.maps.Map2(document.getElementById(&quot;thurtene_map&quot;));
        map.setUIToDefault();
        map.setCenter(new google.maps.LatLng('48.858001709', '2.29460000992'), 4);
        &lt;? endif; ?&gt;

        personIcon            = new GIcon();
  /**
  * Change this marker to be a small transparent custom image
  *
  */

        personIcon.image      = '/img/common/custom_marker.png';
        personIcon.shadow     = &quot;http://www.google.com/mapfiles/shadow50.png&quot;;
        personIcon.iconSize   = new GSize(32,32);
        personIcon.shadowSize = new GSize(37,34);

        personIcon.iconAnchor = new GPoint(9,34);
        personIcon.infoWindowAnchor = new GPoint(9,2);
    }
    addOverlays(map);
}

function personMarker(point, index, dto){
 //set our marker to be the custom icon we created
    markerOptions = {icon:personIcon};
    var marker = new GMarker(point, markerOptions);

/**
 * This is the content in the info bubble
 */
    GEvent.addListener(marker, 'click', function(){
       marker.openInfoWindowHtml(
            '&lt;h3&gt;&lt;a href=&quot;/profile/view/'+dto.user_id+'&quot;&gt;'+dto.user_first+' '+ dto.user_last+'&lt;/a&gt;&lt;/h3&gt;
'
            +dto.user_address+'

'
            +dto.user_city+'
'
            +dto.user_state+' '+dto.user_zip
        );

    });
    return marker;
}
/*
* Here is where we loop through our users and create new markers for each

*/
function addOverlays(map){
    &lt;?php
  $geo = $this-&gt;users-&gt;_toArray();
        $size = sizeof($geo);

 ?&gt;
    &lt;? for($i = 0; $i &lt; $size; $i++): ?&gt;
                var latLon = new GLatLng(&lt;?php echo $geo[$i]['lat'];?&gt;, &lt;?php echo $geo[$i]['lon'];?&gt;);

                map.addOverlay(personMarker(latLon, &lt;?=$i?&gt;, &lt;?=Zend_Json::encode($geo[$i])?&gt;));
    &lt;?php endfor; ?&gt;
}
&lt;/script&gt;
</pre>
<p>Post a comment if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery Tabs Keyboard Shortcuts</title>
		<link>http://www.bitconsultants.net/2009/jquery-tabs-keyboard-shortcuts/</link>
		<comments>http://www.bitconsultants.net/2009/jquery-tabs-keyboard-shortcuts/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 02:49:50 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.bitconsultants.net/?p=240</guid>
		<description><![CDATA[I have integrated jQuery&#8217;s UI tabs into a number of my projects and have had to significantly modify the code to achieve some goals (like being able to easily add and remove tabs. One of the simpler thing I have done with the tabbed interface...]]></description>
			<content:encoded><![CDATA[<p>I have integrated jQuery&#8217;s UI tabs into a number of my projects and have had to significantly modify the code to achieve some goals (like being able to easily add and remove tabs. One of the simpler thing I have done with the tabbed interface is let users navigate them using keyboard shortcuts. Here is a simple snippet I use to allow users to select a tab using alt+number. I use the js-hotkeys plugin found <a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;ved=0CBUQFjAD&amp;url=http%3A%2F%2Fcode.google.com%2Fp%2Fjs-hotkeys%2F&amp;ei=s__US4njBcKB8gb807DlDw&amp;usg=AFQjCNGt_wU-gT73jN7XAqGDx6vfWymgSQ&amp;sig2=P7ja4dADKQ_JLe3DYOycYQ" target="_blank">here</a></p>
<pre class="brush: xml;">
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Keyboard Tabs&lt;/title&gt;
		&lt;link rel=&quot;stylesheet&quot; href=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css&quot; type=&quot;text/css&quot; /&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.9.min.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot;&gt;
			$(document).ready(function(){
                //initialize tabs
				$tabs = $('#tabs').tabs();
                //create a reference to our document
				var this_document = $(document);
                //nothing fancy, just iterate over a number set and generate the shortcuts
				$.each([1,2,3,4,5,6,7,8,9], function(index){
					this_document.bind('keydown', 'alt+'+index, function(e){
						 $tabs.tabs('select', (index - 1));
						 e.preventDefault();
					 });
				});
			});
		&lt;/script&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div class=&quot;demo&quot;&gt;

		&lt;div id=&quot;tabs&quot;&gt;
			&lt;ul&gt;
				&lt;li&gt;&lt;a href=&quot;#tabs-1&quot;&gt;Nunc tincidunt&lt;/a&gt;&lt;/li&gt;
				&lt;li&gt;&lt;a href=&quot;#tabs-2&quot;&gt;Proin dolor&lt;/a&gt;&lt;/li&gt;
				&lt;li&gt;&lt;a href=&quot;#tabs-3&quot;&gt;Aenean lacinia&lt;/a&gt;&lt;/li&gt;
			&lt;/ul&gt;
			&lt;div id=&quot;tabs-1&quot;&gt;
				&lt;p&gt;Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.&lt;/p&gt;
			&lt;/div&gt;
			&lt;div id=&quot;tabs-2&quot;&gt;
				&lt;p&gt;Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.&lt;/p&gt;
			&lt;/div&gt;
			&lt;div id=&quot;tabs-3&quot;&gt;
				&lt;p&gt;Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.&lt;/p&gt;
				&lt;p&gt;Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.&lt;/p&gt;
			&lt;/div&gt;
		&lt;/div&gt;

		&lt;/div&gt;&lt;!-- End demo --&gt;

		&lt;div style=&quot;display: none;&quot; class=&quot;demo-description&quot;&gt;

		&lt;p&gt;Click tabs to swap between content that is broken into logical sections.&lt;/p&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If you missed the part that did the shortcuts, here it is again:</p>
<pre class="brush: jscript;">
$(document).ready(function(){
                //initialize tabs
				$tabs = $('#tabs').tabs();
                //create a reference to our document
				var this_document = $(document);
                //nothing fancy, just iterate over a number set and generate the shortcuts
				$.each([1,2,3,4,5,6,7,8,9], function(index){
					this_document.bind('keydown', 'alt+'+index, function(e){
						 $tabs.tabs('select', (index - 1));
						 e.preventDefault();
					 });
				});
			});
</pre>
<p>That&#8217;s it! If you have any questions, please post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitconsultants.net/2009/jquery-tabs-keyboard-shortcuts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript 2.0: Centered Popup Window</title>
		<link>http://www.bitconsultants.net/2009/actionscript-2-0-centered-popup-window/</link>
		<comments>http://www.bitconsultants.net/2009/actionscript-2-0-centered-popup-window/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 05:31:31 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.bitconsultants.net/?p=136</guid>
		<description><![CDATA[Centered popup for Actionscript 2.0]]></description>
			<content:encoded><![CDATA[<p>At some point we all need a centered popup windows. Here is a useful Actionscript 2.0 function to help get a popup window, that allows for some customization based on input parameters. Check the actual implementation at the bottom. It is also worth mentioning here, that no AS popup script will work in local testing mode. You must upload to a server (or move to a local server directory) for this to function properly.</p>
<pre class="brush: jscript;">

function winOpen(url:String, winName:String, wide:Number, high:Number, chrome:Boolean, centered:Boolean)
{
//Chrome on window un-centered
if (chrome == true &amp;&amp; centered == false)­
{
getURL(&quot;javascript:&quot;+winName+&quot;=window.open('&quot;+url+&quot;', '&quot;+winName+&quot;', 'width=&quot;+wide+&quot;,height=&quot;+high+&quot;,left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); &quot;+winName+&quot;.focus(); void(0);&quot;);
}
//Chromeless un-centered
else if (chrome == false &amp;&amp; centered == false)­
{
getURL(&quot;javascript:&quot;+winName+&quot;=window.open('&quot;+url+&quot;', '&quot;+winName+&quot;', 'width=&quot;+wide+&quot;,height=&quot;+high+&quot;'); &quot;+winName+&quot;.focus(); void(0);&quot;);
}
//Chrome on, centered
else if (chrome == true &amp;&amp; centered == true)
{
getURL(&quot;javascript:&quot;+winName+&quot;=window.open('&quot;+url+&quot;', '&quot;+winName+&quot;', 'width=&quot;+wide+&quot;,height=&quot;+high+&quot;,left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); &quot;+winName+&quot;.focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-(&quot;+wide+&quot;/2); top_point = parseInt(screen_height/2)-(&quot;+high+&quot;/2); setTimeout('&quot;+winName+&quot;.moveTo(left_point,top_point)',10); void(0);&quot;);
}
//Chromeless, centered (most popular!)
else if (chrome == false &amp;&amp; centered == true)
{
getURL(&quot;javascript:&quot;+winName+&quot;=window.open('&quot;+url+&quot;', '&quot;+winName+&quot;', 'width=&quot;+wide+&quot;,height=&quot;+high+&quot;,left=0,top=0,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no'); &quot;+winName+&quot;.focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-(&quot;+wide+&quot;/2); top_point = parseInt(screen_height/2)-(&quot;+high+&quot;/2); setTimeout('&quot;+winName+&quot;.moveTo(left_point,top_point)',10); void(0);&quot;);
}
}

//Sample of application, requires a button with instance name of button_mc on stage
button_mc.onRelease = function()
{
winOpen(&quot;http://www.bitconsultants.net/&quot;, &quot;MyWindow&quot;, 350, 350, true, true);
­}
</pre>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">
<pre class="geshifilter-actionscript"><span style="color: #000000; font-weight: bold;">function</span> winOpen<span style="color: #66cc66;">(</span><span style="color: #0066cc;">url</span>:<span style="color: #0066cc;">String</span>, winName:<span style="color: #0066cc;">String</span>, wide:<span style="color: #0066cc;">Number</span>, high:<span style="color: #0066cc;">Number</span>, chrome:<span style="color: #0066cc;">Boolean</span>, centered:<span style="color: #0066cc;">Boolean</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="color: #808080; font-style: italic;">//Chrome on window un-centered</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>chrome == <span style="color: #000000; font-weight: bold;">true</span> &amp;&amp; centered == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">)</span>­
    <span style="color: #66cc66;">{</span>
        <span style="color: #0066cc;">getURL</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"javascript:"</span>+winName+<span style="color: #ff0000;">"=window.open('"</span>+<span style="color: #0066cc;">url</span>+<span style="color: #ff0000;">"', '"</span>+winName+<span style="color: #ff0000;">"', 'width="</span>+wide+<span style="color: #ff0000;">",height="</span>+high+<span style="color: #ff0000;">",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "</span>+winName+<span style="color: #ff0000;">".focus(); void(0);"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #808080; font-style: italic;">//Chromeless un-centered</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>chrome == <span style="color: #000000; font-weight: bold;">false</span> &amp;&amp; centered == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">)</span>­
    <span style="color: #66cc66;">{</span>
        <span style="color: #0066cc;">getURL</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"javascript:"</span>+winName+<span style="color: #ff0000;">"=window.open('"</span>+<span style="color: #0066cc;">url</span>+<span style="color: #ff0000;">"', '"</span>+winName+<span style="color: #ff0000;">"', 'width="</span>+wide+<span style="color: #ff0000;">",height="</span>+high+<span style="color: #ff0000;">"'); "</span>+winName+<span style="color: #ff0000;">".focus(); void(0);"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #808080; font-style: italic;">//Chrome on, centered</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>chrome == <span style="color: #000000; font-weight: bold;">true</span> &amp;&amp; centered == <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #0066cc;">getURL</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"javascript:"</span>+winName+<span style="color: #ff0000;">"=window.open('"</span>+<span style="color: #0066cc;">url</span>+<span style="color: #ff0000;">"', '"</span>+winName+<span style="color: #ff0000;">"', 'width="</span>+wide+<span style="color: #ff0000;">",height="</span>+high+<span style="color: #ff0000;">",left=0,top=0,toolbar=1,location=1,scrollbars=1,status=1,resizable=1,fullscreen=no'); "</span>+winName+<span style="color: #ff0000;">".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("</span>+wide+<span style="color: #ff0000;">"/2); top_point = parseInt(screen_height/2)-("</span>+high+<span style="color: #ff0000;">"/2); setTimeout('"</span>+winName+<span style="color: #ff0000;">".moveTo(left_point,top_point)',10); void(0);"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #808080; font-style: italic;">//Chromeless, centered (most popular!)</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>chrome == <span style="color: #000000; font-weight: bold;">false</span> &amp;&amp; centered == <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #0066cc;">getURL</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"javascript:"</span>+winName+<span style="color: #ff0000;">"=window.open('"</span>+<span style="color: #0066cc;">url</span>+<span style="color: #ff0000;">"', '"</span>+winName+<span style="color: #ff0000;">"', 'width="</span>+wide+<span style="color: #ff0000;">",height="</span>+high+<span style="color: #ff0000;">",left=0,top=0,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no'); "</span>+winName+<span style="color: #ff0000;">".focus(); screen_height = window.screen.availHeight;screen_width = window.screen.availWidth; left_point = parseInt(screen_width/2)-("</span>+wide+<span style="color: #ff0000;">"/2); top_point = parseInt(screen_height/2)-("</span>+high+<span style="color: #ff0000;">"/2); setTimeout('"</span>+winName+<span style="color: #ff0000;">".moveTo(left_point,top_point)',10); void(0);"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>

<span style="color: #808080; font-style: italic;">//Sample of application, requires a button with instance name of button_mc on stage</span>
button_mc.<span style="color: #0066cc;">onRelease</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    winOpen<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"http://www.bitconsultants.net/"</span>, <span style="color: #ff0000;">"MyWindow"</span>, <span style="color: #cc66cc;">350</span>, <span style="color: #cc66cc;">350</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>;
­<span style="color: #66cc66;">}</span></pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.bitconsultants.net/2009/actionscript-2-0-centered-popup-window/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery Form Validation: theory and practice</title>
		<link>http://www.bitconsultants.net/2008/jquery-form-validation-theory-and-practice/</link>
		<comments>http://www.bitconsultants.net/2008/jquery-form-validation-theory-and-practice/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 21:37:30 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.bitconsultants.net/?p=173</guid>
		<description><![CDATA[jQuery makes it very easy to validate forms. There are a number of useful plugins, the most prominent being validate. If you are wanting something very lightweight or are just interested in how something like this works, read on. The $.each function allows you to...]]></description>
			<content:encoded><![CDATA[<p>jQuery makes it very easy to validate forms. There are a number of useful plugins, the most prominent being <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">validate</a>. If you are wanting something very lightweight or are just interested in how something like this works, read on. The $.each function allows you to iterate over collections in jQuery, it is really one of the most useful utility methods available. We can iterate over all input elements on a page using $.each like this<br />
<script type="text/javascript"><!--
google_ad_client = "pub-4761320180230999";
/* 336x280, created 4/15/10 */
google_ad_slot = "4518447987";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<pre class="brush: jscript;">
var input_count = 0;

$(document).ready(function(){
	$.each($('input'), function(index, value){
		input_count++;
	});
	console.log(&quot;Number of inputs:  %i&quot;,  input_count);//use Firebug, not alert() !
});
</pre>
<h3>Iterating Over Text Inputs</h3>
<p>There is a problem with the above code, any input elements (including buttons) are going to be iterated over, so let&#8217;s use a more specific selector to only retrieve input elements of type &#8220;text&#8221;</p>
<pre class="brush: jscript;">

...

$.each($('input[type=&quot;text&quot;]'), function(index, value){

...
</pre>
<h3>Required Elements</h3>
<p>Okay, now we can iterate over our text inputs. So, where to go from here? Do we check that each item has a value? What if some items aren&#8217;t required? The easiest way I&#8217;ve found to do this is to use a class on required items (I usually call it &#8220;required&#8221; (creative, huh?)).</p>
<pre class="brush: xml;">
...
&lt;form action=&quot;&quot; method=&quot;POST&quot; id=&quot;login_form&quot;&gt;
	&lt;dl&gt;
	  &lt;dt&gt;
		&lt;dd&gt;Username &lt;span class=&quot;req&quot;&gt;*&lt;/span&gt;&lt;/dd&gt;
		&lt;dd&gt;&lt;input type=&quot;text&quot; name=&quot;username&quot; class=&quot;required&quot; maxlength=&quot;7&quot; /&gt;&lt;/dd&gt;
		&lt;dd&gt;Password &lt;span class=&quot;req&quot;&gt;*&lt;/span&gt;&lt;/dd&gt;
		&lt;dd&gt;&lt;input type=&quot;text&quot; name=&quot;password&quot; class=&quot;required&quot; maxlength=&quot;7&quot; /&gt;&lt;/dd&gt;
		&lt;dd&gt;Favorite Color&lt;dd&gt;
		&lt;dd&gt;&lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;/dd&gt;
		&lt;dd&gt;Finish&lt;/dd&gt;
		&lt;dd&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; /&gt;&lt;/dd&gt;
	  &lt;/dt&gt;
	&lt;/dl&gt;
&lt;/form&gt;
...
</pre>
<p>Now we can check which fields are required and cancel form submission if they are empty. Getting rid of our input counter variable, here is the new code</p>
<pre class="brush: jscript;">
$(document).ready(function(){
	$('#login_form').submit(function(){//add an onsubmit event handler to our form
		var blank_fields = false;//initialize a generic error detector
		$.each($('input[type=&quot;text&quot;]'), function(index, value){
			var t = $(this);//limit the number of new jQuery objects created
			if(t.hasClass('required')){//we know it is required
				if(t.val() == &quot;&quot; || t.val() === undefined){
					console.error('%s can not be blank', t.attr('name'));
					blank_fields = true;
				}
			}
		});
		return !blank_fields;//if there are no blank fields, submit
	});
});
</pre>
<p>That is a mouthful, but it is actually quite simple. When our form is submitted, we iterate over each input item to see if it is required. If it&#8217;s required, check to see if it is empty, if it is empty, return false (cancelling form submission).</p>
<p>Now, there are a couple errors with the code above. For one, it is checking all form inputs (not just the one in our form), so if we had multiple forms on the page, both with required elements, this would fail. Also, being able to check more specific criteria would be nice. We will limit our iteration to input items that are of type text and are descendants of the target form. Let&#8217;s also add a maxlength attribute to a form field to ensure that it is within the set character length:</p>
<pre class="brush: jscript;">
$(document).ready(function(){
	$('#login_form').submit(function(){
		var blank_fields = false;
		var length_error = false;//check for length errors
		$.each($('#login_form input[type=&quot;text&quot;]'), function(index, value){//limit input elements to those within the form
			var t = $(this);
			if(t.hasClass('required')){
				if(t.val() == &quot;&quot; || t.val() === undefined){//check if it's empty
					console.error('%s can not be blank', t.attr('name'));
					blank_fields = true;
				}
				if(t.attr('maxlength') !== undefined){//if maxlength attribute is present in the current element
					if(t.val().length &gt; t.attr('maxlength')){//check string length against given maxlength
						console.error('%s is too long: %i chars', t.attr('name'), t.val().length);
						length_error = true;
					}
				}
			}
		});
		return !(blank_fields || length_error);//if there are no blank fields and no length errors, submit
	});
});
</pre>
<p>Okay, now we are getting somewhere. This could be used as is, but it doesn&#8217;t provide very friendly error messages (they have to have Firebug installed to know what the errors are). Let&#8217;s wrap up our code so far into a jQuery function and provide some more useful errors. I&#8217;ve commented changes to the code, but it is mostly the same.</p>
<pre class="brush: jscript;">
$.fn.validate = function(options){
	/*
	* Define the default error message tag and styling, allow this to be overridden
	*/
	var defaults = {
		 formTag: 'div',
		 color: '#8A1F11',
		 border: '2px solid #FBC2C4',
		 errorBG: '#FBE3E4 0 0',
		 marginBottom:'1em',
		 padding:'0.8em',
		 width: $(this).width()
	 };
	var o = $.extend(defaults, options);

	/*
	* Private method creates error element
	*/
	function genError(msg){
		var err = $(document.createElement(o.formTag));//create an error element
		err.css('border', 		 o.border)
		   .css('background', 	 o.errorBG)
		   .css('margin-bottom', o.marginBottom)
		   .css('padding', 		 o.padding)
		   .css('color', 		 o.color)
		   .css('width', 		 o.width);//Apply css style rules to the error
		err.html(msg);//insert the error message into the element
		return err;
	}

	/*
	* Our slightly modified form validation
	*/
	$(this).submit(function(){//now uses $(this) reference as the method is applied to the form selector
		var blank_fields = false;//initialize a generic error detector
		var length_error = false;
		//now we use $(this).find('input[type=&quot;text&quot;]') to locate our forms child elements
		$.each($(this).find('input[type=&quot;text&quot;]'), function(index, value){
			var t = $(this);
			if(t.hasClass('required')){
				if(t.val() == &quot;&quot; || t.val() === undefined){
					//instead of logging error, create a new element and append it to the input elements parent
					errormsg = genError(t.attr('name') + ' can not be blank')
					t.parent().append(errormsg);
					blank_fields = true;
				}
				if(t.attr('maxlength') !== undefined){
					if(t.val().length &gt; t.attr('maxlength')){
						//same as above but more concise
						t.parent().append(genError(t.attr('name') + ' is too long'));
						length_error = true;
					}
				}
			}
		});
		return !(blank_fields || length_error);
	});
}
</pre>
<p>Usage:</p>
<pre class="brush: jscript;">
$(document).ready(function(){
	$('#login_form').validate({color:'#000'});
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bitconsultants.net/2008/jquery-form-validation-theory-and-practice/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
