/*#######################################################################

The following functions are used for dynamically generating the vehicle make/model
form select dropdowns in the purchase process.

##########################################################################*/
$(document).ready(function() {
	initVehicleSelectVars ();
	vehicletypeListener ();
	yearListener ();
	makeListener ();
	modelListener ();
	valueListener ();
	vehicletypeInit ();
  sentriInit ();
});

//Initialize variables for vehicle select. These are global.
function initVehicleSelectVars () {
	vehicle_type 	= $('#vehicle_type_select').val();	
	year 			= $('#year_select').val();	
	make 			= $('#make_select').val();	
	model 			= $('#model_select').val();	
	body_style 		= $('#body_style_select').val();
	quote_idx 		= $('#csx').val();
	is_primary 		= $('#prim').val();
	is_startpage 	= $('#is_startpage').val();
  is_sentri_ref = $('#is_sentri_ref').val();
	primary_value 	= $('#primary_value').val();
	curvn 			= $('#curvn').val();
}

function vehicletypeListener () {
	$('#vehicle_type_select').click (function() {
		getHelp($('#vehicle_type_selector_help').attr('alt'));
	}).change(function() {
		vehicletypeToggled (); 
		hideWarnings ();
	});
}

function sentriInit () {
	if (is_sentri_ref == 1) {
    checkSentri();
	}
}

function vehicletypeInit () {
	if (vehicle_type == -1) {
		checkNamedDriver(true);
	}
}

function yearListener () {
	$('#year_select').click (function() {
		//alert ();
    getHelp($('#year_selector_help').attr('alt'));
    //id="year_selector_help" alt="towed_shell_year"
	}).change(function() {
		yearToggled (); 
		hideWarnings (); 
	});
}

function makeListener () {
	$('#make_select').click (function() {
		getHelp($('#make_selector_help').attr('alt'));
	}).change(function() {
		makeToggled (); 
		hideWarnings (); 
	});
}

function modelListener () {
	$('#model_select').click (function() {
		getHelp($('#model_selector_help').attr('alt'));
	}).change(function() {
		hideWarnings ();
		modelToggled (); 
	});
}

function bodystyleListener () {
	$('#body_style_select').click (function() {
		getHelp($('#model_selector_help').attr('alt'));
	}).change(function() {
		bodystyleToggled (); 	
	});
}

function valueListener () {
	$('#value_select').click (function() {
		getHelp($('#value_selector_help').attr('alt'));
	}).change(function() {
		hideWarnings ();
		valueToggled (); 
	});
}

function vehicletypeToggled () {
	vehicle_type 	= $('#vehicle_type_select').val();	
	
	if (vehicle_type == -1) {
		checkNamedDriver(true);
	} else {
		checkNamedDriver(false);
		$('#year_selector').load('/buy/vehicle-year-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type}, function() {
			yearListener (this);
		});
		$('#make_select').val("").attr('disabled', 'disabled');
		$('#model_select').val("").attr('disabled', 'disabled');
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'is_startpage' : is_startpage, 'quote_idx' : quote_idx});
	}
	hideBodyStyle ();
  
 	if (vehicle_type == -1) {
    var warning_sign = '<img src="/images/global/caution_sign_small.gif" alt="Caution" width="30" height="27" class="floatleft" style="margin-right: 5px;" />';
    $('#vehicle_type_warning').html(warning_sign + '<strong>Driver\'s license policies do not provide coverage while driving motorcycles, motorhomes, buses, or Mexican plated vehicles.</strong>').show();
  } else {
    $('#vehicle_type_warning').hide();
  }

	return false;
}

function yearToggled () {
	
	vehicle_type 	= $('#vehicle_type_select').val();	
	year 			= $('#year_select').val();
	
	//Look for year
	var yrn_found 		= year.indexOf('yrn');
	if (yrn_found != -1) {
		$('#year_selector').load('/buy/vehicle-year-select.php', {'from_ajax': true, 'notlisted': true, 'listcontrol' : true, 'vehicle_type': vehicle_type, 'year': year});
		$('#make_selector').load('/buy/vehicle-make-select.php', {'from_ajax': true, 'notlisted': true, 'vehicle_type': vehicle_type, 'year': year});
		$('#model_selector').load('/buy/vehicle-model-select.php', {'from_ajax': true, 'notlisted': true, 'vehicle_type': vehicle_type, 'year': year, 'make_notlisted' : 1});	
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'is_startpage' : is_startpage, 'quote_idx' : quote_idx});
		
		//  for value_selector
	} else {
		$('#make_selector').load('/buy/vehicle-make-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year}, function() {
			makeListener (this);
		});
		$('#model_selector').load('/buy/vehicle-model-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year}, function() {
			modelListener (this);
		});
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'is_startpage' : is_startpage, 'primary_value' : primary_value, 'quote_idx' : quote_idx}, function() {
			valueListener (this);
		});
	}
	
	hideBodyStyle ();
	return false;
}

function makeToggled () {
	vehicle_type 	= $('#vehicle_type_select').val();	
	year 			= $('#year_select').val();	
	make 			= $('#make_select').val();		
	
	//Look for year
	var mkn_found 		= make.indexOf('mkn');
	if (mkn_found != -1) {
		$('#make_selector').load('/buy/vehicle-make-select.php', {'from_ajax': true, 'notlisted': true, 'listcontrol' : true, 'vehicle_type': vehicle_type, 'year': year});	
		$('#model_selector').load('/buy/vehicle-model-select.php', {'from_ajax': true, 'notlisted': true, 'vehicle_type': vehicle_type, 'year': year, 'make_notlisted' : 1});	
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year, 'show_value' : 'FALSE'});
	} else {
	
		$('#model_selector').load('/buy/vehicle-model-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year, 'make' : make}, function() {
			modelListener (this);
		});	
		
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'is_startpage' : is_startpage, 'primary_value' : primary_value, 'quote_idx' : quote_idx}, function() {
			valueListener (this);
		});
	}
	
	hideBodyStyle ();
	return false;
}


function modelToggled () {
	vehicle_type 	= $('#vehicle_type_select').val();	
	year 			= $('#year_select').val();	
	make 			= $('#make_select').val();	
	model 			= $('#model_select').val();	
	
	//Look for year
	var mdn_found 		= model.indexOf('mdn');
	if (mdn_found != -1) {		
		$('#model_selector').load('/buy/vehicle-model-select.php', {'from_ajax': true, 'notlisted': true, 'listcontrol' : true, 'vehicle_type': vehicle_type, 'year': year});	
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year, 'show_value' : 'FALSE'});
		hideBodyStyle ();
		
	}  else {
		$('#body_style_selector').load('/buy/vehicle-body-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year': year, 'make' : make, 'model' : model}, function() {
			bodystyleListener (this);
		}).show();	
		
		$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'primary_value' : primary_value, 'is_startpage' : is_startpage, 'quote_idx' : quote_idx}, function() {
			valueListener (this);
		});
	}
	
	var warning_sign = '<img src="/images/global/caution_sign_small.gif" alt="Caution" width="30" height="27" class="floatleft" style="margin-right: 5px;" />';
	if (mdn_found != -1) {$('#model_warning').html(warning_sign + 'This vehicle must be listed in our system for physical damage and theft coverage. Call (888) 552-2252 for assistance.').show();} 
	if (vehicle_type == 10 && mdn_found == -1) {$('#model_warning').html(warning_sign + 'This motorcycle must be street legal for physical damage and theft coverage. Call (888) 552-2252 for assistance.').show();} 
	if (model == 'WRANGLER') {$('#model_warning').html(warning_sign + 'Soft-Top vehicles are NOT ELIGIBLE FOR FULL COVERAGE.').show();}
	
	return false;
}


function bodystyleToggled () {
	vehicle_type 	= $('#vehicle_type_select').val();	
	year 			= $('#year_select').val();	
	make 			= $('#make_select').val();	
	model 			= $('#model_select').val();
	body_style 		= $('#body_style_select').val();	
	quote_idx 		= $('#csx').val();	
	
	
	
	//Toggle Value
	$('#value_selector').load('/buy/vehicle-value-select.php', {'from_ajax': true, 'vehicle_type': vehicle_type, 'year' : year, 'make' : make, 'model' : model, 'body_style' : body_style, 'curvn' : curvn, 'is_primary' : is_primary, 'is_startpage' : is_startpage, 'primary_value' : primary_value, 'quote_idx' : quote_idx}, function() {
		valueListener (this);
	});
	
	return false;
}

function valueToggled () {
	var value 			= $('#value_select').val();	
	var warning_sign = '<img src="/images/global/caution_sign_small.gif" alt="Caution" width="30" height="27" class="floatleft" style="margin-right: 5px;" />';
	if (value == 60000) {$('#value_warning').html(warning_sign + 'You have selected the maximum value. We cannot insure vehicles valued over $60,000.').show();} 
	
	if (value > 0) {
		$('.warningbox').show("normal");
	} else {
		$('.warningbox').hide();
	}
	
	return false;
}

function hideBodyStyle () {
	$('#body_style_selector').hide();
}

function hideWarnings () {
	$('#model_warning').hide();
	$('#value_warning').hide();
}

// Check if user has selected Named Driver
// If so, disable all the stuff Named Driver doesn't use (year, make, model).
// Otherwise, re-enable year, make, model.
// vt = vehicle type
// tm = currently selected coverage term



function checkSentri() { 

	$('#towed_items, #under21_row, #rental_car_row').hide();		
    $('#sentri_towed').show();
    options = '<option value="3">Annual</option>';
		$("#tm").html(options);
		toggleNumberOfDays();  // Toggle "Number of Days" dropdown.
}

function checkNamedDriver(named_driver_selected) { 

	if (named_driver_selected) { // Named driver, hide everything but Vehicle Type
	
		//############################################################### For the start.php
		$('#vehicle_year_row, #vehicle_make_row, #vehicle_model_row, #vehicle_value_row, #towed_items, #year_selector, #make_selector, #model_selector').hide();			
		$('#named_driver_towed').show();
    options = '<option value="3">Annual</option>';
		$("#tm").html(options);
		toggleNumberOfDays();  // Toggle "Number of Days" dropdown. 
		
	} else {
		
    if (is_sentri_ref == 1) {
      $('#vehicle_year_row, #vehicle_make_row, #vehicle_model_row, #vehicle_value_row, #year_selector, #make_selector, #model_selector').show();	
      $('#named_driver_towed').hide();
      
      toggleNumberOfDays();
    } else {
		//############################################################### For start.php
      $('#vehicle_year_row, #vehicle_make_row, #vehicle_model_row, #vehicle_value_row, #towed_items, #year_selector, #make_selector, #model_selector').show();	
      $('#named_driver_towed').hide();
      options = '<option value="1">Daily</option><option value="2">6 Month</option><option value="3">Annual</option>';
      $("#tm").html(options);
      $('#tm option:first').attr('selected', 'selected');
      toggleNumberOfDays();
    }
	}
}