<?xml version='1.0' ?><rss version='2.0'><channel><title>Progressive-Coding.com</title><link>http://www.progressive-coding.com/</link><description>personal site of Laurent Haan with lots of tutorials and articles</description><language>en-us</language><docs>http://www.progressive-coding.com/full.xml</docs><item><title>News: Weekly wroundup</title><description>The last week was rather quiet and while I'm still working on a major project about Rich Internet Technologies comparison, I didn't stumble over a particularly interesting part that would be worth mentioning here. Over the next days, I might release some of the documents that I wrote about RIA, such as the actual comparison methods, the result evaluation and the data types and storage methods used.&lt;br /&gt;
&lt;br /&gt;
Since the launch of this site, one of the most popular resources is the &lt;a href=&quot;http://www.progressive-coding.com/tutorial.php?id=0&quot;&gt;Advanced Encryption Standard tutorial&lt;/a&gt;. While it might not be the most interesting subject and might not attract the broadest audience, I think that the quality of the tutorial is among the best you'll find on the Internet about AES. I am particularly pleased that &lt;a href=&quot;http://blog.josh-davis.org/&quot; target=&quot;_blank&quot;&gt;Josh Davis&lt;/a&gt; used my tutorial as a starting point to implement &lt;a href=&quot;http://blog.josh-davis.org/2007/05/13/javascript-aes-advanced-encryption-standard/&quot; target=&quot;_blank&quot;&gt;JavaScript AES&lt;/a&gt;. He kindly contacted me and asked permission to use my tutorial and since everything on this page is released under the &lt;a href=&quot;http://en.wikipedia.org/wiki/GPL&quot; target=&quot;_blank&quot;&gt;GNU Public License&lt;/a&gt;, he was free to use and modify the code as he pleased.&lt;br /&gt;
&lt;br /&gt;
Finally, if you keep yourself updated in Rich Internet Technologies, you might be interested in hearing that SUN announced &lt;a href=&quot;http://en.wikipedia.org/wiki/JavaFX&quot; target=&quot;_blank&quot;&gt;JavaFX&lt;/a&gt;. Especially &lt;a href=&quot;http://en.wikipedia.org/wiki/JavaFX_Script&quot; target=&quot;_blank&quot;&gt;JavaFX Script&lt;/a&gt; might become popular among the Java community, since Java is still a little too heavy to use for small web applications.</description><link>http://www.progressive-coding.com/news.php?id=11</link><guid>http://www.progressive-coding.com/news.php?id=11</guid></item><item><title>News: How to correctly use the power of widgets in Ajax</title><description>Over the last few weeks, my research on Rich Internet Applications was particularly focused on the increased accessibility that is provided through the use of expressive GUI widgets and how these widgets can be implemented in the different technologies, and more specifically Ajax. Before elaborating on that particular problem, it is important to first explain the basic concepts of what exactly is a widget and why it is useful in Computer Science.&lt;br /&gt;
&lt;p&gt;According to the &lt;a href=&quot;http://en.wikipedia.org/wiki/GUI_Widget&quot; target=&quot;_blank&quot;&gt;Wikipedia definition&lt;/a&gt;, &lt;div class=&quot;quote&quot;&gt;A widget is an interface element that a computer user interacts with. [...] Programmers use widgets to build graphical user interfaces.&lt;/div&gt; This means that widgets are the interactive elements of a computer program, that serve as an interaction medium, a link, between the user and the program. The user is limited in his actions and intentions by the possible widgets that are at his disposal, restricting his expressiveness to the different values the widgets may have.&lt;/p&gt;&lt;h3&gt;The standard HTML widgets&lt;/h3&gt;&lt;p&gt;Since our particular interest lies within the domain of Internet applications, let's analyze what widgets are at our disposal, using the latest &lt;a href=&quot;http://www.w3.org/TR/html401/&quot; target=&quot;_blank&quot;&gt;HTML 4.01 specifications&lt;/a&gt; provided by the W3C. There are basically three different categories of elements that can be used as widgets, which are the INPUT, the SELECT and the TEXTAREA elements. The INPUT element has an attribute called &lt;a href=&quot;http://www.w3.org/TR/html401/interact/forms.html#adef-type-INPUT&quot; target=&quot;_blank&quot;&gt;type&lt;/a&gt; that specifies the type of control to create, which can take the following values: text, password, checkbox, radio, submit, image, reset, button, hidden and file. While this list, dating back to December 1999, provides, from a Human Computer Interaction perspective, enough means of interaction within a web application for every basic operation, it is by no means impressive and definitely not worthy of being called Web 2.0. If we compare this list to the widget list on Wikipedia, which has over thirty different types of widgets, we immediately notice the need for better widgets, that provide additional interactive possibilities and better customization.&lt;/p&gt;&lt;h3&gt;Accessibility guidelines&lt;/h3&gt;&lt;p&gt;Unfortunately, creating new widgets isn't as easy as it might sound. Simply throwing together  graphical elements with several event listener might be functional, but doesn't correspond to the modern accessibility criteria, explained in the &lt;a href=&quot;http://www.w3.org/TR/WAI-WEBCONTENT&quot; target=&quot;_blank&quot;&gt;Web Content Accessibility Guidelines 1.0&lt;/a&gt;. These guidelines explain how to make web content accessible to people with disabilities but also more available to all users, whatever user agent they are using. In the HTML techniques for &lt;a href=&quot;http://www.w3.org/TR/WCAG10-HTML-TECHS/&quot; target=&quot;_blank&quot;&gt;Web Content Accessibility Guidelines 1.0&lt;/a&gt;, the most important sections concerning self-made widgets are the &lt;a href=&quot;http://www.w3.org/TR/WCAG10-HTML-TECHS/#forms&quot; target=&quot;_blank&quot;&gt;Forms&lt;/a&gt; and the &lt;a href=&quot;http://www.w3.org/TR/WCAG10-HTML-TECHS/#scripts&quot; target=&quot;_blank&quot;&gt;Scripts&lt;/a&gt; section, which are directly applicable to our intention to code our own widgets. The Forms section basically describes all the features that an interactive widget should have to make it as accessible as possible, ranging from keyboard access to labeling form controls and providing a text equivalent for every non-text element. The most important section in the scripts section is that one should ensure that dynamic content is accessible or &lt;i&gt;provide an alternative presentation&lt;/i&gt;. This is commonly referred to as &lt;i&gt;graceful degradation&lt;/i&gt;, or the ensuring that pages are accessible with scripts turned off or in browsers that don't support scripts. One might argue that these guidelines date back to November 2000, when Rich Internet Applications were less common and that nowadays most users have scripting support turned on. Imagine what would happen to the whole Flash world if these guidelines had to respected &amp;agrave; la lettre. I'm not saying that we should change the whole widget concept because of these guidelines but we shouldn't completely forget about them either. I think that Accessibility is a very important subject and that we should try to make the web as accessible as possible. A good example for widget design principles can be found in the dojo toolkit, in the Dojo Book chapter &lt;a href=&quot;http://dojotoolkit.org/dojos-widget-design-principles&quot; target=&quot;_blank&quot;&gt;Dojo Widget Design principles&lt;/a&gt;. I strongly recommend everyone to read those principles and at least intent to adapt them as closely as possible.&lt;/p&gt;&lt;h3&gt;Widgets 1.0 Draft&lt;/h3&gt;&lt;p&gt;The &lt;a href=&quot;http://www.w3.org/2006/rwc/Activity&quot; target=&quot;_blank&quot;&gt;Rich Web Clients Activity&lt;/a&gt; Working Group is currently working on the &lt;a href=&quot;http://www.w3.org/TR/widgets/&quot; target=&quot;_blank&quot;&gt;Widgets 1.0&lt;/a&gt; Draft, that covers the packaging format, the manifest file and the scripting interfaces for working with widgets. They refer to widgets as &quot;small client-side applications for displaying and updating remote data, packaged in a way to allow a single download and installation on a client machine. The describes widget object is based on &lt;a href=&quot;http://developer.apple.com/documentation/AppleApplications/Reference/Dashboard_Ref/index.html&quot; target=&quot;_blank&quot;&gt;Apple's Dashboard&lt;/a&gt; and is intended to provide a standardized interface to self-made widgets in the future.&lt;/p&gt;&lt;p&gt;Unfortunately, the Widgets 1.0 format is only a draft, a work in progress, and if you are familiar with the working speed of the W3C, it is safe to assume that this won't be applied in the near future, which means we are on our own. In the next chapter, I will cover the general concept of creating widgets in Ajax, based on my experience over the last few weeks, where I spend the major portion of my time with this particular problem. Before you even consider coding your own widgets, I ask you to please carefully consider if you're doing so to increase the functionality of your interface or only for the sake of the technological aspect. Design should always primarily focus on the user experience and never be guided by the implementation challenges or the technical aspects. If the interface doesn't gain any additional functionality, your web applications will get worse if you implement your own widget. Not only aren't you using any of the standardized widgets which are fully compliant to the accessibility guidelines, you're probably also reducing the number of potential users by keeping them from interacting with your widget in non-browser user agents.&lt;/p&gt;&lt;h3&gt;Widgets in JavaScript&lt;/h3&gt;&lt;p&gt;I'm not going to focus on JavaScript frameworks, that already provide widgets. If you are looking for working implementations where you only have to add some lines of code to your page, then I recommend that you use the framework of your choice, but if you want to truly understand the design and implementation challenge and maybe implement your very own widget, then please continue reading. As I explained in great detail in my tutorial about &lt;a href=&quot;http://www.progressive-coding.com/tutorial.php?id=6&quot; target=&quot;_blank&quot;&gt;creating an Ajax rating widget&lt;/a&gt;, the starting point for a widget is a standard DOM element, that is modified in JavaScript. Any element can be used, but if we respect the Accessibility guidelines, we have to make sure that our web application is still functional if scripting support is disabled. If we use a div container and the user has JavaScript disabled, then he won't have any interaction possibility since he'll only be presented by an empty container. As long as you ensure that there is any of the standard interaction widgets, you can use whatever you like. A good choice would be to use one of the standard widgets that is as close to what you want to achieve with your own widget as possible. If your widget is intended to only provide two different options, don't use a text field as the fail-safe replacement, since the user will likely enter a non-conform value; rather use a select menu with two options or radio buttons. You can also wrap the standard widget with a div container and remove all the child nodes of the container before inserting your own. That way, you have a good starting point to insert your widget into and you can work in a very specific subtree of the HTML DOM tree without the risk to delete something that wasn't intended to be deleted.&lt;/p&gt;&lt;p&gt;I won't enter into the details on how to create your own widget, the rating widget tutorial should have covered that section enough. What I want to focus on is a more delicate subject, that wasn't much of a problem during the design of the rating widget. In fact, the rating widget is designed to be used as a stand-alone widget, that issues a request upon interaction and never has to interface with other parts of the web application. While this is the generally accepted behavior of a rating widget, most widgets are user to communicate a value from the user to the application. They provide several interactive features which are translated into a value that can be used by the program to determine what the user has entered. This can be a state as in the case of a checkbox, that can be check or not, or a numerical value, as in the case of a slider. The value of the standard widgets are used during &lt;a href=&quot;http://www.w3.org/TR/html401/interact/forms.html#submit-format&quot; target=&quot;_blank&quot;&gt;form submission&lt;/a&gt;, where each successful control generates a (control name, current value) pair that is transmitted in the form data set. This is a serious problem that requires additional methods to make our widget functional. Even though are widget by itself is able to handle user interaction and store its value somewhere, it isn't recognized by the user agent as a form element and thus that value isn't transmitted to the processing agent.&lt;/p&gt;&lt;h3&gt;Implementation methods&lt;/h3&gt;&lt;p&gt;To deal with this situation, several methods exist, of which I want to explain two. The first one uses the default form submission process to transmit the values of our widget by hooking onto the form submit button and modifying the form DOM tree before submitting the form. For this to work, it is necessary to create for every self-made widget an hidden input field, that has the correct control name and whose value is the value we want to transmit. In this case, our widget is nothing but an interface to the underlying hidden input field and every interaction with the widget is automatically applied to the corresponding hidden element. With a little caution and imagination in the element naming process, it is even possible to apply the standard methods, such as document.getElementById('widgetId').value to our self-made widget, if we take care that the hidden element is called widgetId and not the widget we created (which could be called widgetId_interface). This was the first method I used when I was tasked to create a dynamically generated interface with custom widgets and although it took some time to get used to, the result was very convincing and I had no major problems getting it to work exactly the way I wanted. I have to admit that this method strongly might be a little unusual but it gets the job done. A more elaborate method, with a lot more options, is probably the second one.&lt;/p&gt;&lt;p&gt;I assume that a lot of people that recently started with JavaScript, make the mistake and compare it to something like PHP, that resides in a different layer than the HTML markup and where the interaction only goes in one direction. They might think that a JavaScript function, upon invocation, generates something which is then displayed on the web site. While this is correct, JavaScript offers a lot more possibilities and contrary to PHP, objects you create aren't deleted when the JavaScript code has ended but it is possible to access them again at a later stage. Additionally, JavaScript is a very powerful object-based language, which is explained in great detail in a &lt;a href=&quot;http://www.sitepoint.com/article/oriented-programming-1&quot; target=&quot;_blank&quot;&gt;tutorial on sitepoint&lt;/a&gt;. One of the problems you have to face when you dynamically generate JavaScript objects is to actually find them again at a later stage, based on a numerical or string value that you received as argument. To solve this problem, a common solution is to create an object, a factory, that is able to store all the created objects in an associative array, or a map, as some might call it. As soon as you create an object that you want to keep for later usage, you stick it into the map, using its identifier as key for the map. This allows us to structure the entire code in a perfect object-oriented approach. First, we define a common interface that every widget we'll create has to respect. It should contain simple functions like getter/setter for the id, the name or the value and functions that define its behavior and visual appearance. This time, when the user submits the form, we don't have to rely on hidden elements to transmit our values but we can iterate over the map of created elements and generate the correct (name, value) pair for every widget we created. As each widget is internally represented by its object, the widget this time serves as visual interface to its underlying object and not to an hidden input field. For those of you that are familiar with dojo, might recognize the difference between dojo.byId, which is nothing else than a short version of document.getElementById and dojo.widget.byId, which is a lookup in the object map and returns the widget object, not the DOM element. This method is far superior to the first one, but also a little more complex to implement. The object-oriented approach has clear benefits and allow for a more modular approach, that is scalable and robust.&lt;/p&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;Writing and designing self-made widgets is a lot of work and requires careful thinking. More than once, you feel like you just reinvented the wheel and that you should better rely on available frameworks and widget engines. Of course, there are also reasons for writing a custom widget engine oneself, like dissatisfaction of the large overhead of some frameworks or the complexity and missing ease of use. Finally, it should be noted that several groups are currently trying to provide better widget frameworks, such as the the &lt;a href=&quot;http://dojotoolkit.org/developer/dijit&quot; target=&quot;_blank&quot;&gt;dojo dijit project&lt;/a&gt;.&lt;/p&gt;</description><link>http://www.progressive-coding.com/news.php?id=10</link><guid>http://www.progressive-coding.com/news.php?id=10</guid></item><item><title>News: New RSS feed</title><description>I would like to inform you that I added a new &lt;a href=&quot;http://www.progressive-coding.com/feed.xml&quot;&gt;RSS feed&lt;/a&gt; to this website, that contains the 10 latest news and tutorials. Even though RSS is a very common way to distribute news around the Internet, I never got around to integrate it into my site, since I don't use any finished CMS but like to program everything myself.&lt;br /&gt;
For those of you interested, I followed a guide that I found on &lt;a href=&quot;http://www.devshed.com/c/a/PHP/Rockin-RSS-with-PHP-on-your-HTML/&quot;  target=&quot;_new&quot;&gt;devshed&lt;/a&gt;, it was easy enough to go through it very fast but unfortunately contained several mistakes in the code. If you think about added an RSS feed to your site as well, have a look at that tutorial and you'll end up with one in no time.</description><link>http://www.progressive-coding.com/news.php?id=9</link><guid>http://www.progressive-coding.com/news.php?id=9</guid></item><item><title>Tutorial: Creating an AJAX Rating Widget</title><description>&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#html_markup&quot;&gt;Getting started with the HTML markup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#javascript&quot;&gt;Adding dynamic behaviour&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ajax&quot;&gt;Connect the widget to the server with different frameworks&lt;/a&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;#dojo&quot;&gt;Using the Dojo framework&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#mootools&quot;&gt;Using the mootools framework&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#jquery&quot;&gt;Using the jQuery framework&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#prototype&quot;&gt;Using the prototype framework&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
    &lt;a name=&quot;introduction&quot;&gt;&lt;/a&gt;&lt;h3&gt;Introduction&lt;/h3&gt;
    Frequent visitors of ajax enabled websites, like &lt;a href=&quot;http://www.ajaxian.com&quot; target=&quot;_blank&quot;&gt;ajaxian&lt;/a&gt;, have all witnessed them already: ajax rating widgets. They are flashy, animated, you can use them to rate the content (usually without refreshing the page) and if you could, you'd present them to your parents and marry them. Compared to the classic rating system, as on &lt;a href=&quot;http://www.imdb.com/title/tt0052077/&quot; target=&quot;_blank&quot;&gt;IMDb&lt;/a&gt;, they incite people to click them, reducing the effective rating process to only one click.&lt;br /&gt;
    In this tutorial, I want to show you how to create the JavaScript framework to display the animated rating widget and how to connect it to your server backend by using some of the most common Ajax frameworks out there. I clearly separate the page creation from the JavaScript functions and the rating backend, to allow the script to be as flexible as possible and to be easy integrable into your existing website.&lt;br /&gt;This tutorial is not meant to present you with a finished script (even though you could simply copy&amp;amp;paste the end result into your website and make it work without any problems), but rather to explain the design and implementation process that would enable you to create your own widgets if you'd need to.
    &lt;a name=&quot;html_markup&quot;&gt;&lt;/a&gt;&lt;h3&gt;Getting started with the HTML markup&lt;/h3&gt;
    The first thing we're going to do is to create the div containers where we'll put our rating widget inside. This simple task already requires careful planning, as it will greatly influence the rest of our script. The problem is that the div containers need to be clearly identifiable by the JavaScript, to transform the static content into our dynamic rating widget and yet, they need to be distinguishable if you'd happen to have several rating widgets on the same page. If you read the &lt;a href=&quot;http://www.quirksmode.org/dom/w3c_core.html&quot; target=&quot;_blank&quot;&gt;quirksmode W3C DOM tables&lt;/a&gt;, you'll notice that only have two functions we can use to access the elements within our webpage, &lt;a href=&quot;http://developer.mozilla.org/en/docs/DOM:document.getElementById&quot; target=&quot;_blank&quot;&gt;getElementById()&lt;/a&gt; and &lt;a href=&quot;http://developer.mozilla.org/en/docs/DOM:element.getElementsByTagName&quot; target=&quot;_blank&quot;&gt;getElementsByTagName()&lt;/a&gt;. I decided to use the second function, getElementsByTagName() to read all the div containers in my webpage, and then check on the className to identify my rating widgets. This is why we'll give our rating widgets all the same class, namely &quot;&lt;i&gt;rating&lt;/i&gt;&quot;. To distinguish our widgets, we'll also give them a unique id, which is made of a common prefix &quot;&lt;i&gt;rating_&lt;/i&gt;&quot; and a number that could serve as ID in the database backend. The same class name also provides us a nice markup hook to apply our CSS to.&lt;br /&gt;
Finally, we need to store the value of the current ratings somewhere, without complicating the server-side code generation too much. I decided to simply store the value of the rating inside the container. The finished div container for a rating widget will then look like the following:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;div class=&quot;rating&quot; id=&quot;rating_1&quot;&amp;gt;3.5&amp;lt;/div&amp;gt;
&lt;/pre&gt;
It is most important that there are no line breaks in this code, as this will complicate the DOM tree unnecessarily. If you are uncertain about what I just said, please read the &lt;a href=&quot;http://www.w3schools.com/htmldom/default.asp&quot; target=&quot;_blank&quot;&gt;W3Schools HTML DOM Tutorial&lt;/a&gt; for further reference, since we are going to access the DOM directly from within our JavaScript.&lt;br /&gt;
As you can see, the div container is pretty easy to generate in the server-side scripting language, requiring most of the time only one line:
&lt;pre class=&quot;code&quot;&gt;
printf(&quot;&amp;lt;div class=\&quot;rating\&quot; id=\&quot;rating_%s\&quot;&amp;gt;%s&amp;lt;/div&amp;gt;&quot;, ratingId, rating);
&lt;/pre&gt;
Following the guideline that we strictly want to separate content from design and behaviour, this is all we have to do in our HTML code. The next chapter describes how to transform each div container into a fully functional rating widget.
    &lt;a name=&quot;javascript&quot;&gt;&lt;/a&gt;&lt;h3&gt;Adding dynamic behaviour&lt;/h3&gt;
    At the moment, the result is not very exciting and definitely not what we want to achieve. The next step is to add our JavaScript code to transform the plain div container into our dynamic rating widget. To do so, we first have to add our JavaScript file to the HTML header and then hook our initialization function to a window event. This is not a simple task and there are many different ways to achieve the same result. In his blog entry, Peter Michaux explains the &lt;a href=&quot;http://peter.michaux.ca/article/553&quot; target=&quot;_blank&quot;&gt;window.onload problem&lt;/a&gt; and presents several techniques to overcome some of the problems. Advanced Ajax frameworks usually include special events that have the same result, such as &lt;a href=&quot;http://dojotoolkit.org/&quot; target=&quot;_blank&quot;&gt;dojo's dojo.addOnLoad()&lt;/a&gt; or &lt;a href=&quot;http://docs.mootools.net/Window/Window-Base.js&quot; target=&quot;_blank&quot;&gt;mootools window.onDomReady()&lt;/a&gt;.&lt;br /&gt;
    In the first version of the code (the one that doesn't use any specific Ajax framework), I decide to simply hook my JavaScript function to the window.onload event:
&lt;pre class=&quot;code&quot;&gt;
window.onload = init_rating;
&lt;/pre&gt;
This line will simply call the function init_rating() when the page is fully loaded, parsed and rendered, which is also one of the problems of this method, in case your page happens to be very image heavy.&lt;br /&gt;
Our next step is to write the init_rating() function, which will do the following:
&lt;ol&gt;
&lt;li&gt;Find all the containers that represent a rating&lt;/li&gt;
&lt;li&gt;Read the rating value contained within the div container&lt;/li&gt;
&lt;li&gt;Remove the rating value text&lt;/li&gt;
&lt;li&gt;Create and add the images to the rating widget&lt;/li&gt;
&lt;li&gt;Add the animation effects&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Find all the rating containers&lt;/h4&gt;
Remember when I said that it was important to think about how to design the div container markup so we could use it in our JavaScript code? This is the place where our design will influence the complexity of the code and fortunately, we can simplify the code a lot. First, we use getElementsByTagName() to read all the div containers in our page into an array, then we loop over the array and use getAttribute to read the class name of the div container and finally, we filter all the elements out that don't have the class name we gave to our rating containers:
&lt;pre class=&quot;code&quot;&gt;
var ratings = document.getElementsByTagName('div');
for (var i = 0; i &amp;lt; ratings.length; i++)
{
    if (ratings[i].className != 'rating')
        continue;
        
    // rest of our code
}
&lt;/pre&gt;
The &quot;continue&quot; keyword in our JavaScript code, just like in most other programming languages, continues with the next iteration of the loop and prevents the execution of the rest of the code during that iteration.
&lt;h4&gt;Read the rating value&lt;/h4&gt;
Now that we have all the rating containers, we can start by reading the rating value written as text inside the div container. To read that value, we access the first child node of the div container, which is a TextNode, and access its &lt;a href=&quot;http://developer.mozilla.org/en/docs/DOM:element.nodeValue&quot; target=&quot;_blank&quot;&gt;nodeValue&lt;/a&gt;, which returns the text CDATA in case of text nodes. This is done by the following line:
&lt;pre class=&quot;code&quot;&gt;
var rating = ratings[i].firstChild.nodeValue;
&lt;/pre&gt;
You probably remember when I said earlier that it is important not to insert unnecessary line breaks in the HTML markup of the div container. Line breaks are also stored in TextNodes, modifying the text content from the desired &quot;4&quot; to a more complex &quot;\n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&quot; (taking 4 as example rating). It is still possible to convert that into a number but why introduce unnecessary complications if we simply think ahead and take care of the line breaks during HTML generation.
&lt;h4&gt;Remove the rating value&lt;/h4&gt;
Even though we just read the rating value, we still have the TextNode inside our div container. Of course, we need to remove that node first before we can start adding the images to the container. This is done using the &lt;a href=&quot;http://developer.mozilla.org/en/docs/DOM:element.removeChild&quot; target=&quot;_blank&quot;&gt; removeChild()&lt;/a&gt; DOM function:
&lt;pre class=&quot;code&quot;&gt;
ratings[i].removeChild(ratings[i].firstChild);
&lt;/pre&gt;
&lt;h4&gt;Add the images to the widget&lt;/h4&gt;
Finally, we have to create and add the images to our rating widget. I decided to use 4 different stars images (&lt;img src=&quot;./images/tutorials/rating/stars/rating_on.gif&quot; alt=&quot;Rating Star On&quot; /&gt;&lt;img src=&quot;./images/tutorials/rating/stars/rating_off.gif&quot; alt=&quot;Rating Star Off&quot; /&gt;&lt;img src=&quot;./images/tutorials/rating/stars/rating_half.gif&quot; alt=&quot;Rating Star Half&quot; /&gt;&lt;img src=&quot;./images/tutorials/rating/stars/rating_over.gif&quot; alt=&quot;Rating Star Over&quot; /&gt;), which represent the &quot;on&quot;, &quot;off&quot;, &quot;half&quot; and &quot;over&quot; states. The &quot;on&quot; state represents a fully coloured star and is used if the rating exceeds that star value. The &quot;off&quot; state represents a grey star and is used if the rating is inferior to that star's value. The &quot;half&quot; state adds the possibility to have decimal ratings but limiting the value of &quot;.5&quot; only. The &quot;over&quot; state is used when the mouse moves over the star, adding the flashy animation effect.&lt;br /&gt;
The first thing we need to do is to add a simple error check mechanism: we need to test if the rating value we read is within the allowed limits of our widget. To do so, we define a constant value called &quot;NUMBER_OF_STARS&quot;, which defines the upper bound of our widget. We then test if the rating is contained within the limits:
&lt;pre class=&quot;code&quot;&gt;
var NUMBER_OF_STARS = 10;

if (rating &gt; NUMBER_OF_STARS &amp;#124;&amp;#124; rating &amp;lt; 0)
            continue;
&lt;/pre&gt;
There is no graceful way to recover from that error, so I simply decided to continue with the next rating container and prevent that error from happening within the server backend.&lt;br /&gt;
Now, we need to loop over the number of stars that are displayed and check what the image graphic will be that is displayed on the star. Using the HTML DOM function &lt;a href=&quot;http://developer.mozilla.org/en/docs/DOM:document.createElement&quot; target=&quot;_blank&quot;&gt;createElement()&lt;/a&gt;, we initialize a new image and progressively add the respective values to that element. Of course, the first thing we are interested in is the displayed image: we could either test the rating against the current iteration value or we could decrement the rating value during each iteration and test against 1, 0.5 and 0. I decided to decrement the rating during each iteration, which presents me with three simple cases to test: if the rating is greater than or equal than 1, the star is &quot;on&quot;, if the rating is exactly 0.5, the star is &quot;half&quot; otherwise, the star is &quot;off&quot;. This results in the following code:
&lt;pre class=&quot;code&quot;&gt;
for (var j = 0; j &amp;lt; NUMBER_OF_STARS; j++)
{
    var star = document.createElement('img');
    if (rating &amp;gt;= 1)
    {
        star.setAttribute('src', './images/stars/rating_on.gif');
        rating--;
    }
    else if(rating == 0.5)
    {
        star.setAttribute('src', './images/stars/rating_half.gif');
        rating = 0;
    }
    else
    {
        star.setAttribute('src', './images/stars/rating_off.gif');
    }
    ratings[i].appendChild(star);
}
&lt;/pre&gt;
The last line appends the newly created image to the div container. If you run this code, you should already see the stars with the correct states.&lt;br /&gt;
Now, before adding the graphical effects, we need to reason a little about what is required to animate the correct rating widget. A rating widget is distinguished by its ID, which is a number and represented in the widget id, as explained in the HTML markup chapter. Every method that is applied onto our stars, need to have several information, like the widget id and the star id, which requires us to extract the widget id and save it somewhere. Since the widget is is stored inside the div container id, it is possible to extract that number from the id:
&lt;pre class=&quot;code&quot;&gt;
var widgetId = ratings[i].getAttribute('id').substr(7);
&lt;/pre&gt;
I use the &lt;a href=&quot;http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:substr&quot; target=&quot;_blank&quot;&gt;substr()&lt;/a&gt; method to cut off the first 7 characters representing &quot;rating_&quot; and leaving only the number.&lt;br /&gt;
Now that we have the widgetId, we can add the animation effects, using the &lt;a href=&quot;http://www.w3schools.com/jsref/jsref_onmouseover.asp&quot; target=&quot;_blank&quot;&gt;onMouseOver&lt;/a&gt; and &lt;a href=&quot;http://www.w3schools.com/jsref/jsref_onmouseout.asp&quot; target=&quot;_blank&quot;&gt;onMouseOut&lt;/a&gt; events:
&lt;pre class=&quot;code&quot;&gt;
var widgetId = ratings[i].getAttribute('id').substr(7);
star.setAttribute('id', 'star_'+widgetId+'_'+j);
star.onmouseover = new Function(&quot;evt&quot;, &quot;displayHover(&quot;+widgetId+&quot;, &quot;+j+&quot;);&quot;);
star.onmouseout = new Function(&quot;evt&quot;, &quot;displayNormal(&quot;+widgetId+&quot;, &quot;+j+&quot;);&quot;);
&lt;/pre&gt;
As you can see, I don't hoop onto the onClick event just yet, since I plan to do so using the specific functions of the Ajax frameworks. In order to prepare for those functions, I save the widgetId and the star number already in the image id, which I can use later to extract those values again.&lt;br /&gt;
Finally, there is one last thing to consider, which is important for the animation effect to work later on. The displayHover() function will replace the image sources with the &quot;over&quot; state graphic and we will lose the current state information of the star. To remember its state before the effect happens, we save its state in the class name, allowing us to read the class name and displaying the correct image again, as soon as the mouse leaves the widget. This results in this final function:
&lt;pre class=&quot;code&quot;&gt;
var NUMBER_OF_STARS = 10;

function init_rating()
{
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i &amp;lt; ratings.length; i++)
    {
        if (ratings[i].className != 'rating')
            continue;
            
        var rating = ratings[i].firstChild.nodeValue;
        ratings[i].removeChild(ratings[i].firstChild);
        if (rating &amp;gt; NUMBER_OF_STARS &amp;#124;&amp;#124; rating &amp;lt; 0)
            continue;
        for (var j = 0; j &amp;lt; NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating &amp;gt;= 1)
            {
                star.setAttribute('src', './images/stars/rating_on.gif');
                star.className = 'on';
                rating--;
            }
            else if(rating == 0.5)
            {
                star.setAttribute('src', './images/stars/rating_half.gif');
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', './images/stars/rating_off.gif');
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            star.onmouseover = new Function(&quot;evt&quot;, &quot;displayHover(&quot;+widgetId+&quot;, &quot;+j+&quot;);&quot;);
            star.onmouseout = new Function(&quot;evt&quot;, &quot;displayNormal(&quot;+widgetId+&quot;, &quot;+j+&quot;);&quot;);
            ratings[i].appendChild(star);
        } 
    }
}
&lt;/pre&gt;
&lt;h4&gt;Add the animation effects&lt;/h4&gt;
To finish the display of our rating widgets, we only need to complete the two function displayHover() and displayNormal(). This shouldn't be too much of a problem. Both functions receive as argument the widgetId and the star number, making it very easy to access the necessary elements in our DOM tree.&lt;br /&gt;
The displayHover() function has to iterate over all the stars within the correct rating widget up to the current star the mouse is over and change their graphics to the &quot;over&quot; state:
&lt;pre class=&quot;code&quot;&gt;
function displayHover(ratingId, star)
{
    for (var i = 0; i &amp;lt;= star; i++)
    {
        var star = document.getElementById('star_'+ratingId+'_'+i)
        star.setAttribute('src', './images/stars/rating_over.gif');
    }
}
&lt;/pre&gt;
As you can see, I don't have to use the troublesome childNodes in the DOM navigation but I can immediately use the star id that already contains the widgetId and the star number.&lt;br /&gt;
The displayNormal() function is similar to the displayHover() function with the only difference that the newly displayed image is dependent on the saved state inside the class name:
&lt;pre class=&quot;code&quot;&gt;
function displayNormal(ratingId, star)
{
    for (var i = 0; i &amp;lt;= star; i++)
    {
        var status = document.getElementById('star_'+ratingId+'_'+i).className;
        var star = document.getElementById('star_'+ratingId+'_'+i);
        star.setAttribute('src', './images/stars/rating_'+status+'.gif');
    }
}
&lt;/pre&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
Our current JavaScript allows us to transform specially marked div containers into animated rating widgets that we can use in specialized Ajax frameworks to link to our server backend. The HTML markup is completely separated from the JavaScript code, which will leave the user with a not very stylish but still visual display of the current rating in case JavaScript is disabled. I've put together a &lt;a href=&quot;./files/tutorials/rating/rating.zip&quot;&gt;ZIP file&lt;/a&gt; with the current result, containing an HTML file, a CSS file, the JavaScript file and the images.
&lt;a name=&quot;ajax&quot;&gt;&lt;/a&gt;&lt;h3&gt;Connect the widget to the server with different frameworks&lt;/h3&gt;
Please make sure to delete the &quot;window.onload=init_rating;&quot; line in the &quot;script.js&quot; file if you downloaded the .zip file, since we're using the specialized framework onload event.&lt;br /&gt;
The examples below are only an illustration of how this given task is achievable with a variety of different JavaScript frameworks. It should not be used as a reference about what framework is superior since not every framework follows the same route and has the same objectives. Before jumping to conclusion, you should read more sources and also hear every site. There are good reasons for dojo not having the $ operator and some interesting points can be found &lt;a href=&quot;http://ajaxian.com/archives/dollar-e-a-documentcreateelement-wrapper&quot; target=&quot;_blank&quot;&gt;here (in the comments)&lt;/a&gt;. When you need to decide which framework to chose, base your decision on your specific task and the framework you're most comfortable with.
&lt;h4&gt;The PHP backend&lt;/h4&gt;
Since we want to focus fully on the clientside frontend, we'll just use the most simple PHP script that reads the values it receives through the GET method and outputs a message in plaintext back to the client. Nothing fancy, nothing useful and totally outside of the scope of this tutorial:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;?php
  header('Content-type: text/plain');
  print &quot;Thank you for rating the content &quot;.$_GET['ratingID'].&quot; with the value &quot;.$_GET['value'];
?&amp;gt;
&lt;/pre&gt;
You'll probably notice that the lowest value a user can rate is 0, since our stars are numerated from 0 to NUMBER_OF_STARS-1. It is up to you to change your PHP scripts to take that into account and increment the received value adequately.
&lt;a name=&quot;dojo&quot;&gt;&lt;/a&gt;&lt;h4&gt;Using the Dojo framework&lt;/h4&gt;
The first framework I want to demonstrate is the &lt;a href=&quot;http://dojotoolkit.org/&quot; target=&quot;_blank&quot;&gt;Dojo 0.4.2&lt;/a&gt; javascript toolkit. Dojo uses a powerful library system, making it possible to only load (and download) the necessary packages for your application to work. Before you can use dojo, you have to download and install dojo, which basically comes down to unzipping the archive (in .tar.gzip format) and placing it in the correct folder, in my case &quot;./js/dojo&quot;. To use dojo in your JavaScript, you have to link to the dojo.js file that contains the framework to load additional packages. A more detailed tutorial can be found in the &lt;a href=&quot;http://dojotoolkit.org/docs/book&quot; target=&quot;_blank&quot;&gt;Dojo Book&lt;/a&gt;.&lt;br /&gt;
Before we're starting with the scripting, make sure that your header looks like the following, where script.js is the code described in chapter 3:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/script.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&quot;js/dojo/dojo.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;
Dojo uses an &lt;a href=&quot;http://dojotoolkit.org/node/88&quot; target=&quot;_blank&quot;&gt;Event System&lt;/a&gt; that makes it possible to listen to certain user actions and react accordingly. The event system is contained within &quot;dojo.event.*&quot; that we have to load before using it. Since Dojo overwrites the window load and unload events, it also provides a special event called dojo.addOnLoad() that we can use to add function calls to the window load event. This means we have to remove the window.onLoad event that we previously used and use the new method to call our function. Additionally, we need to add an event listener to every rating widget within our website.&lt;br /&gt;
To add an event listener, dojo provides a method called dojo.event.connect() that takes the object to listen to, the event and the callback function (the function that is executed when the event happens). Since we need such an event listener to every star in every rating widget, we have to use the same approach as earlier to read out all the div containers representing rating widgets and then iterate over all the childs and add the event listener:
&lt;pre class=&quot;code&quot;&gt;
function dojoInit()
{
    init_rating();
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i &amp;lt; ratings.length; i++)
    {
        if (ratings[i].getAttribute('class') != 'rating')
            continue;
        
        for (var j = 0; j &amp;lt; ratings[i].childNodes.length; j++)
        {
            dojo.event.connect(ratings[i].childNodes[j], 'onclick', 'submitRating');
        }
    }
}
dojo.addOnLoad(dojoInit);
&lt;/pre&gt;
As you can see, I used the dojo.addOnLoad() method to call the dojoInit() function when the site has fully loaded. The first thing I do in dojoInit() is to call our init_rating() function that does everything described in chapter 3. Afterwards, I read all the div containers in the page, filter them using their class name and finally iterate over all the children, using the DOM property childNodes[] and add the dojo event listener to them, having them call submitRating() when someone clicks on them.&lt;br /&gt;
As you can see, we have no possibility to specify the arguments to the submitRating() method. By default, dojo provides one argument, which is an &lt;a href=&quot;http://dojotoolkit.org/node/93&quot; target=&quot;_blank&quot;&gt;Event Object&lt;/a&gt;, that we can use to identify the object that was clicked. The &quot;target&quot; property of the Event Object is a pointer to the clicked object within the DOM tree and we'll use it to read its id, where we earlier stored the widgetId and the star number. The id has the format &quot;star_widgetId_number&quot; and we can use several consecutive substr() calls to read the values:
&lt;pre class=&quot;code&quot;&gt;
function submitRating(evt)
{
    var tmp = evt.target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
}
&lt;/pre&gt;
Now that we have the two values, all that is left to do is to create an asynchronous call to the submitRating.php script using the &lt;a href=&quot;http://dojotoolkit.org/node/80&quot; target=&quot;_blank&quot;&gt;dojo io package&lt;/a&gt;. This package provides a powerful tool to send and receive asynchronous server calls using the XMLHttpRequest object. I won't enter to much into the details of how the I/O package works but we'll use the &lt;a href=&quot;http://dojotoolkit.org/docs/book/intro_to_dojo_io&quot; target=&quot;_blank&quot;&gt;dojo io bind&lt;/a&gt; method that takes several arguments, among which the url, the callback handler and then content are the most important. The url is the location of our server-side script, the callback handler is the function that is called when a new event occurs (more specifically when the onreadystate event of the XHR object occurs) and the content are the GET arguments sent to the server. The code should look like this:
&lt;pre class=&quot;code&quot;&gt;
function submitRating(evt)
{
    var tmp = evt.target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    dojo.io.bind({
               url: './php/submitRating.php',
               handler: function(type, data, evt)
                  {
                    if (type == 'error')
                      alert('Error submitting the rating!');
                    else
                      alert(data);
                  },
               content: {'ratingID': widgetId, 'value':starNbr}
            });
}
&lt;/pre&gt;
As you can see, I used an anonymous function as callback handler, that takes the dojo predefined three arguments type, data and evt of which type and data are the most important. The &quot;type&quot; argument is used to check if something went wrong during the request (you could also register an error handler using the &quot;error&quot; argument of the dojo.io.bind() method) and &quot;data&quot; contains the received content from the server-side script, in this case the plaintext message.&lt;br /&gt;
The entire code, using the dojo 0.4.2 framework, should look like the following:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
dojo.require(&quot;dojo.event.*&quot;);
dojo.require(&quot;dojo.io.*&quot;);

function submitRating(evt)
{
    var tmp = evt.target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    dojo.io.bind({
               url: './php/submitRating.php',
               handler: function(type, data, evt)
                  {
                    if (type == 'error')
                      alert('Error submitting the rating!');
                    else
                      alert(data);
                  },
               content: {'ratingID': widgetId, 'value':starNbr}
            });
}

function dojoInit()
{
    init_rating();
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i &amp;lt; ratings.length; i++)
    {
        if (ratings[i].getAttribute('class') != 'rating')
            continue;
        
        for (var j = 0; j &amp;lt; ratings[i].childNodes.length; j++)
        {
            dojo.event.connect(ratings[i].childNodes[j], 'onclick', 'submitRating');
        }
    }
}
dojo.addOnLoad(dojoInit);
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;a name=&quot;mootools&quot;&gt;&lt;/a&gt;&lt;h4&gt;Using the mootools framework&lt;/h4&gt;
&lt;a href=&quot;http://mootools.net/&quot; target=&quot;_blank&quot;&gt;mootools&lt;/a&gt; is a &lt;i&gt;compact, modular, Object-Oriented&lt;/i&gt; JavaScript framework that I used initially because of its powerful Accordion widget. The small size, the customized download and the nice effects makes it a good choice for intermediate programmers that want something small that &quot;just works&quot;.&lt;br /&gt;
Before we can even think about using mootools though, we need to assemble all the important parts in the download section of the page. This can be a little confusing for beginners, since a missing package keeps your script from running correctly (if at all). It is of course possible to just select everything (for which there exists a shortcut, simply enter &quot;javascript:Download.all();&quot; in your browser's location bar and press Enter) but that increases unnecessarily the code size. Remember that every visitor to your site has to download the JavaScript file so try to keep it as small as possible.&lt;br /&gt;
The required parts for our script to work are: &quot;Element&quot;, &quot;Event&quot;, &quot;Dom&quot;, &quot;Window.Base&quot; and &quot;Ajax&quot;. The &quot;Element&quot; package allows us to add function callbacks to element events and we'll use it to hook onto the onClick event of our stars. The &quot;Event&quot; package is provides cross-browser methods to manage events and although we only use the target property of our events, I consider this to be a good idea. The &quot;Dom&quot; package allows us to select the div containers using their css class. The &quot;Window.Base&quot; package contains window events, such as the onDomReady event that we'll use to call our initialization function instead of window.onload(). Finally, the &quot;Ajax&quot; package extends the XMLHttpRequest wrapper package XHR with additional methods and functionalities and provides a cross-browser method to send Ajax requests.&lt;br /&gt;
Before we'll start with the JavaScript code, we have to add the mootools library to our website. This is simply done by writing:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/mootools/mootools.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;
The structure of the program will strongly resemble to the dojo code. The things that do change are the initialization call, the access to the rating widgets, the event listener and the Ajax request. As already mentioned, the &quot;Window.Base&quot; package provides us with a method called &lt;a href=&quot;http://docs.mootools.net/Window/Window-Base.js#Window.window.onDomReady&quot; target=&quot;_blank&quot;&gt;window.onDomReady()&lt;/a&gt; that takes as argument the function to call when the DOM is ready, which means when the document tree has loaded, &lt;i&gt;not&lt;/i&gt; waiting for images (which is a good thing). The use of that method is shown here:
&lt;pre class=&quot;code&quot;&gt;
window.onDomReady(mooInit);
&lt;/pre&gt;
The mooInit() function does the same thing as the dojoInit() function; it finds all the stars within our page and adds an event listener to the onClick event. To access the rating widgets, we can use the &lt;a href=&quot;http://docs.mootools.net/Native/Element.js#$$&quot; target=&quot;_blank&quot;&gt;$$&lt;/a&gt; operator, that allows us to access elements by their CSS class name, to get all the rating widgets and the &lt;a href=&quot;http://docs.mootools.net/Native/Element.js#Element.getChildren&quot; target=&quot;_blank&quot;&gt;getChildren()&lt;/a&gt; method to get an array of all the stars images. To iterate over the array, mootools provides a method called &lt;a href=&quot;http://docs.mootools.net/Native/Array.js#Array.each&quot; target=&quot;_blank&quot;&gt;each&lt;/a&gt; that is located in the Array package. each expects as argument a function and passes a reference to the current element as argument to the function. The &quot;Element&quot; package provides us with the method &lt;a href=&quot;http://docs.mootools.net/Native/Element.js#Element.addEvent&quot; target=&quot;_blank&quot;&gt;addEvent()&lt;/a&gt; which attaches an event listener to a DOM element:
&lt;pre class=&quot;code&quot;&gt;
function mooInit()
{
    init_rating();
	$$(&quot;.rating&quot;).getChildren().each(function(star) {
	    star.addEvent('click', submitRating);
    });
}
&lt;/pre&gt;
As you can see, I defined an anonymous function that has one argument called star. This function is called once for every item in the array returned by getChildren(). Please notice that the addEvent() is a method of the DOM element, contrary to dojo where the DOM element is an argument. Now, all that is left to do is to create the callback function submitRating(evt) and to send the Ajax request. mootools provides a specialized package called &quot;Event&quot; that provides a cross-browser compatible way of using events. The first thing we need to do within our function is to initialize an Event object. This is done by passing the standard event argument as argument to the &lt;a href=&quot;http://docs.mootools.net/Native/Event.js#Event&quot;  target=&quot;_blank&quot;&gt;Event object&lt;/a&gt;. Then we can use the Event object's target property and don't worry about the underlying implementation. &lt;br /&gt;
In order to send an Ajax request, mootools provides a special package called &lt;a href=&quot;http://docs.mootools.net/Remote/Ajax.js&quot; target=&quot;_blank&quot;&gt;Ajax.js&lt;/a&gt;. The object constructor takes two arguments, the url and the options. Among all the possible options, only two are useful for our purpose, the method and the onComplete event handler. For our rating widget, we'll specify that the method will be &quot;get&quot; and the event handler will be once again an anonymous function that simply alerts the returned message. To send the request, the Ajax object has the request() method. Contrary to the dojo framework, that provided an option where we could specify the get arguments and values, mootools provides no special tool to specify those values. We are required to construct the respective url ourselves. To facilitate this task, mootools provides a method called &lt;a href=&quot;http://docs.mootools.net/Remote/Ajax.js#Object.toQueryString&quot; target=&quot;_blank&quot;&gt;Object.toQueryString()&lt;/a&gt; that takes key/pair values and transform it into the corresponding url. The code to send the Ajax request should look like the following:
&lt;pre class=&quot;code&quot;&gt;
function submitRating(evt)
{
    var tmp = new Event(evt).target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    var queryString = Object.toQueryString({'ratingID': widgetId, 'value': starNbr});
    var myAjax = new Ajax('./php/submitRating?'+queryString, {
        method: 'get',
        onComplete: function(result)
            {
                alert(result);
            }
        }).request();
}
&lt;/pre&gt;
The entire code, using the mootools 1.00 release version, should look like the following:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/mootools/mootools.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;


function submitRating(evt)
{
    var tmp = new Event(evt).target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    var queryString = Object.toQueryString({'ratingID': widgetId, 'value': starNbr});
    var myAjax = new Ajax('./php/submitRating?'+queryString, {
        method: 'get',
        onComplete: function(result)
            {
                alert(result);
            }
        }).request();
}


function mooInit()
{
    init_rating();
	$$(&quot;.rating&quot;).getChildren().each(function(star) {
	    star.addEvent('click', submitRating);
    });
}

window.onDomReady(mooInit);
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;a name=&quot;jquery&quot;&gt;&lt;/a&gt;&lt;h4&gt;Using the jQuery framework&lt;/h4&gt;
The &lt;a href=&quot;http://jquery.com/&quot; target=&quot;_blank&quot;&gt;jQuery&lt;/a&gt; library &lt;i&gt;is designed to change the way that you write JavaScript&lt;/i&gt; and oh boy, they don't exaggerate. jQuery might not have the fanciest widgets or the the most beautiful effects ever but you can do some really cool stuff with it. But first things first, before we can do anything with it, we need to add the JavaScript file to our website. This is done by first downloading the &lt;a href=&quot;http://code.jquery.com/jquery-latest.pack.js&quot; target=&quot;_blank&quot;&gt;latest jQuery release&lt;/a&gt; and then add it to the code, using:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/jquery/jquery.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;
Once again, we have to deal with the same problems as usual, which is add the initializer to the windows.onload event, add an event listener to the click event of every star in every rating widget and finally send the Ajax request.&lt;br /&gt;
To add a function call to the window.onload event, jQuery offers its own method which is called &lt;a href=&quot;http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29&quot; target=&quot;_blank&quot;&gt;$(document).ready()&lt;/a&gt;. Basically, this uses already one of the most powerful features of jQuery, which is the &lt;a href=&quot;http://docs.jquery.com/Selectors&quot; target=&quot;_blank&quot;&gt;Selector&lt;/a&gt;. It is a combination of CSS 1-3, XPath and some custom modification and allows you virtually to access every element within your page. In this simple example, we use the selector to access the document element and attach a function to the ready event:
&lt;pre class=&quot;code&quot;&gt;
$(document).ready(jqueryInit);
&lt;/pre&gt;
The next problem would be to add an event listener to each star image in every rating widget. While mootools offered us the possibility to use the $$ operator to access all the div containers that had the &quot;rating&quot; class, jQuery offers us a little more. Not only can we use the Selector to access the div containers by their class name, we can also use the XPath expression to access their children of type &quot;img&quot;. If that's not enough, the &lt;a href=&quot;http://docs.jquery.com/Events#bind.28_type.2C_data.2C_fn_.29&quot; target=&quot;_blank&quot;&gt;bind()&lt;/a&gt; method, that is used to add an event listener to an element event, can be used on collection of objections (instead of iterating over all the objects of a collection and using the method on each object individually). Not convinced? Here's the code:
&lt;pre class=&quot;code&quot;&gt;
function jqueryInit()
{
    init_rating();
    $('div.rating/img').bind('click', submitRating);
}
&lt;/pre&gt;
There's another thing worth mentioning: the exact same thing &lt;i&gt;could&lt;/i&gt; maybe be possible with the other frameworks I presented but honestly, I couldn't find it in their documentation. jQuery not only offers a good JavaScript framework, but also a great documentation that made it very easy to find all those things.&lt;br /&gt;
Finally, all that is left is to send the Ajax request. This is done by using the &lt;a href=&quot;http://docs.jquery.com/Ajax#.24.get.28_url.2C_params.2C_callback_.29&quot; target=&quot;_blank&quot;&gt;$.get()&lt;/a&gt; function, which is basically an higher-level abstraction of their low-level Ajax implementation. The function has three argument, the url, the parameters and the callback function:
&lt;pre class=&quot;code&quot;&gt;
function submitRating(evt)
{
    var tmp = evt.target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    $.get(&quot;./php/submitRating.php&quot;,
       { ratingID: widgetId, value: starNbr},
       function(data){
         alert(data);
       }
     );
}
&lt;/pre&gt;
The entire code, using the jQuery 1.1.2 library, should look like the following:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/jquery/jquery.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;

function submitRating(evt)
{
    var tmp = evt.target.getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    $.get(&quot;./php/submitRating.php&quot;,
       { ratingID: widgetId, value: starNbr},
       function(data){
         alert(data);
       }
     );
}

function jqueryInit()
{
    init_rating();
    $('div.rating/img').bind('click', submitRating);
}

$(document).ready(jqueryInit);
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;a name=&quot;prototype&quot;&gt;&lt;/a&gt;&lt;h4&gt;Using the prototype framework&lt;/h4&gt;
Next on our list is the &lt;a href=&quot;http://prototypejs.org/&quot; target=&quot;_blank&quot;&gt;prototype&lt;/a&gt; framework, that &lt;i&gt;aims to ease development of dynamic web applications&lt;/i&gt;. Even though I've never used prototype before, the &lt;a href=&quot;http://prototypejs.org/api&quot; target=&quot;_blank&quot;&gt;prototype API&lt;/a&gt; has a great documentation since mid-January and if you know what you're looking for, you should have no problems finding it (apart from the window.onload event that was hidden in an example somewhere deep within).&lt;br /&gt;
I am using the &lt;a href=&quot;http://prototypejs.org/assets/2007/1/18/prototype.js&quot; target=&quot;_blank&quot;&gt;1.5.0 version&lt;/a&gt; and, as usual, the very first thing to do is to add the script to your header:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/prototype/prototype.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;
If you read through the other framework examples, you should know by now that we have three simple tasks to complete: hook an initialization function to the window.onload event, add a &quot;click&quot; event listener to each star in every rating widget and send the Ajax request. The Event manager in prototype uses a method called &lt;a href=&quot;http://prototypejs.org/api/event/observe&quot; target=&quot;_blank&quot;&gt;observe()&lt;/a&gt; that registers a callback function to an element event. To hook onto the onload event, we need to observe the &quot;window&quot; element and register the &quot;load&quot; event:
&lt;pre class=&quot;code&quot;&gt;
Event.observe(window, 'load', prototypeInit);
&lt;/pre&gt;
In order to register an event handler for every star in each rating widget, prototype provides several &lt;a href=&quot;http://prototypejs.org/api/utility&quot; target=&quot;_blank&quot;&gt;utility methods&lt;/a&gt;, such as the $() method to return an element based on its id or the $$() method that returns elements based on their CSS class. In their Enumerable package, prototype has several methods that work on collections of values. One of these methods is the &lt;a href=&quot;http://prototypejs.org/api/enumerable/each&quot; target=&quot;_blank&quot;&gt;each()&lt;/a&gt; method that basically iterates over every element of a collection and returns the element, similar to foreach() operations in programming languages such as PHP. Using the each() method, our prototypeInit() function looks like the following:
&lt;pre class=&quot;code&quot;&gt;
function prototypeInit()
{
    init_rating();
    $$('.rating').each(function(n){
                        n.immediateDescendants().each(function(c){
                            Event.observe(c, 'click', submitRating);
                        });
                    });
}
&lt;/pre&gt;
As you can see, I use the $$() method to get an Enumeration of div containers (the rating widgets), then use each() to iterate over each div container. The parameter &quot;n&quot; is the Enumerable, a reference to the current object in the iteration. It is not possible to apply each to the standard &quot;childNodes&quot; property of a DOM element, so you have to use the method &lt;a href=&quot;http://prototypejs.org/api/element/immediateDescendants&quot; target=&quot;_blank&quot;&gt;immediateDescendants()&lt;/a&gt; to get a collection of star images. Finally, we apply each() on that collection to iterate over each star image and use observe to register an event handler to it.&lt;br /&gt;
For our submitRating event, prototype provides a cross-browser way of accessing event information, that are explained in the &lt;a href=&quot;http://prototypejs.org/api/event&quot; target=&quot;_blank&quot;&gt;Event&lt;/a&gt; package. We only have to initialize a new Event object and pass our event as argument.&lt;br /&gt;
Finally, to send the Ajax request, we'll use the &lt;a href=&quot;http://prototypejs.org/api/ajax/request&quot; target=&quot;_blank&quot;&gt;Ajax.Request()&lt;/a&gt; method that takes two arguments, the url and the options. The only &lt;a href=&quot;http://prototypejs.org/api/ajax/options&quot; target=&quot;_blank&quot;&gt;Ajax Options&lt;/a&gt; that interest us are the &quot;method&quot;, the &quot;parameters&quot; as well as the &quot;onSuccess&quot; handler. Please note that there are several existing callback handlers, such as onComplete, onFailure, onException and many more, but for the sake of simplicity, we'll only use the onSuccess handler and discard the rest:
&lt;pre class=&quot;code&quot;&gt;
function submitRating(evt)
{
    var tmp = Event.element(evt).getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    new Ajax.Request('./php/submitRating', {
      method: 'get',
      parameters: {ratingID: widgetId, value: starNbr},
      onSuccess: function(transport) {
          alert(transport.responseText);
      }
    });
}
&lt;/pre&gt;
The entire code, using the prototype 1.5.0 framework, should look like the following:
&lt;pre class=&quot;code&quot;&gt;
&amp;lt;script src=&quot;js/prototype/prototype.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;

function submitRating(evt)
{
    var tmp = Event.element(evt).getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    new Ajax.Request('./php/submitRating', {
      method: 'get',
      parameters: {ratingID: widgetId, value: starNbr},
      onSuccess: function(transport) {
          alert(transport.responseText);
      }
    });
}

function prototypeInit()
{
    init_rating();
    $$('.rating').each(function(n){
                        n.immediateDescendants().each(function(c){
                            Event.observe(c, 'click', submitRating);
                        });
                    });
}

Event.observe(window, 'load', prototypeInit);
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;a name=&quot;conclusion&quot;&gt;&lt;/a&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;
You can download a fully functional finished script, using the jQuery framework, from &lt;a href=&quot;./files/tutorials/rating/rating_final.zip&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
I hope that you learned something new and that by now, you should have a fully functional Ajax rating widget. Once again, I'd like to specify that my intention is to show you an approach to coding with Ajax and less to provide you with a finished script. I hope I could show you how I separated the steps into different tasks and how each task was solved, using several different JavaScript frameworks. If you have anything to add, feel free to &lt;a href=&quot;contact.php&quot;&gt;drop me a note&lt;/a&gt; and I'll make sure to include any necessary changes into this tutorial.</description><link>http://www.progressive-coding.com/tutorial.php?id=6</link><guid>http://www.progressive-coding.com/tutorial.php?id=6</guid></item><item><title>News: Rating Widget Tutorial Update</title><description>I received a lot of feedback when this tutorial was published on &lt;a href=&quot;http://ajaxian.com/&quot; target=&quot;_blank&quot;&gt;Ajaxian&lt;/a&gt; and was informed that several things weren't Internet Explorer compatible. I went back and changed several lines of code and updated the tutorial and the zip archive. Basically, IE doesn't seem to like when you try to access an elements class name using the getAttribute() method. Additionally, I had to change the onmouseover/onmouseout event handlers to have them work correctly.&lt;br /&gt;
Another reader informed me that he had problems using the downloaded zip archive with the frameworks. This is because the downloaded script.js still contains the window.onload event handler that needs to be removed and replaced with the respective framework onload event handler. I added a mention to the tutorial that should explain this problem.&lt;br /&gt;
&lt;br /&gt;
I would like to thank everyone that took the time to send me constructive feedback about my tutorial, such as framework development teams that informed me how I could optimize even further several lines of code. I've put up an &lt;a href=&quot;http://www.progressive-coding.com/files/tutorials/rating/rating_final.zip&quot;&gt;ZIP archive&lt;/a&gt; with the finished script, using the jQuery framework. You should be able to unpack and use it without any modifications.</description><link>http://www.progressive-coding.com/news.php?id=8</link><guid>http://www.progressive-coding.com/news.php?id=8</guid></item><item><title>News: Creating an Ajax Rating Widget</title><description>As some of you might have noticed already, I've put up a new tutorial about &lt;a href=&quot;http://www.progressive-coding.com/tutorial.php?id=6&quot;&gt;Creating an Ajax Rating Widget&lt;/a&gt;. The tutorial describes in great detail the implementation steps from the simple HTML markup to the use of an JavaScript framework to connect the widget to a server backend. For those of you that are already familiar with a certain framework, I ported the code to several different frameworks and explain the differences and similarities between them.&lt;br /&gt;
The tutorial was recently published on &lt;a href=&quot;http://ajaxian.com/&quot; target=&quot;_blank&quot;&gt;Ajaxian&lt;/a&gt; and if you are satisfied with the tutorial, you can  vote for it &lt;a href=&quot;http://ajaxian.com/archives/creating-an-ajax-rating-widget&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.</description><link>http://www.progressive-coding.com/news.php?id=7</link><guid>http://www.progressive-coding.com/news.php?id=7</guid></item><item><title>Tutorial: Creating a CSS Design from Scratch</title><description>&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#analyze&quot;&gt;Analyze, identify and structure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#global&quot;&gt;Creating the global layout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#header&quot;&gt;Designing the header&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#sidebar&quot;&gt;Designing the sidebars&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#center&quot;&gt;Designing the center part&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

        &lt;a name=&quot;introduction&quot;&gt;&lt;/a&gt;&lt;h3&gt;
          Introduction
        &lt;/h3&gt;
        &lt;div style=&quot;float: right; padding: 20px&quot;&gt;
          &lt;a href=&quot;./images/tutorials/queatrix/design_final.png&quot; rel=&quot;lightbox&quot;&gt;&lt;img src=&quot;./images/tutorials/queatrix/design_final_small.png&quot; border=&quot;0&quot; alt=&quot;Small preview of final Design&quot;&gt;&lt;/a&gt;
        &lt;/div&gt;
        For years, the easiest way to convert a design, that you or a fellow designer carefully composed in Photoshop, was to cut it in several rectangular parts, wrap a table around, spanning columns and rows as you pleased and you're done. Nowadays, this has become a &quot;little&quot; more complicated, as web standardists enter the scene and tell you what you should do. Don't understand me wrong, this is a good thing and I'm a full supporter of most of the standards and ideas that are promoted from organizations like the W3C. Unfortunately, it takes a lot of practice, trial and error and experience to do things the correct way; if there is a correct way.&lt;br&gt;
        &lt;br&gt;
        I want to show you , using as reference a website that I recently visited and had the pleasure to look at the html code, how it is possible to draw a clean line between content &amp;amp; design, just the way it is intended. The objective is to convert the old design, using non-standard HTML, evil hacks and overly complicated CSS into a fully cross-browser compatible, structured design that is easily extendable and modifiable. The end result can be seen &lt;a href=&quot;http://www.progressive-coding.com/projects/queatrix/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.
        &lt;a name=&quot;analyze&quot;&gt;&lt;/a&gt;&lt;h3&gt;
          Analyze, identify and structure
        &lt;/h3&gt;The first step in the design process is to look at the design and identify the main components. Imagine a castle made of different logo blocks: your task is to analyze the castle, identify the different parts and use the correct logo blocks to be able to build that castle. This is probably the hardest part of the entire process and requires careful planing. You have to break the design into separate pieces, as if you'd cut the image with a knife into small regions, each region representing a new element in the design.&lt;br&gt;
        Fortunately, it is very easy to identify the structure of this design: one horizontal header, and a three column layout for the body. Several other things you could notice is that the website width is 100%, the two sidebars are of fixed with and each represent element within the sidebars is independent from each other and could be loaded dynamically as modules.&lt;br&gt;
        I wouldn't worry at this stage about how to design the elements, our current goal is to identify and structure. Another important concept is to identify parts that share common properties. Even though the sidebars have 6 different boxes, you probably noticed that they have several things in common (like the way the background is designed, the position of the title, the icon left of the title, the width and the position of the content). Generally, if you are able to regroup several objects, you can design them using the &lt;a href=&quot;http://www.w3.org/TR/html401/struct/global.html#adef-class&quot; target=&quot;_blank&quot;&gt;class attribute&lt;/a&gt;, otherwise, if they are unique, it is best to assign them a unique &lt;a href=&quot;http://www.w3.org/TR/html401/struct/global.html#adef-id&quot; target=&quot;_blank&quot;&gt;identifier id&lt;/a&gt; and reference them directly in CSS.
        &lt;div class=&quot;quote&quot;&gt;
          The class attribute, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances.
        &lt;/div&gt;
        &lt;a name=&quot;global&quot;&gt;&lt;/a&gt;&lt;h3&gt;Creating the global layout&lt;/h3&gt;
        Let's start our design process by creating the basic layout for our website. We do this by adding elements to our HTML code and position them according to our desired result with CSS. A rule of thumb is that everything related to the visual aspect of our website is left to CSS and HTML represents only the content. If we want to add an element to our website, we add a new &lt;a href=&quot;http://www.w3.org/TR/html401/struct/global.html#edef-DIV&quot; target=&quot;_blank&quot;&gt;DIV container&lt;/a&gt;, which is nothing else than a generic language/style container and imposes no other presentational idiom on the content. A div container is nothing else than a box, that can be modified using a CSS stylesheet, the div is accessed by the mean of his class and/or id. This being said, the structure of our HTML document will look like this, if we only add the header and the three columns.
        &lt;pre class=&quot;code&quot;&gt;
&amp;lt;body&amp;gt;
    &amp;lt;div id=&quot;header&quot;&amp;gt;header&amp;lt;/div&amp;gt;
    &amp;lt;div id=&quot;wrapper&quot;&amp;gt;
        &amp;lt;div id=&quot;container&quot;&amp;gt;
            &amp;lt;div id=&quot;center&quot;&amp;gt;center&amp;lt;/div&amp;gt;
            &amp;lt;div id=&quot;leftSidebar&quot;&amp;gt;left&amp;lt;/div&amp;gt;
            &amp;lt;div id=&quot;rightSidebar&quot;&amp;gt;right&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/pre&gt;You probably notice that I put a div contained called &quot;wrapper&quot; around three of the containers. I did this for several reasons: first of all, they can be considered as one global element, compared to the header file, which is the content of our website. If we cut the image in parts with our knife, the first horizontal cut under the header would create two parts, the header and the page body. The wrapper container represents just that. The need for yet another container becomes clear in a second, when I discuss the problems with a multi-column design.&lt;br&gt;
        Another reason is that the wrapper allows us to apply a CSS stylesheet to all three of the elements at the same time, which is very important for what we want to achieve now: a three-column liquid layout. This is probably the most argumented and discussed subject in the world of web design and there is no generally accepted way of doing this. I copied my implementation from &lt;a href=&quot;http://alistapart.com/articles/multicolumnlayouts&quot; target=&quot;_blank&quot;&gt;A List Apart&lt;/a&gt;, which is a great resource for everything website-related. Author &lt;a href=&quot;http://alttag.org/&quot; target=&quot;_blank&quot;&gt;Alan Pearce&lt;/a&gt; explains in great detail how he arrived to his CSS design and how to use it in an own design; which is exactly what we're going to do now:
        &lt;pre class=&quot;code&quot;&gt;
body {
background-color:#2f2f2f;
margin:0;
padding:0
}

#header {
height:141px;
background-color: red;
}

#wrapper {
font-family:Tahoma, arial, verdana, courier;
padding:0 200px
}

#container {
float:left;
width:100%;
border-left:200px solid #2f2f2f;
border-right:200px solid #2f2f2f;
margin-left:-200px;
margin-right:-200px;
display:inline
}

#leftSidebar {
background-color: blue;
float:left;
width:200px;
margin-left:-200px;
position:relative
}

#center {
background-color: yellow;
float:left;
width:100%;
margin-right:-100%
}

#rightSidebar {
background-color: white;
float:right;
width:200px;
margin-right:-200px;
position:relative
}

&lt;/pre&gt;A few things you should note is that I added a margin and padding value for the body element, which essentially takes away the default margin between the browser borders and the content and which makes it possible to have our design fill up the entire width of the screen as well as go up till the very top of the window. I also added distinctive colors to each of the elements, to identify potential problems and check if everything shows up correctly. If you did everything as described, you should end up with &lt;a href=&quot;./images/tutorials/queatrix/design_step1.png&quot; rel=&quot;lightbox&quot;&gt;something like this&lt;/a&gt;, which is a very good start to continue the rest of the design from.
        &lt;a name=&quot;header&quot;&gt;&lt;/a&gt;&lt;h3&gt;Designing the header&lt;/h3&gt;
        Since we have already our header container, the only thing left to do is to enter the header content (the logo title and slogan) and to design the header visually. Since the logo title and the slogan are unique within the website, I decide to give them a unique id. The line beneath the logo title could be done with an image, but to stay true to the nature of CSS, I decide to draw the line using the bottom-border of the logo title.&lt;br&gt;
        The HTML code once again only contains the content and the entire look is done within the CSS code:
        &lt;pre class=&quot;code&quot;&gt;
&amp;lt;div id=&quot;header&quot;&amp;gt;
    &amp;lt;div id=&quot;logo&quot;&amp;gt;
        &amp;lt;div id=&quot;logo_title&quot;&amp;gt;
            Queatrix's.com
        &amp;lt;/div&amp;gt;
        &amp;lt;div id=&quot;logo_slogan&quot;&amp;gt;
            program blog and archive.
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;
    Let's start with the header background. If you give it a close look, you'll notice that the background is made up of repeatable, regular, diagonal stripes. Everything which is repeatable and regular is good, since it makes it possible to cut a very specific part out of the image and repeat it horizontally over the whole width, which saves not only space as the image is smaller, but the site will load faster (less image data to transfer) and the design is resolution independent. To cut the needed part out from the image, I usually start in the lower left corner and select as many pixels to the right until the texture starts to repeat itself (five pixels in this case). Notice that the background has a vertical gradient and a much darker part at the bottom, which makes it impossible to repeat vertically, otherwise we could have cut a small rectangle of only 5x5 pixels and repeated in every direction to fill the entire div container. If you perform the cut correctly, you should end up with an 5x141 pixel image.&lt;br&gt;
        The CSS code for the text is pretty standard, the only thing to note is the use of the bottom-border to simulate the horizontal line underneath the title logo:
        &lt;pre class=&quot;code&quot;&gt;

#header {
height:141px;
background:url(../images/header_bg.png) repeat-x 0 0
}

#logo {
color:#fff;
padding:30px 0 0 50px
}

#logo_title {
font-family:georgia, tahoma, arial narrow, arial;
font-size:26px;
font-weight:400;
text-transform:uppercase;
letter-spacing:2px;
border-bottom:1px solid #7ea3ac;
width:160px
}

#logo_slogan {
font-size:10px;
font-family:tahoma, arial
}
		&lt;/pre&gt;If you followed the instructions carefully, you should now have the &lt;a href=&quot;./images/tutorials/queatrix/design_step2.png&quot; rel=&quot;lightbox&quot;&gt;following result&lt;/a&gt;.
		&lt;a name=&quot;sidebar&quot;&gt;&lt;/a&gt;&lt;h3&gt;Designing the sidebars&lt;/h3&gt;
		Now that we're done with the header, we'll focus on the sidebars. The left and right sidebar can be treated equally, as their content is independent of their position within the side. Each sidebar is made up of different modules and as you might guess, each module is a div container. The modules share common properties, which is best represented using a class, since one CSS definition of the class will automatically be applied to every member of that class. However, each module also has its very own properties, like the background color. To do this, not only do I define a class for the sidebar modules, but I also give each sidebar its very own id, that can be used to modify its look without changing the other modules.&lt;br /&gt;
		To save some space, I only post the HTML code from the left sidebar, the right sidebar modules are done in exactly the same way:
		&lt;pre class=&quot;code&quot;&gt;
&amp;lt;div id=&quot;leftSidebar&quot;&amp;gt;
    &amp;lt;div id=&quot;categories&quot; class=&quot;sidebar_item&quot;&amp;gt;
        &amp;lt;div class=&quot;sidebar_title&quot;&amp;gt;
            Categories
        &amp;lt;/div&amp;gt;
        &amp;lt;ul&amp;gt;
            &amp;lt;li&amp;gt;
                &amp;lt;a href=&quot;http://cboard.cprogramming.com/&quot; target=&quot;_blank&quot;&amp;gt;link test&amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;Games
            &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div id=&quot;resources&quot; class=&quot;sidebar_item&quot;&amp;gt;
        &amp;lt;div class=&quot;sidebar_title&quot;&amp;gt;
            Other Resources
        &amp;lt;/div&amp;gt;
        &amp;lt;ul&amp;gt;
            &amp;lt;li&amp;gt;Tools
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;Games
            &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div id=&quot;foo&quot; class=&quot;sidebar_item&quot;&amp;gt;
        &amp;lt;div class=&quot;sidebar_title&quot;&amp;gt;
            Foo
        &amp;lt;/div&amp;gt;
        &amp;lt;ul&amp;gt;
            &amp;lt;li&amp;gt;Tools
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;Games
            &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
		&lt;/pre&gt;
		Once again, there is no markup that would change the visual aspect of modules. Please note that each module has an id and a class. The class defines the shared attributes like width, minimal height, font attributes and positions while the id specifies the particular aspects like background. Since two modules can share the same background color, it would have been possible to define a class &quot;orange_background&quot; and add that class to each module that has an orange background.&lt;br/&gt;
		The CSS code for the class sidebar_item can be found here:
		&lt;pre class=&quot;code&quot;&gt;
.sidebar_item {
float:left;
width:100%;
min-height: 160px;
margin:1px 0 0;
padding:0
}
		&lt;/pre&gt;
		&lt;div style=&quot;float: right; padding: 20px&quot;&gt;
          &lt;a href=&quot;./images/tutorials/queatrix/sidebar_module_big.jpg&quot; rel=&quot;lightbox&quot;&gt;&lt;img src=&quot;./images/tutorials/queatrix/sidebar_module.jpg&quot; border=&quot;0&quot; alt=&quot;Explanation of how the sidebar module is designed&quot;&gt;&lt;/a&gt;
        &lt;/div&gt;
        Now, let's start designing the modules themselves. Since the background has no defined elements apart from the vertical gradient, it is possible to only use an image of one pixel width and repeat that image horizontally. Since the module is of unknown height, we have to limit the gradient to a certain height, or else we'd stretch the image which would result in a horrible quality. Fortunately, the gradient results in a constant color value at a certain height, so all we have to do is cut out a region whose height is big enough to hold the gradient, and then we simply fill the background of the container with the same color than the color value at the bottom of the image.&lt;br/&gt;
        This might sound a little complicated but in fact, it's only one simple CSS line:
        &lt;pre class=&quot;code&quot;&gt;
#categories {
background:#e67701 url(../images/orange_gradient_bg.png) repeat-x 0 0
}            
        &lt;/pre&gt;
        That one line defines that the color fill value is #e67701, that a background image is uses (of size 1x160), that it should be repeated horizontally and that it's fixed at the top left corner.&lt;br/&gt;
        Since it is possible in CSS to regroup several id definitions, the complete code for the six different modules looks like this:
        &lt;pre class=&quot;code&quot;&gt;
#categories,#search {
background:#e67701 url(../images/orange_gradient_bg.png) repeat-x 0 0
}

#resources,#meta {
background:#2b6475 url(../images/blue_gradient_bg.png) repeat-x 0 0
}

#archives,#foo {
background:#6d8a0c url(../images/green_gradient_bg.png) repeat-x 0 0
}
        &lt;/pre&gt;
        The next step is to place the module title together with the image. I recommend you that, whenever an image is used that gives no additional meaning to the content and is only for decorative purpose, to set is as a background image. Simply set the image as background with CSS, position it to the left (in this case), don't repeat it and then add a padding-left to your container the width of your image:
        &lt;pre class=&quot;code&quot;&gt;
.sidebar_title {
background:url(../images/sidebar_title_logo.gif) no-repeat 0 0;
height:30px;
font-size:13px;
color:#493800;
font-weight:700;
letter-spacing:normal;
margin:20px 0 0 35px;
padding:4px 0 0 40px
}
        &lt;/pre&gt;
        Finally, all that is left to do is to design the list inside the module. First, we remove the usual list style (since we plan on using our own) and do the same trick as with the module title. The little white arrow is set as li background, positioned to the left and the text is padded to the right accordingly. The line underneath the list text is once again only a CSS border-bottom, with the desired style:
        &lt;pre class=&quot;code&quot;&gt;
li {
list-style:none;
background:url(../images/list_item.gif) no-repeat 0 5px;
border-bottom:1px dotted #b2c56f;
color:#fff;
font-size:11px;
width:100px;
padding:0 0 5px 7px
}            
        &lt;/pre&gt;
        The search module has to be treated separately, since it has its own icon and the form needs to line up nicely. I added search_title to the class list of the module title container and simply redefined the background image. Finally, I modified the padding inside the search form, gave the text input a nicer look and positioned it relative to the image:
        &lt;pre class=&quot;code&quot;&gt;
&amp;lt;div id=&quot;search&quot; class=&quot;sidebar_item&quot;&amp;gt;
    &amp;lt;div class=&quot;sidebar_title search_title&quot;&amp;gt;
        Search
    &amp;lt;/div&amp;gt;
    &amp;lt;form method=&quot;get&quot; id=&quot;searchform&quot; action=&quot;search.php&quot; name=&quot;searchform&quot;&amp;gt;
        &amp;lt;input type=&quot;text&quot; name=&quot;searchinput&quot; id=&quot;searchinput&quot; value=&quot;&quot;&amp;gt;
        &amp;lt;input type=&quot;image&quot; src=&quot;./images/go.gif&quot; value=&quot;submit&quot;&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;

.search_title {
background:url(../images/search_bg.gif) no-repeat 0 0
}

#searchform {
padding:20px 0 0 10px
}

#searchinput {
font-size:12px;
color:#483221;
font-family:Tahoma, arial, verdana, courier;
width:109px;
height:21px;
border:#483221 solid 1px;
vertical-align:top;
margin:0 5px
}
        &lt;/pre&gt;
        &lt;a name=&quot;center&quot;&gt;&lt;/a&gt;&lt;h3&gt;Designing the center part&lt;/h3&gt;
        Apart from the current website, I didn't have much information about the design concept of the center section, which is why I kept this part pretty simple. First, I added a class which I called center_item, which basically is a container for one item in the center part, be it news or whatever is shown there. To make the center part align nicely with the two sidebars, I added a margin at the top of the same size than the darker region in the background image of the side modules. Finally, I padded the content, modified the font face, size and color and set the alignment to justify:
        &lt;pre class=&quot;code&quot;&gt;
.center_item {
border-top:8px solid #2f2f2f;
text-align:justify;
color:#fff;
font-family:tahoma, arial narrow, arial;
font-size:12px;
padding:10px 40px
}
        &lt;/pre&gt;
        &lt;a name=&quot;conclusion&quot;&gt;&lt;/a&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;
        I hope I was able to show you how convenient it is to strictly separate the content from the actual design. By carefully structuring and naming the content according to its function within the page, the true power from CSS can be applied. This makes it possible in the future to change the design without changing the HTML code.&lt;br/&gt;
        Apart from the fluid three-column CSS design, there were no major problems that had to be overcome, since the design was pretty simple. If you have any questions, feel free to drop me a note using the &lt;a href=&quot;contact.php&quot;&gt;Contact Form&lt;/a&gt;.</description><link>http://www.progressive-coding.com/tutorial.php?id=5</link><guid>http://www.progressive-coding.com/tutorial.php?id=5</guid></item><item><title>Tutorial: Collaborative editing</title><description>HTML version not available, please use the PDF version</description><link>http://www.progressive-coding.com/tutorial.php?id=4</link><guid>http://www.progressive-coding.com/tutorial.php?id=4</guid></item><item><title>Tutorial: Block Cipher Modes of operation</title><description>&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;Introduction to modes of operation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#init_vector&quot;&gt;The Initialization Vector (IV)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#padding&quot;&gt;Message padding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ecb&quot;&gt;Electronic codebook (ECB)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#cbc&quot;&gt;Cipher-block Chaining(CBC)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#cfb&quot;&gt;Cipher feedback (CFB)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ofb&quot;&gt;Output feedback (OFB)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#end&quot;&gt;Putting it all together&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;a name=&quot;introduction&quot;&gt;&lt;/a&gt;&lt;h3&gt;Introduction to modes of operation&lt;/h3&gt;
If you followed this tutorial up to this point, you should know by now that block ciphers operate on blocks of fixed size, in this case 128 bits. Although it might happen that we need to encode a message of exactly 128 bits, we want our plaintext to be of any length. As you can see, encrypting the same plaintext under the same key always produces the same output, we need a mode of operation to provide confidentiality for messages of arbitrary length.&lt;br/&gt;
In this final chapter, we will learn about three modes of operation, namely CBC, OFB and CFB.
&lt;a name=&quot;init_vector&quot;&gt;&lt;/a&gt;&lt;h3&gt;The Initialization Vector (IV)&lt;/h3&gt;
Every block cipher mode of operation requires an initialization vector (commonly called IV), which serves as basis block to start the process and at the same time introduces randomization in the process. There is no need for the IV to be secret (in most cases), but as you will seen soon enough, it might cause a leak of information if it is reused with the same key. Just remember that the IV is a initial block of the same size than our block cipher block (128 bits for AES).
&lt;a name=&quot;padding&quot;&gt;&lt;/a&gt;&lt;h3&gt;Message padding&lt;/h3&gt;
Because a block cipher works on blocks of a fixed size, but messages come in a variety of lengths, some modes (mainly CBC) require that the final block be padded before encryption. As you can read in &lt;a href=&quot;http://en.wikipedia.org/wiki/Padding_%28cryptography%29&quot; target=&quot;_blank&quot;&gt;this article&lt;/a&gt;, many padding schemes exist. The simplest (which we will implement) is to add null bytes to the plaintext to bring its length to a multiple of the block size. Slightly more complex is the original DES method, which is to add a single one bit, followed by enough zero bits to fill out the block. If the message ends on a block boundary, a whole padding block will be added.
&lt;a name=&quot;ecb&quot;&gt;&lt;/a&gt;&lt;h3&gt;Electronic codebook (ECB)&lt;/h3&gt;
The simplest (and also the least secure) encryption mode is the electronic codebook (also called ECB). The input message is simply divided into blocks of the required size and each block is encrypted separately. The disadvantage is evident: identical plaintext blocks are encrypted into identitcal ciphertext blocks (since the secret key stays the same during the entire process). I strongly recommend to not use this mode at all, I'm only showing this for information purpose only.&lt;br/&gt;
&lt;img src=&quot;images/tutorials/AES/ecb_enc.png&quot; alt=&quot;ECB encryption&quot;&gt;&lt;br/&gt;&lt;img src=&quot;images/tutorials/AES/ecb_dec.png&quot; alt=&quot;ECB decryption&quot;&gt;&lt;br/&gt;
As you can see on these images (taken from this &lt;a href=&quot;http://en.wikipedia.org/wiki/Modes_of_operation&quot; target=&quot;_blank&quot;&gt;Wikipedia article&lt;/a&gt;), you can see that ECB is fairly simple to implement and even more simple to misuse for malicious purpose. An even more stunning example is the encryption of this image under ECB. Since identical input pattern result in the same output pattern, it is possible to recognize the source image, even under its encrypted version:&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img src=&quot;images/tutorials/AES/ecb_tux.jpg&quot; alt=&quot;TUX ECB&quot;&gt;&lt;img src=&quot;images/tutorials/AES/ecb_tux_enc.jpg&quot; alt=&quot;TUX ECB encrypted&quot;&gt;
&lt;/div&gt;
&lt;a name=&quot;cbc&quot;&gt;&lt;/a&gt;&lt;h3&gt;Cipher-block chaining (CBC)&lt;/h3&gt;
In CBC mode, the input message is once again divided into blocks and each block is XORed to the previous ciphertext block (the output of the previous encrytion) before being encrypted. As you can see, we require a ciphertext block to XOR the first message block to, which will be our IV. In CBC mode, each ciphertext block is dependent on all plaintext blocks processed up to that point.&lt;br/&gt;
&lt;img src=&quot;images/tutorials/AES/cbc_enc.png&quot; alt=&quot;CBC encryption&quot;&gt;&lt;br/&gt;&lt;img src=&quot;images/tutorials/AES/cbc_dec.png&quot; alt=&quot;CBC decryption&quot;&gt;&lt;br/&gt;
CBC is the most commonly used mode of operation but its main drawback is that the message must be padded to a multiple of the cipher block size, and therefor is being replaced by synchronous stream-ciphers such as CFB or OFB.&lt;br/&gt;
Before we start implementing the CBC mode, let us first define a structure that we'll use to select the appropriate mode as well as the prototype of our encryption function:
&lt;pre class=&quot;code&quot;&gt;
enum modeOfOperation{
    OFB,
    CFB,
    CBC
    };

void encrypt(FILE *in, FILE *out, enum modeOfOperation);
&lt;/pre&gt;
If at a later stage, you have several block ciphers implemented, you could change this prototype to have a pointer to a block cipher, which would then be used internally by the mode of operation.&lt;br/&gt;
I decided to use several blocks of 128 bits, that each serve its very own purpose:
&lt;ul&gt;
&lt;li&gt;plaintext: this corresponds to the 128 bit plaintext block that we encrypt during each iteration&lt;/li&gt;
&lt;li&gt;input: input corresponds to what is being used as input for the block cipher (the AES input)&lt;/li&gt;
&lt;li&gt;output: this corresponds to the output of our block cipher&lt;/li&gt;
&lt;li&gt;ciphertext: this corresponds to the output of our mode of operation for this iteration&lt;/li&gt;
&lt;/ul&gt;
As you will see, we could manage with a less blocks but since each mode of operation has its own special structure, we can keep our code clean and structured by introducing a little overhead. I'm also using an additional variable that defines if we are currently in the first round of the mode (where we use the IV) or at a later stage.&lt;br/&gt;
In order to recover the exact plaintext, even using our padding scheme, I decided to first write the mode of operation in use as well as the exact input file size into the output file:
&lt;pre class=&quot;code&quot;&gt;
fseek(in,0,SEEK_END);
fileSize = ftell(in);
fseek(in,0,SEEK_SET);

/* add the file header */
fwrite(&amp;amp;mode, sizeof(mode), 1, out);
fwrite(&amp;amp;fileSize, sizeof(fileSize), 1, out);
&lt;/pre&gt;
Since we are working with binary files, I use &lt;a href=&quot;http://www.cplusplus.com/reference/clibrary/cstdio/fread.html&quot; target=&quot;_blank&quot;&gt;fread()&lt;/a&gt; and &lt;a href=&quot;http://www.cplusplus.com/reference/clibrary/cstdio/fwrite.html&quot; target=&quot;_blank&quot;&gt;fwrite()&lt;/a&gt; to process the two files. Our algorithm reads blocks of 16 bytes (saving the return value of fread() in case we read less than 16 bytes and we need to padd), we perform the padding if we need to, XOR the plaintext to either the IV or the output (in CBC, the output is equal to the ciphertext), apply our AES encryption and write the result to the file.
&lt;pre class=&quot;code&quot;&gt;
/* the non-expanded keySize */
enum keySize size = SIZE_32;

/* the AES input/output */
unsigned char plaintext[16] = {0};
unsigned char input[16] = {0};
unsigned char output[16] = {0};
unsigned char ciphertext[16] = {0};
unsigned char IV[16] = {0};

/* the AES key */
unsigned char key[32] = {0x0};

/* char firstRound */
char firstRound = 1;

size_t read;
int i;

while ((read = fread(plaintext, sizeof(unsigned char), 16, in)) &gt; 0)
{
    /* padd with 0 bytes */
    if (read &lt; 16)
    {
        for (i = read; i &lt; 16; i++)
            plaintext[i] = 0;
    }

    for (i = 0; i &lt; 16; i++)
    {
        input[i] = plaintext[i] ^ ((firstRound) ? IV[i] : ciphertext[i]);
    }
    firstRound = 0;
    aes_encrypt(input, ciphertext, key, size);
    /* always 16 bytes output because of the padding for CBC */
    fwrite(ciphertext, sizeof(unsigned char), 16, out);
}
&lt;/pre&gt;
As you can see, we don't need the output block this time, as the ciphertext is identical to the output. The CBC decryption is similar, except for the fact that we pay attention to the originalFileSize we recovered from the input file and in case we have less than 16 bytes left to decrypt, we write the remaining bytes instead of the 16 bytes we read from the file:
&lt;pre class=&quot;code&quot;&gt;
while ((read = fread(ciphertext, sizeof(unsigned char), 16, in)) &gt; 0)
{
    aes_decrypt(ciphertext, output, key, size);
    for (i = 0; i &lt; 16; i++)
    {
        plaintext[i] = ((firstRound) ? IV[i] : input[i]) ^ output[i];
    }
    firstRound = 0;
    if (originalFileSize &lt; 16)
    {
        fwrite(plaintext, sizeof(unsigned char), originalFileSize, out);
    }
    else
    {
        fwrite(plaintext, sizeof(unsigned char), read, out);
        originalFileSize -= 16;
    }
    memcpy(input, ciphertext, 16*sizeof(unsigned char));
}
&lt;/pre&gt;
You probably noticed that I copied the content of the ciphertext into the input block. This is because ciphertext will be overwritten during the next iteration by the new ciphertext block, but we still need the ciphertext from the previous iteration to perform the XOR operation. In this case, I use input not as the input for the AES block cipher but for the XOR operation. Don't worry if you can't implement this code into a working example, I'll post the complete code at the end of this chapter.
&lt;a name=&quot;cfb&quot;&gt;&lt;/a&gt;&lt;h3&gt;Cipher feedback (CFB)&lt;/h3&gt;
The cipher feedback (CFB) mode, a close relative of CBC, makes a block cipher into a self-synchronizing stream cipher. Operation is very similar; in particular, CFB decryption is almost identical to CBC decryption performed in reverse.&lt;br/&gt;
&lt;img src=&quot;images/tutorials/AES/cfb_enc.png&quot; alt=&quot;CFB encryption&quot;&gt;&lt;br/&gt;&lt;img src=&quot;images/tutorials/AES/cfb_dec.png&quot; alt=&quot;CFB decryption&quot;&gt;&lt;br/&gt;
CFB shares two advantages over CBC mode with the stream cipher modes OFB and CTR: the block cipher is only ever used in the encrypting direction, and the message does not need to be padded to a multiple of the cipher block size. I personally think this mode of fairly easy to implement, so without any hesitation I show you the final code for encryption:
&lt;pre class=&quot;code&quot;&gt;
while ((read = fread(plaintext, sizeof(unsigned char), 16, in)) &gt; 0)
{
    if (firstRound)
    {
        aes_encrypt(IV, output, key, size);
        firstRound = 0;
    }
    else
    {
        aes_encrypt(input, output, key, size);
    }
    for (i = 0; i &lt; 16; i++)
    {
        ciphertext[i] = plaintext[i] ^ output[i];
    }
    fwrite(ciphertext, sizeof(unsigned char), read, out);
    memcpy(input, ciphertext, 16*sizeof(unsigned char));
}
&lt;/pre&gt;
In this case, you'll probably notice that I could have used the ciphertext immediately as input for the AES encryption, something that I found illogical for naming convention, so I simply copied the content from the ciphertext into the input. In case you're looking for optimal performance, feel free to change this.&lt;br/&gt;
The decryption is very similar to the encryption (it even uses the AES &lt;b&gt;encryption&lt;/b&gt;) and here's the code:
&lt;pre class=&quot;code&quot;&gt;
while ((read = fread(ciphertext, sizeof(unsigned char), 16, in)) &gt; 0)
{
    if (firstRound)
    {
        aes_encrypt(IV, output, key, size);
        firstRound = 0;
    }
    else
    {
        aes_encrypt(input, output, key, size);
    }
    for (i = 0; i &lt; 16; i++)
    {
        plaintext[i] = output[i] ^ ciphertext[i];
    }
    fwrite(plaintext, sizeof(unsigned char), read, out);
    memcpy(input, ciphertext, 16*sizeof(unsigned char));
}
&lt;/pre&gt;
&lt;a name=&quot;ofb&quot;&gt;&lt;/a&gt;&lt;h3&gt;Output feedback (OFB)&lt;/h3&gt;
If you read and understood the CFB mode, you shouldn't have any problems implementing the OFB mode. The only difference is that it uses the AES output before the XOR operation (and not like CFB after the XOR operation) as input for the next iteration.&lt;br/&gt;
&lt;img src=&quot;images/tutorials/AES/ofb_enc.png&quot; alt=&quot;OFB encryption&quot;&gt;&lt;br/&gt;&lt;img src=&quot;images/tutorials/AES/ofb_dec.png&quot; alt=&quot;OFB decryption&quot;&gt;&lt;br/&gt;
Here's the code for the encryption:
&lt;pre class=&quot;code&quot;&gt;
while ((read = fread(plaintext, sizeof(unsigned char), 16, in)) &gt; 0)
{
    if (firstRound)
    {
        aes_encrypt(IV, output, key, size);
        firstRound = 0;
    }
    else
    {
        aes_encrypt(input, output, key, size);
    }
    for (i = 0; i &lt; 16; i++)
    {
        ciphertext[i] = plaintext[i] ^ output[i];
    }
    fwrite(ciphertext, sizeof(unsigned char), read, out);
    memcpy(input, output, 16*sizeof(unsigned char));
}
&lt;/pre&gt;
As you can see, the only difference to CFB mode is that I copy the content of output into input and not the content of ciphertext. Here's the code for the decryption:
&lt;pre class=&quot;code&quot;&gt;
while ((read = fread(ciphertext, sizeof(unsigned char), 16, in)) &gt; 0)
{
   if (firstRound)
    {
        aes_encrypt(IV, output, key, size);
        firstRound = 0;
    }
    else
    {
        aes_encrypt(input, output, key, size);
    }
    for (i = 0; i &lt; 16; i++)
    {
        plaintext[i] = output[i] ^ ciphertext[i];
    }
    fwrite(plaintext, sizeof(unsigned char), read, out);
    memcpy(input, output, 16*sizeof(unsigned char));
}
&lt;/pre&gt;
&lt;a name=&quot;end&quot;&gt;&lt;/a&gt;&lt;h3&gt;Putting it all together&lt;/h3&gt;
Now that we have explained and implementing our three modes of operation, all that is left is to put them all together into one piece of code.&lt;br/&gt;
Encryption:
&lt;pre class=&quot;code&quot;&gt;
void encrypt(FILE *in, FILE *out, enum modeOfOperation mode)
{
    /* the non-expanded keySize */
    enum keySize size = SIZE_32;

    /* the AES input/output */
    unsigned char plaintext[16] = {0};
    unsigned char input[16] = {0};
    unsigned char output[16] = {0};
    unsigned char ciphertext[16] = {0};
    unsigned char IV[16] = {0};

    /* the AES key */
    unsigned char key[32] = {0x0};

    /* char firstRound */
    char firstRound = 1;

    long int fileSize;
    size_t read;
    int i;

    if (in != NULL)
    {
        fseek(in,0,SEEK_END);
        fileSize = ftell(in);
        fseek(in,0,SEEK_SET);

        /* add the file header */
        fwrite(&amp;amp;mode, sizeof(mode), 1, out);
        fwrite(&amp;amp;fileSize, sizeof(fileSize), 1, out);

        while ((read = fread(plaintext, sizeof(unsigned char), 16, in)) &gt; 0)
        {

            if (mode == CFB)
            {
                if (firstRound)
                {
                    aes_encrypt(IV, output, key, size);
                    firstRound = 0;
                }
                else
                {
                    aes_encrypt(input, output, key, size);
                }
                for (i = 0; i &lt; 16; i++)
                {
                    ciphertext[i] = plaintext[i] ^ output[i];
                }
                fwrite(ciphertext, sizeof(unsigned char), read, out);
                memcpy(input, ciphertext, 16*sizeof(unsigned char));
            }
            else if (mode == OFB)
            {
                if (firstRound)
                {
                    aes_encrypt(IV, output, key, size);
                    firstRound = 0;
                }
                else
                {
                    aes_encrypt(input, output, key, size);
                }
                for (i = 0; i &lt; 16; i++)
                {
                    ciphertext[i] = plaintext[i] ^ output[i];
                }
                fwrite(ciphertext, sizeof(unsigned char), read, out);
                memcpy(input, output, 16*sizeof(unsigned char));
            }
            else if (mode == CBC)
            {
                /* padd with 0 bytes */
                if (read &lt; 16)
                {
                    for (i = read; i &lt; 16; i++)
                        plaintext[i] = 0;
                }

                for (i = 0; i &lt; 16; i++)
                {
                    input[i] = plaintext[i] ^ ((firstRound) ? IV[i] : ciphertext[i]);
                }
                firstRound = 0;
                aes_encrypt(input, ciphertext, key, size);
                /* always 16 bytes because of the padding for CBC */
                fwrite(ciphertext, sizeof(unsigned char), 16, out);
            }

        }
    }
}
&lt;/pre&gt;
&lt;br/&gt;
Decryption:
&lt;pre class=&quot;code&quot;&gt;
void decrypt(FILE *in, FILE *out)
{
    /* the non-expanded keySize */
    enum keySize size = SIZE_32;

    /* the AES input/output */
    unsigned char ciphertext[16] = {0};
    unsigned char input[16] = {0};
    unsigned char output[16] = {0};
    unsigned char plaintext[16] = {0};
    unsigned char IV[16] = {0};

    /* the AES key */
    unsigned char key[32] = {0x0};

    /* char firstRound */
    char firstRound = 1;

    enum modeOfOperation mode;

    long int originalFileSize = 0;
    size_t read;
    int i;

    if (in != NULL)
    {
        fread(&amp;amp;mode, sizeof(enum modeOfOperation), 1, in);
        fread(&amp;amp;originalFileSize, sizeof(originalFileSize), 1, in);

        while ((read = fread(ciphertext, sizeof(unsigned char), 16, in)) &gt; 0)
        {
            if (mode == CFB)
            {
                if (firstRound)
                {
                    aes_encrypt(IV, output, key, size);
                    firstRound = 0;
                }
                else
                {
                    aes_encrypt(input, output, key, size);
                }
                for (i = 0; i &lt; 16; i++)
                {
                    plaintext[i] = output[i] ^ ciphertext[i];
                }
                fwrite(plaintext, sizeof(unsigned char), read, out);
                memcpy(input, ciphertext, 16*sizeof(unsigned char));
            }
            else if (mode == OFB)
            {
                if (firstRound)
                {
                    aes_encrypt(IV, output, key, size);
                    firstRound = 0;
                }
                else
                {
                    aes_encrypt(input, output, key, size);
                }
                for (i = 0; i &lt; 16; i++)
                {
                    plaintext[i] = output[i] ^ ciphertext[i];
                }
                fwrite(plaintext, sizeof(unsigned char), read, out);
                memcpy(input, output, 16*sizeof(unsigned char));
            }
            else if(mode == CBC)
            {
                aes_decrypt(ciphertext, output, key, size);
                for (i = 0; i &lt; 16; i++)
                {
                    plaintext[i] = ((firstRound) ? IV[i] : input[i]) ^ output[i];
                }
                firstRound = 0;
                if (originalFileSize &lt; 16)
                {
                    fwrite(plaintext, sizeof(unsigned char), originalFileSize, out);
                }
                else
                {
                    fwrite(plaintext, sizeof(unsigned char), read, out);
                    originalFileSize -= 16;
                }
                memcpy(input, ciphertext, 16*sizeof(unsigned char));
            }

        }
    }
}
&lt;/pre&gt;</description><link>http://www.progressive-coding.com/tutorial.php?id=3</link><guid>http://www.progressive-coding.com/tutorial.php?id=3</guid></item><item><title>Tutorial: Data Types and Structures</title><description>HTML version soon, please use PDF</description><link>http://www.progressive-coding.com/tutorial.php?id=2</link><guid>http://www.progressive-coding.com/tutorial.php?id=2</guid></item></channel></rss>