/*
 * Copyright © 2007 Manfred Stienstra <manfred@fngtps.com>, Fingertips
 *
 * The $ function is borrowed from the Prototype JavaScript framework.
 * Copyright © 2005 Sam Stephenson <sam@conio.net>
 */


// Element finding helper
function $() {
  var results = [], element;
  for (var i = 0; i < arguments.length; i++) {
    element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push(element);
  }
  return results.length < 2 ? results[0] : results;
}

// Class for managing the language options in a form
var SignupLocation = {
  languages: [["Arabic", "ar"], ["German", "de"], ["English", "en"], ["Spanish", "es"], ["French", "fr"], ["Greek", "gre"], ["Italian", "it"], ["Dutch", "nl"], ["Polish", "pl"], ["Portuguese", "pt"], ["Russian", "ru"]],
  signupLanguageRestrictions: {mx: [["Spanish", "es"], ["English", "en"]], lb: [["Arabic", "ar"], ["English", "en"]], it: [["Italian", "it"], ["English", "en"]], gr: [["Greek", "gre"], ["English", "en"]], dz: [["Arabic", "ar"], ["English", "en"]], ye: [["Arabic", "ar"], ["English", "en"]], kw: [["Arabic", "ar"], ["English", "en"]], de: [["German", "de"], ["English", "en"]], fr: [["French", "fr"], ["English", "en"]], mo: [["Arabic", "ar"], ["English", "en"]], ly: [["Arabic", "ar"], ["English", "en"]], eg: [["Arabic", "ar"], ["English", "en"]], km: [["Arabic", "ar"], ["English", "en"]], es: [["Spanish", "es"], ["English", "en"]], sa: [["Arabic", "ar"], ["English", "en"]], ru: [["Russian", "ru"], ["English", "en"]], ps: [["Arabic", "ar"], ["English", "en"]], be: [["Dutch", "nl"], ["French", "fr"], ["English", "en"]], ae: [["Arabic", "ar"], ["English", "en"]], tn: [["Arabic", "ar"], ["English", "en"]], ch: [["German", "de"], ["French", "fr"], ["English", "en"]], mr: [["Arabic", "ar"], ["English", "en"]], jo: [["Arabic", "ar"], ["English", "en"]], br: [["Portuguese", "pt"], ["English", "en"]], sy: [["Arabic", "ar"], ["English", "en"]], sd: [["Arabic", "ar"], ["English", "en"]], pl: [["Polish", "pl"], ["English", "en"]], dj: [["Arabic", "ar"], ["English", "en"]], bh: [["Arabic", "ar"], ["English", "en"]], ar: [["Spanish", "es"], ["English", "en"]], om: [["Arabic", "ar"], ["English", "en"]], nl: [["Dutch", "nl"], ["English", "en"]], iq: [["Arabic", "ar"], ["English", "en"]], at: [["German", "de"], ["English", "en"]], lu: [["French", "fr"], ["English", "en"]], cl: [["Spanish", "es"], ["English", "en"]]},
  
  updateLanguageOptionsFor: function(countryElement, languageElement) {
    if ($(countryElement).value) {
      var newLanguages = this.signupLanguageRestrictions[$(countryElement).value];
      if (newLanguages) {
        this.resetOptionsTo(languageElement, newLanguages);
      } else {
        this.resetOptionsTo(languageElement, [['English', 'en']]);
      }
    } else {
      this.resetOptionsTo(languageElement, this.languages);
    }
  },
  
  resetOptionsTo: function(languageElement, newLanguages) {
    var selected = $(languageElement).value;
    $(languageElement).options.length = 0;
    $(languageElement).options[0] = new Option('', '');
    for (var i = 0; i < newLanguages.length; i++) {
      var option = new Option(newLanguages[i][0], newLanguages[i][1]);
      if (selected == newLanguages[i][1]) {
        option.selected = true;
      }
      $(languageElement).options[i+1] = option;
    }
  }
};

/*
 * Copyright © 2007 Manfred Stienstra <manfred@fngtps.com>, Fingertips
 *
 */
var Embed = {
  embedCSS: function(url) {
    var head = document.getElementsByTagName('head')[0];
    var style = document.createElement('link');
    style.type = 'text/css';
    style.rel = 'stylesheet';
    style.href = url;
    head.appendChild(style);
  },
  
  embedHTML: function(html) {
    var embed = document.getElementById('rhubarb');
    if (embed != null) {
      embed.innerHTML = html;
    } else {
      document.write("<div id=\"rhubarb\">");
      document.write(html);
      document.write("</div>");
    }
  }
}

Embed.embedCSS("http://write-a-letter.greenpeace.org/stylesheets/public.css");
Embed.embedCSS("http://write-a-letter.greenpeace.org/569/inline_stylesheet.css");
Embed.embedHTML("<h1>Demand AbitibiBowater protect intact areas of the Boreal Forest</h1>\n\n<p>\nLogging company AbitibiBowater controls the largest tracts of publically-owned forest in the provinces of Quebec and Ontario in Canada, a total of 24 million hectares (59 million acres). Less than 35% of this area remains intact &#8211; most of it has been degraded by logging roads and massive clearcuts. Much of these forests have been turned into newsprint, book and catalogue papers, inserts and junk mail. \n</p>\n<p>\nAbitibiBowater does very little to protect important wildlife habitat for threatened species such as woodland caribou and migratory birds. In fact, less than 3 percent of the land under AbitibiBowater&#8217;s management is protected from logging in Qu&#233;bec, and less than 6 percent in Ontario. \n</p>\n<p>\nWe are urging the company&#8217;s president and CEO to stop logging the remaining intact areas of the Boreal Forest under AbitibiBowater management. \n</p>\n<h2>\nSend a loud and clear message to the CEO of AbitibiBowater. Let him know that the conservation of intact Boreal Forest is needed now!\n</h2>\n\n\n<script type=\"text/javascript\">try{urchinTracker('/outbound/#{CGI::escape(@appeal.title)}');}catch(e){};</script>\n\n<form action=\"http://write-a-letter.greenpeace.org/569/messages\" method=\"post\" onsubmit=\"try{urchinTracker('/outbound/Demand+AbitibiBowater+protect+intact+areas+of+the+Boreal+Forest');}catch(e){};return true;\">\n    <fieldset>\n    <div>\n      <div><label for=\"message_name\">Your name</label></div>\n      <input class=\"text small\" id=\"message_name\" maxlength=\"100\" name=\"message[name]\" size=\"40\" type=\"text\" />\n    </div>\n    \n    <div>\n      <div><label for=\"message_email\">Your email address</label></div>\n      <input class=\"text small\" id=\"message_email\" maxlength=\"100\" name=\"message[email]\" size=\"40\" type=\"text\" />\n    </div>\n    \n    <div>\n      <div><label for=\"message_subject\">Subject</label></div>\n      \n        <input class=\"text\" id=\"message_subject\" maxlength=\"200\" name=\"message[subject]\" size=\"60\" type=\"text\" value=\"Protect our intact forests\" />\n      \n    </div>\n    \n    <div>\n      <div><label for=\"message_body\">Message</label></div>\n      \n        <textarea cols=\"60\" id=\"message_body\" name=\"message[body]\" rows=\"15\">Dear Mr. Paterson, \n\nAs the head of AbitibiBowater, a company that controls the largest area of public forest in Qu&#233;bec and Ontario, you must reconsider your position on the conservation of intact forests.\n\nIt is unacceptable that the majority of your company&#8217;s logging tenures in Qu&#233;bec and Ontario are fragmented and degraded.\n\nIt is even more unacceptable that less than 3 percent of all the territory managed by your company is protected in Qu&#233;bec, and less than 6 percent in Ontario.\n\nMr. Paterson, intact forests are vital ecosystems for maintaining biodiversity, protecting threatened species and stabilizing the world&#8217;s climate. \n\nIf you truly intend to embrace the cause of sustainability, why do you refuse to stop forestry operations in important areas of intact forest?\n\nProtecting the environment can go hand in hand with business sustainability. If you did make significant moves to protect the last remaining intact areas of forest under your management, this would help make your business and sales of magazine, catalogue and book papers, as well as newsprint and construction lumber more acceptable.\n\nTruly embracing sustainability could be a benefit to your business.\n\n\nRespectfully yours,\n</textarea>\n      \n    </div>\n    \n      </fieldset>\n  \n  <div class=\"submit\">\n    <input name=\"commit\" type=\"submit\" value=\"Next...\" />\n  </div>\n</form>\n");
