function start() 
{
    document.getElementById('day').value = "";
    document.getElementById('month').value = "";
    document.getElementById('year').value = "";
    document.getElementById('age').value = "";
    
}


function GetElementData( iElementId )
{
  if( typeof(iElementId) == "string" && iElementId.length > 0 )
  {
    var element = document.getElementById(iElementId);
    if( element )
      return element.value;
    else
      alert("ERROR: could not find the requested element");
  }
}

function leapyear(a) 
{
    if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0)) 
    return true; 
    else 
    return false;
}

function go() 
{

    mm = parseInt(mm); dd = parseInt(dd); yy = parseInt(yy);
    if ((mm < 1) || (mm > 12) || (dd < 1) || (dd > 31) || (yy < 1) ||(mm == " ") || (dd == " ") || (yy == " "))  main="Invalid";
    else if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30)) main = "Invalid";
    else if (mm == 2) 
    {
        if (dd > 29) main = "Invalid"; 
        else if(( dd > 28) && (!lyear(yy))) main = "Invalid";
    }
    else main = main;
    if(main == "valid") 
    {
        var m;
        if (mm == 1) n = 31; 
        if (mm == 2) n = 59 + 1; 
        if (mm == 3) n = 90 + 1; 
        if (mm == 4) n = 120 + 1; 
        if (mm == 5) n = 151 + 1; 
        if (mm == 6) n = 181 + 1; 
        if (mm == 7) n = 212 + 1; 
        if (mm == 8) n = 243 + 1; 
        if (mm == 9) n = 273 + 1; 
        if (mm == 10) n = 304 + 1; 
        if (mm == 11) n = 334 + 1; 
        if (mm == 12) n = 365 + 1;
        if((mm == 1)||(mm == 3)||(mm == 5)||(mm == 7)||(mm == 8)||(mm == 10)||(mm == 12)) n += 31 + dd;
        else if((mm == 4)||(mm == 6)||(mm == 9)||(mm == 11)) n += 31 + dd + 1;
        else if(mm == 2) 
        {
            if(lyear(yy)) n += 29 + dd - 3; 
            else if(!lyear(yy)) n += 28 + dd - 1;
        }
        switch(0)
        {
            case 0 : document.getElementById('age').value +=""
            break
        }
    }
    else 
    {
        document.getElementById('age').value += main + " Date";
    }
}

function run() 
{
    var ap;
    dd = document.getElementById('day').value;
    mm = document.getElementById('month').value;
    yy = document.getElementById('year').value;
    main="valid";
    
    if ((mm < 1) || (mm > 12) || (dd < 1) || (dd > 31) || (yy < 1) ||(mm == "") || (dd == "") || (yy == "")) main = "Invalid";
    else if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30)) main = "Invalid";
    else if (mm == 2) 
    {
        if (dd > 29) main = "Invalid"; 
        else if((dd > 28) && (!lyear(yy))) main="Invalid";
    }
    else if((yy > 9999)||(yy < 0)) main = "Invalid";
    else main = main;
    		
    if(main == "valid") 
    {

        days = new Date(); 
        gdate = days.getDate(); 
        gmonth = days.getMonth(); 
        gyear = days.getYear(); 
        
      	if (navigator.appName!="Microsoft Internet Explorer")
		    {
			    gyear += 1900
		    }
			    
        age = gyear - yy;
			
        if((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) 
        {
            age = age;
        } 
        else 
        {
            if(mm <= (gmonth)) 
            {
                age = age;
            } 
            else 
            {
                age = age - 1;
            }
        }
        if(age == 0) age = age; 
        document.getElementById('age').value=age;
        document.getElementById("under30").style.display = "none"; 
        document.getElementById("over30").style.display = "none";
        if (age>=30) document.getElementById("over30").style.display = "block"; 
        else 
        {
            document.getElementById("under30").style.display = "block";
        } 
        if(mm <= (gmonth + 1)) age = age - 1; 
        if((mm == (gmonth + 1)) && (dd > parseInt(gdate))) age = age + 1; go();
    }
    else 
    {
        document.getElementById('age').value = main + " Date";
        alert('Invalid Birth Date.  Please try again');
    }
}

if (window.attachEvent) {window.attachEvent('onload', start);}
else if (window.addEventListener) {window.addEventListener('load', start, false);}
else {document.addEventListener('load', start, false);} 
