
if(document.all) {
   window.attachEvent('onload', addEvents)
}
else {
   window.onload = addEvents
}

var bChanged = false

function addEvents() {
//   alert('Binding Events')
   
   var aDivs = document.getElementsByTagName('div')
   for(i = 0;i < aDivs.length; i++) {
      if(aDivs[i].className == 'EquipmentIcon') {
         aDivs[i].attachEvent('onmouseenter', highlight)
         aDivs[i].attachEvent('onmouseleave', lowlight)
      }
   }

   var bDone = false

   for(f=0;f<document.forms.length;f++) {
      var oForm = document.forms[f];
      if(oForm) {
         var oElements
         if(document.all) {
            oElements = oForm.elements
         } else {
            oElements = oForm.childNodes
         }

         for(i=0;i<oElements.length;i++) {
            var oEl = oElements[i]

            if(bDone == false && oEl.type == 'text') {
//               oEl.focus()
               bDone = true;
            }
            if(oEl.type == 'text') {
               if(oEl.maxLength != undefined) {
//                  oEl.attachEvent('onkeydown', checkLength)
               }
               if(oEl.exclude != '1') {
                  if(document.all) {
                     oEl.attachEvent('onkeyup', setModified)
                  } else {
                     // alert('Attaching event')
                     oEl.onkeyup = setModified
                  }
               }
            }
            else if(oEl.type == 'radio' || oEl.type == 'checkbox') {
               if(document.all) {
                  oEl.attachEvent('onclick', setModified)
               } else {
                     // alert('Attaching event')
                  oEl.onclick = setModified
               }
            }
         }
         
         if(document.all) {
            oForm.attachEvent('onsubmit', checkForm)
         } else {
            oForm.onsubmit = checkForm
         }
      }
   }
}

function checkForm(e) {
   // alert('Starting Form validation')
   var oForm 
   var oElements
   
   if(document.all) {
      oForm = e.srcElement
      oElements = oForm.elements
   } else {
      oForm = e.currentTarget
      oElements = oForm.childNodes
   }

   for(i=0;i<oElements.length;i++) {

      if(document.all) {
         if(oElements[i].type == 'text') {
            if(validateField(oElements[i]) == false) {
               return false;
            }
         }
      }
      else {
         if(oElements[i].nodeType == 3) {
            if(validateField(oElements[i]) == false) {
               return false;
            }
         }
      }         
   }

   for(i=0;i<oElements.length;i++) {
      if(oElements[i].type == 'button') {
         oElements[i].disabled = true
      }
      else if(oElements[i].type == 'submit') {
         oElements[i].disabled = true
      }
   }

}

function validateField(oElement) {
   var maxLength = oElement.maxLength
   var validationType = oElement.validation

   if(validationType == 'simple' || validationType == '' || validationType == undefined) {
      if(maxLength != undefined && maxLength != 0 && oElement.value.length > maxLength) {
         alert('Maximum field length exceeded (' + maxLength + ' characters)')
         oElement.value = oElement.value.substring(0, maxLength);
         return false;
      }
      if(oElement.force == 'Y') {
         if(oElement.value == '') {
            oElement.style.backgroundColor = 'yellow';
            alert('Please ensure that all required fields have been filled (missing values highlighted in yellow!')
            return false;
         }
         else {
            oElement.style.backgroundColor = '';
            return true;
         }
      }
   }
   else if(validationType == 'date') {
      if(oElement.value == '' && oElement.force != 'Y') {
         return true;
      }
      else {
         var rCheck = new RegExp("^[0-3]?[0-9] (January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{4}$")
         if(rCheck.test(oElement.value) == false) {
            oElement.style.backgroundColor = 'yellow';
            alert("Date '" + oElement.value + "' is not a valid date for the field highlighted in yellow.  Please enter dates as '1 January 2004'?")
            return false;
         }
         else {
            oElement.style.backgroundColor = '';
            return true;
         }
      }
   }
}


function setModified() {
   bChanged = true
}

function highlight(e) {
   e.srcElement.className = 'EquipmentIconHigh';
}

function lowlight(e) {
   e.srcElement.className = 'EquipmentIcon';
}

function chooseDate(e) {
   if((event.srcElement.className == 'CalRE1') || (event.srcElement.className == 'CalRO1') || (event.srcElement.className == 'CalRE2') || (event.srcElement.className == 'CalRO2')) {
      var sDate = event.srcElement.myDate
      var sTime = event.srcElement.myTime
      var oForm = document.book

      if(event.button == 1) {
         // Left mouse click
         oForm.StartDate.value = sDate
         
         for(i=0; i<oForm.StartTime.length;i++) {
            if(oForm.StartTime[i].value == sTime) {
               oForm.StartTime.selectedIndex = i
            }
         }
      }
      else if(event.button == 2) {
         // Right mouse click
         oForm.EndDate.value = sDate

         for(i=0; i<oForm.EndTime.length;i++) {
            if(oForm.EndTime[i].value == sTime) {
               oForm.EndTime.selectedIndex = i
            }
         }
      }
      return false;
   }
   else {
      return true;
   }
   
}

function showAnswer(sDiv) {
	if(document.all) {
		var oDiv = document.all(sDiv)
	}
	else {
		var oDiv = document.getElementById(sDiv)
	}
	if(oDiv.style.display == 'none') {
		oDiv.style.display = ''
	}
	else {
		oDiv.style.display = 'none'
	}
}

function expand(oLink, sDiv) {
	if(document.all) {
		var oDiv = document.all(sDiv)
	}
	else {
		var oDiv = document.getElementById(sDiv)
	}
	if(oDiv.style.display == 'none') {
		oDiv.style.display = ''
		oLink.innerHTML = 'Hide Detail'
	}
	else {
		oDiv.style.display = 'none'
		oLink.innerHTML = 'Show Detail'
	}
}