// Title: 	Scheduling Request Form Validation (JavaScript)
// File:	validate.js
// Author:	Eric Paul (epaul@cecs.pdx.edu)

//
// Form Page 1 validation
//
function checkForm_p1( form ){

    error_list = "";	// error container
	
	// check FIRST_NAME field
    if( isEmpty( form.FIRST_NAME ) ){
    	error_list = 'First Name\n'; 	    
    }
	
	// check LAST_NAME field
    if( isEmpty( form.LAST_NAME ) ){
    	error_list = 'Last Name\n'; 	    
    }
	
	// check EVENT_NAME field
	if( isEmpty( form.EVENT_NAME ) ){
        error_list += 'Event Name\n';        
    }
	
	// check ATT field (expected attendance)
	if (isNaN(parseInt(form.ATT.value))) {
		error_list += 'Expected Attendance (numbers only)\n';
	}
	
	// check ORG field
	if( isEmpty( form.ORG ) ){
        error_list += 'Organization\n';        
    }
	
	// check ADD field
	if( isEmpty( form.ADD ) ){
        error_list += 'Address\n';        
    }
	
	// check CITY field
	if( isEmpty( form.CITY ) ){
        error_list += 'City & State\n';        
    }
	
	// check ZIP field
	if( !ValidZIP( form.ZIP ) ){
    	error_list += 'Valid ZIP code (5 or 9 digits, dahses OK)\n'; 	    
    }
	
	// check TEL field
	if( !ValidatePhone( form.TEL ) ){
        error_list += 'Valid Telephone Number (10 digits, separators OK)\n';
    }
	
	// check EMAIL
    if( notValidEmail( form.EMAIL ) ){
		error_list += 'Valid email (example: foo@server.net)\n';
    }
	
	if (error_list == "") {
		return true;
	} else {
		alert('The following fields are required:\n\n' + error_list);
		return false;
	}

}


//
// Form page 2 validation
//
function checkForm_p2( form ){
	
	error_list = "";
	
	// check Preferred Start Date
    if( isEmpty( form.START_DATE_01 ) ){
    	error_list += 'Preferred Start Date\n'; 	    
    }
	
	// check Preferred Start Time
	if( !ValidTime( form.START_TIME_01 ) ){
    	error_list += 'Valid Preferred Start Time (example: 5:00)\n'; 	    
    }
	
	// check Preferred End Date
    if( isEmpty( form.END_DATE_01 ) ){
    	error_list += 'Preferred End Date\n'; 	    
    }
	
	// check Prefrered End Time
	if( !ValidTime( form.END_TIME_01 ) ){
    	error_list += 'Valid Preferred End Time (example: 7:00)\n'; 	    
    }
	
	/* check Preferred Room Selection
	if ( form.ROOM_01.selectedIndex == 0 ) {
        error_list += 'Preferred Room Selection\n';
    }
	*/
	 
	if (error_list == "") {
		return true;
	} else {
		alert('The following fields are required:\n\n' + error_list);
		return false;
	}
}

//
// Form page 3
//
function checkForm_p3( form ){

	error_list = "";
	
	// check that pre-event duration is empty or a number
	if (! isEmpty(form.PRE_TIME)) {
		if ( (isNaN(parseFloat(form.PRE_TIME.value)))) {
			error_list += 'Pre-Event Duration must be left blank or numeric (example: 1.5)\n';
		}
	}

	// check that post-event duration is empty or a number
	if (! isEmpty(form.POST_TIME)) {
		if ( (isNaN(parseFloat(form.POST_TIME.value)))) {
			error_list += 'Post-Event Duration must be left blank or numeric (example: 1.5)\n';
		}
	}
	
	if (error_list == "") {
		return true;
	} else {
		alert('Input Error(s):\n\n' + error_list);
		return false;
	}
}


//
// Form page 4 validation
//

function checkForm_p4( form ){

	error_list = "";
	
	// check certifications
	if( notChecked( form.CERT_1 ) ){
        error_list = 'All Five Certification check boxes\n';
    }

	if( notChecked( form.CERT_2 ) ){
        error_list = 'All Five Certification check boxes\n';
    }
	
	if( notChecked( form.CERT_3 ) ){
        error_list = 'All Five Certification check boxes\n';
    }
	
	if( notChecked( form.CERT_4 ) ){
        error_list = 'All Five Certification check boxes\n';
    }
	
	if( notChecked( form.CERT_5 ) ){
        error_list = 'All Five Certification check boxes\n';
    }
	
	// check university affiliation
	if( form.UNIV_STATUS.selectedIndex == 0 ){	// no selection made
		error_list += 'Your University Status\n';
	}
	if (error_list == "") {
		return true;
	} else {
		alert('The following fields are required:\n\n' + error_list);
		return false;
	}
}

//
// Form page 5 validation
//

function checkForm_p5( form ){

	error_list = "";

	// check University Index Number
    if( isEmpty( form.UIN ) ){
    	error_list += 'University Index Number\n'; 	    
    }
	
	// check catering choice
	if ( ( form.FOOD[0].checked ) && (form.FOOD_SRC.selectedIndex == 0 )) {
        error_list += 'Catering Choice\n';
    }
	
	if (error_list == "") {
		return true;
	} else {
		alert('The following fields are required:\n\n' + error_list);
		return false;
	}
}

//
// HELPER FUNCITON DEFINITIONS
//
function isEmpty( str ){			// throws error on empty field
    strRE = new RegExp( );
    strRE.compile( '^[\s ]*$', 'gi' );
    return strRE.test( str.value );
}


function notValidEmail( str ){	// throws error on INVALID email: not 'foo@server.com'
    mailRE = new RegExp( );
    mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
    return !(mailRE.test( str.value ));
}

function notChecked( box ){		// throws error on no certification of vehicle registration
    if( box.checked ){
        return false;
    }
    else{
        return true;
    }
}


function ValidatePhone( str ) {
	
	if (isEmpty(str)) {
		return false;	// fails on empty field
	
	} else {	// validate entered data
	
		//strip out acceptable non-numeric characters
		var stripped = str.value.replace(/[\(\)\.\-\ ]/g, '');

		if (isNaN(parseInt(stripped))) {
			return false;	// fails on illegal characters in phone #
			
		} else if (!(stripped.length == 10)) { 
			return false;	// fails on length
	
		} else {
			return true;	// right length and character content (all digits)
		}
	}
}

function ValidZIP( str ) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test( str.value ));
}
function ValidTime( str ) {
	
	if (isEmpty(str)) {
		return false;	// fails on empty field
	
	} else {	// validate entered data
	
		//strip out acceptable non-numeric characters
		var stripped = str.value.replace(/:/g, '');

		if (isNaN(parseInt(stripped))) {
			return false;	// fails on illegal characters in time
	
		} else {
			return true;	// right length and character content (all digits)
		}
	}
}
