// validateform.js : 20/12/2007
var errorCount = 0;
var dependentField = null;		// if of form element upon which field validation dependencies are based
var dependentFieldValue = null;		// the value of the dependent form field to check for 
var depval = null;

function validateform() {
    $('#submit-message').remove();
    $(':input.required').blur();
    $(':input.dep-required').trigger('blur');
    var errors = $('.form-invalid', this).length;
    if (errors > 0){
        // show summary message
        $('#submitbutton', this).addClass('noedit');
        $('<span id="submit-message"></span>')
            .text('Please correct errors above before submitting the form')
            .addClass('errormsg')
            .insertAfter($('#submitbutton', this));					
        return false;
    }
    document.orderForm.submit();
}

$(document).ready(function() {

		// flag corres labels for required form fields

		$('FORM :input').filter('.required').prev('label').addClass('required').append(requiredFlag);
		// validation event handler
		$('form :input').blur(function() {
				if ($(this).is('.required')) {
					var label = "";
					$(this).next('span.form-invalid').remove();// remove any pre-existing messages
					
					// check for empty mandatory fields
					// alert('type='+this.type + ' val='+this.value);
					if ( ((this.type == "checkbox" || this.type == "radio") && ! this.checked) ||
						this.value == "") {
						var fld = $(this);
						$(this).addClass('errorfld');

						if (useLabelNames) { // shall we use the label names as part of error message ??
							label = ' for '+$(this).prev('label').text() 
						};
						
						if (this.type == "checkbox" || this.type == "radio") {
							var msg = errorMessage2;
						}
						else {
							var msg = errorMessage;
						}
						$('<span></span>')
							.text(msg+label)
							.addClass('form-invalid')
							.insertAfter(this);
						return;
					}else{
						$(this).removeClass('errorfld');
					}					
				} // end required check
				
				// check for fields which are required provided some dependant field is also present
				if ($(this).is('.dep-required')) {
					var label = "";
					var dependent_field_present = false;
					
					$(this).next('span.form-invalid').remove();// remove any pre-existing messages
					
					// check for empty mandatory fields
					if ( ((this.type == "checkbox" || this.type == "radio") && ! this.checked) ||
						this.value == "") {
							
						if (dependentField != null ) {
							depval = document.getElementById(dependentField).value;
							if (depval == dependentFieldValue) {
								dependent_field_present = true;
							}
						}	

						if (dependent_field_present) {	// is the field on which this form field is dependent also present/has value ??
							var fld = $(this);
							$(this).addClass('errorfld');
	
							if (useLabelNames) { // shall we use the label names as part of error message ??
								label = ' for '+$(this).prev('label').text() 
							};
							
							if (this.type == "checkbox" || this.type == "radio") {
								var msg = errorMessage2;
							}
							else {
								var msg = errorMessage;
							}
							$('<span></span>')
								.text(msg+label)
								.addClass('form-invalid')
								.insertAfter(this);
						}
						return;
					}else{
						$(this).removeClass('errorfld');
					}
					
				} // end dep-required check
				
				// check for invalid email addresses
				if ($(this).is('.email')) {
					if (!/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)) {	//empty?
						var fld = $(this);
						$(this).addClass('errorfld');
						$('<span></span>')
							.text('invalid email address')
							.addClass('form-invalid')
							.insertAfter(this);
					}else{
						$(this).removeClass('errorfld');
					}
				} // end email check
				
				// check for currency fields
				if ($(this).is('.currency')) {
					if (!/^[0-9]+\.?[0-9]{0,2}$/.test(this.value)) {	//empty?
						var fld = $(this);
						$(this).addClass('errorfld');
						$('<span></span>')
							.text('This is an invalid amount')
							.addClass('form-invalid')
							.insertAfter(this);
					}else{
						$(this).removeClass('errorfld');
					}
				} // end email check
		});
});
