YAHOO.namespace('ticode');
YAHOO.namespace('ticode.contact');

if (!document.proof)
{
  function proof(text)
  {
    var proof       = document.createElement("h1");
    proof.innerHTML = text;

    document.body.appendChild(proof);
  }
}

YAHOO.ticode.contact = function ()
{
  /*
  ** ***********************************************************
  ** private variables available only to methods of the module.
  */

  /*
  ** private shorthand variables to comon YUI parts/utilities
  */
  var Yue   = YAHOO.util.Event,
      Elem  = YAHOO.util.Element,
      Dom   = YAHOO.util.Dom;
  /*
  ** private shorthand variable for built YUI objects. set in
  ** init()
  */

  /*
  ** constants used in this module.
  */
  var basicClick    = 'open_basic',
      comingClick   = 'open_coming',
      timesClick    = 'open_times',
      lookingClick  = 'open_looking',
      //passportClick = 'open_passport',
      lodgingClick  = 'open_lodging',
      considerClick = 'open_consider';
  /*
  ** module state:
  */
  var contactVariables = false,
      lookupFields     = {};
      countFields      = {};
      hiddenFields     =
        { basic:
          { normal: 6,
            div: '-',
            show: '-',
            subs:
            { js_fname:   ['-','-'],
              js_lname:   ['-','-'],
              js_email:   ['-','-'],
              txt_cntry:  ['-','-'],
              txt_phone1: ['-','-']
            }
          },
          coming:
          { normal: 0/*,
            subs:
            { js_people: ['-','-']
            }*/
          },
         times:
         { normal: 1,
           subs:
           { js_arrival: ['-','-'],
             js_depart: ['-','-'],
             js_shotel: ['-','-']
           }
         },
         looking:  { normal: 2 },
         //passport: { normal: 5 },
         lodging:  { normal: 5 },
         consider: { normal: 5 }
       };

  /*
  ** these are Dom refrences that won't change, they are set by
  ** direct calls when needed, and then used freely after that.
  */
  var first   = '',
      last    = '',
      email   = '',
      country = '',
      phone   = '',
      people  = '',
      arrival = '',
      depart  = '',
      shotel  = '',
      action  = '';

  var basic    = '',
      coming   = '',
      times    = '',
      looking  = '',
      //passport = '',
      lodging  = '',
      consider = '';

  var basicCount    = '',
      comingCount   = '',
      timesCount    = '',
      lookingCount  = '',
      //passportCount = '',
      lodgingCount  = '',
      considerCount = '';

  var firstWrap    = '',
      lastWrap     = '',
      emailWrap    = '',
      countryWrap  = '',
      phoneWrap    = '',
      peopleWrap   = '',
      arrivalWrap  = '',
      departWrap   = '',
      shotelWrap   = '';
  /*
  ** var for helper methods.
  */

  /*
  ** ***********************************************************
  ** private methods available only to other methods of the
  ** module.
  */

  /*
  ** private Wrapper for ticode methods.
  */
  var trim = function (id)
  {
    YAHOO.ticode.trim(id);
  };

  /*
  ** private method setContactVariables to be run once.  Access
  ** is protected by private variable contactVariables, but for
  ** more speed, test variable directly with if -- although it
  ** will only save you one function call:
  **
  ** if (!contactVariables)
  ** {
  **   setContactVariables();
  ** }
  */
  var setContactVariables = function ()
  {
    if (!contactVariables)
    {
      if (!(first = Dom.get('js_fname')))
      {
        alert('First Name text field not found.');
      }
      if (!(last = Dom.get('js_lname')))
      {
        alert('Last Name text field not found.');
      }
      if (!(email = Dom.get('js_email')))
      {
        alert('Email text field not found.');
      }
      if (!(country = Dom.get('txt_cntry')))
      {
        alert('Country text field not found.');
      }
      if (!(phone = Dom.get('txt_phone1')))
      {
        alert('Phone Number text field not found.');
      }
      if (!(people = Dom.get('js_people')))
      {
        alert('Num People text field not found.');
      }
      if (!(arrival = Dom.get('js_arrival')))
      {
        alert('Arrival text field not found.');
      }
      if (!(depart = Dom.get('js_depart')))
      {
        alert('Depart text field not found.');
      }
      if (!(action = Dom.get('js_action')))
      {
        alert('Action hidden field not found.');
      }

      if (!(basic = new Elem('basic')))
      {
        alert('Basic div not found.');
      }
      if (!(coming = new Elem('coming')))
      {
        alert('Coming div not found.');
      }
      if (!(times = new Elem('times')))
      {
        alert('Times div not found.');
      }
      if (!(looking = new Elem('looking')))
      {
        alert('Looking div not found.');
      }
      /*if (!(passport = new Elem('passport')))
      {
        alert('Passport div not found.');
      }*/
      if (!(lodging = new Elem('lodging')))
      {
        alert('Lodging div not found.');
      }
      if (!(consider = new Elem('consider')))
      {
        alert('Consider div not found.');
      }

      if (!(basicCount = Dom.get('basic_count')))
      {
        alert('Basic Count user visible text bold element not'
          +' found.');
      }
      if (!(comingCount = Dom.get('coming_count')))
      {
        alert('Coming Count user visible text bold element not'
          +' found.');
      }
      if (!(timesCount = Dom.get('times_count')))
      {
        alert('Times Count user visible text bold element not'
          +' found.');
      }
      if (!(lookingCount = Dom.get('looking_count')))
      {
        alert('Looking Count user visible text bold element not'
          +' found.');
      }
/*      if (!(passportCount = Dom.get('passport_count')))
      {
        alert('Passport Count user visible text bold element not'
          +' found.');
      }*/
      if (!(lodgingCount = Dom.get('lodging_count')))
      {
        alert('Lodging Count user visible text bold element not'
          +' found.');
      }
      if (!(considerCount = Dom.get('consider_count')))
      {
        alert('Consider Count user visible text bold element not'
          +' found.');
      }
      if (!(firstWrap = new Elem('fname')))
      {
        alert('First Name wrapper div not found.');
      }
      if (!(lastWrap = new Elem('lname')))
      {
        alert('Last Name wrapper div not found.');
      }
      if (!(emailWrap = new Elem('email')))
      {
        alert('Email wrapper div not found.');
      }
      if (!(countryWrap = new Elem('cntry')))
      {
        alert('Wrap wrapper div not found.');
      }
      if (!(phoneWrap = new Elem('phone1')))
      {
        alert('Phone Number wrapper div not found.');
      }
      if (!(peopleWrap = new Elem('people')))
      {
        alert('Num People wrapper div not found.');
      }
      if (!(arrivalWrap = new Elem('arrival')))
      {
        alert('Arrival wrapper div not found.');
      }
      if (!(departWrap = new Elem('departure')))
      {
        alert('Depart wrapper div not found.');
      }
      contactVariables = true;
    }
  };

  /*
  ** private method toggleOpen. ClickHandler, 'this' is the
  ** parent of the clicked node. Toggles class 'open'.
  */
  var toggleOpen = function ()
  {
    if (!this.hasClass('open'))
    {
      this.addClass('open');
    }
    else
    {
      this.removeClass('open');
    }
    YAHOO.ticode.contact.countHiddenFields();
  };

  /*
  ** private method setupHiddenFields.  sets up lookupFields
  ** association, and countFields association.
  */
  var setupHiddenFields = function ()
  {
    hiddenFields['basic']['div']                   = basic;
    hiddenFields['basic']['show']                  = basicCount;
    hiddenFields['basic']['subs']['js_fname'][0]   = first;
    hiddenFields['basic']['subs']['js_fname'][1]   = firstWrap;
    hiddenFields['basic']['subs']['js_lname'][0]   = last;
    hiddenFields['basic']['subs']['js_lname'][1]   = lastWrap;
    hiddenFields['basic']['subs']['js_email'][0]   = email;
    hiddenFields['basic']['subs']['js_email'][1]   = emailWrap;
    hiddenFields['basic']['subs']['txt_cntry'][0]  = country;
    hiddenFields['basic']['subs']['txt_cntry'][1]  = countryWrap;
    hiddenFields['basic']['subs']['txt_phone1'][0] = phone;
    hiddenFields['basic']['subs']['txt_phone1'][1] = phoneWrap;
    hiddenFields['coming']['div']                  = coming;
    hiddenFields['coming']['show']                 = comingCount;
  //hiddenFields['coming']['subs']['js_people'][0] = people;
  //hiddenFields['coming']['subs']['js_people'][1] = peopleWrap;
    hiddenFields['times']['div']                   = times;
    hiddenFields['times']['show']                  = timesCount;
    hiddenFields['times']['subs']['js_arrival'][0] = arrival;
    hiddenFields['times']['subs']['js_arrival'][1] = arrivalWrap;
    hiddenFields['times']['subs']['js_depart'][0]  = depart;
    hiddenFields['times']['subs']['js_depart'][1]  = departWrap;
    hiddenFields['times']['subs']['js_shotel'][0]  = shotel;
    hiddenFields['times']['subs']['js_shotel'][1]  = shotelWrap;
    hiddenFields['looking']['div']                 = looking;
    hiddenFields['looking']['show']                =lookingCount;
  //hiddenFields['passport']['div']                = passport;
  //hiddenFields['passport']['show']             = passportCount;
    hiddenFields['lodging']['div']                 = lodging;
    hiddenFields['lodging']['show']                =lodgingCount;
    hiddenFields['consider']['div']                = consider;
    hiddenFields['consider']['show']             = considerCount;

    YAHOO.ticode.contact.countHiddenFields();
  };

  /*
  ** private method countHiddenFields.  Counts fields hidden by
  ** collapsed fieldsets, update for user.
  */
  var countHiddenFields = function ()
  {
    for (counter in hiddenFields)
    {
      var div   = hiddenFields[counter]['div'],
          count = 0,
          show  = hiddenFields[counter]['show'];
      show.innerHTML  = '';

      if (!div.hasClass('open'))
      {
        count = hiddenFields[counter]['normal'];

        for (field in hiddenFields[counter]['subs'])
        {
          var lookup  = hiddenFields[counter]['subs'][field][0],
              wrap    = hiddenFields[counter]['subs'][field][1];
          var value   = lookup.value;

          wrap.removeClass('show');

          if (value.length)
          {
            wrap.addClass('show');
            ++count;
          }
        }
        if (count)
        {
          show.innerHTML = '(' + count + ')';
        }
      }
    }
  };

  /*
  ** private method countHiddenFields.  Counts fields hidden by
  ** collapsed fieldsets, update for user.
  */
  var bail = function (fieldset, wrapper, text)
  {
    fieldset.addClass('open');
    wrapper.removeClass('show');
    wrapper.setStyle('backgroundColor', '#F90');
    window.scrollTo
      (0,Dom.getXY(wrapper.get('element').id)[0]+200);
    alert(text);
    wrapper.setStyle('backgroundColor', 'transparent');
  };

  var that =
  {
    /*
    ** publicly accessible variables where we store AJAX results
    ** by evaluating valid js pointing to these arrays.
    */
    xhiddenFields: {}, // YAHOO.ticode.contact.xhiddenFields{}

    /*
    ** public method countHiddenFiles() runs a private version.
    */
    countHiddenFields: function ()
    {
      countHiddenFields();
    },

    /*
    ** public method submit() verifies form required entries and
    ** sends data.
    */
    submit: function ()
    {
      if (first.value == '')
      {
        bail(basic, firstWrap, 'The First Name is required');
        proof('first');
      }
      else if (last.value == '')
      {
        bail(basic, lastWrap, 'The Last Name is required');
        proof('last');
      }
      else if (email.value == '')
      {
        bail(basic, emailWrap, 'The Email is required');
        proof('email1');
      }
      else if (email.value.indexOf('@') == -1)
      {
        bail(basic, emailWrap, 'Invalid email format');
        proof('email2');
      }
      else if (country.value == '')
      {
        bail(basic, countryWrap, 'The Country is required');
        proof('country');
      }
      else if (phone.value == '')
      {
        bail(basic, phoneWrap, 'The Phone is required');
        proof('phone');
      }
      else if (people.value == '')
      {
        bail(coming, peopleWrap, 'The # of people is required');
        proof('people');
      }
      else if (arrival.value == '')
      {
        bail(times,
          arrivalWrap, 'The date of arrival is required');
        proof('arrival');
      }
      else if (depart.value == '')
      {
        bail(times,
          departWrap, 'The date of departure is required');
        proof('depart');
      }
      //else if (0) -- for debuging
      else
      {
        action.value = 'Submit';
        return true;
      }
      return false;
    },

    /*
    ** public method init() builds data set for contact.
    */
    init: function ()
    {
      setContactVariables();
      Yue.addListener(basicClick,    'click', toggleOpen,
        basic,   true);
      Yue.addListener(comingClick,   'click', toggleOpen,
        coming,  true);
      Yue.addListener(timesClick,    'click', toggleOpen,
        times,   true);
      Yue.addListener(lookingClick,  'click', toggleOpen,
        looking, true);
      //Yue.addListener(passportClick, 'click', toggleOpen,
      //  passport,true);
      Yue.addListener(lodgingClick,  'click', toggleOpen,
        lodging, true);
      Yue.addListener(considerClick, 'click', toggleOpen,
        consider,true);

      setupHiddenFields();
    }
  };

  return that;

}(); // the parens here cause the anonymous func to execute and return
