/* 
Author: Kevin Dolby
Name: train_reg2.js
Purpose: This is to check the form 'train_reg_form' for HR
Date: 4 / 20 / 04'
*/



/*---------------------------------function err--------------------
     performs closing duties for a found error
     focuses of input box and selects invalid data  
     and returns false to cancel form submission
-------------------------------------------------------------------*/

function err (x)
{
   var y = eval("document.training_reg_form." + x);
   y.focus();
   if((y.type != 'select-one')&&(y.type != 'select-multiple')) y.select();
   return false;
}


/*--------------------- function phonecheck-------------------------
checks for a valid area code, exchange and extension of a phone number
                  returns false for invalid data
--------------------------------------------------------------------*/

function phonecheck (a,b,c)
{
   var areacode = eval("document.training_reg_form." + a)
   var exchange = eval("document.training_reg_form." + b)
   var extension = eval("document.training_reg_form." + c)
   var goodarea = areacode.value.match(/\d\d\d/)
   var goodexch = exchange.value.match(/\d\d\d/)
   var goodextn = extension.value.match(/\d\d\d\d/)

   if(!goodarea)
      {
        alert('Please enter a valid area code.');
        return err(areacode.name);
      }
   if(!goodexch)
      {
        alert('Please enter a valid telephone exchange.');
        return err(exchange.name); 
      }   
   if(!goodextn)
      {
        alert('Please enter a valid phone extension.');
        return err(extension.name);
      }
	  return true;
}

/*-------------------function emailCheck------------------
       returns true for a valid e-mail address
          x is the name of the e-mail field
 goodEmail is undefined if there is not a valid string match
 also checks for maxlength of 60 chars
-----------------------------------------------------------*/
function emailCheck(x)
{
   var email = eval("document.training_reg_form." + x);  
   if(email.value.length > 60)
      {
	     alert('Your Email address must be 60 characters or less. Please enter a shorter Email.')
		 return err(email.name);	   
	  }
   //var goodEmail = email.value.match(/\b(^(\S+@)\S+((\.com)|(\.net)|(\.int)|(\.edu)|(\.mil)|(\.gov)|(\.info)|(\.biz)|(\.org)|(\..{2,2}))$)\b/gi);
   var goodEmail = email.value.match(/^([^\s@])+@{1}[^\s@]+(((\.com)|(\.net)|(\.int)|(\.edu)|(\.mil)|(\.gov)|(\.info)|(\.biz)|(\.org)|(\..{2,2}))$)/gi);
   if(!goodEmail)
      {
         alert('This email address does not appear to be valid. Please enter a valid e-mail address.');
         return err(email.name);
      }
   else
      return true;
}





/*-------------------------- check function -------------------------
       checks each field for correct input
   returns false (from err) to cancel submission
   kd 3-1-04.. 
-----------------------------------------------------------------------*/
function check(f)
 {
 
 
 /* --------------------------- Dept Checker ---------------------------------
	------- checks to make sure they selected a dept from ONE pull down -------		
   -----------------------------------------------------------------*/
	if(f.DEPT1.options[0].selected == true && f.DEPT2.options[0].selected == true &&
	   f.DEPT3.options[0].selected == true && f.DEPT4.options[0].selected == true &&
	   f.DEPT5.options[0].selected == true && f.DEPT6.options[0].selected == true && 
	   f.DEPT7.options[0].selected == true && !f.DEPT8.checked)
	     {
		    alert("Please choose ONE Department from your correct pull down menu.");
            return err(f.DEPT1.name);
	     }

/*--------------------------------------- First and Last Names -------------------------
   checks First_Name field for correct input
   returns false (from err) to cancel submission
--------------------------------------------------------------------------------------*/				
	if(f.FIRST_NAME.value == "")
         {
            alert('Be sure to enter your first name.');
            return err(f.FIRST_NAME.name);
         }
		 
	if (f.FIRST_NAME.value.length > 20)
		{
			alert(' Your first name is too long. \n\n  It should only contain 20 characters, you have entered ' +f.FIRST_NAME.value.length+ ' characters.');
			return err(f.FIRST_NAME.name);
		}
		
	//the following capitalizes the first letter of the FIRST_NAME	 
	f.FIRST_NAME.value = f.FIRST_NAME.value.charAt(0).toUpperCase() + f.FIRST_NAME.value.substring(1, f.FIRST_NAME.value.length);			
	
	
	
	if(f.LAST_NAME.value == "")
         {
            alert('Be sure to enter your last name.');
            return err(f.LAST_NAME.name);
         }
	
	if (f.LAST_NAME.value.length > 20)
		{
			alert(' Your last name is too long. \n\n  It should only contain 20 characters, you have entered ' +f.LAST_NAME.value.length+ ' characters.');
			return err(f.LAST_NAME.name);
		}
		 
	//the following capitalizes the first letter of the LNAME	 
	f.LAST_NAME.value = f.LAST_NAME.value.charAt(0).toUpperCase() + f.LAST_NAME.value.substring(1, f.LAST_NAME.value.length);		 

		 
	
		 
	/*---------------------------------- function phonecheck-------------------------
checks for a valid area code, exchange and extension of a phone number
                  returns false for invalid data
--------------------------------------------------------------------------------*/
    if(f.AREACODE.value + f.EXCHANGE.value + f.EXTENSION.value == "")
         {
            alert('Please enter a valid phone number');
            return err(f.AREACODE.name);
         }

    if(f.AREACODE.value + f.EXCHANGE.value + f.EXTENSION.value != "")
         {
             if(!phonecheck(f.AREACODE.name, f.EXCHANGE.name, f.EXTENSION.name))
	         {
         	     return false;
		     }
         }

		  
/*--------------------------------- email check ----------------------------------------
		if(f.EMAIL.value == "" && f.SUPER_EMAIL.value == "")
	{
	        alert('Please enter an Email Address');
            return err(f.EMAIL.name);
	}   
	THIS WAS OLD WAY IN WHICH WE ONLY REQUIRED THE REGISTRANTS EMAIL AND NOT THE SUPERS
	  CHANGED PER REQUEST OF SCOTT ON AUG. 22, 2005
----------------------------------------------------------------------------------*/	
	if(f.EMAIL.value == "")
	{
	        alert('Please enter your Email Address.');
            return err(f.EMAIL.name);
	}	
	
	if (f.EMAIL.value != "")
		if(!emailCheck(f.EMAIL.name))
			return false;
			
				
	if(f.SUPER_EMAIL.value == "")
	{
	        alert('Please enter your Supervisor\'s Email Address. ');
            return err(f.SUPER_EMAIL.name);
	}	
			
	if (f.SUPER_EMAIL.value != "")
		if(!emailCheck(f.SUPER_EMAIL.name))
			return false;
	
	
 

/* --------------------------- Course Checker ---------------------------------
------- checks to make sure they selected a course from pull down -------		
------------------------------------------------------------------------*/ 
	if(f.COURSE.options[0].selected == true)
	     {
		    alert("Please choose a Course");
            return err(f.COURSE.name);
	     }
	
	


		
		
return true;}  //returns true when all form fields have passed check, which completes submit
