var g_pEntryPrompt = "Please enter the "
var g_ErrorMsg = false
var g_Invalidyear 
var g_Invalidmonth 
var g_Invalidday 


function makeArray(n)
{
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}




// This function finds out the current browser and it's version number
// This would return false if it is less than IE 4.0 or Netscape 3.0.
function checkjs()
{       
	var navName = navigator.appName;
	var navVer = parseFloat(navigator.appVersion);
        
        if (navName == "Netscape")
	{
	   if (navVer >= 3)
		return true;
	   else
	        return false;
	}
	else
       if (navName == "Microsoft Internet Explorer")
          {
	   if (navVer >= 4)
		return true;
	   else
		return false;
           }
	return false;
}




// This function strips the leading and trailing spaces in a string
function trim(stringin)
{
  var stringout=""
  var firstpos, lastpos
  var i, j
  firstpos = 0;
  lastpos = 0;
  len = stringin.length 
  
  // Find the first non-space character
  for (i = 0; i <= len-1; i++)
    {
      if (stringin.charAt(i) != " ")
	{ 
         firstpos = i;
         i = len - 1;           
        }
    }
  

  // Find the last non-space character
  for (i = len-1; i >= 0; i--)
    {
      if (stringin.charAt(i) != " ")
	{ 
         lastpos = i;
         i = 0;
        }
    }
 
  // Get only those characters which are inbetween the first non-space and 
  // last non-space characters.
  for (i = firstpos; i <= lastpos; i++)
    {
     stringout = stringout + stringin.charAt(i);
    }
  
  return(stringout);
  
}

function MonthName(month)
{
 var Monthstr = makeArray(12);
  Monthstr[0] = "Jan";
  Monthstr[1] = "Feb";   
  Monthstr[2] = "Mar";
  Monthstr[3] = "Apr";
  Monthstr[4] = "May";
  Monthstr[5] = "Jun";
  Monthstr[6] = "Jul";
  Monthstr[7] = "Aug";
  Monthstr[8] = "Sep";
  Monthstr[9] = "Oct";
  Monthstr[10] = "Nov";
  Monthstr[11] = "Dec";
  
  return(Monthstr[month]);
} 


function currentdate()
{
 var dateobj
 var month, day, year, FormattedDate, i
  
 dateobj = new Date();
 day = dateobj.getDate();
 month = MonthName(dateobj.getMonth());
 dateobj = dateobj + "";
 year = "";
 for (i = dateobj.length-4; i <= dateobj.length-1; i++)
    { 
    year = year + dateobj.charAt(i);
    }
 
 FormattedDate = month + " " + day + ", " + year;
 return(FormattedDate);
}


//The following function checks if a string is an unsigned integer
function isNumeric(stringin)
{
  var i 
  for (i = 0; i <= stringin.length-1; i++)
    {
     if (isNaN(parseInt(stringin.charAt(i))))
      {
       return false;
      }
    }
  return true;
}



//The following function checks if a string is an unsigned decimal number
function isDecimal(stringin,limit)
{ 
  var s
  var i, digitsafterdec=0, numdecpt=0
  var decpointpos=stringin.length
  s = trim(stringin); 
  len = s.length
  
  for (i = 0; i <= len-1; i++)
    {
     
     if (s.charAt(i) != ".")
        {
     	if (i > decpointpos)
          {
           digitsafterdec++;
          }
        //If it is any character other than numbers and decimal point then it is invalid.
        if (isNaN(parseInt(s.charAt(i))))
          {
           return false;
          }
        }
     else
        {
         numdecpt++;
         decpointpos = i;
        }
    }


  // If there are more than one decimal point or if there are more than 3 digits
  // after the decimal poin then it is invalid
  if ((numdecpt > 1) || (digitsafterdec > 2))
    {
     return false;
    }


  // If there is only one character in the string and it is decimal point then it is 
  // invalid
  if ((len == 1) && (decpointpos == 0))
    {
     return false;
    }
  

  // If the decimal number is greater then the permissible limit then it is invalid
  if (s > limit)
    {
     return false;
    }
  
  return true;
}



//The following function checks if a string is a blank
function isBlank(stringin)
{
  var i 
  for (i = 0; i <= stringin.length-1; i++)
    {
     if (stringin.charAt(i) != " ")
      {
       return false;
      }
    }
  return true;
}



function goto_url(url)
{
   window.location = url;
}

function isEmpty(s)
{
   return ((s == null) || (s.length == 0));
}

function promptEntry (s)
{
   alert(g_pEntryPrompt + s)
}


function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}



function InvalidDate(l_year, l_month, l_day)
{
  var daysInMonth = makeArray(12);
  daysInMonth[1] = 31;
  daysInMonth[2] = 29;   
  daysInMonth[3] = 31;
  daysInMonth[4] = 30;
  daysInMonth[5] = 31;
  daysInMonth[6] = 30;
  daysInMonth[7] = 31;
  daysInMonth[8] = 31;
  daysInMonth[9] = 30;
  daysInMonth[10] = 31;
  daysInMonth[11] = 30;
  daysInMonth[12] = 31;
  g_Invalidyear = false;
  g_Invalidmonth = false;
  g_Invalidday = false;

  l_year = trim(l_year);
  l_month = trim(l_month);
  l_day = trim(l_day);
  
  if (isEmpty(l_year))
  {
   g_ErrorMsg = "Please enter the year of birth.";
   g_Invalidyear = true;
   return true;
  }
  
  if (!(isNumeric(l_year)))  
  {
   g_ErrorMsg = "The year entered is invalid. Please enter a valid year of birth.";
   g_Invalidyear = true;
   return true;
  }
  else
  if (isNumeric(l_year))
  { 
    if ((l_year < 1870) || (l_year > 9999))
    {
     g_ErrorMsg = "The year entered is invalid. Please enter a valid year of birth.";
     g_Invalidyear = true;
     return true;
    }
  }



  if (isEmpty(l_month))
  {
   g_ErrorMsg = "Please enter the month of birth.";
   g_Invalidmonth = true;
   return true;
  }
  

  if (!(isNumeric(l_month)))   
  {
   g_ErrorMsg = "The month entered is invalid. Please enter a valid month of birth.";
   g_Invalidmonth = true;
   return true;
  }
  else
  if (isNumeric(l_month))
  { 
    if ((l_month < 1) || (l_month > 12))
  
     {
      g_ErrorMsg = "The month entered is invalid. Please enter a valid month of birth.";
      g_Invalidmonth = true;
      return true;
     }
  }
  
  if (isEmpty(l_day))
  {
   g_ErrorMsg = "Please enter the day of birth.";
   g_Invalidday = true;
   return true;
  }

  if (!(isNumeric(l_day)))   
  {
   g_ErrorMsg = "The day entered is invalid. Please enter a valid day of birth.";
   g_Invalidday = true;
   return true;
  }
  else
  if (isNumeric(l_day))
  { 
    if ((l_day < 1) || (l_day > daysInMonth[l_month]))
    {
     g_ErrorMsg = "The day entered is invalid. Please enter a valid day of birth.";
     g_Invalidday = true;
     return true;
    }
  }


  if ((l_month == 2) && (l_day > daysInFebruary(l_year)))
  {
   g_ErrorMsg = "The day entered is invalid. Please enter a valid day of birth.";
   g_Invalidday = true;
   return true;
  }

  

  return false;
}