/**
 * Validate Field [jQuery]
 *
 * Intelligently checks a form field to make sure either a selection has
 * been made or a value entered (as appropriate to the field type).
 *
 * Last modified: 2009-01-22 {pf}
 *
 */

function validateField(field) {
    
    switch ( field.attr('type') ) {
        
        case 'select':
        case 'select-one':
            if ( !field.val() ) {
                return false;
            }
            break;
        
        case 'checkbox':
        case 'radio':
            if ( field.hasClass('requiredGroup') ) {
                var groupChecked = false;
                var fieldset = $(field).parents('fieldset')[0];
                $('input.requiredGroup', fieldset).each ( function() {
                    if ( $(this).attr('checked') ) {
                        groupChecked = true;
                    }
                });
                return groupChecked;
            }
            else if ( !field.attr('checked') ) {
                return false;
            }
            break;
        
        case 'password':
            // not currently testing or comparing passwords in JS, so treated as...
        default: // text
            if ( field.hasClass('validateEmail') ) {
                var emailPattern = new RegExp(/^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
                if ( field.val().match(emailPattern) === null ) {
                    return false;
                }
            }
            else if ( !field.val() ) {
                return false;
            }
        
    }
    
    return true;
    
}


/**
 * Check Requireds [jQuery]
 *
 * Alerts the user to any 'required' inputs, in the target form, which
 * have not been completed.
 *
 * Last modified: 2009-01-22 {pf}
 */
function checkRequireds(thisForm) {
    
    var validated = false;
    
    //  Failure counter
    var failed = 0;
    
    //  NOTE: ':input' is jQuery shorthand for *all* form field types
    $(':input.required', thisForm).each( function () {
        
        var fieldValid = validateField($(this));
        
        if ( !fieldValid ) {
            
            failed++;
            
            //  Highlight error
            if ( $(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio' ) {
                $(this).parent().addClass('error');
            }
            else if ( !$(this).parent().hasClass('error') ) {
                $(this).wrap('<div class="error"></div>');
            }
            
        }
        else {
            
            //  Remove error highlight (if present)
            $(this).parent().removeClass('error');
            
        }
        
    });
    
    if (failed > 0) {
        
        if ( $('div.alert', thisForm).length < 1 ) {
            //  Requires 'reqWarn' HTML string (compiled in head of actual HTML document)
            thisForm.prepend(reqWarn);
        }
        
        //$(window).scrollTo($('div.requiredWarning:first', thisForm));
        
    }
    else if (failed == 0) {
        
        validated = true;
        
    }
    
    return validated;
    
}

/**
 * Find Requireds [jQuery]
 *
 * Automatically scans the page for forms, then adds an 'onsubmit'
 * function to any forms which contain input elements classed as
 * 'required'.
 *
 * Last modified: 2009-01-22 {pf}
 */

function findRequireds() {
    
    $('form').each( function() {
        var thisForm = $(this);
        if ( $(':input.required', thisForm) ) {
            $(thisForm).submit( function() {
                return checkRequireds(thisForm);
            });
        }
    });
	
}

// Bind form validation when DOM ready [uses jQuery]
$(document).ready( function() {
    if ( $(':input.required').length > 0 ) {
        findRequireds();
    }
});


/**
 *	Add Swap [jQuery]
 *	Rewrite of original 3Sixty image swap function, previously based on
 *	a redundant version of Prototype. Note that this *doesn't* use
 *	hover; it causes an error when the alert appears above the
 *	form and pushes the button away from the user's cursor.
 *	Last modified: 2009-03-13 {pf}
 */

function addSwap() {
	$("input.button").each( function(){
		$(this).mouseover( function() {
			$(this).parent().attr('class', 'buttonWrapperOn');
		});
		$(this).mouseout( function() {
			$(this).parent().attr('class', 'buttonWrapperOff');
		});
	});
}

$(document).ready( function() {
	addSwap();
});


/**
 *	Fade on load [jQuery]
 *	Simple "fade out" effect applied to any matching elements on page
 *	load.
 *	Last modified: 2009-02-06 {pf}
 */

function fadeOnLoad() {
	
	$(".fade").each( function(){
		$(this).animate({opacity:1.0},4250).fadeOut(650);
	});
	
}

$(document).ready( function() {
	fadeOnLoad();
});


