
String.prototype.trim = function () {    
    var    str = this.replace(/^\s\s*/, ''),
    ws = /\s/,
    i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}
		
jQuery(function($){
$("#birthdate").mask("99/99/9999",{placeholder:" "});
$("#zip").mask("99999",{placeholder:" "});
$("#state").mask("aa",{placeholder:" "});
$("#phone1").mask("(999) 999-9999",{placeholder:" "});
$("#phone2").mask("(999) 999-9999",{placeholder:" "});
$("#employedPast").mask("99/9999 - 99/9999",{placeholder:" "});
$("#driverstate").mask("aa",{placeholder:" "});
$("#regstate").mask("aa",{placeholder:" "});
$("#autoliaexp").mask("99/9999",{placeholder:" "});
$("#omissionliaexp").mask("99/9999",{placeholder:" "});
});

$(document).ready(function() {

			var features = {"btn1":$('.feat1'), "btn2":$('.feat2'), "btn3":$('.feat3'), "btn4":$('.feat4'), "btn5":$('.feat5')};
			var buttons = [$('.btn1'), $('.btn2'), $('.btn3'), $('.btn4'), $('.btn5')];

			var selectedButton = buttons[0];
			
			$(buttons).each(function() {
				$(this).click(function() {
					var self = this;
					
					// fade out
					selectedButton.parent().removeClass('selected');
					$('.selectedFeat').fadeOut(400, function() {
						$(this).removeClass('selectedFeat').addClass('feat');
						
						// fade in
						features[$(self).attr('class')].fadeIn(400, function() {
							$(this).removeClass('feat').addClass('selectedFeat');
							$(self).parent().addClass('selected');
							selectedButton = $(self);
						});
					});

				});
			});
			
	//form validation
			
			$('.save_continue').click(function(){
				$(window).scrollTo( '#logo', 300 );
			});
					
			$('label.required').append(' <strong>*</strong> ');
			$('#sf1').hide();
			$('#sf2').hide();
			$('#sf3').hide();
			$('.indicate0').hide();
			$('.indicate1').hide();
			$('.indicate2').hide();
			$('.indicate3').hide();
			$('.indicate4').hide();
			$('.indicate5').hide();
			$('.indicate6').hide();
			$('.indicate7').hide();
			$('.indicate8').hide();
			
			$(":radio[name='employedPast']").click(function(){
			  var newVal = $(":radio[name='employedPast']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate0").fadeIn(400);
			  } else {
			    $(".indicate0").fadeOut(400);
			  }
			});


			$(":radio[name='revoked']").click(function(){
			  var newVal = $(":radio[name='revoked']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate1").fadeIn(400);
			  } else {
			    $(".indicate1").fadeOut(400);
			  }
			});

			$(":radio[name='convicted']").click(function(){
			  var newVal = $(":radio[name='convicted']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate2").fadeIn(400);
			  } else {
			    $(".indicate2").fadeOut(400);
			  }
			});
			
			$(":radio[name='alleged']").click(function(){
			  var newVal = $(":radio[name='alleged']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate3").fadeIn(400);
			  } else {
			    $(".indicate3").fadeOut(400);
			  }
			});			

			$(":radio[name='language']").click(function(){
			  var newVal = $(":radio[name='language']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate4").fadeIn(400);
			  } else {
			    $(".indicate4").fadeOut(400);
			  }
			});
	
			$(":radio[name='adjlic']").click(function(){
			  var newVal = $(":radio[name='adjlic']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate5").fadeIn(400);
			  } else {
			    $(".indicate5").fadeOut(400);
			  }
			});

			$(":radio[name='autolia']").click(function(){
			  var newVal = $(":radio[name='autolia']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate6").fadeIn(400);
			  } else {
			    $(".indicate6").fadeOut(400);
			  }
			});

			$(":radio[name='omissions']").click(function(){
			  var newVal = $(":radio[name='omissions']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate7").fadeIn(400);
			  } else {
			    $(".indicate7").fadeOut(400);
			  }
			});

			$(":radio[name='currentcert']").click(function(){
			  var newVal = $(":radio[name='currentcert']:checked").val();
			  if (newVal == "Yes") {
			    $(".indicate8").fadeIn(400);
			  } else {
			    $(".indicate8").fadeOut(400);
			  }
			});

			var currentDiv = 0;
			
			$('#freeform').submit(function() {
				var isValid = true;
				
				if(currentDiv == 0) {
					// since this is div 0, validate each element with class "validate0"
					$(".validate0").each(function() {
						var label = $('#' + $(this).attr('id') + "Lbl");
						var self = this;
						var setFieldError = function(msg) {
							$(self).parent().addClass($(self).get()[0].tagName.toLowerCase() == "input" ? "input-text-error" : "input-textarea-error");
							$(label).addClass("errormsg");
							$(label).append(msg + ", ");
						}
									
						if($(this).hasClass("not_empty")) {
							if($(this).val().trim() == '') {
								isValid = false;
								setFieldError(" Required Field");
							}
						}
									
						if($(this).hasClass("is_email")) {
							var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
							if(!filter.test($(this).val())) {
								isValid = false;
								setFieldError(" Invalid Email Email");
							}
						}
			
						if(label.hasClass("errormsg")) {
							// strip off the comma
							label.text(label.text().substring(0, label.text().length - 2));
						}						
					});
				} else if(currentDiv == 1) {
					// since this is div 1, validate each element with class "validate1"
					$(".validate1").each(function() {
						var label = $('#' + $(this).attr('id') + "Lbl");
						var self = this;
						var setFieldError = function(msg) {
							$(self).parent().addClass($(self).get()[0].tagName.toLowerCase() == "input" ? "input-text-error" : "input-textarea-error");
							$(label).addClass("errormsg");
							$(label).append(msg + ", ");
						}
									
						if($(this).hasClass("not_empty")) {
							if($(this).val().trim() == '') {
								isValid = false;
								setFieldError(" Required Field");
							}
						}
									
						if($(this).hasClass("is_email")) {
							var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
							if(!filter.test($(this).val())) {
								isValid = false;
								setFieldError(" Invalid Email");
							}
						}
			
						if(label.hasClass("errormsg")) {
							// strip off the comma
							label.text(label.text().substring(0, label.text().length - 2));
						}
					});
				} else if(currentDiv == 2) {
					// since this is div 2, validate each element with class "validate1"
					$(".validate2").each(function() {
						var label = $('#' + $(this).attr('id') + "Lbl");
						var self = this;
						var setFieldError = function(msg) {
							$(self).parent().addClass($(self).get()[0].tagName.toLowerCase() == "input" ? "input-text-error" : "input-textarea-error");
							$(label).addClass("errormsg");
							$(label).append(msg + ", ");
						}
									
						if($(this).hasClass("not_empty")) {
							if($(this).val().trim() == '') {
								isValid = false;
								setFieldError(" Required Field");
							}
						}
									
						if($(this).hasClass("is_email")) {
							var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
							if(!filter.test($(this).val())) {
								isValid = false;
								setFieldError(" Invalid Email");
							}
						}
			
						if(label.hasClass("errormsg")) {
							// strip off the comma
							label.text(label.text().substring(0, label.text().length - 2));
						}
					});
				} else {
					// since this is div 3, validate each element with class "validate1"
					$(".validate3").each(function() {
						var label = $('#' + $(this).attr('id') + "Lbl");
						var self = this;
						var setFieldError = function(msg) {
							$(self).parent().addClass($(self).get()[0].tagName.toLowerCase() == "input" ? "input-text-error" : "input-textarea-error");
							$(label).addClass("errormsg");
							$(label).append(msg + ", ");
						}
									
						if($(this).hasClass("not_empty")) {
							if($(this).val().trim() == '') {
								isValid = false;
								setFieldError(" Required Field");
							}
						}
									
						if($(this).hasClass("is_email")) {
							var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
							if(!filter.test($(this).val())) {
								isValid = false;
								setFieldError(" Invalid Email");
							}
						}
			
						if(label.hasClass("errormsg")) {
							// strip off the comma
							label.text(label.text().substring(0, label.text().length - 2));
						}
					});
				}
				
				if(currentDiv != 3 && isValid) {
					// hide current div, show new div because the validation checks out and we are not on the last div
					var divName = "#sf" + currentDiv;
					$(divName).fadeOut(400);
					currentDiv++;
					var divName2 = "#sf" + currentDiv;
					$(divName2).fadeIn(400);
					
					 
					return false;
				}
					
				// this will only be reached if we're on the last div and form is valid OR we're not on the last div and the form is invalid
				return isValid;
			});

				

	});			