//set some variables
var invalid_date_msg = 'Please enter a valid date';

//set url if under age
var underage_url = "http://www.vivelohoy.com/noticias/especial/vlh-mensagemenores-1005,0,3828981.story";

//use cookies? set to false for no cookies-
var usecookies = true;
//set age ok URL if not using cookies
var oknocookies = "http://www.vivelohoy.com/noticias/especial/vlh-forma-cuervo,0,5337599.customform";

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) {
	   
	  location = oknocookies;
   }
   else
   {
	   //redirect to the under age url
       location = underage_url; 

   }

 }


