// JavaScript Document


									
									
$(document).ready(function() {	

	$("#date").dateinput({
		format: 'yyyy-mm-dd',	// the format displayed for the user
		selectors: false,             	// whether month/year dropdowns are shown
		min: -0,                    // min selectable day (100 days backwards)
		max: 180,                    	// max selectable day (100 days onwards)
		offset: [-30, -0],            	// tweak the position of the calendar
		speed: 'fast',               	// calendar reveal speed
		firstDay: 1                  	// which day starts a week. 0 = sunday, 1 = monday etc..
	});
		$("#zipFinder").autocomplete('/servlet/servlets.ZipServlet', {
				minChars: 2,
				max: 10,
				autoFill: false,
			formatItem: function(rowdata) {
				var details = rowdata[0].split(":");
				return details[1];
			},
			formatResult: function (rowdata) {
				var details = rowdata[0].split(":");
				return details[1];
			}
		 }).result(function(event, data, formatted) {
			 getZip();
			 
		 });
		 
		
		 
});
var auto = false;
function getZip(){
	var data = "q=" + $("#zipFinder").val();
	
	$.ajax({
		  type: "POST",
		  url: "/servlet/servlets.ZipFinder",
		  data: data,
		  beforeSend: function(html){
				$("#zipFinderError").html("<img src='/images/wait.gif'/> searching...");  
		  },
		  success: function(html){
				$("#zipFinderError").html("");
				if(html != 'not_found'){
					$("#movingZip").val(html);
					doZipFinder();
				}else{
					$("#zipFinderError").html("Not found, please try again");
				}
				
		  }
	  });
}
function toggleAuto(selection){
	if(selection == 'yes'){
		$("#auto_transportbox").fadeIn("slow", function(){
				auto = true;
			});
		$('input:radio[name=wantAuto][value=yes]').attr('checked', true);
		$("#access_box").hide();
	}else{
		
		$("#auto_transportbox").fadeOut("slow", function(){
				auto = false;				
				$("#access_box").fadeIn("slow");
				
			});
			$('input:radio[name=wantAuto][value=no]').attr('checked', true);
	}
}
function moveType(move){
	
	if(move == 'auto_shipping'){
		toggleAuto('yes');	
		return;
	}
	toggleAuto('no');
	
	if(move == 'international'){
		window.location = '/type/international.jsp';	
	}
	
	if(move == 'uload'){
		window.location = '/type/uload.jsp';	
	}
	
	if(move == 'truckrental'){
		window.location = '/type/truck_rental.jsp';	
	}
	if(move == 'storage'){
		window.location = '/type/self_storage.jsp';	
	}
		
	
}
function doZipFinder(){
	$("#zipfinderbox").fadeToggle("slow");	
}
function validateForm(){
	
	var errors = "";
	
	if($("#type").val() == ''){
		errors += "Quote Type is required<br/>";	
		$("#type").addClass("redbg");	
	}else{
		$("#type").removeClass("redbg");
	}
	if($("#zip").val() == ''){
		errors += "Zip is required<br/>";	
		$("#zip").addClass("redbg");	
	}else{
		$("#zip").removeClass("redbg");
	}
	if($("#movingZip").val() == ''){
		errors += "Moving Zip is required<br/>";	
		$("#movingZip").addClass("redbg");	
	}else{
		$("#movingZip").removeClass("redbg");
	}
	if($("#date").val() == ''){
		errors += "Date is required<br/>";
		$("#date").addClass("redbg");		
	}else{
		$("#date").removeClass("redbg");
	}
	if($("#access").val() == '' && $("#type").val() != 'auto_shipping'){
		errors += "Move Size is required<br/>";
		$("#access").addClass("redbg");		
	}else{
		$("#access").removeClass("redbg");
	}
	if(typeof $("input[name='wantAuto']:checked").val() === 'undefined'){
		errors += "Auto Shipping is required<br/>";
		$("#wantradiobox").addClass("redbg");	
	}else{
		if($("input[name='wantAuto']:checked").val() == 'yes'){
			if($("#model").val() == ''){
				errors += "Make of Auto is required<br/>";
				$("#model").addClass("redbg");	
			}else{
				$("#model").removeClass("redbg");
			}
			if($("#modelType").val() == ''){
				errors += "Model of Auto is required<br/>";
				$("#modelType").addClass("redbg");	
			}else{
				$("#modelType").removeClass("redbg");
			}
			if($("#carType").val() == ''){
				errors += "Car Type is required<br/>";
				$("#carType").addClass("redbg");	
			}else{
				$("#carType").removeClass("redbg");
			}
			if($("#model").val() != '' && $("#modelType").val() != ''){
				$("#carselected").html($("#model").val() + " " + $("#modelType").val());	
			}
			if($("#year").val() == ''){
				errors += "Car Year is required<br/>";
				$("#year").addClass("redbg");	
			}else{
				$("#year").removeClass("redbg");
			}
			if(typeof  $("input[name='modelStatus']:checked").val()  === 'undefined'){
				errors += "Model Status is required<br/>";
				$("#modelStatusBox").addClass("redbg");	
			}else{
				$("#modelStatusBox").removeClass("redbg");
			}
			
		}
		$("#wantradiobox").removeClass("redbg");
		
	}
	
	if($("#name").val() == ''){
		errors += "Name is required<br/>";
		$("#name").addClass("redbg");		
	}else{
		$("#name").removeClass("redbg");
	}
	if($("#phone").val() == ''  || !phoneNumber){
		errors += "Phone is required<br/>";
		$("#phone").addClass("redbg");		
	}else{
		$("#phone").removeClass("redbg");
	}
	if($("#email").val() == '' || !validateEmail( $("#email").val() ) ){
		errors += "Email is required<br/>";
		$("#email").addClass("redbg");		
	}else{
		$("#email").removeClass("redbg");
	}
	if(errors.length == 0){
		// do ajax submit
		return true;
		
	}else{
		$("#errors").html(errors).fadeIn("slow");
	}
	
	return false;
}
function validateZip(zip, obj){
	
	var data = "z=" + zip;
		
	$.ajax({
		  type: "POST",
		  url: "/servlet/servlets.ZipChecker",
		  data: data,
		  beforeSend: function(html){
				$("#" + obj).html("<img src='/images/wait.gif'/> searching...");  
		  },
		  success: function(html){
				$("#" + obj).html("");
				if(html == 'not_found'){
					$("#" + obj).html("Invalid Zip").addClass("redbg");
				}else{
					$("#" + obj).html('').removeClass("redbg");
				}
				
		  }
	  });
}
var phoneNumber = false;
function checkPhone(phone){
	
	var data = "area=" + $.trim(phone);
		
	$.ajax({
		  type: "POST",
		  url: "/servlet/servlets.AreaCodeFinder",
		  data: data,
		  beforeSend: function(html){
				$("#phoneError").html("<img src='/images/wait.gif'/> searching...");  
		  },
		  success: function(html){
				$("#phoneError").html("");
				if(html == 'not_found'){
					$("#phoneError").html("Invalid Phone").addClass("redbg");
					phoneNumber = false;
				}else{
					$("#phoneError").html('').removeClass("redbg");
					phoneNumber = true;
				}
				
		  }
	  });
}
