var pos = document.cookie.indexOf("UnderAge=");
if (pos == false)
{
  location = "index2.html";
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }


function isDate(day,month,year) {
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
    (month == test.getMonth()) &&
    (day == test.getDate()) )
    return true;
    else
    return false
}


function getMonthLength(month,year,julianFlag)
{
   var ml;
   if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10||month==12)
      {ml = 31;}
   else {
       if(month==2) {
          ml = 28;
          if(!(year%4) && (julianFlag==1 || year%100 || !(year%400)))
             ml++;
       }
       else
          {ml = 30;}
   }
   return ml;    
}


function CheckAge(form)
{
   var myLimit = 21;
   
    // Get Current Date
   Today=new Date();
   var yd = Today.getYear();
   var md = Today.getMonth();
   var dd = Today.getDate();
  
   // Get Date from the form
   var yb = form.yb.value;
   var mb = form.mb.options[form.mb.selectedIndex].value;
   var db = form.db.options[form.db.selectedIndex].value;
   
  
   
   MNames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
      "Oct","Nov","Dec");
   
   
    if((db == 0) || 
       (mb == 0) || 
       (yb == 0))
   {
      alert("You must enter a valid date");
      return;
   }
   else
   {
		if (isDate(db,mb,yb)==false) 
			{
		    alert("You must enter a valid date");
		    return;
		    }
   }
   
   
   
   // Month length 0->use calendar length
   var mLength = 0;
   // 0 if Gregorian, 1 is Julian
   var isJulian = 1;

   var ma=0;
   var ya=0;

   var da = dd-db;
   // This is the all-important day borrowing code.
   if(da<0)
   {
      md--;
      // Borrow months from the year if necesssary.
      if(md<1)
      {
	 yd--;
	 // Determine no. of months in year
	 if(mLength)
	    {md=md+parseInt(365/mLength);}
	 else
	    {md=md+12;}
      }
      if(mLength==0) // Use real month length if no fixed
      {              // length is indicated - note that we add a leap day if necessary.
         ml=getMonthLength(md,yd,isJulian);
	 da=da+ml;
      } 
      // For this case, everything works like it did in elementary school.
      else
	 {da+=mLength;} // Use fixed month length
   }

   ma = md - mb;
   // Month borrowing code - borrows months from years.
   if(ma<0)
   {
      yd--;
      if(mLength!=0)
	 {ma=ma+parseInt(365/mLength);}
      else
	 {ma=ma+12;}
   }

   ya = yd - yb;

    // Find if you are over AgeLimit.
    if(ya>=myLimit)
   {
      makeCookie("UnderAge",false);
      location = "index2.html"; 
   }
   else
   {
      alert("Sorry, you are not allowed to view this site.");
   }

 }


//Cookie things

function makeCookie(cookieName,cookieData)
{
        var expiry = new Date();
        expiry.setTime(expiry.getTime() + 1 * (24 * 60 * 60 * 1000));
        setCookie(cookieName, cookieData, expiry);
}

function setCookie(cookieName, cookieData, expiry)
{
    document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expiry.toGMTString();;
}



