$(document).ready(function(){
		$('#photos').galleryView({
			panel_width: 450,
			panel_height: 338,
			frame_width: 75,
			frame_height: 56,
			overlay_height: 40,
			nav_theme: 'light',
		    easing: 'easeInOutQuad',
		    pause_on_hover: true,
	        background_color: '#808080',
	        border: 'none',
	        show_captions: false
	                
		});
		
		$("#tabs").tabs();
		$("#tech-specs").click(function(){
			$("#tabs").tabs('select', 1); //switch to tab 2
			//return false; commented this out so that the browser moved to anchor.
		});
		$("a[rel^='lightbox-slideshow']").slimbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
		
		
		//BEGIN JQUERY FORM SUBMIT SETUP
		var options = { 
        target:        '#last-step',   // target element(s) to be updated with server response 
        beforeSubmit:  validate,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
        dataType:  		'json',
		url:			'/contactSeller/small-form/_validate.cfm'
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    	}; 
		$('div#last-step').hide();
	    $('#contact-form').submit(function() { 
	        // inside event callbacks 'this' is the DOM element so we first 
	        // wrap it in a jQuery object and then invoke ajaxSubmit 
	        $(this).ajaxSubmit(options); 
	 
	        // !!! Important !!! 
	        // always return false to prevent standard browser submit and page navigation 
	        return false; 
	    }); 
	    
	    // pre-submit callback 
		function validate(formData, jqForm, options) { 
		     // jqForm is a jQuery object which wraps the form DOM element 
		    // 
		    // To validate, we can access the DOM elements directly and return true 
		    // only if the values of both the username and password fields evaluate 
		    // to true 
		    $("div.error").remove();
		    $("form#contact-form input").removeClass("error");
		    $("form#contact-form #submit").hide();
		 	var hasError;
		 	//var error = "";
		 	var error = 0;
		    var form = jqForm[0]; 
		    if (!form.firstName.value) { 
		    	error++;
		        //error = error + "Please provide a First Name.<br/>";
		        $("input#firstName").addClass("error");
		        hasError = true;
		    }
		    if (!form.lastName.value) {
		    	error++;
		        //error = error + "Please provide a Last Name.<br/>";
		        $("input#lastName").addClass("error");
		        hasError = true;
		    }
		    if (!form.zipCode.value) { 
		    	error++;
		        //error = error + "Please provide a Zip Code.<br/>";
		         $("input#zipCode").addClass("error");
		        hasError = true;
		    }
		    if (!form.emailAddress.value || !isValidEmailAddress(form.emailAddress.value)) { 
		    	error++;
		       // error = error + "Please provide an Email Address.<br/>";
		        $("input#emailAddress").addClass("error");
		        hasError = true;
		    }
		    if (!form.recaptcha_response_field.value) {
		    	error++;
		    	$("input#recaptcha_response_field").addClass("error");
		       // error = error + "Captcha invalid.<br/>";
		        hasError = true;
		    }
		    //AT THE END DO WE HAVE AN ERROR?
		    
		    if(hasError == true){
		    	$("<div></div>").addClass("error").prependTo('div#first-step');
		    	$("div.error").html("Please fix the " + error + " error(s) marked below.");
		    	$("div.error").fadeIn(1000);
		    	$("form#contact-form #submit").show();
	    		return false;
		    }else{
		    	$("form#contact-form img.loading").show();
			}
		}
		
		function isValidEmailAddress(emailAddress) {
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(emailAddress);
		}
				 
		// post-submit callback 
		function showResponse(data, formData)  { 
		    	 
		    // if the ajaxSubmit method was passed an Options Object with the dataType 
		    // property set to 'json' then the first argument to the success callback 
		    // is the json data object returned by the server 
		 
		   // 'data' is the json object returned from the server 
   			
   			//alert("JSON DATA: " + " contactData: " + data.contactData + " captchaData: " + data.captchaData + " captchaCheck: " + data.captchaCheck + " personID: " + data.personID + " passDupeCheck: " + data.passDupeCheck);
			if(data.contactData == true && data.captchaData == true){
			
				if(data.passDupeCheck == false){
					// HERE IS WHERE YOU DO THE ERROR MESSAGE FOR DUPES (EMAIL ALREADY IN USE NOT MATCH First & Last)
					//alert("EMAIL ERROR");
					Recaptcha.reload();
					$("<div></div>").addClass("error").prependTo('div#first-step');
			    	$("div.error").html("The email address you entered has been found in our records, and we could not verify that it is yours based on the info you supplied.");
			    	$("div.error").fadeIn(1000);
			    	$("form#contact-form img.loading").hide();
			    	$("form#contact-form #submit").show();
		    		//return false;
		    	}else if (data.captchaCheck == false){
	   				//alert("INVALID CATPCHA");
					Recaptcha.reload();
					$("input#recaptcha_response_field").addClass("error");
					$("<div></div>").addClass("error").prependTo('div#first-step');
			    	$("div.error").html("The Response is reported as wrong. If you are having trouble use the icons on the right to receive a new image.");
			    	$("div.error").fadeIn(1000);
			    	$("form#contact-form img.loading").hide();
			    	$("form#contact-form #submit").show();
		    		//return false;
	   			}else if ((data.personID == false || isNaN(data.personID) == false) && data.captchaCheck == true && data.passDupeCheck == true){
	   				$("div#first-step").hide();
	   				$("div#last-step").show();
					$("<div></div>").addClass("success").prependTo('div#last-step');
					responsestring = "<p>Success! The seller will be contacted.</p><img border='0' src='https://linkconnector.com/tm.php?lc=000000002250&oid="+$("form#contact-form #emailAddress").val()+"&amt='>";
	   				$("div#last-step div.success").html(responsestring);
					$("div.error").fadeIn(1000);
					$('#contact-form input#personID').val(data.personID);
					$('#contact-form input#bypass').val(data.bypassCheck);
	   				//$("div#last-step").html("JSON DATA: " + " contactData: " + data.contactData + " captchaData: " + data.captchaData + " captchaCheck: " + data.captchaCheck + " personID: " + data.personID + " passDupeCheck: " + data.passDupeCheck);
	   				var queryString = $('#contact-form').formSerialize();
		   			$.post('/contactSeller/small-form/_submit.cfm', queryString); 
					//return true;
	   			} else {
				
					Recaptcha.reload();
					$("<div></div>").addClass("error").prependTo('div#first-step');
			    	$("div.error").html("An unknown error has occured. Please try again in a few minutes.");
			    	$("div.error").fadeIn(1000);
			    	$("form#contact-form img.loading").hide();
			    	$("form#contact-form #submit").show();
		    		//return false;
				}
				
   			} else {
					Recaptcha.reload();
					$("<div></div>").addClass("error").prependTo('div#first-step');
			    	$("div.error").html("An unknown error has occured. Please try again in a few minutes.");
			    	$("div.error").fadeIn(1000);
			    	$("form#contact-form img.loading").hide();
			    	$("form#contact-form #submit").show();
		    		//return false;
			}
   			 
		}
		$("#loan-button").click(function(){
			var L,P,n,c,dp;
			L = parseInt($("#principal").val());
			n = parseInt($("#years").val()) * 12;
			c = parseFloat($("#rate").val())/1200;
			dp = parseInt($("#down").val());
			L = L - dp;
			P = (L*(c*Math.pow(1+c,n)))/(Math.pow(1+c,n)-1);
			if(!isNaN(P))
			{
				$("#payment").val(P.toFixed(2));
			}
			else
			{
				$("#payment").val('There was an error');
			}
			return false;
		}); 
//		jQuery.validator.messages.required = "";
//		$("#contact-form").validate({
//			rules: {
//				firstName: {required: true},
//				lastName: {required: true},
//				zipCode: {required: true},
//				emailAddress: {
//					required: true,
//					email: true,
//					remote: {
//						url: "/findrv/_contact_info_validate_jquery.cfm",
//						type: "post",
//						data: {
//							firstName: function() {
//	            			return $("#firstName").val();
//	            			},
//	            			lastName: function() {
//	            			return $("#lastName").val();
//	            			},
//	            			zipCode: function() {
//	            			return $("#zipCode").val();
//	            			}
//						}
//					}
//				},
//				recaptcha_response_field: {
//					required: true,
//					remote: {
//						url: "/findrv/_contact_info_validate_jquery.cfm",
//						type: "post",
//						data: {
//							recaptcha_challenge_field: function() {
//	            			return $("#recaptcha_challenge_field").val();
//	            			}
//	            		}
//					}
//				}
//			},
//		messages: {
//			//firstName: "This field is required",
//			//lastName: "This field is required",
//			emailAddress: "Please enter a valid email",
//			//zipCode: "This field is required",
//			recaptcha_response_field: "Verification Code was incorrect"
//			},
//		invalidHandler: function(e, validator) {
//			var errors = validator.numberOfInvalids();
//			if (errors) {
//				var message = errors == 1
//					? 'There is an error with 1 field.'
//					: 'There are errors with ' + errors + ' fields.';
//				$("div.error span").html(message);
//				$("div.error").show();
//			} else {
//				$("div.error").hide();
//			}
//		},
//		success: function() {
//			// USE THIS IF YOU WANT A CUSTOM MESSAGE OF SUCCESS (FOR EACH)
//			$("label.error").hide();
//			$("div.error").hide();
//		},
//		submitHandler: function() {
//			$("div#first-step").hide();
//			$("div#last-step").show();
//		},
//		onkeyup: false
//				
//	});

}); // END DOCUMENT READY
