
/*
 * Open a popup window
 * href = STRING : URL of target page
 * size_h = height of popup
 * size_w = width of popup
 * pos_t  = distance from the top of the screen
 * pos_l  = distance from the left of the screen
 * scroll = STRING : sets scroll bars (YES,NO,AUTO)
 */
function openPopup(href,size_h,size_w,pos_t,pos_l)
{
  var v_address = href;
  var v_option = "height="+size_h+",width="+size_w+",top="+pos_t+",left="+pos_l;
      v_option = v_option + ",caption=NO,toolbar=NO,location=NO,directories=NO,status=NO,menubar=NO,scrollbars=yes,resizable=yes"

  window.open(v_address, "", v_option);
}

/*
 * check that a radiobox selection has been made
 * form_element = object reference of radiobox
 */
function radioboxChecked(form_element)
{
  for (i=0; i<form_element.length; i++)
  {
    if (form_element[i].checked)
    {
      return true;
    }
  }
  return false;
}

/*
 * validate the email address
 * checks for '@' and '.' within a string
 */
function validateEmail(s)
{
	var b_found_at = false;
	var b_found_dot = false;
	for (var i = 0; i < s.length; i++)
	{
		var ch = s.substring(i, i+1);
		if (ch == '@') { b_found_at = true; }
		else if (ch == '.') { b_found_dot = true; }
	}
	if ((b_found_at==false)||(b_found_dot==false))
	{
		return false;
	}
	return true;
}

/*
 * remove spaces from a string
 */
function removeSpaces(s)
{
  var out = ""

  if (s=="") return out     // No work to do, return empty string

  for (var i=0;i<s.length;i++)
  {
    ch = s.substring(i,i+1) // examine each char in turn
    if (ch!=" ") out += ch  // add to output string if Not a space
  }

  return out
}

/*
 * remove spaces from a string
 */
function makeInt(mi_num)
{
  if (isNumber(mi_num))
  {
    mi_len = mi_num.length
    mi_ch1 = mi_num.substring(0,1)

    if ( (mi_len==2) && (mi_ch1=="0") )
    {
      mi_num = mi_num.substring(1,2);
    }

    mi_num = parseInt(mi_num);
  }
  else
  {
    mi_num = false
  }
  
  return mi_num
}

/*
 * check postcode format
 * dependencies: removeSpaces(s)
 */
function checkPostcode(s)
{
  var pc=s.toUpperCase(), pcformat="", len
  pc=removeSpaces(pc)

  if ((pc.length <5) || (pc.length > 7))
  {
    alert("The postcode length is incorrect - please re-enter")
    return "" // Nope, bad postcode length
  }
  len = pc.length

  for (var i=0;i<len;i++)
  {
    ch = pc.substring(i,i+1) // examine each char in turn
    if ((ch>="A") && (ch<="Z"))
    {
      // if we are dealing with the last 2 chars
      // then examine for unpermitted chars C,I,K,M,O.V
      if ((i+3)>len)
      {
        if ((ch!="C") && (ch!="I") && (ch!="K") && (ch!="M") && (ch!="O") && (ch!="V"))
        {
          pcformat += "X" // special letter
        }
      }
      else
      {
        pcformat += "A" // Normal letter
      }
    }
    if ((ch>="0") && (ch<="9")) pcformat += "N" // is it a letter?
  }

  // the format string MUST be the same length as the postcode string!
  if (pc.length!=pcformat.length)
  {
    alert("The postcode is not correct - please re-enter")
    return "" // Non-alphanumeric chars detected
  }

  // is the format one of the allowed?
  // if so, return the correctly formatted postcode (with space!)

  if (pcformat=="ANNXX")
  {
    outpc = pc.substring(0,2) + " " + pc.substring(2,5) //AN NXX
    return outpc // ok
  }

  if (pcformat=="ANNNXX")
  {
    outpc = pc.substring(0,3) + " " + pc.substring(3,6) //ANN NXX
    return outpc // ok
  }

  if (pcformat=="AANNXX")
  {
    outpc = pc.substring(0,3) + " " + pc.substring(3,6) //AAN NXX
    return outpc // ok
  }

  if (pcformat=="AANNNXX")
  {
    outpc = pc.substring(0,4) + " " + pc.substring(4,7) //AANN NXX
    return outpc // ok
  }

  if (pcformat=="ANANXX")
  {
    outpc = pc.substring(0,3) + " " + pc.substring(3,6) //ANA NXX
    return outpc // ok
  }

  if (pcformat=="AANANXX")
  {
    outpc = pc.substring(0,4) + " " + pc.substring(4,7) //AANA NXX
    return outpc // ok
  }

  // Not one of the correct format... therefore failure!
  {
    alert("The postcode is not in the correct format - please re-enter")
    return ""
  }
}

/*
 * check that a string is a number
 * returns True or False
 */
function isNumber(s)
{
  newString = "";
  count = 0;

  // return false if string is blank
  if (s == "")
  {
    return false
  }

  for (i = 0; i < s.length; i++) 
  {
    ch = s.substring(i, i+1)

    if ((ch >= "0" && ch <= "9") || (ch == ".") || (ch == ",")) 
    {
      newString += ch
    }
  }
    
  if (s != newString) 
  {
    return false
  }
  else 
  {
    return true
  }
}

/*
 * check that a string is only letters
 * returns True or False
 */
function isAlpha(s)
{
  newString = "";
  count = 0;

  // return false if string is blank
  if (s == "")
  {
    return false
  }

  for (i = 0; i < s.length; i++) 
  {
    ch = s.substring(i, i+1);

    if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z" ))
    {
      newString += ch;
    }
  }
    
  if (s != newString) 
  {
    return false
  }
  else 
  {
    return true
  }
}

/*
 * check that age is within boundaries
 * returns True or False
 * dependencies: calculateAge(nDay,nMonth,nYear)
 */
function validateAge(nDay,nMonth,nYear,nMinDays,nMinYears,nMaxYears)
{
  var err;
  var nAge = calculateAge(nDay,nMonth,nYear);

  var ageDays = nAge[0];
  var ageMonths = nAge[1];
  var ageYears = nAge[2];

  //alert(ageDays +' days '+ ageMonths +' months '+ ageYears +' years.');

  if ( (ageDays < 0) || (ageMonths < 0) || (ageYears < 0) )
  {
    alert('Date of birth cannot be in the future.');
    return false;
  }
  if ( (ageDays < nMinDays) && (ageMonths == 0) && (ageYears == 0) )
  {
    alert('Invalid date of birth, Age cannot be under '+ nMinDays +' days.');
    return false;
  }
  if ( ( ageYears > nMaxYears ) || (( ageYears == nMaxYears ) && ( ageMonths >= 1 )) || (( ageYears == nMaxYears ) && ( ageDays >= 1 )) )
  {
    alert('Invalid date of birth, Age cannot be over '+ nMaxYears +' years.');
    return false;
  }
  if ( ageYears < nMinYears )
  {
    alert('Invalid date of birth, Age cannot be under '+ nMinYears +' years.');
    return false;
  }

  return true;
}

/*
 * Silent version of 'validateAge' function
 * Produces no error output
 */
function validateAgeSilent(nDay,nMonth,nYear,nMinDays,nMinYears,nMaxYears)
{
  var err;
  var nAge = calculateAge(nDay,nMonth,nYear);

  var ageDays = nAge[0];
  var ageMonths = nAge[1];
  var ageYears = nAge[2];

  if ( (ageDays < 0) || (ageMonths < 0) || (ageYears < 0) )
  {
    return false;
  }
  if ( (ageDays < nMinDays) && (ageMonths == 0) && (ageYears == 0) )
  {
    return false;
  }
  if ( ( ageYears > nMaxYears ) || (( ageYears == nMaxYears ) && ( ageMonths >= 1 )) || (( ageYears == nMaxYears ) && ( ageDays >= 1 )) )
  {
    return false;
  }
  if ( ageYears < nMinYears )
  {
    return false;
  }

  return true;
}

/*
 * check that a date is valid
 * returns True or False
 */
function validateDate(nDay,nMonth,nYear)
{
  var err,g;

  // Validate Month

  if (nMonth<1 || nMonth>12) err = 1
  if (isNaN(nMonth)) err=1;

  // Validate Day

  if (nDay<1 || nDay>31) err = 2

  if (nMonth==4 || nMonth==6 || nMonth==9 || nMonth==11)
  {
    if (nDay==31) err=2
  }

  if (nMonth==2)
  {
    g=parseInt(nYear/4)

    if (isNaN(g)) err=1

    if (nDay>29) err=2

    if (nDay==29 && ((nYear/4)!=parseInt(nYear/4))) err=2
  }

  if (isNaN(nDay))  err=2;

  // Validate Year

  if (nYear == "") err = 3;
  if (isNaN(nYear)) err = 3;

  // Error Messages
  if ((err==1) || (err==2) || (err==3))
  {
    return false;
  }
  else
  {
    return true;
  }
}

/*
 * calculate age from a date of birth
 */
function calculateAge(nDay,nMonth,nYear)
{
  todaysDate = new Date();
  todaysYear = todaysDate.getYear();
  if (todaysYear < 2000)
  { todaysYear += 1900; }

  todaysMonth = todaysDate.getMonth()+1;
  todaysDay = todaysDate.getDate();

  var monarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  // check for leap year
  if (((todaysYear % 4 == 0) && (todaysYear % 100 != 0)) || (todaysYear % 400 == 0)) monarr[1] = "29";

  countMonth = monarr[todaysMonth];

  // Doing the calculation
  if (todaysDay > nDay)
  {
    diffDay = todaysDay - nDay;
    calcMonth = 0;
  }
  else
  {
    diffDay = todaysDay + countMonth - nDay;
    calcMonth = -1;
  }

  if (todaysMonth > nMonth)
  {
    diffMonth = todaysMonth - nMonth + calcMonth;
    calcYear = 0;
  }
  else
  {
    diffMonth = todaysMonth + 12 - nMonth + calcMonth;
    calcYear = -1;
  }

  diffYear = todaysYear - nYear + calcYear;

  if (diffDay == countMonth) { diffDay = 0; diffMonth ++; }
  if (diffMonth == 12) { diffMonth = 0; diffYear ++; }

  var age = new Array(diffDay, diffMonth, diffYear);

 return age;
}

