
/**
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//\\\       \\\\\\\\|
//\\\ @@    @@\\\\\\| Hot Toddy Contact Form
//\\ @@@@  @@@@\\\\\|
//\\\@@@@| @@@@\\\\\|
//\\\ @@ |\\@@\\\\\\| http://www.hframework.com
//\\\\  ||   \\\\\\\| (c) Copyright 2009 Richard York, All rights Reserved
//\\\\  \\_   \\\\\\|
//\\\\\        \\\\\| Use and redistribution are subject to the terms of the license.
//\\\\\  ----  \@@@@| http://www.hframework.com/license
//@@@@@\       \@@@@|
//@@@@@@\     \@@@@@|
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/

$$.extend({

  SwapStates : function()
  {
    if (parseInt($(this).val()) > 0) {
      $$.ContactForm.States.length = 0;

      $.get(
        '/plugins/hLocation/getStates?hLocationCountryID=' + $(this).val(),
        function(xml) {
          $$.ContactForm.States.disabled = false;

          var $iso2  = $(xml).find('hLocationCountryISO2').text();
          var $path  = '/images/icons/32x32/flags/' + $iso2.toLowerCase() + '.png';
          var $alt   = $iso2 + ' Flag';

          if (!hFramework.IE6) {
             $('span#hContactFormFlag img').attr({
               src: $path,
               alt: $alt
             });
          } else {
            $('span#hContactFormFlag span').replaceWith(
              "<img src='" + $path + "' alt='" + $alt + "' onload='$(this).PNG();' />"
            );
          }

          var $label = $(xml).find('hLocationStateLabel').text();

          $('td#hContactFormStateLabel span').text($label + ':');

          var $states = $(xml).find('hLocationState');

          if ($states.length  > 1) {
            $states.each(
              function() {
                var $option = document.createElement('option');

                $option.value = $(this).attr('hLocationStateID');
                
                if ($option.value == '0') {
                  var $optionLabel = 'Please select a ' + $label.toLowerCase();
                } else {
                  var $optionLabel = $(this).text().length? $(this).text() : ' ';
                }

                $option.appendChild(document.createTextNode($optionLabel));

                $$.ContactForm.States.appendChild($option);
              }
            );
          } else {
            $$.ContactForm.States.disabled = true;
          }
        }
      );
    }
  },

  ContactForm : {

    States : null,
    Country : null,

    Ready : function()
    {
      if ($('select#hLocationStateID').length) {
        this.States  = $('select#hLocationStateID').get(0); 
        this.Country = $('select#hLocationCountryID').get(0);

        if (!this.States.length) {
          this.States.disabled = true;
        }

        $('select#hLocationCountryID').change(
          function() {
            $(this).SwapStates();
          }
        );
      }

      if ($('input#hContactDateOfBirth').length && typeof($.datepicker) != 'undefined') {

        $('input#hContactDateOfBirth').datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: '-150:+1'
        });
      }
      
    }
  }
});

$(document).ready(
  function() {
    $$.ContactForm.Ready();
  }
);