var phone_field_length=0;
function TabNext(obj,event,len,next_field) 
{
if (event == "down") {
	phone_field_length=obj.value.length;
	}
	else if (event == "up") {
	if (obj.value.length != phone_field_length) {
		phone_field_length=obj.value.length;
		if (phone_field_length == len) {
			next_field.focus();
			}
			}

		}
}

function isEmpty(inputStr) {
  if (inputStr == "" || inputStr == null) {
    return true
  }
  return false
}

function inRange(inputStr, lo, hi) {
  var num = parseInt(inputStr, 10)
  if (num < lo || num > hi) {
    return false
  }
  return true
}


function validateMonth() 
      {
            var t1=document.form.m1.value
            var t2=document.form.m2.value
            var month=t1+t2

            var input =month
            if (isEmpty(input)) {
            alert("Enter the Month")
	    select(document.form.m1)
            return false
            } else {
            input = parseInt(month, 10)
            if (isNaN(input)) {
            alert("Month Field should be in numbers only")
	    select(document.form.m1)
            return false
            } else {
          
            if (!inRange(input,1,12)) {
            alert("Enter month between 1 (Jan) and 12 (Dec).")
	    select(document.form.m1)
            return false
            }
           }
         }
  return true
}


function validateDate() {
	    var dd1 = document.form.d1.value
   	    var dd2 = document.form.d2.value
            var t1=document.form.m1.value
            var t2=document.form.m2.value
            var month=t1+t2
  
            var input=dd1+dd2

  if (isEmpty(input)) {
    alert("Enter the Day")
    select(document.form.d1)
    return false
  } else {
    input = parseInt(input, 10)
    if (isNaN(input)) {
      alert("Date should be in numbers only")
      select(document.form.d1)
      return false
    } else {
      var mm1=document.form.m1.value;
      var mm2=document.form.m2.value;
      var month
      var monthField = month
      if (!validateMonth(monthField, true)) return false
      var monthVal = parseInt(monthField, 10)
      var monthMax = new Array(31,31,29,31,30,31,30,31,
                               31,30,31,30,31)
      var top = monthMax[monthVal]
      if (!inRange(input,1,top)) {
        alert("Enter a number between 1 and " + top + ".")
        select(document.form.d1)
        return false
      }
    }
  }
  return true
}



function validateYear(year) 
{
            var t5=document.form.y1.value
            var t6=document.form.y2.value
            var t7=document.form.y3.value
            var t8=document.form.y4.value            
       
       var year=t5+t6+t7+t8
       var input = year
      if (isEmpty(input)) 
      {
    alert("Enter the year")
    select(document.form.y1)
    return false
      } 
      else 
      {
    input = parseInt(year, 10)
    if (isNaN(input)) 
        {
      alert("Year entry must be in numbers only")
      select(document.form.y1)
      return false
        } else 
        {
          if (!inRange(input,1900,2005)) 
          {
          alert("Enter a year between 1900 and 2005.")
          select(document.form.y1)
          return false
          }
        }
       }
return true
}

function select(field) {
  field.focus()
  field.select()
}

function tot()
{
var t1=parseInt(document.form.m1.value);
var t2=parseInt(document.form.m2.value);
var t3=parseInt(document.form.d1.value);
var t4=parseInt(document.form.d2.value);
var t5=parseInt(document.form.y1.value);
var t6=parseInt(document.form.y2.value);
var t7=parseInt(document.form.y3.value);
var t8=parseInt(document.form.y4.value);

var total=t1+t2+t3+t4+t5+t6+t7+t8;
var d=(total % 9);
window.open("lucky"+d+".html","myWindow","top=20,left=150,width=500,height=400,buttons=no,scrollbars=yes,location=no,menubar=no,resizable=no,status=no,directories=no,toolbar=no");     

}
function checkFields() 
	{
         validateMonth()&& validateYear()&& validateDate()&& tot()
     }
