/***************************************
// Handles basic validation for a form
//*************************************/

$(document).ready(function() {	
 
	$('form').submit(function() {
		$('form .error').remove();
		var hasError = false;
		
		// REQUIRED TEXT 
		// useage: class="requiredText".  
		// This class should be placed on the input element to be validated.
		$('.requiredText').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				$(this).parent().append('<span class="error">This is required.</span>');
				hasError = true;
			}
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		
		// REQUIRED RADIO OR CHECKBOX 
		// useage: class="requiredRadio" 
		// This class should be placed on the parent element containing the radio buttons or check boxes to be validated.
		$('.requiredRadio').each(function(){ 
			//make sure at least one is selected 
			 if( ! $('input:checked', this).length > 0){
			  	$(this).append('<span class="error">This is required.</span>');
				hasError = true;
			} 
			$(this).one('click', function() {
				$(this).find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		}); 
		
		// VALIDATE EMAIL ADDRESSES 
		// useage: class="validateEmail"
		// This class should be placed on the input element
		$('.validateEmail').each(function() {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var address = $(this).val();
			   if(reg.test(address) == false) {
			      $(this).parent().append('<span class="error">The E-Mail address is invalid</span>');
				  hasError = true;
				}
				
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		
		
		
		// VALIDATE AGE
		// useage: class="validateAge"
		// This class should be placed on the input element
		$('.validateAge').each(function() {
			var reg = /^\d+$/;
			var age = jQuery.trim($(this).val())
			   if(reg.test(age) == false && age !='') {
			      $(this).parent().append('<span class="error">Age must be numeric</span>');
				  hasError = true;
				}
				
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		// VALIDATE Test Score
		// useage: class="validateScore"
		// This class should be placed on the input element
		$('.validateScore').each(function() {
										 
			var reg = /^\d+$/;
			var score = jQuery.trim($(this).val());
			   if(reg.test(score) == false && score !='') {
			      $(this).parent().append('<span class="error">Test scores must be numeric</span>');
				  hasError = true;
				}
				
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		//forces user to enter either all 3 SAT scores OR an ACT composite score
		$('#test_scores').each(function(){
			var SAT1 = jQuery.trim($('#sat_critical_reading').val());
			var SAT2 = jQuery.trim($('#sat_mathematics').val());
			var SAT3 = jQuery.trim($('#sat_writing').val());
			var ACT = jQuery.trim($('#act_composite').val());
			
			//if the ACT info is complete OR if all SAT info is complete
			if((SAT1 !='' && SAT2 !='' && SAT3 !='') || ACT !='')
			{
					//if ACT is complete
					if(ACT !='')
					{
						//if any of the SAT info is filled out then all must be filled out
						if((SAT1 !='' || SAT2 !='' || SAT3 !='') && !(SAT1 !='' && SAT2 !='' && SAT3 !=''))
						{
							
							$(this).parent().append('<span class="error">Please enter all SAT scores or ACT score</span>');
							hasError = true;
						}
					}
			}
			else
			{
				$(this).parent().append('<span class="error">Please enter all SAT scores or ACT score</span>');
				hasError = true;
			}
			
			
		}); 
		
		
		// VALIDATE Social Security Number 
		// useage: class="validateSSN".  
		// This class should be placed on the input item
		$('.validateSSN').each(function() {
				var ssn = jQuery.trim($(this).val());
				var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
				var numDashes =  ssn.split('-').length - 1;
				
				if (matchArr == null || numDashes == 1) {
					 $(this).parent().append('<span class="error">Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.</span>');
					 hasError = true;
				}
				else if (parseInt(matchArr[1],10)==0) {
					$(this).parent().append('<span class="error">Invalid SSN: SSNs cant start with 000.</span>');
					hasError = true;
				}
				
				$(this).one('blur', function() {
					$(this).parent().find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
				});
			
		});
		
		// VALIDATE Zip Code in both formats nnnnn or nnnnn-nnnn 
		// useage: class="validateZip" 
		// This class should be placed on the input item
		$('.validateZip').each(function() {
			var zipcode;
			zipcode = $('.validateZip').val();
		     // Check for correct zip code
		     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
		
		     if (!reZip.test(zipcode)) {
		        $(this).parent().append('<span class="error">Please enter a valid zip code.</span>');
				hasError = true;
		     }
		     $(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		// Checks to see if the passwords are equal and are at least 6 characters long
		// usage: class=".requiredPassword" 
		// This should be placed on the parent element of both password input fields.
		
		$('.requiredPassword').each(function() {
			var message = '';
			//MAKE SURE PASSWORDS ARE EQUAL
			var password = $('.requiredPassword #password').val();
			var password_verify =$('.requiredPassword #password_verify').val();
			if(password != password_verify) {
				message = 'The passwords do not match. ';
				hasError = true;
			}	
			if(password.length < 6) {
				message = 'The password must be at least 6 characters long.';
				hasError = true;
			}
			if(password.search(/[!@#$%^&*?_~]/) <= 0) {
				message = message + "The password must contain a special character, such as $, @ or !<br />";
				hasError = true;
			}			
			if(message != '') $('.requiredPassword #password_verify').parent().append('<span class="error">'+message+'</span>');
			$('.requiredPassword #password_verify').one('blur', function() {
					$('.requiredPassword #password_verify').parent().find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
			});
		});	
		
		// Validates First and Last name 
		// useage: class="requiredName"
		// This class should be placed on the parent element containing the first_name and last_name 
		$('.requiredName').each(function() {
			
			var message = '';
			if(jQuery.trim($('.requiredName #first_name').val()) == '' || jQuery.trim($('.requiredName #last_name').val()) == '') {
				
				message = 'First and Last names are required.';
				hasError = true;
			} else {
				if(jQuery.trim($('.requiredName #first_name').val()) == '') {
					message = 'First name is required.';
					hasError = true;
				}
				if(jQuery.trim($('.requiredName #last_name').val()) == '') {
					message = 'Last name is required.';
					hasError = true;
				}
			}
			if(message != '') $(this).append('<span class="error">'+message+'');
			$('.requiredName #last_name').one('blur', function() {
				$('.requiredName').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
			$('.requiredName #first_name').one('blur', function() {
				$('.requiredName').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		

		
		// REQUIRED DATE (useage: class="requiredDate".  
		// This class should be placed on the span that contains the date in the contact.html page 
		$('.requiredDate').each(function(index) {
			var day = '';
			var year = '';
			var month='';
			var message = '';
			if(index == 0) {
				day = jQuery.trim($('#address_applicable_from_day').val());
				year = jQuery.trim($('#address_applicable_from_year').val());
				month = jQuery.trim($('#address_applicable_from_month').val());				
			} 
			else {
				day = jQuery.trim($('#address_applicable_to_day').val());				
				year = jQuery.trim($('#address_applicable_to_year').val());
				month = jQuery.trim($('#address_applicable_to_month').val());								
			}
			
			if(day == '' || year == '' || month == '') {
				message = "Month, Day and Year are required.";
				hasError = true;
			}
			else {
				if((isNaN(day) || day < 1 || day > 31) && (isNaN(year) || year.length > 4 || year.length < 4)) 
					message = 'The day and year are invalid.';
				else if(isNaN(day) || day < 1 || day > 31) 
					message = 'The day is invalid.';
				else if(isNaN(year) || year.length > 4 || year.length < 4) 
					message = 'The year is invalid.';
			}
			
			if(message != '') $(this).append('<span class="error">'+message+'</span>');
			
			if(index == 0) {
				$('.requiredDate #address_applicable_from_day').one('blur', function() {
					$('.requiredDate').find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
				});
				$('.requiredDate #address_applicable_from_year').one('blur', function() {
					$('.requiredDate').find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
				});
			} 
			else {
				$('.requiredDate #address_applicable_to_day').one('blur', function() {
					$('.requiredDate').find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
				});
				$('.requiredDate #address_applicable_to_year').one('blur', function() {
					$('.requiredDate').find('span.error').fadeOut('normal', function() {
						$(this).remove();
					});
				});
			}
			
		});
		
		// REQUIRED DOB 
		// useage: class="requiredDOB".  
		// This class should be placed on the span that contains the date in the getting-started.html page
		$('.requiredDOB').each(function(index) {
			var day = '';
			var year = '';
			var month = '';
			var message = '';
	
			day = jQuery.trim($('#dob_day').val());
			year = jQuery.trim($('#dob_year').val());
			month = jQuery.trim($('#dob_month').val());
	
			
			if(day == '' || year == '' || month == '') {
				message = "Month, Day, and Year are required.";
				hasError = true;
			}
			else {
				if((isNaN(day) || day < 1 || day > 31) && (isNaN(year) || year.length > 4 || year.length < 4)) {
					message = 'The day and year are invalid.';
					hasError = true;
				}
				else if(isNaN(day) || day < 1 || day > 31) {
					message = 'The day is invalid.';
					hasError = true;
				}
				else if(year == '') {
					message = 'The year is invalid.';
					hasError = true;
				}
			}
			if(message != '') $(this).append('<span class="error">'+message+'</span>');
			$('.requiredDOB #dob_day').one('blur', function() {
				$('.requiredDOB').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
			$('.requiredDOB #dob_year').one('blur', function() {
				$('.requiredDOB').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		// REQUIRED PHONE Validates Home and Cell phone (insures that at least one is entered)
		// useage: class="requiredPhone"
		// Place this class on the parent element conaining the home and cell phone inputs
		$('.requiredPhone').each(function() {
			
			var message = '';
			if(!(jQuery.trim($('.requiredPhone #phone_home').val()) != '' || jQuery.trim($('.requiredPhone #phone_cell').val()) != '')) {
				
				message = 'At least one phone number is required.';
				hasError = true;
			} 
			
			if(message != '') $(this).append('<span class="error">'+message+'');
			$('.requiredPhone #phone_home').one('blur', function() {
				$('.requiredPhone').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
			$('.requiredPhone #phone_cell').one('blur', function() {
				$('.requiredPhone').find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		// REQUIRED SELECT 
		// useage: class="requiredSelect".  
		// This class should be placed on the select item
		$('.requiredSelect').each(function() {
			if(jQuery.trim($(this).val()) == $('option:first', this).val()) {
				$(this).parent().append('<span class="error">This is required.</span>');
				hasError = true;
			}
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});
		
		// VALIDATE YEAR
		// useage: class="validateYear".  
		// This class should be placed on the input item
		$('.validateYear').each(function() {
			var year = jQuery.trim($(this).val());
			if(year == '' || year.length < 4 || year.length > 4) {
				$(this).parent().append('<span class="error">Please enter a valid year.</span>');
				hasError = true;
			}
			$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
			
		});
		
		
		$(".notBlank",this).each(function(){
   var thisInput = $(this);
			if(jQuery.trim($(this).val()) == '') {
		     	$(this).parent().append('<span class="error">This is required.</span>');
			 hasError = true;
		   		}
				
				$(this).one('blur', function() {
				$(this).parent().find('span.error').fadeOut('normal', function() {
					$(this).remove();
				});
			});
		});	

   $("#honors-reference_1",this).each(function(){
										
		var homePhone = $(this).find("#honors_reference_1_homephone").val();	
		var workPhone = $(this).find("#honors_reference_1_workphone").val();
		
		if((homePhone == "") && (workPhone == ""))
		{
			$(this).append('<span class="error">Please enter a Work Phone Number or Home Phone Number for Reference #1</span>');
		hasError = true;
		}
	
	});	
   
    $("#honors-reference_2",this).each(function(){
								
		var homePhone = $(this).find("#honors_reference_2_homephone").val();	
		var workPhone = $(this).find("#honors_reference_2_workphone").val();
		
		if((homePhone == "") && (workPhone == ""))
		{
			$(this).append('<span class="error">Please enter a Work Phone Number or Home Phone Number for Reference #2</span>');
		hasError = true;
		}
	
	});	
	
	 $("#honors-reference_3",this).each(function(){
										
		var homePhone = $(this).find("#honors_reference_3_homephone").val();	
		var workPhone = $(this).find("#honors_reference_3_workphone").val();
		
		if((homePhone == "") && (workPhone == ""))
		{
			$(this).append('<span class="error">Please enter a Work Phone Number or Home Phone Number for Reference #3</span>');
		hasError = true;
		}
	
	});	
  	
		
		if(hasError) {
			return false;
		}
		
	});
	
});