/*
 * Copyright (c) 2009 Simo Niemelä
 */
 
/*
 * Builds a form from xml
 *
 * Example:
 *  // To append the result in to an element
 *  jQuery_1_4_2('#form_wrapper').buildForm('http://www.domain.com/my/action.xml');
 */
 (function() {
   jQuery_1_4_2.fn.buildForm = function(url, options) {
     settings = jQuery_1_4_2.extend({
       handler: "/sites/anandaseva.fi/crmple-remote-client/lib/crmple_remote_client.php"
     }, options);
     return this.each(function(){
       var $form = jQuery_1_4_2(this);
       var requestHandler = settings['handler'] + "?url=" + url;
       jQuery_1_4_2.get(requestHandler, function(data){
         $form.html(jQuery_1_4_2(data).text());
         $form.children('form').each(function(i, el){ 
           jQuery_1_4_2(el).replaceWith(jQuery_1_4_2(el).html());
         });
         $form.attr('onSubmit', 'return false').attr('action', '').attr('method', 'post');
         // Remove hidden fields without an id
         $form.find("input[type=hidden]:not('[id]')").each(function(i, el){ 
           jQuery_1_4_2(el).remove();
         });
       });
     });
   }
 })();
