/**
 *  file: wrpValidator.js
 *  created: ?
 *  updated: mvellines - 2007/03/30 | extracted script from html page and modified to work as external script.
 *  purpose: separate behavioral enhancements from structural markup
 *
 *  Adds event handler to validate the Worker Retraining Information
 *  Request form without using HTML attributes.
 */

// Ensures all prior page-load handlers run plus the specifed one.
function addLoadEvent(fn) {
  var old = window.onload;
  if(typeof window.onload != 'function') {
    window.onload = fn;
  } else {
    window.onload = function() {
      old();
      fn();
    }
  }
}


// Add event handler for the form submission.
function wireFormHandler(e) {
  var frm = document.getElementById('wrpRequest');
  frm.onsubmit = checkform;
  return true;
}


function checkform (e) {
  if(!e) { e = window.event; } // IE
  var form = document.getElementById('wrpRequest');
  
  // NAME
  if (form.Name1.value == "") {
    alert( "Please enter your 'Name' under 'Contact Information'." );
    form.Name1.focus();

    // Prevent main window from following link.
    if(e.preventDefault) {
      e.preventDefault();
    } else {
      //     e.cancelBubble= true;
      e.returnValue = false;
    }
    return false;
  }
  
  // TELEPHONE
  if (form.Telephone.value == "") {
    alert( "Please enter your 'Phone' under 'Contact Information'." );
    form.Telephone.focus();
    
    // Prevent main window from following link.
    if(e.preventDefault) {
      e.preventDefault();
    } else {
      //     e.cancelBubble= true;
      e.returnValue = false;
    }
    return false;
  }
  
  //  EMAIL
  if (form.Email.value == "") {
    alert( "Please provide an 'email' under 'Contact Information'." );
    form.Email.focus();
    
    // Prevent main window from following link.
    if(e.preventDefault) {
      e.preventDefault();
    } else {
      //     e.cancelBubble= true;
      e.returnValue = false;
    }
    return false;
  }
  
  // ADDRESS
  if (form.Address1.value == "") {
    alert( "Please enter your 'Street' under 'Contact Information'." );
    form.Address1.focus();
    
    // Prevent main window from following link.
    if(e.preventDefault) {
      e.preventDefault();
    } else {
      //     e.cancelBubble= true;
      e.returnValue = false;
    }
    return false;
  }
  
  // CITY STATE AND ZIP
  if (form.Address2.value == "") {
    alert( "Please enter your 'City, State and ZIP' under 'Contact Information'." );
    form.Address2.focus();
    
    // Prevent main window from following link.
    if(e.preventDefault) {
      e.preventDefault();
    } else {
      //     e.cancelBubble= true;
      e.returnValue = false;
    }
    return false;
  }
  
  // AREAS OF INTEREST
  //check for at least one selection of Area of Interest
  //var selectedProgram = false ;
			
  //		for(var i=0; i<=document.info.Program.length-1 && !selectedProgram; i++){
  //				if (document.info.Program[i].checked == true){	
  //				selectedProgram = true;	
  //			}
  //		}
  //		if(!selectedProgram){
  //			alert("Please, select your area of interest!");
  //			document.info.Program[0].focus();
  //			return false;
  //		}
 return true;
}


// Hook the initializer function to the document load.
addLoadEvent(wireFormHandler);