// FILE: cwdAgeCheck.js
// CREATED: 04/06/2007
// MODIFIED: 04/06/2007
// AUTHOR: Christopher W. DeLaurentis
// EMAIL: cwd@thoughtchamber.com
// MODIFIED-BY: Christopher W. DeLaurentis
// NOTES: 

//CWD----------------------------------------------------------------------------------------------

var AgeManager=Class.create();
  AgeManager.prototype={
    initialize: function(oSelectCountry,oSelectYear,oSelectMonth,oSelectDay,oMonthDayDiv,oEnterButton,strNextPageURL,strErrorPageURL,strPopUpURL,blnSkipInvalidYears,iProductID)
    {
      this.blnSkipInvalidYears=blnSkipInvalidYears;
      this.oSelectCountry=$(oSelectCountry);
      this.oSelectYear=$(oSelectYear);
      this.oSelectMonth=$(oSelectMonth);
      this.oSelectDay=$(oSelectDay);
      this.oMonthDayDiv=$(oMonthDayDiv);
      this.oEnterButton=$(oEnterButton);
      this.strNextPageURL=strNextPageURL;
      this.strPopUpURL=strPopUpURL;
      this.strErrorPageURL=strErrorPageURL;
      this.iProductID=iProductID;

      this.aMonths=[
        { name: "January", id: 1, days: 31 },
        { name: "February", id: 2, days: 28 },
        { name: "March", id: 3, days: 31 },
        { name: "April", id: 4, days: 30 },
        { name: "May", id: 5, days: 31 },
        { name: "June", id: 6, days: 30 },
        { name: "July", id: 7, days: 31 },
        { name: "August", id: 8, days: 31 },
        { name: "September", id: 9, days: 30 },
        { name: "October", id: 10, days: 31 },
        { name: "November", id: 11, days: 30 },
        { name: "December", id: 12, days: 31 }
        ];

      this.aCountries= [
      {id: 223, age: 21, country: "United States"},
      {id: 138, age: 18, country: "Mexico"},
      {id: 1, age: 999, country: "Afghanistan"},
      {id: 2, age: 18, country: "Albania"},
      {id: 3, age: 18, country: "Algeria"},
      {id: 5, age: 18, country: "Andorra"},
      {id: 6, age: 18, country: "Angola"},
      {id: 9, age: 18, country: "Antigua And Barbuda"},
      {id: 10, age: 18, country: "Argentina"},
      {id: 11, age: 18, country: "Armenia"},
      {id: 13, age: 18, country: "Australia"},
      {id: 14, age: 18, country: "Austria"},
      {id: 15, age: 999, country: "Azerbaijan"},
      {id: 15, age: 18, country: "Bahamas"},
      {id: 17, age: 21, country: "Bahrain"},
      {id: 18, age: 999, country: "Bangladesh"},
      {id: 19, age: 18, country: "Barbados"},
      {id: 20, age: 18, country: "Belarus"},
      {id: 21, age: 18, country: "Belgium"},
      {id: 22, age: 18, country: "Belize"},
      {id: 23, age: 18, country: "Benin"},
      {id: 25, age: 18, country: "Bhutan"},
      {id: 26, age: 18, country: "Bolivia"},
      {id: 27, age: 18, country: "Bosnia-Herzegovina"},
      {id: 28, age: 18, country: "Botswana"},
      {id: 30, age: 18, country: "Brazil"},
      {id: 32, age: 999, country: "Brunei Darussalam"},
      {id: 33, age: 18, country: "Bulgaria"},
      {id: 34, age: 18, country: "Burkina Faso"},
      {id: 35, age: 18, country: "Burundi"},
      {id: 36, age: 18, country: "Cambodia"},
      {id: 37, age: 18, country: "Cameroon"},
      {id: 38, age: 19, country: "Canada"},
      {id: 39, age: 18, country: "Cape Verde"},
      {id: 41, age: 18, country: "Central African Republic"},
      {id: 42, age: 18, country: "Chad"},
      {id: 43, age: 21, country: "Chile"},
      {id: 44, age: 18, country: "China"},
      {id: 47, age: 18, country: "Colombia"},
      {id: 48, age: 18, country: "Comoros"},
      {id: 49, age: 18, country: "Congo"},
      {id: 51, age: 18, country: "Costa Rica"},
      {id: 52, age: 18, country: "Cote D'ivoire"},
      {id: 53, age: 18, country: "Croatia"},
      {id: 54, age: 18, country: "Cuba"},
      {id: 55, age: 18, country: "Cyprus"},
      {id: 56, age: 18, country: "Czech Republic"},
      {id: 57, age: 18, country: "Denmark"},
      {id: 58, age: 999, country: "Djibouti"},
      {id: 59, age: 18, country: "Dominica"},
      {id: 60, age: 18, country: "Dominican Republic"},
      {id: 237, age: 18, country: "Dr Congo"},
      {id: 61, age: 18, country: "East Timor"},
      {id: 62, age: 18, country: "Ecuador"},
      {id: 63, age: 999, country: "Egypt"},
      {id: 64, age: 18, country: "El Salvador"},
      {id: 65, age: 18, country: "Equatorial Guinea"},
      {id: 66, age: 18, country: "Eritrea"},
      {id: 67, age: 18, country: "Estonia"},
      {id: 68, age: 999, country: "Ethiopia"},
      {id: 71, age: 18, country: "Fiji"},
      {id: 72, age: 999, country: "Finland"},
      {id: 73, age: 999, country: "France"},
      {id: 78, age: 18, country: "Gabon"},
      {id: 79, age: 18, country: "Gambia"},
      {id: 80, age: 18, country: "Georgia"},
      {id: 81, age: 18, country: "Germany"},
      {id: 82, age: 18, country: "Ghana"},
      {id: 84, age: 18, country: "Greece"},
      {id: 86, age: 18, country: "Grenada"},
      {id: 89, age: 18, country: "Guatemala"},
      {id: 90, age: 18, country: "Guinea"},
      {id: 91, age: 18, country: "Guinea-Bissau"},
      {id: 92, age: 18, country: "Guyana"},
      {id: 93, age: 18, country: "Haiti"},
      {id: 95, age: 18, country: "Honduras"},
      {id: 96, age: 18, country: "Hong Kong"},
      {id: 97, age: 999, country: "Hungary"},
      {id: 98, age: 999, country: "Iceland"},
      {id: 99, age: 25, country: "India"},
      {id: 100, age: 21, country: "Indonesia"},
      {id: 101, age: 999, country: "Iran"},
      {id: 102, age: 999, country: "Iraq"},
      {id: 103, age: 18, country: "Ireland"},
      {id: 104, age: 18, country: "Israel"},
      {id: 105, age: 18, country: "Italy"},
      {id: 106, age: 18, country: "Jamaica"},
      {id: 107, age: 20, country: "Japan"},
      {id: 108, age: 999, country: "Jordan"},
      {id: 109, age: 999, country: "Kazakhstan"},
      {id: 110, age: 18, country: "Kenya"},
      {id: 111, age: 18, country: "Kiribati"},
      {id: 112, age: 18, country: "North Korea"},
      {id: 113, age: 18, country: "Korea"},
      {id: 114, age: 999, country: "Kuwait"},
      {id: 115, age: 999, country: "Kyrgyzstan"},
      {id: 116, age: 18, country: "Lao"},
      {id: 117, age: 18, country: "Latvia"},
      {id: 118, age: 18, country: "Lebanon"},
      {id: 119, age: 18, country: "Lesotho"},
      {id: 120, age: 18, country: "Liberia"},
      {id: 121, age: 999, country: "Libya"},
      {id: 122, age: 18, country: "Liechtenstein"},
      {id: 123, age: 18, country: "Lithuania"},
      {id: 124, age: 18, country: "Luxembourg"},
      {id: 126, age: 18, country: "Macedonia"},
      {id: 127, age: 18, country: "Madagascar"},
      {id: 128, age: 18, country: "Malawi"},
      {id: 129, age: 18, country: "Malaysia"},
      {id: 130, age: 18, country: "Maldives"},
      {id: 131, age: 18, country: "Mali"},
      {id: 132, age: 18, country: "Malta"},
      {id: 133, age: 18, country: "Marshall Islands"},
      {id: 135, age: 18, country: "Mauritania"},
      {id: 136, age: 18, country: "Mauritius"},
      {id: 139, age: 18, country: "Micronesia"},
      {id: 140, age: 18, country: "Moldova"},
      {id: 141, age: 18, country: "Monaco"},
      {id: 142, age: 18, country: "Mongolia"},
      {id: 144, age: 18, country: "Morocco"},
      {id: 145, age: 18, country: "Mozambique"},
      {id: 146, age: 18, country: "Myanmar"},
      {id: 147, age: 18, country: "Namibia"},
      {id: 148, age: 18, country: "Nauru"},
      {id: 149, age: 999, country: "Nepal"},
      {id: 150, age: 18, country: "Netherlands"},
      {id: 153, age: 18, country: "New Zealand"},
      {id: 154, age: 18, country: "Nicaragua"},
      {id: 155, age: 18, country: "Niger"},
      {id: 156, age: 18, country: "Nigeria"},
      {id: 160, age: 999, country: "Norway"},
      {id: 161, age: 21, country: "Oman"},
      {id: 162, age: 999, country: "Pakistan"},
      {id: 163, age: 18, country: "Palau"},
      {id: 164, age: 18, country: "Panama"},
      {id: 165, age: 18, country: "Papua New Guinea"},
      {id: 166, age: 20, country: "Paraguay"},
      {id: 167, age: 18, country: "Peru"},
      {id: 168, age: 18, country: "Philippines"},
      {id: 170, age: 999, country: "Poland"},
      {id: 171, age: 18, country: "Portugal"},
      {id: 173, age: 18, country: "Qatar"},
      {id: 175, age: 18, country: "Romania"},
      {id: 176, age: 18, country: "Russian Federation"},
      {id: 177, age: 18, country: "Rwanda"},
      {id: 181, age: 21, country: "Samoa"},
      {id: 182, age: 18, country: "San Marino"},
      {id: 183, age: 18, country: "Sao Tome And Principe"},
      {id: 184, age: 999, country: "Saudi Arabia"},
      {id: 185, age: 18, country: "Senegal"},
      {id: 186, age: 18, country: "Seychelles"},
      {id: 187, age: 18, country: "Sierra Leone"},
      {id: 188, age: 18, country: "Singapore"},
      {id: 189, age: 18, country: "Slovakia"},
      {id: 190, age: 18, country: "Slovenia"},
      {id: 191, age: 21, country: "Solomon Islands"},
      {id: 192, age: 18, country: "Somalia"},
      {id: 193, age: 18, country: "South Africa"},
      {id: 195, age: 18, country: "Spain"},
      {id: 196, age: 18, country: "Sri Lanka"},
      {id: 178, age: 18, country: "St Kitts And Nevis"},
      {id: 179, age: 18, country: "St Lucia"},
      {id: 180, age: 18, country: "St Vincent And Gren."},
      {id: 199, age: 999, country: "Sudan"},
      {id: 200, age: 18, country: "Suriname"},
      {id: 202, age: 18, country: "Swaziland"},
      {id: 203, age: 999, country: "Sweden"},
      {id: 204, age: 18, country: "Switzerland"},
      {id: 205, age: 999, country: "Syrian Arab Republic"},
      {id: 206, age: 18, country: "Taiwan"},
      {id: 207, age: 999, country: "Tajikistan"},
      {id: 208, age: 18, country: "Tanzania"},
      {id: 209, age: 20, country: "Thailand"},
      {id: 210, age: 18, country: "Togo"},
      {id: 212, age: 18, country: "Tonga"},
      {id: 213, age: 18, country: "Trinidad And Tobago"},
      {id: 214, age: 18, country: "Tunisia"},
      {id: 215, age: 18, country: "Turkey"},
      {id: 216, age: 999, country: "Turkmenistan"},
      {id: 218, age: 18, country: "Tuvalu"},
      {id: 219, age: 18, country: "Uganda"},
      {id: 220, age: 18, country: "Ukraine"},
      {id: 221, age: 21, country: "United Arab Emirates"},
      {id: 222, age: 18, country: "United Kingdom"},
      {id: 223, age: 18, country: "Uruguay"},
      {id: 224, age: 999, country: "Uzbekistan"},
      {id: 226, age: 18, country: "Vanuatu"},
      {id: 228, age: 18, country: "Venezuela"},
      {id: 229, age: 18, country: "Viet Nam"},
      {id: 234, age: 999, country: "Yemen"},
      {id: 238, age: 18, country: "Zambia"},
      {id: 239, age: 18, country: "Zimbabwe"}];

      this.loadCountries();
      Event.observe(this.oSelectCountry,"change", this.loadYears.bindAsEventListener(this));
      Event.observe(this.oSelectYear,"change", this.loadMonths.bindAsEventListener(this));
      Event.observe(this.oSelectMonth,"change", this.loadDays.bindAsEventListener(this));
      Event.observe(this.oEnterButton,"click",this.enterSite.bindAsEventListener(this));
      this.oSelectCountry.selectedIndex=0;
      this.loadYears();
      this.loadMonths();
    },

    findCountryByID: function(iCID)
    {
      for(var i=0;i<this.aCountries.length;++i)
      {
        if(this.aCountries[i].id==iCID)
          return this.aCountries[i];
      }

      return null;
    },

    loadCountries: function()
    {
      for(var i=0;i<this.aCountries.length;++i)
        this.oSelectCountry.options[this.oSelectCountry.options.length]=new Option(this.aCountries[i].country,this.aCountries[i].id);
    },

    getSelectedCountryID: function()
    {
      return parseInt(this.oSelectCountry.options[this.oSelectCountry.selectedIndex].value);
    },

    getSelectedYear: function()
    {
      return parseInt(this.oSelectYear.options[this.oSelectYear.selectedIndex].value);
    },

    getSelectedMonth: function()
    {
      return parseInt(this.oSelectMonth.options[this.oSelectMonth.selectedIndex].value);
    },

    getSelectedDay: function()
    {
      return parseInt(this.oSelectDay.options[this.oSelectDay.selectedIndex].value);
    },

    getCountryAge: function(iCID)
    {
      var oC=this.findCountryByID(parseInt(iCID));

      if(oC==null)
        return 0;

      return oC.age;
    },

    loadYears: function()
    {
      var dt=new Date();
      var iCurrentYear=dt.getFullYear();
      var iStart=iCurrentYear; //CWD-- list all years regardless

      if(this.blnSkipInvalidYears)
        iStart=iCurrentYear-this.getCountryAge(this.getSelectedCountryID());  //CWD-- only list applicable years

      this.oSelectYear.innerHTML="";

      for(var i=0;i<100;++i,iStart--)
        this.oSelectYear.options[this.oSelectYear.options.length]=new Option(iStart,iStart);
    },

    loadMonths: function()
    {
     this.oSelectMonth.innerHTML="";

      for(var i=0;i<this.aMonths.length;++i)
        this.oSelectMonth.options[this.oSelectMonth.options.length]=new Option(this.aMonths[i].name,this.aMonths[i].id);

      this.loadDays();
    },

    loadDays: function()
    {
      var iDays=this.aMonths[this.oSelectMonth.selectedIndex].days;
      var iYear=this.oSelectYear.options[this.oSelectYear.selectedIndex].value;
      this.oSelectDay.innerHTML="";
      iYear=parseInt(iYear);
      iDays=parseInt(iDays);

      if(this.oSelectMonth.selectedIndex==1)
      {
        if((iYear % 400)==0)
          iDays+=1;
        else if((iYear % 100)==0)
          iDays+=0;
        else if((iYear % 4)==0)
          iDays+=1;
        else
          iDays+=0;
      }

      for(var i=1;i<=iDays;++i)
        this.oSelectDay.options[this.oSelectDay.options.length]=new Option(i,i);
    },

    exposeMonthDay: function()
    {
      var dt=new Date();
      var iCurrentYear=dt.getFullYear();
      var iLastValidYear=iCurrentYear-this.getCountryAge(this.getSelectedCountryID());
      var iSelectedYear=this.getSelectedYear();

      if(iSelectedYear==iLastValidYear)
      {
        if(this.oMonthDayDiv.style.display=="block")
          return false;

        this.oMonthDayDiv.style.display="block";
        return true;
      }

      return false;
    },

    checkAge: function()
    {
      var blnIsValidMonthDay=false;
      var dt=new Date();
      var iCurrentYear=dt.getFullYear();
      var iCurrentDay=dt.getDate();
      var iSelectedDay=this.getSelectedDay();
      var iCurrentMonth=dt.getMonth()+1;
      var iSelectedMonth=this.getSelectedMonth();
      var iLastValidYear=iCurrentYear-this.getCountryAge(this.getSelectedCountryID());
      var iSelectedYear=this.getSelectedYear();

      if(this.oMonthDayDiv.style.display=="block")
      {
        if(iSelectedMonth<iCurrentMonth)
          blnIsValidMonthDay=true;
        else if((iCurrentMonth==iSelectedMonth)&&(iSelectedDay<=iCurrentDay))
          blnIsValidMonthDay=true;
      }
      else
        blnIsValidMonthDay=true;

      return ((iSelectedYear<=iLastValidYear)&&blnIsValidMonthDay);
    },

    enterSite: function()
    {
      if(this.exposeMonthDay())
        return;

      if(this.checkAge())
      {
        var oHidden=$("hiddenFrame");

        if(oHidden)
        {
          oHidden.src=this.strNextPageURL+"&ISPOPUP=1&PRODUCTID="+this.iProductID+"&COUNTRYID="+this.getSelectedCountryID()+"&YEAR="+this.getSelectedYear()+"&MONTH="+this.getSelectedMonth()+"&DAY="+this.getSelectedDay();
          launchWindow(this.strPopUpURL);
        }
        else
          launchWindow(this.strNextPageURL);
          //document.location=this.strNextPageURL;
      }
      else
        window.open(this.strErrorPageURL,"");
    }
  };


function launchWindow(str)
{
  var iLeft = (screen.width-770)/2;
  var iTop = (screen.height-600)/2;
  window.open(str,'mainwindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable=yes,width=770,height=600,left='+iLeft+',top='+iTop,true);
}
