$(document).ready(function () {

 TypeDefined=0;
 DevicePrice=0;
 BaloonPrice=0;
 
 function compensa() {
 
 	pPrice =  parseInt($('#pprice').val());
 	gPrice =  parseInt($('#gprice').val());
 	monthlyRun =  parseInt($('#monthlyrun').val());
 	cons = parseInt($('#consumption').val());
 	dprice = parseInt($('#deviceprice').val());

 	
 	if ((pPrice!=gPrice)) {
 		cres = Math.round(100*dprice/((pPrice-gPrice)*cons*monthlyRun));
 		
 		if (cres>4) {
 			cres = ' за '+cres + ' месяцев';
 		} else {
 		
 			if (cres>1) {
 				cres = ' за '+cres + ' месяца';
 			} else {
 				if (cres==0) {
 				 	cres = ' меньше, чем за месяц';
 				} else {
 					if (cres<0) {
 						cres = ', когда бензин станет дороже газа';
 					} else {
 						cres = ' за '+cres + ' месяц';
 					}
 				}
 			}
 		
 		}

 		$('#compensabilita span').text(cres);
 	}
 	


 	
 
 }
 

 

 function typecalc(){
  	if (TypeDefined) {
  	  	BaloonPrice=parseInt($('#balon select').val());
  	  	$("#balon :checkbox:checked").each(function() {
  	  		BaloonPrice+=parseInt(this.value);
  	  	});
  		result=DevicePrice+BaloonPrice;
  		$('#deviceprice').val(result);
  		compensa();
 		$('#price span').text(result);
 		$('#price').css('display','block');
 		$('#step2').css('display','block');
 		$('#step3').css('display','block');
 		$('#order').css('display','block');
 		
 	}
 }
	
 $('#typeselect div').click(function(){
 
	CurrentType=$(this);
 	$('#typeselect div').each(function() {
 			if (CurrentType.attr('price')!=$(this).attr("price")){
 				$(this).animate({opacity:0.4},500);
 			} else {
 			 	CurrentType.animate({opacity:1},500);
 			}
 		
 	});
  	$('this').css('display','none');	
  	DevicePrice=parseInt(CurrentType.attr("price"));
 
  	TypeDefined=1;
 	typecalc();

 });
 
  $('#balon').click(function(){
  	typecalc();
  });
  
  $('#step2').keyup(function(){
  	compensa();
  });
  
  $('#order').keyup(function(){
  		name = $('#person').val();
  		telephone = $('#phone').val();
  		automobile = $('#car').val();
  		
  		if (name && telephone && automobile) { 
  			$('#sendrequest').removeAttr("disabled"); 
  		} else {
  			$('#sendrequest').attr("disabled","disabled");
  		}
  });
  
 
  $('#order').ajaxForm(function() {
  	
    $('#order').hide("normal");
	$('#oresult').show("normal");
    
  }); 


		//$.post('stations.xml', {}, function(xml){  // загрузку XML из файла example.xml
			//$(xml).find('GeoObject').each(function(){       // заполняем DOM элемент данными из XML
				//$('#xmlresult').append('To: '   + $(this).find('gml:name').text() + '<br/>')
							//.append('From: ' + $(this).find('gml:description').text() + '<br/>');
				//});
		// }, 'xml');                                     // указываем явно тип данных



 
 
});

