$().ready(function() {
	$(".other_elm").attr("disabled", "disabled");
	$(".other_elm_year").attr("disabled", "disabled");
	$(".other_elm_config").attr("disabled", "disabled");
	//$("#qualify_btn").attr("disabled", "disabled");
	
	check_status();
	
	$('#whatsthis').SetBubblePopup({
		innerHtml: "<p><img src='images/salvage_pic.jpg' border='0' alt='' /></p><p style='width:200px;'><i class='font_s'>Look for the word SALVAGE in the history box just under the words 'CERTIFICATE OF TITLE' at the top of your title.</i></p>"
	});
	
});	

//to check if the two must selected fields have been selected
function check_status(){
	var test1 = $('input:radio[name=reg_in_ca]:checked').val();
	var test2 = $('input:radio[name=salvage_title]:checked').val();
	
	if(test1 == 0){
		jAlert("Sorry, your car must be registered in California to qualify for a car title loan from BSL. If you incorrectly clicked the 'No' answer, click 'OK' to this message and then click the 'Yes' answer.", "Attention");
		return false;
	}
	
	if(test2 == 1){
		jAlert("Sorry, we cannot make a car title loan on a car with a Salvage title. If you incorrectly clicked the 'Yes' answer to this question, click 'OK' to this message and then click the 'No' answer.", "Attention");	
		return false;
	}
	
	if(test1 == 1 && test2 == 0){
		$(".other_elm").removeAttr("disabled");
		$(".other_elm_year").removeAttr("disabled");
		
		//to check if the user entered a VIN
		if($("#vin").val() != ''){
			$(".other_elm_year").attr("disabled", "disabled");	
			$(".other_elm_config").attr("disabled", "disabled");
			$("#make").html('');
			$("#model").html('');
			$("#trim").html('');
			$("#engine").html('');
			$("#drivetrain").html('');
			$("#transmission").html('');
	
			//show the final qualification button
			if($("#mileage").val() != ''){
				//$("#qualify_btn").removeAttr("disabled");	
				return true;
			}else{
				//$("#qualify_btn").attr("disabled", "disabled");	
				return false;
			}
			
		}else{
			//$(".other_elm_config").removeAttr("disabled");
			if($("#mileage").val() != '' && $("#year").val() != '' && $("#make").val() != '' && $("#model").val() != '' && $("#trim").val() != '' && $("#engine").val() != '' && $("#drivetrain").val() != '' && $("#transmission").val() != ''){
				//$("#qualify_btn").removeAttr("disabled");	
				return true;
			}else{
				//$("#qualify_btn").attr("disabled", "disabled");
				return false;
			}
		}
		
		
	}else{
		$(".other_elm").attr("disabled", "disabled");	
		$(".other_elm_year").attr("disabled", "disabled");	
		$(".other_elm_config").attr("disabled", "disabled");
		$("#make").html('');
		$("#model").html('');
		$("#trim").html('');
		$("#engine").html('');
		$("#drivetrain").html('');
		$("#transmission").html('');
		
		//$("#qualify_btn").attr("disabled", "disabled");	
		return false;
	}
}


//submit action
function submit_fn(flag){
	//used to check if the system has picked up a returned vehicle from vin query for multiple result
	var selected_vin_record_id = -1;
	
	if(!check_status() && flag == 'valuate'){ //only when submit button clicked, then check to see if all fields were filled in
		jAlert("Please fill in all required fields", "Attention");
		return false;
	}
	
	$("#error").hide();
	if(flag == 'year'){
		$("#make_ajax").show();
		
		$("#make").attr("disabled", "disabled");
		$("#make").html('');
		$("#model").attr("disabled", "disabled");
		$("#model").html('');
		$("#trim").attr("disabled", "disabled");
		$("#trim").html('');
		$("#engine").attr("disabled", "disabled");
		$("#engine").html('');
		$("#drivetrain").attr("disabled", "disabled");
		$("#drivetrain").html('');
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');
	
	}else if(flag == 'make'){
		$("#model_ajax").show();
		
		$("#model").attr("disabled", "disabled");
		$("#model").html('');
		$("#trim").attr("disabled", "disabled");
		$("#trim").html('');
		$("#engine").attr("disabled", "disabled");
		$("#engine").html('');
		$("#drivetrain").attr("disabled", "disabled");
		$("#drivetrain").html('');
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');
	
	}else if(flag == 'model'){
		$("#trim_ajax").show();
		
		$("#trim").attr("disabled", "disabled");
		$("#trim").html('');
		$("#engine").attr("disabled", "disabled");
		$("#engine").html('');
		$("#drivetrain").attr("disabled", "disabled");
		$("#drivetrain").html('');
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');
	
	}else if(flag == 'trim'){
		$("#engine_ajax").show();
		
		$("#engine").attr("disabled", "disabled");
		$("#engine").html('');
		$("#drivetrain").attr("disabled", "disabled");
		$("#drivetrain").html('');
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');		
	
	}else if(flag == 'engine'){
		$("#drivetrain_ajax").show();
		
		$("#drivetrain").attr("disabled", "disabled");
		$("#drivetrain").html('');
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');			
	
	}else if(flag == 'drivetrain'){
		$("#transmission_ajax").show();
		
		$("#transmission").attr("disabled", "disabled");
		$("#transmission").html('');		
	
	}else if(flag == 'transmission'){
		//show the final qualification button
		if($("#mileage").val() != ''){
			//$("#qualify_btn").removeAttr("disabled");	
		}else{
			//$("#qualify_btn").attr("disabled", "disabled");
		}
		
	}else if(flag == 'valuate'){
		$("#qualify_btn").hide();
		//$("#qualify_btn").attr("disabled", "disabled");
		$("#qualify_btn_ajax").show();	
	
	}	
	
	
	if(flag != 'transmission'){
	
		//to get all the values	
		var mileage = $("#mileage").val();
		mileage = mileage.replace(",", "");
		
		var amount_owed = $("#amount_owed").val();
		amount_owed = amount_owed.replace("$", "");
		amount_owed = amount_owed.replace(",", "");
		
		var vin = $("#vin").val();
		
		var year = $("#year").val();
		var year_name = $("#year option:selected").text();
		
		var make = $("#make").val();
		var make_name = $("#make option:selected").text();
		
		var model = $("#model").val();
		var model_name = $("#model option:selected").text();
		
		var trim = $("#trim").val();
		var trim_name = $("#trim option:selected").text();
		
		var engine = $("#engine").val();
		var engine_name = $("#engine option:selected").text();
		
		var drivetrain = $("#drivetrain").val();
		var drivetrain_name = $("#drivetrain option:selected").text();
		
		var transmission = $("#transmission").val();
		var transmission_name = $("#transmission option:selected").text();
		
		if(vin != ''){
			$(".vin_multi_radio").each(function(i){
				if($(this).attr('checked') == true){
					selected_vin_record_id = $(this).val();
				}
			});
				
		}else{
			//clear the div if there is content
			$("#vin_multiple").html('');	
		}
		
		$.post('qualify.php', {task:'query', flag:flag, mileage:mileage, amount_owed:amount_owed, vin:vin, year:year, make:make, model:model, trim:trim, engine:engine, drivetrain:drivetrain, transmission:transmission, year_name:year_name, make_name:make_name, model_name:model_name, trim_name:trim_name, engine_name:engine_name, drivetrain_name:drivetrain_name, transmission_name:transmission_name, selected_vin_record_id:selected_vin_record_id}, function(data) {
			if(data != ''){
				//pre-process the data
				var content_arr = new Array();
				content_arr = data.split('#@#');
				var content = content_arr[0];
				var content_count = content_arr[1];
				
				if(flag == 'year'){
					$("#make").html(content);
					$("#make_ajax").hide();
					$("#make").removeAttr("disabled");	
					
					if(content_count == 1){
						submit_fn('make');	
					}
				
				}else if(flag == 'make'){
					$("#model").html(content);	
					$("#model_ajax").hide();
					$("#model").removeAttr("disabled");	
					
					if(content_count == 1){
						submit_fn('model');	
					}
				
				}else if(flag == 'model'){
					$("#trim").html(content);
					$("#trim_ajax").hide();
					$("#trim").removeAttr("disabled");	
					
					if(content_count == 1){
						submit_fn('trim');	
					}	
				
				}else if(flag == 'trim'){
					$("#engine").html(content);	
					$("#engine_ajax").hide();
					$("#engine").removeAttr("disabled");	
					
					if(content_count == 1){
						submit_fn('engine');	
					}	
				
				}else if(flag == 'engine'){
					$("#drivetrain").html(content);
					$("#drivetrain_ajax").hide();	
					$("#drivetrain").removeAttr("disabled");		
					
					if(content_count == 1){
						submit_fn('drivetrain');	
					}	
				
				}else if(flag == 'drivetrain'){
					$("#transmission").html(content);	
					$("#transmission_ajax").hide();	
					$("#transmission").removeAttr("disabled");	
					
				}else if(flag == 'valuate'){
					$("#qualify_btn_ajax").hide();
					
					//used to check the return value when VIN is used for query, fo checking if multiple vehicle records were returned by VIN query
					var vin_query_check_arr = new Array();
					if(vin != ''){
						vin_query_check_arr = data.split('#');
					}
					
					if(data == 'success'){
						window.location.href='application_form.php';
					
					}else if(vin != '' && vin_query_check_arr[0] == 'vin_to_multiple'){
						//shwo the select radio options under vin input field
						$("#vin_multiple").html(data.substr(16));
						
						//show the final qualification button
						$("#qualify_btn").show();
						
					}else{
						$("#error").show();
						$("#error").html(data);	
						//show the final qualification button
						$("#qualify_btn").show();
						//$("#qualify_btn").removeAttr("disabled");	
					
					}
				}
				
				check_status();
			}
		});

	}
	
}

function remove_default_text(){
	var check_text = $('#amount_owed').val();
	if(check_text == 'Enter 0 if paid off'){
		$('#amount_owed').val('');	
	}
}