//-----------------------------------------------------------------------------------
//    MODULE : Form Validation JavaScript Code
//  FILENAME : _Script_Library/formValidate.js
//      PATH : /
//   VERSION : 1.0
//    AUTHOR : Andrew G. Brewton -- Technical Software Services, Inc.
//   PURPOSE : This module contains scripts which will be used to validate data
//             in the form fields for the D4L web site.
//   CREATED : January 18, 2000
//   Last Mod: Feb 12, 2001 cmbernard
//-----------------------------------------------------------------------------------

function checkForm(theForm) {
	
	var x;
	var element, returnValue, propertyflag;
	for (x=0; x < theForm.elements.length; x++) {
	     
		element = theForm.elements[x];
		switch (element.name) {
		
		  case "name"	:  
                                          returnValue = checkNull(element);
                                          if (returnValue == false) 
                                            return(false);
                                          
                                          break;

		  case "email"		: returnValue = checkNull(element);
                                          if (returnValue == false) 
                                            return(false);
                                          
                                          returnValue = checkEmail(element);
                                          if (returnValue == false) 
                                            return(false);
                                          
                                          break;

                		
	}
       
     }
   return(true);
}
        
//-----------------------------------------------------------------------------------		
function checkNull(field) {

	if (field.value == "") {
		msg = "No data was entered for the " + field.name + " field.";
		alert(msg);
                field.focus();
                field.select();
		return (false);
	}	
	else return (true);
}		
//------------------------------------------------------------------------------------		
function validateChar(OKChars, field) {
	
	for (x=0; x < field.value.length; x++) {
		ch = field.value.charAt(x);
		for (y=0; y < OKChars.length; y++) {	
			if (ch == OKChars.charAt(y)) 
				break;
			if (y == OKChars.length - 1) {
				return (false);
			}	
		}		
	}
	return (true);
}		
//-------------------------------------------------------------------------------------		
function isDigit(ch) {

	if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || 
	    ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9')	
 		return (true);
	else
		return (false);
}
//-------------------------------------------------------------------------------------		
function isHyphenDotSpace(ch) {

	if (ch == '-' || ch == '.' || ch == ' ')	
 		return (true);
	else
		return (false);
}
//-------------------------------------------------------------------------------------		
function checkEmail(field) {
	
	// Validate all characters in the email field
	//--------------------------------------------------
	
	OKChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@.";
	returnValue = validateChar(OKChars, field);
	if (returnValue == false) {
		msg = "Email address contains invalid characters";	
		alert(msg);
		return (false);
	}
	
	// Check for a '@' and a '.' in the email field 
	//--------------------------------------------------
	
	for (x=0; x < field.value.length; x++) {
		if (field.value.charAt(x) == '@') {
			for (y=x+1; y < field.value.length; y++)			
				if (field.value.charAt(y) == '.')
					return (true);
		}
	}	
	msg = "Email address needs to be in the form:  yourname@anywhere.com";
	alert(msg);
	field.focus();
	field.select();
	return (false);

}
//------------------------------------------------------------------------------------
function checkPhone(field) {
		
	// Check for properly formatted phone number 
	//--------------------------------------------------
	
	for (x=0; x <= 11; x++) {
          	ch = field.value.charAt(x);
		if (x == 3 || x == 7) {
			returnValue = isHyphenDotSpace (ch);
			if (returnValue == false) {
				phoneMessage ();
                                field.focus();
                                field.select();
				return (false);	
			}
		}
		else { 
			returnValue = isDigit (ch);
			if (returnValue == false) {
				phoneMessage ();
                                field.focus();
                                field.select();
				return (false);
			}
		}
	}		
   	return (true);
}
//-------------------------------------------------------------------------------------
function checkZip(field) {

	// Check for properly formatted phone number 
	//--------------------------------------------------
	
	if (field.value.length == 5 || field.value.length == 9) {
		for (x=0; x < field.value.length; x++) {		
			ch = field.value.charAt(x);
			returnValue = isDigit (ch);
			if (returnValue == false) {
				zipMessage ();
                                field.focus();
                                field.select();
				return (false);
			}	
		}
		return (true);		
	}

	if (field.value.length == 10) {
		for (x=0; x <= 9; x++) {
        	  	ch = field.value.charAt(x);
			if (x == 5) {
				returnValue = isHyphenDotSpace (ch);
				if (returnValue == false) {
					zipMessage ();
                                        field.focus();
                                        field.select();
					return (false);	
				}
			}              
			else { 
				returnValue = isDigit (ch);
				if (returnValue == false) {
					zipMessage ();
                                        field.focus();
                                        field.select();
					return (false);
				}
			}
		}		
	   	return (true);
	}
	else {
		zipMessage ();
                field.focus();
                field.select();
		return (false);
	}
}
//------------------------------------------------------------------------------------------
function zipMessage () {

	msg = "Zip code must be digits in the form:  XXXXX or XXXXX-XXXX";
	alert(msg);
	return (true);
}
//------------------------------------------------------------------------------------------
function phoneMessage () {

	msg = "Phone number must be in the form:  XXX-XXX-XXXX";
	alert(msg);
	return (true);
}