function togDiv(dv,togDir) {
// Toggles layers on and off by explicitly setting their display style.
  dv = document.getElementById(dv);
  if ((dv) && (togDir != '')) {
    if (togDir == 'show') { dv.style['display'] = 'block'; }
    else { dv.style['display'] = 'none'; }
  }
}

function checkAddHist(step) {
// Calculates how many addresses should be displayed on the Address Details page and calls togDiv to turn them on and off.
  var years1 = parseInt(document.getElementById('addYears').value);
  var months1 = parseInt(document.getElementById('addMonths').value);
  var years2 = parseInt(document.getElementById('prevYears').value);
  var months2 = parseInt(document.getElementById('prevMonths').value);
  var totMths1 = months1 + (years1 * 12);
  var totMths2 = months2 + (years2 * 12);
  if (step == '1') {
    if (totMths1 < 36) { togDiv('prevAddress1','show'); if (totMths2 != 0) { checkAddHist('2'); } }
    else { togDiv('prevAddress1','hide'); togDiv('prevAddress2','hide'); }
  } else if (step == '2') {
    if ((totMths1 + totMths2) < 36) { togDiv('prevAddress2','show'); }
    else { togDiv('prevAddress2','hide'); }
  }
}

function defCompGrps() {
// Defines the fields that should be on or off depending on whether Company Details should be displaying.
  compOnGrp = new Array();
  compOffGrp = new Array();

  for (n=0; n<100; n++) {
    if (document.getElementById('compOn' + n)) {
//      alert('Found ' + 'compOn' + n);
      compOnGrp[n] = document.getElementById('compOn' + n);
    }
  }
  for (n=0; n<100; n++) {
    if (document.getElementById('compOff' + n)) {
//      alert('Found ' + 'compOff' + n);
      compOffGrp[n] = document.getElementById('compOff' + n);
    }
  }
}

function setCompOn() {
//  Runs setComp with a value of On. This is needed to call setComp from an event handler which can't take variables.
  setComp('On');
}

function setComp(way) {
// Displays and hides elements from the compGrps by changing their classes.
// alert('compOnGrp.length = ' + compOnGrp.length + '\n' + 'compOffGrp.length = ' + compOffGrp.length);
  for (n = 0; n < compOnGrp.length; n++) {
    if (way == 'On') { compOnGrp[n].className = 'vis'; }
    else { compOnGrp[n].className = 'hid'; }
  }
  for (n = 0; n < compOffGrp.length; n++) {
    if (way == 'On') { compOffGrp[n].className = 'hid'; }
    else { compOffGrp[n].className = 'vis'; }
  }
}

function addEvent(obj, evType, fn){
// alert('addEvent is Adding on' + evType + ' to ' + obj + '.');
  if (obj.addEventListener) { obj.addEventListener(evType, fn, true); return true; }
  else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; }
  else { return false; }
}

function dispForm() {
// Displays hidden forms.
var hidForms = new Array('yourdetails', 'addressdetails', 'addressdetailscompany', 'payment')
  for (x=0; x < hidForms.length; x++) {
    if (document.getElementById(hidForms[x])) {
      document.getElementById(hidForms[x]).style['display'] = 'block';
    }
  }
}

function checkdates(obj) {
	year = document.payment.birthyear.options[document.payment.birthyear.selectedIndex].value;
	month = document.payment.birthmonth.options[document.payment.birthmonth.selectedIndex].value;
	day = document.payment.birthday.options[document.payment.birthday.selectedIndex].value;
	var isleap=0;
	var dateerror=0;
	if (year % 4 != 0) {
		isleap=0;
	} else if  (year % 400 == 0) {
		isleap=1;
	} else if (year % 100 == 0) {
		isleap=0;
	} else {
		isleap=1;
	}
	if ((day == 31) && ((month== 2) || (month == 4) || (month == 6) || (month == 9) || (month == 11))) {
		dateerror=1;
	}
	if ((day == 30) && (month == 2)) {
		dateerror=2;
	}
	if (month == 2 && day == 29 && isleap == 0) {
		dateerror=3;
	}
	if (dateerror != 0) {
		alert('The date you have selected is invalid.');
		obj.selectedIndex=0;
		return false;
	} else {
		return true;
	}
}