/*
var goodFname = false;
var goodLname = false;
var goodEmail = false;
var goodPhoneArea = false;
var goodPhonePre = false;
var goodPhoneSuff = false;
var goodPhone = false;

function isNumeric(form_value) { 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
}
*/

/*
function contactSubmit() {
	$('#contactShowroom-errorMsg').css('display','none');
	if(goodFname == false) {
		$('.isrequiredFname').css('border-color','#CE4343');
		$('.isrequiredFname').parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
		$('.isrequiredFname').parent().find('span#fNameErrorMsg').html('Please provide your first name.');	
	} else if(goodLname == false) {
		$('.isrequiredLname').css('border-color','#CE4343');
		$('.isrequiredLname').parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
		$('.isrequiredLname').parent().find('span#lNameErrorMsg').html('Please provide your last name.');
	} else if (goodEmail == false) {
		$('.isrequiredEmail').css('border-color','#CE4343');
		$('.isrequiredEmail').parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
		$('.isrequiredEmail').parent().find('span#emailErrorMsg').html('Please check. A valid email address is required.');		
	} else if (goodPhone == false) {
		$('#phoneNumberInputs input').css('border-color','#CE4343');
		$('#phoneNumberInputs').find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
		$('#phoneNumberInputs').find('span#phoneErrorMsg').html('Please check. This looks like an invalid phone number.');
	} else {
		//Submit Form
	}
}
*/

(function($) {
    $(document).ready(function() {
        $('#contactForm').attachAjaxValidator({
            url : "/contactFormValidation.jsp"
        });

		$('input.isrequiredFname').bind("validationSuccess", function() {
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
            $(this).css('border-color','#CCCCCC');
            $(this).parent().find('span#fNameErrorMsg').html('&nbsp;');
		});
		$('input.isrequiredFname').bind("validationFailure validationReset", function(event, fieldErrors, globalErrors, fieldErrorMap) {
            $(this).css('border-color','#CE4343');
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
            if (!fieldErrors || fieldErrors[0] == "empty") {
                $(this).parent().find('span#fNameErrorMsg').html('Please provide your first name.');
            } 
		});

		$('input.isrequiredLname').bind("validationSuccess", function() {
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
            $(this).css('border-color','#CCCCCC');
            $(this).parent().find('span#lNameErrorMsg').html('&nbsp;');
		});
		$('input.isrequiredLname').bind("validationFailure validationReset", function(event, fieldErrors, globalErrors, fieldErrorMap) {
            $(this).css('border-color','#CE4343');
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
            if (!fieldErrors || fieldErrors[0] == "empty") {
                $(this).parent().find('span#lNameErrorMsg').html('Please provide your last name.');
            } else if (fieldErrors[0] == 'invalid'){
                $(this).parent().find('span#lNameErrorMsg').html('Please provide your last name.');
            }
		});

		$('input.isrequiredEmail').bind("validationSuccess", function() {
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
            $(this).css('border-color','#CCCCCC');
            $(this).parent().find('span#emailErrorMsg').html('&nbsp;');
		});
		$('input.isrequiredEmail').bind("validationFailure validationReset", function(event, fieldErrors, globalErrors, fieldErrorMap) {
            $(this).css('border-color','#CE4343');
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
            if (!fieldErrors || fieldErrors[0] == "empty") {
                $(this).parent().find('span#emailErrorMsg').html('Please provide your email address.');
            } else if (fieldErrors[0] == "invalid") {
                $(this).parent().find('span#emailErrorMsg').html('Please use an e-mail with an @ symbol &amp; at least 1 dot.');
            }
		});

        var focusedElem = null;
        $(":text, :password, select, :checkbox, :radio, textarea").focus(function () {
            focusedElem = this;
        }).blur(function() {
            if (this == focusedElem) {
                focusedElem = null;
            }
        });

        
		$('input.phone').bind("validationSuccess", function() {
            $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
            $('input.phone').css('border-color','#CCCCCC');
            $(this).parent().find('span#phoneErrorMsg').html('&nbsp;');
		});
		$('input.phone').bind("validationFailure validationReset", function(event, fieldErrors, globalErrors, fieldErrorMap) {
            if (!$(focusedElem).is("input.phone")) {
                $('input.phone').css('border-color','#CE4343');
                $(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
                if (!fieldErrors || fieldErrors[0] == "empty") {
                    $(this).parent().find('span#phoneErrorMsg').html('Please provide your phone number.');
                } else if (fieldErrors[0] == "invalid") {
                    $(this).parent().find('span#phoneErrorMsg').html('Please check. This looks like an invalid phone number.');
                }
            }
		});


        /*
        $("#contactForm").submit(function() {
            $(this).doValidation();
            return false;
        });

        $("#contactForm").bind("validationSuccess", function(event) {
            $(this).submit();
        });
        */

		
        /*
		$('input.phoneArea').blur(function() {
			if($(this).attr("value") == null || $(this).attr("value") == '') {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please provide your phone number.');
				goodPhone = false;
			} else if ($(this).attr("value").length != 3 || !isNumeric($(this).attr("value"))) {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please check. This looks like an invalid phone number.');
				goodPhone = false;
			} else {
				$(this).css('border-color','#CCCCCC');
				goodPhoneArea = true;
				if(goodPhoneArea==true && goodPhonePre==true && goodPhoneSuff==true) {
					goodPhone=true;
					$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
					$(this).parent().find('span#phoneErrorMsg').html('&nbsp;');
				}
			}
		});
		
		$('input.phonePre').blur(function() {
			if($(this).attr("value") == null || $(this).attr("value") == '') {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please provide your phone number.');
				goodPhone = false;
			} else if ($(this).attr("value").length != 3 || !isNumeric($(this).attr("value"))) {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please check. This looks like an invalid phone number.');
				goodPhone = false;			
			} else {
				$(this).css('border-color','#CCCCCC');
				goodPhonePre = true;
				if(goodPhoneArea==true && goodPhonePre==true && goodPhoneSuff==true) {
					goodPhone=true;
					$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
					$(this).parent().find('span#phoneErrorMsg').html('&nbsp;');
				}
			}
		});
		
		$('input.phoneSuff').blur(function() {
			if($(this).attr("value") == null || $(this).attr("value") == '') {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please provide your phone number.');
				goodPhone = false;
			} else if ($(this).attr("value").length != 4 || !isNumeric($(this).attr("value"))) {
				$(this).css('border-color','#CE4343');
				$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldBad.gif");
				$(this).parent().find('span#phoneErrorMsg').html('Please check. This looks like an invalid phone number.');
				goodPhone = false;				
			} else {
				$(this).css('border-color','#CCCCCC');
				goodPhoneSuff = true;
				if(goodPhoneArea==true && goodPhonePre==true && goodPhoneSuff==true) {
					goodPhone=true;
					$(this).parent().find('img.validatedIcon').attr("src","images/icon-contactFormFieldGood.gif");
					$(this).parent().find('span#phoneErrorMsg').html('&nbsp;');
				}
			}
		});
        */
		
	});
})(jQuery);
