//set some variables
var invalid_date_msg = 'Please enter a valid date';
var nocookies_msg = 'You must <a href="http://www.google.com/cookies.html" target="_blank">enable cookies</a> to enter this site';

//set url if under age
var underage_url = "http://www.bobthebuilder.com/de/main.html";

//use cookies? set to false for no cookies-
var usecookies = true;
//set age ok URL if not using cookies
var oknocookies = "index.html";

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

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

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

function CheckAge(form)
{

   /* set age limit */
   var ageLimit = 21;

    //Current Date
   Today=new Date();
   var ytoday = Today.getFullYear();
   var mtoday = Today.getMonth();
   var dtoday = Today.getDate();

   //add 1 ro current month
   mtoday +=1;

   // Get Date from the form
   var birthyear = document.getElementById("birthyear").value;
   var birthmonth = document.getElementById("birthmonth").value;
   var birthdate = document.getElementById("birthdate").value;

  //alert(birthyear+"\n"+birthmonth+"\n"+birthdate);

   if((birthdate == 0) ||
       (birthmonth == 0) ||
       (birthyear == 0))
   {

        document.getElementById('errors').innerHTML=invalid_date_msg;
        document.getElementById('errors').style.display="block";
    return;
   }
   else
   {
                if (valDate(birthdate,birthmonth,birthyear)==false)
                        {
                    document.getElementById('errors').innerHTML=invalid_date_msg;
                        document.getElementById('errors').style.display="block";
                    return;
                    }
   }



   // if 0 use calendar length
   var mLength = 0;
   // 0 if Gregorian calendar
   var isJulian = 1;

   var mmonth=0;
   var yyear=0;

   var dday = dtoday-birthdate;
   // borrow days/months if necessary
   if(dday<0)
   {
      mtoday--;
      // Borrow months  if necesssary.
      if(mtoday<1)
      {
         ytoday--;
         // months in year
         if(mLength)
            {mtoday=mtoday+parseInt(365/mLength);}
         else
            {mtoday=mtoday+12;}
      }
      if(mLength==0)
      {              // add a leap day if necessary.
         monthdays=MonthLength(mtoday,ytoday,isJulian);
         dday=dday+monthdays;
      }
      // default month/days
      else
         {dday+=mLength;} // Use fixed month length
   }

   mmonth = mtoday - birthmonth;
   // borrow months if necessary
   if(mmonth<0)
   {
      ytoday--;
      if(mLength!=0)
         {mmonth=mmonth+parseInt(365/mLength);}
      else
         {mmonth=mmonth+12;}
   }

   yyear = ytoday - birthyear;
   //alert("ytoday "+ytoday+"\nbirthyear "+birthyear+"\nyyear "+yyear);

   // over ageLimit? submit form

   if(yyear>=ageLimit) {

           if (usecookies) document.forms.verify.submit();
           else location = oknocookies;
   }
   else
   {
           //redirect to the under age url
       location = underage_url;

   }

 }


//test for cookies
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
else return "";
}

function testCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie() {
//hide the no javascript default message
document.getElementById("nojs").style.display="none";
if (usecookies){
testCookie('username','username',365);
username=getCookie('username');
if (usecookies && (username==null || username=="")) {
        document.getElementById('errors').innerHTML=nocookies_msg;
        document.getElementById('errors').style.display="block";
}
}
}
onload=checkCookie;



//this is the test for cookies if cookies are enabled -
//if cookies are enabled, adding <script type="text/javascript" src="intro/verify.js"></script>
//to every page will check if the cookie is set and if not, redirect to index.html to verify
function getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
  }
}
else return "";
}
//set usecookies to false to not use cookies
if (usecookies)        {
agecookie=getCookie('age');
loc = location.toString();
if (!agecookie && !(loc.indexOf("index.html")+1)) location="index.html";
}


