// This script contains validation functions for the online RI/RA Request forms.  
// Validation functions below
function validateFormOnSubmit(theForm) {
  var reason = "";

	with(theForm) {
    // Dealer RA form validation
		if( id == "raform" ) {
	    reason = isEmpty( contact_name, "A contact name is required.<br />" );
		  reason += isEmail( contact_email, "A valid contact email address is required.<br />" );
		  reason += validatePhone( contact_phone );
		  reason += validateNum( contact_ext, "Extension number must be numbers only.<br />" );
		  reason += isEmpty( dealer_name, "Dealer name is required.<br />" ); 
			reason += isEmpty( dealer_code, "Dealer code is required. If you do not know your dealer code, enter UNKNOWN.<br />" );
	    reason += isEmpty( address, "Address is required.<br />" );
		  reason += isEmpty( city, "City is required.<br />" );
		  reason += isEmpty( state, "State is required.<br />" );
		  reason += isZipcode( zip ); 
		  reason += validatePhone( phone );
		  reason += isEmpty( model, "A model number is required.<br />" );
		  reason += isEmpty( serial, "A serial number is required.<br />" );
		  reason += isEmpty( problem, "A problem description is required.<br />" );
		  reason += isEmptyRadio( warranty, "Warranty status must be specified.<br />" );
			
		  if(return_type.value == "credit") {
			  reason += isEmpty( credit_reason, "A reason must be specified for credit only requests.<br />" );
		  }
		
		  if( unit_status.value == "customer" ) {
		    reason += isEmpty( customer_name, "A customer name is required.<br />" );
			  if( warranty[0].checked ) {
			    reason += isEmpty( customer_DOP, "A date of purchase is required.<br />" );
				  reason += isValidDate( customer_DOP );
  				if( pop[0].checked ){
	  			  reason += isEmpty( fileupload, "A receipt must be submitted with this request.<br />" );
				  }
			  }
		  }
	    
			if( warranty[1].checked ) {
	      reason += isEmptyCheckbox( preapprove, "Non warranty repairs must be preapproved.<br />" );
	    }
	  }		
    // Customer RA form validation
		else if( id == "raform-customer" ) {
	    reason = isEmpty( contact_name, "A contact name is required.<br />" );
		  reason += isEmail( contact_email, "A valid contact email address is required.<br />" );
		  reason += validatePhone( contact_phone );
		  reason += validateNum( contact_ext, "Extension number must be numbers only.<br />" ); 
	    reason += isEmpty( address, "Address is required.<br />" );
		  reason += isEmpty( city, "City is required.<br />" );
		  reason += isEmpty( state, "State is required.<br />" );
		  reason += isZipcode( zip ); 
		  reason += isEmpty( model, "A model number is required.<br />" );
		  reason += isEmpty( serial, "A serial number is required.<br />" );
		  reason += isEmpty( problem, "A problem description is required.<br />" );
		  reason += isEmptyRadio( warranty, "Warranty status must be specified.<br />" );
	  
		  if( warranty[0].checked ) {
		    reason += isEmpty( dealer, "Dealer name is required.<br />" );
		    reason += isEmpty( dealer_state, "Dealer state is required.<br />" );
			  reason += isEmpty( customer_DOP, "A date of purchase is required.<br />" );
  		  reason += isValidDate( customer_DOP );
  			if( pop[0].checked ){
	 			  reason += isEmpty( fileupload, "A receipt must be submitted with this request.<br />" );
			  }
		  }
			else if( warranty[1].checked ) {
	      reason += isEmptyCheckbox( preapprove, "Non warranty repairs must be preapproved.<br />" );
	    }
	  }			
	}
	
  if (reason != "") {
	  reason = "<h3>Form Not Submitted</h3>\n<p>The following errors were found:</p>" + reason;
		updateHTML( reason, 'errors' );
   	var errorSlide = new Fx.Slide('errors').hide();
		document.getElementById('errors').style.display = '';
		errorSlide.slideIn();
		return false;
	}
	
	// Clear any previous validation errors
	document.getElementById('errors').style.display = 'none';
	return true;
}
