function hideTypeDetails(){
	$(".typ_vozidla").hide();
}

function showTypeDetails(type){
	if(type!='sanita' && type!='obytne' && type!='tahac'){
		$('#moznosti_'+type).show();
	}
	
	if(type=='osobni'){
		$('#vozidlo_zimni_pneu').removeAttr("disabled").removeClass("disabled");
	}
	else{
		$('#vozidlo_zimni_pneu').removeAttr("checked").attr("disabled", "disabled").addClass("disabled");
	}
}

function toggleTypes(){
	
	if(getCheckedTypesNumber()){//je neco zaskrtnuteho
		hideNotChecked();
	}
	else{
		showAllTypes();
		hideVozidlo();
		hidePojistnik();
		hidePojisteni();
	}
}

function hideVozidlo(){
	$('#kalkulator_vozidlo').hide();
}
function showVozidlo(){
	$('#kalkulator_vozidlo').show();
	$('#kalkulator_vozidlo_warning').hide();
}

function hidePojistnik(){
	$('#kalkulator_pojistnik').hide();
}
function showPojistnik(){
	$('#kalkulator_pojistnik').show();
	$('#kalkulator_pojistnik_warning').hide();
}

function hidePojisteni(){
	$('#kalkulator_pojisteni').hide();
}
function showPojisteni(){
	$('#kalkulator_pojisteni').show();
	$('#kalkulator_pojisteni_warning').hide();
}


function hideNotChecked(){
	$("#div_vozidlo_typ div input:checked ").show();
	$("#div_vozidlo_typ div input").not($("#div_vozidlo_typ div input:checked")).each(
			function(){
			  $(this).parent().slideUp("slow")
			}

	)
	showShower();
	hideHider();
	hideTypeDetails();
	showTypeDetails(whichType());
	showVozidlo();
	showPojistnik();
	showPojisteni();
	hideVykon();
}
function whichType(){
	return($('#div_vozidlo_typ input:checked').attr('id'));
}

function hideVykon(){
	if (whichType()=='pripojne'){
		$('#vykon').attr("disabled", "true").val("none");
	}
	else{
		$('#vykon').removeAttr("disabled");
	}
}
function getCheckedTypesNumber(){
	return ($('#div_vozidlo_typ div input:checked').length);
}
function showAllTypes(){
	$('#div_vozidlo_typ div').show('slow');
	hideShower();
	if(getCheckedTypesNumber()) showHider();
}

function showShower(){
	$('#shower').fadeIn();
}
function hideShower(){
	$('#shower').fadeOut();
}
function showHider(){
	$('#hider').fadeIn();
}
function hideHider(){
	$('#hider').fadeOut();
}

function pscChanged(){
	var psc=$('#psc_text').val();
	psc=psc.replace(' ', '');
	if(psc.length==5){
		$.ajax({
			   type: "POST",
			   url: "ajax.php",
			   data: "psc="+psc+"&action=getSelectMestoNewPojistnik",
			   datatype: 'xml',
			   success: function(xml){
			     $('#pojistnik_mesto_id').text('');
			      $(xml).find('sel').each(
					  function(){
					    option = $('<option></option>')
					    option.attr('value', $(this).attr('value'))
					    option.text($(this).text())
					    
					    if($(this).attr('selected') == 'true'){
					    	option.attr('selected', 'selected')
					    }
					    $('#pojistnik_mesto_id').append(option);
					  }
					  )
			
			   }	 
			 });
	}
}

function pscChangedDrzitel(){
	var psc=$('#drzitel_psc').val();
	psc=psc.replace(' ', '');
	if(psc.length==5){
		$.ajax({
			   type: "POST",
			   url: "ajax.php",
			   data: "psc="+psc+"&action=getSelectMestoNewDrzitel",
			   datatype: 'xml',
			   success: function(xml){
			     $('#drzitel_mesto').text('');
			      $(xml).find('sel').each(
					  function(){
					    option = $('<option></option>')
					    option.attr('value',$(this).attr('value'))
					    option.text($(this).text())
					    
					    if($(this).attr('selected') == 'true'){
					    	option.attr('selected', 'selected')
					    }
					    $('#drzitel_mesto').append(option);
					  }
					  )
			
			   }	 
			 });
	}
}


function createGetString(){
	return $('#kalkulator').serialize();
}

function zmenenaFormaPojistnika(){
	if($('#forma_pojistnika').val()!=0){
		$(
			'#pojistnik_datum_narozeni,' +
			'#pojistnik_ztp,'+
			'#pojistnik_hasic'
			)
			.attr("disabled", "true");

		
		$('#pojistnik_datum_narozeni').val("").addClass("disabled");
			
		$(
			'#pojistnik_lekar,' +
			'#pojistnik_ztp' +
			'#pojistnik_hasic'
			).removeAttr("checked");
		$('#err_pojistnik_datum_narozeni').html('');
	}
	else{
		$(
			'#pojistnik_datum_narozeni,' +
			'#pojistnik_lekar,' +
			'#pojistnik_ztp,' +
			'#pojistnik_hasic'
			).removeAttr("disabled").removeClass("disabled");
	}
}

function zmenenoDrzitelTotozny(){
	if($('#drzitel_totozny').attr("checked")){//zaskrtnuto, ze je drzitel totozny s pojistnikem
		$('#drzitel_hide').slideUp();
	}
	else{
		$('#drzitel_hide').slideDown();
	}
	
	if($('#drzitel_totozny').attr('checked')){
		$('#drzitel_forma, #drzitel_datum_narozeni, #drzitel_psc, #drzitel_mesto').attr("disabled", "disabled").addClass("disabled");
		$('#drzitel_forma, #drzitel_datum_narozeni, #drzitel_psc').val("");
		
		option = option = $('<option></option>')
	    option.attr('value', '')
	    option.text('-- vyberte --');
		$('#drzitel_mesto').html(option);
		$('#err_drzitel_forma, #err_drzitel_datum_narozeni, #err_drzitel_psc, #err_drzitel_mesto').html('');
	}
	else if($('#drzitel_forma').val()==0){
		$('#drzitel_forma, #drzitel_datum_narozeni, #drzitel_psc, #drzitel_mesto').removeAttr("disabled").removeClass("disabled");
	}else{
    $('#drzitel_forma, #drzitel_psc, #drzitel_mesto').removeAttr("disabled").removeClass("disabled");
  }

}

function zmenenaFormaDrzitele(){
	if($('#drzitel_forma').val()!=0){
		$('#drzitel_datum_narozeni').attr("disabled", "disabled").addClass("disabled");
		$('#drzitel_datum_narozeni').val("");
		$('#err_drzitel_forma, #err_drzitel_datum_narozeni').html('');
	}
	else{
		$('#drzitel_datum_narozeni').removeAttr("disabled").removeClass("disabled");
	}
}


function form_init(){
	zmenenaFormaPojistnika();
	zmenenaFormaDrzitele();
	zmenenoDrzitelTotozny();
	toggleTypes();
}

$(document).ready( function() {
	$(".kalkulator_typ_vozidla input").click(function() { hideNotChecked();}).dblclick(function() { showAllTypes();});
	$(".kalkulator_typ_vozidla label").dblclick(function() { showAllTypes();});
	$("#shower").click(function(){ showAllTypes(); });
	$("#hider").click(function(){ hideNotChecked(); });
	$('#forma_pojistnika')
		.click(function(){ zmenenaFormaPojistnika(); })
		.change(function(){ zmenenaFormaPojistnika(); })
		.keyup(function(){ zmenenaFormaPojistnika(); })
	;
	$('#drzitel_totozny')
		.click(function(){ zmenenoDrzitelTotozny(); })
		.change(function(){zmenenoDrzitelTotozny(); })
		.keyup(function(){ zmenenoDrzitelTotozny(); })
	;
	$('#drzitel_forma')
		.click(function(){ zmenenaFormaDrzitele(); })
		.change(function(){ zmenenaFormaDrzitele(); })
		.keyup(function(){ zmenenaFormaDrzitele(); })
	;

	$('#pojistnik_lekar, #pojistnik_ztp, #pojistnik_hasic').focus(function(){ schovatNapovedu(); });
	$('#psc_text').keyup(function(){ pscChanged();});
	$('#psc_text').change(function(){ pscChanged();});
	
	$('#drzitel_psc').keyup(function(){ pscChangedDrzitel();});
	$('#drzitel_psc').change(function(){ pscChangedDrzitel();});
	//pscChanged();
	//pscChangedDrzitel();

   var options = { 
       // target:        '#output1',   // target element(s) to be updated with server response 
        beforeSubmit:  beforeSubmit,  // pre-submit callback
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };

  $('#kalkulator').ajaxForm(options);

	/* ONLINE KONTROLA */
	needChange = new Array(
      'typ_osobni',
      'typ_nakladni',
      'typ_motocykl',
      'typ_autobus',
      'typ_pripojne',
      'typ_pracovni',
      'typ_traktor',
      'typ_sanita',
      'typ_obytne',
      'typ_tahac',
		'pojistnik_datum_narozeni',
		'drzitel_datum_narozeni',
		'psc_text',
		'drzitel_psc',
		'pojisteni_pojistne_udalosti',
		'typ_osobni',
		'vykon',
		'stari_vozidla',
		'pouziti_vozidla',
		'pojistnik_mesto_id',
		'forma_pojistnika',
		'drzitel_forma',
		'drzitel_mesto',
		'bezeskodni_prubeh',
		'splatky',
		'vyska_limitu',
		'drzitel_forma'
			
	);
	
	
	$(needChange).each(
		function(){
			$('#'+this).change(
					function (){
						checkElement($(this))
					}
		    );
		}
	)
	
	$("#dialog").dialog({ autoOpen: false, modal: true, bgiframe: true, title: 'Formulář obsahuje následující chyby', width: 500, buttons: { "Ok": function() { $(this).dialog("close"); } } });
});


/**
NOVE FORMULARE */
function showResponse(responseText, statusText){
  schovatNapovedu();
  $('#ajaxLoaderSubmit').remove();
  if(checkElements(responseText)){
    $("#showForm1Result").html(responseText);
    $('html,body').animate({scrollTop: 200}, 1000);
  }else{
    $("#showForm1Result").html('');
  }
  
}

function beforeSubmit(){
  $('#kalkulator_end').append($('<img>').attr('src', './images/ajax-loader.gif').attr('id', 'ajaxLoaderSubmit'));
}

function checkElement(element){
	postString = createGetString()
	var buffer = '';
	$.ajax({
		   type: "POST",
		   url: "ajax.php",
		   data: postString+"&action=checkValueForm1",
		   datatype: 'xml',
		   beforeSend: function(){
		     img = $('<img>').attr('src', './images/ajax-loader.gif');
		     $('#err_' + element.attr('name')).html(img);
	       },
		   success: function(xml){
		    	 $(xml).find('message').each(
		    	   function(){
               if($(this).attr('name') == element.attr('id')){
                 buffer += $(this).text()+ '<br>';
               }
		    	   }
		    	 )
		       if(buffer.length > 0){
		         img = $('<img>').attr('src', './images/fail.png');
		         img.attr('title', buffer);
		         img.tooltip({showURL: false});
		       }else{
		    	   img = $('<img>').attr('src', './images/ok.png');
		       }
		     $('#err_' + element.attr('name')).html(img);
		   }	 
		 });
}

function checkElements(xml){
  $('#dialog').html('');
		     if($(xml).find('status').text() == 'false'){
		    	 $(xml).find('message').each(
				    	   function(){
				    		   img = $('<img>').attr('src', './images/fail.png');
						       img.attr('title', $(this).text());
						       img.tooltip({showURL: false});
						       $('#err_' + $(this).attr('name')).html(img);
						       $('#dialog').append('<strong>'+$('[for='+$(this).attr('name')+']').html() + '</strong>: ' + $(this).text() + '<br><br>');
						       
				    	   }
				    	 )
			     $("#dialog").dialog('open');
           return false;
	       }
	return true;

}

