$(document).ready(function()
{
	
	function ie6disable() {
		 if (document.getElementsByTagName) {
			 var s = document.getElementsByTagName('select');
			 if (s.length > 0) {
				 window.select_current = new Array();
				 for (var i=0, select; select = s[i]; i++) {
					 select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }
					 select.onchange = function(){ restore(this); }
					 emulate(select);
				 }
			 }
		 }
	}
	
	function restore(e) {
		if (e.options[e.selectedIndex].disabled) {
			e.selectedIndex = window.select_current[e.id];
		}
	}
	
	function emulate(e) {
		for (var i=0, option; option = e.options[i]; i++) {
			if (option.disabled) {
				option.style.color = "graytext";
			}
			else {
				option.style.color = "menutext";
			}
		}
	}
	
	
	
	
	
	function check_clause()
	{
		var insurance_type = $('select#clause_id').val();
		
		if(insurance_type == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}


	
	function calculate_ten_years_before()
	{
		var day = $('select#day').val();
		var month = $('select#month').val();
		var year = $('select#year').val();
		var date = month + '.' + day + '.' + '.' + year;
		
		var parsed_date  = Date.parse(date);
		var ten_years = Date.today().addYears(-10);
		
		// 1 greater, 0 equal -1 lesser
		return Date.compare(parsed_date, ten_years);
	
	}
	
	function select_type_id_zero()
	{
		$('select#type_id').find("option[value='1']").attr("disabled","disabled");
		$('select#type_id').find("option[value='2']").attr("disabled","disabled");
		$('select#type_id').find("option[value='3']").attr("disabled","disabled");
		$('select#type_id').find("option[value='0']").attr("selected","selected");
		if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); }
	}
	
	function set_dropdown_menus_values()
	{
		
		var selected_type =  $('select#type_id').val();
		
		
		if(selected_type == 1 || selected_type == 2 || selected_type == 3  )
		{
			select_type_id_zero();
		}
		
		$('select#type_id').find("option[value='1']").attr("disabled","disabled");
		$('select#type_id').find("option[value='2']").attr("disabled","disabled");
		$('select#type_id').find("option[value='3']").attr("disabled","disabled");
		if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); }
	}
	
	
	function actions_on_change()
	{
		if(check_clause() == true)
		{
			compare_dates = calculate_ten_years_before();

			if(compare_dates == -1 || compare_dates == 0)
			{
				set_dropdown_menus_values();
			}
			else
			{
				$('select#type_id').find('option').each(function(index)
				{
					$(this).removeAttr("disabled");
				});
			}
			
		}
		
	}
	
	
	

	
	// НУЛИРА ВСИЧКИ ДРОПДАУН МЕНЮТА 
	$('select#clause_id').find("option[value='0']").attr("selected","selected");
	$('select#type_id').find("option[value='0']").attr("selected","selected");
	
	$('div#message').hide();
	
	// ТИП ЗАСТРАХОВКА **/
	$('select#clause_id').change(function()
	{

		var insurance_type = $('select#clause_id').val();
		
		
		
		$('select#type_id').find('option').each(function(index){
    	
			$(this).removeAttr("disabled");
			
		});
		
		actions_on_change();
		$('div#message').empty();
		$('div#message').hide();
		
		if(insurance_type > 0)
		{
			$('div#message').show();
			
		}
		
		// ОСНОВНО ПОКРИТИЕ **/
		if(insurance_type == 1)
		{

		
			$('div#message').append('Основно покритие не се предлага за леки автомобили, лекотоварни до 1.5 тона и джипове над 10 г.');
			
			$('select#paid').find('option').each(function(index){
		    	
				$(this).removeAttr("disabled");	

			});
			$('select#paid').find("option[value='0']").attr("selected", "selected");
		}
		// РАЗШИРЕНО ПОКРИТИЕ **/
		else if(insurance_type == 2)
		{
			$('div#message').append('Застраховка "Разширено покритие" не се предлага за мотоциклети и мотопеди');
			$('select#type_id').find("option[value='18']").attr("disabled","disabled");
			$('select#type_id').find("option[value='19']").attr("disabled","disabled");
			$('select#type_id').find("option[value='0']").attr("selected","selected");
			
			$('select#paid').find('option').each(function(index){
		    	
				$(this).removeAttr("disabled");	

			});
			$('select#paid').find("option[value='0']").attr("selected", "selected");

		}
		// КРАЖБА 
		else if(insurance_type == 3)
		{
			$('div#message').append('Застраховка "Кражба" не се предлага за мотоциклети и мотопеди');
			$('select#type_id').find("option[value='18']").attr("disabled","disabled");
			$('select#type_id').find("option[value='19']").attr("disabled","disabled");
			$('select#type_id').find("option[value='0']").attr("selected","selected");
			
			$('select#paid').find("option[value='2']").attr("disabled", "disabled");
			$('select#paid').find("option[value='3']").attr("disabled", "disabled");
			$('select#paid').find("option[value='4']").attr("disabled", "disabled");
			$('select#paid').find("option[value='1']").attr("selected", "selected");
			
		}
		
		if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); }
	
	});	
	
	
	

	
	
	// Изчислява датата при всяка промяна на ден, месец, година 
	$('select#day').change(function(){ actions_on_change();  if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); } });
	$('select#month').change(function(){ actions_on_change();  if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); } });	
	$('select#year').change(function(){ actions_on_change();  if( $.browser.msie && $.browser.version=="6.0" ) { ie6disable(); } });

	
		
});

